binfmt_aout.c 12 KB

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