binfmt_elf.c 46 KB

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