binfmt_elf_fdpic.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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. .min_coredump = ELF_EXEC_PAGESIZE,
  74. #endif
  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 (!can_mmap_file(file))
  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 elf_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 && interp_params.flags & ELF_FDPIC_FLAG_PRESENT) {
  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. SET_PERSONALITY(exec_params.hdr);
  293. if (elf_check_fdpic(&exec_params.hdr))
  294. current->personality |= PER_LINUX_FDPIC;
  295. if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
  296. current->personality |= READ_IMPLIES_EXEC;
  297. setup_new_exec(bprm);
  298. set_binfmt(&elf_fdpic_format);
  299. current->mm->start_code = 0;
  300. current->mm->end_code = 0;
  301. current->mm->start_stack = 0;
  302. current->mm->start_data = 0;
  303. current->mm->end_data = 0;
  304. current->mm->context.exec_fdpic_loadmap = 0;
  305. current->mm->context.interp_fdpic_loadmap = 0;
  306. #ifdef CONFIG_MMU
  307. elf_fdpic_arch_lay_out_mm(&exec_params,
  308. &interp_params,
  309. &current->mm->start_stack,
  310. &current->mm->start_brk);
  311. retval = setup_arg_pages(bprm, current->mm->start_stack,
  312. executable_stack);
  313. if (retval < 0)
  314. goto error;
  315. #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
  316. retval = arch_setup_additional_pages(bprm, !!interpreter_name);
  317. if (retval < 0)
  318. goto error;
  319. #endif
  320. #endif
  321. /* load the executable and interpreter into memory */
  322. retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
  323. "executable");
  324. if (retval < 0)
  325. goto error;
  326. if (interpreter_name) {
  327. retval = elf_fdpic_map_file(&interp_params, interpreter,
  328. current->mm, "interpreter");
  329. if (retval < 0) {
  330. printk(KERN_ERR "Unable to load interpreter\n");
  331. goto error;
  332. }
  333. exe_file_allow_write_access(interpreter);
  334. fput(interpreter);
  335. interpreter = NULL;
  336. }
  337. #ifdef CONFIG_MMU
  338. if (!current->mm->start_brk)
  339. current->mm->start_brk = current->mm->end_data;
  340. current->mm->brk = current->mm->start_brk =
  341. PAGE_ALIGN(current->mm->start_brk);
  342. #else
  343. /* create a stack area and zero-size brk area */
  344. stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
  345. if (stack_size < PAGE_SIZE * 2)
  346. stack_size = PAGE_SIZE * 2;
  347. stack_prot = PROT_READ | PROT_WRITE;
  348. if (executable_stack == EXSTACK_ENABLE_X ||
  349. (executable_stack == EXSTACK_DEFAULT && VM_STACK_FLAGS & VM_EXEC))
  350. stack_prot |= PROT_EXEC;
  351. current->mm->start_brk = vm_mmap(NULL, 0, stack_size, stack_prot,
  352. MAP_PRIVATE | MAP_ANONYMOUS |
  353. MAP_UNINITIALIZED | MAP_GROWSDOWN,
  354. 0);
  355. if (IS_ERR_VALUE(current->mm->start_brk)) {
  356. retval = current->mm->start_brk;
  357. current->mm->start_brk = 0;
  358. goto error;
  359. }
  360. current->mm->brk = current->mm->start_brk;
  361. current->mm->context.end_brk = current->mm->start_brk;
  362. current->mm->start_stack = current->mm->start_brk + stack_size;
  363. #endif
  364. retval = create_elf_fdpic_tables(bprm, current->mm, &exec_params,
  365. &interp_params);
  366. if (retval < 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. exe_file_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. unsigned long flags = 0;
  429. int ei_index;
  430. elf_addr_t *elf_info;
  431. #ifdef CONFIG_MMU
  432. /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  433. * by the processes running on the same package. One thing we can do is
  434. * to shuffle the initial stack for them, so we give the architecture
  435. * an opportunity to do so here.
  436. */
  437. sp = arch_align_stack(bprm->p);
  438. #else
  439. sp = mm->start_stack;
  440. /* stack the program arguments and environment */
  441. if (transfer_args_to_stack(bprm, &sp) < 0)
  442. return -EFAULT;
  443. sp &= ~15;
  444. #endif
  445. /*
  446. * If this architecture has a platform capability string, copy it
  447. * to userspace. In some cases (Sparc), this info is impossible
  448. * for userspace to get any other way, in others (i386) it is
  449. * merely difficult.
  450. */
  451. k_platform = ELF_PLATFORM;
  452. u_platform = NULL;
  453. if (k_platform) {
  454. platform_len = strlen(k_platform) + 1;
  455. sp -= platform_len;
  456. u_platform = (char __user *) sp;
  457. if (copy_to_user(u_platform, k_platform, platform_len) != 0)
  458. return -EFAULT;
  459. }
  460. /*
  461. * If this architecture has a "base" platform capability
  462. * string, copy it to userspace.
  463. */
  464. k_base_platform = ELF_BASE_PLATFORM;
  465. u_base_platform = NULL;
  466. if (k_base_platform) {
  467. platform_len = strlen(k_base_platform) + 1;
  468. sp -= platform_len;
  469. u_base_platform = (char __user *) sp;
  470. if (copy_to_user(u_base_platform, k_base_platform, platform_len) != 0)
  471. return -EFAULT;
  472. }
  473. sp &= ~7UL;
  474. /* stack the load map(s) */
  475. len = sizeof(struct elf_fdpic_loadmap);
  476. len += sizeof(struct elf_fdpic_loadseg) * exec_params->loadmap->nsegs;
  477. sp = (sp - len) & ~7UL;
  478. exec_params->map_addr = sp;
  479. if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
  480. return -EFAULT;
  481. current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
  482. if (interp_params->loadmap) {
  483. len = sizeof(struct elf_fdpic_loadmap);
  484. len += sizeof(struct elf_fdpic_loadseg) *
  485. interp_params->loadmap->nsegs;
  486. sp = (sp - len) & ~7UL;
  487. interp_params->map_addr = sp;
  488. if (copy_to_user((void __user *) sp, interp_params->loadmap,
  489. len) != 0)
  490. return -EFAULT;
  491. current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
  492. }
  493. /* force 16 byte _final_ alignment here for generality */
  494. #define DLINFO_ITEMS 15
  495. nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0) +
  496. (k_base_platform ? 1 : 0) + AT_VECTOR_SIZE_ARCH;
  497. if (bprm->have_execfd)
  498. nitems++;
  499. #ifdef ELF_HWCAP2
  500. nitems++;
  501. #endif
  502. #ifdef ELF_HWCAP3
  503. nitems++;
  504. #endif
  505. #ifdef ELF_HWCAP4
  506. nitems++;
  507. #endif
  508. csp = sp;
  509. sp -= nitems * 2 * sizeof(unsigned long);
  510. sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
  511. sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
  512. sp -= 1 * sizeof(unsigned long); /* argc */
  513. csp -= sp & 15UL;
  514. sp -= sp & 15UL;
  515. /* Create the ELF interpreter info */
  516. elf_info = (elf_addr_t *)mm->saved_auxv;
  517. /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
  518. #define NEW_AUX_ENT(id, val) \
  519. do { \
  520. *elf_info++ = id; \
  521. *elf_info++ = val; \
  522. } while (0)
  523. #ifdef ARCH_DLINFO
  524. /*
  525. * ARCH_DLINFO must come first so PPC can do its special alignment of
  526. * AUXV.
  527. * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
  528. * ARCH_DLINFO changes
  529. */
  530. ARCH_DLINFO;
  531. #endif
  532. NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
  533. #ifdef ELF_HWCAP2
  534. NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
  535. #endif
  536. #ifdef ELF_HWCAP3
  537. NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3);
  538. #endif
  539. #ifdef ELF_HWCAP4
  540. NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4);
  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. if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0)
  549. flags |= AT_FLAGS_PRESERVE_ARGV0;
  550. NEW_AUX_ENT(AT_FLAGS, flags);
  551. NEW_AUX_ENT(AT_ENTRY, exec_params->entry_addr);
  552. NEW_AUX_ENT(AT_UID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
  553. NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
  554. NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
  555. NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
  556. NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
  557. NEW_AUX_ENT(AT_EXECFN, bprm->exec);
  558. if (k_platform)
  559. NEW_AUX_ENT(AT_PLATFORM,
  560. (elf_addr_t)(unsigned long)u_platform);
  561. if (k_base_platform)
  562. NEW_AUX_ENT(AT_BASE_PLATFORM,
  563. (elf_addr_t)(unsigned long)u_base_platform);
  564. if (bprm->have_execfd)
  565. NEW_AUX_ENT(AT_EXECFD, bprm->execfd);
  566. #undef NEW_AUX_ENT
  567. /* AT_NULL is zero; clear the rest too */
  568. memset(elf_info, 0, (char *)mm->saved_auxv +
  569. sizeof(mm->saved_auxv) - (char *)elf_info);
  570. /* And advance past the AT_NULL entry. */
  571. elf_info += 2;
  572. ei_index = elf_info - (elf_addr_t *)mm->saved_auxv;
  573. csp -= ei_index * sizeof(elf_addr_t);
  574. /* Put the elf_info on the stack in the right place. */
  575. if (copy_to_user((void __user *)csp, mm->saved_auxv,
  576. ei_index * sizeof(elf_addr_t)))
  577. return -EFAULT;
  578. /* allocate room for argv[] and envv[] */
  579. csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
  580. envp = (elf_caddr_t __user *) csp;
  581. csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
  582. argv = (elf_caddr_t __user *) csp;
  583. /* stack argc */
  584. csp -= sizeof(unsigned long);
  585. if (put_user(bprm->argc, (unsigned long __user *) csp))
  586. return -EFAULT;
  587. BUG_ON(csp != sp);
  588. /* fill in the argv[] array */
  589. #ifdef CONFIG_MMU
  590. current->mm->arg_start = bprm->p;
  591. #else
  592. current->mm->arg_start = current->mm->start_stack -
  593. (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
  594. #endif
  595. p = (char __user *) current->mm->arg_start;
  596. for (loop = bprm->argc; loop > 0; loop--) {
  597. if (put_user((elf_caddr_t) p, argv++))
  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, argv))
  605. return -EFAULT;
  606. current->mm->arg_end = (unsigned long) p;
  607. /* fill in the envv[] array */
  608. current->mm->env_start = (unsigned long) p;
  609. for (loop = bprm->envc; loop > 0; loop--) {
  610. if (put_user((elf_caddr_t)(unsigned long) p, envp++))
  611. return -EFAULT;
  612. len = strnlen_user(p, MAX_ARG_STRLEN);
  613. if (!len || len > MAX_ARG_STRLEN)
  614. return -EINVAL;
  615. p += len;
  616. }
  617. if (put_user(NULL, envp))
  618. return -EFAULT;
  619. current->mm->env_end = (unsigned long) p;
  620. mm->start_stack = (unsigned long) sp;
  621. return 0;
  622. }
  623. /*****************************************************************************/
  624. /*
  625. * load the appropriate binary image (executable or interpreter) into memory
  626. * - we assume no MMU is available
  627. * - if no other PIC bits are set in params->hdr->e_flags
  628. * - we assume that the LOADable segments in the binary are independently relocatable
  629. * - we assume R/O executable segments are shareable
  630. * - else
  631. * - we assume the loadable parts of the image to require fixed displacement
  632. * - the image is not shareable
  633. */
  634. static int elf_fdpic_map_file(struct elf_fdpic_params *params,
  635. struct file *file,
  636. struct mm_struct *mm,
  637. const char *what)
  638. {
  639. struct elf_fdpic_loadmap *loadmap;
  640. #ifdef CONFIG_MMU
  641. struct elf_fdpic_loadseg *mseg;
  642. unsigned long load_addr;
  643. #endif
  644. struct elf_fdpic_loadseg *seg;
  645. struct elf_phdr *phdr;
  646. unsigned nloads, tmp;
  647. unsigned long stop;
  648. int loop, ret;
  649. /* allocate a load map table */
  650. nloads = 0;
  651. for (loop = 0; loop < params->hdr.e_phnum; loop++)
  652. if (params->phdrs[loop].p_type == PT_LOAD)
  653. nloads++;
  654. if (nloads == 0)
  655. return -ELIBBAD;
  656. loadmap = kzalloc_flex(*loadmap, segs, nloads);
  657. if (!loadmap)
  658. return -ENOMEM;
  659. params->loadmap = loadmap;
  660. loadmap->version = ELF_FDPIC_LOADMAP_VERSION;
  661. loadmap->nsegs = nloads;
  662. /* map the requested LOADs into the memory space */
  663. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  664. case ELF_FDPIC_FLAG_CONSTDISP:
  665. case ELF_FDPIC_FLAG_CONTIGUOUS:
  666. #ifndef CONFIG_MMU
  667. ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
  668. if (ret < 0)
  669. return ret;
  670. break;
  671. #endif
  672. default:
  673. ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
  674. if (ret < 0)
  675. return ret;
  676. break;
  677. }
  678. /* map the entry point */
  679. if (params->hdr.e_entry) {
  680. seg = loadmap->segs;
  681. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  682. if (params->hdr.e_entry >= seg->p_vaddr &&
  683. params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
  684. params->entry_addr =
  685. (params->hdr.e_entry - seg->p_vaddr) +
  686. seg->addr;
  687. break;
  688. }
  689. }
  690. }
  691. /* determine where the program header table has wound up if mapped */
  692. stop = params->hdr.e_phoff;
  693. stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
  694. phdr = params->phdrs;
  695. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  696. if (phdr->p_type != PT_LOAD)
  697. continue;
  698. if (phdr->p_offset > params->hdr.e_phoff ||
  699. phdr->p_offset + phdr->p_filesz < stop)
  700. continue;
  701. seg = loadmap->segs;
  702. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  703. if (phdr->p_vaddr >= seg->p_vaddr &&
  704. phdr->p_vaddr + phdr->p_filesz <=
  705. seg->p_vaddr + seg->p_memsz) {
  706. params->ph_addr =
  707. (phdr->p_vaddr - seg->p_vaddr) +
  708. seg->addr +
  709. params->hdr.e_phoff - phdr->p_offset;
  710. break;
  711. }
  712. }
  713. break;
  714. }
  715. /* determine where the dynamic section has wound up if there is one */
  716. phdr = params->phdrs;
  717. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  718. if (phdr->p_type != PT_DYNAMIC)
  719. continue;
  720. seg = loadmap->segs;
  721. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  722. if (phdr->p_vaddr >= seg->p_vaddr &&
  723. phdr->p_vaddr + phdr->p_memsz <=
  724. seg->p_vaddr + seg->p_memsz) {
  725. Elf_Dyn __user *dyn;
  726. Elf_Sword d_tag;
  727. params->dynamic_addr =
  728. (phdr->p_vaddr - seg->p_vaddr) +
  729. seg->addr;
  730. /* check the dynamic section contains at least
  731. * one item, and that the last item is a NULL
  732. * entry */
  733. if (phdr->p_memsz == 0 ||
  734. phdr->p_memsz % sizeof(Elf_Dyn) != 0)
  735. goto dynamic_error;
  736. tmp = phdr->p_memsz / sizeof(Elf_Dyn);
  737. dyn = (Elf_Dyn __user *)params->dynamic_addr;
  738. if (get_user(d_tag, &dyn[tmp - 1].d_tag) ||
  739. d_tag != 0)
  740. goto dynamic_error;
  741. break;
  742. }
  743. }
  744. break;
  745. }
  746. /* now elide adjacent segments in the load map on MMU linux
  747. * - on uClinux the holes between may actually be filled with system
  748. * stuff or stuff from other processes
  749. */
  750. #ifdef CONFIG_MMU
  751. nloads = loadmap->nsegs;
  752. mseg = loadmap->segs;
  753. seg = mseg + 1;
  754. for (loop = 1; loop < nloads; loop++) {
  755. /* see if we have a candidate for merging */
  756. if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
  757. load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
  758. if (load_addr == (seg->addr & PAGE_MASK)) {
  759. mseg->p_memsz +=
  760. load_addr -
  761. (mseg->addr + mseg->p_memsz);
  762. mseg->p_memsz += seg->addr & ~PAGE_MASK;
  763. mseg->p_memsz += seg->p_memsz;
  764. loadmap->nsegs--;
  765. continue;
  766. }
  767. }
  768. mseg++;
  769. if (mseg != seg)
  770. *mseg = *seg;
  771. }
  772. #endif
  773. kdebug("Mapped Object [%s]:", what);
  774. kdebug("- elfhdr : %lx", params->elfhdr_addr);
  775. kdebug("- entry : %lx", params->entry_addr);
  776. kdebug("- PHDR[] : %lx", params->ph_addr);
  777. kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
  778. seg = loadmap->segs;
  779. for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
  780. kdebug("- LOAD[%d] : %08llx-%08llx [va=%llx ms=%llx]",
  781. loop,
  782. (unsigned long long) seg->addr,
  783. (unsigned long long) seg->addr + seg->p_memsz - 1,
  784. (unsigned long long) seg->p_vaddr,
  785. (unsigned long long) seg->p_memsz);
  786. return 0;
  787. dynamic_error:
  788. printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
  789. what, file_inode(file)->i_ino);
  790. return -ELIBBAD;
  791. }
  792. /*****************************************************************************/
  793. /*
  794. * map a file with constant displacement under uClinux
  795. */
  796. #ifndef CONFIG_MMU
  797. static int elf_fdpic_map_file_constdisp_on_uclinux(
  798. struct elf_fdpic_params *params,
  799. struct file *file,
  800. struct mm_struct *mm)
  801. {
  802. struct elf_fdpic_loadseg *seg;
  803. struct elf_phdr *phdr;
  804. unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0;
  805. int loop, ret;
  806. load_addr = params->load_addr;
  807. seg = params->loadmap->segs;
  808. /* determine the bounds of the contiguous overall allocation we must
  809. * make */
  810. phdr = params->phdrs;
  811. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  812. if (params->phdrs[loop].p_type != PT_LOAD)
  813. continue;
  814. if (base > phdr->p_vaddr)
  815. base = phdr->p_vaddr;
  816. if (top < phdr->p_vaddr + phdr->p_memsz)
  817. top = phdr->p_vaddr + phdr->p_memsz;
  818. }
  819. /* allocate one big anon block for everything */
  820. maddr = vm_mmap(NULL, load_addr, top - base,
  821. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, 0);
  822. if (IS_ERR_VALUE(maddr))
  823. return (int) maddr;
  824. if (load_addr != 0)
  825. load_addr += PAGE_ALIGN(top - base);
  826. /* and then load the file segments into it */
  827. phdr = params->phdrs;
  828. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  829. if (params->phdrs[loop].p_type != PT_LOAD)
  830. continue;
  831. seg->addr = maddr + (phdr->p_vaddr - base);
  832. seg->p_vaddr = phdr->p_vaddr;
  833. seg->p_memsz = phdr->p_memsz;
  834. ret = read_code(file, seg->addr, phdr->p_offset,
  835. phdr->p_filesz);
  836. if (ret < 0)
  837. return ret;
  838. /* map the ELF header address if in this segment */
  839. if (phdr->p_offset == 0)
  840. params->elfhdr_addr = seg->addr;
  841. /* clear any space allocated but not loaded */
  842. if (phdr->p_filesz < phdr->p_memsz) {
  843. if (clear_user((void *) (seg->addr + phdr->p_filesz),
  844. phdr->p_memsz - phdr->p_filesz))
  845. return -EFAULT;
  846. }
  847. if (mm) {
  848. if (phdr->p_flags & PF_X) {
  849. if (!mm->start_code) {
  850. mm->start_code = seg->addr;
  851. mm->end_code = seg->addr +
  852. phdr->p_memsz;
  853. }
  854. } else if (!mm->start_data) {
  855. mm->start_data = seg->addr;
  856. mm->end_data = seg->addr + phdr->p_memsz;
  857. }
  858. }
  859. seg++;
  860. }
  861. return 0;
  862. }
  863. #endif
  864. /*****************************************************************************/
  865. /*
  866. * map a binary by direct mmap() of the individual PT_LOAD segments
  867. */
  868. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
  869. struct file *file,
  870. struct mm_struct *mm)
  871. {
  872. struct elf_fdpic_loadseg *seg;
  873. struct elf_phdr *phdr;
  874. unsigned long load_addr, delta_vaddr;
  875. int loop, dvset;
  876. load_addr = params->load_addr;
  877. delta_vaddr = 0;
  878. dvset = 0;
  879. seg = params->loadmap->segs;
  880. /* deal with each load segment separately */
  881. phdr = params->phdrs;
  882. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  883. unsigned long maddr, disp, excess;
  884. int prot = 0, flags;
  885. if (phdr->p_type != PT_LOAD)
  886. continue;
  887. kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
  888. (unsigned long) phdr->p_vaddr,
  889. (unsigned long) phdr->p_offset,
  890. (unsigned long) phdr->p_filesz,
  891. (unsigned long) phdr->p_memsz);
  892. /* determine the mapping parameters */
  893. if (phdr->p_flags & PF_R) prot |= PROT_READ;
  894. if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
  895. if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
  896. flags = MAP_PRIVATE;
  897. maddr = 0;
  898. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  899. case ELF_FDPIC_FLAG_INDEPENDENT:
  900. /* PT_LOADs are independently locatable */
  901. break;
  902. case ELF_FDPIC_FLAG_HONOURVADDR:
  903. /* the specified virtual address must be honoured */
  904. maddr = phdr->p_vaddr;
  905. flags |= MAP_FIXED;
  906. break;
  907. case ELF_FDPIC_FLAG_CONSTDISP:
  908. /* constant displacement
  909. * - can be mapped anywhere, but must be mapped as a
  910. * unit
  911. */
  912. if (!dvset) {
  913. maddr = load_addr;
  914. delta_vaddr = phdr->p_vaddr;
  915. dvset = 1;
  916. } else {
  917. maddr = load_addr + phdr->p_vaddr - delta_vaddr;
  918. flags |= MAP_FIXED;
  919. }
  920. break;
  921. case ELF_FDPIC_FLAG_CONTIGUOUS:
  922. /* contiguity handled later */
  923. break;
  924. default:
  925. BUG();
  926. }
  927. maddr &= PAGE_MASK;
  928. /* create the mapping */
  929. disp = phdr->p_vaddr & ~PAGE_MASK;
  930. maddr = vm_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
  931. phdr->p_offset - disp);
  932. kdebug("mmap[%d] <file> sz=%llx pr=%x fl=%x of=%llx --> %08lx",
  933. loop, (unsigned long long) phdr->p_memsz + disp,
  934. prot, flags, (unsigned long long) phdr->p_offset - disp,
  935. maddr);
  936. if (IS_ERR_VALUE(maddr))
  937. return (int) maddr;
  938. if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
  939. ELF_FDPIC_FLAG_CONTIGUOUS)
  940. load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
  941. seg->addr = maddr + disp;
  942. seg->p_vaddr = phdr->p_vaddr;
  943. seg->p_memsz = phdr->p_memsz;
  944. /* map the ELF header address if in this segment */
  945. if (phdr->p_offset == 0)
  946. params->elfhdr_addr = seg->addr;
  947. /* clear the bit between beginning of mapping and beginning of
  948. * PT_LOAD */
  949. if (prot & PROT_WRITE && disp > 0) {
  950. kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
  951. if (clear_user((void __user *) maddr, disp))
  952. return -EFAULT;
  953. maddr += disp;
  954. }
  955. /* clear any space allocated but not loaded
  956. * - on uClinux we can just clear the lot
  957. * - on MMU linux we'll get a SIGBUS beyond the last page
  958. * extant in the file
  959. */
  960. excess = phdr->p_memsz - phdr->p_filesz;
  961. #ifdef CONFIG_MMU
  962. unsigned long excess1
  963. = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
  964. if (excess > excess1) {
  965. unsigned long xaddr = maddr + phdr->p_filesz + excess1;
  966. unsigned long xmaddr;
  967. flags |= MAP_FIXED | MAP_ANONYMOUS;
  968. xmaddr = vm_mmap(NULL, xaddr, excess - excess1,
  969. prot, flags, 0);
  970. kdebug("mmap[%d] <anon>"
  971. " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
  972. loop, xaddr, excess - excess1, prot, flags,
  973. xmaddr);
  974. if (xmaddr != xaddr)
  975. return -ENOMEM;
  976. }
  977. if (prot & PROT_WRITE && excess1 > 0) {
  978. kdebug("clear[%d] ad=%lx sz=%lx",
  979. loop, maddr + phdr->p_filesz, excess1);
  980. if (clear_user((void __user *) maddr + phdr->p_filesz,
  981. excess1))
  982. return -EFAULT;
  983. }
  984. #else
  985. if (excess > 0) {
  986. kdebug("clear[%d] ad=%llx sz=%lx", loop,
  987. (unsigned long long) maddr + phdr->p_filesz,
  988. excess);
  989. if (clear_user((void *) maddr + phdr->p_filesz, excess))
  990. return -EFAULT;
  991. }
  992. #endif
  993. if (mm) {
  994. if (phdr->p_flags & PF_X) {
  995. if (!mm->start_code) {
  996. mm->start_code = maddr;
  997. mm->end_code = maddr + phdr->p_memsz;
  998. }
  999. } else if (!mm->start_data) {
  1000. mm->start_data = maddr;
  1001. mm->end_data = maddr + phdr->p_memsz;
  1002. }
  1003. }
  1004. seg++;
  1005. }
  1006. return 0;
  1007. }
  1008. /*****************************************************************************/
  1009. /*
  1010. * ELF-FDPIC core dumper
  1011. *
  1012. * Modelled on fs/exec.c:aout_core_dump()
  1013. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  1014. *
  1015. * Modelled on fs/binfmt_elf.c core dumper
  1016. */
  1017. #ifdef CONFIG_ELF_CORE
  1018. struct elf_prstatus_fdpic
  1019. {
  1020. struct elf_prstatus_common common;
  1021. elf_gregset_t pr_reg; /* GP registers */
  1022. /* When using FDPIC, the loadmap addresses need to be communicated
  1023. * to GDB in order for GDB to do the necessary relocations. The
  1024. * fields (below) used to communicate this information are placed
  1025. * immediately after ``pr_reg'', so that the loadmap addresses may
  1026. * be viewed as part of the register set if so desired.
  1027. */
  1028. unsigned long pr_exec_fdpic_loadmap;
  1029. unsigned long pr_interp_fdpic_loadmap;
  1030. int pr_fpvalid; /* True if math co-processor being used. */
  1031. };
  1032. /* An ELF note in memory */
  1033. struct memelfnote
  1034. {
  1035. const char *name;
  1036. int type;
  1037. unsigned int datasz;
  1038. void *data;
  1039. };
  1040. static int notesize(struct memelfnote *en)
  1041. {
  1042. int sz;
  1043. sz = sizeof(struct elf_note);
  1044. sz += roundup(strlen(en->name) + 1, 4);
  1045. sz += roundup(en->datasz, 4);
  1046. return sz;
  1047. }
  1048. /* #define DEBUG */
  1049. static int writenote(struct memelfnote *men, struct coredump_params *cprm)
  1050. {
  1051. struct elf_note en;
  1052. en.n_namesz = strlen(men->name) + 1;
  1053. en.n_descsz = men->datasz;
  1054. en.n_type = men->type;
  1055. return dump_emit(cprm, &en, sizeof(en)) &&
  1056. dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
  1057. dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
  1058. }
  1059. static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
  1060. {
  1061. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1062. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1063. elf->e_ident[EI_DATA] = ELF_DATA;
  1064. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1065. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1066. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1067. elf->e_type = ET_CORE;
  1068. elf->e_machine = ELF_ARCH;
  1069. elf->e_version = EV_CURRENT;
  1070. elf->e_entry = 0;
  1071. elf->e_phoff = sizeof(struct elfhdr);
  1072. elf->e_shoff = 0;
  1073. elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
  1074. elf->e_ehsize = sizeof(struct elfhdr);
  1075. elf->e_phentsize = sizeof(struct elf_phdr);
  1076. elf->e_phnum = segs;
  1077. elf->e_shentsize = 0;
  1078. elf->e_shnum = 0;
  1079. elf->e_shstrndx = 0;
  1080. return;
  1081. }
  1082. static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1083. {
  1084. phdr->p_type = PT_NOTE;
  1085. phdr->p_offset = offset;
  1086. phdr->p_vaddr = 0;
  1087. phdr->p_paddr = 0;
  1088. phdr->p_filesz = sz;
  1089. phdr->p_memsz = 0;
  1090. phdr->p_flags = 0;
  1091. phdr->p_align = 4;
  1092. return;
  1093. }
  1094. static inline void __fill_note(struct memelfnote *note, const char *name, int type,
  1095. unsigned int sz, void *data)
  1096. {
  1097. note->name = name;
  1098. note->type = type;
  1099. note->datasz = sz;
  1100. note->data = data;
  1101. return;
  1102. }
  1103. #define fill_note(note, type, sz, data) \
  1104. __fill_note(note, NN_ ## type, NT_ ## type, sz, data)
  1105. /*
  1106. * fill up all the fields in prstatus from the given task struct, except
  1107. * registers which need to be filled up separately.
  1108. */
  1109. static void fill_prstatus(struct elf_prstatus_common *prstatus,
  1110. struct task_struct *p, long signr)
  1111. {
  1112. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1113. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1114. prstatus->pr_sighold = p->blocked.sig[0];
  1115. rcu_read_lock();
  1116. prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
  1117. rcu_read_unlock();
  1118. prstatus->pr_pid = task_pid_vnr(p);
  1119. prstatus->pr_pgrp = task_pgrp_vnr(p);
  1120. prstatus->pr_sid = task_session_vnr(p);
  1121. if (thread_group_leader(p)) {
  1122. struct task_cputime cputime;
  1123. /*
  1124. * This is the record for the group leader. It shows the
  1125. * group-wide total, not its individual thread total.
  1126. */
  1127. thread_group_cputime(p, &cputime);
  1128. prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime);
  1129. prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime);
  1130. } else {
  1131. u64 utime, stime;
  1132. task_cputime(p, &utime, &stime);
  1133. prstatus->pr_utime = ns_to_kernel_old_timeval(utime);
  1134. prstatus->pr_stime = ns_to_kernel_old_timeval(stime);
  1135. }
  1136. prstatus->pr_cutime = ns_to_kernel_old_timeval(p->signal->cutime);
  1137. prstatus->pr_cstime = ns_to_kernel_old_timeval(p->signal->cstime);
  1138. }
  1139. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1140. struct mm_struct *mm)
  1141. {
  1142. const struct cred *cred;
  1143. unsigned int i, len;
  1144. unsigned int state;
  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. state = READ_ONCE(p->__state);
  1164. i = state ? ffz(~state) + 1 : 0;
  1165. psinfo->pr_state = i;
  1166. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1167. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1168. psinfo->pr_nice = task_nice(p);
  1169. psinfo->pr_flag = p->flags;
  1170. rcu_read_lock();
  1171. cred = __task_cred(p);
  1172. SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
  1173. SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
  1174. rcu_read_unlock();
  1175. get_task_comm(psinfo->pr_fname, p);
  1176. return 0;
  1177. }
  1178. /* Here is the structure in which status of each thread is captured. */
  1179. struct elf_thread_status
  1180. {
  1181. struct elf_thread_status *next;
  1182. struct elf_prstatus_fdpic prstatus; /* NT_PRSTATUS */
  1183. elf_fpregset_t fpu; /* NT_PRFPREG */
  1184. struct memelfnote notes[2];
  1185. int num_notes;
  1186. };
  1187. /*
  1188. * In order to add the specific thread information for the elf file format,
  1189. * we need to keep a linked list of every thread's pr_status and then create
  1190. * a single section for them in the final core file.
  1191. */
  1192. static struct elf_thread_status *elf_dump_thread_status(long signr, struct task_struct *p, int *sz)
  1193. {
  1194. const struct user_regset_view *view = task_user_regset_view(p);
  1195. struct elf_thread_status *t;
  1196. int i, ret;
  1197. t = kzalloc_obj(struct elf_thread_status);
  1198. if (!t)
  1199. return t;
  1200. fill_prstatus(&t->prstatus.common, p, signr);
  1201. t->prstatus.pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
  1202. t->prstatus.pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
  1203. regset_get(p, &view->regsets[0],
  1204. sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg);
  1205. fill_note(&t->notes[0], PRSTATUS, sizeof(t->prstatus), &t->prstatus);
  1206. t->num_notes++;
  1207. *sz += notesize(&t->notes[0]);
  1208. for (i = 1; i < view->n; ++i) {
  1209. const struct user_regset *regset = &view->regsets[i];
  1210. if (regset->core_note_type != NT_PRFPREG)
  1211. continue;
  1212. if (regset->active && regset->active(p, regset) <= 0)
  1213. continue;
  1214. ret = regset_get(p, regset, sizeof(t->fpu), &t->fpu);
  1215. if (ret >= 0)
  1216. t->prstatus.pr_fpvalid = 1;
  1217. break;
  1218. }
  1219. if (t->prstatus.pr_fpvalid) {
  1220. fill_note(&t->notes[1], PRFPREG, sizeof(t->fpu), &t->fpu);
  1221. t->num_notes++;
  1222. *sz += notesize(&t->notes[1]);
  1223. }
  1224. return t;
  1225. }
  1226. static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
  1227. elf_addr_t e_shoff, int segs)
  1228. {
  1229. elf->e_shoff = e_shoff;
  1230. elf->e_shentsize = sizeof(*shdr4extnum);
  1231. elf->e_shnum = 1;
  1232. elf->e_shstrndx = SHN_UNDEF;
  1233. memset(shdr4extnum, 0, sizeof(*shdr4extnum));
  1234. shdr4extnum->sh_type = SHT_NULL;
  1235. shdr4extnum->sh_size = elf->e_shnum;
  1236. shdr4extnum->sh_link = elf->e_shstrndx;
  1237. shdr4extnum->sh_info = segs;
  1238. }
  1239. /*
  1240. * dump the segments for an MMU process
  1241. */
  1242. static bool elf_fdpic_dump_segments(struct coredump_params *cprm,
  1243. struct core_vma_metadata *vma_meta,
  1244. int vma_count)
  1245. {
  1246. int i;
  1247. for (i = 0; i < vma_count; i++) {
  1248. struct core_vma_metadata *meta = vma_meta + i;
  1249. if (!dump_user_range(cprm, meta->start, meta->dump_size))
  1250. return false;
  1251. }
  1252. return true;
  1253. }
  1254. /*
  1255. * Actual dumper
  1256. *
  1257. * This is a two-pass process; first we find the offsets of the bits,
  1258. * and then they are actually written out. If we run out of core limit
  1259. * we just truncate.
  1260. */
  1261. static int elf_fdpic_core_dump(struct coredump_params *cprm)
  1262. {
  1263. int has_dumped = 0;
  1264. int segs;
  1265. int i;
  1266. struct elfhdr *elf = NULL;
  1267. loff_t offset = 0, dataoff;
  1268. struct memelfnote psinfo_note, auxv_note;
  1269. struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
  1270. struct elf_thread_status *thread_list = NULL;
  1271. int thread_status_size = 0;
  1272. elf_addr_t *auxv;
  1273. struct elf_phdr *phdr4note = NULL;
  1274. struct elf_shdr *shdr4extnum = NULL;
  1275. Elf_Half e_phnum;
  1276. elf_addr_t e_shoff;
  1277. struct core_thread *ct;
  1278. struct elf_thread_status *tmp;
  1279. /* alloc memory for large data structures: too large to be on stack */
  1280. elf = kmalloc_obj(*elf);
  1281. if (!elf)
  1282. goto end_coredump;
  1283. psinfo = kmalloc_obj(*psinfo);
  1284. if (!psinfo)
  1285. goto end_coredump;
  1286. for (ct = current->signal->core_state->dumper.next;
  1287. ct; ct = ct->next) {
  1288. tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
  1289. ct->task, &thread_status_size);
  1290. if (!tmp)
  1291. goto end_coredump;
  1292. tmp->next = thread_list;
  1293. thread_list = tmp;
  1294. }
  1295. /* now collect the dump for the current */
  1296. tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
  1297. current, &thread_status_size);
  1298. if (!tmp)
  1299. goto end_coredump;
  1300. tmp->next = thread_list;
  1301. thread_list = tmp;
  1302. segs = cprm->vma_count + elf_core_extra_phdrs(cprm);
  1303. /* for notes section */
  1304. segs++;
  1305. /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
  1306. * this, kernel supports extended numbering. Have a look at
  1307. * include/linux/elf.h for further information. */
  1308. e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
  1309. /* Set up header */
  1310. fill_elf_fdpic_header(elf, e_phnum);
  1311. has_dumped = 1;
  1312. /*
  1313. * Set up the notes in similar form to SVR4 core dumps made
  1314. * with info from their /proc.
  1315. */
  1316. fill_psinfo(psinfo, current->group_leader, current->mm);
  1317. fill_note(&psinfo_note, PRPSINFO, sizeof(*psinfo), psinfo);
  1318. thread_status_size += notesize(&psinfo_note);
  1319. auxv = (elf_addr_t *) current->mm->saved_auxv;
  1320. i = 0;
  1321. do
  1322. i += 2;
  1323. while (auxv[i - 2] != AT_NULL);
  1324. fill_note(&auxv_note, AUXV, i * sizeof(elf_addr_t), auxv);
  1325. thread_status_size += notesize(&auxv_note);
  1326. offset = sizeof(*elf); /* ELF header */
  1327. offset += segs * sizeof(struct elf_phdr); /* Program headers */
  1328. /* Write notes phdr entry */
  1329. phdr4note = kmalloc_obj(*phdr4note);
  1330. if (!phdr4note)
  1331. goto end_coredump;
  1332. fill_elf_note_phdr(phdr4note, thread_status_size, offset);
  1333. offset += thread_status_size;
  1334. /* Page-align dumped data */
  1335. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1336. offset += cprm->vma_data_size;
  1337. offset += elf_core_extra_data_size(cprm);
  1338. e_shoff = offset;
  1339. if (e_phnum == PN_XNUM) {
  1340. shdr4extnum = kmalloc_obj(*shdr4extnum);
  1341. if (!shdr4extnum)
  1342. goto end_coredump;
  1343. fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
  1344. }
  1345. offset = dataoff;
  1346. if (!dump_emit(cprm, elf, sizeof(*elf)))
  1347. goto end_coredump;
  1348. if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
  1349. goto end_coredump;
  1350. /* write program headers for segments dump */
  1351. for (i = 0; i < cprm->vma_count; i++) {
  1352. struct core_vma_metadata *meta = cprm->vma_meta + i;
  1353. struct elf_phdr phdr;
  1354. size_t sz;
  1355. sz = meta->end - meta->start;
  1356. phdr.p_type = PT_LOAD;
  1357. phdr.p_offset = offset;
  1358. phdr.p_vaddr = meta->start;
  1359. phdr.p_paddr = 0;
  1360. phdr.p_filesz = meta->dump_size;
  1361. phdr.p_memsz = sz;
  1362. offset += phdr.p_filesz;
  1363. phdr.p_flags = 0;
  1364. if (meta->flags & VM_READ)
  1365. phdr.p_flags |= PF_R;
  1366. if (meta->flags & VM_WRITE)
  1367. phdr.p_flags |= PF_W;
  1368. if (meta->flags & VM_EXEC)
  1369. phdr.p_flags |= PF_X;
  1370. phdr.p_align = ELF_EXEC_PAGESIZE;
  1371. if (!dump_emit(cprm, &phdr, sizeof(phdr)))
  1372. goto end_coredump;
  1373. }
  1374. if (!elf_core_write_extra_phdrs(cprm, offset))
  1375. goto end_coredump;
  1376. /* write out the notes section */
  1377. if (!writenote(thread_list->notes, cprm))
  1378. goto end_coredump;
  1379. if (!writenote(&psinfo_note, cprm))
  1380. goto end_coredump;
  1381. if (!writenote(&auxv_note, cprm))
  1382. goto end_coredump;
  1383. for (i = 1; i < thread_list->num_notes; i++)
  1384. if (!writenote(thread_list->notes + i, cprm))
  1385. goto end_coredump;
  1386. /* write out the thread status notes section */
  1387. for (tmp = thread_list->next; tmp; tmp = tmp->next) {
  1388. for (i = 0; i < tmp->num_notes; i++)
  1389. if (!writenote(&tmp->notes[i], cprm))
  1390. goto end_coredump;
  1391. }
  1392. dump_skip_to(cprm, dataoff);
  1393. if (!elf_fdpic_dump_segments(cprm, cprm->vma_meta, cprm->vma_count))
  1394. goto end_coredump;
  1395. if (!elf_core_write_extra_data(cprm))
  1396. goto end_coredump;
  1397. if (e_phnum == PN_XNUM) {
  1398. if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
  1399. goto end_coredump;
  1400. }
  1401. if (cprm->file->f_pos != offset) {
  1402. /* Sanity check */
  1403. printk(KERN_WARNING
  1404. "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
  1405. cprm->file->f_pos, offset);
  1406. }
  1407. end_coredump:
  1408. while (thread_list) {
  1409. tmp = thread_list;
  1410. thread_list = thread_list->next;
  1411. kfree(tmp);
  1412. }
  1413. kfree(phdr4note);
  1414. kfree(elf);
  1415. kfree(psinfo);
  1416. kfree(shdr4extnum);
  1417. return has_dumped;
  1418. }
  1419. #endif /* CONFIG_ELF_CORE */