binfmt_aout.c 13 KB

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