binfmt_elf_fdpic.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /* binfmt_elf_fdpic.c: FDPIC ELF binary format
  2. *
  3. * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * Derived from binfmt_elf.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/errno.h>
  19. #include <linux/signal.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/string.h>
  22. #include <linux/file.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/slab.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/security.h>
  27. #include <linux/highmem.h>
  28. #include <linux/highuid.h>
  29. #include <linux/personality.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/init.h>
  32. #include <linux/elf.h>
  33. #include <linux/elf-fdpic.h>
  34. #include <linux/elfcore.h>
  35. #include <linux/coredump.h>
  36. #include <linux/dax.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/param.h>
  39. #include <asm/pgalloc.h>
  40. typedef char *elf_caddr_t;
  41. #if 0
  42. #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
  43. #else
  44. #define kdebug(fmt, ...) do {} while(0)
  45. #endif
  46. #if 0
  47. #define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
  48. #else
  49. #define kdcore(fmt, ...) do {} while(0)
  50. #endif
  51. MODULE_LICENSE("GPL");
  52. static int load_elf_fdpic_binary(struct linux_binprm *);
  53. static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
  54. static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
  55. struct mm_struct *, const char *);
  56. static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
  57. struct elf_fdpic_params *,
  58. struct elf_fdpic_params *);
  59. #ifndef CONFIG_MMU
  60. static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
  61. struct file *,
  62. struct mm_struct *);
  63. #endif
  64. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
  65. struct file *, struct mm_struct *);
  66. #ifdef CONFIG_ELF_CORE
  67. static int elf_fdpic_core_dump(struct coredump_params *cprm);
  68. #endif
  69. static struct linux_binfmt elf_fdpic_format = {
  70. .module = THIS_MODULE,
  71. .load_binary = load_elf_fdpic_binary,
  72. #ifdef CONFIG_ELF_CORE
  73. .core_dump = elf_fdpic_core_dump,
  74. #endif
  75. .min_coredump = ELF_EXEC_PAGESIZE,
  76. };
  77. static int __init init_elf_fdpic_binfmt(void)
  78. {
  79. register_binfmt(&elf_fdpic_format);
  80. return 0;
  81. }
  82. static void __exit exit_elf_fdpic_binfmt(void)
  83. {
  84. unregister_binfmt(&elf_fdpic_format);
  85. }
  86. core_initcall(init_elf_fdpic_binfmt);
  87. module_exit(exit_elf_fdpic_binfmt);
  88. static int is_elf(struct elfhdr *hdr, struct file *file)
  89. {
  90. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
  91. return 0;
  92. if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
  93. return 0;
  94. if (!elf_check_arch(hdr))
  95. return 0;
  96. if (!file->f_op->mmap)
  97. return 0;
  98. return 1;
  99. }
  100. #ifndef elf_check_fdpic
  101. #define elf_check_fdpic(x) 0
  102. #endif
  103. #ifndef elf_check_const_displacement
  104. #define elf_check_const_displacement(x) 0
  105. #endif
  106. static int is_constdisp(struct elfhdr *hdr)
  107. {
  108. if (!elf_check_fdpic(hdr))
  109. return 1;
  110. if (elf_check_const_displacement(hdr))
  111. return 1;
  112. return 0;
  113. }
  114. /*****************************************************************************/
  115. /*
  116. * read the program headers table into memory
  117. */
  118. static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
  119. struct file *file)
  120. {
  121. struct elf32_phdr *phdr;
  122. unsigned long size;
  123. int retval, loop;
  124. if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
  125. return -ENOMEM;
  126. if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
  127. return -ENOMEM;
  128. size = params->hdr.e_phnum * sizeof(struct elf_phdr);
  129. params->phdrs = kmalloc(size, GFP_KERNEL);
  130. if (!params->phdrs)
  131. return -ENOMEM;
  132. retval = kernel_read(file, params->hdr.e_phoff,
  133. (char *) params->phdrs, size);
  134. if (unlikely(retval != size))
  135. return retval < 0 ? retval : -ENOEXEC;
  136. /* determine stack size for this binary */
  137. phdr = params->phdrs;
  138. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  139. if (phdr->p_type != PT_GNU_STACK)
  140. continue;
  141. if (phdr->p_flags & PF_X)
  142. params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
  143. else
  144. params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
  145. params->stack_size = phdr->p_memsz;
  146. break;
  147. }
  148. return 0;
  149. }
  150. /*****************************************************************************/
  151. /*
  152. * load an fdpic binary into various bits of memory
  153. */
  154. static int load_elf_fdpic_binary(struct linux_binprm *bprm)
  155. {
  156. struct elf_fdpic_params exec_params, interp_params;
  157. struct pt_regs *regs = current_pt_regs();
  158. struct elf_phdr *phdr;
  159. unsigned long stack_size, entryaddr;
  160. #ifdef ELF_FDPIC_PLAT_INIT
  161. unsigned long dynaddr;
  162. #endif
  163. #ifndef CONFIG_MMU
  164. unsigned long stack_prot;
  165. #endif
  166. struct file *interpreter = NULL; /* to shut gcc up */
  167. char *interpreter_name = NULL;
  168. int executable_stack;
  169. int retval, i;
  170. kdebug("____ LOAD %d ____", current->pid);
  171. memset(&exec_params, 0, sizeof(exec_params));
  172. memset(&interp_params, 0, sizeof(interp_params));
  173. exec_params.hdr = *(struct elfhdr *) bprm->buf;
  174. exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
  175. /* check that this is a binary we know how to deal with */
  176. retval = -ENOEXEC;
  177. if (!is_elf(&exec_params.hdr, bprm->file))
  178. goto error;
  179. if (!elf_check_fdpic(&exec_params.hdr)) {
  180. #ifdef CONFIG_MMU
  181. /* binfmt_elf handles non-fdpic elf except on nommu */
  182. goto error;
  183. #else
  184. /* nommu can only load ET_DYN (PIE) ELF */
  185. if (exec_params.hdr.e_type != ET_DYN)
  186. goto error;
  187. #endif
  188. }
  189. /* read the program header table */
  190. retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
  191. if (retval < 0)
  192. goto error;
  193. /* scan for a program header that specifies an interpreter */
  194. phdr = exec_params.phdrs;
  195. for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
  196. switch (phdr->p_type) {
  197. case PT_INTERP:
  198. retval = -ENOMEM;
  199. if (phdr->p_filesz > PATH_MAX)
  200. goto error;
  201. retval = -ENOENT;
  202. if (phdr->p_filesz < 2)
  203. goto error;
  204. /* read the name of the interpreter into memory */
  205. interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
  206. if (!interpreter_name)
  207. goto error;
  208. retval = kernel_read(bprm->file,
  209. phdr->p_offset,
  210. interpreter_name,
  211. phdr->p_filesz);
  212. if (unlikely(retval != phdr->p_filesz)) {
  213. if (retval >= 0)
  214. retval = -ENOEXEC;
  215. goto error;
  216. }
  217. retval = -ENOENT;
  218. if (interpreter_name[phdr->p_filesz - 1] != '\0')
  219. goto error;
  220. kdebug("Using ELF interpreter %s", interpreter_name);
  221. /* replace the program with the interpreter */
  222. interpreter = open_exec(interpreter_name);
  223. retval = PTR_ERR(interpreter);
  224. if (IS_ERR(interpreter)) {
  225. interpreter = NULL;
  226. goto error;
  227. }
  228. /*
  229. * If the binary is not readable then enforce
  230. * mm->dumpable = 0 regardless of the interpreter's
  231. * permissions.
  232. */
  233. would_dump(bprm, interpreter);
  234. retval = kernel_read(interpreter, 0, bprm->buf,
  235. BINPRM_BUF_SIZE);
  236. if (unlikely(retval != BINPRM_BUF_SIZE)) {
  237. if (retval >= 0)
  238. retval = -ENOEXEC;
  239. goto error;
  240. }
  241. interp_params.hdr = *((struct elfhdr *) bprm->buf);
  242. break;
  243. case PT_LOAD:
  244. #ifdef CONFIG_MMU
  245. if (exec_params.load_addr == 0)
  246. exec_params.load_addr = phdr->p_vaddr;
  247. #endif
  248. break;
  249. }
  250. }
  251. if (is_constdisp(&exec_params.hdr))
  252. exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
  253. /* perform insanity checks on the interpreter */
  254. if (interpreter_name) {
  255. retval = -ELIBBAD;
  256. if (!is_elf(&interp_params.hdr, interpreter))
  257. goto error;
  258. interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
  259. /* read the interpreter's program header table */
  260. retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
  261. if (retval < 0)
  262. goto error;
  263. }
  264. stack_size = exec_params.stack_size;
  265. if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
  266. executable_stack = EXSTACK_ENABLE_X;
  267. else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
  268. executable_stack = EXSTACK_DISABLE_X;
  269. else
  270. executable_stack = EXSTACK_DEFAULT;
  271. if (stack_size == 0) {
  272. stack_size = interp_params.stack_size;
  273. if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
  274. executable_stack = EXSTACK_ENABLE_X;
  275. else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
  276. executable_stack = EXSTACK_DISABLE_X;
  277. else
  278. executable_stack = EXSTACK_DEFAULT;
  279. }
  280. retval = -ENOEXEC;
  281. if (stack_size == 0)
  282. stack_size = 131072UL; /* same as exec.c's default commit */
  283. if (is_constdisp(&interp_params.hdr))
  284. interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
  285. /* flush all traces of the currently running executable */
  286. retval = flush_old_exec(bprm);
  287. if (retval)
  288. goto error;
  289. /* there's now no turning back... the old userspace image is dead,
  290. * defunct, deceased, etc.
  291. */
  292. if (elf_check_fdpic(&exec_params.hdr))
  293. set_personality(PER_LINUX_FDPIC);
  294. else
  295. set_personality(PER_LINUX);
  296. if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
  297. current->personality |= READ_IMPLIES_EXEC;
  298. setup_new_exec(bprm);
  299. set_binfmt(&elf_fdpic_format);
  300. current->mm->start_code = 0;
  301. current->mm->end_code = 0;
  302. current->mm->start_stack = 0;
  303. current->mm->start_data = 0;
  304. current->mm->end_data = 0;
  305. current->mm->context.exec_fdpic_loadmap = 0;
  306. current->mm->context.interp_fdpic_loadmap = 0;
  307. #ifdef CONFIG_MMU
  308. elf_fdpic_arch_lay_out_mm(&exec_params,
  309. &interp_params,
  310. &current->mm->start_stack,
  311. &current->mm->start_brk);
  312. retval = setup_arg_pages(bprm, current->mm->start_stack,
  313. executable_stack);
  314. if (retval < 0)
  315. goto error;
  316. #endif
  317. /* load the executable and interpreter into memory */
  318. retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
  319. "executable");
  320. if (retval < 0)
  321. goto error;
  322. if (interpreter_name) {
  323. retval = elf_fdpic_map_file(&interp_params, interpreter,
  324. current->mm, "interpreter");
  325. if (retval < 0) {
  326. printk(KERN_ERR "Unable to load interpreter\n");
  327. goto error;
  328. }
  329. allow_write_access(interpreter);
  330. fput(interpreter);
  331. interpreter = NULL;
  332. }
  333. #ifdef CONFIG_MMU
  334. if (!current->mm->start_brk)
  335. current->mm->start_brk = current->mm->end_data;
  336. current->mm->brk = current->mm->start_brk =
  337. PAGE_ALIGN(current->mm->start_brk);
  338. #else
  339. /* create a stack area and zero-size brk area */
  340. stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
  341. if (stack_size < PAGE_SIZE * 2)
  342. stack_size = PAGE_SIZE * 2;
  343. stack_prot = PROT_READ | PROT_WRITE;
  344. if (executable_stack == EXSTACK_ENABLE_X ||
  345. (executable_stack == EXSTACK_DEFAULT && VM_STACK_FLAGS & VM_EXEC))
  346. stack_prot |= PROT_EXEC;
  347. current->mm->start_brk = vm_mmap(NULL, 0, stack_size, stack_prot,
  348. MAP_PRIVATE | MAP_ANONYMOUS |
  349. MAP_UNINITIALIZED | MAP_GROWSDOWN,
  350. 0);
  351. if (IS_ERR_VALUE(current->mm->start_brk)) {
  352. retval = current->mm->start_brk;
  353. current->mm->start_brk = 0;
  354. goto error;
  355. }
  356. current->mm->brk = current->mm->start_brk;
  357. current->mm->context.end_brk = current->mm->start_brk;
  358. current->mm->start_stack = current->mm->start_brk + stack_size;
  359. #endif
  360. install_exec_creds(bprm);
  361. if (create_elf_fdpic_tables(bprm, current->mm,
  362. &exec_params, &interp_params) < 0)
  363. goto error;
  364. kdebug("- start_code %lx", current->mm->start_code);
  365. kdebug("- end_code %lx", current->mm->end_code);
  366. kdebug("- start_data %lx", current->mm->start_data);
  367. kdebug("- end_data %lx", current->mm->end_data);
  368. kdebug("- start_brk %lx", current->mm->start_brk);
  369. kdebug("- brk %lx", current->mm->brk);
  370. kdebug("- start_stack %lx", current->mm->start_stack);
  371. #ifdef ELF_FDPIC_PLAT_INIT
  372. /*
  373. * The ABI may specify that certain registers be set up in special
  374. * ways (on i386 %edx is the address of a DT_FINI function, for
  375. * example. This macro performs whatever initialization to
  376. * the regs structure is required.
  377. */
  378. dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
  379. ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
  380. dynaddr);
  381. #endif
  382. /* everything is now ready... get the userspace context ready to roll */
  383. entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
  384. start_thread(regs, entryaddr, current->mm->start_stack);
  385. retval = 0;
  386. error:
  387. if (interpreter) {
  388. allow_write_access(interpreter);
  389. fput(interpreter);
  390. }
  391. kfree(interpreter_name);
  392. kfree(exec_params.phdrs);
  393. kfree(exec_params.loadmap);
  394. kfree(interp_params.phdrs);
  395. kfree(interp_params.loadmap);
  396. return retval;
  397. }
  398. /*****************************************************************************/
  399. #ifndef ELF_BASE_PLATFORM
  400. /*
  401. * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
  402. * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
  403. * will be copied to the user stack in the same manner as AT_PLATFORM.
  404. */
  405. #define ELF_BASE_PLATFORM NULL
  406. #endif
  407. /*
  408. * present useful information to the program by shovelling it onto the new
  409. * process's stack
  410. */
  411. static int create_elf_fdpic_tables(struct linux_binprm *bprm,
  412. struct mm_struct *mm,
  413. struct elf_fdpic_params *exec_params,
  414. struct elf_fdpic_params *interp_params)
  415. {
  416. const struct cred *cred = current_cred();
  417. unsigned long sp, csp, nitems;
  418. elf_caddr_t __user *argv, *envp;
  419. size_t platform_len = 0, len;
  420. char *k_platform, *k_base_platform;
  421. char __user *u_platform, *u_base_platform, *p;
  422. int loop;
  423. int nr; /* reset for each csp adjustment */
  424. #ifdef CONFIG_MMU
  425. /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  426. * by the processes running on the same package. One thing we can do is
  427. * to shuffle the initial stack for them, so we give the architecture
  428. * an opportunity to do so here.
  429. */
  430. sp = arch_align_stack(bprm->p);
  431. #else
  432. sp = mm->start_stack;
  433. /* stack the program arguments and environment */
  434. if (transfer_args_to_stack(bprm, &sp) < 0)
  435. return -EFAULT;
  436. sp &= ~15;
  437. #endif
  438. /*
  439. * If this architecture has a platform capability string, copy it
  440. * to userspace. In some cases (Sparc), this info is impossible
  441. * for userspace to get any other way, in others (i386) it is
  442. * merely difficult.
  443. */
  444. k_platform = ELF_PLATFORM;
  445. u_platform = NULL;
  446. if (k_platform) {
  447. platform_len = strlen(k_platform) + 1;
  448. sp -= platform_len;
  449. u_platform = (char __user *) sp;
  450. if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
  451. return -EFAULT;
  452. }
  453. /*
  454. * If this architecture has a "base" platform capability
  455. * string, copy it to userspace.
  456. */
  457. k_base_platform = ELF_BASE_PLATFORM;
  458. u_base_platform = NULL;
  459. if (k_base_platform) {
  460. platform_len = strlen(k_base_platform) + 1;
  461. sp -= platform_len;
  462. u_base_platform = (char __user *) sp;
  463. if (__copy_to_user(u_base_platform, k_base_platform, platform_len) != 0)
  464. return -EFAULT;
  465. }
  466. sp &= ~7UL;
  467. /* stack the load map(s) */
  468. len = sizeof(struct elf32_fdpic_loadmap);
  469. len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
  470. sp = (sp - len) & ~7UL;
  471. exec_params->map_addr = sp;
  472. if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
  473. return -EFAULT;
  474. current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
  475. if (interp_params->loadmap) {
  476. len = sizeof(struct elf32_fdpic_loadmap);
  477. len += sizeof(struct elf32_fdpic_loadseg) *
  478. interp_params->loadmap->nsegs;
  479. sp = (sp - len) & ~7UL;
  480. interp_params->map_addr = sp;
  481. if (copy_to_user((void __user *) sp, interp_params->loadmap,
  482. len) != 0)
  483. return -EFAULT;
  484. current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
  485. }
  486. /* force 16 byte _final_ alignment here for generality */
  487. #define DLINFO_ITEMS 15
  488. nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0) +
  489. (k_base_platform ? 1 : 0) + AT_VECTOR_SIZE_ARCH;
  490. if (bprm->interp_flags & BINPRM_FLAGS_EXECFD)
  491. nitems++;
  492. csp = sp;
  493. sp -= nitems * 2 * sizeof(unsigned long);
  494. sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
  495. sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
  496. sp -= 1 * sizeof(unsigned long); /* argc */
  497. csp -= sp & 15UL;
  498. sp -= sp & 15UL;
  499. /* put the ELF interpreter info on the stack */
  500. #define NEW_AUX_ENT(id, val) \
  501. do { \
  502. struct { unsigned long _id, _val; } __user *ent; \
  503. \
  504. ent = (void __user *) csp; \
  505. __put_user((id), &ent[nr]._id); \
  506. __put_user((val), &ent[nr]._val); \
  507. nr++; \
  508. } while (0)
  509. nr = 0;
  510. csp -= 2 * sizeof(unsigned long);
  511. NEW_AUX_ENT(AT_NULL, 0);
  512. if (k_platform) {
  513. nr = 0;
  514. csp -= 2 * sizeof(unsigned long);
  515. NEW_AUX_ENT(AT_PLATFORM,
  516. (elf_addr_t) (unsigned long) u_platform);
  517. }
  518. if (k_base_platform) {
  519. nr = 0;
  520. csp -= 2 * sizeof(unsigned long);
  521. NEW_AUX_ENT(AT_BASE_PLATFORM,
  522. (elf_addr_t) (unsigned long) u_base_platform);
  523. }
  524. if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
  525. nr = 0;
  526. csp -= 2 * sizeof(unsigned long);
  527. NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
  528. }
  529. nr = 0;
  530. csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
  531. NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
  532. #ifdef ELF_HWCAP2
  533. NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
  534. #endif
  535. NEW_AUX_ENT(AT_PAGESZ, PAGE_SIZE);
  536. NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
  537. NEW_AUX_ENT(AT_PHDR, exec_params->ph_addr);
  538. NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
  539. NEW_AUX_ENT(AT_PHNUM, exec_params->hdr.e_phnum);
  540. NEW_AUX_ENT(AT_BASE, interp_params->elfhdr_addr);
  541. NEW_AUX_ENT(AT_FLAGS, 0);
  542. NEW_AUX_ENT(AT_ENTRY, exec_params->entry_addr);
  543. NEW_AUX_ENT(AT_UID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
  544. NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
  545. NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
  546. NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
  547. NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
  548. NEW_AUX_ENT(AT_EXECFN, bprm->exec);
  549. #ifdef ARCH_DLINFO
  550. nr = 0;
  551. csp -= AT_VECTOR_SIZE_ARCH * 2 * sizeof(unsigned long);
  552. /* ARCH_DLINFO must come last so platform specific code can enforce
  553. * special alignment requirements on the AUXV if necessary (eg. PPC).
  554. */
  555. ARCH_DLINFO;
  556. #endif
  557. #undef NEW_AUX_ENT
  558. /* allocate room for argv[] and envv[] */
  559. csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
  560. envp = (elf_caddr_t __user *) csp;
  561. csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
  562. argv = (elf_caddr_t __user *) csp;
  563. /* stack argc */
  564. csp -= sizeof(unsigned long);
  565. __put_user(bprm->argc, (unsigned long __user *) csp);
  566. BUG_ON(csp != sp);
  567. /* fill in the argv[] array */
  568. #ifdef CONFIG_MMU
  569. current->mm->arg_start = bprm->p;
  570. #else
  571. current->mm->arg_start = current->mm->start_stack -
  572. (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
  573. #endif
  574. p = (char __user *) current->mm->arg_start;
  575. for (loop = bprm->argc; loop > 0; loop--) {
  576. __put_user((elf_caddr_t) p, argv++);
  577. len = strnlen_user(p, MAX_ARG_STRLEN);
  578. if (!len || len > MAX_ARG_STRLEN)
  579. return -EINVAL;
  580. p += len;
  581. }
  582. __put_user(NULL, argv);
  583. current->mm->arg_end = (unsigned long) p;
  584. /* fill in the envv[] array */
  585. current->mm->env_start = (unsigned long) p;
  586. for (loop = bprm->envc; loop > 0; loop--) {
  587. __put_user((elf_caddr_t)(unsigned long) p, envp++);
  588. len = strnlen_user(p, MAX_ARG_STRLEN);
  589. if (!len || len > MAX_ARG_STRLEN)
  590. return -EINVAL;
  591. p += len;
  592. }
  593. __put_user(NULL, envp);
  594. current->mm->env_end = (unsigned long) p;
  595. mm->start_stack = (unsigned long) sp;
  596. return 0;
  597. }
  598. /*****************************************************************************/
  599. /*
  600. * load the appropriate binary image (executable or interpreter) into memory
  601. * - we assume no MMU is available
  602. * - if no other PIC bits are set in params->hdr->e_flags
  603. * - we assume that the LOADable segments in the binary are independently relocatable
  604. * - we assume R/O executable segments are shareable
  605. * - else
  606. * - we assume the loadable parts of the image to require fixed displacement
  607. * - the image is not shareable
  608. */
  609. static int elf_fdpic_map_file(struct elf_fdpic_params *params,
  610. struct file *file,
  611. struct mm_struct *mm,
  612. const char *what)
  613. {
  614. struct elf32_fdpic_loadmap *loadmap;
  615. #ifdef CONFIG_MMU
  616. struct elf32_fdpic_loadseg *mseg;
  617. #endif
  618. struct elf32_fdpic_loadseg *seg;
  619. struct elf32_phdr *phdr;
  620. unsigned long load_addr, stop;
  621. unsigned nloads, tmp;
  622. size_t size;
  623. int loop, ret;
  624. /* allocate a load map table */
  625. nloads = 0;
  626. for (loop = 0; loop < params->hdr.e_phnum; loop++)
  627. if (params->phdrs[loop].p_type == PT_LOAD)
  628. nloads++;
  629. if (nloads == 0)
  630. return -ELIBBAD;
  631. size = sizeof(*loadmap) + nloads * sizeof(*seg);
  632. loadmap = kzalloc(size, GFP_KERNEL);
  633. if (!loadmap)
  634. return -ENOMEM;
  635. params->loadmap = loadmap;
  636. loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
  637. loadmap->nsegs = nloads;
  638. load_addr = params->load_addr;
  639. seg = loadmap->segs;
  640. /* map the requested LOADs into the memory space */
  641. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  642. case ELF_FDPIC_FLAG_CONSTDISP:
  643. case ELF_FDPIC_FLAG_CONTIGUOUS:
  644. #ifndef CONFIG_MMU
  645. ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
  646. if (ret < 0)
  647. return ret;
  648. break;
  649. #endif
  650. default:
  651. ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
  652. if (ret < 0)
  653. return ret;
  654. break;
  655. }
  656. /* map the entry point */
  657. if (params->hdr.e_entry) {
  658. seg = loadmap->segs;
  659. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  660. if (params->hdr.e_entry >= seg->p_vaddr &&
  661. params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
  662. params->entry_addr =
  663. (params->hdr.e_entry - seg->p_vaddr) +
  664. seg->addr;
  665. break;
  666. }
  667. }
  668. }
  669. /* determine where the program header table has wound up if mapped */
  670. stop = params->hdr.e_phoff;
  671. stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
  672. phdr = params->phdrs;
  673. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  674. if (phdr->p_type != PT_LOAD)
  675. continue;
  676. if (phdr->p_offset > params->hdr.e_phoff ||
  677. phdr->p_offset + phdr->p_filesz < stop)
  678. continue;
  679. seg = loadmap->segs;
  680. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  681. if (phdr->p_vaddr >= seg->p_vaddr &&
  682. phdr->p_vaddr + phdr->p_filesz <=
  683. seg->p_vaddr + seg->p_memsz) {
  684. params->ph_addr =
  685. (phdr->p_vaddr - seg->p_vaddr) +
  686. seg->addr +
  687. params->hdr.e_phoff - phdr->p_offset;
  688. break;
  689. }
  690. }
  691. break;
  692. }
  693. /* determine where the dynamic section has wound up if there is one */
  694. phdr = params->phdrs;
  695. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  696. if (phdr->p_type != PT_DYNAMIC)
  697. continue;
  698. seg = loadmap->segs;
  699. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  700. if (phdr->p_vaddr >= seg->p_vaddr &&
  701. phdr->p_vaddr + phdr->p_memsz <=
  702. seg->p_vaddr + seg->p_memsz) {
  703. params->dynamic_addr =
  704. (phdr->p_vaddr - seg->p_vaddr) +
  705. seg->addr;
  706. /* check the dynamic section contains at least
  707. * one item, and that the last item is a NULL
  708. * entry */
  709. if (phdr->p_memsz == 0 ||
  710. phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
  711. goto dynamic_error;
  712. tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
  713. if (((Elf32_Dyn *)
  714. params->dynamic_addr)[tmp - 1].d_tag != 0)
  715. goto dynamic_error;
  716. break;
  717. }
  718. }
  719. break;
  720. }
  721. /* now elide adjacent segments in the load map on MMU linux
  722. * - on uClinux the holes between may actually be filled with system
  723. * stuff or stuff from other processes
  724. */
  725. #ifdef CONFIG_MMU
  726. nloads = loadmap->nsegs;
  727. mseg = loadmap->segs;
  728. seg = mseg + 1;
  729. for (loop = 1; loop < nloads; loop++) {
  730. /* see if we have a candidate for merging */
  731. if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
  732. load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
  733. if (load_addr == (seg->addr & PAGE_MASK)) {
  734. mseg->p_memsz +=
  735. load_addr -
  736. (mseg->addr + mseg->p_memsz);
  737. mseg->p_memsz += seg->addr & ~PAGE_MASK;
  738. mseg->p_memsz += seg->p_memsz;
  739. loadmap->nsegs--;
  740. continue;
  741. }
  742. }
  743. mseg++;
  744. if (mseg != seg)
  745. *mseg = *seg;
  746. }
  747. #endif
  748. kdebug("Mapped Object [%s]:", what);
  749. kdebug("- elfhdr : %lx", params->elfhdr_addr);
  750. kdebug("- entry : %lx", params->entry_addr);
  751. kdebug("- PHDR[] : %lx", params->ph_addr);
  752. kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
  753. seg = loadmap->segs;
  754. for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
  755. kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
  756. loop,
  757. seg->addr, seg->addr + seg->p_memsz - 1,
  758. seg->p_vaddr, seg->p_memsz);
  759. return 0;
  760. dynamic_error:
  761. printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
  762. what, file_inode(file)->i_ino);
  763. return -ELIBBAD;
  764. }
  765. /*****************************************************************************/
  766. /*
  767. * map a file with constant displacement under uClinux
  768. */
  769. #ifndef CONFIG_MMU
  770. static int elf_fdpic_map_file_constdisp_on_uclinux(
  771. struct elf_fdpic_params *params,
  772. struct file *file,
  773. struct mm_struct *mm)
  774. {
  775. struct elf32_fdpic_loadseg *seg;
  776. struct elf32_phdr *phdr;
  777. unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
  778. int loop, ret;
  779. load_addr = params->load_addr;
  780. seg = params->loadmap->segs;
  781. /* determine the bounds of the contiguous overall allocation we must
  782. * make */
  783. phdr = params->phdrs;
  784. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  785. if (params->phdrs[loop].p_type != PT_LOAD)
  786. continue;
  787. if (base > phdr->p_vaddr)
  788. base = phdr->p_vaddr;
  789. if (top < phdr->p_vaddr + phdr->p_memsz)
  790. top = phdr->p_vaddr + phdr->p_memsz;
  791. }
  792. /* allocate one big anon block for everything */
  793. mflags = MAP_PRIVATE;
  794. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  795. mflags |= MAP_EXECUTABLE;
  796. maddr = vm_mmap(NULL, load_addr, top - base,
  797. PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
  798. if (IS_ERR_VALUE(maddr))
  799. return (int) maddr;
  800. if (load_addr != 0)
  801. load_addr += PAGE_ALIGN(top - base);
  802. /* and then load the file segments into it */
  803. phdr = params->phdrs;
  804. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  805. if (params->phdrs[loop].p_type != PT_LOAD)
  806. continue;
  807. seg->addr = maddr + (phdr->p_vaddr - base);
  808. seg->p_vaddr = phdr->p_vaddr;
  809. seg->p_memsz = phdr->p_memsz;
  810. ret = read_code(file, seg->addr, phdr->p_offset,
  811. phdr->p_filesz);
  812. if (ret < 0)
  813. return ret;
  814. /* map the ELF header address if in this segment */
  815. if (phdr->p_offset == 0)
  816. params->elfhdr_addr = seg->addr;
  817. /* clear any space allocated but not loaded */
  818. if (phdr->p_filesz < phdr->p_memsz) {
  819. if (clear_user((void *) (seg->addr + phdr->p_filesz),
  820. phdr->p_memsz - phdr->p_filesz))
  821. return -EFAULT;
  822. }
  823. if (mm) {
  824. if (phdr->p_flags & PF_X) {
  825. if (!mm->start_code) {
  826. mm->start_code = seg->addr;
  827. mm->end_code = seg->addr +
  828. phdr->p_memsz;
  829. }
  830. } else if (!mm->start_data) {
  831. mm->start_data = seg->addr;
  832. mm->end_data = seg->addr + phdr->p_memsz;
  833. }
  834. }
  835. seg++;
  836. }
  837. return 0;
  838. }
  839. #endif
  840. /*****************************************************************************/
  841. /*
  842. * map a binary by direct mmap() of the individual PT_LOAD segments
  843. */
  844. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
  845. struct file *file,
  846. struct mm_struct *mm)
  847. {
  848. struct elf32_fdpic_loadseg *seg;
  849. struct elf32_phdr *phdr;
  850. unsigned long load_addr, delta_vaddr;
  851. int loop, dvset;
  852. load_addr = params->load_addr;
  853. delta_vaddr = 0;
  854. dvset = 0;
  855. seg = params->loadmap->segs;
  856. /* deal with each load segment separately */
  857. phdr = params->phdrs;
  858. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  859. unsigned long maddr, disp, excess, excess1;
  860. int prot = 0, flags;
  861. if (phdr->p_type != PT_LOAD)
  862. continue;
  863. kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
  864. (unsigned long) phdr->p_vaddr,
  865. (unsigned long) phdr->p_offset,
  866. (unsigned long) phdr->p_filesz,
  867. (unsigned long) phdr->p_memsz);
  868. /* determine the mapping parameters */
  869. if (phdr->p_flags & PF_R) prot |= PROT_READ;
  870. if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
  871. if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
  872. flags = MAP_PRIVATE | MAP_DENYWRITE;
  873. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  874. flags |= MAP_EXECUTABLE;
  875. maddr = 0;
  876. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  877. case ELF_FDPIC_FLAG_INDEPENDENT:
  878. /* PT_LOADs are independently locatable */
  879. break;
  880. case ELF_FDPIC_FLAG_HONOURVADDR:
  881. /* the specified virtual address must be honoured */
  882. maddr = phdr->p_vaddr;
  883. flags |= MAP_FIXED;
  884. break;
  885. case ELF_FDPIC_FLAG_CONSTDISP:
  886. /* constant displacement
  887. * - can be mapped anywhere, but must be mapped as a
  888. * unit
  889. */
  890. if (!dvset) {
  891. maddr = load_addr;
  892. delta_vaddr = phdr->p_vaddr;
  893. dvset = 1;
  894. } else {
  895. maddr = load_addr + phdr->p_vaddr - delta_vaddr;
  896. flags |= MAP_FIXED;
  897. }
  898. break;
  899. case ELF_FDPIC_FLAG_CONTIGUOUS:
  900. /* contiguity handled later */
  901. break;
  902. default:
  903. BUG();
  904. }
  905. maddr &= PAGE_MASK;
  906. /* create the mapping */
  907. disp = phdr->p_vaddr & ~PAGE_MASK;
  908. maddr = vm_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
  909. phdr->p_offset - disp);
  910. kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
  911. loop, phdr->p_memsz + disp, prot, flags,
  912. phdr->p_offset - disp, maddr);
  913. if (IS_ERR_VALUE(maddr))
  914. return (int) maddr;
  915. if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
  916. ELF_FDPIC_FLAG_CONTIGUOUS)
  917. load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
  918. seg->addr = maddr + disp;
  919. seg->p_vaddr = phdr->p_vaddr;
  920. seg->p_memsz = phdr->p_memsz;
  921. /* map the ELF header address if in this segment */
  922. if (phdr->p_offset == 0)
  923. params->elfhdr_addr = seg->addr;
  924. /* clear the bit between beginning of mapping and beginning of
  925. * PT_LOAD */
  926. if (prot & PROT_WRITE && disp > 0) {
  927. kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
  928. if (clear_user((void __user *) maddr, disp))
  929. return -EFAULT;
  930. maddr += disp;
  931. }
  932. /* clear any space allocated but not loaded
  933. * - on uClinux we can just clear the lot
  934. * - on MMU linux we'll get a SIGBUS beyond the last page
  935. * extant in the file
  936. */
  937. excess = phdr->p_memsz - phdr->p_filesz;
  938. excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
  939. #ifdef CONFIG_MMU
  940. if (excess > excess1) {
  941. unsigned long xaddr = maddr + phdr->p_filesz + excess1;
  942. unsigned long xmaddr;
  943. flags |= MAP_FIXED | MAP_ANONYMOUS;
  944. xmaddr = vm_mmap(NULL, xaddr, excess - excess1,
  945. prot, flags, 0);
  946. kdebug("mmap[%d] <anon>"
  947. " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
  948. loop, xaddr, excess - excess1, prot, flags,
  949. xmaddr);
  950. if (xmaddr != xaddr)
  951. return -ENOMEM;
  952. }
  953. if (prot & PROT_WRITE && excess1 > 0) {
  954. kdebug("clear[%d] ad=%lx sz=%lx",
  955. loop, maddr + phdr->p_filesz, excess1);
  956. if (clear_user((void __user *) maddr + phdr->p_filesz,
  957. excess1))
  958. return -EFAULT;
  959. }
  960. #else
  961. if (excess > 0) {
  962. kdebug("clear[%d] ad=%lx sz=%lx",
  963. loop, maddr + phdr->p_filesz, excess);
  964. if (clear_user((void *) maddr + phdr->p_filesz, excess))
  965. return -EFAULT;
  966. }
  967. #endif
  968. if (mm) {
  969. if (phdr->p_flags & PF_X) {
  970. if (!mm->start_code) {
  971. mm->start_code = maddr;
  972. mm->end_code = maddr + phdr->p_memsz;
  973. }
  974. } else if (!mm->start_data) {
  975. mm->start_data = maddr;
  976. mm->end_data = maddr + phdr->p_memsz;
  977. }
  978. }
  979. seg++;
  980. }
  981. return 0;
  982. }
  983. /*****************************************************************************/
  984. /*
  985. * ELF-FDPIC core dumper
  986. *
  987. * Modelled on fs/exec.c:aout_core_dump()
  988. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  989. *
  990. * Modelled on fs/binfmt_elf.c core dumper
  991. */
  992. #ifdef CONFIG_ELF_CORE
  993. /*
  994. * Decide whether a segment is worth dumping; default is yes to be
  995. * sure (missing info is worse than too much; etc).
  996. * Personally I'd include everything, and use the coredump limit...
  997. *
  998. * I think we should skip something. But I am not sure how. H.J.
  999. */
  1000. static int maydump(struct vm_area_struct *vma, unsigned long mm_flags)
  1001. {
  1002. int dump_ok;
  1003. /* Do not dump I/O mapped devices or special mappings */
  1004. if (vma->vm_flags & VM_IO) {
  1005. kdcore("%08lx: %08lx: no (IO)", vma->vm_start, vma->vm_flags);
  1006. return 0;
  1007. }
  1008. /* If we may not read the contents, don't allow us to dump
  1009. * them either. "dump_write()" can't handle it anyway.
  1010. */
  1011. if (!(vma->vm_flags & VM_READ)) {
  1012. kdcore("%08lx: %08lx: no (!read)", vma->vm_start, vma->vm_flags);
  1013. return 0;
  1014. }
  1015. /* support for DAX */
  1016. if (vma_is_dax(vma)) {
  1017. if (vma->vm_flags & VM_SHARED) {
  1018. dump_ok = test_bit(MMF_DUMP_DAX_SHARED, &mm_flags);
  1019. kdcore("%08lx: %08lx: %s (DAX shared)", vma->vm_start,
  1020. vma->vm_flags, dump_ok ? "yes" : "no");
  1021. } else {
  1022. dump_ok = test_bit(MMF_DUMP_DAX_PRIVATE, &mm_flags);
  1023. kdcore("%08lx: %08lx: %s (DAX private)", vma->vm_start,
  1024. vma->vm_flags, dump_ok ? "yes" : "no");
  1025. }
  1026. return dump_ok;
  1027. }
  1028. /* By default, dump shared memory if mapped from an anonymous file. */
  1029. if (vma->vm_flags & VM_SHARED) {
  1030. if (file_inode(vma->vm_file)->i_nlink == 0) {
  1031. dump_ok = test_bit(MMF_DUMP_ANON_SHARED, &mm_flags);
  1032. kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
  1033. vma->vm_flags, dump_ok ? "yes" : "no");
  1034. return dump_ok;
  1035. }
  1036. dump_ok = test_bit(MMF_DUMP_MAPPED_SHARED, &mm_flags);
  1037. kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
  1038. vma->vm_flags, dump_ok ? "yes" : "no");
  1039. return dump_ok;
  1040. }
  1041. #ifdef CONFIG_MMU
  1042. /* By default, if it hasn't been written to, don't write it out */
  1043. if (!vma->anon_vma) {
  1044. dump_ok = test_bit(MMF_DUMP_MAPPED_PRIVATE, &mm_flags);
  1045. kdcore("%08lx: %08lx: %s (!anon)", vma->vm_start,
  1046. vma->vm_flags, dump_ok ? "yes" : "no");
  1047. return dump_ok;
  1048. }
  1049. #endif
  1050. dump_ok = test_bit(MMF_DUMP_ANON_PRIVATE, &mm_flags);
  1051. kdcore("%08lx: %08lx: %s", vma->vm_start, vma->vm_flags,
  1052. dump_ok ? "yes" : "no");
  1053. return dump_ok;
  1054. }
  1055. /* An ELF note in memory */
  1056. struct memelfnote
  1057. {
  1058. const char *name;
  1059. int type;
  1060. unsigned int datasz;
  1061. void *data;
  1062. };
  1063. static int notesize(struct memelfnote *en)
  1064. {
  1065. int sz;
  1066. sz = sizeof(struct elf_note);
  1067. sz += roundup(strlen(en->name) + 1, 4);
  1068. sz += roundup(en->datasz, 4);
  1069. return sz;
  1070. }
  1071. /* #define DEBUG */
  1072. static int writenote(struct memelfnote *men, struct coredump_params *cprm)
  1073. {
  1074. struct elf_note en;
  1075. en.n_namesz = strlen(men->name) + 1;
  1076. en.n_descsz = men->datasz;
  1077. en.n_type = men->type;
  1078. return dump_emit(cprm, &en, sizeof(en)) &&
  1079. dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
  1080. dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
  1081. }
  1082. static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
  1083. {
  1084. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1085. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1086. elf->e_ident[EI_DATA] = ELF_DATA;
  1087. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1088. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1089. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1090. elf->e_type = ET_CORE;
  1091. elf->e_machine = ELF_ARCH;
  1092. elf->e_version = EV_CURRENT;
  1093. elf->e_entry = 0;
  1094. elf->e_phoff = sizeof(struct elfhdr);
  1095. elf->e_shoff = 0;
  1096. elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
  1097. elf->e_ehsize = sizeof(struct elfhdr);
  1098. elf->e_phentsize = sizeof(struct elf_phdr);
  1099. elf->e_phnum = segs;
  1100. elf->e_shentsize = 0;
  1101. elf->e_shnum = 0;
  1102. elf->e_shstrndx = 0;
  1103. return;
  1104. }
  1105. static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1106. {
  1107. phdr->p_type = PT_NOTE;
  1108. phdr->p_offset = offset;
  1109. phdr->p_vaddr = 0;
  1110. phdr->p_paddr = 0;
  1111. phdr->p_filesz = sz;
  1112. phdr->p_memsz = 0;
  1113. phdr->p_flags = 0;
  1114. phdr->p_align = 0;
  1115. return;
  1116. }
  1117. static inline void fill_note(struct memelfnote *note, const char *name, int type,
  1118. unsigned int sz, void *data)
  1119. {
  1120. note->name = name;
  1121. note->type = type;
  1122. note->datasz = sz;
  1123. note->data = data;
  1124. return;
  1125. }
  1126. /*
  1127. * fill up all the fields in prstatus from the given task struct, except
  1128. * registers which need to be filled up separately.
  1129. */
  1130. static void fill_prstatus(struct elf_prstatus *prstatus,
  1131. struct task_struct *p, long signr)
  1132. {
  1133. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1134. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1135. prstatus->pr_sighold = p->blocked.sig[0];
  1136. rcu_read_lock();
  1137. prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
  1138. rcu_read_unlock();
  1139. prstatus->pr_pid = task_pid_vnr(p);
  1140. prstatus->pr_pgrp = task_pgrp_vnr(p);
  1141. prstatus->pr_sid = task_session_vnr(p);
  1142. if (thread_group_leader(p)) {
  1143. struct task_cputime cputime;
  1144. /*
  1145. * This is the record for the group leader. It shows the
  1146. * group-wide total, not its individual thread total.
  1147. */
  1148. thread_group_cputime(p, &cputime);
  1149. cputime_to_timeval(cputime.utime, &prstatus->pr_utime);
  1150. cputime_to_timeval(cputime.stime, &prstatus->pr_stime);
  1151. } else {
  1152. cputime_t utime, stime;
  1153. task_cputime(p, &utime, &stime);
  1154. cputime_to_timeval(utime, &prstatus->pr_utime);
  1155. cputime_to_timeval(stime, &prstatus->pr_stime);
  1156. }
  1157. cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
  1158. cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
  1159. prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
  1160. prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
  1161. }
  1162. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1163. struct mm_struct *mm)
  1164. {
  1165. const struct cred *cred;
  1166. unsigned int i, len;
  1167. /* first copy the parameters from user space */
  1168. memset(psinfo, 0, sizeof(struct elf_prpsinfo));
  1169. len = mm->arg_end - mm->arg_start;
  1170. if (len >= ELF_PRARGSZ)
  1171. len = ELF_PRARGSZ - 1;
  1172. if (copy_from_user(&psinfo->pr_psargs,
  1173. (const char __user *) mm->arg_start, len))
  1174. return -EFAULT;
  1175. for (i = 0; i < len; i++)
  1176. if (psinfo->pr_psargs[i] == 0)
  1177. psinfo->pr_psargs[i] = ' ';
  1178. psinfo->pr_psargs[len] = 0;
  1179. rcu_read_lock();
  1180. psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
  1181. rcu_read_unlock();
  1182. psinfo->pr_pid = task_pid_vnr(p);
  1183. psinfo->pr_pgrp = task_pgrp_vnr(p);
  1184. psinfo->pr_sid = task_session_vnr(p);
  1185. i = p->state ? ffz(~p->state) + 1 : 0;
  1186. psinfo->pr_state = i;
  1187. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1188. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1189. psinfo->pr_nice = task_nice(p);
  1190. psinfo->pr_flag = p->flags;
  1191. rcu_read_lock();
  1192. cred = __task_cred(p);
  1193. SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
  1194. SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
  1195. rcu_read_unlock();
  1196. strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
  1197. return 0;
  1198. }
  1199. /* Here is the structure in which status of each thread is captured. */
  1200. struct elf_thread_status
  1201. {
  1202. struct list_head list;
  1203. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  1204. elf_fpregset_t fpu; /* NT_PRFPREG */
  1205. struct task_struct *thread;
  1206. #ifdef ELF_CORE_COPY_XFPREGS
  1207. elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
  1208. #endif
  1209. struct memelfnote notes[3];
  1210. int num_notes;
  1211. };
  1212. /*
  1213. * In order to add the specific thread information for the elf file format,
  1214. * we need to keep a linked list of every thread's pr_status and then create
  1215. * a single section for them in the final core file.
  1216. */
  1217. static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
  1218. {
  1219. struct task_struct *p = t->thread;
  1220. int sz = 0;
  1221. t->num_notes = 0;
  1222. fill_prstatus(&t->prstatus, p, signr);
  1223. elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
  1224. fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
  1225. &t->prstatus);
  1226. t->num_notes++;
  1227. sz += notesize(&t->notes[0]);
  1228. t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
  1229. if (t->prstatus.pr_fpvalid) {
  1230. fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
  1231. &t->fpu);
  1232. t->num_notes++;
  1233. sz += notesize(&t->notes[1]);
  1234. }
  1235. #ifdef ELF_CORE_COPY_XFPREGS
  1236. if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
  1237. fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
  1238. sizeof(t->xfpu), &t->xfpu);
  1239. t->num_notes++;
  1240. sz += notesize(&t->notes[2]);
  1241. }
  1242. #endif
  1243. return sz;
  1244. }
  1245. static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
  1246. elf_addr_t e_shoff, int segs)
  1247. {
  1248. elf->e_shoff = e_shoff;
  1249. elf->e_shentsize = sizeof(*shdr4extnum);
  1250. elf->e_shnum = 1;
  1251. elf->e_shstrndx = SHN_UNDEF;
  1252. memset(shdr4extnum, 0, sizeof(*shdr4extnum));
  1253. shdr4extnum->sh_type = SHT_NULL;
  1254. shdr4extnum->sh_size = elf->e_shnum;
  1255. shdr4extnum->sh_link = elf->e_shstrndx;
  1256. shdr4extnum->sh_info = segs;
  1257. }
  1258. /*
  1259. * dump the segments for an MMU process
  1260. */
  1261. static bool elf_fdpic_dump_segments(struct coredump_params *cprm)
  1262. {
  1263. struct vm_area_struct *vma;
  1264. for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
  1265. unsigned long addr;
  1266. if (!maydump(vma, cprm->mm_flags))
  1267. continue;
  1268. #ifdef CONFIG_MMU
  1269. for (addr = vma->vm_start; addr < vma->vm_end;
  1270. addr += PAGE_SIZE) {
  1271. bool res;
  1272. struct page *page = get_dump_page(addr);
  1273. if (page) {
  1274. void *kaddr = kmap(page);
  1275. res = dump_emit(cprm, kaddr, PAGE_SIZE);
  1276. kunmap(page);
  1277. put_page(page);
  1278. } else {
  1279. res = dump_skip(cprm, PAGE_SIZE);
  1280. }
  1281. if (!res)
  1282. return false;
  1283. }
  1284. #else
  1285. if (!dump_emit(cprm, (void *) vma->vm_start,
  1286. vma->vm_end - vma->vm_start))
  1287. return false;
  1288. #endif
  1289. }
  1290. return true;
  1291. }
  1292. static size_t elf_core_vma_data_size(unsigned long mm_flags)
  1293. {
  1294. struct vm_area_struct *vma;
  1295. size_t size = 0;
  1296. for (vma = current->mm->mmap; vma; vma = vma->vm_next)
  1297. if (maydump(vma, mm_flags))
  1298. size += vma->vm_end - vma->vm_start;
  1299. return size;
  1300. }
  1301. /*
  1302. * Actual dumper
  1303. *
  1304. * This is a two-pass process; first we find the offsets of the bits,
  1305. * and then they are actually written out. If we run out of core limit
  1306. * we just truncate.
  1307. */
  1308. static int elf_fdpic_core_dump(struct coredump_params *cprm)
  1309. {
  1310. #define NUM_NOTES 6
  1311. int has_dumped = 0;
  1312. mm_segment_t fs;
  1313. int segs;
  1314. int i;
  1315. struct vm_area_struct *vma;
  1316. struct elfhdr *elf = NULL;
  1317. loff_t offset = 0, dataoff;
  1318. int numnote;
  1319. struct memelfnote *notes = NULL;
  1320. struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
  1321. struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
  1322. LIST_HEAD(thread_list);
  1323. struct list_head *t;
  1324. elf_fpregset_t *fpu = NULL;
  1325. #ifdef ELF_CORE_COPY_XFPREGS
  1326. elf_fpxregset_t *xfpu = NULL;
  1327. #endif
  1328. int thread_status_size = 0;
  1329. elf_addr_t *auxv;
  1330. struct elf_phdr *phdr4note = NULL;
  1331. struct elf_shdr *shdr4extnum = NULL;
  1332. Elf_Half e_phnum;
  1333. elf_addr_t e_shoff;
  1334. struct core_thread *ct;
  1335. struct elf_thread_status *tmp;
  1336. /*
  1337. * We no longer stop all VM operations.
  1338. *
  1339. * This is because those proceses that could possibly change map_count
  1340. * or the mmap / vma pages are now blocked in do_exit on current
  1341. * finishing this core dump.
  1342. *
  1343. * Only ptrace can touch these memory addresses, but it doesn't change
  1344. * the map_count or the pages allocated. So no possibility of crashing
  1345. * exists while dumping the mm->vm_next areas to the core file.
  1346. */
  1347. /* alloc memory for large data structures: too large to be on stack */
  1348. elf = kmalloc(sizeof(*elf), GFP_KERNEL);
  1349. if (!elf)
  1350. goto cleanup;
  1351. prstatus = kzalloc(sizeof(*prstatus), GFP_KERNEL);
  1352. if (!prstatus)
  1353. goto cleanup;
  1354. psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
  1355. if (!psinfo)
  1356. goto cleanup;
  1357. notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
  1358. if (!notes)
  1359. goto cleanup;
  1360. fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
  1361. if (!fpu)
  1362. goto cleanup;
  1363. #ifdef ELF_CORE_COPY_XFPREGS
  1364. xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
  1365. if (!xfpu)
  1366. goto cleanup;
  1367. #endif
  1368. for (ct = current->mm->core_state->dumper.next;
  1369. ct; ct = ct->next) {
  1370. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  1371. if (!tmp)
  1372. goto cleanup;
  1373. tmp->thread = ct->task;
  1374. list_add(&tmp->list, &thread_list);
  1375. }
  1376. list_for_each(t, &thread_list) {
  1377. struct elf_thread_status *tmp;
  1378. int sz;
  1379. tmp = list_entry(t, struct elf_thread_status, list);
  1380. sz = elf_dump_thread_status(cprm->siginfo->si_signo, tmp);
  1381. thread_status_size += sz;
  1382. }
  1383. /* now collect the dump for the current */
  1384. fill_prstatus(prstatus, current, cprm->siginfo->si_signo);
  1385. elf_core_copy_regs(&prstatus->pr_reg, cprm->regs);
  1386. segs = current->mm->map_count;
  1387. segs += elf_core_extra_phdrs();
  1388. /* for notes section */
  1389. segs++;
  1390. /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
  1391. * this, kernel supports extended numbering. Have a look at
  1392. * include/linux/elf.h for further information. */
  1393. e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
  1394. /* Set up header */
  1395. fill_elf_fdpic_header(elf, e_phnum);
  1396. has_dumped = 1;
  1397. /*
  1398. * Set up the notes in similar form to SVR4 core dumps made
  1399. * with info from their /proc.
  1400. */
  1401. fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
  1402. fill_psinfo(psinfo, current->group_leader, current->mm);
  1403. fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
  1404. numnote = 2;
  1405. auxv = (elf_addr_t *) current->mm->saved_auxv;
  1406. i = 0;
  1407. do
  1408. i += 2;
  1409. while (auxv[i - 2] != AT_NULL);
  1410. fill_note(&notes[numnote++], "CORE", NT_AUXV,
  1411. i * sizeof(elf_addr_t), auxv);
  1412. /* Try to dump the FPU. */
  1413. if ((prstatus->pr_fpvalid =
  1414. elf_core_copy_task_fpregs(current, cprm->regs, fpu)))
  1415. fill_note(notes + numnote++,
  1416. "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
  1417. #ifdef ELF_CORE_COPY_XFPREGS
  1418. if (elf_core_copy_task_xfpregs(current, xfpu))
  1419. fill_note(notes + numnote++,
  1420. "LINUX", ELF_CORE_XFPREG_TYPE, sizeof(*xfpu), xfpu);
  1421. #endif
  1422. fs = get_fs();
  1423. set_fs(KERNEL_DS);
  1424. offset += sizeof(*elf); /* Elf header */
  1425. offset += segs * sizeof(struct elf_phdr); /* Program headers */
  1426. /* Write notes phdr entry */
  1427. {
  1428. int sz = 0;
  1429. for (i = 0; i < numnote; i++)
  1430. sz += notesize(notes + i);
  1431. sz += thread_status_size;
  1432. phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
  1433. if (!phdr4note)
  1434. goto end_coredump;
  1435. fill_elf_note_phdr(phdr4note, sz, offset);
  1436. offset += sz;
  1437. }
  1438. /* Page-align dumped data */
  1439. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1440. offset += elf_core_vma_data_size(cprm->mm_flags);
  1441. offset += elf_core_extra_data_size();
  1442. e_shoff = offset;
  1443. if (e_phnum == PN_XNUM) {
  1444. shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
  1445. if (!shdr4extnum)
  1446. goto end_coredump;
  1447. fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
  1448. }
  1449. offset = dataoff;
  1450. if (!dump_emit(cprm, elf, sizeof(*elf)))
  1451. goto end_coredump;
  1452. if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
  1453. goto end_coredump;
  1454. /* write program headers for segments dump */
  1455. for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
  1456. struct elf_phdr phdr;
  1457. size_t sz;
  1458. sz = vma->vm_end - vma->vm_start;
  1459. phdr.p_type = PT_LOAD;
  1460. phdr.p_offset = offset;
  1461. phdr.p_vaddr = vma->vm_start;
  1462. phdr.p_paddr = 0;
  1463. phdr.p_filesz = maydump(vma, cprm->mm_flags) ? sz : 0;
  1464. phdr.p_memsz = sz;
  1465. offset += phdr.p_filesz;
  1466. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1467. if (vma->vm_flags & VM_WRITE)
  1468. phdr.p_flags |= PF_W;
  1469. if (vma->vm_flags & VM_EXEC)
  1470. phdr.p_flags |= PF_X;
  1471. phdr.p_align = ELF_EXEC_PAGESIZE;
  1472. if (!dump_emit(cprm, &phdr, sizeof(phdr)))
  1473. goto end_coredump;
  1474. }
  1475. if (!elf_core_write_extra_phdrs(cprm, offset))
  1476. goto end_coredump;
  1477. /* write out the notes section */
  1478. for (i = 0; i < numnote; i++)
  1479. if (!writenote(notes + i, cprm))
  1480. goto end_coredump;
  1481. /* write out the thread status notes section */
  1482. list_for_each(t, &thread_list) {
  1483. struct elf_thread_status *tmp =
  1484. list_entry(t, struct elf_thread_status, list);
  1485. for (i = 0; i < tmp->num_notes; i++)
  1486. if (!writenote(&tmp->notes[i], cprm))
  1487. goto end_coredump;
  1488. }
  1489. if (!dump_skip(cprm, dataoff - cprm->pos))
  1490. goto end_coredump;
  1491. if (!elf_fdpic_dump_segments(cprm))
  1492. goto end_coredump;
  1493. if (!elf_core_write_extra_data(cprm))
  1494. goto end_coredump;
  1495. if (e_phnum == PN_XNUM) {
  1496. if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
  1497. goto end_coredump;
  1498. }
  1499. if (cprm->file->f_pos != offset) {
  1500. /* Sanity check */
  1501. printk(KERN_WARNING
  1502. "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
  1503. cprm->file->f_pos, offset);
  1504. }
  1505. end_coredump:
  1506. set_fs(fs);
  1507. cleanup:
  1508. while (!list_empty(&thread_list)) {
  1509. struct list_head *tmp = thread_list.next;
  1510. list_del(tmp);
  1511. kfree(list_entry(tmp, struct elf_thread_status, list));
  1512. }
  1513. kfree(phdr4note);
  1514. kfree(elf);
  1515. kfree(prstatus);
  1516. kfree(psinfo);
  1517. kfree(notes);
  1518. kfree(fpu);
  1519. kfree(shdr4extnum);
  1520. #ifdef ELF_CORE_COPY_XFPREGS
  1521. kfree(xfpu);
  1522. #endif
  1523. return has_dumped;
  1524. #undef NUM_NOTES
  1525. }
  1526. #endif /* CONFIG_ELF_CORE */