binfmt_aout.c 15 KB

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