binfmt_elf.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. /*
  2. * linux/fs/binfmt_elf.c
  3. *
  4. * These are the functions used to load ELF format executables as used
  5. * on SVr4 machines. Information on the format may be found in the book
  6. * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
  7. * Tools".
  8. *
  9. * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/time.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/a.out.h>
  19. #include <linux/errno.h>
  20. #include <linux/signal.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/string.h>
  23. #include <linux/file.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/slab.h>
  27. #include <linux/shm.h>
  28. #include <linux/personality.h>
  29. #include <linux/elfcore.h>
  30. #include <linux/init.h>
  31. #include <linux/highuid.h>
  32. #include <linux/smp.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/compiler.h>
  35. #include <linux/highmem.h>
  36. #include <linux/pagemap.h>
  37. #include <linux/security.h>
  38. #include <linux/syscalls.h>
  39. #include <linux/random.h>
  40. #include <linux/elf.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/param.h>
  43. #include <asm/page.h>
  44. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
  45. static int load_elf_library(struct file *);
  46. static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
  47. #ifndef elf_addr_t
  48. #define elf_addr_t unsigned long
  49. #endif
  50. /*
  51. * If we don't support core dumping, then supply a NULL so we
  52. * don't even try.
  53. */
  54. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  55. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file);
  56. #else
  57. #define elf_core_dump NULL
  58. #endif
  59. #if ELF_EXEC_PAGESIZE > PAGE_SIZE
  60. #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
  61. #else
  62. #define ELF_MIN_ALIGN PAGE_SIZE
  63. #endif
  64. #ifndef ELF_CORE_EFLAGS
  65. #define ELF_CORE_EFLAGS 0
  66. #endif
  67. #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
  68. #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
  69. #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
  70. static struct linux_binfmt elf_format = {
  71. .module = THIS_MODULE,
  72. .load_binary = load_elf_binary,
  73. .load_shlib = load_elf_library,
  74. .core_dump = elf_core_dump,
  75. .min_coredump = ELF_EXEC_PAGESIZE
  76. };
  77. #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
  78. static int set_brk(unsigned long start, unsigned long end)
  79. {
  80. start = ELF_PAGEALIGN(start);
  81. end = ELF_PAGEALIGN(end);
  82. if (end > start) {
  83. unsigned long addr;
  84. down_write(&current->mm->mmap_sem);
  85. addr = do_brk(start, end - start);
  86. up_write(&current->mm->mmap_sem);
  87. if (BAD_ADDR(addr))
  88. return addr;
  89. }
  90. current->mm->start_brk = current->mm->brk = end;
  91. return 0;
  92. }
  93. /* We need to explicitly zero any fractional pages
  94. after the data section (i.e. bss). This would
  95. contain the junk from the file that should not
  96. be in memory
  97. */
  98. static int padzero(unsigned long elf_bss)
  99. {
  100. unsigned long nbyte;
  101. nbyte = ELF_PAGEOFFSET(elf_bss);
  102. if (nbyte) {
  103. nbyte = ELF_MIN_ALIGN - nbyte;
  104. if (clear_user((void __user *) elf_bss, nbyte))
  105. return -EFAULT;
  106. }
  107. return 0;
  108. }
  109. /* Let's use some macros to make this stack manipulation a litle clearer */
  110. #ifdef CONFIG_STACK_GROWSUP
  111. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
  112. #define STACK_ROUND(sp, items) \
  113. ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
  114. #define STACK_ALLOC(sp, len) ({ \
  115. elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
  116. old_sp; })
  117. #else
  118. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
  119. #define STACK_ROUND(sp, items) \
  120. (((unsigned long) (sp - items)) &~ 15UL)
  121. #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
  122. #endif
  123. static int
  124. create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
  125. int interp_aout, unsigned long load_addr,
  126. unsigned long interp_load_addr)
  127. {
  128. unsigned long p = bprm->p;
  129. int argc = bprm->argc;
  130. int envc = bprm->envc;
  131. elf_addr_t __user *argv;
  132. elf_addr_t __user *envp;
  133. elf_addr_t __user *sp;
  134. elf_addr_t __user *u_platform;
  135. const char *k_platform = ELF_PLATFORM;
  136. int items;
  137. elf_addr_t *elf_info;
  138. int ei_index = 0;
  139. struct task_struct *tsk = current;
  140. /*
  141. * If this architecture has a platform capability string, copy it
  142. * to userspace. In some cases (Sparc), this info is impossible
  143. * for userspace to get any other way, in others (i386) it is
  144. * merely difficult.
  145. */
  146. u_platform = NULL;
  147. if (k_platform) {
  148. size_t len = strlen(k_platform) + 1;
  149. /*
  150. * In some cases (e.g. Hyper-Threading), we want to avoid L1
  151. * evictions by the processes running on the same package. One
  152. * thing we can do is to shuffle the initial stack for them.
  153. */
  154. p = arch_align_stack(p);
  155. u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
  156. if (__copy_to_user(u_platform, k_platform, len))
  157. return -EFAULT;
  158. }
  159. /* Create the ELF interpreter info */
  160. elf_info = (elf_addr_t *)current->mm->saved_auxv;
  161. #define NEW_AUX_ENT(id, val) \
  162. do { \
  163. elf_info[ei_index++] = id; \
  164. elf_info[ei_index++] = val; \
  165. } while (0)
  166. #ifdef ARCH_DLINFO
  167. /*
  168. * ARCH_DLINFO must come first so PPC can do its special alignment of
  169. * AUXV.
  170. */
  171. ARCH_DLINFO;
  172. #endif
  173. NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
  174. NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
  175. NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
  176. NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
  177. NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
  178. NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
  179. NEW_AUX_ENT(AT_BASE, interp_load_addr);
  180. NEW_AUX_ENT(AT_FLAGS, 0);
  181. NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
  182. NEW_AUX_ENT(AT_UID, tsk->uid);
  183. NEW_AUX_ENT(AT_EUID, tsk->euid);
  184. NEW_AUX_ENT(AT_GID, tsk->gid);
  185. NEW_AUX_ENT(AT_EGID, tsk->egid);
  186. NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
  187. if (k_platform) {
  188. NEW_AUX_ENT(AT_PLATFORM,
  189. (elf_addr_t)(unsigned long)u_platform);
  190. }
  191. if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
  192. NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
  193. }
  194. #undef NEW_AUX_ENT
  195. /* AT_NULL is zero; clear the rest too */
  196. memset(&elf_info[ei_index], 0,
  197. sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
  198. /* And advance past the AT_NULL entry. */
  199. ei_index += 2;
  200. sp = STACK_ADD(p, ei_index);
  201. items = (argc + 1) + (envc + 1);
  202. if (interp_aout) {
  203. items += 3; /* a.out interpreters require argv & envp too */
  204. } else {
  205. items += 1; /* ELF interpreters only put argc on the stack */
  206. }
  207. bprm->p = STACK_ROUND(sp, items);
  208. /* Point sp at the lowest address on the stack */
  209. #ifdef CONFIG_STACK_GROWSUP
  210. sp = (elf_addr_t __user *)bprm->p - items - ei_index;
  211. bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
  212. #else
  213. sp = (elf_addr_t __user *)bprm->p;
  214. #endif
  215. /* Now, let's put argc (and argv, envp if appropriate) on the stack */
  216. if (__put_user(argc, sp++))
  217. return -EFAULT;
  218. if (interp_aout) {
  219. argv = sp + 2;
  220. envp = argv + argc + 1;
  221. if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
  222. __put_user((elf_addr_t)(unsigned long)envp, sp++))
  223. return -EFAULT;
  224. } else {
  225. argv = sp;
  226. envp = argv + argc + 1;
  227. }
  228. /* Populate argv and envp */
  229. p = current->mm->arg_end = current->mm->arg_start;
  230. while (argc-- > 0) {
  231. size_t len;
  232. if (__put_user((elf_addr_t)p, argv++))
  233. return -EFAULT;
  234. len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
  235. if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
  236. return 0;
  237. p += len;
  238. }
  239. if (__put_user(0, argv))
  240. return -EFAULT;
  241. current->mm->arg_end = current->mm->env_start = p;
  242. while (envc-- > 0) {
  243. size_t len;
  244. if (__put_user((elf_addr_t)p, envp++))
  245. return -EFAULT;
  246. len = strnlen_user((void __user *)p, PAGE_SIZE*MAX_ARG_PAGES);
  247. if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
  248. return 0;
  249. p += len;
  250. }
  251. if (__put_user(0, envp))
  252. return -EFAULT;
  253. current->mm->env_end = p;
  254. /* Put the elf_info on the stack in the right place. */
  255. sp = (elf_addr_t __user *)envp + 1;
  256. if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
  257. return -EFAULT;
  258. return 0;
  259. }
  260. #ifndef elf_map
  261. static unsigned long elf_map(struct file *filep, unsigned long addr,
  262. struct elf_phdr *eppnt, int prot, int type)
  263. {
  264. unsigned long map_addr;
  265. unsigned long pageoffset = ELF_PAGEOFFSET(eppnt->p_vaddr);
  266. down_write(&current->mm->mmap_sem);
  267. /* mmap() will return -EINVAL if given a zero size, but a
  268. * segment with zero filesize is perfectly valid */
  269. if (eppnt->p_filesz + pageoffset)
  270. map_addr = do_mmap(filep, ELF_PAGESTART(addr),
  271. eppnt->p_filesz + pageoffset, prot, type,
  272. eppnt->p_offset - pageoffset);
  273. else
  274. map_addr = ELF_PAGESTART(addr);
  275. up_write(&current->mm->mmap_sem);
  276. return(map_addr);
  277. }
  278. #endif /* !elf_map */
  279. /* This is much more generalized than the library routine read function,
  280. so we keep this separate. Technically the library read function
  281. is only provided so that we can read a.out libraries that have
  282. an ELF header */
  283. static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
  284. struct file *interpreter, unsigned long *interp_load_addr)
  285. {
  286. struct elf_phdr *elf_phdata;
  287. struct elf_phdr *eppnt;
  288. unsigned long load_addr = 0;
  289. int load_addr_set = 0;
  290. unsigned long last_bss = 0, elf_bss = 0;
  291. unsigned long error = ~0UL;
  292. int retval, i, size;
  293. /* First of all, some simple consistency checks */
  294. if (interp_elf_ex->e_type != ET_EXEC &&
  295. interp_elf_ex->e_type != ET_DYN)
  296. goto out;
  297. if (!elf_check_arch(interp_elf_ex))
  298. goto out;
  299. if (!interpreter->f_op || !interpreter->f_op->mmap)
  300. goto out;
  301. /*
  302. * If the size of this structure has changed, then punt, since
  303. * we will be doing the wrong thing.
  304. */
  305. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
  306. goto out;
  307. if (interp_elf_ex->e_phnum < 1 ||
  308. interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
  309. goto out;
  310. /* Now read in all of the header information */
  311. size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
  312. if (size > ELF_MIN_ALIGN)
  313. goto out;
  314. elf_phdata = kmalloc(size, GFP_KERNEL);
  315. if (!elf_phdata)
  316. goto out;
  317. retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
  318. (char *)elf_phdata,size);
  319. error = -EIO;
  320. if (retval != size) {
  321. if (retval < 0)
  322. error = retval;
  323. goto out_close;
  324. }
  325. eppnt = elf_phdata;
  326. for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
  327. if (eppnt->p_type == PT_LOAD) {
  328. int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
  329. int elf_prot = 0;
  330. unsigned long vaddr = 0;
  331. unsigned long k, map_addr;
  332. if (eppnt->p_flags & PF_R)
  333. elf_prot = PROT_READ;
  334. if (eppnt->p_flags & PF_W)
  335. elf_prot |= PROT_WRITE;
  336. if (eppnt->p_flags & PF_X)
  337. elf_prot |= PROT_EXEC;
  338. vaddr = eppnt->p_vaddr;
  339. if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
  340. elf_type |= MAP_FIXED;
  341. map_addr = elf_map(interpreter, load_addr + vaddr,
  342. eppnt, elf_prot, elf_type);
  343. error = map_addr;
  344. if (BAD_ADDR(map_addr))
  345. goto out_close;
  346. if (!load_addr_set &&
  347. interp_elf_ex->e_type == ET_DYN) {
  348. load_addr = map_addr - ELF_PAGESTART(vaddr);
  349. load_addr_set = 1;
  350. }
  351. /*
  352. * Check to see if the section's size will overflow the
  353. * allowed task size. Note that p_filesz must always be
  354. * <= p_memsize so it's only necessary to check p_memsz.
  355. */
  356. k = load_addr + eppnt->p_vaddr;
  357. if (BAD_ADDR(k) ||
  358. eppnt->p_filesz > eppnt->p_memsz ||
  359. eppnt->p_memsz > TASK_SIZE ||
  360. TASK_SIZE - eppnt->p_memsz < k) {
  361. error = -ENOMEM;
  362. goto out_close;
  363. }
  364. /*
  365. * Find the end of the file mapping for this phdr, and
  366. * keep track of the largest address we see for this.
  367. */
  368. k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
  369. if (k > elf_bss)
  370. elf_bss = k;
  371. /*
  372. * Do the same thing for the memory mapping - between
  373. * elf_bss and last_bss is the bss section.
  374. */
  375. k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
  376. if (k > last_bss)
  377. last_bss = k;
  378. }
  379. }
  380. /*
  381. * Now fill out the bss section. First pad the last page up
  382. * to the page boundary, and then perform a mmap to make sure
  383. * that there are zero-mapped pages up to and including the
  384. * last bss page.
  385. */
  386. if (padzero(elf_bss)) {
  387. error = -EFAULT;
  388. goto out_close;
  389. }
  390. /* What we have mapped so far */
  391. elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
  392. /* Map the last of the bss segment */
  393. if (last_bss > elf_bss) {
  394. down_write(&current->mm->mmap_sem);
  395. error = do_brk(elf_bss, last_bss - elf_bss);
  396. up_write(&current->mm->mmap_sem);
  397. if (BAD_ADDR(error))
  398. goto out_close;
  399. }
  400. *interp_load_addr = load_addr;
  401. error = ((unsigned long)interp_elf_ex->e_entry) + load_addr;
  402. out_close:
  403. kfree(elf_phdata);
  404. out:
  405. return error;
  406. }
  407. static unsigned long load_aout_interp(struct exec *interp_ex,
  408. struct file *interpreter)
  409. {
  410. unsigned long text_data, elf_entry = ~0UL;
  411. char __user * addr;
  412. loff_t offset;
  413. current->mm->end_code = interp_ex->a_text;
  414. text_data = interp_ex->a_text + interp_ex->a_data;
  415. current->mm->end_data = text_data;
  416. current->mm->brk = interp_ex->a_bss + text_data;
  417. switch (N_MAGIC(*interp_ex)) {
  418. case OMAGIC:
  419. offset = 32;
  420. addr = (char __user *)0;
  421. break;
  422. case ZMAGIC:
  423. case QMAGIC:
  424. offset = N_TXTOFF(*interp_ex);
  425. addr = (char __user *)N_TXTADDR(*interp_ex);
  426. break;
  427. default:
  428. goto out;
  429. }
  430. down_write(&current->mm->mmap_sem);
  431. do_brk(0, text_data);
  432. up_write(&current->mm->mmap_sem);
  433. if (!interpreter->f_op || !interpreter->f_op->read)
  434. goto out;
  435. if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
  436. goto out;
  437. flush_icache_range((unsigned long)addr,
  438. (unsigned long)addr + text_data);
  439. down_write(&current->mm->mmap_sem);
  440. do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
  441. interp_ex->a_bss);
  442. up_write(&current->mm->mmap_sem);
  443. elf_entry = interp_ex->a_entry;
  444. out:
  445. return elf_entry;
  446. }
  447. /*
  448. * These are the functions used to load ELF style executables and shared
  449. * libraries. There is no binary dependent code anywhere else.
  450. */
  451. #define INTERPRETER_NONE 0
  452. #define INTERPRETER_AOUT 1
  453. #define INTERPRETER_ELF 2
  454. #ifndef STACK_RND_MASK
  455. #define STACK_RND_MASK 0x7ff /* with 4K pages 8MB of VA */
  456. #endif
  457. static unsigned long randomize_stack_top(unsigned long stack_top)
  458. {
  459. unsigned int random_variable = 0;
  460. if ((current->flags & PF_RANDOMIZE) &&
  461. !(current->personality & ADDR_NO_RANDOMIZE)) {
  462. random_variable = get_random_int() & STACK_RND_MASK;
  463. random_variable <<= PAGE_SHIFT;
  464. }
  465. #ifdef CONFIG_STACK_GROWSUP
  466. return PAGE_ALIGN(stack_top) + random_variable;
  467. #else
  468. return PAGE_ALIGN(stack_top) - random_variable;
  469. #endif
  470. }
  471. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
  472. {
  473. struct file *interpreter = NULL; /* to shut gcc up */
  474. unsigned long load_addr = 0, load_bias = 0;
  475. int load_addr_set = 0;
  476. char * elf_interpreter = NULL;
  477. unsigned int interpreter_type = INTERPRETER_NONE;
  478. unsigned char ibcs2_interpreter = 0;
  479. unsigned long error;
  480. struct elf_phdr *elf_ppnt, *elf_phdata;
  481. unsigned long elf_bss, elf_brk;
  482. int elf_exec_fileno;
  483. int retval, i;
  484. unsigned int size;
  485. unsigned long elf_entry, interp_load_addr = 0;
  486. unsigned long start_code, end_code, start_data, end_data;
  487. unsigned long reloc_func_desc = 0;
  488. char passed_fileno[6];
  489. struct files_struct *files;
  490. int have_pt_gnu_stack, executable_stack = EXSTACK_DEFAULT;
  491. unsigned long def_flags = 0;
  492. struct {
  493. struct elfhdr elf_ex;
  494. struct elfhdr interp_elf_ex;
  495. struct exec interp_ex;
  496. } *loc;
  497. loc = kmalloc(sizeof(*loc), GFP_KERNEL);
  498. if (!loc) {
  499. retval = -ENOMEM;
  500. goto out_ret;
  501. }
  502. /* Get the exec-header */
  503. loc->elf_ex = *((struct elfhdr *)bprm->buf);
  504. retval = -ENOEXEC;
  505. /* First of all, some simple consistency checks */
  506. if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  507. goto out;
  508. if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
  509. goto out;
  510. if (!elf_check_arch(&loc->elf_ex))
  511. goto out;
  512. if (!bprm->file->f_op||!bprm->file->f_op->mmap)
  513. goto out;
  514. /* Now read in all of the header information */
  515. if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
  516. goto out;
  517. if (loc->elf_ex.e_phnum < 1 ||
  518. loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
  519. goto out;
  520. size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
  521. retval = -ENOMEM;
  522. elf_phdata = kmalloc(size, GFP_KERNEL);
  523. if (!elf_phdata)
  524. goto out;
  525. retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
  526. (char *)elf_phdata, size);
  527. if (retval != size) {
  528. if (retval >= 0)
  529. retval = -EIO;
  530. goto out_free_ph;
  531. }
  532. files = current->files; /* Refcounted so ok */
  533. retval = unshare_files();
  534. if (retval < 0)
  535. goto out_free_ph;
  536. if (files == current->files) {
  537. put_files_struct(files);
  538. files = NULL;
  539. }
  540. /* exec will make our files private anyway, but for the a.out
  541. loader stuff we need to do it earlier */
  542. retval = get_unused_fd();
  543. if (retval < 0)
  544. goto out_free_fh;
  545. get_file(bprm->file);
  546. fd_install(elf_exec_fileno = retval, bprm->file);
  547. elf_ppnt = elf_phdata;
  548. elf_bss = 0;
  549. elf_brk = 0;
  550. start_code = ~0UL;
  551. end_code = 0;
  552. start_data = 0;
  553. end_data = 0;
  554. for (i = 0; i < loc->elf_ex.e_phnum; i++) {
  555. if (elf_ppnt->p_type == PT_INTERP) {
  556. /* This is the program interpreter used for
  557. * shared libraries - for now assume that this
  558. * is an a.out format binary
  559. */
  560. retval = -ENOEXEC;
  561. if (elf_ppnt->p_filesz > PATH_MAX ||
  562. elf_ppnt->p_filesz < 2)
  563. goto out_free_file;
  564. retval = -ENOMEM;
  565. elf_interpreter = kmalloc(elf_ppnt->p_filesz,
  566. GFP_KERNEL);
  567. if (!elf_interpreter)
  568. goto out_free_file;
  569. retval = kernel_read(bprm->file, elf_ppnt->p_offset,
  570. elf_interpreter,
  571. elf_ppnt->p_filesz);
  572. if (retval != elf_ppnt->p_filesz) {
  573. if (retval >= 0)
  574. retval = -EIO;
  575. goto out_free_interp;
  576. }
  577. /* make sure path is NULL terminated */
  578. retval = -ENOEXEC;
  579. if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
  580. goto out_free_interp;
  581. /* If the program interpreter is one of these two,
  582. * then assume an iBCS2 image. Otherwise assume
  583. * a native linux image.
  584. */
  585. if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
  586. strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
  587. ibcs2_interpreter = 1;
  588. /*
  589. * The early SET_PERSONALITY here is so that the lookup
  590. * for the interpreter happens in the namespace of the
  591. * to-be-execed image. SET_PERSONALITY can select an
  592. * alternate root.
  593. *
  594. * However, SET_PERSONALITY is NOT allowed to switch
  595. * this task into the new images's memory mapping
  596. * policy - that is, TASK_SIZE must still evaluate to
  597. * that which is appropriate to the execing application.
  598. * This is because exit_mmap() needs to have TASK_SIZE
  599. * evaluate to the size of the old image.
  600. *
  601. * So if (say) a 64-bit application is execing a 32-bit
  602. * application it is the architecture's responsibility
  603. * to defer changing the value of TASK_SIZE until the
  604. * switch really is going to happen - do this in
  605. * flush_thread(). - akpm
  606. */
  607. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  608. interpreter = open_exec(elf_interpreter);
  609. retval = PTR_ERR(interpreter);
  610. if (IS_ERR(interpreter))
  611. goto out_free_interp;
  612. retval = kernel_read(interpreter, 0, bprm->buf,
  613. BINPRM_BUF_SIZE);
  614. if (retval != BINPRM_BUF_SIZE) {
  615. if (retval >= 0)
  616. retval = -EIO;
  617. goto out_free_dentry;
  618. }
  619. /* Get the exec headers */
  620. loc->interp_ex = *((struct exec *)bprm->buf);
  621. loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
  622. break;
  623. }
  624. elf_ppnt++;
  625. }
  626. elf_ppnt = elf_phdata;
  627. for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
  628. if (elf_ppnt->p_type == PT_GNU_STACK) {
  629. if (elf_ppnt->p_flags & PF_X)
  630. executable_stack = EXSTACK_ENABLE_X;
  631. else
  632. executable_stack = EXSTACK_DISABLE_X;
  633. break;
  634. }
  635. have_pt_gnu_stack = (i < loc->elf_ex.e_phnum);
  636. /* Some simple consistency checks for the interpreter */
  637. if (elf_interpreter) {
  638. interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
  639. /* Now figure out which format our binary is */
  640. if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
  641. (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
  642. (N_MAGIC(loc->interp_ex) != QMAGIC))
  643. interpreter_type = INTERPRETER_ELF;
  644. if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  645. interpreter_type &= ~INTERPRETER_ELF;
  646. retval = -ELIBBAD;
  647. if (!interpreter_type)
  648. goto out_free_dentry;
  649. /* Make sure only one type was selected */
  650. if ((interpreter_type & INTERPRETER_ELF) &&
  651. interpreter_type != INTERPRETER_ELF) {
  652. // FIXME - ratelimit this before re-enabling
  653. // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
  654. interpreter_type = INTERPRETER_ELF;
  655. }
  656. /* Verify the interpreter has a valid arch */
  657. if ((interpreter_type == INTERPRETER_ELF) &&
  658. !elf_check_arch(&loc->interp_elf_ex))
  659. goto out_free_dentry;
  660. } else {
  661. /* Executables without an interpreter also need a personality */
  662. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  663. }
  664. /* OK, we are done with that, now set up the arg stuff,
  665. and then start this sucker up */
  666. if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
  667. char *passed_p = passed_fileno;
  668. sprintf(passed_fileno, "%d", elf_exec_fileno);
  669. if (elf_interpreter) {
  670. retval = copy_strings_kernel(1, &passed_p, bprm);
  671. if (retval)
  672. goto out_free_dentry;
  673. bprm->argc++;
  674. }
  675. }
  676. /* Flush all traces of the currently running executable */
  677. retval = flush_old_exec(bprm);
  678. if (retval)
  679. goto out_free_dentry;
  680. /* Discard our unneeded old files struct */
  681. if (files) {
  682. put_files_struct(files);
  683. files = NULL;
  684. }
  685. /* OK, This is the point of no return */
  686. current->mm->start_data = 0;
  687. current->mm->end_data = 0;
  688. current->mm->end_code = 0;
  689. current->mm->mmap = NULL;
  690. current->flags &= ~PF_FORKNOEXEC;
  691. current->mm->def_flags = def_flags;
  692. /* Do this immediately, since STACK_TOP as used in setup_arg_pages
  693. may depend on the personality. */
  694. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  695. if (elf_read_implies_exec(loc->elf_ex, executable_stack))
  696. current->personality |= READ_IMPLIES_EXEC;
  697. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  698. current->flags |= PF_RANDOMIZE;
  699. arch_pick_mmap_layout(current->mm);
  700. /* Do this so that we can load the interpreter, if need be. We will
  701. change some of these later */
  702. current->mm->free_area_cache = current->mm->mmap_base;
  703. current->mm->cached_hole_size = 0;
  704. retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
  705. executable_stack);
  706. if (retval < 0) {
  707. send_sig(SIGKILL, current, 0);
  708. goto out_free_dentry;
  709. }
  710. current->mm->start_stack = bprm->p;
  711. /* Now we do a little grungy work by mmaping the ELF image into
  712. the correct location in memory. At this point, we assume that
  713. the image should be loaded at fixed address, not at a variable
  714. address. */
  715. for(i = 0, elf_ppnt = elf_phdata;
  716. i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
  717. int elf_prot = 0, elf_flags;
  718. unsigned long k, vaddr;
  719. if (elf_ppnt->p_type != PT_LOAD)
  720. continue;
  721. if (unlikely (elf_brk > elf_bss)) {
  722. unsigned long nbyte;
  723. /* There was a PT_LOAD segment with p_memsz > p_filesz
  724. before this one. Map anonymous pages, if needed,
  725. and clear the area. */
  726. retval = set_brk (elf_bss + load_bias,
  727. elf_brk + load_bias);
  728. if (retval) {
  729. send_sig(SIGKILL, current, 0);
  730. goto out_free_dentry;
  731. }
  732. nbyte = ELF_PAGEOFFSET(elf_bss);
  733. if (nbyte) {
  734. nbyte = ELF_MIN_ALIGN - nbyte;
  735. if (nbyte > elf_brk - elf_bss)
  736. nbyte = elf_brk - elf_bss;
  737. if (clear_user((void __user *)elf_bss +
  738. load_bias, nbyte)) {
  739. /*
  740. * This bss-zeroing can fail if the ELF
  741. * file specifies odd protections. So
  742. * we don't check the return value
  743. */
  744. }
  745. }
  746. }
  747. if (elf_ppnt->p_flags & PF_R)
  748. elf_prot |= PROT_READ;
  749. if (elf_ppnt->p_flags & PF_W)
  750. elf_prot |= PROT_WRITE;
  751. if (elf_ppnt->p_flags & PF_X)
  752. elf_prot |= PROT_EXEC;
  753. elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
  754. vaddr = elf_ppnt->p_vaddr;
  755. if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
  756. elf_flags |= MAP_FIXED;
  757. } else if (loc->elf_ex.e_type == ET_DYN) {
  758. /* Try and get dynamic programs out of the way of the
  759. * default mmap base, as well as whatever program they
  760. * might try to exec. This is because the brk will
  761. * follow the loader, and is not movable. */
  762. if (current->flags & PF_RANDOMIZE)
  763. load_bias = randomize_range(0x10000,
  764. ELF_ET_DYN_BASE,
  765. 0);
  766. else
  767. load_bias = ELF_ET_DYN_BASE;
  768. load_bias = ELF_PAGESTART(load_bias - vaddr);
  769. }
  770. error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
  771. elf_prot, elf_flags);
  772. if (BAD_ADDR(error)) {
  773. send_sig(SIGKILL, current, 0);
  774. goto out_free_dentry;
  775. }
  776. if (!load_addr_set) {
  777. load_addr_set = 1;
  778. load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
  779. if (loc->elf_ex.e_type == ET_DYN) {
  780. load_bias += error -
  781. ELF_PAGESTART(load_bias + vaddr);
  782. load_addr += load_bias;
  783. reloc_func_desc = load_bias;
  784. }
  785. }
  786. k = elf_ppnt->p_vaddr;
  787. if (k < start_code)
  788. start_code = k;
  789. if (start_data < k)
  790. start_data = k;
  791. /*
  792. * Check to see if the section's size will overflow the
  793. * allowed task size. Note that p_filesz must always be
  794. * <= p_memsz so it is only necessary to check p_memsz.
  795. */
  796. if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
  797. elf_ppnt->p_memsz > TASK_SIZE ||
  798. TASK_SIZE - elf_ppnt->p_memsz < k) {
  799. /* set_brk can never work. Avoid overflows. */
  800. send_sig(SIGKILL, current, 0);
  801. goto out_free_dentry;
  802. }
  803. k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  804. if (k > elf_bss)
  805. elf_bss = k;
  806. if ((elf_ppnt->p_flags & PF_X) && end_code < k)
  807. end_code = k;
  808. if (end_data < k)
  809. end_data = k;
  810. k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  811. if (k > elf_brk)
  812. elf_brk = k;
  813. }
  814. loc->elf_ex.e_entry += load_bias;
  815. elf_bss += load_bias;
  816. elf_brk += load_bias;
  817. start_code += load_bias;
  818. end_code += load_bias;
  819. start_data += load_bias;
  820. end_data += load_bias;
  821. /* Calling set_brk effectively mmaps the pages that we need
  822. * for the bss and break sections. We must do this before
  823. * mapping in the interpreter, to make sure it doesn't wind
  824. * up getting placed where the bss needs to go.
  825. */
  826. retval = set_brk(elf_bss, elf_brk);
  827. if (retval) {
  828. send_sig(SIGKILL, current, 0);
  829. goto out_free_dentry;
  830. }
  831. if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
  832. send_sig(SIGSEGV, current, 0);
  833. retval = -EFAULT; /* Nobody gets to see this, but.. */
  834. goto out_free_dentry;
  835. }
  836. if (elf_interpreter) {
  837. if (interpreter_type == INTERPRETER_AOUT)
  838. elf_entry = load_aout_interp(&loc->interp_ex,
  839. interpreter);
  840. else
  841. elf_entry = load_elf_interp(&loc->interp_elf_ex,
  842. interpreter,
  843. &interp_load_addr);
  844. if (BAD_ADDR(elf_entry)) {
  845. force_sig(SIGSEGV, current);
  846. retval = IS_ERR((void *)elf_entry) ?
  847. (int)elf_entry : -EINVAL;
  848. goto out_free_dentry;
  849. }
  850. reloc_func_desc = interp_load_addr;
  851. allow_write_access(interpreter);
  852. fput(interpreter);
  853. kfree(elf_interpreter);
  854. } else {
  855. elf_entry = loc->elf_ex.e_entry;
  856. if (BAD_ADDR(elf_entry)) {
  857. force_sig(SIGSEGV, current);
  858. retval = -EINVAL;
  859. goto out_free_dentry;
  860. }
  861. }
  862. kfree(elf_phdata);
  863. if (interpreter_type != INTERPRETER_AOUT)
  864. sys_close(elf_exec_fileno);
  865. set_binfmt(&elf_format);
  866. #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
  867. retval = arch_setup_additional_pages(bprm, executable_stack);
  868. if (retval < 0) {
  869. send_sig(SIGKILL, current, 0);
  870. goto out;
  871. }
  872. #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
  873. compute_creds(bprm);
  874. current->flags &= ~PF_FORKNOEXEC;
  875. create_elf_tables(bprm, &loc->elf_ex,
  876. (interpreter_type == INTERPRETER_AOUT),
  877. load_addr, interp_load_addr);
  878. /* N.B. passed_fileno might not be initialized? */
  879. if (interpreter_type == INTERPRETER_AOUT)
  880. current->mm->arg_start += strlen(passed_fileno) + 1;
  881. current->mm->end_code = end_code;
  882. current->mm->start_code = start_code;
  883. current->mm->start_data = start_data;
  884. current->mm->end_data = end_data;
  885. current->mm->start_stack = bprm->p;
  886. if (current->personality & MMAP_PAGE_ZERO) {
  887. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  888. and some applications "depend" upon this behavior.
  889. Since we do not have the power to recompile these, we
  890. emulate the SVr4 behavior. Sigh. */
  891. down_write(&current->mm->mmap_sem);
  892. error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
  893. MAP_FIXED | MAP_PRIVATE, 0);
  894. up_write(&current->mm->mmap_sem);
  895. }
  896. #ifdef ELF_PLAT_INIT
  897. /*
  898. * The ABI may specify that certain registers be set up in special
  899. * ways (on i386 %edx is the address of a DT_FINI function, for
  900. * example. In addition, it may also specify (eg, PowerPC64 ELF)
  901. * that the e_entry field is the address of the function descriptor
  902. * for the startup routine, rather than the address of the startup
  903. * routine itself. This macro performs whatever initialization to
  904. * the regs structure is required as well as any relocations to the
  905. * function descriptor entries when executing dynamically links apps.
  906. */
  907. ELF_PLAT_INIT(regs, reloc_func_desc);
  908. #endif
  909. start_thread(regs, elf_entry, bprm->p);
  910. if (unlikely(current->ptrace & PT_PTRACED)) {
  911. if (current->ptrace & PT_TRACE_EXEC)
  912. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  913. else
  914. send_sig(SIGTRAP, current, 0);
  915. }
  916. retval = 0;
  917. out:
  918. kfree(loc);
  919. out_ret:
  920. return retval;
  921. /* error cleanup */
  922. out_free_dentry:
  923. allow_write_access(interpreter);
  924. if (interpreter)
  925. fput(interpreter);
  926. out_free_interp:
  927. kfree(elf_interpreter);
  928. out_free_file:
  929. sys_close(elf_exec_fileno);
  930. out_free_fh:
  931. if (files)
  932. reset_files_struct(current, files);
  933. out_free_ph:
  934. kfree(elf_phdata);
  935. goto out;
  936. }
  937. /* This is really simpleminded and specialized - we are loading an
  938. a.out library that is given an ELF header. */
  939. static int load_elf_library(struct file *file)
  940. {
  941. struct elf_phdr *elf_phdata;
  942. struct elf_phdr *eppnt;
  943. unsigned long elf_bss, bss, len;
  944. int retval, error, i, j;
  945. struct elfhdr elf_ex;
  946. error = -ENOEXEC;
  947. retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
  948. if (retval != sizeof(elf_ex))
  949. goto out;
  950. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  951. goto out;
  952. /* First of all, some simple consistency checks */
  953. if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
  954. !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
  955. goto out;
  956. /* Now read in all of the header information */
  957. j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
  958. /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
  959. error = -ENOMEM;
  960. elf_phdata = kmalloc(j, GFP_KERNEL);
  961. if (!elf_phdata)
  962. goto out;
  963. eppnt = elf_phdata;
  964. error = -ENOEXEC;
  965. retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
  966. if (retval != j)
  967. goto out_free_ph;
  968. for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
  969. if ((eppnt + i)->p_type == PT_LOAD)
  970. j++;
  971. if (j != 1)
  972. goto out_free_ph;
  973. while (eppnt->p_type != PT_LOAD)
  974. eppnt++;
  975. /* Now use mmap to map the library into memory. */
  976. down_write(&current->mm->mmap_sem);
  977. error = do_mmap(file,
  978. ELF_PAGESTART(eppnt->p_vaddr),
  979. (eppnt->p_filesz +
  980. ELF_PAGEOFFSET(eppnt->p_vaddr)),
  981. PROT_READ | PROT_WRITE | PROT_EXEC,
  982. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  983. (eppnt->p_offset -
  984. ELF_PAGEOFFSET(eppnt->p_vaddr)));
  985. up_write(&current->mm->mmap_sem);
  986. if (error != ELF_PAGESTART(eppnt->p_vaddr))
  987. goto out_free_ph;
  988. elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
  989. if (padzero(elf_bss)) {
  990. error = -EFAULT;
  991. goto out_free_ph;
  992. }
  993. len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
  994. ELF_MIN_ALIGN - 1);
  995. bss = eppnt->p_memsz + eppnt->p_vaddr;
  996. if (bss > len) {
  997. down_write(&current->mm->mmap_sem);
  998. do_brk(len, bss - len);
  999. up_write(&current->mm->mmap_sem);
  1000. }
  1001. error = 0;
  1002. out_free_ph:
  1003. kfree(elf_phdata);
  1004. out:
  1005. return error;
  1006. }
  1007. /*
  1008. * Note that some platforms still use traditional core dumps and not
  1009. * the ELF core dump. Each platform can select it as appropriate.
  1010. */
  1011. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  1012. /*
  1013. * ELF core dumper
  1014. *
  1015. * Modelled on fs/exec.c:aout_core_dump()
  1016. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  1017. */
  1018. /*
  1019. * These are the only things you should do on a core-file: use only these
  1020. * functions to write out all the necessary info.
  1021. */
  1022. static int dump_write(struct file *file, const void *addr, int nr)
  1023. {
  1024. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1025. }
  1026. static int dump_seek(struct file *file, loff_t off)
  1027. {
  1028. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1029. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1030. return 0;
  1031. } else {
  1032. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1033. if (!buf)
  1034. return 0;
  1035. while (off > 0) {
  1036. unsigned long n = off;
  1037. if (n > PAGE_SIZE)
  1038. n = PAGE_SIZE;
  1039. if (!dump_write(file, buf, n))
  1040. return 0;
  1041. off -= n;
  1042. }
  1043. free_page((unsigned long)buf);
  1044. }
  1045. return 1;
  1046. }
  1047. /*
  1048. * Decide whether a segment is worth dumping; default is yes to be
  1049. * sure (missing info is worse than too much; etc).
  1050. * Personally I'd include everything, and use the coredump limit...
  1051. *
  1052. * I think we should skip something. But I am not sure how. H.J.
  1053. */
  1054. static int maydump(struct vm_area_struct *vma)
  1055. {
  1056. /* Do not dump I/O mapped devices or special mappings */
  1057. if (vma->vm_flags & (VM_IO | VM_RESERVED))
  1058. return 0;
  1059. /* Dump shared memory only if mapped from an anonymous file. */
  1060. if (vma->vm_flags & VM_SHARED)
  1061. return vma->vm_file->f_dentry->d_inode->i_nlink == 0;
  1062. /* If it hasn't been written to, don't write it out */
  1063. if (!vma->anon_vma)
  1064. return 0;
  1065. return 1;
  1066. }
  1067. /* An ELF note in memory */
  1068. struct memelfnote
  1069. {
  1070. const char *name;
  1071. int type;
  1072. unsigned int datasz;
  1073. void *data;
  1074. };
  1075. static int notesize(struct memelfnote *en)
  1076. {
  1077. int sz;
  1078. sz = sizeof(struct elf_note);
  1079. sz += roundup(strlen(en->name) + 1, 4);
  1080. sz += roundup(en->datasz, 4);
  1081. return sz;
  1082. }
  1083. #define DUMP_WRITE(addr, nr, foffset) \
  1084. do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
  1085. static int alignfile(struct file *file, loff_t *foffset)
  1086. {
  1087. static const char buf[4] = { 0, };
  1088. DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
  1089. return 1;
  1090. }
  1091. static int writenote(struct memelfnote *men, struct file *file,
  1092. loff_t *foffset)
  1093. {
  1094. struct elf_note en;
  1095. en.n_namesz = strlen(men->name) + 1;
  1096. en.n_descsz = men->datasz;
  1097. en.n_type = men->type;
  1098. DUMP_WRITE(&en, sizeof(en), foffset);
  1099. DUMP_WRITE(men->name, en.n_namesz, foffset);
  1100. if (!alignfile(file, foffset))
  1101. return 0;
  1102. DUMP_WRITE(men->data, men->datasz, foffset);
  1103. if (!alignfile(file, foffset))
  1104. return 0;
  1105. return 1;
  1106. }
  1107. #undef DUMP_WRITE
  1108. #define DUMP_WRITE(addr, nr) \
  1109. if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
  1110. goto end_coredump;
  1111. #define DUMP_SEEK(off) \
  1112. if (!dump_seek(file, (off))) \
  1113. goto end_coredump;
  1114. static void fill_elf_header(struct elfhdr *elf, int segs)
  1115. {
  1116. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1117. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1118. elf->e_ident[EI_DATA] = ELF_DATA;
  1119. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1120. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1121. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1122. elf->e_type = ET_CORE;
  1123. elf->e_machine = ELF_ARCH;
  1124. elf->e_version = EV_CURRENT;
  1125. elf->e_entry = 0;
  1126. elf->e_phoff = sizeof(struct elfhdr);
  1127. elf->e_shoff = 0;
  1128. elf->e_flags = ELF_CORE_EFLAGS;
  1129. elf->e_ehsize = sizeof(struct elfhdr);
  1130. elf->e_phentsize = sizeof(struct elf_phdr);
  1131. elf->e_phnum = segs;
  1132. elf->e_shentsize = 0;
  1133. elf->e_shnum = 0;
  1134. elf->e_shstrndx = 0;
  1135. return;
  1136. }
  1137. static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1138. {
  1139. phdr->p_type = PT_NOTE;
  1140. phdr->p_offset = offset;
  1141. phdr->p_vaddr = 0;
  1142. phdr->p_paddr = 0;
  1143. phdr->p_filesz = sz;
  1144. phdr->p_memsz = 0;
  1145. phdr->p_flags = 0;
  1146. phdr->p_align = 0;
  1147. return;
  1148. }
  1149. static void fill_note(struct memelfnote *note, const char *name, int type,
  1150. unsigned int sz, void *data)
  1151. {
  1152. note->name = name;
  1153. note->type = type;
  1154. note->datasz = sz;
  1155. note->data = data;
  1156. return;
  1157. }
  1158. /*
  1159. * fill up all the fields in prstatus from the given task struct, except
  1160. * registers which need to be filled up separately.
  1161. */
  1162. static void fill_prstatus(struct elf_prstatus *prstatus,
  1163. struct task_struct *p, long signr)
  1164. {
  1165. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1166. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1167. prstatus->pr_sighold = p->blocked.sig[0];
  1168. prstatus->pr_pid = p->pid;
  1169. prstatus->pr_ppid = p->parent->pid;
  1170. prstatus->pr_pgrp = process_group(p);
  1171. prstatus->pr_sid = p->signal->session;
  1172. if (thread_group_leader(p)) {
  1173. /*
  1174. * This is the record for the group leader. Add in the
  1175. * cumulative times of previous dead threads. This total
  1176. * won't include the time of each live thread whose state
  1177. * is included in the core dump. The final total reported
  1178. * to our parent process when it calls wait4 will include
  1179. * those sums as well as the little bit more time it takes
  1180. * this and each other thread to finish dying after the
  1181. * core dump synchronization phase.
  1182. */
  1183. cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
  1184. &prstatus->pr_utime);
  1185. cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
  1186. &prstatus->pr_stime);
  1187. } else {
  1188. cputime_to_timeval(p->utime, &prstatus->pr_utime);
  1189. cputime_to_timeval(p->stime, &prstatus->pr_stime);
  1190. }
  1191. cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
  1192. cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
  1193. }
  1194. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1195. struct mm_struct *mm)
  1196. {
  1197. unsigned int i, len;
  1198. /* first copy the parameters from user space */
  1199. memset(psinfo, 0, sizeof(struct elf_prpsinfo));
  1200. len = mm->arg_end - mm->arg_start;
  1201. if (len >= ELF_PRARGSZ)
  1202. len = ELF_PRARGSZ-1;
  1203. if (copy_from_user(&psinfo->pr_psargs,
  1204. (const char __user *)mm->arg_start, len))
  1205. return -EFAULT;
  1206. for(i = 0; i < len; i++)
  1207. if (psinfo->pr_psargs[i] == 0)
  1208. psinfo->pr_psargs[i] = ' ';
  1209. psinfo->pr_psargs[len] = 0;
  1210. psinfo->pr_pid = p->pid;
  1211. psinfo->pr_ppid = p->parent->pid;
  1212. psinfo->pr_pgrp = process_group(p);
  1213. psinfo->pr_sid = p->signal->session;
  1214. i = p->state ? ffz(~p->state) + 1 : 0;
  1215. psinfo->pr_state = i;
  1216. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1217. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1218. psinfo->pr_nice = task_nice(p);
  1219. psinfo->pr_flag = p->flags;
  1220. SET_UID(psinfo->pr_uid, p->uid);
  1221. SET_GID(psinfo->pr_gid, p->gid);
  1222. strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
  1223. return 0;
  1224. }
  1225. /* Here is the structure in which status of each thread is captured. */
  1226. struct elf_thread_status
  1227. {
  1228. struct list_head list;
  1229. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  1230. elf_fpregset_t fpu; /* NT_PRFPREG */
  1231. struct task_struct *thread;
  1232. #ifdef ELF_CORE_COPY_XFPREGS
  1233. elf_fpxregset_t xfpu; /* NT_PRXFPREG */
  1234. #endif
  1235. struct memelfnote notes[3];
  1236. int num_notes;
  1237. };
  1238. /*
  1239. * In order to add the specific thread information for the elf file format,
  1240. * we need to keep a linked list of every threads pr_status and then create
  1241. * a single section for them in the final core file.
  1242. */
  1243. static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
  1244. {
  1245. int sz = 0;
  1246. struct task_struct *p = t->thread;
  1247. t->num_notes = 0;
  1248. fill_prstatus(&t->prstatus, p, signr);
  1249. elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
  1250. fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
  1251. &(t->prstatus));
  1252. t->num_notes++;
  1253. sz += notesize(&t->notes[0]);
  1254. if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
  1255. &t->fpu))) {
  1256. fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
  1257. &(t->fpu));
  1258. t->num_notes++;
  1259. sz += notesize(&t->notes[1]);
  1260. }
  1261. #ifdef ELF_CORE_COPY_XFPREGS
  1262. if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
  1263. fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu),
  1264. &t->xfpu);
  1265. t->num_notes++;
  1266. sz += notesize(&t->notes[2]);
  1267. }
  1268. #endif
  1269. return sz;
  1270. }
  1271. /*
  1272. * Actual dumper
  1273. *
  1274. * This is a two-pass process; first we find the offsets of the bits,
  1275. * and then they are actually written out. If we run out of core limit
  1276. * we just truncate.
  1277. */
  1278. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file)
  1279. {
  1280. #define NUM_NOTES 6
  1281. int has_dumped = 0;
  1282. mm_segment_t fs;
  1283. int segs;
  1284. size_t size = 0;
  1285. int i;
  1286. struct vm_area_struct *vma;
  1287. struct elfhdr *elf = NULL;
  1288. loff_t offset = 0, dataoff, foffset;
  1289. unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1290. int numnote;
  1291. struct memelfnote *notes = NULL;
  1292. struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
  1293. struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
  1294. struct task_struct *g, *p;
  1295. LIST_HEAD(thread_list);
  1296. struct list_head *t;
  1297. elf_fpregset_t *fpu = NULL;
  1298. #ifdef ELF_CORE_COPY_XFPREGS
  1299. elf_fpxregset_t *xfpu = NULL;
  1300. #endif
  1301. int thread_status_size = 0;
  1302. elf_addr_t *auxv;
  1303. /*
  1304. * We no longer stop all VM operations.
  1305. *
  1306. * This is because those proceses that could possibly change map_count
  1307. * or the mmap / vma pages are now blocked in do_exit on current
  1308. * finishing this core dump.
  1309. *
  1310. * Only ptrace can touch these memory addresses, but it doesn't change
  1311. * the map_count or the pages allocated. So no possibility of crashing
  1312. * exists while dumping the mm->vm_next areas to the core file.
  1313. */
  1314. /* alloc memory for large data structures: too large to be on stack */
  1315. elf = kmalloc(sizeof(*elf), GFP_KERNEL);
  1316. if (!elf)
  1317. goto cleanup;
  1318. prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
  1319. if (!prstatus)
  1320. goto cleanup;
  1321. psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
  1322. if (!psinfo)
  1323. goto cleanup;
  1324. notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
  1325. if (!notes)
  1326. goto cleanup;
  1327. fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
  1328. if (!fpu)
  1329. goto cleanup;
  1330. #ifdef ELF_CORE_COPY_XFPREGS
  1331. xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
  1332. if (!xfpu)
  1333. goto cleanup;
  1334. #endif
  1335. if (signr) {
  1336. struct elf_thread_status *tmp;
  1337. rcu_read_lock();
  1338. do_each_thread(g,p)
  1339. if (current->mm == p->mm && current != p) {
  1340. tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
  1341. if (!tmp) {
  1342. rcu_read_unlock();
  1343. goto cleanup;
  1344. }
  1345. tmp->thread = p;
  1346. list_add(&tmp->list, &thread_list);
  1347. }
  1348. while_each_thread(g,p);
  1349. rcu_read_unlock();
  1350. list_for_each(t, &thread_list) {
  1351. struct elf_thread_status *tmp;
  1352. int sz;
  1353. tmp = list_entry(t, struct elf_thread_status, list);
  1354. sz = elf_dump_thread_status(signr, tmp);
  1355. thread_status_size += sz;
  1356. }
  1357. }
  1358. /* now collect the dump for the current */
  1359. memset(prstatus, 0, sizeof(*prstatus));
  1360. fill_prstatus(prstatus, current, signr);
  1361. elf_core_copy_regs(&prstatus->pr_reg, regs);
  1362. segs = current->mm->map_count;
  1363. #ifdef ELF_CORE_EXTRA_PHDRS
  1364. segs += ELF_CORE_EXTRA_PHDRS;
  1365. #endif
  1366. /* Set up header */
  1367. fill_elf_header(elf, segs + 1); /* including notes section */
  1368. has_dumped = 1;
  1369. current->flags |= PF_DUMPCORE;
  1370. /*
  1371. * Set up the notes in similar form to SVR4 core dumps made
  1372. * with info from their /proc.
  1373. */
  1374. fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
  1375. fill_psinfo(psinfo, current->group_leader, current->mm);
  1376. fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
  1377. numnote = 2;
  1378. auxv = (elf_addr_t *)current->mm->saved_auxv;
  1379. i = 0;
  1380. do
  1381. i += 2;
  1382. while (auxv[i - 2] != AT_NULL);
  1383. fill_note(&notes[numnote++], "CORE", NT_AUXV,
  1384. i * sizeof(elf_addr_t), auxv);
  1385. /* Try to dump the FPU. */
  1386. if ((prstatus->pr_fpvalid =
  1387. elf_core_copy_task_fpregs(current, regs, fpu)))
  1388. fill_note(notes + numnote++,
  1389. "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
  1390. #ifdef ELF_CORE_COPY_XFPREGS
  1391. if (elf_core_copy_task_xfpregs(current, xfpu))
  1392. fill_note(notes + numnote++,
  1393. "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
  1394. #endif
  1395. fs = get_fs();
  1396. set_fs(KERNEL_DS);
  1397. DUMP_WRITE(elf, sizeof(*elf));
  1398. offset += sizeof(*elf); /* Elf header */
  1399. offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
  1400. foffset = offset;
  1401. /* Write notes phdr entry */
  1402. {
  1403. struct elf_phdr phdr;
  1404. int sz = 0;
  1405. for (i = 0; i < numnote; i++)
  1406. sz += notesize(notes + i);
  1407. sz += thread_status_size;
  1408. #ifdef ELF_CORE_WRITE_EXTRA_NOTES
  1409. sz += ELF_CORE_EXTRA_NOTES_SIZE;
  1410. #endif
  1411. fill_elf_note_phdr(&phdr, sz, offset);
  1412. offset += sz;
  1413. DUMP_WRITE(&phdr, sizeof(phdr));
  1414. }
  1415. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1416. /* Write program headers for segments dump */
  1417. for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
  1418. struct elf_phdr phdr;
  1419. size_t sz;
  1420. sz = vma->vm_end - vma->vm_start;
  1421. phdr.p_type = PT_LOAD;
  1422. phdr.p_offset = offset;
  1423. phdr.p_vaddr = vma->vm_start;
  1424. phdr.p_paddr = 0;
  1425. phdr.p_filesz = maydump(vma) ? sz : 0;
  1426. phdr.p_memsz = sz;
  1427. offset += phdr.p_filesz;
  1428. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1429. if (vma->vm_flags & VM_WRITE)
  1430. phdr.p_flags |= PF_W;
  1431. if (vma->vm_flags & VM_EXEC)
  1432. phdr.p_flags |= PF_X;
  1433. phdr.p_align = ELF_EXEC_PAGESIZE;
  1434. DUMP_WRITE(&phdr, sizeof(phdr));
  1435. }
  1436. #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
  1437. ELF_CORE_WRITE_EXTRA_PHDRS;
  1438. #endif
  1439. /* write out the notes section */
  1440. for (i = 0; i < numnote; i++)
  1441. if (!writenote(notes + i, file, &foffset))
  1442. goto end_coredump;
  1443. #ifdef ELF_CORE_WRITE_EXTRA_NOTES
  1444. ELF_CORE_WRITE_EXTRA_NOTES;
  1445. #endif
  1446. /* write out the thread status notes section */
  1447. list_for_each(t, &thread_list) {
  1448. struct elf_thread_status *tmp =
  1449. list_entry(t, struct elf_thread_status, list);
  1450. for (i = 0; i < tmp->num_notes; i++)
  1451. if (!writenote(&tmp->notes[i], file, &foffset))
  1452. goto end_coredump;
  1453. }
  1454. /* Align to page */
  1455. DUMP_SEEK(dataoff - foffset);
  1456. for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
  1457. unsigned long addr;
  1458. if (!maydump(vma))
  1459. continue;
  1460. for (addr = vma->vm_start;
  1461. addr < vma->vm_end;
  1462. addr += PAGE_SIZE) {
  1463. struct page *page;
  1464. struct vm_area_struct *vma;
  1465. if (get_user_pages(current, current->mm, addr, 1, 0, 1,
  1466. &page, &vma) <= 0) {
  1467. DUMP_SEEK(PAGE_SIZE);
  1468. } else {
  1469. if (page == ZERO_PAGE(addr)) {
  1470. DUMP_SEEK(PAGE_SIZE);
  1471. } else {
  1472. void *kaddr;
  1473. flush_cache_page(vma, addr,
  1474. page_to_pfn(page));
  1475. kaddr = kmap(page);
  1476. if ((size += PAGE_SIZE) > limit ||
  1477. !dump_write(file, kaddr,
  1478. PAGE_SIZE)) {
  1479. kunmap(page);
  1480. page_cache_release(page);
  1481. goto end_coredump;
  1482. }
  1483. kunmap(page);
  1484. }
  1485. page_cache_release(page);
  1486. }
  1487. }
  1488. }
  1489. #ifdef ELF_CORE_WRITE_EXTRA_DATA
  1490. ELF_CORE_WRITE_EXTRA_DATA;
  1491. #endif
  1492. end_coredump:
  1493. set_fs(fs);
  1494. cleanup:
  1495. while (!list_empty(&thread_list)) {
  1496. struct list_head *tmp = thread_list.next;
  1497. list_del(tmp);
  1498. kfree(list_entry(tmp, struct elf_thread_status, list));
  1499. }
  1500. kfree(elf);
  1501. kfree(prstatus);
  1502. kfree(psinfo);
  1503. kfree(notes);
  1504. kfree(fpu);
  1505. #ifdef ELF_CORE_COPY_XFPREGS
  1506. kfree(xfpu);
  1507. #endif
  1508. return has_dumped;
  1509. #undef NUM_NOTES
  1510. }
  1511. #endif /* USE_ELF_CORE_DUMP */
  1512. static int __init init_elf_binfmt(void)
  1513. {
  1514. return register_binfmt(&elf_format);
  1515. }
  1516. static void __exit exit_elf_binfmt(void)
  1517. {
  1518. /* Remove the COFF and ELF loaders. */
  1519. unregister_binfmt(&elf_format);
  1520. }
  1521. core_initcall(init_elf_binfmt);
  1522. module_exit(exit_elf_binfmt);
  1523. MODULE_LICENSE("GPL");