binfmt_elf.c 57 KB

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