binfmt_elf_fdpic.c 44 KB

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