binfmt_elf.c 62 KB

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