binfmt_aout.c 15 KB

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