binfmt_elf.c 64 KB

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