binfmt_elf.c 63 KB

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