binfmt_elf_fdpic.c 44 KB

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