PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/x86/ia32/ia32_aout.c

https://gitlab.com/webhaikal/SenseiRN3
C | 492 lines | 369 code | 67 blank | 56 comment | 65 complexity | e355632dc421f216b8966a28b695ca2d 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/binfmts.h>
  23. #include <linux/personality.h>
  24. #include <linux/init.h>
  25. #include <linux/jiffies.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/user32.h>
  30. #include <asm/ia32.h>
  31. #undef WARN_OLD
  32. #undef CORE_DUMP /* definitely broken */
  33. static int load_aout_binary(struct linux_binprm *);
  34. static int load_aout_library(struct file *);
  35. #ifdef CORE_DUMP
  36. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file,
  37. unsigned long limit);
  38. /*
  39. * fill in the user structure for a core dump..
  40. */
  41. static void dump_thread32(struct pt_regs *regs, struct user32 *dump)
  42. {
  43. u32 fs, gs;
  44. /* changed the size calculations - should hopefully work better. lbt */
  45. dump->magic = CMAGIC;
  46. dump->start_code = 0;
  47. dump->start_stack = regs->sp & ~(PAGE_SIZE - 1);
  48. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  49. dump->u_dsize = ((unsigned long)
  50. (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  51. dump->u_dsize -= dump->u_tsize;
  52. dump->u_ssize = 0;
  53. dump->u_debugreg[0] = current->thread.debugreg0;
  54. dump->u_debugreg[1] = current->thread.debugreg1;
  55. dump->u_debugreg[2] = current->thread.debugreg2;
  56. dump->u_debugreg[3] = current->thread.debugreg3;
  57. dump->u_debugreg[4] = 0;
  58. dump->u_debugreg[5] = 0;
  59. dump->u_debugreg[6] = current->thread.debugreg6;
  60. dump->u_debugreg[7] = current->thread.debugreg7;
  61. if (dump->start_stack < 0xc0000000) {
  62. unsigned long tmp;
  63. tmp = (unsigned long) (0xc0000000 - dump->start_stack);
  64. dump->u_ssize = tmp >> PAGE_SHIFT;
  65. }
  66. dump->regs.bx = regs->bx;
  67. dump->regs.cx = regs->cx;
  68. dump->regs.dx = regs->dx;
  69. dump->regs.si = regs->si;
  70. dump->regs.di = regs->di;
  71. dump->regs.bp = regs->bp;
  72. dump->regs.ax = regs->ax;
  73. dump->regs.ds = current->thread.ds;
  74. dump->regs.es = current->thread.es;
  75. savesegment(fs, fs);
  76. dump->regs.fs = fs;
  77. savesegment(gs, gs);
  78. dump->regs.gs = gs;
  79. dump->regs.orig_ax = regs->orig_ax;
  80. dump->regs.ip = regs->ip;
  81. dump->regs.cs = regs->cs;
  82. dump->regs.flags = regs->flags;
  83. dump->regs.sp = regs->sp;
  84. dump->regs.ss = regs->ss;
  85. #if 1 /* FIXME */
  86. dump->u_fpvalid = 0;
  87. #else
  88. dump->u_fpvalid = dump_fpu(regs, &dump->i387);
  89. #endif
  90. }
  91. #endif
  92. static struct linux_binfmt aout_format = {
  93. .module = THIS_MODULE,
  94. .load_binary = load_aout_binary,
  95. .load_shlib = load_aout_library,
  96. #ifdef CORE_DUMP
  97. .core_dump = aout_core_dump,
  98. #endif
  99. .min_coredump = PAGE_SIZE
  100. };
  101. static void set_brk(unsigned long start, unsigned long end)
  102. {
  103. start = PAGE_ALIGN(start);
  104. end = PAGE_ALIGN(end);
  105. if (end <= start)
  106. return;
  107. vm_brk(start, end - start);
  108. }
  109. #ifdef CORE_DUMP
  110. /*
  111. * These are the only things you should do on a core-file: use only these
  112. * macros to write out all the necessary info.
  113. */
  114. #include <linux/coredump.h>
  115. #define DUMP_WRITE(addr, nr) \
  116. if (!dump_write(file, (void *)(addr), (nr))) \
  117. goto end_coredump;
  118. #define DUMP_SEEK(offset) \
  119. if (!dump_seek(file, offset)) \
  120. goto end_coredump;
  121. #define START_DATA() (u.u_tsize << PAGE_SHIFT)
  122. #define START_STACK(u) (u.start_stack)
  123. /*
  124. * Routine writes a core dump image in the current directory.
  125. * Currently only a stub-function.
  126. *
  127. * Note that setuid/setgid files won't make a core-dump if the uid/gid
  128. * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  129. * field, which also makes sure the core-dumps won't be recursive if the
  130. * dumping of the process results in another error..
  131. */
  132. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file,
  133. unsigned long limit)
  134. {
  135. mm_segment_t fs;
  136. int has_dumped = 0;
  137. unsigned long dump_start, dump_size;
  138. struct user32 dump;
  139. fs = get_fs();
  140. set_fs(KERNEL_DS);
  141. has_dumped = 1;
  142. strncpy(dump.u_comm, current->comm, sizeof(current->comm));
  143. dump.u_ar0 = offsetof(struct user32, regs);
  144. dump.signal = signr;
  145. dump_thread32(regs, &dump);
  146. /*
  147. * If the size of the dump file exceeds the rlimit, then see
  148. * what would happen if we wrote the stack, but not the data
  149. * area.
  150. */
  151. if ((dump.u_dsize + dump.u_ssize + 1) * PAGE_SIZE > limit)
  152. dump.u_dsize = 0;
  153. /* Make sure we have enough room to write the stack and data areas. */
  154. if ((dump.u_ssize + 1) * PAGE_SIZE > limit)
  155. dump.u_ssize = 0;
  156. /* make sure we actually have a data and stack area to dump */
  157. set_fs(USER_DS);
  158. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_DATA(dump),
  159. dump.u_dsize << PAGE_SHIFT))
  160. dump.u_dsize = 0;
  161. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_STACK(dump),
  162. 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 - sizeof(dump));
  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. end_coredump:
  184. set_fs(fs);
  185. return has_dumped;
  186. }
  187. #endif
  188. /*
  189. * create_aout_tables() parses the env- and arg-strings in new user
  190. * memory and creates the pointer tables from them, and puts their
  191. * addresses on the "stack", returning the new stack pointer value.
  192. */
  193. static u32 __user *create_aout_tables(char __user *p, struct linux_binprm *bprm)
  194. {
  195. u32 __user *argv, *envp, *sp;
  196. int argc = bprm->argc, envc = bprm->envc;
  197. sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p);
  198. sp -= envc+1;
  199. envp = sp;
  200. sp -= argc+1;
  201. argv = sp;
  202. put_user((unsigned long) envp, --sp);
  203. put_user((unsigned long) argv, --sp);
  204. put_user(argc, --sp);
  205. current->mm->arg_start = (unsigned long) p;
  206. while (argc-- > 0) {
  207. char c;
  208. put_user((u32)(unsigned long)p, argv++);
  209. do {
  210. get_user(c, p++);
  211. } while (c);
  212. }
  213. put_user(0, argv);
  214. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  215. while (envc-- > 0) {
  216. char c;
  217. put_user((u32)(unsigned long)p, envp++);
  218. do {
  219. get_user(c, p++);
  220. } while (c);
  221. }
  222. put_user(0, envp);
  223. current->mm->env_end = (unsigned long) p;
  224. return sp;
  225. }
  226. /*
  227. * These are the functions used to load a.out style executables and shared
  228. * libraries. There is no binary dependent code anywhere else.
  229. */
  230. static int load_aout_binary(struct linux_binprm *bprm)
  231. {
  232. unsigned long error, fd_offset, rlim;
  233. struct pt_regs *regs = current_pt_regs();
  234. struct exec ex;
  235. int retval;
  236. ex = *((struct exec *) bprm->buf); /* exec-header */
  237. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  238. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  239. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  240. i_size_read(file_inode(bprm->file)) <
  241. ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  242. return -ENOEXEC;
  243. }
  244. fd_offset = N_TXTOFF(ex);
  245. /* Check initial limits. This avoids letting people circumvent
  246. * size limits imposed on them by creating programs with large
  247. * arrays in the data or bss.
  248. */
  249. rlim = rlimit(RLIMIT_DATA);
  250. if (rlim >= RLIM_INFINITY)
  251. rlim = ~0;
  252. if (ex.a_data + ex.a_bss > rlim)
  253. return -ENOMEM;
  254. /* Flush all traces of the currently running executable */
  255. retval = flush_old_exec(bprm);
  256. if (retval)
  257. return retval;
  258. /* OK, This is the point of no return */
  259. set_personality(PER_LINUX);
  260. set_personality_ia32(false);
  261. setup_new_exec(bprm);
  262. regs->cs = __USER32_CS;
  263. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  264. regs->r13 = regs->r14 = regs->r15 = 0;
  265. current->mm->end_code = ex.a_text +
  266. (current->mm->start_code = N_TXTADDR(ex));
  267. current->mm->end_data = ex.a_data +
  268. (current->mm->start_data = N_DATADDR(ex));
  269. current->mm->brk = ex.a_bss +
  270. (current->mm->start_brk = N_BSSADDR(ex));
  271. retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
  272. if (retval < 0) {
  273. /* Someone check-me: is this error path enough? */
  274. send_sig(SIGKILL, current, 0);
  275. return retval;
  276. }
  277. install_exec_creds(bprm);
  278. if (N_MAGIC(ex) == OMAGIC) {
  279. unsigned long text_addr, map_size;
  280. text_addr = N_TXTADDR(ex);
  281. map_size = ex.a_text+ex.a_data;
  282. error = vm_brk(text_addr & PAGE_MASK, map_size);
  283. if (error != (text_addr & PAGE_MASK)) {
  284. send_sig(SIGKILL, current, 0);
  285. return error;
  286. }
  287. error = read_code(bprm->file, text_addr, 32,
  288. ex.a_text + ex.a_data);
  289. if ((signed long)error < 0) {
  290. send_sig(SIGKILL, current, 0);
  291. return error;
  292. }
  293. } else {
  294. #ifdef WARN_OLD
  295. static unsigned long error_time, error_time2;
  296. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  297. (N_MAGIC(ex) != NMAGIC) &&
  298. time_after(jiffies, error_time2 + 5*HZ)) {
  299. printk(KERN_NOTICE "executable not page aligned\n");
  300. error_time2 = jiffies;
  301. }
  302. if ((fd_offset & ~PAGE_MASK) != 0 &&
  303. time_after(jiffies, error_time + 5*HZ)) {
  304. printk(KERN_WARNING
  305. "fd_offset is not page aligned. Please convert "
  306. "program: %s\n",
  307. bprm->file->f_path.dentry->d_name.name);
  308. error_time = jiffies;
  309. }
  310. #endif
  311. if (!bprm->file->f_op->mmap || (fd_offset & ~PAGE_MASK) != 0) {
  312. vm_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  313. read_code(bprm->file, N_TXTADDR(ex), fd_offset,
  314. ex.a_text+ex.a_data);
  315. goto beyond_if;
  316. }
  317. error = vm_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  318. PROT_READ | PROT_EXEC,
  319. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
  320. MAP_EXECUTABLE | MAP_32BIT,
  321. fd_offset);
  322. if (error != N_TXTADDR(ex)) {
  323. send_sig(SIGKILL, current, 0);
  324. return error;
  325. }
  326. error = vm_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  327. PROT_READ | PROT_WRITE | PROT_EXEC,
  328. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
  329. MAP_EXECUTABLE | MAP_32BIT,
  330. fd_offset + ex.a_text);
  331. if (error != N_DATADDR(ex)) {
  332. send_sig(SIGKILL, current, 0);
  333. return error;
  334. }
  335. }
  336. beyond_if:
  337. set_binfmt(&aout_format);
  338. set_brk(current->mm->start_brk, current->mm->brk);
  339. current->mm->start_stack =
  340. (unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
  341. /* start thread */
  342. loadsegment(fs, 0);
  343. loadsegment(ds, __USER32_DS);
  344. loadsegment(es, __USER32_DS);
  345. load_gs_index(0);
  346. (regs)->ip = ex.a_entry;
  347. (regs)->sp = current->mm->start_stack;
  348. (regs)->flags = 0x200;
  349. (regs)->cs = __USER32_CS;
  350. (regs)->ss = __USER32_DS;
  351. regs->r8 = regs->r9 = regs->r10 = regs->r11 =
  352. regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
  353. set_fs(USER_DS);
  354. return 0;
  355. }
  356. static int load_aout_library(struct file *file)
  357. {
  358. unsigned long bss, start_addr, len, error;
  359. int retval;
  360. struct exec ex;
  361. retval = -ENOEXEC;
  362. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  363. if (error != sizeof(ex))
  364. goto out;
  365. /* We come in here for the regular a.out style of shared libraries */
  366. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  367. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  368. i_size_read(file_inode(file)) <
  369. ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  370. goto out;
  371. }
  372. if (N_FLAGS(ex))
  373. goto out;
  374. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  375. this off to get the starting address for the page */
  376. start_addr = ex.a_entry & 0xfffff000;
  377. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  378. #ifdef WARN_OLD
  379. static unsigned long error_time;
  380. if (time_after(jiffies, error_time + 5*HZ)) {
  381. printk(KERN_WARNING
  382. "N_TXTOFF is not page aligned. Please convert "
  383. "library: %s\n",
  384. file->f_path.dentry->d_name.name);
  385. error_time = jiffies;
  386. }
  387. #endif
  388. vm_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  389. read_code(file, start_addr, N_TXTOFF(ex),
  390. ex.a_text + ex.a_data);
  391. retval = 0;
  392. goto out;
  393. }
  394. /* Now use mmap to map the library into memory. */
  395. error = vm_mmap(file, start_addr, ex.a_text + ex.a_data,
  396. PROT_READ | PROT_WRITE | PROT_EXEC,
  397. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT,
  398. N_TXTOFF(ex));
  399. retval = error;
  400. if (error != start_addr)
  401. goto out;
  402. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  403. bss = ex.a_text + ex.a_data + ex.a_bss;
  404. if (bss > len) {
  405. error = vm_brk(start_addr + len, bss - len);
  406. retval = error;
  407. if (error != start_addr + len)
  408. goto out;
  409. }
  410. retval = 0;
  411. out:
  412. return retval;
  413. }
  414. static int __init init_aout_binfmt(void)
  415. {
  416. register_binfmt(&aout_format);
  417. return 0;
  418. }
  419. static void __exit exit_aout_binfmt(void)
  420. {
  421. unregister_binfmt(&aout_format);
  422. }
  423. module_init(init_aout_binfmt);
  424. module_exit(exit_aout_binfmt);
  425. MODULE_LICENSE("GPL");