binfmt_elf.c 54 KB

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