binfmt_aout32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. extern void dump_thread(struct pt_regs *, struct user *);
  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. set_mm_counter(current->mm, rss, 0);
  213. current->mm->mmap = NULL;
  214. compute_creds(bprm);
  215. current->flags &= ~PF_FORKNOEXEC;
  216. if (N_MAGIC(ex) == NMAGIC) {
  217. loff_t pos = fd_offset;
  218. /* Fuck me plenty... */
  219. down_write(&current->mm->mmap_sem);
  220. error = do_brk(N_TXTADDR(ex), ex.a_text);
  221. up_write(&current->mm->mmap_sem);
  222. bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
  223. ex.a_text, &pos);
  224. down_write(&current->mm->mmap_sem);
  225. error = do_brk(N_DATADDR(ex), ex.a_data);
  226. up_write(&current->mm->mmap_sem);
  227. bprm->file->f_op->read(bprm->file, (char __user *)N_DATADDR(ex),
  228. ex.a_data, &pos);
  229. goto beyond_if;
  230. }
  231. if (N_MAGIC(ex) == OMAGIC) {
  232. loff_t pos = fd_offset;
  233. down_write(&current->mm->mmap_sem);
  234. do_brk(N_TXTADDR(ex) & PAGE_MASK,
  235. ex.a_text+ex.a_data + PAGE_SIZE - 1);
  236. up_write(&current->mm->mmap_sem);
  237. bprm->file->f_op->read(bprm->file, (char __user *)N_TXTADDR(ex),
  238. ex.a_text+ex.a_data, &pos);
  239. } else {
  240. static unsigned long error_time;
  241. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  242. (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
  243. {
  244. printk(KERN_NOTICE "executable not page aligned\n");
  245. error_time = jiffies;
  246. }
  247. if (!bprm->file->f_op->mmap) {
  248. loff_t pos = fd_offset;
  249. down_write(&current->mm->mmap_sem);
  250. do_brk(0, ex.a_text+ex.a_data);
  251. up_write(&current->mm->mmap_sem);
  252. bprm->file->f_op->read(bprm->file,
  253. (char __user *)N_TXTADDR(ex),
  254. ex.a_text+ex.a_data, &pos);
  255. goto beyond_if;
  256. }
  257. down_write(&current->mm->mmap_sem);
  258. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  259. PROT_READ | PROT_EXEC,
  260. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  261. fd_offset);
  262. up_write(&current->mm->mmap_sem);
  263. if (error != N_TXTADDR(ex)) {
  264. send_sig(SIGKILL, current, 0);
  265. return error;
  266. }
  267. down_write(&current->mm->mmap_sem);
  268. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  269. PROT_READ | PROT_WRITE | PROT_EXEC,
  270. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  271. fd_offset + ex.a_text);
  272. up_write(&current->mm->mmap_sem);
  273. if (error != N_DATADDR(ex)) {
  274. send_sig(SIGKILL, current, 0);
  275. return error;
  276. }
  277. }
  278. beyond_if:
  279. set_binfmt(&aout32_format);
  280. set_brk(current->mm->start_brk, current->mm->brk);
  281. /* Make sure STACK_TOP returns the right thing. */
  282. orig_thr_flags = current_thread_info()->flags;
  283. current_thread_info()->flags |= _TIF_32BIT;
  284. retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  285. if (retval < 0) {
  286. current_thread_info()->flags = orig_thr_flags;
  287. /* Someone check-me: is this error path enough? */
  288. send_sig(SIGKILL, current, 0);
  289. return retval;
  290. }
  291. current->mm->start_stack =
  292. (unsigned long) create_aout32_tables((char __user *)bprm->p, bprm);
  293. if (!(orig_thr_flags & _TIF_32BIT)) {
  294. unsigned long pgd_cache = get_pgd_cache(current->mm->pgd);
  295. __asm__ __volatile__("stxa\t%0, [%1] %2\n\t"
  296. "membar #Sync"
  297. : /* no outputs */
  298. : "r" (pgd_cache),
  299. "r" (TSB_REG), "i" (ASI_DMMU));
  300. }
  301. start_thread32(regs, ex.a_entry, current->mm->start_stack);
  302. if (current->ptrace & PT_PTRACED)
  303. send_sig(SIGTRAP, current, 0);
  304. return 0;
  305. }
  306. /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
  307. static int load_aout32_library(struct file *file)
  308. {
  309. struct inode * inode;
  310. unsigned long bss, start_addr, len;
  311. unsigned long error;
  312. int retval;
  313. struct exec ex;
  314. inode = file->f_dentry->d_inode;
  315. retval = -ENOEXEC;
  316. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  317. if (error != sizeof(ex))
  318. goto out;
  319. /* We come in here for the regular a.out style of shared libraries */
  320. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  321. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  322. inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  323. goto out;
  324. }
  325. if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
  326. (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
  327. printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
  328. goto out;
  329. }
  330. if (N_FLAGS(ex))
  331. goto out;
  332. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  333. this off to get the starting address for the page */
  334. start_addr = ex.a_entry & 0xfffff000;
  335. /* Now use mmap to map the library into memory. */
  336. down_write(&current->mm->mmap_sem);
  337. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  338. PROT_READ | PROT_WRITE | PROT_EXEC,
  339. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  340. N_TXTOFF(ex));
  341. up_write(&current->mm->mmap_sem);
  342. retval = error;
  343. if (error != start_addr)
  344. goto out;
  345. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  346. bss = ex.a_text + ex.a_data + ex.a_bss;
  347. if (bss > len) {
  348. down_write(&current->mm->mmap_sem);
  349. error = do_brk(start_addr + len, bss - len);
  350. up_write(&current->mm->mmap_sem);
  351. retval = error;
  352. if (error != start_addr + len)
  353. goto out;
  354. }
  355. retval = 0;
  356. out:
  357. return retval;
  358. }
  359. static int __init init_aout32_binfmt(void)
  360. {
  361. return register_binfmt(&aout32_format);
  362. }
  363. static void __exit exit_aout32_binfmt(void)
  364. {
  365. unregister_binfmt(&aout32_format);
  366. }
  367. module_init(init_aout32_binfmt);
  368. module_exit(exit_aout32_binfmt);