binfmt_elf_fdpic.c 30 KB

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