binfmt_elf.c 54 KB

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