binfmt_elf.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /*
  2. * linux/fs/binfmt_elf.c
  3. *
  4. * These are the functions used to load ELF format executables as used
  5. * on SVr4 machines. Information on the format may be found in the book
  6. * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
  7. * Tools".
  8. *
  9. * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/time.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/a.out.h>
  19. #include <linux/errno.h>
  20. #include <linux/signal.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/string.h>
  23. #include <linux/file.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/slab.h>
  27. #include <linux/shm.h>
  28. #include <linux/personality.h>
  29. #include <linux/elfcore.h>
  30. #include <linux/init.h>
  31. #include <linux/highuid.h>
  32. #include <linux/smp.h>
  33. #include <linux/compiler.h>
  34. #include <linux/highmem.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/security.h>
  37. #include <linux/syscalls.h>
  38. #include <linux/random.h>
  39. #include <linux/elf.h>
  40. #include <linux/utsname.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/param.h>
  43. #include <asm/page.h>
  44. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
  45. static int load_elf_library(struct file *);
  46. static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
  47. /*
  48. * If we don't support core dumping, then supply a NULL so we
  49. * don't even try.
  50. */
  51. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  52. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file);
  53. #else
  54. #define elf_core_dump NULL
  55. #endif
  56. #if ELF_EXEC_PAGESIZE > PAGE_SIZE
  57. #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
  58. #else
  59. #define ELF_MIN_ALIGN PAGE_SIZE
  60. #endif
  61. #ifndef ELF_CORE_EFLAGS
  62. #define ELF_CORE_EFLAGS 0
  63. #endif
  64. #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
  65. #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
  66. #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
  67. static struct linux_binfmt elf_format = {
  68. .module = THIS_MODULE,
  69. .load_binary = load_elf_binary,
  70. .load_shlib = load_elf_library,
  71. .core_dump = elf_core_dump,
  72. .min_coredump = ELF_EXEC_PAGESIZE,
  73. .hasvdso = 1
  74. };
  75. #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
  76. static int set_brk(unsigned long start, unsigned long end)
  77. {
  78. start = ELF_PAGEALIGN(start);
  79. end = ELF_PAGEALIGN(end);
  80. if (end > start) {
  81. unsigned long addr;
  82. down_write(&current->mm->mmap_sem);
  83. addr = do_brk(start, end - start);
  84. up_write(&current->mm->mmap_sem);
  85. if (BAD_ADDR(addr))
  86. return addr;
  87. }
  88. current->mm->start_brk = current->mm->brk = end;
  89. return 0;
  90. }
  91. /* We need to explicitly zero any fractional pages
  92. after the data section (i.e. bss). This would
  93. contain the junk from the file that should not
  94. be in memory
  95. */
  96. static int padzero(unsigned long elf_bss)
  97. {
  98. unsigned long nbyte;
  99. nbyte = ELF_PAGEOFFSET(elf_bss);
  100. if (nbyte) {
  101. nbyte = ELF_MIN_ALIGN - nbyte;
  102. if (clear_user((void __user *) elf_bss, nbyte))
  103. return -EFAULT;
  104. }
  105. return 0;
  106. }
  107. /* Let's use some macros to make this stack manipulation a litle clearer */
  108. #ifdef CONFIG_STACK_GROWSUP
  109. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
  110. #define STACK_ROUND(sp, items) \
  111. ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
  112. #define STACK_ALLOC(sp, len) ({ \
  113. elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
  114. old_sp; })
  115. #else
  116. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
  117. #define STACK_ROUND(sp, items) \
  118. (((unsigned long) (sp - items)) &~ 15UL)
  119. #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
  120. #endif
  121. static int
  122. create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
  123. int interp_aout, unsigned long load_addr,
  124. unsigned long interp_load_addr)
  125. {
  126. unsigned long p = bprm->p;
  127. int argc = bprm->argc;
  128. int envc = bprm->envc;
  129. elf_addr_t __user *argv;
  130. elf_addr_t __user *envp;
  131. elf_addr_t __user *sp;
  132. elf_addr_t __user *u_platform;
  133. const char *k_platform = ELF_PLATFORM;
  134. int items;
  135. elf_addr_t *elf_info;
  136. int ei_index = 0;
  137. struct task_struct *tsk = current;
  138. struct vm_area_struct *vma;
  139. /*
  140. * If this architecture has a platform capability string, copy it
  141. * to userspace. In some cases (Sparc), this info is impossible
  142. * for userspace to get any other way, in others (i386) it is
  143. * merely difficult.
  144. */
  145. u_platform = NULL;
  146. if (k_platform) {
  147. size_t len = strlen(k_platform) + 1;
  148. /*
  149. * In some cases (e.g. Hyper-Threading), we want to avoid L1
  150. * evictions by the processes running on the same package. One
  151. * thing we can do is to shuffle the initial stack for them.
  152. */
  153. p = arch_align_stack(p);
  154. u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
  155. if (__copy_to_user(u_platform, k_platform, len))
  156. return -EFAULT;
  157. }
  158. /* Create the ELF interpreter info */
  159. elf_info = (elf_addr_t *)current->mm->saved_auxv;
  160. #define NEW_AUX_ENT(id, val) \
  161. do { \
  162. elf_info[ei_index++] = id; \
  163. elf_info[ei_index++] = val; \
  164. } while (0)
  165. #ifdef ARCH_DLINFO
  166. /*
  167. * ARCH_DLINFO must come first so PPC can do its special alignment of
  168. * AUXV.
  169. */
  170. ARCH_DLINFO;
  171. #endif
  172. NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
  173. NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
  174. NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
  175. NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
  176. NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
  177. NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
  178. NEW_AUX_ENT(AT_BASE, interp_load_addr);
  179. NEW_AUX_ENT(AT_FLAGS, 0);
  180. NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
  181. NEW_AUX_ENT(AT_UID, tsk->uid);
  182. NEW_AUX_ENT(AT_EUID, tsk->euid);
  183. NEW_AUX_ENT(AT_GID, tsk->gid);
  184. NEW_AUX_ENT(AT_EGID, tsk->egid);
  185. NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
  186. if (k_platform) {
  187. NEW_AUX_ENT(AT_PLATFORM,
  188. (elf_addr_t)(unsigned long)u_platform);
  189. }
  190. if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
  191. NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
  192. }
  193. #undef NEW_AUX_ENT
  194. /* AT_NULL is zero; clear the rest too */
  195. memset(&elf_info[ei_index], 0,
  196. sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
  197. /* And advance past the AT_NULL entry. */
  198. ei_index += 2;
  199. sp = STACK_ADD(p, ei_index);
  200. items = (argc + 1) + (envc + 1);
  201. if (interp_aout) {
  202. items += 3; /* a.out interpreters require argv & envp too */
  203. } else {
  204. items += 1; /* ELF interpreters only put argc on the stack */
  205. }
  206. bprm->p = STACK_ROUND(sp, items);
  207. /* Point sp at the lowest address on the stack */
  208. #ifdef CONFIG_STACK_GROWSUP
  209. sp = (elf_addr_t __user *)bprm->p - items - ei_index;
  210. bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
  211. #else
  212. sp = (elf_addr_t __user *)bprm->p;
  213. #endif
  214. /*
  215. * Grow the stack manually; some architectures have a limit on how
  216. * far ahead a user-space access may be in order to grow the stack.
  217. */
  218. vma = find_extend_vma(current->mm, bprm->p);
  219. if (!vma)
  220. return -EFAULT;
  221. /* Now, let's put argc (and argv, envp if appropriate) on the stack */
  222. if (__put_user(argc, sp++))
  223. return -EFAULT;
  224. if (interp_aout) {
  225. argv = sp + 2;
  226. envp = argv + argc + 1;
  227. if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
  228. __put_user((elf_addr_t)(unsigned long)envp, sp++))
  229. return -EFAULT;
  230. } else {
  231. argv = sp;
  232. envp = argv + argc + 1;
  233. }
  234. /* Populate argv and envp */
  235. p = current->mm->arg_end = current->mm->arg_start;
  236. while (argc-- > 0) {
  237. size_t len;
  238. if (__put_user((elf_addr_t)p, argv++))
  239. return -EFAULT;
  240. len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
  241. if (!len || len > MAX_ARG_STRLEN)
  242. return 0;
  243. p += len;
  244. }
  245. if (__put_user(0, argv))
  246. return -EFAULT;
  247. current->mm->arg_end = current->mm->env_start = p;
  248. while (envc-- > 0) {
  249. size_t len;
  250. if (__put_user((elf_addr_t)p, envp++))
  251. return -EFAULT;
  252. len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
  253. if (!len || len > MAX_ARG_STRLEN)
  254. return 0;
  255. p += len;
  256. }
  257. if (__put_user(0, envp))
  258. return -EFAULT;
  259. current->mm->env_end = p;
  260. /* Put the elf_info on the stack in the right place. */
  261. sp = (elf_addr_t __user *)envp + 1;
  262. if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
  263. return -EFAULT;
  264. return 0;
  265. }
  266. #ifndef elf_map
  267. static unsigned long elf_map(struct file *filep, unsigned long addr,
  268. struct elf_phdr *eppnt, int prot, int type)
  269. {
  270. unsigned long map_addr;
  271. unsigned long pageoffset = ELF_PAGEOFFSET(eppnt->p_vaddr);
  272. down_write(&current->mm->mmap_sem);
  273. /* mmap() will return -EINVAL if given a zero size, but a
  274. * segment with zero filesize is perfectly valid */
  275. if (eppnt->p_filesz + pageoffset)
  276. map_addr = do_mmap(filep, ELF_PAGESTART(addr),
  277. eppnt->p_filesz + pageoffset, prot, type,
  278. eppnt->p_offset - pageoffset);
  279. else
  280. map_addr = ELF_PAGESTART(addr);
  281. up_write(&current->mm->mmap_sem);
  282. return(map_addr);
  283. }
  284. #endif /* !elf_map */
  285. /* This is much more generalized than the library routine read function,
  286. so we keep this separate. Technically the library read function
  287. is only provided so that we can read a.out libraries that have
  288. an ELF header */
  289. static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
  290. struct file *interpreter, unsigned long *interp_load_addr)
  291. {
  292. struct elf_phdr *elf_phdata;
  293. struct elf_phdr *eppnt;
  294. unsigned long load_addr = 0;
  295. int load_addr_set = 0;
  296. unsigned long last_bss = 0, elf_bss = 0;
  297. unsigned long error = ~0UL;
  298. int retval, i, size;
  299. /* First of all, some simple consistency checks */
  300. if (interp_elf_ex->e_type != ET_EXEC &&
  301. interp_elf_ex->e_type != ET_DYN)
  302. goto out;
  303. if (!elf_check_arch(interp_elf_ex))
  304. goto out;
  305. if (!interpreter->f_op || !interpreter->f_op->mmap)
  306. goto out;
  307. /*
  308. * If the size of this structure has changed, then punt, since
  309. * we will be doing the wrong thing.
  310. */
  311. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
  312. goto out;
  313. if (interp_elf_ex->e_phnum < 1 ||
  314. interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
  315. goto out;
  316. /* Now read in all of the header information */
  317. size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
  318. if (size > ELF_MIN_ALIGN)
  319. goto out;
  320. elf_phdata = kmalloc(size, GFP_KERNEL);
  321. if (!elf_phdata)
  322. goto out;
  323. retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
  324. (char *)elf_phdata,size);
  325. error = -EIO;
  326. if (retval != size) {
  327. if (retval < 0)
  328. error = retval;
  329. goto out_close;
  330. }
  331. eppnt = elf_phdata;
  332. for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
  333. if (eppnt->p_type == PT_LOAD) {
  334. int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
  335. int elf_prot = 0;
  336. unsigned long vaddr = 0;
  337. unsigned long k, map_addr;
  338. if (eppnt->p_flags & PF_R)
  339. elf_prot = PROT_READ;
  340. if (eppnt->p_flags & PF_W)
  341. elf_prot |= PROT_WRITE;
  342. if (eppnt->p_flags & PF_X)
  343. elf_prot |= PROT_EXEC;
  344. vaddr = eppnt->p_vaddr;
  345. if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
  346. elf_type |= MAP_FIXED;
  347. map_addr = elf_map(interpreter, load_addr + vaddr,
  348. eppnt, elf_prot, elf_type);
  349. error = map_addr;
  350. if (BAD_ADDR(map_addr))
  351. goto out_close;
  352. if (!load_addr_set &&
  353. interp_elf_ex->e_type == ET_DYN) {
  354. load_addr = map_addr - ELF_PAGESTART(vaddr);
  355. load_addr_set = 1;
  356. }
  357. /*
  358. * Check to see if the section's size will overflow the
  359. * allowed task size. Note that p_filesz must always be
  360. * <= p_memsize so it's only necessary to check p_memsz.
  361. */
  362. k = load_addr + eppnt->p_vaddr;
  363. if (BAD_ADDR(k) ||
  364. eppnt->p_filesz > eppnt->p_memsz ||
  365. eppnt->p_memsz > TASK_SIZE ||
  366. TASK_SIZE - eppnt->p_memsz < k) {
  367. error = -ENOMEM;
  368. goto out_close;
  369. }
  370. /*
  371. * Find the end of the file mapping for this phdr, and
  372. * keep track of the largest address we see for this.
  373. */
  374. k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
  375. if (k > elf_bss)
  376. elf_bss = k;
  377. /*
  378. * Do the same thing for the memory mapping - between
  379. * elf_bss and last_bss is the bss section.
  380. */
  381. k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
  382. if (k > last_bss)
  383. last_bss = k;
  384. }
  385. }
  386. /*
  387. * Now fill out the bss section. First pad the last page up
  388. * to the page boundary, and then perform a mmap to make sure
  389. * that there are zero-mapped pages up to and including the
  390. * last bss page.
  391. */
  392. if (padzero(elf_bss)) {
  393. error = -EFAULT;
  394. goto out_close;
  395. }
  396. /* What we have mapped so far */
  397. elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
  398. /* Map the last of the bss segment */
  399. if (last_bss > elf_bss) {
  400. down_write(&current->mm->mmap_sem);
  401. error = do_brk(elf_bss, last_bss - elf_bss);
  402. up_write(&current->mm->mmap_sem);
  403. if (BAD_ADDR(error))
  404. goto out_close;
  405. }
  406. *interp_load_addr = load_addr;
  407. error = ((unsigned long)interp_elf_ex->e_entry) + load_addr;
  408. out_close:
  409. kfree(elf_phdata);
  410. out:
  411. return error;
  412. }
  413. static unsigned long load_aout_interp(struct exec *interp_ex,
  414. struct file *interpreter)
  415. {
  416. unsigned long text_data, elf_entry = ~0UL;
  417. char __user * addr;
  418. loff_t offset;
  419. current->mm->end_code = interp_ex->a_text;
  420. text_data = interp_ex->a_text + interp_ex->a_data;
  421. current->mm->end_data = text_data;
  422. current->mm->brk = interp_ex->a_bss + text_data;
  423. switch (N_MAGIC(*interp_ex)) {
  424. case OMAGIC:
  425. offset = 32;
  426. addr = (char __user *)0;
  427. break;
  428. case ZMAGIC:
  429. case QMAGIC:
  430. offset = N_TXTOFF(*interp_ex);
  431. addr = (char __user *)N_TXTADDR(*interp_ex);
  432. break;
  433. default:
  434. goto out;
  435. }
  436. down_write(&current->mm->mmap_sem);
  437. do_brk(0, text_data);
  438. up_write(&current->mm->mmap_sem);
  439. if (!interpreter->f_op || !interpreter->f_op->read)
  440. goto out;
  441. if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
  442. goto out;
  443. flush_icache_range((unsigned long)addr,
  444. (unsigned long)addr + text_data);
  445. down_write(&current->mm->mmap_sem);
  446. do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
  447. interp_ex->a_bss);
  448. up_write(&current->mm->mmap_sem);
  449. elf_entry = interp_ex->a_entry;
  450. out:
  451. return elf_entry;
  452. }
  453. /*
  454. * These are the functions used to load ELF style executables and shared
  455. * libraries. There is no binary dependent code anywhere else.
  456. */
  457. #define INTERPRETER_NONE 0
  458. #define INTERPRETER_AOUT 1
  459. #define INTERPRETER_ELF 2
  460. #ifndef STACK_RND_MASK
  461. #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
  462. #endif
  463. static unsigned long randomize_stack_top(unsigned long stack_top)
  464. {
  465. unsigned int random_variable = 0;
  466. if ((current->flags & PF_RANDOMIZE) &&
  467. !(current->personality & ADDR_NO_RANDOMIZE)) {
  468. random_variable = get_random_int() & STACK_RND_MASK;
  469. random_variable <<= PAGE_SHIFT;
  470. }
  471. #ifdef CONFIG_STACK_GROWSUP
  472. return PAGE_ALIGN(stack_top) + random_variable;
  473. #else
  474. return PAGE_ALIGN(stack_top) - random_variable;
  475. #endif
  476. }
  477. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
  478. {
  479. struct file *interpreter = NULL; /* to shut gcc up */
  480. unsigned long load_addr = 0, load_bias = 0;
  481. int load_addr_set = 0;
  482. char * elf_interpreter = NULL;
  483. unsigned int interpreter_type = INTERPRETER_NONE;
  484. unsigned char ibcs2_interpreter = 0;
  485. unsigned long error;
  486. struct elf_phdr *elf_ppnt, *elf_phdata;
  487. unsigned long elf_bss, elf_brk;
  488. int elf_exec_fileno;
  489. int retval, i;
  490. unsigned int size;
  491. unsigned long elf_entry, interp_load_addr = 0;
  492. unsigned long start_code, end_code, start_data, end_data;
  493. unsigned long reloc_func_desc = 0;
  494. char passed_fileno[6];
  495. struct files_struct *files;
  496. int executable_stack = EXSTACK_DEFAULT;
  497. unsigned long def_flags = 0;
  498. struct {
  499. struct elfhdr elf_ex;
  500. struct elfhdr interp_elf_ex;
  501. struct exec interp_ex;
  502. } *loc;
  503. loc = kmalloc(sizeof(*loc), GFP_KERNEL);
  504. if (!loc) {
  505. retval = -ENOMEM;
  506. goto out_ret;
  507. }
  508. /* Get the exec-header */
  509. loc->elf_ex = *((struct elfhdr *)bprm->buf);
  510. retval = -ENOEXEC;
  511. /* First of all, some simple consistency checks */
  512. if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  513. goto out;
  514. if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
  515. goto out;
  516. if (!elf_check_arch(&loc->elf_ex))
  517. goto out;
  518. if (!bprm->file->f_op||!bprm->file->f_op->mmap)
  519. goto out;
  520. /* Now read in all of the header information */
  521. if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
  522. goto out;
  523. if (loc->elf_ex.e_phnum < 1 ||
  524. loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
  525. goto out;
  526. size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
  527. retval = -ENOMEM;
  528. elf_phdata = kmalloc(size, GFP_KERNEL);
  529. if (!elf_phdata)
  530. goto out;
  531. retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
  532. (char *)elf_phdata, size);
  533. if (retval != size) {
  534. if (retval >= 0)
  535. retval = -EIO;
  536. goto out_free_ph;
  537. }
  538. files = current->files; /* Refcounted so ok */
  539. retval = unshare_files();
  540. if (retval < 0)
  541. goto out_free_ph;
  542. if (files == current->files) {
  543. put_files_struct(files);
  544. files = NULL;
  545. }
  546. /* exec will make our files private anyway, but for the a.out
  547. loader stuff we need to do it earlier */
  548. retval = get_unused_fd();
  549. if (retval < 0)
  550. goto out_free_fh;
  551. get_file(bprm->file);
  552. fd_install(elf_exec_fileno = retval, bprm->file);
  553. elf_ppnt = elf_phdata;
  554. elf_bss = 0;
  555. elf_brk = 0;
  556. start_code = ~0UL;
  557. end_code = 0;
  558. start_data = 0;
  559. end_data = 0;
  560. for (i = 0; i < loc->elf_ex.e_phnum; i++) {
  561. if (elf_ppnt->p_type == PT_INTERP) {
  562. /* This is the program interpreter used for
  563. * shared libraries - for now assume that this
  564. * is an a.out format binary
  565. */
  566. retval = -ENOEXEC;
  567. if (elf_ppnt->p_filesz > PATH_MAX ||
  568. elf_ppnt->p_filesz < 2)
  569. goto out_free_file;
  570. retval = -ENOMEM;
  571. elf_interpreter = kmalloc(elf_ppnt->p_filesz,
  572. GFP_KERNEL);
  573. if (!elf_interpreter)
  574. goto out_free_file;
  575. retval = kernel_read(bprm->file, elf_ppnt->p_offset,
  576. elf_interpreter,
  577. elf_ppnt->p_filesz);
  578. if (retval != elf_ppnt->p_filesz) {
  579. if (retval >= 0)
  580. retval = -EIO;
  581. goto out_free_interp;
  582. }
  583. /* make sure path is NULL terminated */
  584. retval = -ENOEXEC;
  585. if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
  586. goto out_free_interp;
  587. /* If the program interpreter is one of these two,
  588. * then assume an iBCS2 image. Otherwise assume
  589. * a native linux image.
  590. */
  591. if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
  592. strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
  593. ibcs2_interpreter = 1;
  594. /*
  595. * The early SET_PERSONALITY here is so that the lookup
  596. * for the interpreter happens in the namespace of the
  597. * to-be-execed image. SET_PERSONALITY can select an
  598. * alternate root.
  599. *
  600. * However, SET_PERSONALITY is NOT allowed to switch
  601. * this task into the new images's memory mapping
  602. * policy - that is, TASK_SIZE must still evaluate to
  603. * that which is appropriate to the execing application.
  604. * This is because exit_mmap() needs to have TASK_SIZE
  605. * evaluate to the size of the old image.
  606. *
  607. * So if (say) a 64-bit application is execing a 32-bit
  608. * application it is the architecture's responsibility
  609. * to defer changing the value of TASK_SIZE until the
  610. * switch really is going to happen - do this in
  611. * flush_thread(). - akpm
  612. */
  613. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  614. interpreter = open_exec(elf_interpreter);
  615. retval = PTR_ERR(interpreter);
  616. if (IS_ERR(interpreter))
  617. goto out_free_interp;
  618. /*
  619. * If the binary is not readable then enforce
  620. * mm->dumpable = 0 regardless of the interpreter's
  621. * permissions.
  622. */
  623. if (file_permission(interpreter, MAY_READ) < 0)
  624. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  625. retval = kernel_read(interpreter, 0, bprm->buf,
  626. BINPRM_BUF_SIZE);
  627. if (retval != BINPRM_BUF_SIZE) {
  628. if (retval >= 0)
  629. retval = -EIO;
  630. goto out_free_dentry;
  631. }
  632. /* Get the exec headers */
  633. loc->interp_ex = *((struct exec *)bprm->buf);
  634. loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
  635. break;
  636. }
  637. elf_ppnt++;
  638. }
  639. elf_ppnt = elf_phdata;
  640. for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
  641. if (elf_ppnt->p_type == PT_GNU_STACK) {
  642. if (elf_ppnt->p_flags & PF_X)
  643. executable_stack = EXSTACK_ENABLE_X;
  644. else
  645. executable_stack = EXSTACK_DISABLE_X;
  646. break;
  647. }
  648. /* Some simple consistency checks for the interpreter */
  649. if (elf_interpreter) {
  650. interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
  651. /* Now figure out which format our binary is */
  652. if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
  653. (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
  654. (N_MAGIC(loc->interp_ex) != QMAGIC))
  655. interpreter_type = INTERPRETER_ELF;
  656. if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  657. interpreter_type &= ~INTERPRETER_ELF;
  658. retval = -ELIBBAD;
  659. if (!interpreter_type)
  660. goto out_free_dentry;
  661. /* Make sure only one type was selected */
  662. if ((interpreter_type & INTERPRETER_ELF) &&
  663. interpreter_type != INTERPRETER_ELF) {
  664. // FIXME - ratelimit this before re-enabling
  665. // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
  666. interpreter_type = INTERPRETER_ELF;
  667. }
  668. /* Verify the interpreter has a valid arch */
  669. if ((interpreter_type == INTERPRETER_ELF) &&
  670. !elf_check_arch(&loc->interp_elf_ex))
  671. goto out_free_dentry;
  672. } else {
  673. /* Executables without an interpreter also need a personality */
  674. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  675. }
  676. /* OK, we are done with that, now set up the arg stuff,
  677. and then start this sucker up */
  678. if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
  679. char *passed_p = passed_fileno;
  680. sprintf(passed_fileno, "%d", elf_exec_fileno);
  681. if (elf_interpreter) {
  682. retval = copy_strings_kernel(1, &passed_p, bprm);
  683. if (retval)
  684. goto out_free_dentry;
  685. bprm->argc++;
  686. }
  687. }
  688. /* Flush all traces of the currently running executable */
  689. retval = flush_old_exec(bprm);
  690. if (retval)
  691. goto out_free_dentry;
  692. /* Discard our unneeded old files struct */
  693. if (files) {
  694. put_files_struct(files);
  695. files = NULL;
  696. }
  697. /* OK, This is the point of no return */
  698. current->flags &= ~PF_FORKNOEXEC;
  699. current->mm->def_flags = def_flags;
  700. /* Do this immediately, since STACK_TOP as used in setup_arg_pages
  701. may depend on the personality. */
  702. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  703. if (elf_read_implies_exec(loc->elf_ex, executable_stack))
  704. current->personality |= READ_IMPLIES_EXEC;
  705. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  706. current->flags |= PF_RANDOMIZE;
  707. arch_pick_mmap_layout(current->mm);
  708. /* Do this so that we can load the interpreter, if need be. We will
  709. change some of these later */
  710. current->mm->free_area_cache = current->mm->mmap_base;
  711. current->mm->cached_hole_size = 0;
  712. retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
  713. executable_stack);
  714. if (retval < 0) {
  715. send_sig(SIGKILL, current, 0);
  716. goto out_free_dentry;
  717. }
  718. current->mm->start_stack = bprm->p;
  719. /* Now we do a little grungy work by mmaping the ELF image into
  720. the correct location in memory. At this point, we assume that
  721. the image should be loaded at fixed address, not at a variable
  722. address. */
  723. for(i = 0, elf_ppnt = elf_phdata;
  724. i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
  725. int elf_prot = 0, elf_flags;
  726. unsigned long k, vaddr;
  727. if (elf_ppnt->p_type != PT_LOAD)
  728. continue;
  729. if (unlikely (elf_brk > elf_bss)) {
  730. unsigned long nbyte;
  731. /* There was a PT_LOAD segment with p_memsz > p_filesz
  732. before this one. Map anonymous pages, if needed,
  733. and clear the area. */
  734. retval = set_brk (elf_bss + load_bias,
  735. elf_brk + load_bias);
  736. if (retval) {
  737. send_sig(SIGKILL, current, 0);
  738. goto out_free_dentry;
  739. }
  740. nbyte = ELF_PAGEOFFSET(elf_bss);
  741. if (nbyte) {
  742. nbyte = ELF_MIN_ALIGN - nbyte;
  743. if (nbyte > elf_brk - elf_bss)
  744. nbyte = elf_brk - elf_bss;
  745. if (clear_user((void __user *)elf_bss +
  746. load_bias, nbyte)) {
  747. /*
  748. * This bss-zeroing can fail if the ELF
  749. * file specifies odd protections. So
  750. * we don't check the return value
  751. */
  752. }
  753. }
  754. }
  755. if (elf_ppnt->p_flags & PF_R)
  756. elf_prot |= PROT_READ;
  757. if (elf_ppnt->p_flags & PF_W)
  758. elf_prot |= PROT_WRITE;
  759. if (elf_ppnt->p_flags & PF_X)
  760. elf_prot |= PROT_EXEC;
  761. elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
  762. vaddr = elf_ppnt->p_vaddr;
  763. if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
  764. elf_flags |= MAP_FIXED;
  765. } else if (loc->elf_ex.e_type == ET_DYN) {
  766. /* Try and get dynamic programs out of the way of the
  767. * default mmap base, as well as whatever program they
  768. * might try to exec. This is because the brk will
  769. * follow the loader, and is not movable. */
  770. load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
  771. }
  772. error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
  773. elf_prot, elf_flags);
  774. if (BAD_ADDR(error)) {
  775. send_sig(SIGKILL, current, 0);
  776. retval = IS_ERR((void *)error) ?
  777. PTR_ERR((void*)error) : -EINVAL;
  778. goto out_free_dentry;
  779. }
  780. if (!load_addr_set) {
  781. load_addr_set = 1;
  782. load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
  783. if (loc->elf_ex.e_type == ET_DYN) {
  784. load_bias += error -
  785. ELF_PAGESTART(load_bias + vaddr);
  786. load_addr += load_bias;
  787. reloc_func_desc = load_bias;
  788. }
  789. }
  790. k = elf_ppnt->p_vaddr;
  791. if (k < start_code)
  792. start_code = k;
  793. if (start_data < k)
  794. start_data = k;
  795. /*
  796. * Check to see if the section's size will overflow the
  797. * allowed task size. Note that p_filesz must always be
  798. * <= p_memsz so it is only necessary to check p_memsz.
  799. */
  800. if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
  801. elf_ppnt->p_memsz > TASK_SIZE ||
  802. TASK_SIZE - elf_ppnt->p_memsz < k) {
  803. /* set_brk can never work. Avoid overflows. */
  804. send_sig(SIGKILL, current, 0);
  805. retval = -EINVAL;
  806. goto out_free_dentry;
  807. }
  808. k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  809. if (k > elf_bss)
  810. elf_bss = k;
  811. if ((elf_ppnt->p_flags & PF_X) && end_code < k)
  812. end_code = k;
  813. if (end_data < k)
  814. end_data = k;
  815. k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  816. if (k > elf_brk)
  817. elf_brk = k;
  818. }
  819. loc->elf_ex.e_entry += load_bias;
  820. elf_bss += load_bias;
  821. elf_brk += load_bias;
  822. start_code += load_bias;
  823. end_code += load_bias;
  824. start_data += load_bias;
  825. end_data += load_bias;
  826. /* Calling set_brk effectively mmaps the pages that we need
  827. * for the bss and break sections. We must do this before
  828. * mapping in the interpreter, to make sure it doesn't wind
  829. * up getting placed where the bss needs to go.
  830. */
  831. retval = set_brk(elf_bss, elf_brk);
  832. if (retval) {
  833. send_sig(SIGKILL, current, 0);
  834. goto out_free_dentry;
  835. }
  836. if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
  837. send_sig(SIGSEGV, current, 0);
  838. retval = -EFAULT; /* Nobody gets to see this, but.. */
  839. goto out_free_dentry;
  840. }
  841. if (elf_interpreter) {
  842. if (interpreter_type == INTERPRETER_AOUT)
  843. elf_entry = load_aout_interp(&loc->interp_ex,
  844. interpreter);
  845. else
  846. elf_entry = load_elf_interp(&loc->interp_elf_ex,
  847. interpreter,
  848. &interp_load_addr);
  849. if (BAD_ADDR(elf_entry)) {
  850. force_sig(SIGSEGV, current);
  851. retval = IS_ERR((void *)elf_entry) ?
  852. (int)elf_entry : -EINVAL;
  853. goto out_free_dentry;
  854. }
  855. reloc_func_desc = interp_load_addr;
  856. allow_write_access(interpreter);
  857. fput(interpreter);
  858. kfree(elf_interpreter);
  859. } else {
  860. elf_entry = loc->elf_ex.e_entry;
  861. if (BAD_ADDR(elf_entry)) {
  862. force_sig(SIGSEGV, current);
  863. retval = -EINVAL;
  864. goto out_free_dentry;
  865. }
  866. }
  867. kfree(elf_phdata);
  868. if (interpreter_type != INTERPRETER_AOUT)
  869. sys_close(elf_exec_fileno);
  870. set_binfmt(&elf_format);
  871. #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
  872. retval = arch_setup_additional_pages(bprm, executable_stack);
  873. if (retval < 0) {
  874. send_sig(SIGKILL, current, 0);
  875. goto out;
  876. }
  877. #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
  878. compute_creds(bprm);
  879. current->flags &= ~PF_FORKNOEXEC;
  880. retval = create_elf_tables(bprm, &loc->elf_ex,
  881. (interpreter_type == INTERPRETER_AOUT),
  882. load_addr, interp_load_addr);
  883. if (retval < 0) {
  884. send_sig(SIGKILL, current, 0);
  885. goto out;
  886. }
  887. /* N.B. passed_fileno might not be initialized? */
  888. if (interpreter_type == INTERPRETER_AOUT)
  889. current->mm->arg_start += strlen(passed_fileno) + 1;
  890. current->mm->end_code = end_code;
  891. current->mm->start_code = start_code;
  892. current->mm->start_data = start_data;
  893. current->mm->end_data = end_data;
  894. current->mm->start_stack = bprm->p;
  895. if (current->personality & MMAP_PAGE_ZERO) {
  896. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  897. and some applications "depend" upon this behavior.
  898. Since we do not have the power to recompile these, we
  899. emulate the SVr4 behavior. Sigh. */
  900. down_write(&current->mm->mmap_sem);
  901. error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
  902. MAP_FIXED | MAP_PRIVATE, 0);
  903. up_write(&current->mm->mmap_sem);
  904. }
  905. #ifdef ELF_PLAT_INIT
  906. /*
  907. * The ABI may specify that certain registers be set up in special
  908. * ways (on i386 %edx is the address of a DT_FINI function, for
  909. * example. In addition, it may also specify (eg, PowerPC64 ELF)
  910. * that the e_entry field is the address of the function descriptor
  911. * for the startup routine, rather than the address of the startup
  912. * routine itself. This macro performs whatever initialization to
  913. * the regs structure is required as well as any relocations to the
  914. * function descriptor entries when executing dynamically links apps.
  915. */
  916. ELF_PLAT_INIT(regs, reloc_func_desc);
  917. #endif
  918. start_thread(regs, elf_entry, bprm->p);
  919. if (unlikely(current->ptrace & PT_PTRACED)) {
  920. if (current->ptrace & PT_TRACE_EXEC)
  921. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  922. else
  923. send_sig(SIGTRAP, current, 0);
  924. }
  925. retval = 0;
  926. out:
  927. kfree(loc);
  928. out_ret:
  929. return retval;
  930. /* error cleanup */
  931. out_free_dentry:
  932. allow_write_access(interpreter);
  933. if (interpreter)
  934. fput(interpreter);
  935. out_free_interp:
  936. kfree(elf_interpreter);
  937. out_free_file:
  938. sys_close(elf_exec_fileno);
  939. out_free_fh:
  940. if (files)
  941. reset_files_struct(current, files);
  942. out_free_ph:
  943. kfree(elf_phdata);
  944. goto out;
  945. }
  946. /* This is really simpleminded and specialized - we are loading an
  947. a.out library that is given an ELF header. */
  948. static int load_elf_library(struct file *file)
  949. {
  950. struct elf_phdr *elf_phdata;
  951. struct elf_phdr *eppnt;
  952. unsigned long elf_bss, bss, len;
  953. int retval, error, i, j;
  954. struct elfhdr elf_ex;
  955. error = -ENOEXEC;
  956. retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
  957. if (retval != sizeof(elf_ex))
  958. goto out;
  959. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  960. goto out;
  961. /* First of all, some simple consistency checks */
  962. if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
  963. !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
  964. goto out;
  965. /* Now read in all of the header information */
  966. j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
  967. /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
  968. error = -ENOMEM;
  969. elf_phdata = kmalloc(j, GFP_KERNEL);
  970. if (!elf_phdata)
  971. goto out;
  972. eppnt = elf_phdata;
  973. error = -ENOEXEC;
  974. retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
  975. if (retval != j)
  976. goto out_free_ph;
  977. for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
  978. if ((eppnt + i)->p_type == PT_LOAD)
  979. j++;
  980. if (j != 1)
  981. goto out_free_ph;
  982. while (eppnt->p_type != PT_LOAD)
  983. eppnt++;
  984. /* Now use mmap to map the library into memory. */
  985. down_write(&current->mm->mmap_sem);
  986. error = do_mmap(file,
  987. ELF_PAGESTART(eppnt->p_vaddr),
  988. (eppnt->p_filesz +
  989. ELF_PAGEOFFSET(eppnt->p_vaddr)),
  990. PROT_READ | PROT_WRITE | PROT_EXEC,
  991. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  992. (eppnt->p_offset -
  993. ELF_PAGEOFFSET(eppnt->p_vaddr)));
  994. up_write(&current->mm->mmap_sem);
  995. if (error != ELF_PAGESTART(eppnt->p_vaddr))
  996. goto out_free_ph;
  997. elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
  998. if (padzero(elf_bss)) {
  999. error = -EFAULT;
  1000. goto out_free_ph;
  1001. }
  1002. len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
  1003. ELF_MIN_ALIGN - 1);
  1004. bss = eppnt->p_memsz + eppnt->p_vaddr;
  1005. if (bss > len) {
  1006. down_write(&current->mm->mmap_sem);
  1007. do_brk(len, bss - len);
  1008. up_write(&current->mm->mmap_sem);
  1009. }
  1010. error = 0;
  1011. out_free_ph:
  1012. kfree(elf_phdata);
  1013. out:
  1014. return error;
  1015. }
  1016. /*
  1017. * Note that some platforms still use traditional core dumps and not
  1018. * the ELF core dump. Each platform can select it as appropriate.
  1019. */
  1020. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  1021. /*
  1022. * ELF core dumper
  1023. *
  1024. * Modelled on fs/exec.c:aout_core_dump()
  1025. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  1026. */
  1027. /*
  1028. * These are the only things you should do on a core-file: use only these
  1029. * functions to write out all the necessary info.
  1030. */
  1031. static int dump_write(struct file *file, const void *addr, int nr)
  1032. {
  1033. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1034. }
  1035. static int dump_seek(struct file *file, loff_t off)
  1036. {
  1037. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1038. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1039. return 0;
  1040. } else {
  1041. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1042. if (!buf)
  1043. return 0;
  1044. while (off > 0) {
  1045. unsigned long n = off;
  1046. if (n > PAGE_SIZE)
  1047. n = PAGE_SIZE;
  1048. if (!dump_write(file, buf, n))
  1049. return 0;
  1050. off -= n;
  1051. }
  1052. free_page((unsigned long)buf);
  1053. }
  1054. return 1;
  1055. }
  1056. /*
  1057. * Decide whether a segment is worth dumping; default is yes to be
  1058. * sure (missing info is worse than too much; etc).
  1059. * Personally I'd include everything, and use the coredump limit...
  1060. *
  1061. * I think we should skip something. But I am not sure how. H.J.
  1062. */
  1063. static int maydump(struct vm_area_struct *vma, unsigned long mm_flags)
  1064. {
  1065. /* The vma can be set up to tell us the answer directly. */
  1066. if (vma->vm_flags & VM_ALWAYSDUMP)
  1067. return 1;
  1068. /* Do not dump I/O mapped devices or special mappings */
  1069. if (vma->vm_flags & (VM_IO | VM_RESERVED))
  1070. return 0;
  1071. /* By default, dump shared memory if mapped from an anonymous file. */
  1072. if (vma->vm_flags & VM_SHARED) {
  1073. if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0)
  1074. return test_bit(MMF_DUMP_ANON_SHARED, &mm_flags);
  1075. else
  1076. return test_bit(MMF_DUMP_MAPPED_SHARED, &mm_flags);
  1077. }
  1078. /* By default, if it hasn't been written to, don't write it out. */
  1079. if (!vma->anon_vma)
  1080. return test_bit(MMF_DUMP_MAPPED_PRIVATE, &mm_flags);
  1081. return test_bit(MMF_DUMP_ANON_PRIVATE, &mm_flags);
  1082. }
  1083. /* An ELF note in memory */
  1084. struct memelfnote
  1085. {
  1086. const char *name;
  1087. int type;
  1088. unsigned int datasz;
  1089. void *data;
  1090. };
  1091. static int notesize(struct memelfnote *en)
  1092. {
  1093. int sz;
  1094. sz = sizeof(struct elf_note);
  1095. sz += roundup(strlen(en->name) + 1, 4);
  1096. sz += roundup(en->datasz, 4);
  1097. return sz;
  1098. }
  1099. #define DUMP_WRITE(addr, nr, foffset) \
  1100. do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
  1101. static int alignfile(struct file *file, loff_t *foffset)
  1102. {
  1103. static const char buf[4] = { 0, };
  1104. DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
  1105. return 1;
  1106. }
  1107. static int writenote(struct memelfnote *men, struct file *file,
  1108. loff_t *foffset)
  1109. {
  1110. struct elf_note en;
  1111. en.n_namesz = strlen(men->name) + 1;
  1112. en.n_descsz = men->datasz;
  1113. en.n_type = men->type;
  1114. DUMP_WRITE(&en, sizeof(en), foffset);
  1115. DUMP_WRITE(men->name, en.n_namesz, foffset);
  1116. if (!alignfile(file, foffset))
  1117. return 0;
  1118. DUMP_WRITE(men->data, men->datasz, foffset);
  1119. if (!alignfile(file, foffset))
  1120. return 0;
  1121. return 1;
  1122. }
  1123. #undef DUMP_WRITE
  1124. #define DUMP_WRITE(addr, nr) \
  1125. if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
  1126. goto end_coredump;
  1127. #define DUMP_SEEK(off) \
  1128. if (!dump_seek(file, (off))) \
  1129. goto end_coredump;
  1130. static void fill_elf_header(struct elfhdr *elf, int segs)
  1131. {
  1132. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1133. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1134. elf->e_ident[EI_DATA] = ELF_DATA;
  1135. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1136. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1137. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1138. elf->e_type = ET_CORE;
  1139. elf->e_machine = ELF_ARCH;
  1140. elf->e_version = EV_CURRENT;
  1141. elf->e_entry = 0;
  1142. elf->e_phoff = sizeof(struct elfhdr);
  1143. elf->e_shoff = 0;
  1144. elf->e_flags = ELF_CORE_EFLAGS;
  1145. elf->e_ehsize = sizeof(struct elfhdr);
  1146. elf->e_phentsize = sizeof(struct elf_phdr);
  1147. elf->e_phnum = segs;
  1148. elf->e_shentsize = 0;
  1149. elf->e_shnum = 0;
  1150. elf->e_shstrndx = 0;
  1151. return;
  1152. }
  1153. static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1154. {
  1155. phdr->p_type = PT_NOTE;
  1156. phdr->p_offset = offset;
  1157. phdr->p_vaddr = 0;
  1158. phdr->p_paddr = 0;
  1159. phdr->p_filesz = sz;
  1160. phdr->p_memsz = 0;
  1161. phdr->p_flags = 0;
  1162. phdr->p_align = 0;
  1163. return;
  1164. }
  1165. static void fill_note(struct memelfnote *note, const char *name, int type,
  1166. unsigned int sz, void *data)
  1167. {
  1168. note->name = name;
  1169. note->type = type;
  1170. note->datasz = sz;
  1171. note->data = data;
  1172. return;
  1173. }
  1174. /*
  1175. * fill up all the fields in prstatus from the given task struct, except
  1176. * registers which need to be filled up separately.
  1177. */
  1178. static void fill_prstatus(struct elf_prstatus *prstatus,
  1179. struct task_struct *p, long signr)
  1180. {
  1181. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1182. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1183. prstatus->pr_sighold = p->blocked.sig[0];
  1184. prstatus->pr_pid = p->pid;
  1185. prstatus->pr_ppid = p->parent->pid;
  1186. prstatus->pr_pgrp = process_group(p);
  1187. prstatus->pr_sid = process_session(p);
  1188. if (thread_group_leader(p)) {
  1189. /*
  1190. * This is the record for the group leader. Add in the
  1191. * cumulative times of previous dead threads. This total
  1192. * won't include the time of each live thread whose state
  1193. * is included in the core dump. The final total reported
  1194. * to our parent process when it calls wait4 will include
  1195. * those sums as well as the little bit more time it takes
  1196. * this and each other thread to finish dying after the
  1197. * core dump synchronization phase.
  1198. */
  1199. cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
  1200. &prstatus->pr_utime);
  1201. cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
  1202. &prstatus->pr_stime);
  1203. } else {
  1204. cputime_to_timeval(p->utime, &prstatus->pr_utime);
  1205. cputime_to_timeval(p->stime, &prstatus->pr_stime);
  1206. }
  1207. cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
  1208. cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
  1209. }
  1210. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1211. struct mm_struct *mm)
  1212. {
  1213. unsigned int i, len;
  1214. /* first copy the parameters from user space */
  1215. memset(psinfo, 0, sizeof(struct elf_prpsinfo));
  1216. len = mm->arg_end - mm->arg_start;
  1217. if (len >= ELF_PRARGSZ)
  1218. len = ELF_PRARGSZ-1;
  1219. if (copy_from_user(&psinfo->pr_psargs,
  1220. (const char __user *)mm->arg_start, len))
  1221. return -EFAULT;
  1222. for(i = 0; i < len; i++)
  1223. if (psinfo->pr_psargs[i] == 0)
  1224. psinfo->pr_psargs[i] = ' ';
  1225. psinfo->pr_psargs[len] = 0;
  1226. psinfo->pr_pid = p->pid;
  1227. psinfo->pr_ppid = p->parent->pid;
  1228. psinfo->pr_pgrp = process_group(p);
  1229. psinfo->pr_sid = process_session(p);
  1230. i = p->state ? ffz(~p->state) + 1 : 0;
  1231. psinfo->pr_state = i;
  1232. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1233. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1234. psinfo->pr_nice = task_nice(p);
  1235. psinfo->pr_flag = p->flags;
  1236. SET_UID(psinfo->pr_uid, p->uid);
  1237. SET_GID(psinfo->pr_gid, p->gid);
  1238. strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
  1239. return 0;
  1240. }
  1241. /* Here is the structure in which status of each thread is captured. */
  1242. struct elf_thread_status
  1243. {
  1244. struct list_head list;
  1245. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  1246. elf_fpregset_t fpu; /* NT_PRFPREG */
  1247. struct task_struct *thread;
  1248. #ifdef ELF_CORE_COPY_XFPREGS
  1249. elf_fpxregset_t xfpu; /* NT_PRXFPREG */
  1250. #endif
  1251. struct memelfnote notes[3];
  1252. int num_notes;
  1253. };
  1254. /*
  1255. * In order to add the specific thread information for the elf file format,
  1256. * we need to keep a linked list of every threads pr_status and then create
  1257. * a single section for them in the final core file.
  1258. */
  1259. static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
  1260. {
  1261. int sz = 0;
  1262. struct task_struct *p = t->thread;
  1263. t->num_notes = 0;
  1264. fill_prstatus(&t->prstatus, p, signr);
  1265. elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
  1266. fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
  1267. &(t->prstatus));
  1268. t->num_notes++;
  1269. sz += notesize(&t->notes[0]);
  1270. if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
  1271. &t->fpu))) {
  1272. fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
  1273. &(t->fpu));
  1274. t->num_notes++;
  1275. sz += notesize(&t->notes[1]);
  1276. }
  1277. #ifdef ELF_CORE_COPY_XFPREGS
  1278. if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
  1279. fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
  1280. &t->xfpu);
  1281. t->num_notes++;
  1282. sz += notesize(&t->notes[2]);
  1283. }
  1284. #endif
  1285. return sz;
  1286. }
  1287. static struct vm_area_struct *first_vma(struct task_struct *tsk,
  1288. struct vm_area_struct *gate_vma)
  1289. {
  1290. struct vm_area_struct *ret = tsk->mm->mmap;
  1291. if (ret)
  1292. return ret;
  1293. return gate_vma;
  1294. }
  1295. /*
  1296. * Helper function for iterating across a vma list. It ensures that the caller
  1297. * will visit `gate_vma' prior to terminating the search.
  1298. */
  1299. static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
  1300. struct vm_area_struct *gate_vma)
  1301. {
  1302. struct vm_area_struct *ret;
  1303. ret = this_vma->vm_next;
  1304. if (ret)
  1305. return ret;
  1306. if (this_vma == gate_vma)
  1307. return NULL;
  1308. return gate_vma;
  1309. }
  1310. /*
  1311. * Actual dumper
  1312. *
  1313. * This is a two-pass process; first we find the offsets of the bits,
  1314. * and then they are actually written out. If we run out of core limit
  1315. * we just truncate.
  1316. */
  1317. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file)
  1318. {
  1319. #define NUM_NOTES 6
  1320. int has_dumped = 0;
  1321. mm_segment_t fs;
  1322. int segs;
  1323. size_t size = 0;
  1324. int i;
  1325. struct vm_area_struct *vma, *gate_vma;
  1326. struct elfhdr *elf = NULL;
  1327. loff_t offset = 0, dataoff, foffset;
  1328. unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1329. int numnote;
  1330. struct memelfnote *notes = NULL;
  1331. struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
  1332. struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
  1333. struct task_struct *g, *p;
  1334. LIST_HEAD(thread_list);
  1335. struct list_head *t;
  1336. elf_fpregset_t *fpu = NULL;
  1337. #ifdef ELF_CORE_COPY_XFPREGS
  1338. elf_fpxregset_t *xfpu = NULL;
  1339. #endif
  1340. int thread_status_size = 0;
  1341. elf_addr_t *auxv;
  1342. unsigned long mm_flags;
  1343. #ifdef ELF_CORE_WRITE_EXTRA_NOTES
  1344. int extra_notes_size;
  1345. #endif
  1346. /*
  1347. * We no longer stop all VM operations.
  1348. *
  1349. * This is because those proceses that could possibly change map_count
  1350. * or the mmap / vma pages are now blocked in do_exit on current
  1351. * finishing this core dump.
  1352. *
  1353. * Only ptrace can touch these memory addresses, but it doesn't change
  1354. * the map_count or the pages allocated. So no possibility of crashing
  1355. * exists while dumping the mm->vm_next areas to the core file.
  1356. */
  1357. /* alloc memory for large data structures: too large to be on stack */
  1358. elf = kmalloc(sizeof(*elf), GFP_KERNEL);
  1359. if (!elf)
  1360. goto cleanup;
  1361. prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
  1362. if (!prstatus)
  1363. goto cleanup;
  1364. psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
  1365. if (!psinfo)
  1366. goto cleanup;
  1367. notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
  1368. if (!notes)
  1369. goto cleanup;
  1370. fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
  1371. if (!fpu)
  1372. goto cleanup;
  1373. #ifdef ELF_CORE_COPY_XFPREGS
  1374. xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
  1375. if (!xfpu)
  1376. goto cleanup;
  1377. #endif
  1378. if (signr) {
  1379. struct elf_thread_status *tmp;
  1380. rcu_read_lock();
  1381. do_each_thread(g,p)
  1382. if (current->mm == p->mm && current != p) {
  1383. tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
  1384. if (!tmp) {
  1385. rcu_read_unlock();
  1386. goto cleanup;
  1387. }
  1388. tmp->thread = p;
  1389. list_add(&tmp->list, &thread_list);
  1390. }
  1391. while_each_thread(g,p);
  1392. rcu_read_unlock();
  1393. list_for_each(t, &thread_list) {
  1394. struct elf_thread_status *tmp;
  1395. int sz;
  1396. tmp = list_entry(t, struct elf_thread_status, list);
  1397. sz = elf_dump_thread_status(signr, tmp);
  1398. thread_status_size += sz;
  1399. }
  1400. }
  1401. /* now collect the dump for the current */
  1402. memset(prstatus, 0, sizeof(*prstatus));
  1403. fill_prstatus(prstatus, current, signr);
  1404. elf_core_copy_regs(&prstatus->pr_reg, regs);
  1405. segs = current->mm->map_count;
  1406. #ifdef ELF_CORE_EXTRA_PHDRS
  1407. segs += ELF_CORE_EXTRA_PHDRS;
  1408. #endif
  1409. gate_vma = get_gate_vma(current);
  1410. if (gate_vma != NULL)
  1411. segs++;
  1412. /* Set up header */
  1413. fill_elf_header(elf, segs + 1); /* including notes section */
  1414. has_dumped = 1;
  1415. current->flags |= PF_DUMPCORE;
  1416. /*
  1417. * Set up the notes in similar form to SVR4 core dumps made
  1418. * with info from their /proc.
  1419. */
  1420. fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
  1421. fill_psinfo(psinfo, current->group_leader, current->mm);
  1422. fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
  1423. numnote = 2;
  1424. auxv = (elf_addr_t *)current->mm->saved_auxv;
  1425. i = 0;
  1426. do
  1427. i += 2;
  1428. while (auxv[i - 2] != AT_NULL);
  1429. fill_note(&notes[numnote++], "CORE", NT_AUXV,
  1430. i * sizeof(elf_addr_t), auxv);
  1431. /* Try to dump the FPU. */
  1432. if ((prstatus->pr_fpvalid =
  1433. elf_core_copy_task_fpregs(current, regs, fpu)))
  1434. fill_note(notes + numnote++,
  1435. "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
  1436. #ifdef ELF_CORE_COPY_XFPREGS
  1437. if (elf_core_copy_task_xfpregs(current, xfpu))
  1438. fill_note(notes + numnote++,
  1439. "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
  1440. #endif
  1441. fs = get_fs();
  1442. set_fs(KERNEL_DS);
  1443. DUMP_WRITE(elf, sizeof(*elf));
  1444. offset += sizeof(*elf); /* Elf header */
  1445. offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
  1446. foffset = offset;
  1447. /* Write notes phdr entry */
  1448. {
  1449. struct elf_phdr phdr;
  1450. int sz = 0;
  1451. for (i = 0; i < numnote; i++)
  1452. sz += notesize(notes + i);
  1453. sz += thread_status_size;
  1454. #ifdef ELF_CORE_WRITE_EXTRA_NOTES
  1455. extra_notes_size = ELF_CORE_EXTRA_NOTES_SIZE;
  1456. sz += extra_notes_size;
  1457. #endif
  1458. fill_elf_note_phdr(&phdr, sz, offset);
  1459. offset += sz;
  1460. DUMP_WRITE(&phdr, sizeof(phdr));
  1461. }
  1462. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1463. /*
  1464. * We must use the same mm->flags while dumping core to avoid
  1465. * inconsistency between the program headers and bodies, otherwise an
  1466. * unusable core file can be generated.
  1467. */
  1468. mm_flags = current->mm->flags;
  1469. /* Write program headers for segments dump */
  1470. for (vma = first_vma(current, gate_vma); vma != NULL;
  1471. vma = next_vma(vma, gate_vma)) {
  1472. struct elf_phdr phdr;
  1473. size_t sz;
  1474. sz = vma->vm_end - vma->vm_start;
  1475. phdr.p_type = PT_LOAD;
  1476. phdr.p_offset = offset;
  1477. phdr.p_vaddr = vma->vm_start;
  1478. phdr.p_paddr = 0;
  1479. phdr.p_filesz = maydump(vma, mm_flags) ? sz : 0;
  1480. phdr.p_memsz = sz;
  1481. offset += phdr.p_filesz;
  1482. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1483. if (vma->vm_flags & VM_WRITE)
  1484. phdr.p_flags |= PF_W;
  1485. if (vma->vm_flags & VM_EXEC)
  1486. phdr.p_flags |= PF_X;
  1487. phdr.p_align = ELF_EXEC_PAGESIZE;
  1488. DUMP_WRITE(&phdr, sizeof(phdr));
  1489. }
  1490. #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
  1491. ELF_CORE_WRITE_EXTRA_PHDRS;
  1492. #endif
  1493. /* write out the notes section */
  1494. for (i = 0; i < numnote; i++)
  1495. if (!writenote(notes + i, file, &foffset))
  1496. goto end_coredump;
  1497. #ifdef ELF_CORE_WRITE_EXTRA_NOTES
  1498. ELF_CORE_WRITE_EXTRA_NOTES;
  1499. foffset += extra_notes_size;
  1500. #endif
  1501. /* write out the thread status notes section */
  1502. list_for_each(t, &thread_list) {
  1503. struct elf_thread_status *tmp =
  1504. list_entry(t, struct elf_thread_status, list);
  1505. for (i = 0; i < tmp->num_notes; i++)
  1506. if (!writenote(&tmp->notes[i], file, &foffset))
  1507. goto end_coredump;
  1508. }
  1509. /* Align to page */
  1510. DUMP_SEEK(dataoff - foffset);
  1511. for (vma = first_vma(current, gate_vma); vma != NULL;
  1512. vma = next_vma(vma, gate_vma)) {
  1513. unsigned long addr;
  1514. if (!maydump(vma, mm_flags))
  1515. continue;
  1516. for (addr = vma->vm_start;
  1517. addr < vma->vm_end;
  1518. addr += PAGE_SIZE) {
  1519. struct page *page;
  1520. struct vm_area_struct *vma;
  1521. if (get_user_pages(current, current->mm, addr, 1, 0, 1,
  1522. &page, &vma) <= 0) {
  1523. DUMP_SEEK(PAGE_SIZE);
  1524. } else {
  1525. if (page == ZERO_PAGE(addr)) {
  1526. if (!dump_seek(file, PAGE_SIZE)) {
  1527. page_cache_release(page);
  1528. goto end_coredump;
  1529. }
  1530. } else {
  1531. void *kaddr;
  1532. flush_cache_page(vma, addr,
  1533. page_to_pfn(page));
  1534. kaddr = kmap(page);
  1535. if ((size += PAGE_SIZE) > limit ||
  1536. !dump_write(file, kaddr,
  1537. PAGE_SIZE)) {
  1538. kunmap(page);
  1539. page_cache_release(page);
  1540. goto end_coredump;
  1541. }
  1542. kunmap(page);
  1543. }
  1544. page_cache_release(page);
  1545. }
  1546. }
  1547. }
  1548. #ifdef ELF_CORE_WRITE_EXTRA_DATA
  1549. ELF_CORE_WRITE_EXTRA_DATA;
  1550. #endif
  1551. end_coredump:
  1552. set_fs(fs);
  1553. cleanup:
  1554. while (!list_empty(&thread_list)) {
  1555. struct list_head *tmp = thread_list.next;
  1556. list_del(tmp);
  1557. kfree(list_entry(tmp, struct elf_thread_status, list));
  1558. }
  1559. kfree(elf);
  1560. kfree(prstatus);
  1561. kfree(psinfo);
  1562. kfree(notes);
  1563. kfree(fpu);
  1564. #ifdef ELF_CORE_COPY_XFPREGS
  1565. kfree(xfpu);
  1566. #endif
  1567. return has_dumped;
  1568. #undef NUM_NOTES
  1569. }
  1570. #endif /* USE_ELF_CORE_DUMP */
  1571. static int __init init_elf_binfmt(void)
  1572. {
  1573. return register_binfmt(&elf_format);
  1574. }
  1575. static void __exit exit_elf_binfmt(void)
  1576. {
  1577. /* Remove the COFF and ELF loaders. */
  1578. unregister_binfmt(&elf_format);
  1579. }
  1580. core_initcall(init_elf_binfmt);
  1581. module_exit(exit_elf_binfmt);
  1582. MODULE_LICENSE("GPL");