binfmt_elf.c 58 KB

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