binfmt_elf.c 49 KB

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