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