binfmt_elf.c 62 KB

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