binfmt_elf_fdpic.c 49 KB

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