PageRenderTime 83ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/binfmt_aout.c

https://github.com/kipill-nn/Kernel-for-Mega
C | 481 lines | 365 code | 59 blank | 57 comment | 76 complexity | 8745cb0288db7ff04c907831d4ce481f MD5 | raw file
  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. #include <asm/a.out-core.h>
  29. static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
  30. static int load_aout_library(struct file*);
  31. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
  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, unsigned long limit)
  80. {
  81. mm_segment_t fs;
  82. int has_dumped = 0;
  83. unsigned long dump_start, dump_size;
  84. struct user dump;
  85. #ifdef __alpha__
  86. # define START_DATA(u) (u.start_data)
  87. #else
  88. # define START_DATA(u) ((u.u_tsize << PAGE_SHIFT) + u.start_code)
  89. #endif
  90. # define START_STACK(u) (u.start_stack)
  91. fs = get_fs();
  92. set_fs(KERNEL_DS);
  93. has_dumped = 1;
  94. current->flags |= PF_DUMPCORE;
  95. strncpy(dump.u_comm, current->comm, sizeof(dump.u_comm));
  96. dump.u_ar0 = offsetof(struct user, regs);
  97. dump.signal = signr;
  98. aout_dump_thread(regs, &dump);
  99. /* If the size of the dump file exceeds the rlimit, then see what would happen
  100. if we wrote the stack, but not the data area. */
  101. if ((dump.u_dsize + dump.u_ssize+1) * PAGE_SIZE > limit)
  102. dump.u_dsize = 0;
  103. /* Make sure we have enough room to write the stack and data areas. */
  104. if ((dump.u_ssize + 1) * PAGE_SIZE > limit)
  105. dump.u_ssize = 0;
  106. /* make sure we actually have a data and stack area to dump */
  107. set_fs(USER_DS);
  108. if (!access_ok(VERIFY_READ, (void __user *)START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
  109. dump.u_dsize = 0;
  110. if (!access_ok(VERIFY_READ, (void __user *)START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
  111. dump.u_ssize = 0;
  112. set_fs(KERNEL_DS);
  113. /* struct user */
  114. DUMP_WRITE(&dump,sizeof(dump));
  115. /* Now dump all of the user data. Include malloced stuff as well */
  116. DUMP_SEEK(PAGE_SIZE);
  117. /* now we start writing out the user space info */
  118. set_fs(USER_DS);
  119. /* Dump the data area */
  120. if (dump.u_dsize != 0) {
  121. dump_start = START_DATA(dump);
  122. dump_size = dump.u_dsize << PAGE_SHIFT;
  123. DUMP_WRITE(dump_start,dump_size);
  124. }
  125. /* Now prepare to dump the stack area */
  126. if (dump.u_ssize != 0) {
  127. dump_start = START_STACK(dump);
  128. dump_size = dump.u_ssize << PAGE_SHIFT;
  129. DUMP_WRITE(dump_start,dump_size);
  130. }
  131. /* Finally dump the task struct. Not be used by gdb, but could be useful */
  132. set_fs(KERNEL_DS);
  133. DUMP_WRITE(current,sizeof(*current));
  134. end_coredump:
  135. set_fs(fs);
  136. return has_dumped;
  137. }
  138. /*
  139. * create_aout_tables() parses the env- and arg-strings in new user
  140. * memory and creates the pointer tables from them, and puts their
  141. * addresses on the "stack", returning the new stack pointer value.
  142. */
  143. static unsigned long __user *create_aout_tables(char __user *p, struct linux_binprm * bprm)
  144. {
  145. char __user * __user *argv;
  146. char __user * __user *envp;
  147. unsigned long __user *sp;
  148. int argc = bprm->argc;
  149. int envc = bprm->envc;
  150. sp = (void __user *)((-(unsigned long)sizeof(char *)) & (unsigned long) p);
  151. #ifdef __alpha__
  152. /* whee.. test-programs are so much fun. */
  153. put_user(0, --sp);
  154. put_user(0, --sp);
  155. if (bprm->loader) {
  156. put_user(0, --sp);
  157. put_user(1003, --sp);
  158. put_user(bprm->loader, --sp);
  159. put_user(1002, --sp);
  160. }
  161. put_user(bprm->exec, --sp);
  162. put_user(1001, --sp);
  163. #endif
  164. sp -= envc+1;
  165. envp = (char __user * __user *) sp;
  166. sp -= argc+1;
  167. argv = (char __user * __user *) sp;
  168. #ifndef __alpha__
  169. put_user((unsigned long) envp,--sp);
  170. put_user((unsigned long) argv,--sp);
  171. #endif
  172. put_user(argc,--sp);
  173. current->mm->arg_start = (unsigned long) p;
  174. while (argc-->0) {
  175. char c;
  176. put_user(p,argv++);
  177. do {
  178. get_user(c,p++);
  179. } while (c);
  180. }
  181. put_user(NULL,argv);
  182. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  183. while (envc-->0) {
  184. char c;
  185. put_user(p,envp++);
  186. do {
  187. get_user(c,p++);
  188. } while (c);
  189. }
  190. put_user(NULL,envp);
  191. current->mm->env_end = (unsigned long) p;
  192. return sp;
  193. }
  194. /*
  195. * These are the functions used to load a.out style executables and shared
  196. * libraries. There is no binary dependent code anywhere else.
  197. */
  198. static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  199. {
  200. struct exec ex;
  201. unsigned long error;
  202. unsigned long fd_offset;
  203. unsigned long rlim;
  204. int retval;
  205. ex = *((struct exec *) bprm->buf); /* exec-header */
  206. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  207. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  208. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  209. i_size_read(bprm->file->f_path.dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  210. return -ENOEXEC;
  211. }
  212. /*
  213. * Requires a mmap handler. This prevents people from using a.out
  214. * as part of an exploit attack against /proc-related vulnerabilities.
  215. */
  216. if (!bprm->file->f_op || !bprm->file->f_op->mmap)
  217. return -ENOEXEC;
  218. fd_offset = N_TXTOFF(ex);
  219. /* Check initial limits. This avoids letting people circumvent
  220. * size limits imposed on them by creating programs with large
  221. * arrays in the data or bss.
  222. */
  223. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  224. if (rlim >= RLIM_INFINITY)
  225. rlim = ~0;
  226. if (ex.a_data + ex.a_bss > rlim)
  227. return -ENOMEM;
  228. /* Flush all traces of the currently running executable */
  229. retval = flush_old_exec(bprm);
  230. if (retval)
  231. return retval;
  232. /* OK, This is the point of no return */
  233. #ifdef __alpha__
  234. SET_AOUT_PERSONALITY(bprm, ex);
  235. #else
  236. set_personality(PER_LINUX);
  237. #endif
  238. current->mm->end_code = ex.a_text +
  239. (current->mm->start_code = N_TXTADDR(ex));
  240. current->mm->end_data = ex.a_data +
  241. (current->mm->start_data = N_DATADDR(ex));
  242. current->mm->brk = ex.a_bss +
  243. (current->mm->start_brk = N_BSSADDR(ex));
  244. current->mm->free_area_cache = current->mm->mmap_base;
  245. current->mm->cached_hole_size = 0;
  246. install_exec_creds(bprm);
  247. current->flags &= ~PF_FORKNOEXEC;
  248. if (N_MAGIC(ex) == OMAGIC) {
  249. unsigned long text_addr, map_size;
  250. loff_t pos;
  251. text_addr = N_TXTADDR(ex);
  252. #ifdef __alpha__
  253. pos = fd_offset;
  254. map_size = ex.a_text+ex.a_data + PAGE_SIZE - 1;
  255. #else
  256. pos = 32;
  257. map_size = ex.a_text+ex.a_data;
  258. #endif
  259. down_write(&current->mm->mmap_sem);
  260. error = do_brk(text_addr & PAGE_MASK, map_size);
  261. up_write(&current->mm->mmap_sem);
  262. if (error != (text_addr & PAGE_MASK)) {
  263. send_sig(SIGKILL, current, 0);
  264. return error;
  265. }
  266. error = bprm->file->f_op->read(bprm->file,
  267. (char __user *)text_addr,
  268. ex.a_text+ex.a_data, &pos);
  269. if ((signed long)error < 0) {
  270. send_sig(SIGKILL, current, 0);
  271. return error;
  272. }
  273. flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
  274. } else {
  275. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  276. (N_MAGIC(ex) != NMAGIC) && printk_ratelimit())
  277. {
  278. printk(KERN_NOTICE "executable not page aligned\n");
  279. }
  280. if ((fd_offset & ~PAGE_MASK) != 0 && printk_ratelimit())
  281. {
  282. printk(KERN_WARNING
  283. "fd_offset is not page aligned. Please convert program: %s\n",
  284. bprm->file->f_path.dentry->d_name.name);
  285. }
  286. if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
  287. loff_t pos = fd_offset;
  288. down_write(&current->mm->mmap_sem);
  289. do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  290. up_write(&current->mm->mmap_sem);
  291. bprm->file->f_op->read(bprm->file,
  292. (char __user *)N_TXTADDR(ex),
  293. ex.a_text+ex.a_data, &pos);
  294. flush_icache_range((unsigned long) N_TXTADDR(ex),
  295. (unsigned long) N_TXTADDR(ex) +
  296. ex.a_text+ex.a_data);
  297. goto beyond_if;
  298. }
  299. down_write(&current->mm->mmap_sem);
  300. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  301. PROT_READ | PROT_EXEC,
  302. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  303. fd_offset);
  304. up_write(&current->mm->mmap_sem);
  305. if (error != N_TXTADDR(ex)) {
  306. send_sig(SIGKILL, current, 0);
  307. return error;
  308. }
  309. down_write(&current->mm->mmap_sem);
  310. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  311. PROT_READ | PROT_WRITE | PROT_EXEC,
  312. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  313. fd_offset + ex.a_text);
  314. up_write(&current->mm->mmap_sem);
  315. if (error != N_DATADDR(ex)) {
  316. send_sig(SIGKILL, current, 0);
  317. return error;
  318. }
  319. }
  320. beyond_if:
  321. set_binfmt(&aout_format);
  322. retval = set_brk(current->mm->start_brk, current->mm->brk);
  323. if (retval < 0) {
  324. send_sig(SIGKILL, current, 0);
  325. return retval;
  326. }
  327. retval = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  328. if (retval < 0) {
  329. /* Someone check-me: is this error path enough? */
  330. send_sig(SIGKILL, current, 0);
  331. return retval;
  332. }
  333. current->mm->start_stack =
  334. (unsigned long) create_aout_tables((char __user *) bprm->p, bprm);
  335. #ifdef __alpha__
  336. regs->gp = ex.a_gpvalue;
  337. #endif
  338. start_thread(regs, ex.a_entry, current->mm->start_stack);
  339. return 0;
  340. }
  341. static int load_aout_library(struct file *file)
  342. {
  343. struct inode * inode;
  344. unsigned long bss, start_addr, len;
  345. unsigned long error;
  346. int retval;
  347. struct exec ex;
  348. inode = file->f_path.dentry->d_inode;
  349. retval = -ENOEXEC;
  350. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  351. if (error != sizeof(ex))
  352. goto out;
  353. /* We come in here for the regular a.out style of shared libraries */
  354. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  355. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  356. i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  357. goto out;
  358. }
  359. /*
  360. * Requires a mmap handler. This prevents people from using a.out
  361. * as part of an exploit attack against /proc-related vulnerabilities.
  362. */
  363. if (!file->f_op || !file->f_op->mmap)
  364. goto out;
  365. if (N_FLAGS(ex))
  366. goto out;
  367. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  368. this off to get the starting address for the page */
  369. start_addr = ex.a_entry & 0xfffff000;
  370. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  371. loff_t pos = N_TXTOFF(ex);
  372. if (printk_ratelimit())
  373. {
  374. printk(KERN_WARNING
  375. "N_TXTOFF is not page aligned. Please convert library: %s\n",
  376. file->f_path.dentry->d_name.name);
  377. }
  378. down_write(&current->mm->mmap_sem);
  379. do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  380. up_write(&current->mm->mmap_sem);
  381. file->f_op->read(file, (char __user *)start_addr,
  382. ex.a_text + ex.a_data, &pos);
  383. flush_icache_range((unsigned long) start_addr,
  384. (unsigned long) start_addr + ex.a_text + ex.a_data);
  385. retval = 0;
  386. goto out;
  387. }
  388. /* Now use mmap to map the library into memory. */
  389. down_write(&current->mm->mmap_sem);
  390. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  391. PROT_READ | PROT_WRITE | PROT_EXEC,
  392. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  393. N_TXTOFF(ex));
  394. up_write(&current->mm->mmap_sem);
  395. retval = error;
  396. if (error != start_addr)
  397. goto out;
  398. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  399. bss = ex.a_text + ex.a_data + ex.a_bss;
  400. if (bss > len) {
  401. down_write(&current->mm->mmap_sem);
  402. error = do_brk(start_addr + len, bss - len);
  403. up_write(&current->mm->mmap_sem);
  404. retval = error;
  405. if (error != start_addr + len)
  406. goto out;
  407. }
  408. retval = 0;
  409. out:
  410. return retval;
  411. }
  412. static int __init init_aout_binfmt(void)
  413. {
  414. return register_binfmt(&aout_format);
  415. }
  416. static void __exit exit_aout_binfmt(void)
  417. {
  418. unregister_binfmt(&aout_format);
  419. }
  420. core_initcall(init_aout_binfmt);
  421. module_exit(exit_aout_binfmt);
  422. MODULE_LICENSE("GPL");