binfmt_elf.c 55 KB

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