binfmt_aout.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
  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(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
  80. {
  81. mm_segment_t fs;
  82. int has_dumped = 0;
  83. unsigned long dump_start, dump_size;
  84. struct user dump;
  85. #if defined(__alpha__)
  86. # define START_DATA(u) (u.start_data)
  87. #elif defined(__arm__)
  88. # define START_DATA(u) ((u.u_tsize << PAGE_SHIFT) + u.start_code)
  89. #elif defined(__sparc__)
  90. # define START_DATA(u) (u.u_tsize)
  91. #elif defined(__i386__) || defined(__mc68000__) || defined(__arch_um__)
  92. # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
  93. #endif
  94. #ifdef __sparc__
  95. # define START_STACK(u) ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
  96. #else
  97. # define START_STACK(u) (u.start_stack)
  98. #endif
  99. fs = get_fs();
  100. set_fs(KERNEL_DS);
  101. has_dumped = 1;
  102. current->flags |= PF_DUMPCORE;
  103. strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
  104. #ifndef __sparc__
  105. dump.u_ar0 = offsetof(struct user, regs);
  106. #endif
  107. dump.signal = signr;
  108. aout_dump_thread(regs, &dump);
  109. /* If the size of the dump file exceeds the rlimit, then see what would happen
  110. if we wrote the stack, but not the data area. */
  111. #ifdef __sparc__
  112. if ((dump.u_dsize + dump.u_ssize) > limit)
  113. dump.u_dsize = 0;
  114. #else
  115. if ((dump.u_dsize + dump.u_ssize+1) * PAGE_SIZE > limit)
  116. dump.u_dsize = 0;
  117. #endif
  118. /* Make sure we have enough room to write the stack and data areas. */
  119. #ifdef __sparc__
  120. if (dump.u_ssize > limit)
  121. dump.u_ssize = 0;
  122. #else
  123. if ((dump.u_ssize + 1) * PAGE_SIZE > limit)
  124. dump.u_ssize = 0;
  125. #endif
  126. /* make sure we actually have a data and stack area to dump */
  127. set_fs(USER_DS);
  128. #ifdef __sparc__
  129. if (!access_ok(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize))
  130. dump.u_dsize = 0;
  131. if (!access_ok(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize))
  132. dump.u_ssize = 0;
  133. #else
  134. if (!access_ok(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
  135. dump.u_dsize = 0;
  136. if (!access_ok(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
  137. dump.u_ssize = 0;
  138. #endif
  139. set_fs(KERNEL_DS);
  140. /* struct user */
  141. DUMP_WRITE(&dump,sizeof(dump));
  142. /* Now dump all of the user data. Include malloced stuff as well */
  143. #ifndef __sparc__
  144. DUMP_SEEK(PAGE_SIZE);
  145. #endif
  146. /* now we start writing out the user space info */
  147. set_fs(USER_DS);
  148. /* Dump the data area */
  149. if (dump.u_dsize != 0) {
  150. dump_start = START_DATA(dump);
  151. #ifdef __sparc__
  152. dump_size = dump.u_dsize;
  153. #else
  154. dump_size = dump.u_dsize << PAGE_SHIFT;
  155. #endif
  156. DUMP_WRITE(dump_start,dump_size);
  157. }
  158. /* Now prepare to dump the stack area */
  159. if (dump.u_ssize != 0) {
  160. dump_start = START_STACK(dump);
  161. #ifdef __sparc__
  162. dump_size = dump.u_ssize;
  163. #else
  164. dump_size = dump.u_ssize << PAGE_SHIFT;
  165. #endif
  166. DUMP_WRITE(dump_start,dump_size);
  167. }
  168. /* Finally dump the task struct. Not be used by gdb, but could be useful */
  169. set_fs(KERNEL_DS);
  170. DUMP_WRITE(current,sizeof(*current));
  171. end_coredump:
  172. set_fs(fs);
  173. return has_dumped;
  174. }
  175. /*
  176. * create_aout_tables() parses the env- and arg-strings in new user
  177. * memory and creates the pointer tables from them, and puts their
  178. * addresses on the "stack", returning the new stack pointer value.
  179. */
  180. static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
  181. {
  182. char __user * __user *argv;
  183. char __user * __user *envp;
  184. unsigned long __user *sp;
  185. int argc = bprm->argc;
  186. int envc = bprm->envc;
  187. sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
  188. #ifdef __sparc__
  189. /* This imposes the proper stack alignment for a new process. */
  190. sp = (void __user *) (((unsigned long) sp) & ~7);
  191. if ((envc+argc+3)&1) --sp;
  192. #endif
  193. #ifdef __alpha__
  194. /* whee.. test-programs are so much fun. */
  195. put_user(0, --sp);
  196. put_user(0, --sp);
  197. if (bprm->loader) {
  198. put_user(0, --sp);
  199. put_user(0x3eb, --sp);
  200. put_user(bprm->loader, --sp);
  201. put_user(0x3ea, --sp);
  202. }
  203. put_user(bprm->exec, --sp);
  204. put_user(0x3e9, --sp);
  205. #endif
  206. sp -= envc+1;
  207. envp = (char __user * __user *) sp;
  208. sp -= argc+1;
  209. argv = (char __user * __user *) sp;
  210. #if defined(__i386__) || defined(__mc68000__) || defined(__arm__) || defined(__arch_um__)
  211. put_user((unsigned long) envp,--sp);
  212. put_user((unsigned long) argv,--sp);
  213. #endif
  214. put_user(argc,--sp);
  215. current->mm->arg_start = (unsigned long) p;
  216. while (argc-->0) {
  217. char c;
  218. put_user(p,argv++);
  219. do {
  220. get_user(c,p++);
  221. } while (c);
  222. }
  223. put_user(NULL,argv);
  224. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  225. while (envc-->0) {
  226. char c;
  227. put_user(p,envp++);
  228. do {
  229. get_user(c,p++);
  230. } while (c);
  231. }
  232. put_user(NULL,envp);
  233. current->mm->env_end = (unsigned long) p;
  234. return sp;
  235. }
  236. /*
  237. * These are the functions used to load a.out style executables and shared
  238. * libraries. There is no binary dependent code anywhere else.
  239. */
  240. static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  241. {
  242. struct exec ex;
  243. unsigned long error;
  244. unsigned long fd_offset;
  245. unsigned long rlim;
  246. int retval;
  247. ex = *((struct exec *) bprm->buf); /* exec-header */
  248. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  249. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  250. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  251. i_size_read(bprm->file->f_path.dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  252. return -ENOEXEC;
  253. }
  254. /*
  255. * Requires a mmap handler. This prevents people from using a.out
  256. * as part of an exploit attack against /proc-related vulnerabilities.
  257. */
  258. if (!bprm->file->f_op || !bprm->file->f_op->mmap)
  259. return -ENOEXEC;
  260. fd_offset = N_TXTOFF(ex);
  261. /* Check initial limits. This avoids letting people circumvent
  262. * size limits imposed on them by creating programs with large
  263. * arrays in the data or bss.
  264. */
  265. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  266. if (rlim >= RLIM_INFINITY)
  267. rlim = ~0;
  268. if (ex.a_data + ex.a_bss > rlim)
  269. return -ENOMEM;
  270. /* Flush all traces of the currently running executable */
  271. retval = flush_old_exec(bprm);
  272. if (retval)
  273. return retval;
  274. /* OK, This is the point of no return */
  275. #if defined(__alpha__)
  276. SET_AOUT_PERSONALITY(bprm, ex);
  277. #elif defined(__sparc__)
  278. set_personality(PER_SUNOS);
  279. #if !defined(__sparc_v9__)
  280. memcpy(&current->thread.core_exec, &ex, sizeof(struct exec));
  281. #endif
  282. #else
  283. set_personality(PER_LINUX);
  284. #endif
  285. current->mm->end_code = ex.a_text +
  286. (current->mm->start_code = N_TXTADDR(ex));
  287. current->mm->end_data = ex.a_data +
  288. (current->mm->start_data = N_DATADDR(ex));
  289. current->mm->brk = ex.a_bss +
  290. (current->mm->start_brk = N_BSSADDR(ex));
  291. current->mm->free_area_cache = current->mm->mmap_base;
  292. current->mm->cached_hole_size = 0;
  293. install_exec_creds(bprm);
  294. current->flags &= ~PF_FORKNOEXEC;
  295. #ifdef __sparc__
  296. if (N_MAGIC(ex) == NMAGIC) {
  297. loff_t pos = fd_offset;
  298. /* Fuck me plenty... */
  299. /* <AOL></AOL> */
  300. down_write(&current->mm->mmap_sem);
  301. error = do_brk(N_TXTADDR(ex), ex.a_text);
  302. up_write(&current->mm->mmap_sem);
  303. bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
  304. ex.a_text, &pos);
  305. down_write(&current->mm->mmap_sem);
  306. error = do_brk(N_DATADDR(ex), ex.a_data);
  307. up_write(&current->mm->mmap_sem);
  308. bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
  309. ex.a_data, &pos);
  310. goto beyond_if;
  311. }
  312. #endif
  313. if (N_MAGIC(ex) == OMAGIC) {
  314. unsigned long text_addr, map_size;
  315. loff_t pos;
  316. text_addr = N_TXTADDR(ex);
  317. #if defined(__alpha__) || defined(__sparc__)
  318. pos = fd_offset;
  319. map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
  320. #else
  321. pos = 32;
  322. map_size = ex.a_text+ex.a_data;
  323. #endif
  324. down_write(&current->mm->mmap_sem);
  325. error = do_brk(text_addr & PAGE_MASK, map_size);
  326. up_write(&current->mm->mmap_sem);
  327. if (error != (text_addr & PAGE_MASK)) {
  328. send_sig(SIGKILL, current, 0);
  329. return error;
  330. }
  331. error = bprm->file->f_op->read(bprm->file,
  332. (char __user *)text_addr,
  333. ex.a_text+ex.a_data, &pos);
  334. if ((signed long)error < 0) {
  335. send_sig(SIGKILL, current, 0);
  336. return error;
  337. }
  338. flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
  339. } else {
  340. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  341. (N_MAGIC(ex) != NMAGIC) && printk_ratelimit())
  342. {
  343. printk(KERN_NOTICE "executable not page aligned\n");
  344. }
  345. if ((fd_offset & ~PAGE_MASK) != 0 && printk_ratelimit())
  346. {
  347. printk(KERN_WARNING
  348. "fd_offset is not page aligned. Please convert program: %s\n",
  349. bprm->file->f_path.dentry->d_name.name);
  350. }
  351. if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
  352. loff_t pos = fd_offset;
  353. down_write(&current->mm->mmap_sem);
  354. do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  355. up_write(&current->mm->mmap_sem);
  356. bprm->file->f_op->read(bprm->file,
  357. (char __user *)N_TXTADDR(ex),
  358. ex.a_text+ex.a_data, &pos);
  359. flush_icache_range((unsigned long) N_TXTADDR(ex),
  360. (unsigned long) N_TXTADDR(ex) +
  361. ex.a_text+ex.a_data);
  362. goto beyond_if;
  363. }
  364. down_write(&current->mm->mmap_sem);
  365. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  366. PROT_READ | PROT_EXEC,
  367. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  368. fd_offset);
  369. up_write(&current->mm->mmap_sem);
  370. if (error != N_TXTADDR(ex)) {
  371. send_sig(SIGKILL, current, 0);
  372. return error;
  373. }
  374. down_write(&current->mm->mmap_sem);
  375. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  376. PROT_READ | PROT_WRITE | PROT_EXEC,
  377. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  378. fd_offset + ex.a_text);
  379. up_write(&current->mm->mmap_sem);
  380. if (error != N_DATADDR(ex)) {
  381. send_sig(SIGKILL, current, 0);
  382. return error;
  383. }
  384. }
  385. beyond_if:
  386. set_binfmt(&aout_format);
  387. retval = set_brk(current->mm->start_brk, current->mm->brk);
  388. if (retval < 0) {
  389. send_sig(SIGKILL, current, 0);
  390. return retval;
  391. }
  392. retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  393. if (retval < 0) {
  394. /* Someone check-me: is this error path enough? */
  395. send_sig(SIGKILL, current, 0);
  396. return retval;
  397. }
  398. current->mm->start_stack =
  399. (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
  400. #ifdef __alpha__
  401. regs->gp = ex.a_gpvalue;
  402. #endif
  403. start_thread(regs, ex.a_entry, current->mm->start_stack);
  404. return 0;
  405. }
  406. static int load_aout_library(struct file *file)
  407. {
  408. struct inode * inode;
  409. unsigned long bss, start_addr, len;
  410. unsigned long error;
  411. int retval;
  412. struct exec ex;
  413. inode = file->f_path.dentry->d_inode;
  414. retval = -ENOEXEC;
  415. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  416. if (error != sizeof(ex))
  417. goto out;
  418. /* We come in here for the regular a.out style of shared libraries */
  419. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  420. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  421. i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  422. goto out;
  423. }
  424. /*
  425. * Requires a mmap handler. This prevents people from using a.out
  426. * as part of an exploit attack against /proc-related vulnerabilities.
  427. */
  428. if (!file->f_op || !file->f_op->mmap)
  429. goto out;
  430. if (N_FLAGS(ex))
  431. goto out;
  432. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  433. this off to get the starting address for the page */
  434. start_addr = ex.a_entry & 0xfffff000;
  435. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  436. loff_t pos = N_TXTOFF(ex);
  437. if (printk_ratelimit())
  438. {
  439. printk(KERN_WARNING
  440. "N_TXTOFF is not page aligned. Please convert library: %s\n",
  441. file->f_path.dentry->d_name.name);
  442. }
  443. down_write(&current->mm->mmap_sem);
  444. do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  445. up_write(&current->mm->mmap_sem);
  446. file->f_op->read(file, (char __user *)start_addr,
  447. ex.a_text + ex.a_data, &pos);
  448. flush_icache_range((unsigned long) start_addr,
  449. (unsigned long) start_addr + ex.a_text + ex.a_data);
  450. retval = 0;
  451. goto out;
  452. }
  453. /* Now use mmap to map the library into memory. */
  454. down_write(&current->mm->mmap_sem);
  455. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  456. PROT_READ | PROT_WRITE | PROT_EXEC,
  457. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  458. N_TXTOFF(ex));
  459. up_write(&current->mm->mmap_sem);
  460. retval = error;
  461. if (error != start_addr)
  462. goto out;
  463. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  464. bss = ex.a_text + ex.a_data + ex.a_bss;
  465. if (bss > len) {
  466. down_write(&current->mm->mmap_sem);
  467. error = do_brk(start_addr + len, bss - len);
  468. up_write(&current->mm->mmap_sem);
  469. retval = error;
  470. if (error != start_addr + len)
  471. goto out;
  472. }
  473. retval = 0;
  474. out:
  475. return retval;
  476. }
  477. static int __init init_aout_binfmt(void)
  478. {
  479. return register_binfmt(&aout_format);
  480. }
  481. static void __exit exit_aout_binfmt(void)
  482. {
  483. unregister_binfmt(&aout_format);
  484. }
  485. core_initcall(init_aout_binfmt);
  486. module_exit(exit_aout_binfmt);
  487. MODULE_LICENSE("GPL");