binfmt_elf_fdpic.c 47 KB

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