binfmt_aout32.c 11 KB

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