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 __user *argv, *envp;
  367. size_t platform_len = 0, len;
  368. char *k_platform;
  369. char __user *u_platform, *p;
  370. long hwcap;
  371. int loop;
  372. /* we're going to shovel a whole load of stuff onto the stack */
  373. #ifdef CONFIG_MMU
  374. sp = bprm->p;
  375. #else
  376. sp = mm->start_stack;
  377. /* stack the program arguments and environment */
  378. if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
  379. return -EFAULT;
  380. #endif
  381. /* get hold of platform and hardware capabilities masks for the machine
  382. * we are running on. In some cases (Sparc), this info is impossible
  383. * to get, in others (i386) it is merely difficult.
  384. */
  385. hwcap = ELF_HWCAP;
  386. k_platform = ELF_PLATFORM;
  387. if (k_platform) {
  388. platform_len = strlen(k_platform) + 1;
  389. sp -= platform_len;
  390. u_platform = (char __user *) sp;
  391. if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
  392. return -EFAULT;
  393. }
  394. #if defined(__i386__) && defined(CONFIG_SMP)
  395. /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
  396. * by the processes running on the same package. One thing we can do
  397. * is to shuffle the initial stack for them.
  398. *
  399. * the conditionals here are unneeded, but kept in to make the
  400. * code behaviour the same as pre change unless we have hyperthreaded
  401. * processors. This keeps Mr Marcelo Person happier but should be
  402. * removed for 2.5
  403. */
  404. if (smp_num_siblings > 1)
  405. sp = sp - ((current->pid % 64) << 7);
  406. #endif
  407. sp &= ~7UL;
  408. /* stack the load map(s) */
  409. len = sizeof(struct elf32_fdpic_loadmap);
  410. len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
  411. sp = (sp - len) & ~7UL;
  412. exec_params->map_addr = sp;
  413. if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
  414. return -EFAULT;
  415. current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
  416. if (interp_params->loadmap) {
  417. len = sizeof(struct elf32_fdpic_loadmap);
  418. len += sizeof(struct elf32_fdpic_loadseg) * interp_params->loadmap->nsegs;
  419. sp = (sp - len) & ~7UL;
  420. interp_params->map_addr = sp;
  421. if (copy_to_user((void __user *) sp, interp_params->loadmap, len) != 0)
  422. return -EFAULT;
  423. current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
  424. }
  425. /* force 16 byte _final_ alignment here for generality */
  426. #define DLINFO_ITEMS 13
  427. nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
  428. #ifdef DLINFO_ARCH_ITEMS
  429. nitems += DLINFO_ARCH_ITEMS;
  430. #endif
  431. csp = sp;
  432. sp -= nitems * 2 * sizeof(unsigned long);
  433. sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
  434. sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
  435. sp -= 1 * sizeof(unsigned long); /* argc */
  436. csp -= sp & 15UL;
  437. sp -= sp & 15UL;
  438. /* put the ELF interpreter info on the stack */
  439. #define NEW_AUX_ENT(nr, id, val) \
  440. do { \
  441. struct { unsigned long _id, _val; } __user *ent = (void __user *) csp; \
  442. __put_user((id), &ent[nr]._id); \
  443. __put_user((val), &ent[nr]._val); \
  444. } while (0)
  445. csp -= 2 * sizeof(unsigned long);
  446. NEW_AUX_ENT(0, AT_NULL, 0);
  447. if (k_platform) {
  448. csp -= 2 * sizeof(unsigned long);
  449. NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
  450. }
  451. csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
  452. NEW_AUX_ENT( 0, AT_HWCAP, hwcap);
  453. NEW_AUX_ENT( 1, AT_PAGESZ, PAGE_SIZE);
  454. NEW_AUX_ENT( 2, AT_CLKTCK, CLOCKS_PER_SEC);
  455. NEW_AUX_ENT( 3, AT_PHDR, exec_params->ph_addr);
  456. NEW_AUX_ENT( 4, AT_PHENT, sizeof(struct elf_phdr));
  457. NEW_AUX_ENT( 5, AT_PHNUM, exec_params->hdr.e_phnum);
  458. NEW_AUX_ENT( 6, AT_BASE, interp_params->elfhdr_addr);
  459. NEW_AUX_ENT( 7, AT_FLAGS, 0);
  460. NEW_AUX_ENT( 8, AT_ENTRY, exec_params->entry_addr);
  461. NEW_AUX_ENT( 9, AT_UID, (elf_addr_t) current->uid);
  462. NEW_AUX_ENT(10, AT_EUID, (elf_addr_t) current->euid);
  463. NEW_AUX_ENT(11, AT_GID, (elf_addr_t) current->gid);
  464. NEW_AUX_ENT(12, AT_EGID, (elf_addr_t) current->egid);
  465. #ifdef ARCH_DLINFO
  466. /* ARCH_DLINFO must come last so platform specific code can enforce
  467. * special alignment requirements on the AUXV if necessary (eg. PPC).
  468. */
  469. ARCH_DLINFO;
  470. #endif
  471. #undef NEW_AUX_ENT
  472. /* allocate room for argv[] and envv[] */
  473. csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
  474. envp = (elf_caddr_t __user *) csp;
  475. csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
  476. argv = (elf_caddr_t __user *) csp;
  477. /* stack argc */
  478. csp -= sizeof(unsigned long);
  479. __put_user(bprm->argc, (unsigned long __user *) csp);
  480. BUG_ON(csp != sp);
  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 __user *) 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 __user *) 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 __user *) 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() */