binfmt_elf_fdpic.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. BUG_ON(csp != sp);
  480. /* fill in the argv[] array */
  481. #ifdef CONFIG_MMU
  482. current->mm->arg_start = bprm->p;
  483. #else
  484. current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
  485. #endif
  486. p = (char *) current->mm->arg_start;
  487. for (loop = bprm->argc; loop > 0; loop--) {
  488. __put_user((elf_caddr_t) p, argv++);
  489. len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
  490. if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
  491. return -EINVAL;
  492. p += len;
  493. }
  494. __put_user(NULL, argv);
  495. current->mm->arg_end = (unsigned long) p;
  496. /* fill in the envv[] array */
  497. current->mm->env_start = (unsigned long) p;
  498. for (loop = bprm->envc; loop > 0; loop--) {
  499. __put_user((elf_caddr_t)(unsigned long) p, envp++);
  500. len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
  501. if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
  502. return -EINVAL;
  503. p += len;
  504. }
  505. __put_user(NULL, envp);
  506. current->mm->env_end = (unsigned long) p;
  507. mm->start_stack = (unsigned long) sp;
  508. return 0;
  509. } /* end create_elf_fdpic_tables() */
  510. /*****************************************************************************/
  511. /*
  512. * transfer the program arguments and environment from the holding pages onto
  513. * the stack
  514. */
  515. #ifndef CONFIG_MMU
  516. static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp)
  517. {
  518. unsigned long index, stop, sp;
  519. char *src;
  520. int ret = 0;
  521. stop = bprm->p >> PAGE_SHIFT;
  522. sp = *_sp;
  523. for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
  524. src = kmap(bprm->page[index]);
  525. sp -= PAGE_SIZE;
  526. if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
  527. ret = -EFAULT;
  528. kunmap(bprm->page[index]);
  529. if (ret < 0)
  530. goto out;
  531. }
  532. *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
  533. out:
  534. return ret;
  535. } /* end elf_fdpic_transfer_args_to_stack() */
  536. #endif
  537. /*****************************************************************************/
  538. /*
  539. * load the appropriate binary image (executable or interpreter) into memory
  540. * - we assume no MMU is available
  541. * - if no other PIC bits are set in params->hdr->e_flags
  542. * - we assume that the LOADable segments in the binary are independently relocatable
  543. * - we assume R/O executable segments are shareable
  544. * - else
  545. * - we assume the loadable parts of the image to require fixed displacement
  546. * - the image is not shareable
  547. */
  548. static int elf_fdpic_map_file(struct elf_fdpic_params *params,
  549. struct file *file,
  550. struct mm_struct *mm,
  551. const char *what)
  552. {
  553. struct elf32_fdpic_loadmap *loadmap;
  554. #ifdef CONFIG_MMU
  555. struct elf32_fdpic_loadseg *mseg;
  556. #endif
  557. struct elf32_fdpic_loadseg *seg;
  558. struct elf32_phdr *phdr;
  559. unsigned long load_addr, stop;
  560. unsigned nloads, tmp;
  561. size_t size;
  562. int loop, ret;
  563. /* allocate a load map table */
  564. nloads = 0;
  565. for (loop = 0; loop < params->hdr.e_phnum; loop++)
  566. if (params->phdrs[loop].p_type == PT_LOAD)
  567. nloads++;
  568. if (nloads == 0)
  569. return -ELIBBAD;
  570. size = sizeof(*loadmap) + nloads * sizeof(*seg);
  571. loadmap = kmalloc(size, GFP_KERNEL);
  572. if (!loadmap)
  573. return -ENOMEM;
  574. params->loadmap = loadmap;
  575. memset(loadmap, 0, size);
  576. loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
  577. loadmap->nsegs = nloads;
  578. load_addr = params->load_addr;
  579. seg = loadmap->segs;
  580. /* map the requested LOADs into the memory space */
  581. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  582. case ELF_FDPIC_FLAG_CONSTDISP:
  583. case ELF_FDPIC_FLAG_CONTIGUOUS:
  584. #ifndef CONFIG_MMU
  585. ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
  586. if (ret < 0)
  587. return ret;
  588. break;
  589. #endif
  590. default:
  591. ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
  592. if (ret < 0)
  593. return ret;
  594. break;
  595. }
  596. /* map the entry point */
  597. if (params->hdr.e_entry) {
  598. seg = loadmap->segs;
  599. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  600. if (params->hdr.e_entry >= seg->p_vaddr &&
  601. params->hdr.e_entry < seg->p_vaddr + seg->p_memsz
  602. ) {
  603. params->entry_addr =
  604. (params->hdr.e_entry - seg->p_vaddr) + seg->addr;
  605. break;
  606. }
  607. }
  608. }
  609. /* determine where the program header table has wound up if mapped */
  610. stop = params->hdr.e_phoff + params->hdr.e_phnum * sizeof (struct elf_phdr);
  611. phdr = params->phdrs;
  612. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  613. if (phdr->p_type != PT_LOAD)
  614. continue;
  615. if (phdr->p_offset > params->hdr.e_phoff ||
  616. phdr->p_offset + phdr->p_filesz < stop)
  617. continue;
  618. seg = loadmap->segs;
  619. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  620. if (phdr->p_vaddr >= seg->p_vaddr &&
  621. phdr->p_vaddr + phdr->p_filesz <= seg->p_vaddr + seg->p_memsz
  622. ) {
  623. params->ph_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr +
  624. params->hdr.e_phoff - phdr->p_offset;
  625. break;
  626. }
  627. }
  628. break;
  629. }
  630. /* determine where the dynamic section has wound up if there is one */
  631. phdr = params->phdrs;
  632. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  633. if (phdr->p_type != PT_DYNAMIC)
  634. continue;
  635. seg = loadmap->segs;
  636. for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
  637. if (phdr->p_vaddr >= seg->p_vaddr &&
  638. phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz
  639. ) {
  640. params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr;
  641. /* check the dynamic section contains at least one item, and that
  642. * the last item is a NULL entry */
  643. if (phdr->p_memsz == 0 ||
  644. phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
  645. goto dynamic_error;
  646. tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
  647. if (((Elf32_Dyn *) params->dynamic_addr)[tmp - 1].d_tag != 0)
  648. goto dynamic_error;
  649. break;
  650. }
  651. }
  652. break;
  653. }
  654. /* now elide adjacent segments in the load map on MMU linux
  655. * - on uClinux the holes between may actually be filled with system stuff or stuff from
  656. * other processes
  657. */
  658. #ifdef CONFIG_MMU
  659. nloads = loadmap->nsegs;
  660. mseg = loadmap->segs;
  661. seg = mseg + 1;
  662. for (loop = 1; loop < nloads; loop++) {
  663. /* see if we have a candidate for merging */
  664. if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
  665. load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
  666. if (load_addr == (seg->addr & PAGE_MASK)) {
  667. mseg->p_memsz += load_addr - (mseg->addr + mseg->p_memsz);
  668. mseg->p_memsz += seg->addr & ~PAGE_MASK;
  669. mseg->p_memsz += seg->p_memsz;
  670. loadmap->nsegs--;
  671. continue;
  672. }
  673. }
  674. mseg++;
  675. if (mseg != seg)
  676. *mseg = *seg;
  677. }
  678. #endif
  679. kdebug("Mapped Object [%s]:", what);
  680. kdebug("- elfhdr : %lx", params->elfhdr_addr);
  681. kdebug("- entry : %lx", params->entry_addr);
  682. kdebug("- PHDR[] : %lx", params->ph_addr);
  683. kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
  684. seg = loadmap->segs;
  685. for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
  686. kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
  687. loop,
  688. seg->addr, seg->addr + seg->p_memsz - 1,
  689. seg->p_vaddr, seg->p_memsz);
  690. return 0;
  691. dynamic_error:
  692. printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
  693. what, file->f_dentry->d_inode->i_ino);
  694. return -ELIBBAD;
  695. } /* end elf_fdpic_map_file() */
  696. /*****************************************************************************/
  697. /*
  698. * map a file with constant displacement under uClinux
  699. */
  700. #ifndef CONFIG_MMU
  701. static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
  702. struct file *file,
  703. struct mm_struct *mm)
  704. {
  705. struct elf32_fdpic_loadseg *seg;
  706. struct elf32_phdr *phdr;
  707. unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
  708. loff_t fpos;
  709. int loop, ret;
  710. load_addr = params->load_addr;
  711. seg = params->loadmap->segs;
  712. /* determine the bounds of the contiguous overall allocation we must make */
  713. phdr = params->phdrs;
  714. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  715. if (params->phdrs[loop].p_type != PT_LOAD)
  716. continue;
  717. if (base > phdr->p_vaddr)
  718. base = phdr->p_vaddr;
  719. if (top < phdr->p_vaddr + phdr->p_memsz)
  720. top = phdr->p_vaddr + phdr->p_memsz;
  721. }
  722. /* allocate one big anon block for everything */
  723. mflags = MAP_PRIVATE;
  724. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  725. mflags |= MAP_EXECUTABLE;
  726. down_write(&mm->mmap_sem);
  727. maddr = do_mmap(NULL, load_addr, top - base,
  728. PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
  729. up_write(&mm->mmap_sem);
  730. if (IS_ERR((void *) maddr))
  731. return (int) maddr;
  732. if (load_addr != 0)
  733. load_addr += PAGE_ALIGN(top - base);
  734. /* and then load the file segments into it */
  735. phdr = params->phdrs;
  736. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  737. if (params->phdrs[loop].p_type != PT_LOAD)
  738. continue;
  739. fpos = phdr->p_offset;
  740. seg->addr = maddr + (phdr->p_vaddr - base);
  741. seg->p_vaddr = phdr->p_vaddr;
  742. seg->p_memsz = phdr->p_memsz;
  743. ret = file->f_op->read(file, (void *) seg->addr, phdr->p_filesz, &fpos);
  744. if (ret < 0)
  745. return ret;
  746. /* map the ELF header address if in this segment */
  747. if (phdr->p_offset == 0)
  748. params->elfhdr_addr = seg->addr;
  749. /* clear any space allocated but not loaded */
  750. if (phdr->p_filesz < phdr->p_memsz)
  751. clear_user((void *) (seg->addr + phdr->p_filesz),
  752. phdr->p_memsz - phdr->p_filesz);
  753. if (mm) {
  754. if (phdr->p_flags & PF_X) {
  755. mm->start_code = seg->addr;
  756. mm->end_code = seg->addr + phdr->p_memsz;
  757. }
  758. else if (!mm->start_data) {
  759. mm->start_data = seg->addr;
  760. #ifndef CONFIG_MMU
  761. mm->end_data = seg->addr + phdr->p_memsz;
  762. #endif
  763. }
  764. #ifdef CONFIG_MMU
  765. if (seg->addr + phdr->p_memsz > mm->end_data)
  766. mm->end_data = seg->addr + phdr->p_memsz;
  767. #endif
  768. }
  769. seg++;
  770. }
  771. return 0;
  772. } /* end elf_fdpic_map_file_constdisp_on_uclinux() */
  773. #endif
  774. /*****************************************************************************/
  775. /*
  776. * map a binary by direct mmap() of the individual PT_LOAD segments
  777. */
  778. static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
  779. struct file *file,
  780. struct mm_struct *mm)
  781. {
  782. struct elf32_fdpic_loadseg *seg;
  783. struct elf32_phdr *phdr;
  784. unsigned long load_addr, delta_vaddr;
  785. int loop, dvset;
  786. load_addr = params->load_addr;
  787. delta_vaddr = 0;
  788. dvset = 0;
  789. seg = params->loadmap->segs;
  790. /* deal with each load segment separately */
  791. phdr = params->phdrs;
  792. for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
  793. unsigned long maddr, disp, excess, excess1;
  794. int prot = 0, flags;
  795. if (phdr->p_type != PT_LOAD)
  796. continue;
  797. kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
  798. (unsigned long) phdr->p_vaddr,
  799. (unsigned long) phdr->p_offset,
  800. (unsigned long) phdr->p_filesz,
  801. (unsigned long) phdr->p_memsz);
  802. /* determine the mapping parameters */
  803. if (phdr->p_flags & PF_R) prot |= PROT_READ;
  804. if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
  805. if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
  806. flags = MAP_PRIVATE | MAP_DENYWRITE;
  807. if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
  808. flags |= MAP_EXECUTABLE;
  809. maddr = 0;
  810. switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
  811. case ELF_FDPIC_FLAG_INDEPENDENT:
  812. /* PT_LOADs are independently locatable */
  813. break;
  814. case ELF_FDPIC_FLAG_HONOURVADDR:
  815. /* the specified virtual address must be honoured */
  816. maddr = phdr->p_vaddr;
  817. flags |= MAP_FIXED;
  818. break;
  819. case ELF_FDPIC_FLAG_CONSTDISP:
  820. /* constant displacement
  821. * - can be mapped anywhere, but must be mapped as a unit
  822. */
  823. if (!dvset) {
  824. maddr = load_addr;
  825. delta_vaddr = phdr->p_vaddr;
  826. dvset = 1;
  827. }
  828. else {
  829. maddr = load_addr + phdr->p_vaddr - delta_vaddr;
  830. flags |= MAP_FIXED;
  831. }
  832. break;
  833. case ELF_FDPIC_FLAG_CONTIGUOUS:
  834. /* contiguity handled later */
  835. break;
  836. default:
  837. BUG();
  838. }
  839. maddr &= PAGE_MASK;
  840. /* create the mapping */
  841. disp = phdr->p_vaddr & ~PAGE_MASK;
  842. down_write(&mm->mmap_sem);
  843. maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
  844. phdr->p_offset - disp);
  845. up_write(&mm->mmap_sem);
  846. kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
  847. loop, phdr->p_memsz + disp, prot, flags, phdr->p_offset - disp,
  848. maddr);
  849. if (IS_ERR((void *) maddr))
  850. return (int) maddr;
  851. if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_CONTIGUOUS)
  852. load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
  853. seg->addr = maddr + disp;
  854. seg->p_vaddr = phdr->p_vaddr;
  855. seg->p_memsz = phdr->p_memsz;
  856. /* map the ELF header address if in this segment */
  857. if (phdr->p_offset == 0)
  858. params->elfhdr_addr = seg->addr;
  859. /* clear the bit between beginning of mapping and beginning of PT_LOAD */
  860. if (prot & PROT_WRITE && disp > 0) {
  861. kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
  862. clear_user((void *) maddr, disp);
  863. maddr += disp;
  864. }
  865. /* clear any space allocated but not loaded
  866. * - on uClinux we can just clear the lot
  867. * - on MMU linux we'll get a SIGBUS beyond the last page
  868. * extant in the file
  869. */
  870. excess = phdr->p_memsz - phdr->p_filesz;
  871. excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
  872. #ifdef CONFIG_MMU
  873. if (excess > excess1) {
  874. unsigned long xaddr = maddr + phdr->p_filesz + excess1;
  875. unsigned long xmaddr;
  876. flags |= MAP_FIXED | MAP_ANONYMOUS;
  877. down_write(&mm->mmap_sem);
  878. xmaddr = do_mmap(NULL, xaddr, excess - excess1, prot, flags, 0);
  879. up_write(&mm->mmap_sem);
  880. kdebug("mmap[%d] <anon>"
  881. " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
  882. loop, xaddr, excess - excess1, prot, flags, xmaddr);
  883. if (xmaddr != xaddr)
  884. return -ENOMEM;
  885. }
  886. if (prot & PROT_WRITE && excess1 > 0) {
  887. kdebug("clear[%d] ad=%lx sz=%lx",
  888. loop, maddr + phdr->p_filesz, excess1);
  889. clear_user((void *) maddr + phdr->p_filesz, excess1);
  890. }
  891. #else
  892. if (excess > 0) {
  893. kdebug("clear[%d] ad=%lx sz=%lx",
  894. loop, maddr + phdr->p_filesz, excess);
  895. clear_user((void *) maddr + phdr->p_filesz, excess);
  896. }
  897. #endif
  898. if (mm) {
  899. if (phdr->p_flags & PF_X) {
  900. mm->start_code = maddr;
  901. mm->end_code = maddr + phdr->p_memsz;
  902. }
  903. else if (!mm->start_data) {
  904. mm->start_data = maddr;
  905. mm->end_data = maddr + phdr->p_memsz;
  906. }
  907. }
  908. seg++;
  909. }
  910. return 0;
  911. } /* end elf_fdpic_map_file_by_direct_mmap() */