binfmt_elf.c 48 KB

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