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