Linux 2.3.0
[davej-history.git] / fs / binfmt_aout.c
bloba4ed4eb34e8075ba69663da232c09f405afbcd0e
1 /*
2 * linux/fs/binfmt_aout.c
4 * Copyright (C) 1991, 1992, 1996 Linus Torvalds
5 */
7 #include <linux/module.h>
9 #include <linux/sched.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/malloc.h>
24 #include <linux/binfmts.h>
25 #include <linux/personality.h>
26 #include <linux/init.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
32 static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
33 static int load_aout_library(int fd);
34 static int aout_core_dump(long signr, struct pt_regs * regs);
36 extern void dump_thread(struct pt_regs *, struct user *);
38 static struct linux_binfmt aout_format = {
39 #ifndef MODULE
40 NULL, NULL, load_aout_binary, load_aout_library, aout_core_dump
41 #else
42 NULL, &__this_module, load_aout_binary, load_aout_library, aout_core_dump
43 #endif
46 static void set_brk(unsigned long start, unsigned long end)
48 start = PAGE_ALIGN(start);
49 end = PAGE_ALIGN(end);
50 if (end <= start)
51 return;
52 do_mmap(NULL, start, end - start,
53 PROT_READ | PROT_WRITE | PROT_EXEC,
54 MAP_FIXED | MAP_PRIVATE, 0);
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.
62 static int dump_write(struct file *file, const void *addr, int nr)
64 int r;
65 down(&file->f_dentry->d_inode->i_sem);
66 r = file->f_op->write(file, addr, nr, &file->f_pos) == nr;
67 up(&file->f_dentry->d_inode->i_sem);
68 return r;
71 #define DUMP_WRITE(addr, nr) \
72 if (!dump_write(file, (void *)(addr), (nr))) \
73 goto close_coredump;
75 #define DUMP_SEEK(offset) \
76 if (file->f_op->llseek) { \
77 if (file->f_op->llseek(file,(offset),0) != (offset)) \
78 goto close_coredump; \
79 } else file->f_pos = (offset)
82 * Routine writes a core dump image in the current directory.
83 * Currently only a stub-function.
85 * Note that setuid/setgid files won't make a core-dump if the uid/gid
86 * changed due to the set[u|g]id. It's enforced by the "current->dumpable"
87 * field, which also makes sure the core-dumps won't be recursive if the
88 * dumping of the process results in another error..
91 static inline int
92 do_aout_core_dump(long signr, struct pt_regs * regs)
94 struct dentry * dentry = NULL;
95 struct inode * inode = NULL;
96 struct file * file;
97 mm_segment_t fs;
98 int has_dumped = 0;
99 char corefile[6+sizeof(current->comm)];
100 unsigned long dump_start, dump_size;
101 struct user dump;
102 #if defined(__alpha__)
103 # define START_DATA(u) (u.start_data)
104 #elif defined(__sparc__)
105 # define START_DATA(u) (u.u_tsize)
106 #elif defined(__i386__) || defined(__mc68000__)
107 # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
108 #endif
109 #ifdef __sparc__
110 # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
111 #else
112 # define START_STACK(u) (u.start_stack)
113 #endif
115 if (!current->dumpable || atomic_read(&current->mm->count) != 1)
116 return 0;
117 current->dumpable = 0;
119 /* See if we have enough room to write the upage. */
120 if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)
121 return 0;
122 fs = get_fs();
123 set_fs(KERNEL_DS);
124 memcpy(corefile,"core.",5);
125 #if 0
126 memcpy(corefile+5,current->comm,sizeof(current->comm));
127 #else
128 corefile[4] = '\0';
129 #endif
130 file = filp_open(corefile,O_CREAT | 2 | O_TRUNC | O_NOFOLLOW, 0600);
131 if (IS_ERR(file))
132 goto end_coredump;
133 dentry = file->f_dentry;
134 inode = dentry->d_inode;
135 if (!S_ISREG(inode->i_mode))
136 goto close_coredump;
137 if (!inode->i_op || !inode->i_op->default_file_ops)
138 goto close_coredump;
139 if (!file->f_op->write)
140 goto close_coredump;
141 has_dumped = 1;
142 current->flags |= PF_DUMPCORE;
143 strncpy(dump.u_comm, current->comm, sizeof(current->comm));
144 #ifndef __sparc__
145 dump.u_ar0 = (void *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
146 #endif
147 dump.signal = signr;
148 dump_thread(regs, &dump);
150 /* If the size of the dump file exceeds the rlimit, then see what would happen
151 if we wrote the stack, but not the data area. */
152 #ifdef __sparc__
153 if ((dump.u_dsize+dump.u_ssize) >
154 current->rlim[RLIMIT_CORE].rlim_cur)
155 dump.u_dsize = 0;
156 #else
157 if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
158 current->rlim[RLIMIT_CORE].rlim_cur)
159 dump.u_dsize = 0;
160 #endif
162 /* Make sure we have enough room to write the stack and data areas. */
163 #ifdef __sparc__
164 if ((dump.u_ssize) >
165 current->rlim[RLIMIT_CORE].rlim_cur)
166 dump.u_ssize = 0;
167 #else
168 if ((dump.u_ssize+1) * PAGE_SIZE >
169 current->rlim[RLIMIT_CORE].rlim_cur)
170 dump.u_ssize = 0;
171 #endif
173 /* make sure we actually have a data and stack area to dump */
174 set_fs(USER_DS);
175 #ifdef __sparc__
176 if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize))
177 dump.u_dsize = 0;
178 if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize))
179 dump.u_ssize = 0;
180 #else
181 if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
182 dump.u_dsize = 0;
183 if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
184 dump.u_ssize = 0;
185 #endif
187 set_fs(KERNEL_DS);
188 /* struct user */
189 DUMP_WRITE(&dump,sizeof(dump));
190 /* Now dump all of the user data. Include malloced stuff as well */
191 #ifndef __sparc__
192 DUMP_SEEK(PAGE_SIZE);
193 #endif
194 /* now we start writing out the user space info */
195 set_fs(USER_DS);
196 /* Dump the data area */
197 if (dump.u_dsize != 0) {
198 dump_start = START_DATA(dump);
199 #ifdef __sparc__
200 dump_size = dump.u_dsize;
201 #else
202 dump_size = dump.u_dsize << PAGE_SHIFT;
203 #endif
204 DUMP_WRITE(dump_start,dump_size);
206 /* Now prepare to dump the stack area */
207 if (dump.u_ssize != 0) {
208 dump_start = START_STACK(dump);
209 #ifdef __sparc__
210 dump_size = dump.u_ssize;
211 #else
212 dump_size = dump.u_ssize << PAGE_SHIFT;
213 #endif
214 DUMP_WRITE(dump_start,dump_size);
216 /* Finally dump the task struct. Not be used by gdb, but could be useful */
217 set_fs(KERNEL_DS);
218 DUMP_WRITE(current,sizeof(*current));
219 close_coredump:
220 filp_close(file, NULL);
221 end_coredump:
222 set_fs(fs);
223 return has_dumped;
226 static int
227 aout_core_dump(long signr, struct pt_regs * regs)
229 int retval;
231 MOD_INC_USE_COUNT;
232 retval = do_aout_core_dump(signr, regs);
233 MOD_DEC_USE_COUNT;
234 return retval;
238 * create_aout_tables() parses the env- and arg-strings in new user
239 * memory and creates the pointer tables from them, and puts their
240 * addresses on the "stack", returning the new stack pointer value.
242 static unsigned long * create_aout_tables(char * p, struct linux_binprm * bprm)
244 char **argv, **envp;
245 unsigned long * sp;
246 int argc = bprm->argc;
247 int envc = bprm->envc;
249 sp = (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
250 #ifdef __sparc__
251 /* This imposes the proper stack alignment for a new process. */
252 sp = (unsigned long *) (((unsigned long) sp) & ~7);
253 if ((envc+argc+3)&1) --sp;
254 #endif
255 #ifdef __alpha__
256 /* whee.. test-programs are so much fun. */
257 put_user(0, --sp);
258 put_user(0, --sp);
259 if (bprm->loader) {
260 put_user(0, --sp);
261 put_user(0x3eb, --sp);
262 put_user(bprm->loader, --sp);
263 put_user(0x3ea, --sp);
265 put_user(bprm->exec, --sp);
266 put_user(0x3e9, --sp);
267 #endif
268 sp -= envc+1;
269 envp = (char **) sp;
270 sp -= argc+1;
271 argv = (char **) sp;
272 #if defined(__i386__) || defined(__mc68000__)
273 put_user((unsigned long) envp,--sp);
274 put_user((unsigned long) argv,--sp);
275 #endif
276 put_user(argc,--sp);
277 current->mm->arg_start = (unsigned long) p;
278 while (argc-->0) {
279 char c;
280 put_user(p,argv++);
281 do {
282 get_user(c,p++);
283 } while (c);
285 put_user(NULL,argv);
286 current->mm->arg_end = current->mm->env_start = (unsigned long) p;
287 while (envc-->0) {
288 char c;
289 put_user(p,envp++);
290 do {
291 get_user(c,p++);
292 } while (c);
294 put_user(NULL,envp);
295 current->mm->env_end = (unsigned long) p;
296 return sp;
300 * These are the functions used to load a.out style executables and shared
301 * libraries. There is no binary dependent code anywhere else.
304 static inline int do_load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
306 struct exec ex;
307 struct file * file;
308 int fd;
309 unsigned long error;
310 unsigned long p = bprm->p;
311 unsigned long fd_offset;
312 unsigned long rlim;
313 int retval;
315 ex = *((struct exec *) bprm->buf); /* exec-header */
316 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
317 N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
318 N_TRSIZE(ex) || N_DRSIZE(ex) ||
319 bprm->dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
320 return -ENOEXEC;
323 fd_offset = N_TXTOFF(ex);
325 #ifdef __i386__
326 if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
327 printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
328 return -ENOEXEC;
331 if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
332 bprm->dentry->d_inode->i_op &&
333 bprm->dentry->d_inode->i_op->bmap &&
334 (fd_offset < bprm->dentry->d_inode->i_sb->s_blocksize)) {
335 printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
336 return -ENOEXEC;
338 #endif
340 /* Check initial limits. This avoids letting people circumvent
341 * size limits imposed on them by creating programs with large
342 * arrays in the data or bss.
344 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
345 if (rlim >= RLIM_INFINITY)
346 rlim = ~0;
347 if (ex.a_data + ex.a_bss > rlim)
348 return -ENOMEM;
350 /* Flush all traces of the currently running executable */
351 retval = flush_old_exec(bprm);
352 if (retval)
353 return retval;
355 /* OK, This is the point of no return */
356 current->personality = PER_LINUX;
358 #if defined(__sparc__) && !defined(__sparc_v9__)
359 memcpy(&current->tss.core_exec, &ex, sizeof(struct exec));
360 #endif
362 current->mm->end_code = ex.a_text +
363 (current->mm->start_code = N_TXTADDR(ex));
364 current->mm->end_data = ex.a_data +
365 (current->mm->start_data = N_DATADDR(ex));
366 current->mm->brk = ex.a_bss +
367 (current->mm->start_brk = N_BSSADDR(ex));
369 current->mm->rss = 0;
370 current->mm->mmap = NULL;
371 compute_creds(bprm);
372 current->flags &= ~PF_FORKNOEXEC;
373 #ifdef __sparc__
374 if (N_MAGIC(ex) == NMAGIC) {
375 /* Fuck me plenty... */
376 error = do_mmap(NULL, N_TXTADDR(ex), ex.a_text,
377 PROT_READ|PROT_WRITE|PROT_EXEC,
378 MAP_FIXED|MAP_PRIVATE, 0);
379 read_exec(bprm->dentry, fd_offset, (char *) N_TXTADDR(ex),
380 ex.a_text, 0);
381 error = do_mmap(NULL, N_DATADDR(ex), ex.a_data,
382 PROT_READ|PROT_WRITE|PROT_EXEC,
383 MAP_FIXED|MAP_PRIVATE, 0);
384 read_exec(bprm->dentry, fd_offset + ex.a_text, (char *) N_DATADDR(ex),
385 ex.a_data, 0);
386 goto beyond_if;
388 #endif
390 if (N_MAGIC(ex) == OMAGIC) {
391 #if defined(__alpha__) || defined(__sparc__)
392 do_mmap(NULL, N_TXTADDR(ex) & PAGE_MASK,
393 ex.a_text+ex.a_data + PAGE_SIZE - 1,
394 PROT_READ|PROT_WRITE|PROT_EXEC,
395 MAP_FIXED|MAP_PRIVATE, 0);
396 read_exec(bprm->dentry, fd_offset, (char *) N_TXTADDR(ex),
397 ex.a_text+ex.a_data, 0);
398 #else
399 do_mmap(NULL, 0, ex.a_text+ex.a_data,
400 PROT_READ|PROT_WRITE|PROT_EXEC,
401 MAP_FIXED|MAP_PRIVATE, 0);
402 read_exec(bprm->dentry, 32, (char *) 0, ex.a_text+ex.a_data, 0);
403 #endif
404 flush_icache_range((unsigned long) 0,
405 (unsigned long) ex.a_text+ex.a_data);
406 } else {
407 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
408 (N_MAGIC(ex) != NMAGIC))
409 printk(KERN_NOTICE "executable not page aligned\n");
411 fd = open_dentry(bprm->dentry, O_RDONLY);
412 if (fd < 0)
413 return fd;
414 file = fcheck(fd);
416 if (!file->f_op || !file->f_op->mmap) {
417 sys_close(fd);
418 do_mmap(NULL, 0, ex.a_text+ex.a_data,
419 PROT_READ|PROT_WRITE|PROT_EXEC,
420 MAP_FIXED|MAP_PRIVATE, 0);
421 read_exec(bprm->dentry, fd_offset,
422 (char *) N_TXTADDR(ex), ex.a_text+ex.a_data, 0);
423 flush_icache_range((unsigned long) N_TXTADDR(ex),
424 (unsigned long) N_TXTADDR(ex) +
425 ex.a_text+ex.a_data);
426 goto beyond_if;
429 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
430 PROT_READ | PROT_EXEC,
431 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
432 fd_offset);
434 if (error != N_TXTADDR(ex)) {
435 sys_close(fd);
436 send_sig(SIGKILL, current, 0);
437 return error;
440 error = do_mmap(file, N_DATADDR(ex), ex.a_data,
441 PROT_READ | PROT_WRITE | PROT_EXEC,
442 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
443 fd_offset + ex.a_text);
444 sys_close(fd);
445 if (error != N_DATADDR(ex)) {
446 send_sig(SIGKILL, current, 0);
447 return error;
450 beyond_if:
451 if (current->exec_domain && current->exec_domain->module)
452 __MOD_DEC_USE_COUNT(current->exec_domain->module);
453 if (current->binfmt && current->binfmt->module)
454 __MOD_DEC_USE_COUNT(current->binfmt->module);
455 current->exec_domain = lookup_exec_domain(current->personality);
456 current->binfmt = &aout_format;
457 if (current->exec_domain && current->exec_domain->module)
458 __MOD_INC_USE_COUNT(current->exec_domain->module);
459 if (current->binfmt && current->binfmt->module)
460 __MOD_INC_USE_COUNT(current->binfmt->module);
462 set_brk(current->mm->start_brk, current->mm->brk);
464 p = setup_arg_pages(p, bprm);
466 p = (unsigned long) create_aout_tables((char *)p, bprm);
467 current->mm->start_stack = p;
468 #ifdef __alpha__
469 regs->gp = ex.a_gpvalue;
470 #endif
471 start_thread(regs, ex.a_entry, p);
472 if (current->flags & PF_PTRACED)
473 send_sig(SIGTRAP, current, 0);
474 return 0;
478 static int
479 load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
481 int retval;
483 MOD_INC_USE_COUNT;
484 retval = do_load_aout_binary(bprm, regs);
485 MOD_DEC_USE_COUNT;
486 return retval;
489 static inline int
490 do_load_aout_library(int fd)
492 struct file * file;
493 struct inode * inode;
494 unsigned long bss, start_addr, len;
495 unsigned long error;
496 int retval;
497 loff_t offset = 0;
498 struct exec ex;
500 retval = -EACCES;
501 file = fget(fd);
502 if (!file)
503 goto out;
504 if (!file->f_op)
505 goto out_putf;
506 inode = file->f_dentry->d_inode;
508 retval = -ENOEXEC;
509 /* N.B. Save current fs? */
510 set_fs(KERNEL_DS);
511 error = file->f_op->read(file, (char *) &ex, sizeof(ex), &offset);
512 set_fs(USER_DS);
513 if (error != sizeof(ex))
514 goto out_putf;
516 /* We come in here for the regular a.out style of shared libraries */
517 if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
518 N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
519 inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
520 goto out_putf;
523 if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
524 (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
525 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
526 goto out_putf;
529 if (N_FLAGS(ex))
530 goto out_putf;
532 /* For QMAGIC, the starting address is 0x20 into the page. We mask
533 this off to get the starting address for the page */
535 start_addr = ex.a_entry & 0xfffff000;
537 /* Now use mmap to map the library into memory. */
538 error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
539 PROT_READ | PROT_WRITE | PROT_EXEC,
540 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
541 N_TXTOFF(ex));
542 retval = error;
543 if (error != start_addr)
544 goto out_putf;
546 len = PAGE_ALIGN(ex.a_text + ex.a_data);
547 bss = ex.a_text + ex.a_data + ex.a_bss;
548 if (bss > len) {
549 error = do_mmap(NULL, start_addr + len, bss - len,
550 PROT_READ | PROT_WRITE | PROT_EXEC,
551 MAP_PRIVATE | MAP_FIXED, 0);
552 retval = error;
553 if (error != start_addr + len)
554 goto out_putf;
556 retval = 0;
558 out_putf:
559 fput(file);
560 out:
561 return retval;
564 static int
565 load_aout_library(int fd)
567 int retval;
569 MOD_INC_USE_COUNT;
570 retval = do_load_aout_library(fd);
571 MOD_DEC_USE_COUNT;
572 return retval;
576 int __init init_aout_binfmt(void)
578 return register_binfmt(&aout_format);
581 #ifdef MODULE
582 int init_module(void) {
583 return init_aout_binfmt();
586 void cleanup_module( void) {
587 unregister_binfmt(&aout_format);
589 #endif