binfmt_aout32.c 11 KB

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