binfmt_elf.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  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/compiler.h>
  34. #include <linux/highmem.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/security.h>
  37. #include <linux/syscalls.h>
  38. #include <linux/random.h>
  39. #include <linux/elf.h>
  40. #include <linux/utsname.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 *,
  47. int, int, unsigned long);
  48. /*
  49. * If we don't support core dumping, then supply a NULL so we
  50. * don't even try.
  51. */
  52. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  53. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
  54. #else
  55. #define elf_core_dump NULL
  56. #endif
  57. #if ELF_EXEC_PAGESIZE > PAGE_SIZE
  58. #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
  59. #else
  60. #define ELF_MIN_ALIGN PAGE_SIZE
  61. #endif
  62. #ifndef ELF_CORE_EFLAGS
  63. #define ELF_CORE_EFLAGS 0
  64. #endif
  65. #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
  66. #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
  67. #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
  68. static struct linux_binfmt elf_format = {
  69. .module = THIS_MODULE,
  70. .load_binary = load_elf_binary,
  71. .load_shlib = load_elf_library,
  72. .core_dump = elf_core_dump,
  73. .min_coredump = ELF_EXEC_PAGESIZE,
  74. .hasvdso = 1
  75. };
  76. #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
  77. static int set_brk(unsigned long start, unsigned long end)
  78. {
  79. start = ELF_PAGEALIGN(start);
  80. end = ELF_PAGEALIGN(end);
  81. if (end > start) {
  82. unsigned long addr;
  83. down_write(&current->mm->mmap_sem);
  84. addr = do_brk(start, end - start);
  85. up_write(&current->mm->mmap_sem);
  86. if (BAD_ADDR(addr))
  87. return addr;
  88. }
  89. current->mm->start_brk = current->mm->brk = end;
  90. return 0;
  91. }
  92. /* We need to explicitly zero any fractional pages
  93. after the data section (i.e. bss). This would
  94. contain the junk from the file that should not
  95. be in memory
  96. */
  97. static int padzero(unsigned long elf_bss)
  98. {
  99. unsigned long nbyte;
  100. nbyte = ELF_PAGEOFFSET(elf_bss);
  101. if (nbyte) {
  102. nbyte = ELF_MIN_ALIGN - nbyte;
  103. if (clear_user((void __user *) elf_bss, nbyte))
  104. return -EFAULT;
  105. }
  106. return 0;
  107. }
  108. /* Let's use some macros to make this stack manipulation a litle clearer */
  109. #ifdef CONFIG_STACK_GROWSUP
  110. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
  111. #define STACK_ROUND(sp, items) \
  112. ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
  113. #define STACK_ALLOC(sp, len) ({ \
  114. elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
  115. old_sp; })
  116. #else
  117. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
  118. #define STACK_ROUND(sp, items) \
  119. (((unsigned long) (sp - items)) &~ 15UL)
  120. #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
  121. #endif
  122. static int
  123. create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
  124. int interp_aout, unsigned long load_addr,
  125. unsigned long interp_load_addr)
  126. {
  127. unsigned long p = bprm->p;
  128. int argc = bprm->argc;
  129. int envc = bprm->envc;
  130. elf_addr_t __user *argv;
  131. elf_addr_t __user *envp;
  132. elf_addr_t __user *sp;
  133. elf_addr_t __user *u_platform;
  134. const char *k_platform = ELF_PLATFORM;
  135. int items;
  136. elf_addr_t *elf_info;
  137. int ei_index = 0;
  138. struct task_struct *tsk = current;
  139. struct vm_area_struct *vma;
  140. /*
  141. * In some cases (e.g. Hyper-Threading), we want to avoid L1
  142. * evictions by the processes running on the same package. One
  143. * thing we can do is to shuffle the initial stack for them.
  144. */
  145. p = arch_align_stack(p);
  146. /*
  147. * If this architecture has a platform capability string, copy it
  148. * to userspace. In some cases (Sparc), this info is impossible
  149. * for userspace to get any other way, in others (i386) it is
  150. * merely difficult.
  151. */
  152. u_platform = NULL;
  153. if (k_platform) {
  154. size_t len = strlen(k_platform) + 1;
  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. /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
  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. * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
  172. * ARCH_DLINFO changes
  173. */
  174. ARCH_DLINFO;
  175. #endif
  176. NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
  177. NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
  178. NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
  179. NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
  180. NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
  181. NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
  182. NEW_AUX_ENT(AT_BASE, interp_load_addr);
  183. NEW_AUX_ENT(AT_FLAGS, 0);
  184. NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
  185. NEW_AUX_ENT(AT_UID, tsk->uid);
  186. NEW_AUX_ENT(AT_EUID, tsk->euid);
  187. NEW_AUX_ENT(AT_GID, tsk->gid);
  188. NEW_AUX_ENT(AT_EGID, tsk->egid);
  189. NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
  190. if (k_platform) {
  191. NEW_AUX_ENT(AT_PLATFORM,
  192. (elf_addr_t)(unsigned long)u_platform);
  193. }
  194. if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
  195. NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
  196. }
  197. #undef NEW_AUX_ENT
  198. /* AT_NULL is zero; clear the rest too */
  199. memset(&elf_info[ei_index], 0,
  200. sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
  201. /* And advance past the AT_NULL entry. */
  202. ei_index += 2;
  203. sp = STACK_ADD(p, ei_index);
  204. items = (argc + 1) + (envc + 1);
  205. if (interp_aout) {
  206. items += 3; /* a.out interpreters require argv & envp too */
  207. } else {
  208. items += 1; /* ELF interpreters only put argc on the stack */
  209. }
  210. bprm->p = STACK_ROUND(sp, items);
  211. /* Point sp at the lowest address on the stack */
  212. #ifdef CONFIG_STACK_GROWSUP
  213. sp = (elf_addr_t __user *)bprm->p - items - ei_index;
  214. bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
  215. #else
  216. sp = (elf_addr_t __user *)bprm->p;
  217. #endif
  218. /*
  219. * Grow the stack manually; some architectures have a limit on how
  220. * far ahead a user-space access may be in order to grow the stack.
  221. */
  222. vma = find_extend_vma(current->mm, bprm->p);
  223. if (!vma)
  224. return -EFAULT;
  225. /* Now, let's put argc (and argv, envp if appropriate) on the stack */
  226. if (__put_user(argc, sp++))
  227. return -EFAULT;
  228. if (interp_aout) {
  229. argv = sp + 2;
  230. envp = argv + argc + 1;
  231. if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
  232. __put_user((elf_addr_t)(unsigned long)envp, sp++))
  233. return -EFAULT;
  234. } else {
  235. argv = sp;
  236. envp = argv + argc + 1;
  237. }
  238. /* Populate argv and envp */
  239. p = current->mm->arg_end = current->mm->arg_start;
  240. while (argc-- > 0) {
  241. size_t len;
  242. if (__put_user((elf_addr_t)p, argv++))
  243. return -EFAULT;
  244. len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
  245. if (!len || len > MAX_ARG_STRLEN)
  246. return 0;
  247. p += len;
  248. }
  249. if (__put_user(0, argv))
  250. return -EFAULT;
  251. current->mm->arg_end = current->mm->env_start = p;
  252. while (envc-- > 0) {
  253. size_t len;
  254. if (__put_user((elf_addr_t)p, envp++))
  255. return -EFAULT;
  256. len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
  257. if (!len || len > MAX_ARG_STRLEN)
  258. return 0;
  259. p += len;
  260. }
  261. if (__put_user(0, envp))
  262. return -EFAULT;
  263. current->mm->env_end = p;
  264. /* Put the elf_info on the stack in the right place. */
  265. sp = (elf_addr_t __user *)envp + 1;
  266. if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
  267. return -EFAULT;
  268. return 0;
  269. }
  270. #ifndef elf_map
  271. static unsigned long elf_map(struct file *filep, unsigned long addr,
  272. struct elf_phdr *eppnt, int prot, int type,
  273. unsigned long total_size)
  274. {
  275. unsigned long map_addr;
  276. unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
  277. unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
  278. addr = ELF_PAGESTART(addr);
  279. size = ELF_PAGEALIGN(size);
  280. /* mmap() will return -EINVAL if given a zero size, but a
  281. * segment with zero filesize is perfectly valid */
  282. if (!size)
  283. return addr;
  284. down_write(&current->mm->mmap_sem);
  285. /*
  286. * total_size is the size of the ELF (interpreter) image.
  287. * The _first_ mmap needs to know the full size, otherwise
  288. * randomization might put this image into an overlapping
  289. * position with the ELF binary image. (since size < total_size)
  290. * So we first map the 'big' image - and unmap the remainder at
  291. * the end. (which unmap is needed for ELF images with holes.)
  292. */
  293. if (total_size) {
  294. total_size = ELF_PAGEALIGN(total_size);
  295. map_addr = do_mmap(filep, addr, total_size, prot, type, off);
  296. if (!BAD_ADDR(map_addr))
  297. do_munmap(current->mm, map_addr+size, total_size-size);
  298. } else
  299. map_addr = do_mmap(filep, addr, size, prot, type, off);
  300. up_write(&current->mm->mmap_sem);
  301. return(map_addr);
  302. }
  303. #endif /* !elf_map */
  304. static unsigned long total_mapping_size(struct elf_phdr *cmds, int nr)
  305. {
  306. int i, first_idx = -1, last_idx = -1;
  307. for (i = 0; i < nr; i++) {
  308. if (cmds[i].p_type == PT_LOAD) {
  309. last_idx = i;
  310. if (first_idx == -1)
  311. first_idx = i;
  312. }
  313. }
  314. if (first_idx == -1)
  315. return 0;
  316. return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
  317. ELF_PAGESTART(cmds[first_idx].p_vaddr);
  318. }
  319. /* This is much more generalized than the library routine read function,
  320. so we keep this separate. Technically the library read function
  321. is only provided so that we can read a.out libraries that have
  322. an ELF header */
  323. static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
  324. struct file *interpreter, unsigned long *interp_map_addr,
  325. unsigned long no_base)
  326. {
  327. struct elf_phdr *elf_phdata;
  328. struct elf_phdr *eppnt;
  329. unsigned long load_addr = 0;
  330. int load_addr_set = 0;
  331. unsigned long last_bss = 0, elf_bss = 0;
  332. unsigned long error = ~0UL;
  333. unsigned long total_size;
  334. int retval, i, size;
  335. /* First of all, some simple consistency checks */
  336. if (interp_elf_ex->e_type != ET_EXEC &&
  337. interp_elf_ex->e_type != ET_DYN)
  338. goto out;
  339. if (!elf_check_arch(interp_elf_ex))
  340. goto out;
  341. if (!interpreter->f_op || !interpreter->f_op->mmap)
  342. goto out;
  343. /*
  344. * If the size of this structure has changed, then punt, since
  345. * we will be doing the wrong thing.
  346. */
  347. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
  348. goto out;
  349. if (interp_elf_ex->e_phnum < 1 ||
  350. interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
  351. goto out;
  352. /* Now read in all of the header information */
  353. size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
  354. if (size > ELF_MIN_ALIGN)
  355. goto out;
  356. elf_phdata = kmalloc(size, GFP_KERNEL);
  357. if (!elf_phdata)
  358. goto out;
  359. retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
  360. (char *)elf_phdata,size);
  361. error = -EIO;
  362. if (retval != size) {
  363. if (retval < 0)
  364. error = retval;
  365. goto out_close;
  366. }
  367. total_size = total_mapping_size(elf_phdata, interp_elf_ex->e_phnum);
  368. if (!total_size) {
  369. error = -EINVAL;
  370. goto out_close;
  371. }
  372. eppnt = elf_phdata;
  373. for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
  374. if (eppnt->p_type == PT_LOAD) {
  375. int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
  376. int elf_prot = 0;
  377. unsigned long vaddr = 0;
  378. unsigned long k, map_addr;
  379. if (eppnt->p_flags & PF_R)
  380. elf_prot = PROT_READ;
  381. if (eppnt->p_flags & PF_W)
  382. elf_prot |= PROT_WRITE;
  383. if (eppnt->p_flags & PF_X)
  384. elf_prot |= PROT_EXEC;
  385. vaddr = eppnt->p_vaddr;
  386. if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
  387. elf_type |= MAP_FIXED;
  388. else if (no_base && interp_elf_ex->e_type == ET_DYN)
  389. load_addr = -vaddr;
  390. map_addr = elf_map(interpreter, load_addr + vaddr,
  391. eppnt, elf_prot, elf_type, total_size);
  392. total_size = 0;
  393. if (!*interp_map_addr)
  394. *interp_map_addr = map_addr;
  395. error = map_addr;
  396. if (BAD_ADDR(map_addr))
  397. goto out_close;
  398. if (!load_addr_set &&
  399. interp_elf_ex->e_type == ET_DYN) {
  400. load_addr = map_addr - ELF_PAGESTART(vaddr);
  401. load_addr_set = 1;
  402. }
  403. /*
  404. * Check to see if the section's size will overflow the
  405. * allowed task size. Note that p_filesz must always be
  406. * <= p_memsize so it's only necessary to check p_memsz.
  407. */
  408. k = load_addr + eppnt->p_vaddr;
  409. if (BAD_ADDR(k) ||
  410. eppnt->p_filesz > eppnt->p_memsz ||
  411. eppnt->p_memsz > TASK_SIZE ||
  412. TASK_SIZE - eppnt->p_memsz < k) {
  413. error = -ENOMEM;
  414. goto out_close;
  415. }
  416. /*
  417. * Find the end of the file mapping for this phdr, and
  418. * keep track of the largest address we see for this.
  419. */
  420. k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
  421. if (k > elf_bss)
  422. elf_bss = k;
  423. /*
  424. * Do the same thing for the memory mapping - between
  425. * elf_bss and last_bss is the bss section.
  426. */
  427. k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
  428. if (k > last_bss)
  429. last_bss = k;
  430. }
  431. }
  432. /*
  433. * Now fill out the bss section. First pad the last page up
  434. * to the page boundary, and then perform a mmap to make sure
  435. * that there are zero-mapped pages up to and including the
  436. * last bss page.
  437. */
  438. if (padzero(elf_bss)) {
  439. error = -EFAULT;
  440. goto out_close;
  441. }
  442. /* What we have mapped so far */
  443. elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
  444. /* Map the last of the bss segment */
  445. if (last_bss > elf_bss) {
  446. down_write(&current->mm->mmap_sem);
  447. error = do_brk(elf_bss, last_bss - elf_bss);
  448. up_write(&current->mm->mmap_sem);
  449. if (BAD_ADDR(error))
  450. goto out_close;
  451. }
  452. error = load_addr;
  453. out_close:
  454. kfree(elf_phdata);
  455. out:
  456. return error;
  457. }
  458. static unsigned long load_aout_interp(struct exec *interp_ex,
  459. struct file *interpreter)
  460. {
  461. unsigned long text_data, elf_entry = ~0UL;
  462. char __user * addr;
  463. loff_t offset;
  464. current->mm->end_code = interp_ex->a_text;
  465. text_data = interp_ex->a_text + interp_ex->a_data;
  466. current->mm->end_data = text_data;
  467. current->mm->brk = interp_ex->a_bss + text_data;
  468. switch (N_MAGIC(*interp_ex)) {
  469. case OMAGIC:
  470. offset = 32;
  471. addr = (char __user *)0;
  472. break;
  473. case ZMAGIC:
  474. case QMAGIC:
  475. offset = N_TXTOFF(*interp_ex);
  476. addr = (char __user *)N_TXTADDR(*interp_ex);
  477. break;
  478. default:
  479. goto out;
  480. }
  481. down_write(&current->mm->mmap_sem);
  482. do_brk(0, text_data);
  483. up_write(&current->mm->mmap_sem);
  484. if (!interpreter->f_op || !interpreter->f_op->read)
  485. goto out;
  486. if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
  487. goto out;
  488. flush_icache_range((unsigned long)addr,
  489. (unsigned long)addr + text_data);
  490. down_write(&current->mm->mmap_sem);
  491. do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
  492. interp_ex->a_bss);
  493. up_write(&current->mm->mmap_sem);
  494. elf_entry = interp_ex->a_entry;
  495. out:
  496. return elf_entry;
  497. }
  498. /*
  499. * These are the functions used to load ELF style executables and shared
  500. * libraries. There is no binary dependent code anywhere else.
  501. */
  502. #define INTERPRETER_NONE 0
  503. #define INTERPRETER_AOUT 1
  504. #define INTERPRETER_ELF 2
  505. #ifndef STACK_RND_MASK
  506. #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
  507. #endif
  508. static unsigned long randomize_stack_top(unsigned long stack_top)
  509. {
  510. unsigned int random_variable = 0;
  511. if ((current->flags & PF_RANDOMIZE) &&
  512. !(current->personality & ADDR_NO_RANDOMIZE)) {
  513. random_variable = get_random_int() & STACK_RND_MASK;
  514. random_variable <<= PAGE_SHIFT;
  515. }
  516. #ifdef CONFIG_STACK_GROWSUP
  517. return PAGE_ALIGN(stack_top) + random_variable;
  518. #else
  519. return PAGE_ALIGN(stack_top) - random_variable;
  520. #endif
  521. }
  522. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
  523. {
  524. struct file *interpreter = NULL; /* to shut gcc up */
  525. unsigned long load_addr = 0, load_bias = 0;
  526. int load_addr_set = 0;
  527. char * elf_interpreter = NULL;
  528. unsigned int interpreter_type = INTERPRETER_NONE;
  529. unsigned long error;
  530. struct elf_phdr *elf_ppnt, *elf_phdata;
  531. unsigned long elf_bss, elf_brk;
  532. int elf_exec_fileno;
  533. int retval, i;
  534. unsigned int size;
  535. unsigned long elf_entry;
  536. unsigned long interp_load_addr = 0;
  537. unsigned long start_code, end_code, start_data, end_data;
  538. unsigned long reloc_func_desc = 0;
  539. char passed_fileno[6];
  540. struct files_struct *files;
  541. int executable_stack = EXSTACK_DEFAULT;
  542. unsigned long def_flags = 0;
  543. struct {
  544. struct elfhdr elf_ex;
  545. struct elfhdr interp_elf_ex;
  546. struct exec interp_ex;
  547. } *loc;
  548. loc = kmalloc(sizeof(*loc), GFP_KERNEL);
  549. if (!loc) {
  550. retval = -ENOMEM;
  551. goto out_ret;
  552. }
  553. /* Get the exec-header */
  554. loc->elf_ex = *((struct elfhdr *)bprm->buf);
  555. retval = -ENOEXEC;
  556. /* First of all, some simple consistency checks */
  557. if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  558. goto out;
  559. if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
  560. goto out;
  561. if (!elf_check_arch(&loc->elf_ex))
  562. goto out;
  563. if (!bprm->file->f_op||!bprm->file->f_op->mmap)
  564. goto out;
  565. /* Now read in all of the header information */
  566. if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
  567. goto out;
  568. if (loc->elf_ex.e_phnum < 1 ||
  569. loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
  570. goto out;
  571. size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
  572. retval = -ENOMEM;
  573. elf_phdata = kmalloc(size, GFP_KERNEL);
  574. if (!elf_phdata)
  575. goto out;
  576. retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
  577. (char *)elf_phdata, size);
  578. if (retval != size) {
  579. if (retval >= 0)
  580. retval = -EIO;
  581. goto out_free_ph;
  582. }
  583. files = current->files; /* Refcounted so ok */
  584. retval = unshare_files();
  585. if (retval < 0)
  586. goto out_free_ph;
  587. if (files == current->files) {
  588. put_files_struct(files);
  589. files = NULL;
  590. }
  591. /* exec will make our files private anyway, but for the a.out
  592. loader stuff we need to do it earlier */
  593. retval = get_unused_fd();
  594. if (retval < 0)
  595. goto out_free_fh;
  596. get_file(bprm->file);
  597. fd_install(elf_exec_fileno = retval, bprm->file);
  598. elf_ppnt = elf_phdata;
  599. elf_bss = 0;
  600. elf_brk = 0;
  601. start_code = ~0UL;
  602. end_code = 0;
  603. start_data = 0;
  604. end_data = 0;
  605. for (i = 0; i < loc->elf_ex.e_phnum; i++) {
  606. if (elf_ppnt->p_type == PT_INTERP) {
  607. /* This is the program interpreter used for
  608. * shared libraries - for now assume that this
  609. * is an a.out format binary
  610. */
  611. retval = -ENOEXEC;
  612. if (elf_ppnt->p_filesz > PATH_MAX ||
  613. elf_ppnt->p_filesz < 2)
  614. goto out_free_file;
  615. retval = -ENOMEM;
  616. elf_interpreter = kmalloc(elf_ppnt->p_filesz,
  617. GFP_KERNEL);
  618. if (!elf_interpreter)
  619. goto out_free_file;
  620. retval = kernel_read(bprm->file, elf_ppnt->p_offset,
  621. elf_interpreter,
  622. elf_ppnt->p_filesz);
  623. if (retval != elf_ppnt->p_filesz) {
  624. if (retval >= 0)
  625. retval = -EIO;
  626. goto out_free_interp;
  627. }
  628. /* make sure path is NULL terminated */
  629. retval = -ENOEXEC;
  630. if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
  631. goto out_free_interp;
  632. /*
  633. * The early SET_PERSONALITY here is so that the lookup
  634. * for the interpreter happens in the namespace of the
  635. * to-be-execed image. SET_PERSONALITY can select an
  636. * alternate root.
  637. *
  638. * However, SET_PERSONALITY is NOT allowed to switch
  639. * this task into the new images's memory mapping
  640. * policy - that is, TASK_SIZE must still evaluate to
  641. * that which is appropriate to the execing application.
  642. * This is because exit_mmap() needs to have TASK_SIZE
  643. * evaluate to the size of the old image.
  644. *
  645. * So if (say) a 64-bit application is execing a 32-bit
  646. * application it is the architecture's responsibility
  647. * to defer changing the value of TASK_SIZE until the
  648. * switch really is going to happen - do this in
  649. * flush_thread(). - akpm
  650. */
  651. SET_PERSONALITY(loc->elf_ex, 0);
  652. interpreter = open_exec(elf_interpreter);
  653. retval = PTR_ERR(interpreter);
  654. if (IS_ERR(interpreter))
  655. goto out_free_interp;
  656. /*
  657. * If the binary is not readable then enforce
  658. * mm->dumpable = 0 regardless of the interpreter's
  659. * permissions.
  660. */
  661. if (file_permission(interpreter, MAY_READ) < 0)
  662. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  663. retval = kernel_read(interpreter, 0, bprm->buf,
  664. BINPRM_BUF_SIZE);
  665. if (retval != BINPRM_BUF_SIZE) {
  666. if (retval >= 0)
  667. retval = -EIO;
  668. goto out_free_dentry;
  669. }
  670. /* Get the exec headers */
  671. loc->interp_ex = *((struct exec *)bprm->buf);
  672. loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
  673. break;
  674. }
  675. elf_ppnt++;
  676. }
  677. elf_ppnt = elf_phdata;
  678. for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
  679. if (elf_ppnt->p_type == PT_GNU_STACK) {
  680. if (elf_ppnt->p_flags & PF_X)
  681. executable_stack = EXSTACK_ENABLE_X;
  682. else
  683. executable_stack = EXSTACK_DISABLE_X;
  684. break;
  685. }
  686. /* Some simple consistency checks for the interpreter */
  687. if (elf_interpreter) {
  688. static int warn;
  689. interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
  690. /* Now figure out which format our binary is */
  691. if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
  692. (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
  693. (N_MAGIC(loc->interp_ex) != QMAGIC))
  694. interpreter_type = INTERPRETER_ELF;
  695. if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  696. interpreter_type &= ~INTERPRETER_ELF;
  697. if (interpreter_type == INTERPRETER_AOUT && warn < 10) {
  698. printk(KERN_WARNING "a.out ELF interpreter %s is "
  699. "deprecated and will not be supported "
  700. "after Linux 2.6.25\n", elf_interpreter);
  701. warn++;
  702. }
  703. retval = -ELIBBAD;
  704. if (!interpreter_type)
  705. goto out_free_dentry;
  706. /* Make sure only one type was selected */
  707. if ((interpreter_type & INTERPRETER_ELF) &&
  708. interpreter_type != INTERPRETER_ELF) {
  709. // FIXME - ratelimit this before re-enabling
  710. // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
  711. interpreter_type = INTERPRETER_ELF;
  712. }
  713. /* Verify the interpreter has a valid arch */
  714. if ((interpreter_type == INTERPRETER_ELF) &&
  715. !elf_check_arch(&loc->interp_elf_ex))
  716. goto out_free_dentry;
  717. } else {
  718. /* Executables without an interpreter also need a personality */
  719. SET_PERSONALITY(loc->elf_ex, 0);
  720. }
  721. /* OK, we are done with that, now set up the arg stuff,
  722. and then start this sucker up */
  723. if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
  724. char *passed_p = passed_fileno;
  725. sprintf(passed_fileno, "%d", elf_exec_fileno);
  726. if (elf_interpreter) {
  727. retval = copy_strings_kernel(1, &passed_p, bprm);
  728. if (retval)
  729. goto out_free_dentry;
  730. bprm->argc++;
  731. }
  732. }
  733. /* Flush all traces of the currently running executable */
  734. retval = flush_old_exec(bprm);
  735. if (retval)
  736. goto out_free_dentry;
  737. /* Discard our unneeded old files struct */
  738. if (files) {
  739. put_files_struct(files);
  740. files = NULL;
  741. }
  742. /* OK, This is the point of no return */
  743. current->flags &= ~PF_FORKNOEXEC;
  744. current->mm->def_flags = def_flags;
  745. /* Do this immediately, since STACK_TOP as used in setup_arg_pages
  746. may depend on the personality. */
  747. SET_PERSONALITY(loc->elf_ex, 0);
  748. if (elf_read_implies_exec(loc->elf_ex, executable_stack))
  749. current->personality |= READ_IMPLIES_EXEC;
  750. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  751. current->flags |= PF_RANDOMIZE;
  752. arch_pick_mmap_layout(current->mm);
  753. /* Do this so that we can load the interpreter, if need be. We will
  754. change some of these later */
  755. current->mm->free_area_cache = current->mm->mmap_base;
  756. current->mm->cached_hole_size = 0;
  757. retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
  758. executable_stack);
  759. if (retval < 0) {
  760. send_sig(SIGKILL, current, 0);
  761. goto out_free_dentry;
  762. }
  763. current->mm->start_stack = bprm->p;
  764. /* Now we do a little grungy work by mmaping the ELF image into
  765. the correct location in memory. */
  766. for(i = 0, elf_ppnt = elf_phdata;
  767. i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
  768. int elf_prot = 0, elf_flags;
  769. unsigned long k, vaddr;
  770. if (elf_ppnt->p_type != PT_LOAD)
  771. continue;
  772. if (unlikely (elf_brk > elf_bss)) {
  773. unsigned long nbyte;
  774. /* There was a PT_LOAD segment with p_memsz > p_filesz
  775. before this one. Map anonymous pages, if needed,
  776. and clear the area. */
  777. retval = set_brk (elf_bss + load_bias,
  778. elf_brk + load_bias);
  779. if (retval) {
  780. send_sig(SIGKILL, current, 0);
  781. goto out_free_dentry;
  782. }
  783. nbyte = ELF_PAGEOFFSET(elf_bss);
  784. if (nbyte) {
  785. nbyte = ELF_MIN_ALIGN - nbyte;
  786. if (nbyte > elf_brk - elf_bss)
  787. nbyte = elf_brk - elf_bss;
  788. if (clear_user((void __user *)elf_bss +
  789. load_bias, nbyte)) {
  790. /*
  791. * This bss-zeroing can fail if the ELF
  792. * file specifies odd protections. So
  793. * we don't check the return value
  794. */
  795. }
  796. }
  797. }
  798. if (elf_ppnt->p_flags & PF_R)
  799. elf_prot |= PROT_READ;
  800. if (elf_ppnt->p_flags & PF_W)
  801. elf_prot |= PROT_WRITE;
  802. if (elf_ppnt->p_flags & PF_X)
  803. elf_prot |= PROT_EXEC;
  804. elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
  805. vaddr = elf_ppnt->p_vaddr;
  806. if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
  807. elf_flags |= MAP_FIXED;
  808. } else if (loc->elf_ex.e_type == ET_DYN) {
  809. /* Try and get dynamic programs out of the way of the
  810. * default mmap base, as well as whatever program they
  811. * might try to exec. This is because the brk will
  812. * follow the loader, and is not movable. */
  813. #ifdef CONFIG_X86
  814. load_bias = 0;
  815. #else
  816. load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
  817. #endif
  818. }
  819. error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
  820. elf_prot, elf_flags, 0);
  821. if (BAD_ADDR(error)) {
  822. send_sig(SIGKILL, current, 0);
  823. retval = IS_ERR((void *)error) ?
  824. PTR_ERR((void*)error) : -EINVAL;
  825. goto out_free_dentry;
  826. }
  827. if (!load_addr_set) {
  828. load_addr_set = 1;
  829. load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
  830. if (loc->elf_ex.e_type == ET_DYN) {
  831. load_bias += error -
  832. ELF_PAGESTART(load_bias + vaddr);
  833. load_addr += load_bias;
  834. reloc_func_desc = load_bias;
  835. }
  836. }
  837. k = elf_ppnt->p_vaddr;
  838. if (k < start_code)
  839. start_code = k;
  840. if (start_data < k)
  841. start_data = k;
  842. /*
  843. * Check to see if the section's size will overflow the
  844. * allowed task size. Note that p_filesz must always be
  845. * <= p_memsz so it is only necessary to check p_memsz.
  846. */
  847. if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
  848. elf_ppnt->p_memsz > TASK_SIZE ||
  849. TASK_SIZE - elf_ppnt->p_memsz < k) {
  850. /* set_brk can never work. Avoid overflows. */
  851. send_sig(SIGKILL, current, 0);
  852. retval = -EINVAL;
  853. goto out_free_dentry;
  854. }
  855. k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  856. if (k > elf_bss)
  857. elf_bss = k;
  858. if ((elf_ppnt->p_flags & PF_X) && end_code < k)
  859. end_code = k;
  860. if (end_data < k)
  861. end_data = k;
  862. k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  863. if (k > elf_brk)
  864. elf_brk = k;
  865. }
  866. loc->elf_ex.e_entry += load_bias;
  867. elf_bss += load_bias;
  868. elf_brk += load_bias;
  869. start_code += load_bias;
  870. end_code += load_bias;
  871. start_data += load_bias;
  872. end_data += load_bias;
  873. /* Calling set_brk effectively mmaps the pages that we need
  874. * for the bss and break sections. We must do this before
  875. * mapping in the interpreter, to make sure it doesn't wind
  876. * up getting placed where the bss needs to go.
  877. */
  878. retval = set_brk(elf_bss, elf_brk);
  879. if (retval) {
  880. send_sig(SIGKILL, current, 0);
  881. goto out_free_dentry;
  882. }
  883. if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
  884. send_sig(SIGSEGV, current, 0);
  885. retval = -EFAULT; /* Nobody gets to see this, but.. */
  886. goto out_free_dentry;
  887. }
  888. if (elf_interpreter) {
  889. if (interpreter_type == INTERPRETER_AOUT) {
  890. elf_entry = load_aout_interp(&loc->interp_ex,
  891. interpreter);
  892. } else {
  893. unsigned long uninitialized_var(interp_map_addr);
  894. elf_entry = load_elf_interp(&loc->interp_elf_ex,
  895. interpreter,
  896. &interp_map_addr,
  897. load_bias);
  898. if (!IS_ERR((void *)elf_entry)) {
  899. /*
  900. * load_elf_interp() returns relocation
  901. * adjustment
  902. */
  903. interp_load_addr = elf_entry;
  904. elf_entry += loc->interp_elf_ex.e_entry;
  905. }
  906. }
  907. if (BAD_ADDR(elf_entry)) {
  908. force_sig(SIGSEGV, current);
  909. retval = IS_ERR((void *)elf_entry) ?
  910. (int)elf_entry : -EINVAL;
  911. goto out_free_dentry;
  912. }
  913. reloc_func_desc = interp_load_addr;
  914. allow_write_access(interpreter);
  915. fput(interpreter);
  916. kfree(elf_interpreter);
  917. } else {
  918. elf_entry = loc->elf_ex.e_entry;
  919. if (BAD_ADDR(elf_entry)) {
  920. force_sig(SIGSEGV, current);
  921. retval = -EINVAL;
  922. goto out_free_dentry;
  923. }
  924. }
  925. kfree(elf_phdata);
  926. if (interpreter_type != INTERPRETER_AOUT)
  927. sys_close(elf_exec_fileno);
  928. set_binfmt(&elf_format);
  929. #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
  930. retval = arch_setup_additional_pages(bprm, executable_stack);
  931. if (retval < 0) {
  932. send_sig(SIGKILL, current, 0);
  933. goto out;
  934. }
  935. #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
  936. compute_creds(bprm);
  937. current->flags &= ~PF_FORKNOEXEC;
  938. retval = create_elf_tables(bprm, &loc->elf_ex,
  939. (interpreter_type == INTERPRETER_AOUT),
  940. load_addr, interp_load_addr);
  941. if (retval < 0) {
  942. send_sig(SIGKILL, current, 0);
  943. goto out;
  944. }
  945. /* N.B. passed_fileno might not be initialized? */
  946. if (interpreter_type == INTERPRETER_AOUT)
  947. current->mm->arg_start += strlen(passed_fileno) + 1;
  948. current->mm->end_code = end_code;
  949. current->mm->start_code = start_code;
  950. current->mm->start_data = start_data;
  951. current->mm->end_data = end_data;
  952. current->mm->start_stack = bprm->p;
  953. #ifdef arch_randomize_brk
  954. if (current->flags & PF_RANDOMIZE)
  955. current->mm->brk = current->mm->start_brk =
  956. arch_randomize_brk(current->mm);
  957. #endif
  958. if (current->personality & MMAP_PAGE_ZERO) {
  959. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  960. and some applications "depend" upon this behavior.
  961. Since we do not have the power to recompile these, we
  962. emulate the SVr4 behavior. Sigh. */
  963. down_write(&current->mm->mmap_sem);
  964. error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
  965. MAP_FIXED | MAP_PRIVATE, 0);
  966. up_write(&current->mm->mmap_sem);
  967. }
  968. #ifdef ELF_PLAT_INIT
  969. /*
  970. * The ABI may specify that certain registers be set up in special
  971. * ways (on i386 %edx is the address of a DT_FINI function, for
  972. * example. In addition, it may also specify (eg, PowerPC64 ELF)
  973. * that the e_entry field is the address of the function descriptor
  974. * for the startup routine, rather than the address of the startup
  975. * routine itself. This macro performs whatever initialization to
  976. * the regs structure is required as well as any relocations to the
  977. * function descriptor entries when executing dynamically links apps.
  978. */
  979. ELF_PLAT_INIT(regs, reloc_func_desc);
  980. #endif
  981. start_thread(regs, elf_entry, bprm->p);
  982. if (unlikely(current->ptrace & PT_PTRACED)) {
  983. if (current->ptrace & PT_TRACE_EXEC)
  984. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  985. else
  986. send_sig(SIGTRAP, current, 0);
  987. }
  988. retval = 0;
  989. out:
  990. kfree(loc);
  991. out_ret:
  992. return retval;
  993. /* error cleanup */
  994. out_free_dentry:
  995. allow_write_access(interpreter);
  996. if (interpreter)
  997. fput(interpreter);
  998. out_free_interp:
  999. kfree(elf_interpreter);
  1000. out_free_file:
  1001. sys_close(elf_exec_fileno);
  1002. out_free_fh:
  1003. if (files)
  1004. reset_files_struct(current, files);
  1005. out_free_ph:
  1006. kfree(elf_phdata);
  1007. goto out;
  1008. }
  1009. /* This is really simpleminded and specialized - we are loading an
  1010. a.out library that is given an ELF header. */
  1011. static int load_elf_library(struct file *file)
  1012. {
  1013. struct elf_phdr *elf_phdata;
  1014. struct elf_phdr *eppnt;
  1015. unsigned long elf_bss, bss, len;
  1016. int retval, error, i, j;
  1017. struct elfhdr elf_ex;
  1018. error = -ENOEXEC;
  1019. retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
  1020. if (retval != sizeof(elf_ex))
  1021. goto out;
  1022. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  1023. goto out;
  1024. /* First of all, some simple consistency checks */
  1025. if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
  1026. !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
  1027. goto out;
  1028. /* Now read in all of the header information */
  1029. j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
  1030. /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
  1031. error = -ENOMEM;
  1032. elf_phdata = kmalloc(j, GFP_KERNEL);
  1033. if (!elf_phdata)
  1034. goto out;
  1035. eppnt = elf_phdata;
  1036. error = -ENOEXEC;
  1037. retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
  1038. if (retval != j)
  1039. goto out_free_ph;
  1040. for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
  1041. if ((eppnt + i)->p_type == PT_LOAD)
  1042. j++;
  1043. if (j != 1)
  1044. goto out_free_ph;
  1045. while (eppnt->p_type != PT_LOAD)
  1046. eppnt++;
  1047. /* Now use mmap to map the library into memory. */
  1048. down_write(&current->mm->mmap_sem);
  1049. error = do_mmap(file,
  1050. ELF_PAGESTART(eppnt->p_vaddr),
  1051. (eppnt->p_filesz +
  1052. ELF_PAGEOFFSET(eppnt->p_vaddr)),
  1053. PROT_READ | PROT_WRITE | PROT_EXEC,
  1054. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  1055. (eppnt->p_offset -
  1056. ELF_PAGEOFFSET(eppnt->p_vaddr)));
  1057. up_write(&current->mm->mmap_sem);
  1058. if (error != ELF_PAGESTART(eppnt->p_vaddr))
  1059. goto out_free_ph;
  1060. elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
  1061. if (padzero(elf_bss)) {
  1062. error = -EFAULT;
  1063. goto out_free_ph;
  1064. }
  1065. len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
  1066. ELF_MIN_ALIGN - 1);
  1067. bss = eppnt->p_memsz + eppnt->p_vaddr;
  1068. if (bss > len) {
  1069. down_write(&current->mm->mmap_sem);
  1070. do_brk(len, bss - len);
  1071. up_write(&current->mm->mmap_sem);
  1072. }
  1073. error = 0;
  1074. out_free_ph:
  1075. kfree(elf_phdata);
  1076. out:
  1077. return error;
  1078. }
  1079. /*
  1080. * Note that some platforms still use traditional core dumps and not
  1081. * the ELF core dump. Each platform can select it as appropriate.
  1082. */
  1083. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  1084. /*
  1085. * ELF core dumper
  1086. *
  1087. * Modelled on fs/exec.c:aout_core_dump()
  1088. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  1089. */
  1090. /*
  1091. * These are the only things you should do on a core-file: use only these
  1092. * functions to write out all the necessary info.
  1093. */
  1094. static int dump_write(struct file *file, const void *addr, int nr)
  1095. {
  1096. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1097. }
  1098. static int dump_seek(struct file *file, loff_t off)
  1099. {
  1100. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1101. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1102. return 0;
  1103. } else {
  1104. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1105. if (!buf)
  1106. return 0;
  1107. while (off > 0) {
  1108. unsigned long n = off;
  1109. if (n > PAGE_SIZE)
  1110. n = PAGE_SIZE;
  1111. if (!dump_write(file, buf, n))
  1112. return 0;
  1113. off -= n;
  1114. }
  1115. free_page((unsigned long)buf);
  1116. }
  1117. return 1;
  1118. }
  1119. /*
  1120. * Decide what to dump of a segment, part, all or none.
  1121. */
  1122. static unsigned long vma_dump_size(struct vm_area_struct *vma,
  1123. unsigned long mm_flags)
  1124. {
  1125. /* The vma can be set up to tell us the answer directly. */
  1126. if (vma->vm_flags & VM_ALWAYSDUMP)
  1127. goto whole;
  1128. /* Do not dump I/O mapped devices or special mappings */
  1129. if (vma->vm_flags & (VM_IO | VM_RESERVED))
  1130. return 0;
  1131. #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
  1132. /* By default, dump shared memory if mapped from an anonymous file. */
  1133. if (vma->vm_flags & VM_SHARED) {
  1134. if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
  1135. FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
  1136. goto whole;
  1137. return 0;
  1138. }
  1139. /* Dump segments that have been written to. */
  1140. if (vma->anon_vma && FILTER(ANON_PRIVATE))
  1141. goto whole;
  1142. if (vma->vm_file == NULL)
  1143. return 0;
  1144. if (FILTER(MAPPED_PRIVATE))
  1145. goto whole;
  1146. /*
  1147. * If this looks like the beginning of a DSO or executable mapping,
  1148. * check for an ELF header. If we find one, dump the first page to
  1149. * aid in determining what was mapped here.
  1150. */
  1151. if (FILTER(ELF_HEADERS) && vma->vm_file != NULL && vma->vm_pgoff == 0) {
  1152. u32 __user *header = (u32 __user *) vma->vm_start;
  1153. u32 word;
  1154. /*
  1155. * Doing it this way gets the constant folded by GCC.
  1156. */
  1157. union {
  1158. u32 cmp;
  1159. char elfmag[SELFMAG];
  1160. } magic;
  1161. BUILD_BUG_ON(SELFMAG != sizeof word);
  1162. magic.elfmag[EI_MAG0] = ELFMAG0;
  1163. magic.elfmag[EI_MAG1] = ELFMAG1;
  1164. magic.elfmag[EI_MAG2] = ELFMAG2;
  1165. magic.elfmag[EI_MAG3] = ELFMAG3;
  1166. if (get_user(word, header) == 0 && word == magic.cmp)
  1167. return PAGE_SIZE;
  1168. }
  1169. #undef FILTER
  1170. return 0;
  1171. whole:
  1172. return vma->vm_end - vma->vm_start;
  1173. }
  1174. /* An ELF note in memory */
  1175. struct memelfnote
  1176. {
  1177. const char *name;
  1178. int type;
  1179. unsigned int datasz;
  1180. void *data;
  1181. };
  1182. static int notesize(struct memelfnote *en)
  1183. {
  1184. int sz;
  1185. sz = sizeof(struct elf_note);
  1186. sz += roundup(strlen(en->name) + 1, 4);
  1187. sz += roundup(en->datasz, 4);
  1188. return sz;
  1189. }
  1190. #define DUMP_WRITE(addr, nr, foffset) \
  1191. do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
  1192. static int alignfile(struct file *file, loff_t *foffset)
  1193. {
  1194. static const char buf[4] = { 0, };
  1195. DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
  1196. return 1;
  1197. }
  1198. static int writenote(struct memelfnote *men, struct file *file,
  1199. loff_t *foffset)
  1200. {
  1201. struct elf_note en;
  1202. en.n_namesz = strlen(men->name) + 1;
  1203. en.n_descsz = men->datasz;
  1204. en.n_type = men->type;
  1205. DUMP_WRITE(&en, sizeof(en), foffset);
  1206. DUMP_WRITE(men->name, en.n_namesz, foffset);
  1207. if (!alignfile(file, foffset))
  1208. return 0;
  1209. DUMP_WRITE(men->data, men->datasz, foffset);
  1210. if (!alignfile(file, foffset))
  1211. return 0;
  1212. return 1;
  1213. }
  1214. #undef DUMP_WRITE
  1215. #define DUMP_WRITE(addr, nr) \
  1216. if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
  1217. goto end_coredump;
  1218. #define DUMP_SEEK(off) \
  1219. if (!dump_seek(file, (off))) \
  1220. goto end_coredump;
  1221. static void fill_elf_header(struct elfhdr *elf, int segs,
  1222. u16 machine, u32 flags, u8 osabi)
  1223. {
  1224. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1225. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1226. elf->e_ident[EI_DATA] = ELF_DATA;
  1227. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1228. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1229. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1230. elf->e_type = ET_CORE;
  1231. elf->e_machine = machine;
  1232. elf->e_version = EV_CURRENT;
  1233. elf->e_entry = 0;
  1234. elf->e_phoff = sizeof(struct elfhdr);
  1235. elf->e_shoff = 0;
  1236. elf->e_flags = flags;
  1237. elf->e_ehsize = sizeof(struct elfhdr);
  1238. elf->e_phentsize = sizeof(struct elf_phdr);
  1239. elf->e_phnum = segs;
  1240. elf->e_shentsize = 0;
  1241. elf->e_shnum = 0;
  1242. elf->e_shstrndx = 0;
  1243. return;
  1244. }
  1245. static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1246. {
  1247. phdr->p_type = PT_NOTE;
  1248. phdr->p_offset = offset;
  1249. phdr->p_vaddr = 0;
  1250. phdr->p_paddr = 0;
  1251. phdr->p_filesz = sz;
  1252. phdr->p_memsz = 0;
  1253. phdr->p_flags = 0;
  1254. phdr->p_align = 0;
  1255. return;
  1256. }
  1257. static void fill_note(struct memelfnote *note, const char *name, int type,
  1258. unsigned int sz, void *data)
  1259. {
  1260. note->name = name;
  1261. note->type = type;
  1262. note->datasz = sz;
  1263. note->data = data;
  1264. return;
  1265. }
  1266. /*
  1267. * fill up all the fields in prstatus from the given task struct, except
  1268. * registers which need to be filled up separately.
  1269. */
  1270. static void fill_prstatus(struct elf_prstatus *prstatus,
  1271. struct task_struct *p, long signr)
  1272. {
  1273. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1274. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1275. prstatus->pr_sighold = p->blocked.sig[0];
  1276. prstatus->pr_pid = task_pid_vnr(p);
  1277. prstatus->pr_ppid = task_pid_vnr(p->real_parent);
  1278. prstatus->pr_pgrp = task_pgrp_vnr(p);
  1279. prstatus->pr_sid = task_session_vnr(p);
  1280. if (thread_group_leader(p)) {
  1281. /*
  1282. * This is the record for the group leader. Add in the
  1283. * cumulative times of previous dead threads. This total
  1284. * won't include the time of each live thread whose state
  1285. * is included in the core dump. The final total reported
  1286. * to our parent process when it calls wait4 will include
  1287. * those sums as well as the little bit more time it takes
  1288. * this and each other thread to finish dying after the
  1289. * core dump synchronization phase.
  1290. */
  1291. cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
  1292. &prstatus->pr_utime);
  1293. cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
  1294. &prstatus->pr_stime);
  1295. } else {
  1296. cputime_to_timeval(p->utime, &prstatus->pr_utime);
  1297. cputime_to_timeval(p->stime, &prstatus->pr_stime);
  1298. }
  1299. cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
  1300. cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
  1301. }
  1302. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1303. struct mm_struct *mm)
  1304. {
  1305. unsigned int i, len;
  1306. /* first copy the parameters from user space */
  1307. memset(psinfo, 0, sizeof(struct elf_prpsinfo));
  1308. len = mm->arg_end - mm->arg_start;
  1309. if (len >= ELF_PRARGSZ)
  1310. len = ELF_PRARGSZ-1;
  1311. if (copy_from_user(&psinfo->pr_psargs,
  1312. (const char __user *)mm->arg_start, len))
  1313. return -EFAULT;
  1314. for(i = 0; i < len; i++)
  1315. if (psinfo->pr_psargs[i] == 0)
  1316. psinfo->pr_psargs[i] = ' ';
  1317. psinfo->pr_psargs[len] = 0;
  1318. psinfo->pr_pid = task_pid_vnr(p);
  1319. psinfo->pr_ppid = task_pid_vnr(p->real_parent);
  1320. psinfo->pr_pgrp = task_pgrp_vnr(p);
  1321. psinfo->pr_sid = task_session_vnr(p);
  1322. i = p->state ? ffz(~p->state) + 1 : 0;
  1323. psinfo->pr_state = i;
  1324. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1325. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1326. psinfo->pr_nice = task_nice(p);
  1327. psinfo->pr_flag = p->flags;
  1328. SET_UID(psinfo->pr_uid, p->uid);
  1329. SET_GID(psinfo->pr_gid, p->gid);
  1330. strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
  1331. return 0;
  1332. }
  1333. static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
  1334. {
  1335. elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
  1336. int i = 0;
  1337. do
  1338. i += 2;
  1339. while (auxv[i - 2] != AT_NULL);
  1340. fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv);
  1341. }
  1342. #ifdef CORE_DUMP_USE_REGSET
  1343. #include <linux/regset.h>
  1344. struct elf_thread_core_info {
  1345. struct elf_thread_core_info *next;
  1346. struct task_struct *task;
  1347. struct elf_prstatus prstatus;
  1348. struct memelfnote notes[0];
  1349. };
  1350. struct elf_note_info {
  1351. struct elf_thread_core_info *thread;
  1352. struct memelfnote psinfo;
  1353. struct memelfnote auxv;
  1354. size_t size;
  1355. int thread_notes;
  1356. };
  1357. static int fill_thread_core_info(struct elf_thread_core_info *t,
  1358. const struct user_regset_view *view,
  1359. long signr, size_t *total)
  1360. {
  1361. unsigned int i;
  1362. /*
  1363. * NT_PRSTATUS is the one special case, because the regset data
  1364. * goes into the pr_reg field inside the note contents, rather
  1365. * than being the whole note contents. We fill the reset in here.
  1366. * We assume that regset 0 is NT_PRSTATUS.
  1367. */
  1368. fill_prstatus(&t->prstatus, t->task, signr);
  1369. (void) view->regsets[0].get(t->task, &view->regsets[0],
  1370. 0, sizeof(t->prstatus.pr_reg),
  1371. &t->prstatus.pr_reg, NULL);
  1372. fill_note(&t->notes[0], "CORE", NT_PRSTATUS,
  1373. sizeof(t->prstatus), &t->prstatus);
  1374. *total += notesize(&t->notes[0]);
  1375. /*
  1376. * Each other regset might generate a note too. For each regset
  1377. * that has no core_note_type or is inactive, we leave t->notes[i]
  1378. * all zero and we'll know to skip writing it later.
  1379. */
  1380. for (i = 1; i < view->n; ++i) {
  1381. const struct user_regset *regset = &view->regsets[i];
  1382. if (regset->core_note_type &&
  1383. (!regset->active || regset->active(t->task, regset))) {
  1384. int ret;
  1385. size_t size = regset->n * regset->size;
  1386. void *data = kmalloc(size, GFP_KERNEL);
  1387. if (unlikely(!data))
  1388. return 0;
  1389. ret = regset->get(t->task, regset,
  1390. 0, size, data, NULL);
  1391. if (unlikely(ret))
  1392. kfree(data);
  1393. else {
  1394. if (regset->core_note_type != NT_PRFPREG)
  1395. fill_note(&t->notes[i], "LINUX",
  1396. regset->core_note_type,
  1397. size, data);
  1398. else {
  1399. t->prstatus.pr_fpvalid = 1;
  1400. fill_note(&t->notes[i], "CORE",
  1401. NT_PRFPREG, size, data);
  1402. }
  1403. *total += notesize(&t->notes[i]);
  1404. }
  1405. }
  1406. }
  1407. return 1;
  1408. }
  1409. static int fill_note_info(struct elfhdr *elf, int phdrs,
  1410. struct elf_note_info *info,
  1411. long signr, struct pt_regs *regs)
  1412. {
  1413. struct task_struct *dump_task = current;
  1414. const struct user_regset_view *view = task_user_regset_view(dump_task);
  1415. struct elf_thread_core_info *t;
  1416. struct elf_prpsinfo *psinfo;
  1417. struct task_struct *g, *p;
  1418. unsigned int i;
  1419. info->size = 0;
  1420. info->thread = NULL;
  1421. psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
  1422. fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
  1423. if (psinfo == NULL)
  1424. return 0;
  1425. /*
  1426. * Figure out how many notes we're going to need for each thread.
  1427. */
  1428. info->thread_notes = 0;
  1429. for (i = 0; i < view->n; ++i)
  1430. if (view->regsets[i].core_note_type != 0)
  1431. ++info->thread_notes;
  1432. /*
  1433. * Sanity check. We rely on regset 0 being in NT_PRSTATUS,
  1434. * since it is our one special case.
  1435. */
  1436. if (unlikely(info->thread_notes == 0) ||
  1437. unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) {
  1438. WARN_ON(1);
  1439. return 0;
  1440. }
  1441. /*
  1442. * Initialize the ELF file header.
  1443. */
  1444. fill_elf_header(elf, phdrs,
  1445. view->e_machine, view->e_flags, view->ei_osabi);
  1446. /*
  1447. * Allocate a structure for each thread.
  1448. */
  1449. rcu_read_lock();
  1450. do_each_thread(g, p)
  1451. if (p->mm == dump_task->mm) {
  1452. t = kzalloc(offsetof(struct elf_thread_core_info,
  1453. notes[info->thread_notes]),
  1454. GFP_ATOMIC);
  1455. if (unlikely(!t)) {
  1456. rcu_read_unlock();
  1457. return 0;
  1458. }
  1459. t->task = p;
  1460. if (p == dump_task || !info->thread) {
  1461. t->next = info->thread;
  1462. info->thread = t;
  1463. } else {
  1464. /*
  1465. * Make sure to keep the original task at
  1466. * the head of the list.
  1467. */
  1468. t->next = info->thread->next;
  1469. info->thread->next = t;
  1470. }
  1471. }
  1472. while_each_thread(g, p);
  1473. rcu_read_unlock();
  1474. /*
  1475. * Now fill in each thread's information.
  1476. */
  1477. for (t = info->thread; t != NULL; t = t->next)
  1478. if (!fill_thread_core_info(t, view, signr, &info->size))
  1479. return 0;
  1480. /*
  1481. * Fill in the two process-wide notes.
  1482. */
  1483. fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
  1484. info->size += notesize(&info->psinfo);
  1485. fill_auxv_note(&info->auxv, current->mm);
  1486. info->size += notesize(&info->auxv);
  1487. return 1;
  1488. }
  1489. static size_t get_note_info_size(struct elf_note_info *info)
  1490. {
  1491. return info->size;
  1492. }
  1493. /*
  1494. * Write all the notes for each thread. When writing the first thread, the
  1495. * process-wide notes are interleaved after the first thread-specific note.
  1496. */
  1497. static int write_note_info(struct elf_note_info *info,
  1498. struct file *file, loff_t *foffset)
  1499. {
  1500. bool first = 1;
  1501. struct elf_thread_core_info *t = info->thread;
  1502. do {
  1503. int i;
  1504. if (!writenote(&t->notes[0], file, foffset))
  1505. return 0;
  1506. if (first && !writenote(&info->psinfo, file, foffset))
  1507. return 0;
  1508. if (first && !writenote(&info->auxv, file, foffset))
  1509. return 0;
  1510. for (i = 1; i < info->thread_notes; ++i)
  1511. if (t->notes[i].data &&
  1512. !writenote(&t->notes[i], file, foffset))
  1513. return 0;
  1514. first = 0;
  1515. t = t->next;
  1516. } while (t);
  1517. return 1;
  1518. }
  1519. static void free_note_info(struct elf_note_info *info)
  1520. {
  1521. struct elf_thread_core_info *threads = info->thread;
  1522. while (threads) {
  1523. unsigned int i;
  1524. struct elf_thread_core_info *t = threads;
  1525. threads = t->next;
  1526. WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus);
  1527. for (i = 1; i < info->thread_notes; ++i)
  1528. kfree(t->notes[i].data);
  1529. kfree(t);
  1530. }
  1531. kfree(info->psinfo.data);
  1532. }
  1533. #else
  1534. /* Here is the structure in which status of each thread is captured. */
  1535. struct elf_thread_status
  1536. {
  1537. struct list_head list;
  1538. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  1539. elf_fpregset_t fpu; /* NT_PRFPREG */
  1540. struct task_struct *thread;
  1541. #ifdef ELF_CORE_COPY_XFPREGS
  1542. elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
  1543. #endif
  1544. struct memelfnote notes[3];
  1545. int num_notes;
  1546. };
  1547. /*
  1548. * In order to add the specific thread information for the elf file format,
  1549. * we need to keep a linked list of every threads pr_status and then create
  1550. * a single section for them in the final core file.
  1551. */
  1552. static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
  1553. {
  1554. int sz = 0;
  1555. struct task_struct *p = t->thread;
  1556. t->num_notes = 0;
  1557. fill_prstatus(&t->prstatus, p, signr);
  1558. elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
  1559. fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
  1560. &(t->prstatus));
  1561. t->num_notes++;
  1562. sz += notesize(&t->notes[0]);
  1563. if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
  1564. &t->fpu))) {
  1565. fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
  1566. &(t->fpu));
  1567. t->num_notes++;
  1568. sz += notesize(&t->notes[1]);
  1569. }
  1570. #ifdef ELF_CORE_COPY_XFPREGS
  1571. if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
  1572. fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
  1573. sizeof(t->xfpu), &t->xfpu);
  1574. t->num_notes++;
  1575. sz += notesize(&t->notes[2]);
  1576. }
  1577. #endif
  1578. return sz;
  1579. }
  1580. struct elf_note_info {
  1581. struct memelfnote *notes;
  1582. struct elf_prstatus *prstatus; /* NT_PRSTATUS */
  1583. struct elf_prpsinfo *psinfo; /* NT_PRPSINFO */
  1584. struct list_head thread_list;
  1585. elf_fpregset_t *fpu;
  1586. #ifdef ELF_CORE_COPY_XFPREGS
  1587. elf_fpxregset_t *xfpu;
  1588. #endif
  1589. int thread_status_size;
  1590. int numnote;
  1591. };
  1592. static int fill_note_info(struct elfhdr *elf, int phdrs,
  1593. struct elf_note_info *info,
  1594. long signr, struct pt_regs *regs)
  1595. {
  1596. #define NUM_NOTES 6
  1597. struct list_head *t;
  1598. struct task_struct *g, *p;
  1599. info->notes = NULL;
  1600. info->prstatus = NULL;
  1601. info->psinfo = NULL;
  1602. info->fpu = NULL;
  1603. #ifdef ELF_CORE_COPY_XFPREGS
  1604. info->xfpu = NULL;
  1605. #endif
  1606. INIT_LIST_HEAD(&info->thread_list);
  1607. info->notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote),
  1608. GFP_KERNEL);
  1609. if (!info->notes)
  1610. return 0;
  1611. info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL);
  1612. if (!info->psinfo)
  1613. return 0;
  1614. info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL);
  1615. if (!info->prstatus)
  1616. return 0;
  1617. info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL);
  1618. if (!info->fpu)
  1619. return 0;
  1620. #ifdef ELF_CORE_COPY_XFPREGS
  1621. info->xfpu = kmalloc(sizeof(*info->xfpu), GFP_KERNEL);
  1622. if (!info->xfpu)
  1623. return 0;
  1624. #endif
  1625. info->thread_status_size = 0;
  1626. if (signr) {
  1627. struct elf_thread_status *tmp;
  1628. rcu_read_lock();
  1629. do_each_thread(g, p)
  1630. if (current->mm == p->mm && current != p) {
  1631. tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
  1632. if (!tmp) {
  1633. rcu_read_unlock();
  1634. return 0;
  1635. }
  1636. tmp->thread = p;
  1637. list_add(&tmp->list, &info->thread_list);
  1638. }
  1639. while_each_thread(g, p);
  1640. rcu_read_unlock();
  1641. list_for_each(t, &info->thread_list) {
  1642. struct elf_thread_status *tmp;
  1643. int sz;
  1644. tmp = list_entry(t, struct elf_thread_status, list);
  1645. sz = elf_dump_thread_status(signr, tmp);
  1646. info->thread_status_size += sz;
  1647. }
  1648. }
  1649. /* now collect the dump for the current */
  1650. memset(info->prstatus, 0, sizeof(*info->prstatus));
  1651. fill_prstatus(info->prstatus, current, signr);
  1652. elf_core_copy_regs(&info->prstatus->pr_reg, regs);
  1653. /* Set up header */
  1654. fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS, ELF_OSABI);
  1655. /*
  1656. * Set up the notes in similar form to SVR4 core dumps made
  1657. * with info from their /proc.
  1658. */
  1659. fill_note(info->notes + 0, "CORE", NT_PRSTATUS,
  1660. sizeof(*info->prstatus), info->prstatus);
  1661. fill_psinfo(info->psinfo, current->group_leader, current->mm);
  1662. fill_note(info->notes + 1, "CORE", NT_PRPSINFO,
  1663. sizeof(*info->psinfo), info->psinfo);
  1664. info->numnote = 2;
  1665. fill_auxv_note(&info->notes[info->numnote++], current->mm);
  1666. /* Try to dump the FPU. */
  1667. info->prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs,
  1668. info->fpu);
  1669. if (info->prstatus->pr_fpvalid)
  1670. fill_note(info->notes + info->numnote++,
  1671. "CORE", NT_PRFPREG, sizeof(*info->fpu), info->fpu);
  1672. #ifdef ELF_CORE_COPY_XFPREGS
  1673. if (elf_core_copy_task_xfpregs(current, info->xfpu))
  1674. fill_note(info->notes + info->numnote++,
  1675. "LINUX", ELF_CORE_XFPREG_TYPE,
  1676. sizeof(*info->xfpu), info->xfpu);
  1677. #endif
  1678. return 1;
  1679. #undef NUM_NOTES
  1680. }
  1681. static size_t get_note_info_size(struct elf_note_info *info)
  1682. {
  1683. int sz = 0;
  1684. int i;
  1685. for (i = 0; i < info->numnote; i++)
  1686. sz += notesize(info->notes + i);
  1687. sz += info->thread_status_size;
  1688. return sz;
  1689. }
  1690. static int write_note_info(struct elf_note_info *info,
  1691. struct file *file, loff_t *foffset)
  1692. {
  1693. int i;
  1694. struct list_head *t;
  1695. for (i = 0; i < info->numnote; i++)
  1696. if (!writenote(info->notes + i, file, foffset))
  1697. return 0;
  1698. /* write out the thread status notes section */
  1699. list_for_each(t, &info->thread_list) {
  1700. struct elf_thread_status *tmp =
  1701. list_entry(t, struct elf_thread_status, list);
  1702. for (i = 0; i < tmp->num_notes; i++)
  1703. if (!writenote(&tmp->notes[i], file, foffset))
  1704. return 0;
  1705. }
  1706. return 1;
  1707. }
  1708. static void free_note_info(struct elf_note_info *info)
  1709. {
  1710. while (!list_empty(&info->thread_list)) {
  1711. struct list_head *tmp = info->thread_list.next;
  1712. list_del(tmp);
  1713. kfree(list_entry(tmp, struct elf_thread_status, list));
  1714. }
  1715. kfree(info->prstatus);
  1716. kfree(info->psinfo);
  1717. kfree(info->notes);
  1718. kfree(info->fpu);
  1719. #ifdef ELF_CORE_COPY_XFPREGS
  1720. kfree(info->xfpu);
  1721. #endif
  1722. }
  1723. #endif
  1724. static struct vm_area_struct *first_vma(struct task_struct *tsk,
  1725. struct vm_area_struct *gate_vma)
  1726. {
  1727. struct vm_area_struct *ret = tsk->mm->mmap;
  1728. if (ret)
  1729. return ret;
  1730. return gate_vma;
  1731. }
  1732. /*
  1733. * Helper function for iterating across a vma list. It ensures that the caller
  1734. * will visit `gate_vma' prior to terminating the search.
  1735. */
  1736. static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
  1737. struct vm_area_struct *gate_vma)
  1738. {
  1739. struct vm_area_struct *ret;
  1740. ret = this_vma->vm_next;
  1741. if (ret)
  1742. return ret;
  1743. if (this_vma == gate_vma)
  1744. return NULL;
  1745. return gate_vma;
  1746. }
  1747. /*
  1748. * Actual dumper
  1749. *
  1750. * This is a two-pass process; first we find the offsets of the bits,
  1751. * and then they are actually written out. If we run out of core limit
  1752. * we just truncate.
  1753. */
  1754. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
  1755. {
  1756. int has_dumped = 0;
  1757. mm_segment_t fs;
  1758. int segs;
  1759. size_t size = 0;
  1760. struct vm_area_struct *vma, *gate_vma;
  1761. struct elfhdr *elf = NULL;
  1762. loff_t offset = 0, dataoff, foffset;
  1763. unsigned long mm_flags;
  1764. struct elf_note_info info;
  1765. /*
  1766. * We no longer stop all VM operations.
  1767. *
  1768. * This is because those proceses that could possibly change map_count
  1769. * or the mmap / vma pages are now blocked in do_exit on current
  1770. * finishing this core dump.
  1771. *
  1772. * Only ptrace can touch these memory addresses, but it doesn't change
  1773. * the map_count or the pages allocated. So no possibility of crashing
  1774. * exists while dumping the mm->vm_next areas to the core file.
  1775. */
  1776. /* alloc memory for large data structures: too large to be on stack */
  1777. elf = kmalloc(sizeof(*elf), GFP_KERNEL);
  1778. if (!elf)
  1779. goto cleanup;
  1780. segs = current->mm->map_count;
  1781. #ifdef ELF_CORE_EXTRA_PHDRS
  1782. segs += ELF_CORE_EXTRA_PHDRS;
  1783. #endif
  1784. gate_vma = get_gate_vma(current);
  1785. if (gate_vma != NULL)
  1786. segs++;
  1787. /*
  1788. * Collect all the non-memory information about the process for the
  1789. * notes. This also sets up the file header.
  1790. */
  1791. if (!fill_note_info(elf, segs + 1, /* including notes section */
  1792. &info, signr, regs))
  1793. goto cleanup;
  1794. has_dumped = 1;
  1795. current->flags |= PF_DUMPCORE;
  1796. fs = get_fs();
  1797. set_fs(KERNEL_DS);
  1798. DUMP_WRITE(elf, sizeof(*elf));
  1799. offset += sizeof(*elf); /* Elf header */
  1800. offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
  1801. foffset = offset;
  1802. /* Write notes phdr entry */
  1803. {
  1804. struct elf_phdr phdr;
  1805. size_t sz = get_note_info_size(&info);
  1806. sz += elf_coredump_extra_notes_size();
  1807. fill_elf_note_phdr(&phdr, sz, offset);
  1808. offset += sz;
  1809. DUMP_WRITE(&phdr, sizeof(phdr));
  1810. }
  1811. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1812. /*
  1813. * We must use the same mm->flags while dumping core to avoid
  1814. * inconsistency between the program headers and bodies, otherwise an
  1815. * unusable core file can be generated.
  1816. */
  1817. mm_flags = current->mm->flags;
  1818. /* Write program headers for segments dump */
  1819. for (vma = first_vma(current, gate_vma); vma != NULL;
  1820. vma = next_vma(vma, gate_vma)) {
  1821. struct elf_phdr phdr;
  1822. phdr.p_type = PT_LOAD;
  1823. phdr.p_offset = offset;
  1824. phdr.p_vaddr = vma->vm_start;
  1825. phdr.p_paddr = 0;
  1826. phdr.p_filesz = vma_dump_size(vma, mm_flags);
  1827. phdr.p_memsz = vma->vm_end - vma->vm_start;
  1828. offset += phdr.p_filesz;
  1829. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1830. if (vma->vm_flags & VM_WRITE)
  1831. phdr.p_flags |= PF_W;
  1832. if (vma->vm_flags & VM_EXEC)
  1833. phdr.p_flags |= PF_X;
  1834. phdr.p_align = ELF_EXEC_PAGESIZE;
  1835. DUMP_WRITE(&phdr, sizeof(phdr));
  1836. }
  1837. #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
  1838. ELF_CORE_WRITE_EXTRA_PHDRS;
  1839. #endif
  1840. /* write out the notes section */
  1841. if (!write_note_info(&info, file, &foffset))
  1842. goto end_coredump;
  1843. if (elf_coredump_extra_notes_write(file, &foffset))
  1844. goto end_coredump;
  1845. /* Align to page */
  1846. DUMP_SEEK(dataoff - foffset);
  1847. for (vma = first_vma(current, gate_vma); vma != NULL;
  1848. vma = next_vma(vma, gate_vma)) {
  1849. unsigned long addr;
  1850. unsigned long end;
  1851. end = vma->vm_start + vma_dump_size(vma, mm_flags);
  1852. for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
  1853. struct page *page;
  1854. struct vm_area_struct *vma;
  1855. if (get_user_pages(current, current->mm, addr, 1, 0, 1,
  1856. &page, &vma) <= 0) {
  1857. DUMP_SEEK(PAGE_SIZE);
  1858. } else {
  1859. if (page == ZERO_PAGE(0)) {
  1860. if (!dump_seek(file, PAGE_SIZE)) {
  1861. page_cache_release(page);
  1862. goto end_coredump;
  1863. }
  1864. } else {
  1865. void *kaddr;
  1866. flush_cache_page(vma, addr,
  1867. page_to_pfn(page));
  1868. kaddr = kmap(page);
  1869. if ((size += PAGE_SIZE) > limit ||
  1870. !dump_write(file, kaddr,
  1871. PAGE_SIZE)) {
  1872. kunmap(page);
  1873. page_cache_release(page);
  1874. goto end_coredump;
  1875. }
  1876. kunmap(page);
  1877. }
  1878. page_cache_release(page);
  1879. }
  1880. }
  1881. }
  1882. #ifdef ELF_CORE_WRITE_EXTRA_DATA
  1883. ELF_CORE_WRITE_EXTRA_DATA;
  1884. #endif
  1885. end_coredump:
  1886. set_fs(fs);
  1887. cleanup:
  1888. kfree(elf);
  1889. free_note_info(&info);
  1890. return has_dumped;
  1891. }
  1892. #endif /* USE_ELF_CORE_DUMP */
  1893. static int __init init_elf_binfmt(void)
  1894. {
  1895. return register_binfmt(&elf_format);
  1896. }
  1897. static void __exit exit_elf_binfmt(void)
  1898. {
  1899. /* Remove the COFF and ELF loaders. */
  1900. unregister_binfmt(&elf_format);
  1901. }
  1902. core_initcall(init_elf_binfmt);
  1903. module_exit(exit_elf_binfmt);
  1904. MODULE_LICENSE("GPL");