/arch/x86_64/ia32/ia32_aout.c

https://bitbucket.org/evzijst/gittest · C · 529 lines · 407 code · 68 blank · 54 comment · 71 complexity · 1bfa252d5fbce4f50d15b5f8761352fe MD5 · raw file

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