binfmt_elf_fdpic.c 30 KB

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