binfmt_aout32.c 12 KB

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