binfmt_elf.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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/stat.h>
  15. #include <linux/time.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/a.out.h>
  19. #include <linux/errno.h>
  20. #include <linux/signal.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/string.h>
  23. #include <linux/file.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/slab.h>
  27. #include <linux/shm.h>
  28. #include <linux/personality.h>
  29. #include <linux/elfcore.h>
  30. #include <linux/init.h>
  31. #include <linux/highuid.h>
  32. #include <linux/smp.h>
  33. #include <linux/compiler.h>
  34. #include <linux/highmem.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/security.h>
  37. #include <linux/syscalls.h>
  38. #include <linux/random.h>
  39. #include <linux/elf.h>
  40. #include <linux/utsname.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/param.h>
  43. #include <asm/page.h>
  44. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
  45. static int load_elf_library(struct file *);
  46. static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
  47. /*
  48. * If we don't support core dumping, then supply a NULL so we
  49. * don't even try.
  50. */
  51. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  52. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
  53. #else
  54. #define elf_core_dump NULL
  55. #endif
  56. #if ELF_EXEC_PAGESIZE > PAGE_SIZE
  57. #define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
  58. #else
  59. #define ELF_MIN_ALIGN PAGE_SIZE
  60. #endif
  61. #ifndef ELF_CORE_EFLAGS
  62. #define ELF_CORE_EFLAGS 0
  63. #endif
  64. #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
  65. #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
  66. #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
  67. static struct linux_binfmt elf_format = {
  68. .module = THIS_MODULE,
  69. .load_binary = load_elf_binary,
  70. .load_shlib = load_elf_library,
  71. .core_dump = elf_core_dump,
  72. .min_coredump = ELF_EXEC_PAGESIZE,
  73. .hasvdso = 1
  74. };
  75. #define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
  76. static int set_brk(unsigned long start, unsigned long end)
  77. {
  78. start = ELF_PAGEALIGN(start);
  79. end = ELF_PAGEALIGN(end);
  80. if (end > start) {
  81. unsigned long addr;
  82. down_write(&current->mm->mmap_sem);
  83. addr = do_brk(start, end - start);
  84. up_write(&current->mm->mmap_sem);
  85. if (BAD_ADDR(addr))
  86. return addr;
  87. }
  88. current->mm->start_brk = current->mm->brk = end;
  89. return 0;
  90. }
  91. /* We need to explicitly zero any fractional pages
  92. after the data section (i.e. bss). This would
  93. contain the junk from the file that should not
  94. be in memory
  95. */
  96. static int padzero(unsigned long elf_bss)
  97. {
  98. unsigned long nbyte;
  99. nbyte = ELF_PAGEOFFSET(elf_bss);
  100. if (nbyte) {
  101. nbyte = ELF_MIN_ALIGN - nbyte;
  102. if (clear_user((void __user *) elf_bss, nbyte))
  103. return -EFAULT;
  104. }
  105. return 0;
  106. }
  107. /* Let's use some macros to make this stack manipulation a litle clearer */
  108. #ifdef CONFIG_STACK_GROWSUP
  109. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
  110. #define STACK_ROUND(sp, items) \
  111. ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
  112. #define STACK_ALLOC(sp, len) ({ \
  113. elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
  114. old_sp; })
  115. #else
  116. #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
  117. #define STACK_ROUND(sp, items) \
  118. (((unsigned long) (sp - items)) &~ 15UL)
  119. #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
  120. #endif
  121. static int
  122. create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
  123. int interp_aout, unsigned long load_addr,
  124. unsigned long interp_load_addr)
  125. {
  126. unsigned long p = bprm->p;
  127. int argc = bprm->argc;
  128. int envc = bprm->envc;
  129. elf_addr_t __user *argv;
  130. elf_addr_t __user *envp;
  131. elf_addr_t __user *sp;
  132. elf_addr_t __user *u_platform;
  133. const char *k_platform = ELF_PLATFORM;
  134. int items;
  135. elf_addr_t *elf_info;
  136. int ei_index = 0;
  137. struct task_struct *tsk = current;
  138. struct vm_area_struct *vma;
  139. /*
  140. * In some cases (e.g. Hyper-Threading), we want to avoid L1
  141. * evictions by the processes running on the same package. One
  142. * thing we can do is to shuffle the initial stack for them.
  143. */
  144. p = arch_align_stack(p);
  145. /*
  146. * If this architecture has a platform capability string, copy it
  147. * to userspace. In some cases (Sparc), this info is impossible
  148. * for userspace to get any other way, in others (i386) it is
  149. * merely difficult.
  150. */
  151. u_platform = NULL;
  152. if (k_platform) {
  153. size_t len = strlen(k_platform) + 1;
  154. u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
  155. if (__copy_to_user(u_platform, k_platform, len))
  156. return -EFAULT;
  157. }
  158. /* Create the ELF interpreter info */
  159. elf_info = (elf_addr_t *)current->mm->saved_auxv;
  160. /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
  161. #define NEW_AUX_ENT(id, val) \
  162. do { \
  163. elf_info[ei_index++] = id; \
  164. elf_info[ei_index++] = val; \
  165. } while (0)
  166. #ifdef ARCH_DLINFO
  167. /*
  168. * ARCH_DLINFO must come first so PPC can do its special alignment of
  169. * AUXV.
  170. * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
  171. * ARCH_DLINFO changes
  172. */
  173. ARCH_DLINFO;
  174. #endif
  175. NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
  176. NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
  177. NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
  178. NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
  179. NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
  180. NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
  181. NEW_AUX_ENT(AT_BASE, interp_load_addr);
  182. NEW_AUX_ENT(AT_FLAGS, 0);
  183. NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
  184. NEW_AUX_ENT(AT_UID, tsk->uid);
  185. NEW_AUX_ENT(AT_EUID, tsk->euid);
  186. NEW_AUX_ENT(AT_GID, tsk->gid);
  187. NEW_AUX_ENT(AT_EGID, tsk->egid);
  188. NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
  189. if (k_platform) {
  190. NEW_AUX_ENT(AT_PLATFORM,
  191. (elf_addr_t)(unsigned long)u_platform);
  192. }
  193. if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
  194. NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
  195. }
  196. #undef NEW_AUX_ENT
  197. /* AT_NULL is zero; clear the rest too */
  198. memset(&elf_info[ei_index], 0,
  199. sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
  200. /* And advance past the AT_NULL entry. */
  201. ei_index += 2;
  202. sp = STACK_ADD(p, ei_index);
  203. items = (argc + 1) + (envc + 1);
  204. if (interp_aout) {
  205. items += 3; /* a.out interpreters require argv & envp too */
  206. } else {
  207. items += 1; /* ELF interpreters only put argc on the stack */
  208. }
  209. bprm->p = STACK_ROUND(sp, items);
  210. /* Point sp at the lowest address on the stack */
  211. #ifdef CONFIG_STACK_GROWSUP
  212. sp = (elf_addr_t __user *)bprm->p - items - ei_index;
  213. bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
  214. #else
  215. sp = (elf_addr_t __user *)bprm->p;
  216. #endif
  217. /*
  218. * Grow the stack manually; some architectures have a limit on how
  219. * far ahead a user-space access may be in order to grow the stack.
  220. */
  221. vma = find_extend_vma(current->mm, bprm->p);
  222. if (!vma)
  223. return -EFAULT;
  224. /* Now, let's put argc (and argv, envp if appropriate) on the stack */
  225. if (__put_user(argc, sp++))
  226. return -EFAULT;
  227. if (interp_aout) {
  228. argv = sp + 2;
  229. envp = argv + argc + 1;
  230. if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
  231. __put_user((elf_addr_t)(unsigned long)envp, sp++))
  232. return -EFAULT;
  233. } else {
  234. argv = sp;
  235. envp = argv + argc + 1;
  236. }
  237. /* Populate argv and envp */
  238. p = current->mm->arg_end = current->mm->arg_start;
  239. while (argc-- > 0) {
  240. size_t len;
  241. if (__put_user((elf_addr_t)p, argv++))
  242. return -EFAULT;
  243. len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
  244. if (!len || len > MAX_ARG_STRLEN)
  245. return 0;
  246. p += len;
  247. }
  248. if (__put_user(0, argv))
  249. return -EFAULT;
  250. current->mm->arg_end = current->mm->env_start = p;
  251. while (envc-- > 0) {
  252. size_t len;
  253. if (__put_user((elf_addr_t)p, envp++))
  254. return -EFAULT;
  255. len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
  256. if (!len || len > MAX_ARG_STRLEN)
  257. return 0;
  258. p += len;
  259. }
  260. if (__put_user(0, envp))
  261. return -EFAULT;
  262. current->mm->env_end = p;
  263. /* Put the elf_info on the stack in the right place. */
  264. sp = (elf_addr_t __user *)envp + 1;
  265. if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
  266. return -EFAULT;
  267. return 0;
  268. }
  269. #ifndef elf_map
  270. static unsigned long elf_map(struct file *filep, unsigned long addr,
  271. struct elf_phdr *eppnt, int prot, int type)
  272. {
  273. unsigned long map_addr;
  274. unsigned long pageoffset = ELF_PAGEOFFSET(eppnt->p_vaddr);
  275. down_write(&current->mm->mmap_sem);
  276. /* mmap() will return -EINVAL if given a zero size, but a
  277. * segment with zero filesize is perfectly valid */
  278. if (eppnt->p_filesz + pageoffset)
  279. map_addr = do_mmap(filep, ELF_PAGESTART(addr),
  280. eppnt->p_filesz + pageoffset, prot, type,
  281. eppnt->p_offset - pageoffset);
  282. else
  283. map_addr = ELF_PAGESTART(addr);
  284. up_write(&current->mm->mmap_sem);
  285. return(map_addr);
  286. }
  287. #endif /* !elf_map */
  288. /* This is much more generalized than the library routine read function,
  289. so we keep this separate. Technically the library read function
  290. is only provided so that we can read a.out libraries that have
  291. an ELF header */
  292. static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
  293. struct file *interpreter, unsigned long *interp_load_addr)
  294. {
  295. struct elf_phdr *elf_phdata;
  296. struct elf_phdr *eppnt;
  297. unsigned long load_addr = 0;
  298. int load_addr_set = 0;
  299. unsigned long last_bss = 0, elf_bss = 0;
  300. unsigned long error = ~0UL;
  301. int retval, i, size;
  302. /* First of all, some simple consistency checks */
  303. if (interp_elf_ex->e_type != ET_EXEC &&
  304. interp_elf_ex->e_type != ET_DYN)
  305. goto out;
  306. if (!elf_check_arch(interp_elf_ex))
  307. goto out;
  308. if (!interpreter->f_op || !interpreter->f_op->mmap)
  309. goto out;
  310. /*
  311. * If the size of this structure has changed, then punt, since
  312. * we will be doing the wrong thing.
  313. */
  314. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
  315. goto out;
  316. if (interp_elf_ex->e_phnum < 1 ||
  317. interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
  318. goto out;
  319. /* Now read in all of the header information */
  320. size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
  321. if (size > ELF_MIN_ALIGN)
  322. goto out;
  323. elf_phdata = kmalloc(size, GFP_KERNEL);
  324. if (!elf_phdata)
  325. goto out;
  326. retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
  327. (char *)elf_phdata,size);
  328. error = -EIO;
  329. if (retval != size) {
  330. if (retval < 0)
  331. error = retval;
  332. goto out_close;
  333. }
  334. eppnt = elf_phdata;
  335. for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
  336. if (eppnt->p_type == PT_LOAD) {
  337. int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
  338. int elf_prot = 0;
  339. unsigned long vaddr = 0;
  340. unsigned long k, map_addr;
  341. if (eppnt->p_flags & PF_R)
  342. elf_prot = PROT_READ;
  343. if (eppnt->p_flags & PF_W)
  344. elf_prot |= PROT_WRITE;
  345. if (eppnt->p_flags & PF_X)
  346. elf_prot |= PROT_EXEC;
  347. vaddr = eppnt->p_vaddr;
  348. if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
  349. elf_type |= MAP_FIXED;
  350. map_addr = elf_map(interpreter, load_addr + vaddr,
  351. eppnt, elf_prot, elf_type);
  352. error = map_addr;
  353. if (BAD_ADDR(map_addr))
  354. goto out_close;
  355. if (!load_addr_set &&
  356. interp_elf_ex->e_type == ET_DYN) {
  357. load_addr = map_addr - ELF_PAGESTART(vaddr);
  358. load_addr_set = 1;
  359. }
  360. /*
  361. * Check to see if the section's size will overflow the
  362. * allowed task size. Note that p_filesz must always be
  363. * <= p_memsize so it's only necessary to check p_memsz.
  364. */
  365. k = load_addr + eppnt->p_vaddr;
  366. if (BAD_ADDR(k) ||
  367. eppnt->p_filesz > eppnt->p_memsz ||
  368. eppnt->p_memsz > TASK_SIZE ||
  369. TASK_SIZE - eppnt->p_memsz < k) {
  370. error = -ENOMEM;
  371. goto out_close;
  372. }
  373. /*
  374. * Find the end of the file mapping for this phdr, and
  375. * keep track of the largest address we see for this.
  376. */
  377. k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
  378. if (k > elf_bss)
  379. elf_bss = k;
  380. /*
  381. * Do the same thing for the memory mapping - between
  382. * elf_bss and last_bss is the bss section.
  383. */
  384. k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
  385. if (k > last_bss)
  386. last_bss = k;
  387. }
  388. }
  389. /*
  390. * Now fill out the bss section. First pad the last page up
  391. * to the page boundary, and then perform a mmap to make sure
  392. * that there are zero-mapped pages up to and including the
  393. * last bss page.
  394. */
  395. if (padzero(elf_bss)) {
  396. error = -EFAULT;
  397. goto out_close;
  398. }
  399. /* What we have mapped so far */
  400. elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
  401. /* Map the last of the bss segment */
  402. if (last_bss > elf_bss) {
  403. down_write(&current->mm->mmap_sem);
  404. error = do_brk(elf_bss, last_bss - elf_bss);
  405. up_write(&current->mm->mmap_sem);
  406. if (BAD_ADDR(error))
  407. goto out_close;
  408. }
  409. *interp_load_addr = load_addr;
  410. error = ((unsigned long)interp_elf_ex->e_entry) + load_addr;
  411. out_close:
  412. kfree(elf_phdata);
  413. out:
  414. return error;
  415. }
  416. static unsigned long load_aout_interp(struct exec *interp_ex,
  417. struct file *interpreter)
  418. {
  419. unsigned long text_data, elf_entry = ~0UL;
  420. char __user * addr;
  421. loff_t offset;
  422. current->mm->end_code = interp_ex->a_text;
  423. text_data = interp_ex->a_text + interp_ex->a_data;
  424. current->mm->end_data = text_data;
  425. current->mm->brk = interp_ex->a_bss + text_data;
  426. switch (N_MAGIC(*interp_ex)) {
  427. case OMAGIC:
  428. offset = 32;
  429. addr = (char __user *)0;
  430. break;
  431. case ZMAGIC:
  432. case QMAGIC:
  433. offset = N_TXTOFF(*interp_ex);
  434. addr = (char __user *)N_TXTADDR(*interp_ex);
  435. break;
  436. default:
  437. goto out;
  438. }
  439. down_write(&current->mm->mmap_sem);
  440. do_brk(0, text_data);
  441. up_write(&current->mm->mmap_sem);
  442. if (!interpreter->f_op || !interpreter->f_op->read)
  443. goto out;
  444. if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
  445. goto out;
  446. flush_icache_range((unsigned long)addr,
  447. (unsigned long)addr + text_data);
  448. down_write(&current->mm->mmap_sem);
  449. do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
  450. interp_ex->a_bss);
  451. up_write(&current->mm->mmap_sem);
  452. elf_entry = interp_ex->a_entry;
  453. out:
  454. return elf_entry;
  455. }
  456. /*
  457. * These are the functions used to load ELF style executables and shared
  458. * libraries. There is no binary dependent code anywhere else.
  459. */
  460. #define INTERPRETER_NONE 0
  461. #define INTERPRETER_AOUT 1
  462. #define INTERPRETER_ELF 2
  463. #ifndef STACK_RND_MASK
  464. #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
  465. #endif
  466. static unsigned long randomize_stack_top(unsigned long stack_top)
  467. {
  468. unsigned int random_variable = 0;
  469. if ((current->flags & PF_RANDOMIZE) &&
  470. !(current->personality & ADDR_NO_RANDOMIZE)) {
  471. random_variable = get_random_int() & STACK_RND_MASK;
  472. random_variable <<= PAGE_SHIFT;
  473. }
  474. #ifdef CONFIG_STACK_GROWSUP
  475. return PAGE_ALIGN(stack_top) + random_variable;
  476. #else
  477. return PAGE_ALIGN(stack_top) - random_variable;
  478. #endif
  479. }
  480. static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
  481. {
  482. struct file *interpreter = NULL; /* to shut gcc up */
  483. unsigned long load_addr = 0, load_bias = 0;
  484. int load_addr_set = 0;
  485. char * elf_interpreter = NULL;
  486. unsigned int interpreter_type = INTERPRETER_NONE;
  487. unsigned char ibcs2_interpreter = 0;
  488. unsigned long error;
  489. struct elf_phdr *elf_ppnt, *elf_phdata;
  490. unsigned long elf_bss, elf_brk;
  491. int elf_exec_fileno;
  492. int retval, i;
  493. unsigned int size;
  494. unsigned long elf_entry, interp_load_addr = 0;
  495. unsigned long start_code, end_code, start_data, end_data;
  496. unsigned long reloc_func_desc = 0;
  497. char passed_fileno[6];
  498. struct files_struct *files;
  499. int executable_stack = EXSTACK_DEFAULT;
  500. unsigned long def_flags = 0;
  501. struct {
  502. struct elfhdr elf_ex;
  503. struct elfhdr interp_elf_ex;
  504. struct exec interp_ex;
  505. } *loc;
  506. loc = kmalloc(sizeof(*loc), GFP_KERNEL);
  507. if (!loc) {
  508. retval = -ENOMEM;
  509. goto out_ret;
  510. }
  511. /* Get the exec-header */
  512. loc->elf_ex = *((struct elfhdr *)bprm->buf);
  513. retval = -ENOEXEC;
  514. /* First of all, some simple consistency checks */
  515. if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  516. goto out;
  517. if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
  518. goto out;
  519. if (!elf_check_arch(&loc->elf_ex))
  520. goto out;
  521. if (!bprm->file->f_op||!bprm->file->f_op->mmap)
  522. goto out;
  523. /* Now read in all of the header information */
  524. if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
  525. goto out;
  526. if (loc->elf_ex.e_phnum < 1 ||
  527. loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
  528. goto out;
  529. size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
  530. retval = -ENOMEM;
  531. elf_phdata = kmalloc(size, GFP_KERNEL);
  532. if (!elf_phdata)
  533. goto out;
  534. retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
  535. (char *)elf_phdata, size);
  536. if (retval != size) {
  537. if (retval >= 0)
  538. retval = -EIO;
  539. goto out_free_ph;
  540. }
  541. files = current->files; /* Refcounted so ok */
  542. retval = unshare_files();
  543. if (retval < 0)
  544. goto out_free_ph;
  545. if (files == current->files) {
  546. put_files_struct(files);
  547. files = NULL;
  548. }
  549. /* exec will make our files private anyway, but for the a.out
  550. loader stuff we need to do it earlier */
  551. retval = get_unused_fd();
  552. if (retval < 0)
  553. goto out_free_fh;
  554. get_file(bprm->file);
  555. fd_install(elf_exec_fileno = retval, bprm->file);
  556. elf_ppnt = elf_phdata;
  557. elf_bss = 0;
  558. elf_brk = 0;
  559. start_code = ~0UL;
  560. end_code = 0;
  561. start_data = 0;
  562. end_data = 0;
  563. for (i = 0; i < loc->elf_ex.e_phnum; i++) {
  564. if (elf_ppnt->p_type == PT_INTERP) {
  565. /* This is the program interpreter used for
  566. * shared libraries - for now assume that this
  567. * is an a.out format binary
  568. */
  569. retval = -ENOEXEC;
  570. if (elf_ppnt->p_filesz > PATH_MAX ||
  571. elf_ppnt->p_filesz < 2)
  572. goto out_free_file;
  573. retval = -ENOMEM;
  574. elf_interpreter = kmalloc(elf_ppnt->p_filesz,
  575. GFP_KERNEL);
  576. if (!elf_interpreter)
  577. goto out_free_file;
  578. retval = kernel_read(bprm->file, elf_ppnt->p_offset,
  579. elf_interpreter,
  580. elf_ppnt->p_filesz);
  581. if (retval != elf_ppnt->p_filesz) {
  582. if (retval >= 0)
  583. retval = -EIO;
  584. goto out_free_interp;
  585. }
  586. /* make sure path is NULL terminated */
  587. retval = -ENOEXEC;
  588. if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
  589. goto out_free_interp;
  590. /* If the program interpreter is one of these two,
  591. * then assume an iBCS2 image. Otherwise assume
  592. * a native linux image.
  593. */
  594. if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
  595. strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
  596. ibcs2_interpreter = 1;
  597. /*
  598. * The early SET_PERSONALITY here is so that the lookup
  599. * for the interpreter happens in the namespace of the
  600. * to-be-execed image. SET_PERSONALITY can select an
  601. * alternate root.
  602. *
  603. * However, SET_PERSONALITY is NOT allowed to switch
  604. * this task into the new images's memory mapping
  605. * policy - that is, TASK_SIZE must still evaluate to
  606. * that which is appropriate to the execing application.
  607. * This is because exit_mmap() needs to have TASK_SIZE
  608. * evaluate to the size of the old image.
  609. *
  610. * So if (say) a 64-bit application is execing a 32-bit
  611. * application it is the architecture's responsibility
  612. * to defer changing the value of TASK_SIZE until the
  613. * switch really is going to happen - do this in
  614. * flush_thread(). - akpm
  615. */
  616. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  617. interpreter = open_exec(elf_interpreter);
  618. retval = PTR_ERR(interpreter);
  619. if (IS_ERR(interpreter))
  620. goto out_free_interp;
  621. /*
  622. * If the binary is not readable then enforce
  623. * mm->dumpable = 0 regardless of the interpreter's
  624. * permissions.
  625. */
  626. if (file_permission(interpreter, MAY_READ) < 0)
  627. bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
  628. retval = kernel_read(interpreter, 0, bprm->buf,
  629. BINPRM_BUF_SIZE);
  630. if (retval != BINPRM_BUF_SIZE) {
  631. if (retval >= 0)
  632. retval = -EIO;
  633. goto out_free_dentry;
  634. }
  635. /* Get the exec headers */
  636. loc->interp_ex = *((struct exec *)bprm->buf);
  637. loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
  638. break;
  639. }
  640. elf_ppnt++;
  641. }
  642. elf_ppnt = elf_phdata;
  643. for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
  644. if (elf_ppnt->p_type == PT_GNU_STACK) {
  645. if (elf_ppnt->p_flags & PF_X)
  646. executable_stack = EXSTACK_ENABLE_X;
  647. else
  648. executable_stack = EXSTACK_DISABLE_X;
  649. break;
  650. }
  651. /* Some simple consistency checks for the interpreter */
  652. if (elf_interpreter) {
  653. static int warn;
  654. interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
  655. /* Now figure out which format our binary is */
  656. if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
  657. (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
  658. (N_MAGIC(loc->interp_ex) != QMAGIC))
  659. interpreter_type = INTERPRETER_ELF;
  660. if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  661. interpreter_type &= ~INTERPRETER_ELF;
  662. if (interpreter_type == INTERPRETER_AOUT && warn < 10) {
  663. printk(KERN_WARNING "a.out ELF interpreter %s is "
  664. "deprecated and will not be supported "
  665. "after Linux 2.6.25\n", elf_interpreter);
  666. warn++;
  667. }
  668. retval = -ELIBBAD;
  669. if (!interpreter_type)
  670. goto out_free_dentry;
  671. /* Make sure only one type was selected */
  672. if ((interpreter_type & INTERPRETER_ELF) &&
  673. interpreter_type != INTERPRETER_ELF) {
  674. // FIXME - ratelimit this before re-enabling
  675. // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
  676. interpreter_type = INTERPRETER_ELF;
  677. }
  678. /* Verify the interpreter has a valid arch */
  679. if ((interpreter_type == INTERPRETER_ELF) &&
  680. !elf_check_arch(&loc->interp_elf_ex))
  681. goto out_free_dentry;
  682. } else {
  683. /* Executables without an interpreter also need a personality */
  684. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  685. }
  686. /* OK, we are done with that, now set up the arg stuff,
  687. and then start this sucker up */
  688. if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
  689. char *passed_p = passed_fileno;
  690. sprintf(passed_fileno, "%d", elf_exec_fileno);
  691. if (elf_interpreter) {
  692. retval = copy_strings_kernel(1, &passed_p, bprm);
  693. if (retval)
  694. goto out_free_dentry;
  695. bprm->argc++;
  696. }
  697. }
  698. /* Flush all traces of the currently running executable */
  699. retval = flush_old_exec(bprm);
  700. if (retval)
  701. goto out_free_dentry;
  702. /* Discard our unneeded old files struct */
  703. if (files) {
  704. put_files_struct(files);
  705. files = NULL;
  706. }
  707. /* OK, This is the point of no return */
  708. current->flags &= ~PF_FORKNOEXEC;
  709. current->mm->def_flags = def_flags;
  710. /* Do this immediately, since STACK_TOP as used in setup_arg_pages
  711. may depend on the personality. */
  712. SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
  713. if (elf_read_implies_exec(loc->elf_ex, executable_stack))
  714. current->personality |= READ_IMPLIES_EXEC;
  715. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  716. current->flags |= PF_RANDOMIZE;
  717. arch_pick_mmap_layout(current->mm);
  718. /* Do this so that we can load the interpreter, if need be. We will
  719. change some of these later */
  720. current->mm->free_area_cache = current->mm->mmap_base;
  721. current->mm->cached_hole_size = 0;
  722. retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
  723. executable_stack);
  724. if (retval < 0) {
  725. send_sig(SIGKILL, current, 0);
  726. goto out_free_dentry;
  727. }
  728. current->mm->start_stack = bprm->p;
  729. /* Now we do a little grungy work by mmaping the ELF image into
  730. the correct location in memory. At this point, we assume that
  731. the image should be loaded at fixed address, not at a variable
  732. address. */
  733. for(i = 0, elf_ppnt = elf_phdata;
  734. i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
  735. int elf_prot = 0, elf_flags;
  736. unsigned long k, vaddr;
  737. if (elf_ppnt->p_type != PT_LOAD)
  738. continue;
  739. if (unlikely (elf_brk > elf_bss)) {
  740. unsigned long nbyte;
  741. /* There was a PT_LOAD segment with p_memsz > p_filesz
  742. before this one. Map anonymous pages, if needed,
  743. and clear the area. */
  744. retval = set_brk (elf_bss + load_bias,
  745. elf_brk + load_bias);
  746. if (retval) {
  747. send_sig(SIGKILL, current, 0);
  748. goto out_free_dentry;
  749. }
  750. nbyte = ELF_PAGEOFFSET(elf_bss);
  751. if (nbyte) {
  752. nbyte = ELF_MIN_ALIGN - nbyte;
  753. if (nbyte > elf_brk - elf_bss)
  754. nbyte = elf_brk - elf_bss;
  755. if (clear_user((void __user *)elf_bss +
  756. load_bias, nbyte)) {
  757. /*
  758. * This bss-zeroing can fail if the ELF
  759. * file specifies odd protections. So
  760. * we don't check the return value
  761. */
  762. }
  763. }
  764. }
  765. if (elf_ppnt->p_flags & PF_R)
  766. elf_prot |= PROT_READ;
  767. if (elf_ppnt->p_flags & PF_W)
  768. elf_prot |= PROT_WRITE;
  769. if (elf_ppnt->p_flags & PF_X)
  770. elf_prot |= PROT_EXEC;
  771. elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
  772. vaddr = elf_ppnt->p_vaddr;
  773. if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
  774. elf_flags |= MAP_FIXED;
  775. } else if (loc->elf_ex.e_type == ET_DYN) {
  776. /* Try and get dynamic programs out of the way of the
  777. * default mmap base, as well as whatever program they
  778. * might try to exec. This is because the brk will
  779. * follow the loader, and is not movable. */
  780. load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
  781. }
  782. error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
  783. elf_prot, elf_flags);
  784. if (BAD_ADDR(error)) {
  785. send_sig(SIGKILL, current, 0);
  786. retval = IS_ERR((void *)error) ?
  787. PTR_ERR((void*)error) : -EINVAL;
  788. goto out_free_dentry;
  789. }
  790. if (!load_addr_set) {
  791. load_addr_set = 1;
  792. load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
  793. if (loc->elf_ex.e_type == ET_DYN) {
  794. load_bias += error -
  795. ELF_PAGESTART(load_bias + vaddr);
  796. load_addr += load_bias;
  797. reloc_func_desc = load_bias;
  798. }
  799. }
  800. k = elf_ppnt->p_vaddr;
  801. if (k < start_code)
  802. start_code = k;
  803. if (start_data < k)
  804. start_data = k;
  805. /*
  806. * Check to see if the section's size will overflow the
  807. * allowed task size. Note that p_filesz must always be
  808. * <= p_memsz so it is only necessary to check p_memsz.
  809. */
  810. if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
  811. elf_ppnt->p_memsz > TASK_SIZE ||
  812. TASK_SIZE - elf_ppnt->p_memsz < k) {
  813. /* set_brk can never work. Avoid overflows. */
  814. send_sig(SIGKILL, current, 0);
  815. retval = -EINVAL;
  816. goto out_free_dentry;
  817. }
  818. k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  819. if (k > elf_bss)
  820. elf_bss = k;
  821. if ((elf_ppnt->p_flags & PF_X) && end_code < k)
  822. end_code = k;
  823. if (end_data < k)
  824. end_data = k;
  825. k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  826. if (k > elf_brk)
  827. elf_brk = k;
  828. }
  829. loc->elf_ex.e_entry += load_bias;
  830. elf_bss += load_bias;
  831. elf_brk += load_bias;
  832. start_code += load_bias;
  833. end_code += load_bias;
  834. start_data += load_bias;
  835. end_data += load_bias;
  836. /* Calling set_brk effectively mmaps the pages that we need
  837. * for the bss and break sections. We must do this before
  838. * mapping in the interpreter, to make sure it doesn't wind
  839. * up getting placed where the bss needs to go.
  840. */
  841. retval = set_brk(elf_bss, elf_brk);
  842. if (retval) {
  843. send_sig(SIGKILL, current, 0);
  844. goto out_free_dentry;
  845. }
  846. if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
  847. send_sig(SIGSEGV, current, 0);
  848. retval = -EFAULT; /* Nobody gets to see this, but.. */
  849. goto out_free_dentry;
  850. }
  851. if (elf_interpreter) {
  852. if (interpreter_type == INTERPRETER_AOUT)
  853. elf_entry = load_aout_interp(&loc->interp_ex,
  854. interpreter);
  855. else
  856. elf_entry = load_elf_interp(&loc->interp_elf_ex,
  857. interpreter,
  858. &interp_load_addr);
  859. if (BAD_ADDR(elf_entry)) {
  860. force_sig(SIGSEGV, current);
  861. retval = IS_ERR((void *)elf_entry) ?
  862. (int)elf_entry : -EINVAL;
  863. goto out_free_dentry;
  864. }
  865. reloc_func_desc = interp_load_addr;
  866. allow_write_access(interpreter);
  867. fput(interpreter);
  868. kfree(elf_interpreter);
  869. } else {
  870. elf_entry = loc->elf_ex.e_entry;
  871. if (BAD_ADDR(elf_entry)) {
  872. force_sig(SIGSEGV, current);
  873. retval = -EINVAL;
  874. goto out_free_dentry;
  875. }
  876. }
  877. kfree(elf_phdata);
  878. if (interpreter_type != INTERPRETER_AOUT)
  879. sys_close(elf_exec_fileno);
  880. set_binfmt(&elf_format);
  881. #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
  882. retval = arch_setup_additional_pages(bprm, executable_stack);
  883. if (retval < 0) {
  884. send_sig(SIGKILL, current, 0);
  885. goto out;
  886. }
  887. #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
  888. compute_creds(bprm);
  889. current->flags &= ~PF_FORKNOEXEC;
  890. retval = create_elf_tables(bprm, &loc->elf_ex,
  891. (interpreter_type == INTERPRETER_AOUT),
  892. load_addr, interp_load_addr);
  893. if (retval < 0) {
  894. send_sig(SIGKILL, current, 0);
  895. goto out;
  896. }
  897. /* N.B. passed_fileno might not be initialized? */
  898. if (interpreter_type == INTERPRETER_AOUT)
  899. current->mm->arg_start += strlen(passed_fileno) + 1;
  900. current->mm->end_code = end_code;
  901. current->mm->start_code = start_code;
  902. current->mm->start_data = start_data;
  903. current->mm->end_data = end_data;
  904. current->mm->start_stack = bprm->p;
  905. if (current->personality & MMAP_PAGE_ZERO) {
  906. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  907. and some applications "depend" upon this behavior.
  908. Since we do not have the power to recompile these, we
  909. emulate the SVr4 behavior. Sigh. */
  910. down_write(&current->mm->mmap_sem);
  911. error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
  912. MAP_FIXED | MAP_PRIVATE, 0);
  913. up_write(&current->mm->mmap_sem);
  914. }
  915. #ifdef ELF_PLAT_INIT
  916. /*
  917. * The ABI may specify that certain registers be set up in special
  918. * ways (on i386 %edx is the address of a DT_FINI function, for
  919. * example. In addition, it may also specify (eg, PowerPC64 ELF)
  920. * that the e_entry field is the address of the function descriptor
  921. * for the startup routine, rather than the address of the startup
  922. * routine itself. This macro performs whatever initialization to
  923. * the regs structure is required as well as any relocations to the
  924. * function descriptor entries when executing dynamically links apps.
  925. */
  926. ELF_PLAT_INIT(regs, reloc_func_desc);
  927. #endif
  928. start_thread(regs, elf_entry, bprm->p);
  929. if (unlikely(current->ptrace & PT_PTRACED)) {
  930. if (current->ptrace & PT_TRACE_EXEC)
  931. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  932. else
  933. send_sig(SIGTRAP, current, 0);
  934. }
  935. retval = 0;
  936. out:
  937. kfree(loc);
  938. out_ret:
  939. return retval;
  940. /* error cleanup */
  941. out_free_dentry:
  942. allow_write_access(interpreter);
  943. if (interpreter)
  944. fput(interpreter);
  945. out_free_interp:
  946. kfree(elf_interpreter);
  947. out_free_file:
  948. sys_close(elf_exec_fileno);
  949. out_free_fh:
  950. if (files)
  951. reset_files_struct(current, files);
  952. out_free_ph:
  953. kfree(elf_phdata);
  954. goto out;
  955. }
  956. /* This is really simpleminded and specialized - we are loading an
  957. a.out library that is given an ELF header. */
  958. static int load_elf_library(struct file *file)
  959. {
  960. struct elf_phdr *elf_phdata;
  961. struct elf_phdr *eppnt;
  962. unsigned long elf_bss, bss, len;
  963. int retval, error, i, j;
  964. struct elfhdr elf_ex;
  965. error = -ENOEXEC;
  966. retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
  967. if (retval != sizeof(elf_ex))
  968. goto out;
  969. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  970. goto out;
  971. /* First of all, some simple consistency checks */
  972. if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
  973. !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
  974. goto out;
  975. /* Now read in all of the header information */
  976. j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
  977. /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
  978. error = -ENOMEM;
  979. elf_phdata = kmalloc(j, GFP_KERNEL);
  980. if (!elf_phdata)
  981. goto out;
  982. eppnt = elf_phdata;
  983. error = -ENOEXEC;
  984. retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
  985. if (retval != j)
  986. goto out_free_ph;
  987. for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
  988. if ((eppnt + i)->p_type == PT_LOAD)
  989. j++;
  990. if (j != 1)
  991. goto out_free_ph;
  992. while (eppnt->p_type != PT_LOAD)
  993. eppnt++;
  994. /* Now use mmap to map the library into memory. */
  995. down_write(&current->mm->mmap_sem);
  996. error = do_mmap(file,
  997. ELF_PAGESTART(eppnt->p_vaddr),
  998. (eppnt->p_filesz +
  999. ELF_PAGEOFFSET(eppnt->p_vaddr)),
  1000. PROT_READ | PROT_WRITE | PROT_EXEC,
  1001. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  1002. (eppnt->p_offset -
  1003. ELF_PAGEOFFSET(eppnt->p_vaddr)));
  1004. up_write(&current->mm->mmap_sem);
  1005. if (error != ELF_PAGESTART(eppnt->p_vaddr))
  1006. goto out_free_ph;
  1007. elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
  1008. if (padzero(elf_bss)) {
  1009. error = -EFAULT;
  1010. goto out_free_ph;
  1011. }
  1012. len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
  1013. ELF_MIN_ALIGN - 1);
  1014. bss = eppnt->p_memsz + eppnt->p_vaddr;
  1015. if (bss > len) {
  1016. down_write(&current->mm->mmap_sem);
  1017. do_brk(len, bss - len);
  1018. up_write(&current->mm->mmap_sem);
  1019. }
  1020. error = 0;
  1021. out_free_ph:
  1022. kfree(elf_phdata);
  1023. out:
  1024. return error;
  1025. }
  1026. /*
  1027. * Note that some platforms still use traditional core dumps and not
  1028. * the ELF core dump. Each platform can select it as appropriate.
  1029. */
  1030. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  1031. /*
  1032. * ELF core dumper
  1033. *
  1034. * Modelled on fs/exec.c:aout_core_dump()
  1035. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  1036. */
  1037. /*
  1038. * These are the only things you should do on a core-file: use only these
  1039. * functions to write out all the necessary info.
  1040. */
  1041. static int dump_write(struct file *file, const void *addr, int nr)
  1042. {
  1043. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1044. }
  1045. static int dump_seek(struct file *file, loff_t off)
  1046. {
  1047. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1048. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1049. return 0;
  1050. } else {
  1051. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1052. if (!buf)
  1053. return 0;
  1054. while (off > 0) {
  1055. unsigned long n = off;
  1056. if (n > PAGE_SIZE)
  1057. n = PAGE_SIZE;
  1058. if (!dump_write(file, buf, n))
  1059. return 0;
  1060. off -= n;
  1061. }
  1062. free_page((unsigned long)buf);
  1063. }
  1064. return 1;
  1065. }
  1066. /*
  1067. * Decide what to dump of a segment, part, all or none.
  1068. */
  1069. static unsigned long vma_dump_size(struct vm_area_struct *vma,
  1070. unsigned long mm_flags)
  1071. {
  1072. /* The vma can be set up to tell us the answer directly. */
  1073. if (vma->vm_flags & VM_ALWAYSDUMP)
  1074. goto whole;
  1075. /* Do not dump I/O mapped devices or special mappings */
  1076. if (vma->vm_flags & (VM_IO | VM_RESERVED))
  1077. return 0;
  1078. #define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
  1079. /* By default, dump shared memory if mapped from an anonymous file. */
  1080. if (vma->vm_flags & VM_SHARED) {
  1081. if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
  1082. FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
  1083. goto whole;
  1084. return 0;
  1085. }
  1086. /* Dump segments that have been written to. */
  1087. if (vma->anon_vma && FILTER(ANON_PRIVATE))
  1088. goto whole;
  1089. if (vma->vm_file == NULL)
  1090. return 0;
  1091. if (FILTER(MAPPED_PRIVATE))
  1092. goto whole;
  1093. /*
  1094. * If this looks like the beginning of a DSO or executable mapping,
  1095. * check for an ELF header. If we find one, dump the first page to
  1096. * aid in determining what was mapped here.
  1097. */
  1098. if (FILTER(ELF_HEADERS) && vma->vm_file != NULL && vma->vm_pgoff == 0) {
  1099. u32 __user *header = (u32 __user *) vma->vm_start;
  1100. u32 word;
  1101. /*
  1102. * Doing it this way gets the constant folded by GCC.
  1103. */
  1104. union {
  1105. u32 cmp;
  1106. char elfmag[SELFMAG];
  1107. } magic;
  1108. BUILD_BUG_ON(SELFMAG != sizeof word);
  1109. magic.elfmag[EI_MAG0] = ELFMAG0;
  1110. magic.elfmag[EI_MAG1] = ELFMAG1;
  1111. magic.elfmag[EI_MAG2] = ELFMAG2;
  1112. magic.elfmag[EI_MAG3] = ELFMAG3;
  1113. if (get_user(word, header) == 0 && word == magic.cmp)
  1114. return PAGE_SIZE;
  1115. }
  1116. #undef FILTER
  1117. return 0;
  1118. whole:
  1119. return vma->vm_end - vma->vm_start;
  1120. }
  1121. /* An ELF note in memory */
  1122. struct memelfnote
  1123. {
  1124. const char *name;
  1125. int type;
  1126. unsigned int datasz;
  1127. void *data;
  1128. };
  1129. static int notesize(struct memelfnote *en)
  1130. {
  1131. int sz;
  1132. sz = sizeof(struct elf_note);
  1133. sz += roundup(strlen(en->name) + 1, 4);
  1134. sz += roundup(en->datasz, 4);
  1135. return sz;
  1136. }
  1137. #define DUMP_WRITE(addr, nr, foffset) \
  1138. do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
  1139. static int alignfile(struct file *file, loff_t *foffset)
  1140. {
  1141. static const char buf[4] = { 0, };
  1142. DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
  1143. return 1;
  1144. }
  1145. static int writenote(struct memelfnote *men, struct file *file,
  1146. loff_t *foffset)
  1147. {
  1148. struct elf_note en;
  1149. en.n_namesz = strlen(men->name) + 1;
  1150. en.n_descsz = men->datasz;
  1151. en.n_type = men->type;
  1152. DUMP_WRITE(&en, sizeof(en), foffset);
  1153. DUMP_WRITE(men->name, en.n_namesz, foffset);
  1154. if (!alignfile(file, foffset))
  1155. return 0;
  1156. DUMP_WRITE(men->data, men->datasz, foffset);
  1157. if (!alignfile(file, foffset))
  1158. return 0;
  1159. return 1;
  1160. }
  1161. #undef DUMP_WRITE
  1162. #define DUMP_WRITE(addr, nr) \
  1163. if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
  1164. goto end_coredump;
  1165. #define DUMP_SEEK(off) \
  1166. if (!dump_seek(file, (off))) \
  1167. goto end_coredump;
  1168. static void fill_elf_header(struct elfhdr *elf, int segs)
  1169. {
  1170. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  1171. elf->e_ident[EI_CLASS] = ELF_CLASS;
  1172. elf->e_ident[EI_DATA] = ELF_DATA;
  1173. elf->e_ident[EI_VERSION] = EV_CURRENT;
  1174. elf->e_ident[EI_OSABI] = ELF_OSABI;
  1175. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  1176. elf->e_type = ET_CORE;
  1177. elf->e_machine = ELF_ARCH;
  1178. elf->e_version = EV_CURRENT;
  1179. elf->e_entry = 0;
  1180. elf->e_phoff = sizeof(struct elfhdr);
  1181. elf->e_shoff = 0;
  1182. elf->e_flags = ELF_CORE_EFLAGS;
  1183. elf->e_ehsize = sizeof(struct elfhdr);
  1184. elf->e_phentsize = sizeof(struct elf_phdr);
  1185. elf->e_phnum = segs;
  1186. elf->e_shentsize = 0;
  1187. elf->e_shnum = 0;
  1188. elf->e_shstrndx = 0;
  1189. return;
  1190. }
  1191. static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
  1192. {
  1193. phdr->p_type = PT_NOTE;
  1194. phdr->p_offset = offset;
  1195. phdr->p_vaddr = 0;
  1196. phdr->p_paddr = 0;
  1197. phdr->p_filesz = sz;
  1198. phdr->p_memsz = 0;
  1199. phdr->p_flags = 0;
  1200. phdr->p_align = 0;
  1201. return;
  1202. }
  1203. static void fill_note(struct memelfnote *note, const char *name, int type,
  1204. unsigned int sz, void *data)
  1205. {
  1206. note->name = name;
  1207. note->type = type;
  1208. note->datasz = sz;
  1209. note->data = data;
  1210. return;
  1211. }
  1212. /*
  1213. * fill up all the fields in prstatus from the given task struct, except
  1214. * registers which need to be filled up separately.
  1215. */
  1216. static void fill_prstatus(struct elf_prstatus *prstatus,
  1217. struct task_struct *p, long signr)
  1218. {
  1219. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  1220. prstatus->pr_sigpend = p->pending.signal.sig[0];
  1221. prstatus->pr_sighold = p->blocked.sig[0];
  1222. prstatus->pr_pid = task_pid_vnr(p);
  1223. prstatus->pr_ppid = task_pid_vnr(p->parent);
  1224. prstatus->pr_pgrp = task_pgrp_vnr(p);
  1225. prstatus->pr_sid = task_session_vnr(p);
  1226. if (thread_group_leader(p)) {
  1227. /*
  1228. * This is the record for the group leader. Add in the
  1229. * cumulative times of previous dead threads. This total
  1230. * won't include the time of each live thread whose state
  1231. * is included in the core dump. The final total reported
  1232. * to our parent process when it calls wait4 will include
  1233. * those sums as well as the little bit more time it takes
  1234. * this and each other thread to finish dying after the
  1235. * core dump synchronization phase.
  1236. */
  1237. cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
  1238. &prstatus->pr_utime);
  1239. cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
  1240. &prstatus->pr_stime);
  1241. } else {
  1242. cputime_to_timeval(p->utime, &prstatus->pr_utime);
  1243. cputime_to_timeval(p->stime, &prstatus->pr_stime);
  1244. }
  1245. cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
  1246. cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
  1247. }
  1248. static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
  1249. struct mm_struct *mm)
  1250. {
  1251. unsigned int i, len;
  1252. /* first copy the parameters from user space */
  1253. memset(psinfo, 0, sizeof(struct elf_prpsinfo));
  1254. len = mm->arg_end - mm->arg_start;
  1255. if (len >= ELF_PRARGSZ)
  1256. len = ELF_PRARGSZ-1;
  1257. if (copy_from_user(&psinfo->pr_psargs,
  1258. (const char __user *)mm->arg_start, len))
  1259. return -EFAULT;
  1260. for(i = 0; i < len; i++)
  1261. if (psinfo->pr_psargs[i] == 0)
  1262. psinfo->pr_psargs[i] = ' ';
  1263. psinfo->pr_psargs[len] = 0;
  1264. psinfo->pr_pid = task_pid_vnr(p);
  1265. psinfo->pr_ppid = task_pid_vnr(p->parent);
  1266. psinfo->pr_pgrp = task_pgrp_vnr(p);
  1267. psinfo->pr_sid = task_session_vnr(p);
  1268. i = p->state ? ffz(~p->state) + 1 : 0;
  1269. psinfo->pr_state = i;
  1270. psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
  1271. psinfo->pr_zomb = psinfo->pr_sname == 'Z';
  1272. psinfo->pr_nice = task_nice(p);
  1273. psinfo->pr_flag = p->flags;
  1274. SET_UID(psinfo->pr_uid, p->uid);
  1275. SET_GID(psinfo->pr_gid, p->gid);
  1276. strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
  1277. return 0;
  1278. }
  1279. /* Here is the structure in which status of each thread is captured. */
  1280. struct elf_thread_status
  1281. {
  1282. struct list_head list;
  1283. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  1284. elf_fpregset_t fpu; /* NT_PRFPREG */
  1285. struct task_struct *thread;
  1286. #ifdef ELF_CORE_COPY_XFPREGS
  1287. elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
  1288. #endif
  1289. struct memelfnote notes[3];
  1290. int num_notes;
  1291. };
  1292. /*
  1293. * In order to add the specific thread information for the elf file format,
  1294. * we need to keep a linked list of every threads pr_status and then create
  1295. * a single section for them in the final core file.
  1296. */
  1297. static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
  1298. {
  1299. int sz = 0;
  1300. struct task_struct *p = t->thread;
  1301. t->num_notes = 0;
  1302. fill_prstatus(&t->prstatus, p, signr);
  1303. elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
  1304. fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
  1305. &(t->prstatus));
  1306. t->num_notes++;
  1307. sz += notesize(&t->notes[0]);
  1308. if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
  1309. &t->fpu))) {
  1310. fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
  1311. &(t->fpu));
  1312. t->num_notes++;
  1313. sz += notesize(&t->notes[1]);
  1314. }
  1315. #ifdef ELF_CORE_COPY_XFPREGS
  1316. if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
  1317. fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
  1318. sizeof(t->xfpu), &t->xfpu);
  1319. t->num_notes++;
  1320. sz += notesize(&t->notes[2]);
  1321. }
  1322. #endif
  1323. return sz;
  1324. }
  1325. static struct vm_area_struct *first_vma(struct task_struct *tsk,
  1326. struct vm_area_struct *gate_vma)
  1327. {
  1328. struct vm_area_struct *ret = tsk->mm->mmap;
  1329. if (ret)
  1330. return ret;
  1331. return gate_vma;
  1332. }
  1333. /*
  1334. * Helper function for iterating across a vma list. It ensures that the caller
  1335. * will visit `gate_vma' prior to terminating the search.
  1336. */
  1337. static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
  1338. struct vm_area_struct *gate_vma)
  1339. {
  1340. struct vm_area_struct *ret;
  1341. ret = this_vma->vm_next;
  1342. if (ret)
  1343. return ret;
  1344. if (this_vma == gate_vma)
  1345. return NULL;
  1346. return gate_vma;
  1347. }
  1348. /*
  1349. * Actual dumper
  1350. *
  1351. * This is a two-pass process; first we find the offsets of the bits,
  1352. * and then they are actually written out. If we run out of core limit
  1353. * we just truncate.
  1354. */
  1355. static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
  1356. {
  1357. #define NUM_NOTES 6
  1358. int has_dumped = 0;
  1359. mm_segment_t fs;
  1360. int segs;
  1361. size_t size = 0;
  1362. int i;
  1363. struct vm_area_struct *vma, *gate_vma;
  1364. struct elfhdr *elf = NULL;
  1365. loff_t offset = 0, dataoff, foffset;
  1366. int numnote;
  1367. struct memelfnote *notes = NULL;
  1368. struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
  1369. struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
  1370. struct task_struct *g, *p;
  1371. LIST_HEAD(thread_list);
  1372. struct list_head *t;
  1373. elf_fpregset_t *fpu = NULL;
  1374. #ifdef ELF_CORE_COPY_XFPREGS
  1375. elf_fpxregset_t *xfpu = NULL;
  1376. #endif
  1377. int thread_status_size = 0;
  1378. elf_addr_t *auxv;
  1379. unsigned long mm_flags;
  1380. /*
  1381. * We no longer stop all VM operations.
  1382. *
  1383. * This is because those proceses that could possibly change map_count
  1384. * or the mmap / vma pages are now blocked in do_exit on current
  1385. * finishing this core dump.
  1386. *
  1387. * Only ptrace can touch these memory addresses, but it doesn't change
  1388. * the map_count or the pages allocated. So no possibility of crashing
  1389. * exists while dumping the mm->vm_next areas to the core file.
  1390. */
  1391. /* alloc memory for large data structures: too large to be on stack */
  1392. elf = kmalloc(sizeof(*elf), GFP_KERNEL);
  1393. if (!elf)
  1394. goto cleanup;
  1395. prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
  1396. if (!prstatus)
  1397. goto cleanup;
  1398. psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
  1399. if (!psinfo)
  1400. goto cleanup;
  1401. notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
  1402. if (!notes)
  1403. goto cleanup;
  1404. fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
  1405. if (!fpu)
  1406. goto cleanup;
  1407. #ifdef ELF_CORE_COPY_XFPREGS
  1408. xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
  1409. if (!xfpu)
  1410. goto cleanup;
  1411. #endif
  1412. if (signr) {
  1413. struct elf_thread_status *tmp;
  1414. rcu_read_lock();
  1415. do_each_thread(g,p)
  1416. if (current->mm == p->mm && current != p) {
  1417. tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
  1418. if (!tmp) {
  1419. rcu_read_unlock();
  1420. goto cleanup;
  1421. }
  1422. tmp->thread = p;
  1423. list_add(&tmp->list, &thread_list);
  1424. }
  1425. while_each_thread(g,p);
  1426. rcu_read_unlock();
  1427. list_for_each(t, &thread_list) {
  1428. struct elf_thread_status *tmp;
  1429. int sz;
  1430. tmp = list_entry(t, struct elf_thread_status, list);
  1431. sz = elf_dump_thread_status(signr, tmp);
  1432. thread_status_size += sz;
  1433. }
  1434. }
  1435. /* now collect the dump for the current */
  1436. memset(prstatus, 0, sizeof(*prstatus));
  1437. fill_prstatus(prstatus, current, signr);
  1438. elf_core_copy_regs(&prstatus->pr_reg, regs);
  1439. segs = current->mm->map_count;
  1440. #ifdef ELF_CORE_EXTRA_PHDRS
  1441. segs += ELF_CORE_EXTRA_PHDRS;
  1442. #endif
  1443. gate_vma = get_gate_vma(current);
  1444. if (gate_vma != NULL)
  1445. segs++;
  1446. /* Set up header */
  1447. fill_elf_header(elf, segs + 1); /* including notes section */
  1448. has_dumped = 1;
  1449. current->flags |= PF_DUMPCORE;
  1450. /*
  1451. * Set up the notes in similar form to SVR4 core dumps made
  1452. * with info from their /proc.
  1453. */
  1454. fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
  1455. fill_psinfo(psinfo, current->group_leader, current->mm);
  1456. fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
  1457. numnote = 2;
  1458. auxv = (elf_addr_t *)current->mm->saved_auxv;
  1459. i = 0;
  1460. do
  1461. i += 2;
  1462. while (auxv[i - 2] != AT_NULL);
  1463. fill_note(&notes[numnote++], "CORE", NT_AUXV,
  1464. i * sizeof(elf_addr_t), auxv);
  1465. /* Try to dump the FPU. */
  1466. if ((prstatus->pr_fpvalid =
  1467. elf_core_copy_task_fpregs(current, regs, fpu)))
  1468. fill_note(notes + numnote++,
  1469. "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
  1470. #ifdef ELF_CORE_COPY_XFPREGS
  1471. if (elf_core_copy_task_xfpregs(current, xfpu))
  1472. fill_note(notes + numnote++,
  1473. "LINUX", ELF_CORE_XFPREG_TYPE, sizeof(*xfpu), xfpu);
  1474. #endif
  1475. fs = get_fs();
  1476. set_fs(KERNEL_DS);
  1477. DUMP_WRITE(elf, sizeof(*elf));
  1478. offset += sizeof(*elf); /* Elf header */
  1479. offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
  1480. foffset = offset;
  1481. /* Write notes phdr entry */
  1482. {
  1483. struct elf_phdr phdr;
  1484. int sz = 0;
  1485. for (i = 0; i < numnote; i++)
  1486. sz += notesize(notes + i);
  1487. sz += thread_status_size;
  1488. sz += elf_coredump_extra_notes_size();
  1489. fill_elf_note_phdr(&phdr, sz, offset);
  1490. offset += sz;
  1491. DUMP_WRITE(&phdr, sizeof(phdr));
  1492. }
  1493. dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  1494. /*
  1495. * We must use the same mm->flags while dumping core to avoid
  1496. * inconsistency between the program headers and bodies, otherwise an
  1497. * unusable core file can be generated.
  1498. */
  1499. mm_flags = current->mm->flags;
  1500. /* Write program headers for segments dump */
  1501. for (vma = first_vma(current, gate_vma); vma != NULL;
  1502. vma = next_vma(vma, gate_vma)) {
  1503. struct elf_phdr phdr;
  1504. phdr.p_type = PT_LOAD;
  1505. phdr.p_offset = offset;
  1506. phdr.p_vaddr = vma->vm_start;
  1507. phdr.p_paddr = 0;
  1508. phdr.p_filesz = vma_dump_size(vma, mm_flags);
  1509. phdr.p_memsz = vma->vm_end - vma->vm_start;
  1510. offset += phdr.p_filesz;
  1511. phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
  1512. if (vma->vm_flags & VM_WRITE)
  1513. phdr.p_flags |= PF_W;
  1514. if (vma->vm_flags & VM_EXEC)
  1515. phdr.p_flags |= PF_X;
  1516. phdr.p_align = ELF_EXEC_PAGESIZE;
  1517. DUMP_WRITE(&phdr, sizeof(phdr));
  1518. }
  1519. #ifdef ELF_CORE_WRITE_EXTRA_PHDRS
  1520. ELF_CORE_WRITE_EXTRA_PHDRS;
  1521. #endif
  1522. /* write out the notes section */
  1523. for (i = 0; i < numnote; i++)
  1524. if (!writenote(notes + i, file, &foffset))
  1525. goto end_coredump;
  1526. if (elf_coredump_extra_notes_write(file, &foffset))
  1527. goto end_coredump;
  1528. /* write out the thread status notes section */
  1529. list_for_each(t, &thread_list) {
  1530. struct elf_thread_status *tmp =
  1531. list_entry(t, struct elf_thread_status, list);
  1532. for (i = 0; i < tmp->num_notes; i++)
  1533. if (!writenote(&tmp->notes[i], file, &foffset))
  1534. goto end_coredump;
  1535. }
  1536. /* Align to page */
  1537. DUMP_SEEK(dataoff - foffset);
  1538. for (vma = first_vma(current, gate_vma); vma != NULL;
  1539. vma = next_vma(vma, gate_vma)) {
  1540. unsigned long addr;
  1541. unsigned long end;
  1542. end = vma->vm_start + vma_dump_size(vma, mm_flags);
  1543. for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
  1544. struct page *page;
  1545. struct vm_area_struct *vma;
  1546. if (get_user_pages(current, current->mm, addr, 1, 0, 1,
  1547. &page, &vma) <= 0) {
  1548. DUMP_SEEK(PAGE_SIZE);
  1549. } else {
  1550. if (page == ZERO_PAGE(0)) {
  1551. if (!dump_seek(file, PAGE_SIZE)) {
  1552. page_cache_release(page);
  1553. goto end_coredump;
  1554. }
  1555. } else {
  1556. void *kaddr;
  1557. flush_cache_page(vma, addr,
  1558. page_to_pfn(page));
  1559. kaddr = kmap(page);
  1560. if ((size += PAGE_SIZE) > limit ||
  1561. !dump_write(file, kaddr,
  1562. PAGE_SIZE)) {
  1563. kunmap(page);
  1564. page_cache_release(page);
  1565. goto end_coredump;
  1566. }
  1567. kunmap(page);
  1568. }
  1569. page_cache_release(page);
  1570. }
  1571. }
  1572. }
  1573. #ifdef ELF_CORE_WRITE_EXTRA_DATA
  1574. ELF_CORE_WRITE_EXTRA_DATA;
  1575. #endif
  1576. end_coredump:
  1577. set_fs(fs);
  1578. cleanup:
  1579. while (!list_empty(&thread_list)) {
  1580. struct list_head *tmp = thread_list.next;
  1581. list_del(tmp);
  1582. kfree(list_entry(tmp, struct elf_thread_status, list));
  1583. }
  1584. kfree(elf);
  1585. kfree(prstatus);
  1586. kfree(psinfo);
  1587. kfree(notes);
  1588. kfree(fpu);
  1589. #ifdef ELF_CORE_COPY_XFPREGS
  1590. kfree(xfpu);
  1591. #endif
  1592. return has_dumped;
  1593. #undef NUM_NOTES
  1594. }
  1595. #endif /* USE_ELF_CORE_DUMP */
  1596. static int __init init_elf_binfmt(void)
  1597. {
  1598. return register_binfmt(&elf_format);
  1599. }
  1600. static void __exit exit_elf_binfmt(void)
  1601. {
  1602. /* Remove the COFF and ELF loaders. */
  1603. unregister_binfmt(&elf_format);
  1604. }
  1605. core_initcall(init_elf_binfmt);
  1606. module_exit(exit_elf_binfmt);
  1607. MODULE_LICENSE("GPL");