binfmt_elf_fdpic.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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 = 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. retval = setup_arg_pages(bprm, current->mm->start_stack, executable_stack);
  247. if (retval < 0) {
  248. send_sig(SIGKILL, current, 0);
  249. goto error_kill;
  250. }
  251. #endif
  252. /* load the executable and interpreter into memory */
  253. retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm, "executable");
  254. if (retval < 0)
  255. goto error_kill;
  256. if (interpreter_name) {
  257. retval = elf_fdpic_map_file(&interp_params, interpreter,
  258. current->mm, "interpreter");
  259. if (retval < 0) {
  260. printk(KERN_ERR "Unable to load interpreter\n");
  261. goto error_kill;
  262. }
  263. allow_write_access(interpreter);
  264. fput(interpreter);
  265. interpreter = NULL;
  266. }
  267. #ifdef CONFIG_MMU
  268. if (!current->mm->start_brk)
  269. current->mm->start_brk = current->mm->end_data;
  270. current->mm->brk = current->mm->start_brk = PAGE_ALIGN(current->mm->start_brk);
  271. #else
  272. /* create a stack and brk area big enough for everyone
  273. * - the brk heap starts at the bottom and works up
  274. * - the stack starts at the top and works down
  275. */
  276. stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
  277. if (stack_size < PAGE_SIZE * 2)
  278. stack_size = PAGE_SIZE * 2;
  279. down_write(&current->mm->mmap_sem);
  280. current->mm->start_brk = do_mmap(NULL,
  281. 0,
  282. stack_size,
  283. PROT_READ | PROT_WRITE | PROT_EXEC,
  284. MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
  285. 0);
  286. if (IS_ERR((void *) current->mm->start_brk)) {
  287. up_write(&current->mm->mmap_sem);
  288. retval = current->mm->start_brk;
  289. current->mm->start_brk = 0;
  290. goto error_kill;
  291. }
  292. if (do_mremap(current->mm->start_brk,
  293. stack_size,
  294. ksize((char *) current->mm->start_brk),
  295. 0, 0
  296. ) == current->mm->start_brk
  297. )
  298. stack_size = ksize((char *) current->mm->start_brk);
  299. up_write(&current->mm->mmap_sem);
  300. current->mm->brk = current->mm->start_brk;
  301. current->mm->context.end_brk = current->mm->start_brk;
  302. current->mm->context.end_brk += (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
  303. current->mm->start_stack = current->mm->start_brk + stack_size;
  304. #endif
  305. compute_creds(bprm);
  306. current->flags &= ~PF_FORKNOEXEC;
  307. if (create_elf_fdpic_tables(bprm, current->mm, &exec_params, &interp_params) < 0)
  308. goto error_kill;
  309. kdebug("- start_code %lx", (long) current->mm->start_code);
  310. kdebug("- end_code %lx", (long) current->mm->end_code);
  311. kdebug("- start_data %lx", (long) current->mm->start_data);
  312. kdebug("- end_data %lx", (long) current->mm->end_data);
  313. kdebug("- start_brk %lx", (long) current->mm->start_brk);
  314. kdebug("- brk %lx", (long) current->mm->brk);
  315. kdebug("- start_stack %lx", (long) current->mm->start_stack);
  316. #ifdef ELF_FDPIC_PLAT_INIT
  317. /*
  318. * The ABI may specify that certain registers be set up in special
  319. * ways (on i386 %edx is the address of a DT_FINI function, for
  320. * example. This macro performs whatever initialization to
  321. * the regs structure is required.
  322. */
  323. ELF_FDPIC_PLAT_INIT(regs,
  324. exec_params.map_addr,
  325. interp_params.map_addr,
  326. interp_params.dynamic_addr ?: exec_params.dynamic_addr
  327. );
  328. #endif
  329. /* everything is now ready... get the userspace context ready to roll */
  330. start_thread(regs,
  331. interp_params.entry_addr ?: exec_params.entry_addr,
  332. current->mm->start_stack);
  333. if (unlikely(current->ptrace & PT_PTRACED)) {
  334. if (current->ptrace & PT_TRACE_EXEC)
  335. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  336. else
  337. send_sig(SIGTRAP, current, 0);
  338. }
  339. retval = 0;
  340. error:
  341. if (interpreter) {
  342. allow_write_access(interpreter);
  343. fput(interpreter);
  344. }
  345. kfree(interpreter_name);
  346. kfree(exec_params.phdrs);
  347. kfree(exec_params.loadmap);
  348. kfree(interp_params.phdrs);
  349. kfree(interp_params.loadmap);
  350. return retval;
  351. /* unrecoverable error - kill the process */
  352. error_kill:
  353. send_sig(SIGSEGV, current, 0);
  354. goto error;
  355. } /* end load_elf_fdpic_binary() */
  356. /*****************************************************************************/
  357. /*
  358. * present useful information to the program
  359. */
  360. static int create_elf_fdpic_tables(struct linux_binprm *bprm,
  361. struct mm_struct *mm,
  362. struct elf_fdpic_params *exec_params,
  363. struct elf_fdpic_params *interp_params)
  364. {
  365. unsigned long sp, csp, nitems;
  366. elf_caddr_t *argv, *envp;
  367. size_t platform_len = 0, len;
  368. char *k_platform, *u_platform, *p;
  369. long hwcap;
  370. int loop;
  371. /* we're going to shovel a whole load of stuff onto the stack */
  372. #ifdef CONFIG_MMU
  373. sp = bprm->p;
  374. #else
  375. sp = mm->start_stack;
  376. /* stack the program arguments and environment */
  377. if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
  378. return -EFAULT;
  379. #endif
  380. /* get hold of platform and hardware capabilities masks for the machine
  381. * we are running on. In some cases (Sparc), this info is impossible
  382. * to get, in others (i386) it is merely difficult.
  383. */
  384. hwcap = ELF_HWCAP;
  385. k_platform = ELF_PLATFORM;
  386. if (k_platform) {
  387. platform_len = strlen(k_platform) + 1;
  388. sp -= platform_len;
  389. if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
  390. return -EFAULT;
  391. }
  392. u_platform = (char *) sp;
  393. #if defined(__i386__) && defined(CONFIG_SMP)
  394. /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  395. * by the processes running on the same package. One thing we can do
  396. * is to shuffle the initial stack for them.
  397. *
  398. * the conditionals here are unneeded, but kept in to make the
  399. * code behaviour the same as pre change unless we have hyperthreaded
  400. * processors. This keeps Mr Marcelo Person happier but should be
  401. * removed for 2.5
  402. */
  403. if (smp_num_siblings > 1)
  404. sp = sp - ((current->pid % 64) << 7);
  405. #endif
  406. sp &= ~7UL;
  407. /* stack the load map(s) */
  408. len = sizeof(struct elf32_fdpic_loadmap);
  409. len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
  410. sp = (sp - len) & ~7UL;
  411. exec_params->map_addr = sp;
  412. if (copy_to_user((void *) sp, exec_params->loadmap, len) != 0)
  413. return -EFAULT;
  414. current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
  415. if (interp_params->loadmap) {
  416. len = sizeof(struct elf32_fdpic_loadmap);
  417. len += sizeof(struct elf32_fdpic_loadseg) * interp_params->loadmap->nsegs;
  418. sp = (sp - len) & ~7UL;
  419. interp_params->map_addr = sp;
  420. if (copy_to_user((void *) sp, interp_params->loadmap, len) != 0)
  421. return -EFAULT;
  422. current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
  423. }
  424. /* force 16 byte _final_ alignment here for generality */
  425. #define DLINFO_ITEMS 13
  426. nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
  427. #ifdef DLINFO_ARCH_ITEMS
  428. nitems += DLINFO_ARCH_ITEMS;
  429. #endif
  430. csp = sp;
  431. sp -= nitems * 2 * sizeof(unsigned long);
  432. sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
  433. sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
  434. sp -= 1 * sizeof(unsigned long); /* argc */
  435. csp -= sp & 15UL;
  436. sp -= sp & 15UL;
  437. /* put the ELF interpreter info on the stack */
  438. #define NEW_AUX_ENT(nr, id, val) \
  439. do { \
  440. struct { unsigned long _id, _val; } *ent = (void *) csp; \
  441. __put_user((id), &ent[nr]._id); \
  442. __put_user((val), &ent[nr]._val); \
  443. } while (0)
  444. csp -= 2 * sizeof(unsigned long);
  445. NEW_AUX_ENT(0, AT_NULL, 0);
  446. if (k_platform) {
  447. csp -= 2 * sizeof(unsigned long);
  448. NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
  449. }
  450. csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
  451. NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
  452. NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE);
  453. NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
  454. NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr);
  455. NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr));
  456. NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum);
  457. NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr);
  458. NEW_AUX_ENT( 7, AT_FLAGS, 0);
  459. NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr);
  460. NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
  461. NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
  462. NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
  463. NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
  464. #ifdef ARCH_DLINFO
  465. /* ARCH_DLINFO must come last so platform specific code can enforce
  466. * special alignment requirements on the AUXV if necessary (eg. PPC).
  467. */
  468. ARCH_DLINFO;
  469. #endif
  470. #undef NEW_AUX_ENT
  471. /* allocate room for argv[] and envv[] */
  472. csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
  473. envp = (elf_caddr_t *) csp;
  474. csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
  475. argv = (elf_caddr_t *) csp;
  476. /* stack argc */
  477. csp -= sizeof(unsigned long);
  478. __put_user(bprm->argc, (unsigned long *) csp);
  479. if (csp != sp)
  480. BUG();
  481. /* fill in the argv[] array */
  482. #ifdef CONFIG_MMU
  483. current->mm->arg_start = bprm->p;
  484. #else
  485. current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
  486. #endif
  487. p = (char *) current->mm->arg_start;
  488. for (loop = bprm->argc; loop > 0; loop--) {
  489. __put_user((elf_caddr_t) p, argv++);
  490. len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
  491. if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
  492. return -EINVAL;
  493. p += len;
  494. }
  495. __put_user(NULL, argv);
  496. current->mm->arg_end = (unsigned long) p;
  497. /* fill in the envv[] array */
  498. current->mm->env_start = (unsigned long) p;
  499. for (loop = bprm->envc; loop > 0; loop--) {
  500. __put_user((elf_caddr_t)(unsigned long) p, envp++);
  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, envp);
  507. current->mm->env_end = (unsigned long) p;
  508. mm->start_stack = (unsigned long) sp;
  509. return 0;
  510. } /* end create_elf_fdpic_tables() */
  511. /*****************************************************************************/
  512. /*
  513. * transfer the program arguments and environment from the holding pages onto
  514. * the stack
  515. */
  516. #ifndef CONFIG_MMU
  517. static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp)
  518. {
  519. unsigned long index, stop, sp;
  520. char *src;
  521. int ret = 0;
  522. stop = bprm->p >> PAGE_SHIFT;
  523. sp = *_sp;
  524. for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
  525. src = kmap(bprm->page[index]);
  526. sp -= PAGE_SIZE;
  527. if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
  528. ret = -EFAULT;
  529. kunmap(bprm->page[index]);
  530. if (ret < 0)
  531. goto out;
  532. }
  533. *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
  534. out:
  535. return ret;
  536. } /* end elf_fdpic_transfer_args_to_stack() */
  537. #endif
  538. /*****************************************************************************/
  539. /*
  540. * load the appropriate binary image (executable or interpreter) into memory
  541. * - we assume no MMU is available
  542. * - if no other PIC bits are set in params->hdr->e_flags
  543. * - we assume that the LOADable segments in the binary are independently relocatable
  544. * - we assume R/O executable segments are shareable
  545. * - else
  546. * - we assume the loadable parts of the image to require fixed displacement
  547. * - the image is not shareable
  548. */
  549. static int elf_fdpic_map_file(struct elf_fdpic_params *params,
  550. struct file *file,
  551. struct mm_struct *mm,
  552. const char *what)
  553. {
  554. struct elf32_fdpic_loadmap *loadmap;
  555. #ifdef CONFIG_MMU
  556. struct elf32_fdpic_loadseg *mseg;
  557. #endif
  558. struct elf32_fdpic_loadseg *seg;
  559. struct elf32_phdr *phdr;
  560. unsigned long load_addr, stop;
  561. unsigned nloads, tmp;
  562. size_t size;
  563. int loop, ret;
  564. /* allocate a load map table */
  565. nloads = 0;
  566. for (loop = 0; loop < params->hdr.e_phnum; loop++)
  567. if (params->phdrs[loop].p_type == PT_LOAD)
  568. nloads++;
  569. if (nloads == 0)
  570. return -ELIBBAD;
  571. size = sizeof(*loadmap) + nloads * sizeof(*seg);
  572. loadmap = kmalloc(size, GFP_KERNEL);
  573. if (!loadmap)
  574. return -ENOMEM;
  575. params->loadmap = loadmap;
  576. memset(loadmap, 0, size);
  577. loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
  578. loadmap->nsegs = nloads;
  579. load_addr = params->load_addr;
  580. seg = loadmap->segs;
  581. /* map the requested LOADs into the memory space */
  582. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  583. case ELF_FDPIC_FLAG_CONSTDISP:
  584. case ELF_FDPIC_FLAG_CONTIGUOUS:
  585. #ifndef CONFIG_MMU
  586. ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
  587. if (ret < 0)
  588. return ret;
  589. break;
  590. #endif
  591. default:
  592. ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
  593. if (ret < 0)
  594. return ret;
  595. break;
  596. }
  597. /* map the entry point */
  598. if (params->hdr.e_entry) {
  599. seg = loadmap->segs;
  600. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  601. if (params->hdr.e_entry >= seg->p_vaddr &&
  602. params->hdr.e_entry < seg->p_vaddr + seg->p_memsz
  603. ) {
  604. params->entry_addr =
  605. (params->hdr.e_entry - seg->p_vaddr) + seg->addr;
  606. break;
  607. }
  608. }
  609. }
  610. /* determine where the program header table has wound up if mapped */
  611. stop = params->hdr.e_phoff + params->hdr.e_phnum * sizeof (struct elf_phdr);
  612. phdr = params->phdrs;
  613. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  614. if (phdr->p_type != PT_LOAD)
  615. continue;
  616. if (phdr->p_offset > params->hdr.e_phoff ||
  617. phdr->p_offset + phdr->p_filesz < stop)
  618. continue;
  619. seg = loadmap->segs;
  620. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  621. if (phdr->p_vaddr >= seg->p_vaddr &&
  622. phdr->p_vaddr + phdr->p_filesz <= seg->p_vaddr + seg->p_memsz
  623. ) {
  624. params->ph_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr +
  625. params->hdr.e_phoff - phdr->p_offset;
  626. break;
  627. }
  628. }
  629. break;
  630. }
  631. /* determine where the dynamic section has wound up if there is one */
  632. phdr = params->phdrs;
  633. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  634. if (phdr->p_type != PT_DYNAMIC)
  635. continue;
  636. seg = loadmap->segs;
  637. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  638. if (phdr->p_vaddr >= seg->p_vaddr &&
  639. phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz
  640. ) {
  641. params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr;
  642. /* check the dynamic section contains at least one item, and that
  643. * the last item is a NULL entry */
  644. if (phdr->p_memsz == 0 ||
  645. phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
  646. goto dynamic_error;
  647. tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
  648. if (((Elf32_Dyn *) params->dynamic_addr)[tmp - 1].d_tag != 0)
  649. goto dynamic_error;
  650. break;
  651. }
  652. }
  653. break;
  654. }
  655. /* now elide adjacent segments in the load map on MMU linux
  656. * - on uClinux the holes between may actually be filled with system stuff or stuff from
  657. * other processes
  658. */
  659. #ifdef CONFIG_MMU
  660. nloads = loadmap->nsegs;
  661. mseg = loadmap->segs;
  662. seg = mseg + 1;
  663. for (loop = 1; loop < nloads; loop++) {
  664. /* see if we have a candidate for merging */
  665. if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
  666. load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
  667. if (load_addr == (seg->addr & PAGE_MASK)) {
  668. mseg->p_memsz += load_addr - (mseg->addr + mseg->p_memsz);
  669. mseg->p_memsz += seg->addr & ~PAGE_MASK;
  670. mseg->p_memsz += seg->p_memsz;
  671. loadmap->nsegs--;
  672. continue;
  673. }
  674. }
  675. mseg++;
  676. if (mseg != seg)
  677. *mseg = *seg;
  678. }
  679. #endif
  680. kdebug("Mapped Object [%s]:", what);
  681. kdebug("- elfhdr : %lx", params->elfhdr_addr);
  682. kdebug("- entry : %lx", params->entry_addr);
  683. kdebug("- PHDR[] : %lx", params->ph_addr);
  684. kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
  685. seg = loadmap->segs;
  686. for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
  687. kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
  688. loop,
  689. seg->addr, seg->addr + seg->p_memsz - 1,
  690. seg->p_vaddr, seg->p_memsz);
  691. return 0;
  692. dynamic_error:
  693. printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
  694. what, file->f_dentry->d_inode->i_ino);
  695. return -ELIBBAD;
  696. } /* end elf_fdpic_map_file() */
  697. /*****************************************************************************/
  698. /*
  699. * map a file with constant displacement under uClinux
  700. */
  701. #ifndef CONFIG_MMU
  702. static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
  703. struct file *file,
  704. struct mm_struct *mm)
  705. {
  706. struct elf32_fdpic_loadseg *seg;
  707. struct elf32_phdr *phdr;
  708. unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
  709. loff_t fpos;
  710. int loop, ret;
  711. load_addr = params->load_addr;
  712. seg = params->loadmap->segs;
  713. /* determine the bounds of the contiguous overall allocation we must make */
  714. phdr = params->phdrs;
  715. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  716. if (params->phdrs[loop].p_type != PT_LOAD)
  717. continue;
  718. if (base > phdr->p_vaddr)
  719. base = phdr->p_vaddr;
  720. if (top < phdr->p_vaddr + phdr->p_memsz)
  721. top = phdr->p_vaddr + phdr->p_memsz;
  722. }
  723. /* allocate one big anon block for everything */
  724. mflags = MAP_PRIVATE;
  725. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  726. mflags |= MAP_EXECUTABLE;
  727. down_write(&mm->mmap_sem);
  728. maddr = do_mmap(NULL, load_addr, top - base,
  729. PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
  730. up_write(&mm->mmap_sem);
  731. if (IS_ERR((void *) maddr))
  732. return (int) maddr;
  733. if (load_addr != 0)
  734. load_addr += PAGE_ALIGN(top - base);
  735. /* and then load the file segments into it */
  736. phdr = params->phdrs;
  737. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  738. if (params->phdrs[loop].p_type != PT_LOAD)
  739. continue;
  740. fpos = phdr->p_offset;
  741. seg->addr = maddr + (phdr->p_vaddr - base);
  742. seg->p_vaddr = phdr->p_vaddr;
  743. seg->p_memsz = phdr->p_memsz;
  744. ret = file->f_op->read(file, (void *) seg->addr, phdr->p_filesz, &fpos);
  745. if (ret < 0)
  746. return ret;
  747. /* map the ELF header address if in this segment */
  748. if (phdr->p_offset == 0)
  749. params->elfhdr_addr = seg->addr;
  750. /* clear any space allocated but not loaded */
  751. if (phdr->p_filesz < phdr->p_memsz)
  752. clear_user((void *) (seg->addr + phdr->p_filesz),
  753. phdr->p_memsz - phdr->p_filesz);
  754. if (mm) {
  755. if (phdr->p_flags & PF_X) {
  756. mm->start_code = seg->addr;
  757. mm->end_code = seg->addr + phdr->p_memsz;
  758. }
  759. else if (!mm->start_data) {
  760. mm->start_data = seg->addr;
  761. #ifndef CONFIG_MMU
  762. mm->end_data = seg->addr + phdr->p_memsz;
  763. #endif
  764. }
  765. #ifdef CONFIG_MMU
  766. if (seg->addr + phdr->p_memsz > mm->end_data)
  767. mm->end_data = seg->addr + phdr->p_memsz;
  768. #endif
  769. }
  770. seg++;
  771. }
  772. return 0;
  773. } /* end elf_fdpic_map_file_constdisp_on_uclinux() */
  774. #endif
  775. /*****************************************************************************/
  776. /*
  777. * map a binary by direct mmap() of the individual PT_LOAD segments
  778. */
  779. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
  780. struct file *file,
  781. struct mm_struct *mm)
  782. {
  783. struct elf32_fdpic_loadseg *seg;
  784. struct elf32_phdr *phdr;
  785. unsigned long load_addr, delta_vaddr;
  786. int loop, dvset;
  787. load_addr = params->load_addr;
  788. delta_vaddr = 0;
  789. dvset = 0;
  790. seg = params->loadmap->segs;
  791. /* deal with each load segment separately */
  792. phdr = params->phdrs;
  793. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  794. unsigned long maddr, disp, excess, excess1;
  795. int prot = 0, flags;
  796. if (phdr->p_type != PT_LOAD)
  797. continue;
  798. kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
  799. (unsigned long) phdr->p_vaddr,
  800. (unsigned long) phdr->p_offset,
  801. (unsigned long) phdr->p_filesz,
  802. (unsigned long) phdr->p_memsz);
  803. /* determine the mapping parameters */
  804. if (phdr->p_flags & PF_R) prot |= PROT_READ;
  805. if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
  806. if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
  807. flags = MAP_PRIVATE | MAP_DENYWRITE;
  808. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  809. flags |= MAP_EXECUTABLE;
  810. maddr = 0;
  811. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  812. case ELF_FDPIC_FLAG_INDEPENDENT:
  813. /* PT_LOADs are independently locatable */
  814. break;
  815. case ELF_FDPIC_FLAG_HONOURVADDR:
  816. /* the specified virtual address must be honoured */
  817. maddr = phdr->p_vaddr;
  818. flags |= MAP_FIXED;
  819. break;
  820. case ELF_FDPIC_FLAG_CONSTDISP:
  821. /* constant displacement
  822. * - can be mapped anywhere, but must be mapped as a unit
  823. */
  824. if (!dvset) {
  825. maddr = load_addr;
  826. delta_vaddr = phdr->p_vaddr;
  827. dvset = 1;
  828. }
  829. else {
  830. maddr = load_addr + phdr->p_vaddr - delta_vaddr;
  831. flags |= MAP_FIXED;
  832. }
  833. break;
  834. case ELF_FDPIC_FLAG_CONTIGUOUS:
  835. /* contiguity handled later */
  836. break;
  837. default:
  838. BUG();
  839. }
  840. maddr &= PAGE_MASK;
  841. /* create the mapping */
  842. disp = phdr->p_vaddr & ~PAGE_MASK;
  843. down_write(&mm->mmap_sem);
  844. maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
  845. phdr->p_offset - disp);
  846. up_write(&mm->mmap_sem);
  847. kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
  848. loop, phdr->p_memsz + disp, prot, flags, phdr->p_offset - disp,
  849. maddr);
  850. if (IS_ERR((void *) maddr))
  851. return (int) maddr;
  852. if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_CONTIGUOUS)
  853. load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
  854. seg->addr = maddr + disp;
  855. seg->p_vaddr = phdr->p_vaddr;
  856. seg->p_memsz = phdr->p_memsz;
  857. /* map the ELF header address if in this segment */
  858. if (phdr->p_offset == 0)
  859. params->elfhdr_addr = seg->addr;
  860. /* clear the bit between beginning of mapping and beginning of PT_LOAD */
  861. if (prot & PROT_WRITE && disp > 0) {
  862. kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
  863. clear_user((void *) maddr, disp);
  864. maddr += disp;
  865. }
  866. /* clear any space allocated but not loaded
  867. * - on uClinux we can just clear the lot
  868. * - on MMU linux we'll get a SIGBUS beyond the last page
  869. * extant in the file
  870. */
  871. excess = phdr->p_memsz - phdr->p_filesz;
  872. excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
  873. #ifdef CONFIG_MMU
  874. if (excess > excess1) {
  875. unsigned long xaddr = maddr + phdr->p_filesz + excess1;
  876. unsigned long xmaddr;
  877. flags |= MAP_FIXED | MAP_ANONYMOUS;
  878. down_write(&mm->mmap_sem);
  879. xmaddr = do_mmap(NULL, xaddr, excess - excess1, prot, flags, 0);
  880. up_write(&mm->mmap_sem);
  881. kdebug("mmap[%d] <anon>"
  882. " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
  883. loop, xaddr, excess - excess1, prot, flags, xmaddr);
  884. if (xmaddr != xaddr)
  885. return -ENOMEM;
  886. }
  887. if (prot & PROT_WRITE && excess1 > 0) {
  888. kdebug("clear[%d] ad=%lx sz=%lx",
  889. loop, maddr + phdr->p_filesz, excess1);
  890. clear_user((void *) maddr + phdr->p_filesz, excess1);
  891. }
  892. #else
  893. if (excess > 0) {
  894. kdebug("clear[%d] ad=%lx sz=%lx",
  895. loop, maddr + phdr->p_filesz, excess);
  896. clear_user((void *) maddr + phdr->p_filesz, excess);
  897. }
  898. #endif
  899. if (mm) {
  900. if (phdr->p_flags & PF_X) {
  901. mm->start_code = maddr;
  902. mm->end_code = maddr + phdr->p_memsz;
  903. }
  904. else if (!mm->start_data) {
  905. mm->start_data = maddr;
  906. mm->end_data = maddr + phdr->p_memsz;
  907. }
  908. }
  909. seg++;
  910. }
  911. return 0;
  912. } /* end elf_fdpic_map_file_by_direct_mmap() */