binfmt_elf_fdpic.c 48 KB

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