RHEL4/fs/binfmt_aout.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/binfmt_aout.c
   3 *
   4 *  Copyright (C) 1991, 1992, 1996  Linus Torvalds
   5 */
   6
   7#include <linux/module.h>
   8
   9#include <linux/time.h>
  10#include <linux/kernel.h>
  11#include <linux/mm.h>
  12#include <linux/mman.h>
  13#include <linux/a.out.h>
  14#include <linux/errno.h>
  15#include <linux/signal.h>
  16#include <linux/string.h>
  17#include <linux/fs.h>
  18#include <linux/file.h>
  19#include <linux/stat.h>
  20#include <linux/fcntl.h>
  21#include <linux/ptrace.h>
  22#include <linux/user.h>
  23#include <linux/slab.h>
  24#include <linux/binfmts.h>
  25#include <linux/personality.h>
  26#include <linux/init.h>
  27
  28#include <asm/system.h>
  29#include <asm/uaccess.h>
  30#include <asm/cacheflush.h>
  31
  32static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
  33static int load_aout_library(struct file*);
  34static int aout_core_dump(long signr, struct pt_regs * regs, struct file *file);
  35
  36extern void dump_thread(struct pt_regs *, struct user *);
  37
  38static struct linux_binfmt aout_format = {
  39        .module         = THIS_MODULE,
  40        .load_binary    = load_aout_binary,
  41        .load_shlib     = load_aout_library,
  42        .core_dump      = aout_core_dump,
  43        .min_coredump   = PAGE_SIZE
  44};
  45
  46#define BAD_ADDR(x)     ((unsigned long)(x) >= TASK_SIZE)
  47
  48static int set_brk(unsigned long start, unsigned long end)
  49{
  50        start = PAGE_ALIGN(start);
  51        end = PAGE_ALIGN(end);
  52        if (end > start) {
  53                unsigned long addr = do_brk_locked(start, end - start);
  54                if (BAD_ADDR(addr))
  55                        return addr;
  56        }
  57        return 0;
  58}
  59
  60/*
  61 * These are the only things you should do on a core-file: use only these
  62 * macros to write out all the necessary info.
  63 */
  64
  65static int dump_write(struct file *file, const void *addr, int nr)
  66{
  67        return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  68}
  69
  70#define DUMP_WRITE(addr, nr)    \
  71        if (!dump_write(file, (void *)(addr), (nr))) \
  72                goto end_coredump;
  73
  74#define DUMP_SEEK(offset) \
  75if (file->f_op->llseek) { \
  76        if (file->f_op->llseek(file,(offset),0) != (offset)) \
  77                goto end_coredump; \
  78} else file->f_pos = (offset)
  79
  80/*
  81 * Routine writes a core dump image in the current directory.
  82 * Currently only a stub-function.
  83 *
  84 * Note that setuid/setgid files won't make a core-dump if the uid/gid
  85 * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  86 * field, which also makes sure the core-dumps won't be recursive if the
  87 * dumping of the process results in another error..
  88 */
  89
  90static int aout_core_dump(long signr, struct pt_regs * regs, struct file *file)
  91{
  92        mm_segment_t fs;
  93        int has_dumped = 0;
  94        unsigned long dump_start, dump_size;
  95        struct user dump;
  96#if defined(__alpha__)
  97#       define START_DATA(u)    (u.start_data)
  98#elif defined(__arm__)
  99#       define START_DATA(u)    ((u.u_tsize << PAGE_SHIFT) + u.start_code)
 100#elif defined(__sparc__)
 101#       define START_DATA(u)    (u.u_tsize)
 102#elif defined(__i386__) || defined(__mc68000__) || defined(__arch_um__)
 103#       define START_DATA(u)    (u.u_tsize << PAGE_SHIFT)
 104#endif
 105#ifdef __sparc__
 106#       define START_STACK(u)   ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
 107#else
 108#       define START_STACK(u)   (u.start_stack)
 109#endif
 110
 111        fs = get_fs();
 112        set_fs(KERNEL_DS);
 113        has_dumped = 1;
 114        current->flags |= PF_DUMPCORE;
 115        strncpy(dump.u_comm, current->comm, sizeof(current->comm));
 116#ifndef __sparc__
 117        dump.u_ar0 = (void *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
 118#endif
 119        dump.signal = signr;
 120        dump_thread(regs, &dump);
 121
 122/* If the size of the dump file exceeds the rlimit, then see what would happen
 123   if we wrote the stack, but not the data area.  */
 124#ifdef __sparc__
 125        if ((dump.u_dsize+dump.u_ssize) >
 126            current->rlim[RLIMIT_CORE].rlim_cur)
 127                dump.u_dsize = 0;
 128#else
 129        if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
 130            current->rlim[RLIMIT_CORE].rlim_cur)
 131                dump.u_dsize = 0;
 132#endif
 133
 134/* Make sure we have enough room to write the stack and data areas. */
 135#ifdef __sparc__
 136        if ((dump.u_ssize) >
 137            current->rlim[RLIMIT_CORE].rlim_cur)
 138                dump.u_ssize = 0;
 139#else
 140        if ((dump.u_ssize+1) * PAGE_SIZE >
 141            current->rlim[RLIMIT_CORE].rlim_cur)
 142                dump.u_ssize = 0;
 143#endif
 144
 145/* make sure we actually have a data and stack area to dump */
 146        set_fs(USER_DS);
 147#ifdef __sparc__
 148        if (verify_area(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize))
 149                dump.u_dsize = 0;
 150        if (verify_area(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize))
 151                dump.u_ssize = 0;
 152#else
 153        if (verify_area(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
 154                dump.u_dsize = 0;
 155        if (verify_area(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
 156                dump.u_ssize = 0;
 157#endif
 158
 159        set_fs(KERNEL_DS);
 160/* struct user */
 161        DUMP_WRITE(&dump,sizeof(dump));
 162/* Now dump all of the user data.  Include malloced stuff as well */
 163#ifndef __sparc__
 164        DUMP_SEEK(PAGE_SIZE);
 165#endif
 166/* now we start writing out the user space info */
 167        set_fs(USER_DS);
 168/* Dump the data area */
 169        if (dump.u_dsize != 0) {
 170                dump_start = START_DATA(dump);
 171#ifdef __sparc__
 172                dump_size = dump.u_dsize;
 173#else
 174                dump_size = dump.u_dsize << PAGE_SHIFT;
 175#endif
 176                DUMP_WRITE(dump_start,dump_size);
 177        }
 178/* Now prepare to dump the stack area */
 179        if (dump.u_ssize != 0) {
 180                dump_start = START_STACK(dump);
 181#ifdef __sparc__
 182                dump_size = dump.u_ssize;
 183#else
 184                dump_size = dump.u_ssize << PAGE_SHIFT;
 185#endif
 186                DUMP_WRITE(dump_start,dump_size);
 187        }
 188/* Finally dump the task struct.  Not be used by gdb, but could be useful */
 189        set_fs(KERNEL_DS);
 190        DUMP_WRITE(current,sizeof(*current));
 191end_coredump:
 192        set_fs(fs);
 193        return has_dumped;
 194}
 195
 196/*
 197 * create_aout_tables() parses the env- and arg-strings in new user
 198 * memory and creates the pointer tables from them, and puts their
 199 * addresses on the "stack", returning the new stack pointer value.
 200 */
 201static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
 202{
 203        char __user * __user *argv;
 204        char __user * __user *envp;
 205        unsigned long __user *sp;
 206        int argc = bprm->argc;
 207        int envc = bprm->envc;
 208
 209        sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
 210#ifdef __sparc__
 211        /* This imposes the proper stack alignment for a new process. */
 212        sp = (void __user *) (((unsigned long) sp) & ~7);
 213        if ((envc+argc+3)&1) --sp;
 214#endif
 215#ifdef __alpha__
 216/* whee.. test-programs are so much fun. */
 217        put_user(0, --sp);
 218        put_user(0, --sp);
 219        if (bprm->loader) {
 220                put_user(0, --sp);
 221                put_user(0x3eb, --sp);
 222                put_user(bprm->loader, --sp);
 223                put_user(0x3ea, --sp);
 224        }
 225        put_user(bprm->exec, --sp);
 226        put_user(0x3e9, --sp);
 227#endif
 228        sp -= envc+1;
 229        envp = (char __user * __user *) sp;
 230        sp -= argc+1;
 231        argv = (char __user * __user *) sp;
 232#if defined(__i386__) || defined(__mc68000__) || defined(__arm__) || defined(__arch_um__)
 233        put_user((unsigned long) envp,--sp);
 234        put_user((unsigned long) argv,--sp);
 235#endif
 236        put_user(argc,--sp);
 237        current->mm->arg_start = (unsigned long) p;
 238        while (argc-->0) {
 239                char c;
 240                put_user(p,argv++);
 241                do {
 242                        get_user(c,p++);
 243                } while (c);
 244        }
 245        put_user(NULL,argv);
 246        current->mm->arg_end = current->mm->env_start = (unsigned long) p;
 247        while (envc-->0) {
 248                char c;
 249                put_user(p,envp++);
 250                do {
 251                        get_user(c,p++);
 252                } while (c);
 253        }
 254        put_user(NULL,envp);
 255        current->mm->env_end = (unsigned long) p;
 256        return sp;
 257}
 258
 259/*
 260 * These are the functions used to load a.out style executables and shared
 261 * libraries.  There is no binary dependent code anywhere else.
 262 */
 263
 264static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
 265{
 266        struct exec ex;
 267        unsigned long error;
 268        unsigned long fd_offset;
 269        unsigned long rlim;
 270        int retval;
 271
 272        ex = *((struct exec *) bprm->buf);              /* exec-header */
 273        if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
 274             N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
 275            N_TRSIZE(ex) || N_DRSIZE(ex) ||
 276            i_size_read(bprm->file->f_dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 277                return -ENOEXEC;
 278        }
 279
 280        fd_offset = N_TXTOFF(ex);
 281
 282        /* Check initial limits. This avoids letting people circumvent
 283         * size limits imposed on them by creating programs with large
 284         * arrays in the data or bss.
 285         */
 286        rlim = current->rlim[RLIMIT_DATA].rlim_cur;
 287        if (rlim >= RLIM_INFINITY)
 288                rlim = ~0;
 289        if (ex.a_data + ex.a_bss > rlim)
 290                return -ENOMEM;
 291
 292        /* Flush all traces of the currently running executable */
 293        retval = flush_old_exec(bprm);
 294        if (retval)
 295                return retval;
 296
 297        /* OK, This is the point of no return */
 298#if defined(__alpha__)
 299        SET_AOUT_PERSONALITY(bprm, ex);
 300#elif defined(__sparc__)
 301        set_personality(PER_SUNOS);
 302#if !defined(__sparc_v9__)
 303        memcpy(&current->thread.core_exec, &ex, sizeof(struct exec));
 304#endif
 305#else
 306        set_personality(PER_LINUX);
 307#endif
 308
 309        current->mm->end_code = ex.a_text +
 310                (current->mm->start_code = N_TXTADDR(ex));
 311        current->mm->end_data = ex.a_data +
 312                (current->mm->start_data = N_DATADDR(ex));
 313        current->mm->brk = ex.a_bss +
 314                (current->mm->start_brk = N_BSSADDR(ex));
 315        current->mm->free_area_cache = current->mm->mmap_base;
 316
 317        current->mm->rss = 0;
 318        current->mm->mmap = NULL;
 319        compute_creds(bprm);
 320        current->flags &= ~PF_FORKNOEXEC;
 321#ifdef __sparc__
 322        if (N_MAGIC(ex) == NMAGIC) {
 323                loff_t pos = fd_offset;
 324                /* Fuck me plenty... */
 325                /* <AOL></AOL> */
 326                error = do_brk_locked(N_TXTADDR(ex), ex.a_text);
 327                bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
 328                          ex.a_text, &pos);
 329                error = do_brk_locked(N_DATADDR(ex), ex.a_data);
 330                bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
 331                          ex.a_data, &pos);
 332                goto beyond_if;
 333        }
 334#endif
 335
 336        if (N_MAGIC(ex) == OMAGIC) {
 337                unsigned long text_addr, map_size;
 338                loff_t pos;
 339
 340                text_addr = N_TXTADDR(ex);
 341
 342#if defined(__alpha__) || defined(__sparc__)
 343                pos = fd_offset;
 344                map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
 345#else
 346                pos = 32;
 347                map_size = ex.a_text+ex.a_data;
 348#endif
 349
 350                error = do_brk_locked(text_addr & PAGE_MASK, map_size);
 351                if (error != (text_addr & PAGE_MASK)) {
 352                        send_sig(SIGKILL, current, 0);
 353                        return error;
 354                }
 355
 356                error = bprm->file->f_op->read(bprm->file,
 357                          (char __user *)text_addr,
 358                          ex.a_text+ex.a_data, &pos);
 359                if ((signed long)error < 0) {
 360                        send_sig(SIGKILL, current, 0);
 361                        return error;
 362                }
 363                         
 364                flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
 365        } else {
 366                static unsigned long error_time, error_time2;
 367                if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
 368                    (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time2) > 5*HZ)
 369                {
 370                        printk(KERN_NOTICE "executable not page aligned\n");
 371                        error_time2 = jiffies;
 372                }
 373
 374                if ((fd_offset & ~PAGE_MASK) != 0 &&
 375                    (jiffies-error_time) > 5*HZ)
 376                {
 377                        printk(KERN_WARNING 
 378                               "fd_offset is not page aligned. Please convert program: %s\n",
 379                               bprm->file->f_dentry->d_name.name);
 380                        error_time = jiffies;
 381                }
 382
 383                if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
 384                        loff_t pos = fd_offset;
 385                        do_brk_locked(N_TXTADDR(ex), ex.a_text+ex.a_data);
 386                        bprm->file->f_op->read(bprm->file,
 387                                        (char __user *)N_TXTADDR(ex),
 388                                        ex.a_text+ex.a_data, &pos);
 389                        flush_icache_range((unsigned long) N_TXTADDR(ex),
 390                                           (unsigned long) N_TXTADDR(ex) +
 391                                           ex.a_text+ex.a_data);
 392                        goto beyond_if;
 393                }
 394
 395                down_write(&current->mm->mmap_sem);
 396                error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
 397                        PROT_READ | PROT_EXEC,
 398                        MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 399                        fd_offset);
 400                up_write(&current->mm->mmap_sem);
 401
 402                if (error != N_TXTADDR(ex)) {
 403                        send_sig(SIGKILL, current, 0);
 404                        return error;
 405                }
 406
 407                down_write(&current->mm->mmap_sem);
 408                error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
 409                                PROT_READ | PROT_WRITE | PROT_EXEC,
 410                                MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 411                                fd_offset + ex.a_text);
 412                up_write(&current->mm->mmap_sem);
 413                if (error != N_DATADDR(ex)) {
 414                        send_sig(SIGKILL, current, 0);
 415                        return error;
 416                }
 417        }
 418beyond_if:
 419        set_binfmt(&aout_format);
 420
 421        retval = set_brk(current->mm->start_brk, current->mm->brk);
 422        if (retval < 0) {
 423                send_sig(SIGKILL, current, 0);
 424                return retval;
 425        }
 426
 427        retval = setup_arg_pages(bprm, EXSTACK_DEFAULT);
 428        if (retval < 0) { 
 429                /* Someone check-me: is this error path enough? */ 
 430                send_sig(SIGKILL, current, 0); 
 431                return retval;
 432        }
 433
 434        current->mm->start_stack =
 435                (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
 436#ifdef __alpha__
 437        regs->gp = ex.a_gpvalue;
 438#endif
 439        start_thread(regs, ex.a_entry, current->mm->start_stack);
 440        if (unlikely(current->ptrace & PT_PTRACED)) {
 441                if (current->ptrace & PT_TRACE_EXEC)
 442                        ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
 443                else
 444                        send_sig(SIGTRAP, current, 0);
 445        }
 446        return 0;
 447}
 448
 449static int load_aout_library(struct file *file)
 450{
 451        struct inode * inode;
 452        unsigned long bss, start_addr, len;
 453        unsigned long error;
 454        int retval;
 455        struct exec ex;
 456
 457        inode = file->f_dentry->d_inode;
 458
 459        retval = -ENOEXEC;
 460        error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
 461        if (error != sizeof(ex))
 462                goto out;
 463
 464        /* We come in here for the regular a.out style of shared libraries */
 465        if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
 466            N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
 467            i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 468                goto out;
 469        }
 470
 471        if (N_FLAGS(ex))
 472                goto out;
 473
 474        /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
 475           this off to get the starting address for the page */
 476
 477        start_addr =  ex.a_entry & 0xfffff000;
 478
 479        if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
 480                static unsigned long error_time;
 481                loff_t pos = N_TXTOFF(ex);
 482
 483                if ((jiffies-error_time) > 5*HZ)
 484                {
 485                        printk(KERN_WARNING 
 486                               "N_TXTOFF is not page aligned. Please convert library: %s\n",
 487                               file->f_dentry->d_name.name);
 488                        error_time = jiffies;
 489                }
 490
 491                do_brk_locked(start_addr, ex.a_text + ex.a_data + ex.a_bss);
 492                
 493                file->f_op->read(file, (char __user *)start_addr,
 494                        ex.a_text + ex.a_data, &pos);
 495                flush_icache_range((unsigned long) start_addr,
 496                                   (unsigned long) start_addr + ex.a_text + ex.a_data);
 497
 498                retval = 0;
 499                goto out;
 500        }
 501        /* Now use mmap to map the library into memory. */
 502        down_write(&current->mm->mmap_sem);
 503        error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
 504                        PROT_READ | PROT_WRITE | PROT_EXEC,
 505                        MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
 506                        N_TXTOFF(ex));
 507        up_write(&current->mm->mmap_sem);
 508        retval = error;
 509        if (error != start_addr)
 510                goto out;
 511
 512        len = PAGE_ALIGN(ex.a_text + ex.a_data);
 513        bss = ex.a_text + ex.a_data + ex.a_bss;
 514        if (bss > len) {
 515                error = do_brk_locked(start_addr + len, bss - len);
 516                retval = error;
 517                if (error != start_addr + len)
 518                        goto out;
 519        }
 520        retval = 0;
 521out:
 522        return retval;
 523}
 524
 525static int __init init_aout_binfmt(void)
 526{
 527        return register_binfmt(&aout_format);
 528}
 529
 530static void __exit exit_aout_binfmt(void)
 531{
 532        unregister_binfmt(&aout_format);
 533}
 534
 535core_initcall(init_aout_binfmt);
 536module_exit(exit_aout_binfmt);
 537MODULE_LICENSE("GPL");
 538