binfmt_aout.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * linux/fs/binfmt_aout.c
  3. *
  4. * Copyright (C) 1991, 1992, 1996 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/time.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/mman.h>
  11. #include <linux/a.out.h>
  12. #include <linux/errno.h>
  13. #include <linux/signal.h>
  14. #include <linux/string.h>
  15. #include <linux/fs.h>
  16. #include <linux/file.h>
  17. #include <linux/stat.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/user.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/personality.h>
  23. #include <linux/init.h>
  24. #include <linux/coredump.h>
  25. #include <linux/slab.h>
  26. #include <asm/system.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/a.out-core.h>
  30. static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
  31. static int load_aout_library(struct file*);
  32. static int aout_core_dump(struct coredump_params *cprm);
  33. static struct linux_binfmt aout_format = {
  34. .module = THIS_MODULE,
  35. .load_binary = load_aout_binary,
  36. .load_shlib = load_aout_library,
  37. .core_dump = aout_core_dump,
  38. .min_coredump = PAGE_SIZE
  39. };
  40. #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
  41. static int set_brk(unsigned long start, unsigned long end)
  42. {
  43. start = PAGE_ALIGN(start);
  44. end = PAGE_ALIGN(end);
  45. if (end > start) {
  46. unsigned long addr;
  47. down_write(&current->mm->mmap_sem);
  48. addr = do_brk(start, end - start);
  49. up_write(&current->mm->mmap_sem);
  50. if (BAD_ADDR(addr))
  51. return addr;
  52. }
  53. return 0;
  54. }
  55. /*
  56. * Routine writes a core dump image in the current directory.
  57. * Currently only a stub-function.
  58. *
  59. * Note that setuid/setgid files won't make a core-dump if the uid/gid
  60. * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  61. * field, which also makes sure the core-dumps won't be recursive if the
  62. * dumping of the process results in another error..
  63. */
  64. static int aout_core_dump(struct coredump_params *cprm)
  65. {
  66. struct file *file = cprm->file;
  67. mm_segment_t fs;
  68. int has_dumped = 0;
  69. void __user *dump_start;
  70. int dump_size;
  71. struct user dump;
  72. #ifdef __alpha__
  73. # define START_DATA(u) ((void __user *)u.start_data)
  74. #else
  75. # define START_DATA(u) ((void __user *)((u.u_tsize << PAGE_SHIFT) + \
  76. u.start_code))
  77. #endif
  78. # define START_STACK(u) ((void __user *)u.start_stack)
  79. fs = get_fs();
  80. set_fs(KERNEL_DS);
  81. has_dumped = 1;
  82. current->flags |= PF_DUMPCORE;
  83. strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
  84. dump.u_ar0 = offsetof(struct user, regs);
  85. dump.signal = cprm->signr;
  86. aout_dump_thread(cprm->regs, &dump);
  87. /* If the size of the dump file exceeds the rlimit, then see what would happen
  88. if we wrote the stack, but not the data area. */
  89. if ((dump.u_dsize + dump.u_ssize+1) * PAGE_SIZE > cprm->limit)
  90. dump.u_dsize = 0;
  91. /* Make sure we have enough room to write the stack and data areas. */
  92. if ((dump.u_ssize + 1) * PAGE_SIZE > cprm->limit)
  93. dump.u_ssize = 0;
  94. /* make sure we actually have a data and stack area to dump */
  95. set_fs(USER_DS);
  96. if (!access_ok(VERIFY_READ, START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
  97. dump.u_dsize = 0;
  98. if (!access_ok(VERIFY_READ, START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
  99. dump.u_ssize = 0;
  100. set_fs(KERNEL_DS);
  101. /* struct user */
  102. if (!dump_write(file, &dump, sizeof(dump)))
  103. goto end_coredump;
  104. /* Now dump all of the user data. Include malloced stuff as well */
  105. if (!dump_seek(cprm->file, PAGE_SIZE - sizeof(dump)))
  106. goto end_coredump;
  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 << PAGE_SHIFT;
  113. if (!dump_write(file, dump_start, dump_size))
  114. goto end_coredump;
  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 << PAGE_SHIFT;
  120. if (!dump_write(file, dump_start, dump_size))
  121. goto end_coredump;
  122. }
  123. /* Finally dump the task struct. Not be used by gdb, but could be useful */
  124. set_fs(KERNEL_DS);
  125. if (!dump_write(file, current, sizeof(*current)))
  126. goto end_coredump;
  127. end_coredump:
  128. set_fs(fs);
  129. return has_dumped;
  130. }
  131. /*
  132. * create_aout_tables() parses the env- and arg-strings in new user
  133. * memory and creates the pointer tables from them, and puts their
  134. * addresses on the "stack", returning the new stack pointer value.
  135. */
  136. static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
  137. {
  138. char __user * __user *argv;
  139. char __user * __user *envp;
  140. unsigned long __user *sp;
  141. int argc = bprm->argc;
  142. int envc = bprm->envc;
  143. sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
  144. #ifdef __alpha__
  145. /* whee.. test-programs are so much fun. */
  146. put_user(0, --sp);
  147. put_user(0, --sp);
  148. if (bprm->loader) {
  149. put_user(0, --sp);
  150. put_user(1003, --sp);
  151. put_user(bprm->loader, --sp);
  152. put_user(1002, --sp);
  153. }
  154. put_user(bprm->exec, --sp);
  155. put_user(1001, --sp);
  156. #endif
  157. sp -= envc+1;
  158. envp = (char __user * __user *) sp;
  159. sp -= argc+1;
  160. argv = (char __user * __user *) sp;
  161. #ifndef __alpha__
  162. put_user((unsigned long) envp,--sp);
  163. put_user((unsigned long) argv,--sp);
  164. #endif
  165. put_user(argc,--sp);
  166. current->mm->arg_start = (unsigned long) p;
  167. while (argc-->0) {
  168. char c;
  169. put_user(p,argv++);
  170. do {
  171. get_user(c,p++);
  172. } while (c);
  173. }
  174. put_user(NULL,argv);
  175. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  176. while (envc-->0) {
  177. char c;
  178. put_user(p,envp++);
  179. do {
  180. get_user(c,p++);
  181. } while (c);
  182. }
  183. put_user(NULL,envp);
  184. current->mm->env_end = (unsigned long) p;
  185. return sp;
  186. }
  187. /*
  188. * These are the functions used to load a.out style executables and shared
  189. * libraries. There is no binary dependent code anywhere else.
  190. */
  191. static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  192. {
  193. struct exec ex;
  194. unsigned long error;
  195. unsigned long fd_offset;
  196. unsigned long rlim;
  197. int retval;
  198. ex = *((struct exec *) bprm->buf); /* exec-header */
  199. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  200. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  201. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  202. i_size_read(bprm->file->f_path.dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  203. return -ENOEXEC;
  204. }
  205. /*
  206. * Requires a mmap handler. This prevents people from using a.out
  207. * as part of an exploit attack against /proc-related vulnerabilities.
  208. */
  209. if (!bprm->file->f_op || !bprm->file->f_op->mmap)
  210. return -ENOEXEC;
  211. fd_offset = N_TXTOFF(ex);
  212. /* Check initial limits. This avoids letting people circumvent
  213. * size limits imposed on them by creating programs with large
  214. * arrays in the data or bss.
  215. */
  216. rlim = rlimit(RLIMIT_DATA);
  217. if (rlim >= RLIM_INFINITY)
  218. rlim = ~0;
  219. if (ex.a_data + ex.a_bss > rlim)
  220. return -ENOMEM;
  221. /* Flush all traces of the currently running executable */
  222. retval = flush_old_exec(bprm);
  223. if (retval)
  224. return retval;
  225. /* OK, This is the point of no return */
  226. #ifdef __alpha__
  227. SET_AOUT_PERSONALITY(bprm, ex);
  228. #else
  229. set_personality(PER_LINUX);
  230. #endif
  231. setup_new_exec(bprm);
  232. current->mm->end_code = ex.a_text +
  233. (current->mm->start_code = N_TXTADDR(ex));
  234. current->mm->end_data = ex.a_data +
  235. (current->mm->start_data = N_DATADDR(ex));
  236. current->mm->brk = ex.a_bss +
  237. (current->mm->start_brk = N_BSSADDR(ex));
  238. current->mm->free_area_cache = current->mm->mmap_base;
  239. current->mm->cached_hole_size = 0;
  240. install_exec_creds(bprm);
  241. current->flags &= ~PF_FORKNOEXEC;
  242. if (N_MAGIC(ex) == OMAGIC) {
  243. unsigned long text_addr, map_size;
  244. loff_t pos;
  245. text_addr = N_TXTADDR(ex);
  246. #ifdef __alpha__
  247. pos = fd_offset;
  248. map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
  249. #else
  250. pos = 32;
  251. map_size = ex.a_text+ex.a_data;
  252. #endif
  253. down_write(&current->mm->mmap_sem);
  254. error = do_brk(text_addr & PAGE_MASK, map_size);
  255. up_write(&current->mm->mmap_sem);
  256. if (error != (text_addr & PAGE_MASK)) {
  257. send_sig(SIGKILL, current, 0);
  258. return error;
  259. }
  260. error = bprm->file->f_op->read(bprm->file,
  261. (char __user *)text_addr,
  262. ex.a_text+ex.a_data, &pos);
  263. if ((signed long)error < 0) {
  264. send_sig(SIGKILL, current, 0);
  265. return error;
  266. }
  267. flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
  268. } else {
  269. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  270. (N_MAGIC(ex) != NMAGIC) && printk_ratelimit())
  271. {
  272. printk(KERN_NOTICE "executable not page aligned\n");
  273. }
  274. if ((fd_offset & ~PAGE_MASK) != 0 && printk_ratelimit())
  275. {
  276. printk(KERN_WARNING
  277. "fd_offset is not page aligned. Please convert program: %s\n",
  278. bprm->file->f_path.dentry->d_name.name);
  279. }
  280. if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
  281. loff_t pos = fd_offset;
  282. down_write(&current->mm->mmap_sem);
  283. do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  284. up_write(&current->mm->mmap_sem);
  285. bprm->file->f_op->read(bprm->file,
  286. (char __user *)N_TXTADDR(ex),
  287. ex.a_text+ex.a_data, &pos);
  288. flush_icache_range((unsigned long) N_TXTADDR(ex),
  289. (unsigned long) N_TXTADDR(ex) +
  290. ex.a_text+ex.a_data);
  291. goto beyond_if;
  292. }
  293. down_write(&current->mm->mmap_sem);
  294. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  295. PROT_READ | PROT_EXEC,
  296. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  297. fd_offset);
  298. up_write(&current->mm->mmap_sem);
  299. if (error != N_TXTADDR(ex)) {
  300. send_sig(SIGKILL, current, 0);
  301. return error;
  302. }
  303. down_write(&current->mm->mmap_sem);
  304. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  305. PROT_READ | PROT_WRITE | PROT_EXEC,
  306. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  307. fd_offset + ex.a_text);
  308. up_write(&current->mm->mmap_sem);
  309. if (error != N_DATADDR(ex)) {
  310. send_sig(SIGKILL, current, 0);
  311. return error;
  312. }
  313. }
  314. beyond_if:
  315. set_binfmt(&aout_format);
  316. retval = set_brk(current->mm->start_brk, current->mm->brk);
  317. if (retval < 0) {
  318. send_sig(SIGKILL, current, 0);
  319. return retval;
  320. }
  321. retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  322. if (retval < 0) {
  323. /* Someone check-me: is this error path enough? */
  324. send_sig(SIGKILL, current, 0);
  325. return retval;
  326. }
  327. current->mm->start_stack =
  328. (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
  329. #ifdef __alpha__
  330. regs->gp = ex.a_gpvalue;
  331. #endif
  332. start_thread(regs, ex.a_entry, current->mm->start_stack);
  333. return 0;
  334. }
  335. static int load_aout_library(struct file *file)
  336. {
  337. struct inode * inode;
  338. unsigned long bss, start_addr, len;
  339. unsigned long error;
  340. int retval;
  341. struct exec ex;
  342. inode = file->f_path.dentry->d_inode;
  343. retval = -ENOEXEC;
  344. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  345. if (error != sizeof(ex))
  346. goto out;
  347. /* We come in here for the regular a.out style of shared libraries */
  348. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  349. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  350. i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  351. goto out;
  352. }
  353. /*
  354. * Requires a mmap handler. This prevents people from using a.out
  355. * as part of an exploit attack against /proc-related vulnerabilities.
  356. */
  357. if (!file->f_op || !file->f_op->mmap)
  358. goto out;
  359. if (N_FLAGS(ex))
  360. goto out;
  361. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  362. this off to get the starting address for the page */
  363. start_addr = ex.a_entry & 0xfffff000;
  364. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  365. loff_t pos = N_TXTOFF(ex);
  366. if (printk_ratelimit())
  367. {
  368. printk(KERN_WARNING
  369. "N_TXTOFF is not page aligned. Please convert library: %s\n",
  370. file->f_path.dentry->d_name.name);
  371. }
  372. down_write(&current->mm->mmap_sem);
  373. do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  374. up_write(&current->mm->mmap_sem);
  375. file->f_op->read(file, (char __user *)start_addr,
  376. ex.a_text + ex.a_data, &pos);
  377. flush_icache_range((unsigned long) start_addr,
  378. (unsigned long) start_addr + ex.a_text + ex.a_data);
  379. retval = 0;
  380. goto out;
  381. }
  382. /* Now use mmap to map the library into memory. */
  383. down_write(&current->mm->mmap_sem);
  384. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  385. PROT_READ | PROT_WRITE | PROT_EXEC,
  386. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  387. N_TXTOFF(ex));
  388. up_write(&current->mm->mmap_sem);
  389. retval = error;
  390. if (error != start_addr)
  391. goto out;
  392. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  393. bss = ex.a_text + ex.a_data + ex.a_bss;
  394. if (bss > len) {
  395. down_write(&current->mm->mmap_sem);
  396. error = do_brk(start_addr + len, bss - len);
  397. up_write(&current->mm->mmap_sem);
  398. retval = error;
  399. if (error != start_addr + len)
  400. goto out;
  401. }
  402. retval = 0;
  403. out:
  404. return retval;
  405. }
  406. static int __init init_aout_binfmt(void)
  407. {
  408. return register_binfmt(&aout_format);
  409. }
  410. static void __exit exit_aout_binfmt(void)
  411. {
  412. unregister_binfmt(&aout_format);
  413. }
  414. core_initcall(init_aout_binfmt);
  415. module_exit(exit_aout_binfmt);
  416. MODULE_LICENSE("GPL");