binfmt_aout.c 11 KB

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