ia32_aout.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * a.out loader for x86-64
  3. *
  4. * Copyright (C) 1991, 1992, 1996 Linus Torvalds
  5. * Hacked together by Andi Kleen
  6. */
  7. #include <linux/module.h>
  8. #include <linux/time.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/mman.h>
  12. #include <linux/a.out.h>
  13. #include <linux/errno.h>
  14. #include <linux/signal.h>
  15. #include <linux/string.h>
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/stat.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/user.h>
  22. #include <linux/slab.h>
  23. #include <linux/binfmts.h>
  24. #include <linux/personality.h>
  25. #include <linux/init.h>
  26. #include <linux/jiffies.h>
  27. #include <asm/system.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/user32.h>
  32. #include <asm/ia32.h>
  33. #undef WARN_OLD
  34. #undef CORE_DUMP /* probably broken */
  35. static int load_aout_binary(struct linux_binprm *, struct pt_regs *regs);
  36. static int load_aout_library(struct file *);
  37. #ifdef CORE_DUMP
  38. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file,
  39. unsigned long limit);
  40. /*
  41. * fill in the user structure for a core dump..
  42. */
  43. static void dump_thread32(struct pt_regs *regs, struct user32 *dump)
  44. {
  45. u32 fs, gs;
  46. /* changed the size calculations - should hopefully work better. lbt */
  47. dump->magic = CMAGIC;
  48. dump->start_code = 0;
  49. dump->start_stack = regs->sp & ~(PAGE_SIZE - 1);
  50. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  51. dump->u_dsize = ((unsigned long)
  52. (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  53. dump->u_dsize -= dump->u_tsize;
  54. dump->u_ssize = 0;
  55. dump->u_debugreg[0] = current->thread.debugreg0;
  56. dump->u_debugreg[1] = current->thread.debugreg1;
  57. dump->u_debugreg[2] = current->thread.debugreg2;
  58. dump->u_debugreg[3] = current->thread.debugreg3;
  59. dump->u_debugreg[4] = 0;
  60. dump->u_debugreg[5] = 0;
  61. dump->u_debugreg[6] = current->thread.debugreg6;
  62. dump->u_debugreg[7] = current->thread.debugreg7;
  63. if (dump->start_stack < 0xc0000000) {
  64. unsigned long tmp;
  65. tmp = (unsigned long) (0xc0000000 - dump->start_stack);
  66. dump->u_ssize = tmp >> PAGE_SHIFT;
  67. }
  68. dump->regs.bx = regs->bx;
  69. dump->regs.cx = regs->cx;
  70. dump->regs.dx = regs->dx;
  71. dump->regs.si = regs->si;
  72. dump->regs.di = regs->di;
  73. dump->regs.bp = regs->bp;
  74. dump->regs.ax = regs->ax;
  75. dump->regs.ds = current->thread.ds;
  76. dump->regs.es = current->thread.es;
  77. savesegment(fs, fs);
  78. dump->regs.fs = fs;
  79. savesegment(gs, gs);
  80. dump->regs.gs = gs;
  81. dump->regs.orig_ax = regs->orig_ax;
  82. dump->regs.ip = regs->ip;
  83. dump->regs.cs = regs->cs;
  84. dump->regs.flags = regs->flags;
  85. dump->regs.sp = regs->sp;
  86. dump->regs.ss = regs->ss;
  87. #if 1 /* FIXME */
  88. dump->u_fpvalid = 0;
  89. #else
  90. dump->u_fpvalid = dump_fpu(regs, &dump->i387);
  91. #endif
  92. }
  93. #endif
  94. static struct linux_binfmt aout_format = {
  95. .module = THIS_MODULE,
  96. .load_binary = load_aout_binary,
  97. .load_shlib = load_aout_library,
  98. #ifdef CORE_DUMP
  99. .core_dump = aout_core_dump,
  100. #endif
  101. .min_coredump = PAGE_SIZE
  102. };
  103. static void set_brk(unsigned long start, unsigned long end)
  104. {
  105. start = PAGE_ALIGN(start);
  106. end = PAGE_ALIGN(end);
  107. if (end <= start)
  108. return;
  109. down_write(&current->mm->mmap_sem);
  110. do_brk(start, end - start);
  111. up_write(&current->mm->mmap_sem);
  112. }
  113. #ifdef CORE_DUMP
  114. /*
  115. * These are the only things you should do on a core-file: use only these
  116. * macros to write out all the necessary info.
  117. */
  118. static int dump_write(struct file *file, const void *addr, int nr)
  119. {
  120. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  121. }
  122. #define DUMP_WRITE(addr, nr) \
  123. if (!dump_write(file, (void *)(addr), (nr))) \
  124. goto end_coredump;
  125. #define DUMP_SEEK(offset) \
  126. if (file->f_op->llseek) { \
  127. if (file->f_op->llseek(file, (offset), 0) != (offset)) \
  128. goto end_coredump; \
  129. } else \
  130. file->f_pos = (offset)
  131. #define START_DATA() (u.u_tsize << PAGE_SHIFT)
  132. #define START_STACK(u) (u.start_stack)
  133. /*
  134. * Routine writes a core dump image in the current directory.
  135. * Currently only a stub-function.
  136. *
  137. * Note that setuid/setgid files won't make a core-dump if the uid/gid
  138. * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  139. * field, which also makes sure the core-dumps won't be recursive if the
  140. * dumping of the process results in another error..
  141. */
  142. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file,
  143. unsigned long limit)
  144. {
  145. mm_segment_t fs;
  146. int has_dumped = 0;
  147. unsigned long dump_start, dump_size;
  148. struct user32 dump;
  149. fs = get_fs();
  150. set_fs(KERNEL_DS);
  151. has_dumped = 1;
  152. current->flags |= PF_DUMPCORE;
  153. strncpy(dump.u_comm, current->comm, sizeof(current->comm));
  154. dump.u_ar0 = offsetof(struct user32, regs);
  155. dump.signal = signr;
  156. dump_thread32(regs, &dump);
  157. /*
  158. * If the size of the dump file exceeds the rlimit, then see
  159. * what would happen if we wrote the stack, but not the data
  160. * area.
  161. */
  162. if ((dump.u_dsize + dump.u_ssize + 1) * PAGE_SIZE > limit)
  163. dump.u_dsize = 0;
  164. /* Make sure we have enough room to write the stack and data areas. */
  165. if ((dump.u_ssize + 1) * PAGE_SIZE > limit)
  166. dump.u_ssize = 0;
  167. /* make sure we actually have a data and stack area to dump */
  168. set_fs(USER_DS);
  169. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_DATA(dump),
  170. dump.u_dsize << PAGE_SHIFT))
  171. dump.u_dsize = 0;
  172. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_STACK(dump),
  173. dump.u_ssize << PAGE_SHIFT))
  174. dump.u_ssize = 0;
  175. set_fs(KERNEL_DS);
  176. /* struct user */
  177. DUMP_WRITE(&dump, sizeof(dump));
  178. /* Now dump all of the user data. Include malloced stuff as well */
  179. DUMP_SEEK(PAGE_SIZE);
  180. /* now we start writing out the user space info */
  181. set_fs(USER_DS);
  182. /* Dump the data area */
  183. if (dump.u_dsize != 0) {
  184. dump_start = START_DATA(dump);
  185. dump_size = dump.u_dsize << PAGE_SHIFT;
  186. DUMP_WRITE(dump_start, dump_size);
  187. }
  188. /* Now prepare to dump the stack area */
  189. if (dump.u_ssize != 0) {
  190. dump_start = START_STACK(dump);
  191. dump_size = dump.u_ssize << PAGE_SHIFT;
  192. DUMP_WRITE(dump_start, dump_size);
  193. }
  194. /*
  195. * Finally dump the task struct. Not be used by gdb, but
  196. * could be useful
  197. */
  198. set_fs(KERNEL_DS);
  199. DUMP_WRITE(current, sizeof(*current));
  200. end_coredump:
  201. set_fs(fs);
  202. return has_dumped;
  203. }
  204. #endif
  205. /*
  206. * create_aout_tables() parses the env- and arg-strings in new user
  207. * memory and creates the pointer tables from them, and puts their
  208. * addresses on the "stack", returning the new stack pointer value.
  209. */
  210. static u32 __user *create_aout_tables(char __user *p, struct linux_binprm *bprm)
  211. {
  212. u32 __user *argv, *envp, *sp;
  213. int argc = bprm->argc, envc = bprm->envc;
  214. sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p);
  215. sp -= envc+1;
  216. envp = sp;
  217. sp -= argc+1;
  218. argv = sp;
  219. put_user((unsigned long) envp, --sp);
  220. put_user((unsigned long) argv, --sp);
  221. put_user(argc, --sp);
  222. current->mm->arg_start = (unsigned long) p;
  223. while (argc-- > 0) {
  224. char c;
  225. put_user((u32)(unsigned long)p, argv++);
  226. do {
  227. get_user(c, p++);
  228. } while (c);
  229. }
  230. put_user(0, argv);
  231. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  232. while (envc-- > 0) {
  233. char c;
  234. put_user((u32)(unsigned long)p, envp++);
  235. do {
  236. get_user(c, p++);
  237. } while (c);
  238. }
  239. put_user(0, envp);
  240. current->mm->env_end = (unsigned long) p;
  241. return sp;
  242. }
  243. /*
  244. * These are the functions used to load a.out style executables and shared
  245. * libraries. There is no binary dependent code anywhere else.
  246. */
  247. static int load_aout_binary(struct linux_binprm *bprm, struct pt_regs *regs)
  248. {
  249. unsigned long error, fd_offset, rlim;
  250. struct exec ex;
  251. int retval;
  252. ex = *((struct exec *) bprm->buf); /* exec-header */
  253. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  254. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  255. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  256. i_size_read(bprm->file->f_path.dentry->d_inode) <
  257. ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  258. return -ENOEXEC;
  259. }
  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. regs->cs = __USER32_CS;
  275. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  276. regs->r13 = regs->r14 = regs->r15 = 0;
  277. /* OK, This is the point of no return */
  278. set_personality(PER_LINUX);
  279. set_thread_flag(TIF_IA32);
  280. clear_thread_flag(TIF_ABI_PENDING);
  281. current->mm->end_code = ex.a_text +
  282. (current->mm->start_code = N_TXTADDR(ex));
  283. current->mm->end_data = ex.a_data +
  284. (current->mm->start_data = N_DATADDR(ex));
  285. current->mm->brk = ex.a_bss +
  286. (current->mm->start_brk = N_BSSADDR(ex));
  287. current->mm->free_area_cache = TASK_UNMAPPED_BASE;
  288. current->mm->cached_hole_size = 0;
  289. current->mm->mmap = NULL;
  290. install_exec_creds(bprm);
  291. current->flags &= ~PF_FORKNOEXEC;
  292. if (N_MAGIC(ex) == OMAGIC) {
  293. unsigned long text_addr, map_size;
  294. loff_t pos;
  295. text_addr = N_TXTADDR(ex);
  296. pos = 32;
  297. map_size = ex.a_text+ex.a_data;
  298. down_write(&current->mm->mmap_sem);
  299. error = do_brk(text_addr & PAGE_MASK, map_size);
  300. up_write(&current->mm->mmap_sem);
  301. if (error != (text_addr & PAGE_MASK)) {
  302. send_sig(SIGKILL, current, 0);
  303. return error;
  304. }
  305. error = bprm->file->f_op->read(bprm->file,
  306. (char __user *)text_addr,
  307. ex.a_text+ex.a_data, &pos);
  308. if ((signed long)error < 0) {
  309. send_sig(SIGKILL, current, 0);
  310. return error;
  311. }
  312. flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
  313. } else {
  314. #ifdef WARN_OLD
  315. static unsigned long error_time, error_time2;
  316. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  317. (N_MAGIC(ex) != NMAGIC) &&
  318. time_after(jiffies, error_time2 + 5*HZ)) {
  319. printk(KERN_NOTICE "executable not page aligned\n");
  320. error_time2 = jiffies;
  321. }
  322. if ((fd_offset & ~PAGE_MASK) != 0 &&
  323. time_after(jiffies, error_time + 5*HZ)) {
  324. printk(KERN_WARNING
  325. "fd_offset is not page aligned. Please convert "
  326. "program: %s\n",
  327. bprm->file->f_path.dentry->d_name.name);
  328. error_time = jiffies;
  329. }
  330. #endif
  331. if (!bprm->file->f_op->mmap || (fd_offset & ~PAGE_MASK) != 0) {
  332. loff_t pos = fd_offset;
  333. down_write(&current->mm->mmap_sem);
  334. do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  335. up_write(&current->mm->mmap_sem);
  336. bprm->file->f_op->read(bprm->file,
  337. (char __user *)N_TXTADDR(ex),
  338. ex.a_text+ex.a_data, &pos);
  339. flush_icache_range((unsigned long) N_TXTADDR(ex),
  340. (unsigned long) N_TXTADDR(ex) +
  341. ex.a_text+ex.a_data);
  342. goto beyond_if;
  343. }
  344. down_write(&current->mm->mmap_sem);
  345. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  346. PROT_READ | PROT_EXEC,
  347. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
  348. MAP_EXECUTABLE | MAP_32BIT,
  349. fd_offset);
  350. up_write(&current->mm->mmap_sem);
  351. if (error != N_TXTADDR(ex)) {
  352. send_sig(SIGKILL, current, 0);
  353. return error;
  354. }
  355. down_write(&current->mm->mmap_sem);
  356. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  357. PROT_READ | PROT_WRITE | PROT_EXEC,
  358. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
  359. MAP_EXECUTABLE | MAP_32BIT,
  360. fd_offset + ex.a_text);
  361. up_write(&current->mm->mmap_sem);
  362. if (error != N_DATADDR(ex)) {
  363. send_sig(SIGKILL, current, 0);
  364. return error;
  365. }
  366. }
  367. beyond_if:
  368. set_binfmt(&aout_format);
  369. set_brk(current->mm->start_brk, current->mm->brk);
  370. retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
  371. if (retval < 0) {
  372. /* Someone check-me: is this error path enough? */
  373. send_sig(SIGKILL, current, 0);
  374. return retval;
  375. }
  376. current->mm->start_stack =
  377. (unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
  378. /* start thread */
  379. loadsegment(fs, 0);
  380. loadsegment(ds, __USER32_DS);
  381. loadsegment(es, __USER32_DS);
  382. load_gs_index(0);
  383. (regs)->ip = ex.a_entry;
  384. (regs)->sp = current->mm->start_stack;
  385. (regs)->flags = 0x200;
  386. (regs)->cs = __USER32_CS;
  387. (regs)->ss = __USER32_DS;
  388. regs->r8 = regs->r9 = regs->r10 = regs->r11 =
  389. regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
  390. set_fs(USER_DS);
  391. return 0;
  392. }
  393. static int load_aout_library(struct file *file)
  394. {
  395. struct inode *inode;
  396. unsigned long bss, start_addr, len, error;
  397. int retval;
  398. struct exec ex;
  399. inode = file->f_path.dentry->d_inode;
  400. retval = -ENOEXEC;
  401. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  402. if (error != sizeof(ex))
  403. goto out;
  404. /* We come in here for the regular a.out style of shared libraries */
  405. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  406. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  407. i_size_read(inode) <
  408. ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  409. goto out;
  410. }
  411. if (N_FLAGS(ex))
  412. goto out;
  413. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  414. this off to get the starting address for the page */
  415. start_addr = ex.a_entry & 0xfffff000;
  416. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  417. loff_t pos = N_TXTOFF(ex);
  418. #ifdef WARN_OLD
  419. static unsigned long error_time;
  420. if (time_after(jiffies, error_time + 5*HZ)) {
  421. printk(KERN_WARNING
  422. "N_TXTOFF is not page aligned. Please convert "
  423. "library: %s\n",
  424. file->f_path.dentry->d_name.name);
  425. error_time = jiffies;
  426. }
  427. #endif
  428. down_write(&current->mm->mmap_sem);
  429. do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  430. up_write(&current->mm->mmap_sem);
  431. file->f_op->read(file, (char __user *)start_addr,
  432. ex.a_text + ex.a_data, &pos);
  433. flush_icache_range((unsigned long) start_addr,
  434. (unsigned long) start_addr + ex.a_text +
  435. ex.a_data);
  436. retval = 0;
  437. goto out;
  438. }
  439. /* Now use mmap to map the library into memory. */
  440. down_write(&current->mm->mmap_sem);
  441. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  442. PROT_READ | PROT_WRITE | PROT_EXEC,
  443. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT,
  444. N_TXTOFF(ex));
  445. up_write(&current->mm->mmap_sem);
  446. retval = error;
  447. if (error != start_addr)
  448. goto out;
  449. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  450. bss = ex.a_text + ex.a_data + ex.a_bss;
  451. if (bss > len) {
  452. down_write(&current->mm->mmap_sem);
  453. error = do_brk(start_addr + len, bss - len);
  454. up_write(&current->mm->mmap_sem);
  455. retval = error;
  456. if (error != start_addr + len)
  457. goto out;
  458. }
  459. retval = 0;
  460. out:
  461. return retval;
  462. }
  463. static int __init init_aout_binfmt(void)
  464. {
  465. return register_binfmt(&aout_format);
  466. }
  467. static void __exit exit_aout_binfmt(void)
  468. {
  469. unregister_binfmt(&aout_format);
  470. }
  471. module_init(init_aout_binfmt);
  472. module_exit(exit_aout_binfmt);
  473. MODULE_LICENSE("GPL");