binfmt_elf_fdpic.c 49 KB

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