binfmt_elf_fdpic.c 49 KB

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