binfmt_aout.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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_dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  255. return -ENOEXEC;
  256. }
  257. fd_offset = N_TXTOFF(ex);
  258. /* Check initial limits. This avoids letting people circumvent
  259. * size limits imposed on them by creating programs with large
  260. * arrays in the data or bss.
  261. */
  262. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  263. if (rlim >= RLIM_INFINITY)
  264. rlim = ~0;
  265. if (ex.a_data + ex.a_bss > rlim)
  266. return -ENOMEM;
  267. /* Flush all traces of the currently running executable */
  268. retval = flush_old_exec(bprm);
  269. if (retval)
  270. return retval;
  271. /* OK, This is the point of no return */
  272. #if defined(__alpha__)
  273. SET_AOUT_PERSONALITY(bprm, ex);
  274. #elif defined(__sparc__)
  275. set_personality(PER_SUNOS);
  276. #if !defined(__sparc_v9__)
  277. memcpy(&current->thread.core_exec, &ex, sizeof(struct exec));
  278. #endif
  279. #else
  280. set_personality(PER_LINUX);
  281. #endif
  282. current->mm->end_code = ex.a_text +
  283. (current->mm->start_code = N_TXTADDR(ex));
  284. current->mm->end_data = ex.a_data +
  285. (current->mm->start_data = N_DATADDR(ex));
  286. current->mm->brk = ex.a_bss +
  287. (current->mm->start_brk = N_BSSADDR(ex));
  288. current->mm->free_area_cache = current->mm->mmap_base;
  289. current->mm->cached_hole_size = 0;
  290. current->mm->mmap = NULL;
  291. compute_creds(bprm);
  292. current->flags &= ~PF_FORKNOEXEC;
  293. #ifdef __sparc__
  294. if (N_MAGIC(ex) == NMAGIC) {
  295. loff_t pos = fd_offset;
  296. /* Fuck me plenty... */
  297. /* <AOL></AOL> */
  298. down_write(&current->mm->mmap_sem);
  299. error = do_brk(N_TXTADDR(ex), ex.a_text);
  300. up_write(&current->mm->mmap_sem);
  301. bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
  302. ex.a_text, &pos);
  303. down_write(&current->mm->mmap_sem);
  304. error = do_brk(N_DATADDR(ex), ex.a_data);
  305. up_write(&current->mm->mmap_sem);
  306. bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
  307. ex.a_data, &pos);
  308. goto beyond_if;
  309. }
  310. #endif
  311. if (N_MAGIC(ex) == OMAGIC) {
  312. unsigned long text_addr, map_size;
  313. loff_t pos;
  314. text_addr = N_TXTADDR(ex);
  315. #if defined(__alpha__) || defined(__sparc__)
  316. pos = fd_offset;
  317. map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
  318. #else
  319. pos = 32;
  320. map_size = ex.a_text+ex.a_data;
  321. #endif
  322. down_write(&current->mm->mmap_sem);
  323. error = do_brk(text_addr & PAGE_MASK, map_size);
  324. up_write(&current->mm->mmap_sem);
  325. if (error != (text_addr & PAGE_MASK)) {
  326. send_sig(SIGKILL, current, 0);
  327. return error;
  328. }
  329. error = bprm->file->f_op->read(bprm->file,
  330. (char __user *)text_addr,
  331. ex.a_text+ex.a_data, &pos);
  332. if ((signed long)error < 0) {
  333. send_sig(SIGKILL, current, 0);
  334. return error;
  335. }
  336. flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
  337. } else {
  338. static unsigned long error_time, error_time2;
  339. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  340. (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time2) > 5*HZ)
  341. {
  342. printk(KERN_NOTICE "executable not page aligned\n");
  343. error_time2 = jiffies;
  344. }
  345. if ((fd_offset & ~PAGE_MASK) != 0 &&
  346. (jiffies-error_time) > 5*HZ)
  347. {
  348. printk(KERN_WARNING
  349. "fd_offset is not page aligned. Please convert program: %s\n",
  350. bprm->file->f_dentry->d_name.name);
  351. error_time = jiffies;
  352. }
  353. if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
  354. loff_t pos = fd_offset;
  355. down_write(&current->mm->mmap_sem);
  356. do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  357. up_write(&current->mm->mmap_sem);
  358. bprm->file->f_op->read(bprm->file,
  359. (char __user *)N_TXTADDR(ex),
  360. ex.a_text+ex.a_data, &pos);
  361. flush_icache_range((unsigned long) N_TXTADDR(ex),
  362. (unsigned long) N_TXTADDR(ex) +
  363. ex.a_text+ex.a_data);
  364. goto beyond_if;
  365. }
  366. down_write(&current->mm->mmap_sem);
  367. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  368. PROT_READ | PROT_EXEC,
  369. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  370. fd_offset);
  371. up_write(&current->mm->mmap_sem);
  372. if (error != N_TXTADDR(ex)) {
  373. send_sig(SIGKILL, current, 0);
  374. return error;
  375. }
  376. down_write(&current->mm->mmap_sem);
  377. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  378. PROT_READ | PROT_WRITE | PROT_EXEC,
  379. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  380. fd_offset + ex.a_text);
  381. up_write(&current->mm->mmap_sem);
  382. if (error != N_DATADDR(ex)) {
  383. send_sig(SIGKILL, current, 0);
  384. return error;
  385. }
  386. }
  387. beyond_if:
  388. set_binfmt(&aout_format);
  389. retval = set_brk(current->mm->start_brk, current->mm->brk);
  390. if (retval < 0) {
  391. send_sig(SIGKILL, current, 0);
  392. return retval;
  393. }
  394. retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  395. if (retval < 0) {
  396. /* Someone check-me: is this error path enough? */
  397. send_sig(SIGKILL, current, 0);
  398. return retval;
  399. }
  400. current->mm->start_stack =
  401. (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
  402. #ifdef __alpha__
  403. regs->gp = ex.a_gpvalue;
  404. #endif
  405. start_thread(regs, ex.a_entry, current->mm->start_stack);
  406. if (unlikely(current->ptrace & PT_PTRACED)) {
  407. if (current->ptrace & PT_TRACE_EXEC)
  408. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  409. else
  410. send_sig(SIGTRAP, current, 0);
  411. }
  412. return 0;
  413. }
  414. static int load_aout_library(struct file *file)
  415. {
  416. struct inode * inode;
  417. unsigned long bss, start_addr, len;
  418. unsigned long error;
  419. int retval;
  420. struct exec ex;
  421. inode = file->f_dentry->d_inode;
  422. retval = -ENOEXEC;
  423. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  424. if (error != sizeof(ex))
  425. goto out;
  426. /* We come in here for the regular a.out style of shared libraries */
  427. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  428. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  429. i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  430. goto out;
  431. }
  432. if (N_FLAGS(ex))
  433. goto out;
  434. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  435. this off to get the starting address for the page */
  436. start_addr = ex.a_entry & 0xfffff000;
  437. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  438. static unsigned long error_time;
  439. loff_t pos = N_TXTOFF(ex);
  440. if ((jiffies-error_time) > 5*HZ)
  441. {
  442. printk(KERN_WARNING
  443. "N_TXTOFF is not page aligned. Please convert library: %s\n",
  444. file->f_dentry->d_name.name);
  445. error_time = jiffies;
  446. }
  447. down_write(&current->mm->mmap_sem);
  448. do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  449. up_write(&current->mm->mmap_sem);
  450. file->f_op->read(file, (char __user *)start_addr,
  451. ex.a_text + ex.a_data, &pos);
  452. flush_icache_range((unsigned long) start_addr,
  453. (unsigned long) start_addr + ex.a_text + ex.a_data);
  454. retval = 0;
  455. goto out;
  456. }
  457. /* Now use mmap to map the library into memory. */
  458. down_write(&current->mm->mmap_sem);
  459. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  460. PROT_READ | PROT_WRITE | PROT_EXEC,
  461. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  462. N_TXTOFF(ex));
  463. up_write(&current->mm->mmap_sem);
  464. retval = error;
  465. if (error != start_addr)
  466. goto out;
  467. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  468. bss = ex.a_text + ex.a_data + ex.a_bss;
  469. if (bss > len) {
  470. down_write(&current->mm->mmap_sem);
  471. error = do_brk(start_addr + len, bss - len);
  472. up_write(&current->mm->mmap_sem);
  473. retval = error;
  474. if (error != start_addr + len)
  475. goto out;
  476. }
  477. retval = 0;
  478. out:
  479. return retval;
  480. }
  481. static int __init init_aout_binfmt(void)
  482. {
  483. return register_binfmt(&aout_format);
  484. }
  485. static void __exit exit_aout_binfmt(void)
  486. {
  487. unregister_binfmt(&aout_format);
  488. }
  489. core_initcall(init_aout_binfmt);
  490. module_exit(exit_aout_binfmt);
  491. MODULE_LICENSE("GPL");