binfmt_elf.c 62 KB

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