binfmt_elf.c 45 KB

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