binfmt_elf_fdpic.c 49 KB

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