binfmt_aout32.c 11 KB

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