binfmt_elf.c 55 KB

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