binfmt_aout.c 12 KB

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