binfmt_elf_fdpic.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /* binfmt_elf_fdpic.c: FDPIC ELF binary format
  2. *
  3. * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * Derived from binfmt_elf.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/errno.h>
  19. #include <linux/signal.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/string.h>
  22. #include <linux/file.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/slab.h>
  25. #include <linux/highmem.h>
  26. #include <linux/personality.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/init.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/elf.h>
  31. #include <linux/elf-fdpic.h>
  32. #include <linux/elfcore.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/param.h>
  35. #include <asm/pgalloc.h>
  36. typedef char *elf_caddr_t;
  37. #ifndef elf_addr_t
  38. #define elf_addr_t unsigned long
  39. #endif
  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. MODULE_LICENSE("GPL");
  46. static int load_elf_fdpic_binary(struct linux_binprm *, struct pt_regs *);
  47. static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
  48. static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
  49. struct mm_struct *, const char *);
  50. static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
  51. struct elf_fdpic_params *,
  52. struct elf_fdpic_params *);
  53. #ifndef CONFIG_MMU
  54. static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *,
  55. unsigned long *);
  56. static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
  57. struct file *,
  58. struct mm_struct *);
  59. #endif
  60. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
  61. struct file *, struct mm_struct *);
  62. static struct linux_binfmt elf_fdpic_format = {
  63. .module = THIS_MODULE,
  64. .load_binary = load_elf_fdpic_binary,
  65. // .core_dump = elf_fdpic_core_dump,
  66. .min_coredump = ELF_EXEC_PAGESIZE,
  67. };
  68. static int __init init_elf_fdpic_binfmt(void)
  69. {
  70. return register_binfmt(&elf_fdpic_format);
  71. }
  72. static void __exit exit_elf_fdpic_binfmt(void)
  73. {
  74. unregister_binfmt(&elf_fdpic_format);
  75. }
  76. module_init(init_elf_fdpic_binfmt);
  77. module_exit(exit_elf_fdpic_binfmt);
  78. static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
  79. {
  80. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
  81. return 0;
  82. if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
  83. return 0;
  84. if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
  85. return 0;
  86. if (!file->f_op || !file->f_op->mmap)
  87. return 0;
  88. return 1;
  89. }
  90. /*****************************************************************************/
  91. /*
  92. * read the program headers table into memory
  93. */
  94. static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
  95. struct file *file)
  96. {
  97. struct elf32_phdr *phdr;
  98. unsigned long size;
  99. int retval, loop;
  100. if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
  101. return -ENOMEM;
  102. if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
  103. return -ENOMEM;
  104. size = params->hdr.e_phnum * sizeof(struct elf_phdr);
  105. params->phdrs = kmalloc(size, GFP_KERNEL);
  106. if (!params->phdrs)
  107. return -ENOMEM;
  108. retval = kernel_read(file, params->hdr.e_phoff,
  109. (char *) params->phdrs, size);
  110. if (retval < 0)
  111. return retval;
  112. /* determine stack size for this binary */
  113. phdr = params->phdrs;
  114. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  115. if (phdr->p_type != PT_GNU_STACK)
  116. continue;
  117. if (phdr->p_flags & PF_X)
  118. params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
  119. else
  120. params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
  121. params->stack_size = phdr->p_memsz;
  122. break;
  123. }
  124. return 0;
  125. }
  126. /*****************************************************************************/
  127. /*
  128. * load an fdpic binary into various bits of memory
  129. */
  130. static int load_elf_fdpic_binary(struct linux_binprm *bprm,
  131. struct pt_regs *regs)
  132. {
  133. struct elf_fdpic_params exec_params, interp_params;
  134. struct elf_phdr *phdr;
  135. unsigned long stack_size, entryaddr;
  136. #ifndef CONFIG_MMU
  137. unsigned long fullsize;
  138. #endif
  139. #ifdef ELF_FDPIC_PLAT_INIT
  140. unsigned long dynaddr;
  141. #endif
  142. struct file *interpreter = NULL; /* to shut gcc up */
  143. char *interpreter_name = NULL;
  144. int executable_stack;
  145. int retval, i;
  146. memset(&exec_params, 0, sizeof(exec_params));
  147. memset(&interp_params, 0, sizeof(interp_params));
  148. exec_params.hdr = *(struct elfhdr *) bprm->buf;
  149. exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
  150. /* check that this is a binary we know how to deal with */
  151. retval = -ENOEXEC;
  152. if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
  153. goto error;
  154. /* read the program header table */
  155. retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
  156. if (retval < 0)
  157. goto error;
  158. /* scan for a program header that specifies an interpreter */
  159. phdr = exec_params.phdrs;
  160. for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
  161. switch (phdr->p_type) {
  162. case PT_INTERP:
  163. retval = -ENOMEM;
  164. if (phdr->p_filesz > PATH_MAX)
  165. goto error;
  166. retval = -ENOENT;
  167. if (phdr->p_filesz < 2)
  168. goto error;
  169. /* read the name of the interpreter into memory */
  170. interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
  171. if (!interpreter_name)
  172. goto error;
  173. retval = kernel_read(bprm->file,
  174. phdr->p_offset,
  175. interpreter_name,
  176. phdr->p_filesz);
  177. if (retval < 0)
  178. goto error;
  179. retval = -ENOENT;
  180. if (interpreter_name[phdr->p_filesz - 1] != '\0')
  181. goto error;
  182. kdebug("Using ELF interpreter %s", interpreter_name);
  183. /* replace the program with the interpreter */
  184. interpreter = open_exec(interpreter_name);
  185. retval = PTR_ERR(interpreter);
  186. if (IS_ERR(interpreter)) {
  187. interpreter = NULL;
  188. goto error;
  189. }
  190. retval = kernel_read(interpreter, 0, bprm->buf,
  191. BINPRM_BUF_SIZE);
  192. if (retval < 0)
  193. goto error;
  194. interp_params.hdr = *((struct elfhdr *) bprm->buf);
  195. break;
  196. case PT_LOAD:
  197. #ifdef CONFIG_MMU
  198. if (exec_params.load_addr == 0)
  199. exec_params.load_addr = phdr->p_vaddr;
  200. #endif
  201. break;
  202. }
  203. }
  204. if (elf_check_const_displacement(&exec_params.hdr))
  205. exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
  206. /* perform insanity checks on the interpreter */
  207. if (interpreter_name) {
  208. retval = -ELIBBAD;
  209. if (!is_elf_fdpic(&interp_params.hdr, interpreter))
  210. goto error;
  211. interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
  212. /* read the interpreter's program header table */
  213. retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
  214. if (retval < 0)
  215. goto error;
  216. }
  217. stack_size = exec_params.stack_size;
  218. if (stack_size < interp_params.stack_size)
  219. stack_size = interp_params.stack_size;
  220. if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
  221. executable_stack = EXSTACK_ENABLE_X;
  222. else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
  223. executable_stack = EXSTACK_DISABLE_X;
  224. else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
  225. executable_stack = EXSTACK_ENABLE_X;
  226. else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
  227. executable_stack = EXSTACK_DISABLE_X;
  228. else
  229. executable_stack = EXSTACK_DEFAULT;
  230. retval = -ENOEXEC;
  231. if (stack_size == 0)
  232. goto error;
  233. if (elf_check_const_displacement(&interp_params.hdr))
  234. interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
  235. /* flush all traces of the currently running executable */
  236. retval = flush_old_exec(bprm);
  237. if (retval)
  238. goto error;
  239. /* there's now no turning back... the old userspace image is dead,
  240. * defunct, deceased, etc. after this point we have to exit via
  241. * error_kill */
  242. set_personality(PER_LINUX_FDPIC);
  243. set_binfmt(&elf_fdpic_format);
  244. current->mm->start_code = 0;
  245. current->mm->end_code = 0;
  246. current->mm->start_stack = 0;
  247. current->mm->start_data = 0;
  248. current->mm->end_data = 0;
  249. current->mm->context.exec_fdpic_loadmap = 0;
  250. current->mm->context.interp_fdpic_loadmap = 0;
  251. current->flags &= ~PF_FORKNOEXEC;
  252. #ifdef CONFIG_MMU
  253. elf_fdpic_arch_lay_out_mm(&exec_params,
  254. &interp_params,
  255. &current->mm->start_stack,
  256. &current->mm->start_brk);
  257. retval = setup_arg_pages(bprm, current->mm->start_stack,
  258. executable_stack);
  259. if (retval < 0) {
  260. send_sig(SIGKILL, current, 0);
  261. goto error_kill;
  262. }
  263. #endif
  264. /* load the executable and interpreter into memory */
  265. retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
  266. "executable");
  267. if (retval < 0)
  268. goto error_kill;
  269. if (interpreter_name) {
  270. retval = elf_fdpic_map_file(&interp_params, interpreter,
  271. current->mm, "interpreter");
  272. if (retval < 0) {
  273. printk(KERN_ERR "Unable to load interpreter\n");
  274. goto error_kill;
  275. }
  276. allow_write_access(interpreter);
  277. fput(interpreter);
  278. interpreter = NULL;
  279. }
  280. #ifdef CONFIG_MMU
  281. if (!current->mm->start_brk)
  282. current->mm->start_brk = current->mm->end_data;
  283. current->mm->brk = current->mm->start_brk =
  284. PAGE_ALIGN(current->mm->start_brk);
  285. #else
  286. /* create a stack and brk area big enough for everyone
  287. * - the brk heap starts at the bottom and works up
  288. * - the stack starts at the top and works down
  289. */
  290. stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
  291. if (stack_size < PAGE_SIZE * 2)
  292. stack_size = PAGE_SIZE * 2;
  293. down_write(&current->mm->mmap_sem);
  294. current->mm->start_brk = do_mmap(NULL, 0, stack_size,
  295. PROT_READ | PROT_WRITE | PROT_EXEC,
  296. MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
  297. 0);
  298. if (IS_ERR_VALUE(current->mm->start_brk)) {
  299. up_write(&current->mm->mmap_sem);
  300. retval = current->mm->start_brk;
  301. current->mm->start_brk = 0;
  302. goto error_kill;
  303. }
  304. /* expand the stack mapping to use up the entire allocation granule */
  305. fullsize = ksize((char *) current->mm->start_brk);
  306. if (!IS_ERR_VALUE(do_mremap(current->mm->start_brk, stack_size,
  307. fullsize, 0, 0)))
  308. stack_size = fullsize;
  309. up_write(&current->mm->mmap_sem);
  310. current->mm->brk = current->mm->start_brk;
  311. current->mm->context.end_brk = current->mm->start_brk;
  312. current->mm->context.end_brk +=
  313. (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
  314. current->mm->start_stack = current->mm->start_brk + stack_size;
  315. #endif
  316. compute_creds(bprm);
  317. current->flags &= ~PF_FORKNOEXEC;
  318. if (create_elf_fdpic_tables(bprm, current->mm,
  319. &exec_params, &interp_params) < 0)
  320. goto error_kill;
  321. kdebug("- start_code %lx", current->mm->start_code);
  322. kdebug("- end_code %lx", current->mm->end_code);
  323. kdebug("- start_data %lx", current->mm->start_data);
  324. kdebug("- end_data %lx", current->mm->end_data);
  325. kdebug("- start_brk %lx", current->mm->start_brk);
  326. kdebug("- brk %lx", current->mm->brk);
  327. kdebug("- start_stack %lx", current->mm->start_stack);
  328. #ifdef ELF_FDPIC_PLAT_INIT
  329. /*
  330. * The ABI may specify that certain registers be set up in special
  331. * ways (on i386 %edx is the address of a DT_FINI function, for
  332. * example. This macro performs whatever initialization to
  333. * the regs structure is required.
  334. */
  335. dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
  336. ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
  337. dynaddr);
  338. #endif
  339. /* everything is now ready... get the userspace context ready to roll */
  340. entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
  341. start_thread(regs, entryaddr, current->mm->start_stack);
  342. if (unlikely(current->ptrace & PT_PTRACED)) {
  343. if (current->ptrace & PT_TRACE_EXEC)
  344. ptrace_notify((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  345. else
  346. send_sig(SIGTRAP, current, 0);
  347. }
  348. retval = 0;
  349. error:
  350. if (interpreter) {
  351. allow_write_access(interpreter);
  352. fput(interpreter);
  353. }
  354. kfree(interpreter_name);
  355. kfree(exec_params.phdrs);
  356. kfree(exec_params.loadmap);
  357. kfree(interp_params.phdrs);
  358. kfree(interp_params.loadmap);
  359. return retval;
  360. /* unrecoverable error - kill the process */
  361. error_kill:
  362. send_sig(SIGSEGV, current, 0);
  363. goto error;
  364. }
  365. /*****************************************************************************/
  366. /*
  367. * present useful information to the program
  368. */
  369. static int create_elf_fdpic_tables(struct linux_binprm *bprm,
  370. struct mm_struct *mm,
  371. struct elf_fdpic_params *exec_params,
  372. struct elf_fdpic_params *interp_params)
  373. {
  374. unsigned long sp, csp, nitems;
  375. elf_caddr_t __user *argv, *envp;
  376. size_t platform_len = 0, len;
  377. char *k_platform;
  378. char __user *u_platform, *p;
  379. long hwcap;
  380. int loop;
  381. /* we're going to shovel a whole load of stuff onto the stack */
  382. #ifdef CONFIG_MMU
  383. sp = bprm->p;
  384. #else
  385. sp = mm->start_stack;
  386. /* stack the program arguments and environment */
  387. if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
  388. return -EFAULT;
  389. #endif
  390. /* get hold of platform and hardware capabilities masks for the machine
  391. * we are running on. In some cases (Sparc), this info is impossible
  392. * to get, in others (i386) it is merely difficult.
  393. */
  394. hwcap = ELF_HWCAP;
  395. k_platform = ELF_PLATFORM;
  396. u_platform = NULL;
  397. if (k_platform) {
  398. platform_len = strlen(k_platform) + 1;
  399. sp -= platform_len;
  400. u_platform = (char __user *) sp;
  401. if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
  402. return -EFAULT;
  403. }
  404. #if defined(__i386__) && defined(CONFIG_SMP)
  405. /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  406. * by the processes running on the same package. One thing we can do is
  407. * to shuffle the initial stack for them.
  408. *
  409. * the conditionals here are unneeded, but kept in to make the code
  410. * behaviour the same as pre change unless we have hyperthreaded
  411. * processors. This keeps Mr Marcelo Person happier but should be
  412. * removed for 2.5
  413. */
  414. if (smp_num_siblings > 1)
  415. sp = sp - ((current->pid % 64) << 7);
  416. #endif
  417. sp &= ~7UL;
  418. /* stack the load map(s) */
  419. len = sizeof(struct elf32_fdpic_loadmap);
  420. len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
  421. sp = (sp - len) & ~7UL;
  422. exec_params->map_addr = sp;
  423. if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
  424. return -EFAULT;
  425. current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
  426. if (interp_params->loadmap) {
  427. len = sizeof(struct elf32_fdpic_loadmap);
  428. len += sizeof(struct elf32_fdpic_loadseg) *
  429. interp_params->loadmap->nsegs;
  430. sp = (sp - len) & ~7UL;
  431. interp_params->map_addr = sp;
  432. if (copy_to_user((void __user *) sp, interp_params->loadmap,
  433. len) != 0)
  434. return -EFAULT;
  435. current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
  436. }
  437. /* force 16 byte _final_ alignment here for generality */
  438. #define DLINFO_ITEMS 13
  439. nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
  440. #ifdef DLINFO_ARCH_ITEMS
  441. nitems += DLINFO_ARCH_ITEMS;
  442. #endif
  443. csp = sp;
  444. sp -= nitems * 2 * sizeof(unsigned long);
  445. sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
  446. sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
  447. sp -= 1 * sizeof(unsigned long); /* argc */
  448. csp -= sp & 15UL;
  449. sp -= sp & 15UL;
  450. /* put the ELF interpreter info on the stack */
  451. #define NEW_AUX_ENT(nr, id, val) \
  452. do { \
  453. struct { unsigned long _id, _val; } __user *ent; \
  454. \
  455. ent = (void __user *) csp; \
  456. __put_user((id), &ent[nr]._id); \
  457. __put_user((val), &ent[nr]._val); \
  458. } while (0)
  459. csp -= 2 * sizeof(unsigned long);
  460. NEW_AUX_ENT(0, AT_NULL, 0);
  461. if (k_platform) {
  462. csp -= 2 * sizeof(unsigned long);
  463. NEW_AUX_ENT(0, AT_PLATFORM,
  464. (elf_addr_t) (unsigned long) u_platform);
  465. }
  466. csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
  467. NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
  468. NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE);
  469. NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
  470. NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr);
  471. NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr));
  472. NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum);
  473. NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr);
  474. NEW_AUX_ENT( 7, AT_FLAGS, 0);
  475. NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr);
  476. NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
  477. NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
  478. NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
  479. NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
  480. #ifdef ARCH_DLINFO
  481. /* ARCH_DLINFO must come last so platform specific code can enforce
  482. * special alignment requirements on the AUXV if necessary (eg. PPC).
  483. */
  484. ARCH_DLINFO;
  485. #endif
  486. #undef NEW_AUX_ENT
  487. /* allocate room for argv[] and envv[] */
  488. csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
  489. envp = (elf_caddr_t __user *) csp;
  490. csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
  491. argv = (elf_caddr_t __user *) csp;
  492. /* stack argc */
  493. csp -= sizeof(unsigned long);
  494. __put_user(bprm->argc, (unsigned long __user *) csp);
  495. BUG_ON(csp != sp);
  496. /* fill in the argv[] array */
  497. #ifdef CONFIG_MMU
  498. current->mm->arg_start = bprm->p;
  499. #else
  500. current->mm->arg_start = current->mm->start_stack -
  501. (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
  502. #endif
  503. p = (char __user *) current->mm->arg_start;
  504. for (loop = bprm->argc; loop > 0; loop--) {
  505. __put_user((elf_caddr_t) p, argv++);
  506. len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
  507. if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
  508. return -EINVAL;
  509. p += len;
  510. }
  511. __put_user(NULL, argv);
  512. current->mm->arg_end = (unsigned long) p;
  513. /* fill in the envv[] array */
  514. current->mm->env_start = (unsigned long) p;
  515. for (loop = bprm->envc; loop > 0; loop--) {
  516. __put_user((elf_caddr_t)(unsigned long) p, envp++);
  517. len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
  518. if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
  519. return -EINVAL;
  520. p += len;
  521. }
  522. __put_user(NULL, envp);
  523. current->mm->env_end = (unsigned long) p;
  524. mm->start_stack = (unsigned long) sp;
  525. return 0;
  526. }
  527. /*****************************************************************************/
  528. /*
  529. * transfer the program arguments and environment from the holding pages onto
  530. * the stack
  531. */
  532. #ifndef CONFIG_MMU
  533. static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm,
  534. unsigned long *_sp)
  535. {
  536. unsigned long index, stop, sp;
  537. char *src;
  538. int ret = 0;
  539. stop = bprm->p >> PAGE_SHIFT;
  540. sp = *_sp;
  541. for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
  542. src = kmap(bprm->page[index]);
  543. sp -= PAGE_SIZE;
  544. if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
  545. ret = -EFAULT;
  546. kunmap(bprm->page[index]);
  547. if (ret < 0)
  548. goto out;
  549. }
  550. *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
  551. out:
  552. return ret;
  553. }
  554. #endif
  555. /*****************************************************************************/
  556. /*
  557. * load the appropriate binary image (executable or interpreter) into memory
  558. * - we assume no MMU is available
  559. * - if no other PIC bits are set in params->hdr->e_flags
  560. * - we assume that the LOADable segments in the binary are independently relocatable
  561. * - we assume R/O executable segments are shareable
  562. * - else
  563. * - we assume the loadable parts of the image to require fixed displacement
  564. * - the image is not shareable
  565. */
  566. static int elf_fdpic_map_file(struct elf_fdpic_params *params,
  567. struct file *file,
  568. struct mm_struct *mm,
  569. const char *what)
  570. {
  571. struct elf32_fdpic_loadmap *loadmap;
  572. #ifdef CONFIG_MMU
  573. struct elf32_fdpic_loadseg *mseg;
  574. #endif
  575. struct elf32_fdpic_loadseg *seg;
  576. struct elf32_phdr *phdr;
  577. unsigned long load_addr, stop;
  578. unsigned nloads, tmp;
  579. size_t size;
  580. int loop, ret;
  581. /* allocate a load map table */
  582. nloads = 0;
  583. for (loop = 0; loop < params->hdr.e_phnum; loop++)
  584. if (params->phdrs[loop].p_type == PT_LOAD)
  585. nloads++;
  586. if (nloads == 0)
  587. return -ELIBBAD;
  588. size = sizeof(*loadmap) + nloads * sizeof(*seg);
  589. loadmap = kmalloc(size, GFP_KERNEL);
  590. if (!loadmap)
  591. return -ENOMEM;
  592. params->loadmap = loadmap;
  593. memset(loadmap, 0, size);
  594. loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
  595. loadmap->nsegs = nloads;
  596. load_addr = params->load_addr;
  597. seg = loadmap->segs;
  598. /* map the requested LOADs into the memory space */
  599. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  600. case ELF_FDPIC_FLAG_CONSTDISP:
  601. case ELF_FDPIC_FLAG_CONTIGUOUS:
  602. #ifndef CONFIG_MMU
  603. ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
  604. if (ret < 0)
  605. return ret;
  606. break;
  607. #endif
  608. default:
  609. ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
  610. if (ret < 0)
  611. return ret;
  612. break;
  613. }
  614. /* map the entry point */
  615. if (params->hdr.e_entry) {
  616. seg = loadmap->segs;
  617. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  618. if (params->hdr.e_entry >= seg->p_vaddr &&
  619. params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
  620. params->entry_addr =
  621. (params->hdr.e_entry - seg->p_vaddr) +
  622. seg->addr;
  623. break;
  624. }
  625. }
  626. }
  627. /* determine where the program header table has wound up if mapped */
  628. stop = params->hdr.e_phoff;
  629. stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
  630. phdr = params->phdrs;
  631. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  632. if (phdr->p_type != PT_LOAD)
  633. continue;
  634. if (phdr->p_offset > params->hdr.e_phoff ||
  635. phdr->p_offset + phdr->p_filesz < stop)
  636. continue;
  637. seg = loadmap->segs;
  638. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  639. if (phdr->p_vaddr >= seg->p_vaddr &&
  640. phdr->p_vaddr + phdr->p_filesz <=
  641. seg->p_vaddr + seg->p_memsz) {
  642. params->ph_addr =
  643. (phdr->p_vaddr - seg->p_vaddr) +
  644. seg->addr +
  645. params->hdr.e_phoff - phdr->p_offset;
  646. break;
  647. }
  648. }
  649. break;
  650. }
  651. /* determine where the dynamic section has wound up if there is one */
  652. phdr = params->phdrs;
  653. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  654. if (phdr->p_type != PT_DYNAMIC)
  655. continue;
  656. seg = loadmap->segs;
  657. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  658. if (phdr->p_vaddr >= seg->p_vaddr &&
  659. phdr->p_vaddr + phdr->p_memsz <=
  660. seg->p_vaddr + seg->p_memsz) {
  661. params->dynamic_addr =
  662. (phdr->p_vaddr - seg->p_vaddr) +
  663. seg->addr;
  664. /* check the dynamic section contains at least
  665. * one item, and that the last item is a NULL
  666. * entry */
  667. if (phdr->p_memsz == 0 ||
  668. phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
  669. goto dynamic_error;
  670. tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
  671. if (((Elf32_Dyn *)
  672. params->dynamic_addr)[tmp - 1].d_tag != 0)
  673. goto dynamic_error;
  674. break;
  675. }
  676. }
  677. break;
  678. }
  679. /* now elide adjacent segments in the load map on MMU linux
  680. * - on uClinux the holes between may actually be filled with system
  681. * stuff or stuff from other processes
  682. */
  683. #ifdef CONFIG_MMU
  684. nloads = loadmap->nsegs;
  685. mseg = loadmap->segs;
  686. seg = mseg + 1;
  687. for (loop = 1; loop < nloads; loop++) {
  688. /* see if we have a candidate for merging */
  689. if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
  690. load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
  691. if (load_addr == (seg->addr & PAGE_MASK)) {
  692. mseg->p_memsz +=
  693. load_addr -
  694. (mseg->addr + mseg->p_memsz);
  695. mseg->p_memsz += seg->addr & ~PAGE_MASK;
  696. mseg->p_memsz += seg->p_memsz;
  697. loadmap->nsegs--;
  698. continue;
  699. }
  700. }
  701. mseg++;
  702. if (mseg != seg)
  703. *mseg = *seg;
  704. }
  705. #endif
  706. kdebug("Mapped Object [%s]:", what);
  707. kdebug("- elfhdr : %lx", params->elfhdr_addr);
  708. kdebug("- entry : %lx", params->entry_addr);
  709. kdebug("- PHDR[] : %lx", params->ph_addr);
  710. kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
  711. seg = loadmap->segs;
  712. for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
  713. kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
  714. loop,
  715. seg->addr, seg->addr + seg->p_memsz - 1,
  716. seg->p_vaddr, seg->p_memsz);
  717. return 0;
  718. dynamic_error:
  719. printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
  720. what, file->f_dentry->d_inode->i_ino);
  721. return -ELIBBAD;
  722. }
  723. /*****************************************************************************/
  724. /*
  725. * map a file with constant displacement under uClinux
  726. */
  727. #ifndef CONFIG_MMU
  728. static int elf_fdpic_map_file_constdisp_on_uclinux(
  729. struct elf_fdpic_params *params,
  730. struct file *file,
  731. struct mm_struct *mm)
  732. {
  733. struct elf32_fdpic_loadseg *seg;
  734. struct elf32_phdr *phdr;
  735. unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
  736. loff_t fpos;
  737. int loop, ret;
  738. load_addr = params->load_addr;
  739. seg = params->loadmap->segs;
  740. /* determine the bounds of the contiguous overall allocation we must
  741. * make */
  742. phdr = params->phdrs;
  743. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  744. if (params->phdrs[loop].p_type != PT_LOAD)
  745. continue;
  746. if (base > phdr->p_vaddr)
  747. base = phdr->p_vaddr;
  748. if (top < phdr->p_vaddr + phdr->p_memsz)
  749. top = phdr->p_vaddr + phdr->p_memsz;
  750. }
  751. /* allocate one big anon block for everything */
  752. mflags = MAP_PRIVATE;
  753. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  754. mflags |= MAP_EXECUTABLE;
  755. down_write(&mm->mmap_sem);
  756. maddr = do_mmap(NULL, load_addr, top - base,
  757. PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
  758. up_write(&mm->mmap_sem);
  759. if (IS_ERR_VALUE(maddr))
  760. return (int) maddr;
  761. if (load_addr != 0)
  762. load_addr += PAGE_ALIGN(top - base);
  763. /* and then load the file segments into it */
  764. phdr = params->phdrs;
  765. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  766. if (params->phdrs[loop].p_type != PT_LOAD)
  767. continue;
  768. fpos = phdr->p_offset;
  769. seg->addr = maddr + (phdr->p_vaddr - base);
  770. seg->p_vaddr = phdr->p_vaddr;
  771. seg->p_memsz = phdr->p_memsz;
  772. ret = file->f_op->read(file, (void *) seg->addr,
  773. phdr->p_filesz, &fpos);
  774. if (ret < 0)
  775. return ret;
  776. /* map the ELF header address if in this segment */
  777. if (phdr->p_offset == 0)
  778. params->elfhdr_addr = seg->addr;
  779. /* clear any space allocated but not loaded */
  780. if (phdr->p_filesz < phdr->p_memsz)
  781. clear_user((void *) (seg->addr + phdr->p_filesz),
  782. phdr->p_memsz - phdr->p_filesz);
  783. if (mm) {
  784. if (phdr->p_flags & PF_X) {
  785. mm->start_code = seg->addr;
  786. mm->end_code = seg->addr + phdr->p_memsz;
  787. } else if (!mm->start_data) {
  788. mm->start_data = seg->addr;
  789. #ifndef CONFIG_MMU
  790. mm->end_data = seg->addr + phdr->p_memsz;
  791. #endif
  792. }
  793. #ifdef CONFIG_MMU
  794. if (seg->addr + phdr->p_memsz > mm->end_data)
  795. mm->end_data = seg->addr + phdr->p_memsz;
  796. #endif
  797. }
  798. seg++;
  799. }
  800. return 0;
  801. }
  802. #endif
  803. /*****************************************************************************/
  804. /*
  805. * map a binary by direct mmap() of the individual PT_LOAD segments
  806. */
  807. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
  808. struct file *file,
  809. struct mm_struct *mm)
  810. {
  811. struct elf32_fdpic_loadseg *seg;
  812. struct elf32_phdr *phdr;
  813. unsigned long load_addr, delta_vaddr;
  814. int loop, dvset;
  815. load_addr = params->load_addr;
  816. delta_vaddr = 0;
  817. dvset = 0;
  818. seg = params->loadmap->segs;
  819. /* deal with each load segment separately */
  820. phdr = params->phdrs;
  821. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  822. unsigned long maddr, disp, excess, excess1;
  823. int prot = 0, flags;
  824. if (phdr->p_type != PT_LOAD)
  825. continue;
  826. kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
  827. (unsigned long) phdr->p_vaddr,
  828. (unsigned long) phdr->p_offset,
  829. (unsigned long) phdr->p_filesz,
  830. (unsigned long) phdr->p_memsz);
  831. /* determine the mapping parameters */
  832. if (phdr->p_flags & PF_R) prot |= PROT_READ;
  833. if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
  834. if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
  835. flags = MAP_PRIVATE | MAP_DENYWRITE;
  836. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  837. flags |= MAP_EXECUTABLE;
  838. maddr = 0;
  839. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  840. case ELF_FDPIC_FLAG_INDEPENDENT:
  841. /* PT_LOADs are independently locatable */
  842. break;
  843. case ELF_FDPIC_FLAG_HONOURVADDR:
  844. /* the specified virtual address must be honoured */
  845. maddr = phdr->p_vaddr;
  846. flags |= MAP_FIXED;
  847. break;
  848. case ELF_FDPIC_FLAG_CONSTDISP:
  849. /* constant displacement
  850. * - can be mapped anywhere, but must be mapped as a
  851. * unit
  852. */
  853. if (!dvset) {
  854. maddr = load_addr;
  855. delta_vaddr = phdr->p_vaddr;
  856. dvset = 1;
  857. } else {
  858. maddr = load_addr + phdr->p_vaddr - delta_vaddr;
  859. flags |= MAP_FIXED;
  860. }
  861. break;
  862. case ELF_FDPIC_FLAG_CONTIGUOUS:
  863. /* contiguity handled later */
  864. break;
  865. default:
  866. BUG();
  867. }
  868. maddr &= PAGE_MASK;
  869. /* create the mapping */
  870. disp = phdr->p_vaddr & ~PAGE_MASK;
  871. down_write(&mm->mmap_sem);
  872. maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
  873. phdr->p_offset - disp);
  874. up_write(&mm->mmap_sem);
  875. kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
  876. loop, phdr->p_memsz + disp, prot, flags,
  877. phdr->p_offset - disp, maddr);
  878. if (IS_ERR_VALUE(maddr))
  879. return (int) maddr;
  880. if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
  881. ELF_FDPIC_FLAG_CONTIGUOUS)
  882. load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
  883. seg->addr = maddr + disp;
  884. seg->p_vaddr = phdr->p_vaddr;
  885. seg->p_memsz = phdr->p_memsz;
  886. /* map the ELF header address if in this segment */
  887. if (phdr->p_offset == 0)
  888. params->elfhdr_addr = seg->addr;
  889. /* clear the bit between beginning of mapping and beginning of
  890. * PT_LOAD */
  891. if (prot & PROT_WRITE && disp > 0) {
  892. kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
  893. clear_user((void __user *) maddr, disp);
  894. maddr += disp;
  895. }
  896. /* clear any space allocated but not loaded
  897. * - on uClinux we can just clear the lot
  898. * - on MMU linux we'll get a SIGBUS beyond the last page
  899. * extant in the file
  900. */
  901. excess = phdr->p_memsz - phdr->p_filesz;
  902. excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
  903. #ifdef CONFIG_MMU
  904. if (excess > excess1) {
  905. unsigned long xaddr = maddr + phdr->p_filesz + excess1;
  906. unsigned long xmaddr;
  907. flags |= MAP_FIXED | MAP_ANONYMOUS;
  908. down_write(&mm->mmap_sem);
  909. xmaddr = do_mmap(NULL, xaddr, excess - excess1,
  910. prot, flags, 0);
  911. up_write(&mm->mmap_sem);
  912. kdebug("mmap[%d] <anon>"
  913. " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
  914. loop, xaddr, excess - excess1, prot, flags,
  915. xmaddr);
  916. if (xmaddr != xaddr)
  917. return -ENOMEM;
  918. }
  919. if (prot & PROT_WRITE && excess1 > 0) {
  920. kdebug("clear[%d] ad=%lx sz=%lx",
  921. loop, maddr + phdr->p_filesz, excess1);
  922. clear_user((void __user *) maddr + phdr->p_filesz,
  923. excess1);
  924. }
  925. #else
  926. if (excess > 0) {
  927. kdebug("clear[%d] ad=%lx sz=%lx",
  928. loop, maddr + phdr->p_filesz, excess);
  929. clear_user((void *) maddr + phdr->p_filesz, excess);
  930. }
  931. #endif
  932. if (mm) {
  933. if (phdr->p_flags & PF_X) {
  934. mm->start_code = maddr;
  935. mm->end_code = maddr + phdr->p_memsz;
  936. } else if (!mm->start_data) {
  937. mm->start_data = maddr;
  938. mm->end_data = maddr + phdr->p_memsz;
  939. }
  940. }
  941. seg++;
  942. }
  943. return 0;
  944. }