binfmt_elf_fdpic.c 47 KB

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