Merge branch 'master' of ssh://master.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6] / arch / sparc64 / kernel / binfmt_aout32.c
1 /*
2  *  linux/fs/binfmt_aout.c
3  *
4  *  Copyright (C) 1991, 1992, 1996  Linus Torvalds
5  *
6  *  Hacked a bit by DaveM to make it work with 32-bit SunOS
7  *  binaries on the sparc64 port.
8  */
9
10 #include <linux/module.h>
11
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/mman.h>
16 #include <linux/a.out.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/string.h>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/slab.h>
27 #include <linux/binfmts.h>
28 #include <linux/personality.h>
29 #include <linux/init.h>
30
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgalloc.h>
34 #include <asm/mmu_context.h>
35
36 static int load_aout32_binary(struct linux_binprm *, struct pt_regs * regs);
37 static int load_aout32_library(struct file*);
38 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
39
40 static struct linux_binfmt aout32_format = {
41         .module         = THIS_MODULE,
42         .load_binary    = load_aout32_binary,
43         .load_shlib     = load_aout32_library,
44         .core_dump      = aout32_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         down_write(&current->mm->mmap_sem);
55         do_brk(start, end - start);
56         up_write(&current->mm->mmap_sem);
57 }
58
59 /*
60  * These are the only things you should do on a core-file: use only these
61  * macros to write out all the necessary info.
62  */
63
64 static int dump_write(struct file *file, const void *addr, int nr)
65 {
66         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
67 }
68
69 #define DUMP_WRITE(addr, nr)    \
70         if (!dump_write(file, (void *)(addr), (nr))) \
71                 goto end_coredump;
72
73 #define DUMP_SEEK(offset) \
74 if (file->f_op->llseek) { \
75         if (file->f_op->llseek(file,(offset),0) != (offset)) \
76                 goto end_coredump; \
77 } else file->f_pos = (offset)
78
79 /*
80  * Routine writes a core dump image in the current directory.
81  * Currently only a stub-function.
82  *
83  * Note that setuid/setgid files won't make a core-dump if the uid/gid
84  * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
85  * field, which also makes sure the core-dumps won't be recursive if the
86  * dumping of the process results in another error..
87  */
88
89 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
90 {
91         mm_segment_t fs;
92         int has_dumped = 0;
93         unsigned long dump_start, dump_size;
94         struct user dump;
95 #       define START_DATA(u)    (u.u_tsize)
96 #       define START_STACK(u)   ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
97
98         fs = get_fs();
99         set_fs(KERNEL_DS);
100         has_dumped = 1;
101         current->flags |= PF_DUMPCORE;
102         strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
103         dump.signal = signr;
104         dump_thread(regs, &dump);
105
106 /* If the size of the dump file exceeds the rlimit, then see what would happen
107    if we wrote the stack, but not the data area.  */
108         if (dump.u_dsize + dump.u_ssize > limit)
109                 dump.u_dsize = 0;
110
111 /* Make sure we have enough room to write the stack and data areas. */
112         if (dump.u_ssize > limit)
113                 dump.u_ssize = 0;
114
115 /* make sure we actually have a data and stack area to dump */
116         set_fs(USER_DS);
117         if (!access_ok(VERIFY_READ, (void __user *) START_DATA(dump), dump.u_dsize))
118                 dump.u_dsize = 0;
119         if (!access_ok(VERIFY_READ, (void __user *) START_STACK(dump), dump.u_ssize))
120                 dump.u_ssize = 0;
121
122         set_fs(KERNEL_DS);
123 /* struct user */
124         DUMP_WRITE(&dump,sizeof(dump));
125 /* now we start writing out the user space info */
126         set_fs(USER_DS);
127 /* Dump the data area */
128         if (dump.u_dsize != 0) {
129                 dump_start = START_DATA(dump);
130                 dump_size = dump.u_dsize;
131                 DUMP_WRITE(dump_start,dump_size);
132         }
133 /* Now prepare to dump the stack area */
134         if (dump.u_ssize != 0) {
135                 dump_start = START_STACK(dump);
136                 dump_size = dump.u_ssize;
137                 DUMP_WRITE(dump_start,dump_size);
138         }
139 /* Finally dump the task struct.  Not be used by gdb, but could be useful */
140         set_fs(KERNEL_DS);
141         DUMP_WRITE(current,sizeof(*current));
142 end_coredump:
143         set_fs(fs);
144         return has_dumped;
145 }
146
147 /*
148  * create_aout32_tables() parses the env- and arg-strings in new user
149  * memory and creates the pointer tables from them, and puts their
150  * addresses on the "stack", returning the new stack pointer value.
151  */
152
153 static u32 __user *create_aout32_tables(char __user *p, struct linux_binprm *bprm)
154 {
155         u32 __user *argv;
156         u32 __user *envp;
157         u32 __user *sp;
158         int argc = bprm->argc;
159         int envc = bprm->envc;
160
161         sp = (u32 __user *)((-(unsigned long)sizeof(char *))&(unsigned long)p);
162
163         /* This imposes the proper stack alignment for a new process. */
164         sp = (u32 __user *) (((unsigned long) sp) & ~7);
165         if ((envc+argc+3)&1)
166                 --sp;
167
168         sp -= envc+1;
169         envp = sp;
170         sp -= argc+1;
171         argv = sp;
172         put_user(argc,--sp);
173         current->mm->arg_start = (unsigned long) p;
174         while (argc-->0) {
175                 char c;
176                 put_user(((u32)(unsigned long)(p)),argv++);
177                 do {
178                         get_user(c,p++);
179                 } while (c);
180         }
181         put_user(0,argv);
182         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
183         while (envc-->0) {
184                 char c;
185                 put_user(((u32)(unsigned long)(p)),envp++);
186                 do {
187                         get_user(c,p++);
188                 } while (c);
189         }
190         put_user(0,envp);
191         current->mm->env_end = (unsigned long) p;
192         return sp;
193 }
194
195 /*
196  * These are the functions used to load a.out style executables and shared
197  * libraries.  There is no binary dependent code anywhere else.
198  */
199
200 static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
201 {
202         struct exec ex;
203         unsigned long error;
204         unsigned long fd_offset;
205         unsigned long rlim;
206         unsigned long orig_thr_flags;
207         int retval;
208
209         ex = *((struct exec *) bprm->buf);              /* exec-header */
210         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
211              N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
212             N_TRSIZE(ex) || N_DRSIZE(ex) ||
213             bprm->file->f_path.dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
214                 return -ENOEXEC;
215         }
216
217         fd_offset = N_TXTOFF(ex);
218
219         /* Check initial limits. This avoids letting people circumvent
220          * size limits imposed on them by creating programs with large
221          * arrays in the data or bss.
222          */
223         rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
224         if (rlim >= RLIM_INFINITY)
225                 rlim = ~0;
226         if (ex.a_data + ex.a_bss > rlim)
227                 return -ENOMEM;
228
229         /* Flush all traces of the currently running executable */
230         retval = flush_old_exec(bprm);
231         if (retval)
232                 return retval;
233
234         /* OK, This is the point of no return */
235         set_personality(PER_SUNOS);
236
237         current->mm->end_code = ex.a_text +
238                 (current->mm->start_code = N_TXTADDR(ex));
239         current->mm->end_data = ex.a_data +
240                 (current->mm->start_data = N_DATADDR(ex));
241         current->mm->brk = ex.a_bss +
242                 (current->mm->start_brk = N_BSSADDR(ex));
243         current->mm->free_area_cache = current->mm->mmap_base;
244         current->mm->cached_hole_size = 0;
245
246         current->mm->mmap = NULL;
247         compute_creds(bprm);
248         current->flags &= ~PF_FORKNOEXEC;
249         if (N_MAGIC(ex) == NMAGIC) {
250                 loff_t pos = fd_offset;
251                 /* Fuck me plenty... */
252                 down_write(&current->mm->mmap_sem);     
253                 error = do_brk(N_TXTADDR(ex), ex.a_text);
254                 up_write(&current->mm->mmap_sem);
255                 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
256                           ex.a_text, &pos);
257                 down_write(&current->mm->mmap_sem);
258                 error = do_brk(N_DATADDR(ex), ex.a_data);
259                 up_write(&current->mm->mmap_sem);
260                 bprm->file->f_op->read(bprm->file, (char __user *)N_DATADDR(ex),
261                           ex.a_data, &pos);
262                 goto beyond_if;
263         }
264
265         if (N_MAGIC(ex) == OMAGIC) {
266                 loff_t pos = fd_offset;
267                 down_write(&current->mm->mmap_sem);
268                 do_brk(N_TXTADDR(ex) & PAGE_MASK,
269                         ex.a_text+ex.a_data + PAGE_SIZE - 1);
270                 up_write(&current->mm->mmap_sem);
271                 bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
272                           ex.a_text+ex.a_data, &pos);
273         } else {
274                 static unsigned long error_time;
275                 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
276                     (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
277                 {
278                         printk(KERN_NOTICE "executable not page aligned\n");
279                         error_time = jiffies;
280                 }
281
282                 if (!bprm->file->f_op->mmap) {
283                         loff_t pos = fd_offset;
284                         down_write(&current->mm->mmap_sem);
285                         do_brk(0, ex.a_text+ex.a_data);
286                         up_write(&current->mm->mmap_sem);
287                         bprm->file->f_op->read(bprm->file,
288                                   (char __user *)N_TXTADDR(ex),
289                                   ex.a_text+ex.a_data, &pos);
290                         goto beyond_if;
291                 }
292
293                 down_write(&current->mm->mmap_sem);
294                 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
295                         PROT_READ | PROT_EXEC,
296                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
297                         fd_offset);
298                 up_write(&current->mm->mmap_sem);
299
300                 if (error != N_TXTADDR(ex)) {
301                         send_sig(SIGKILL, current, 0);
302                         return error;
303                 }
304
305                 down_write(&current->mm->mmap_sem);
306                 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
307                                 PROT_READ | PROT_WRITE | PROT_EXEC,
308                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
309                                 fd_offset + ex.a_text);
310                 up_write(&current->mm->mmap_sem);
311                 if (error != N_DATADDR(ex)) {
312                         send_sig(SIGKILL, current, 0);
313                         return error;
314                 }
315         }
316 beyond_if:
317         set_binfmt(&aout32_format);
318
319         set_brk(current->mm->start_brk, current->mm->brk);
320
321         /* Make sure STACK_TOP returns the right thing.  */
322         orig_thr_flags = current_thread_info()->flags;
323         current_thread_info()->flags |= _TIF_32BIT;
324
325         retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
326         if (retval < 0) { 
327                 current_thread_info()->flags = orig_thr_flags;
328
329                 /* Someone check-me: is this error path enough? */ 
330                 send_sig(SIGKILL, current, 0); 
331                 return retval;
332         }
333
334         current->mm->start_stack =
335                 (unsigned long) create_aout32_tables((char __user *)bprm->p, bprm);
336         tsb_context_switch(current->mm);
337
338         start_thread32(regs, ex.a_entry, current->mm->start_stack);
339         if (current->ptrace & PT_PTRACED)
340                 send_sig(SIGTRAP, current, 0);
341         return 0;
342 }
343
344 /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
345 static int load_aout32_library(struct file *file)
346 {
347         struct inode * inode;
348         unsigned long bss, start_addr, len;
349         unsigned long error;
350         int retval;
351         struct exec ex;
352
353         inode = file->f_path.dentry->d_inode;
354
355         retval = -ENOEXEC;
356         error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
357         if (error != sizeof(ex))
358                 goto out;
359
360         /* We come in here for the regular a.out style of shared libraries */
361         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
362             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
363             inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
364                 goto out;
365         }
366
367         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
368             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
369                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
370                 goto out;
371         }
372
373         if (N_FLAGS(ex))
374                 goto out;
375
376         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
377            this off to get the starting address for the page */
378
379         start_addr =  ex.a_entry & 0xfffff000;
380
381         /* Now use mmap to map the library into memory. */
382         down_write(&current->mm->mmap_sem);
383         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
384                         PROT_READ | PROT_WRITE | PROT_EXEC,
385                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
386                         N_TXTOFF(ex));
387         up_write(&current->mm->mmap_sem);
388         retval = error;
389         if (error != start_addr)
390                 goto out;
391
392         len = PAGE_ALIGN(ex.a_text + ex.a_data);
393         bss = ex.a_text + ex.a_data + ex.a_bss;
394         if (bss > len) {
395                 down_write(&current->mm->mmap_sem);
396                 error = do_brk(start_addr + len, bss - len);
397                 up_write(&current->mm->mmap_sem);
398                 retval = error;
399                 if (error != start_addr + len)
400                         goto out;
401         }
402         retval = 0;
403 out:
404         return retval;
405 }
406
407 static int __init init_aout32_binfmt(void)
408 {
409         return register_binfmt(&aout32_format);
410 }
411
412 static void __exit exit_aout32_binfmt(void)
413 {
414         unregister_binfmt(&aout32_format);
415 }
416
417 module_init(init_aout32_binfmt);
418 module_exit(exit_aout32_binfmt);