binfmt_elf_fdpic.c 48 KB

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