binfmt_elf.c 46 KB

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