kcore.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * fs/proc/kcore.c kernel ELF core dumper
  3. *
  4. * Modelled on fs/exec.c:aout_core_dump()
  5. * Jeremy Fitzhardinge <jeremy@sw.oz.au>
  6. * ELF version written by David Howells <David.Howells@nexor.co.uk>
  7. * Modified and incorporated into 2.3.x by Tigran Aivazian <tigran@veritas.com>
  8. * Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <tigran@veritas.com>
  9. * Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <kanoj@sgi.com>
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/user.h>
  14. #include <linux/a.out.h>
  15. #include <linux/capability.h>
  16. #include <linux/elf.h>
  17. #include <linux/elfcore.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/highmem.h>
  20. #include <linux/init.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/io.h>
  23. #define CORE_STR "CORE"
  24. static int open_kcore(struct inode * inode, struct file * filp)
  25. {
  26. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  27. }
  28. static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
  29. const struct file_operations proc_kcore_operations = {
  30. .read = read_kcore,
  31. .open = open_kcore,
  32. };
  33. #ifndef kc_vaddr_to_offset
  34. #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
  35. #endif
  36. #ifndef kc_offset_to_vaddr
  37. #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
  38. #endif
  39. /* An ELF note in memory */
  40. struct memelfnote
  41. {
  42. const char *name;
  43. int type;
  44. unsigned int datasz;
  45. void *data;
  46. };
  47. static struct kcore_list *kclist;
  48. static DEFINE_RWLOCK(kclist_lock);
  49. void
  50. kclist_add(struct kcore_list *new, void *addr, size_t size)
  51. {
  52. new->addr = (unsigned long)addr;
  53. new->size = size;
  54. write_lock(&kclist_lock);
  55. new->next = kclist;
  56. kclist = new;
  57. write_unlock(&kclist_lock);
  58. }
  59. static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
  60. {
  61. size_t try, size;
  62. struct kcore_list *m;
  63. *nphdr = 1; /* PT_NOTE */
  64. size = 0;
  65. for (m=kclist; m; m=m->next) {
  66. try = kc_vaddr_to_offset((size_t)m->addr + m->size);
  67. if (try > size)
  68. size = try;
  69. *nphdr = *nphdr + 1;
  70. }
  71. *elf_buflen = sizeof(struct elfhdr) +
  72. (*nphdr + 2)*sizeof(struct elf_phdr) +
  73. 3 * ((sizeof(struct elf_note)) +
  74. roundup(sizeof(CORE_STR), 4)) +
  75. roundup(sizeof(struct elf_prstatus), 4) +
  76. roundup(sizeof(struct elf_prpsinfo), 4) +
  77. roundup(sizeof(struct task_struct), 4);
  78. *elf_buflen = PAGE_ALIGN(*elf_buflen);
  79. return size + *elf_buflen;
  80. }
  81. /*****************************************************************************/
  82. /*
  83. * determine size of ELF note
  84. */
  85. static int notesize(struct memelfnote *en)
  86. {
  87. int sz;
  88. sz = sizeof(struct elf_note);
  89. sz += roundup((strlen(en->name) + 1), 4);
  90. sz += roundup(en->datasz, 4);
  91. return sz;
  92. } /* end notesize() */
  93. /*****************************************************************************/
  94. /*
  95. * store a note in the header buffer
  96. */
  97. static char *storenote(struct memelfnote *men, char *bufp)
  98. {
  99. struct elf_note en;
  100. #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
  101. en.n_namesz = strlen(men->name) + 1;
  102. en.n_descsz = men->datasz;
  103. en.n_type = men->type;
  104. DUMP_WRITE(&en, sizeof(en));
  105. DUMP_WRITE(men->name, en.n_namesz);
  106. /* XXX - cast from long long to long to avoid need for libgcc.a */
  107. bufp = (char*) roundup((unsigned long)bufp,4);
  108. DUMP_WRITE(men->data, men->datasz);
  109. bufp = (char*) roundup((unsigned long)bufp,4);
  110. #undef DUMP_WRITE
  111. return bufp;
  112. } /* end storenote() */
  113. /*
  114. * store an ELF coredump header in the supplied buffer
  115. * nphdr is the number of elf_phdr to insert
  116. */
  117. static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
  118. {
  119. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  120. struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
  121. struct elf_phdr *nhdr, *phdr;
  122. struct elfhdr *elf;
  123. struct memelfnote notes[3];
  124. off_t offset = 0;
  125. struct kcore_list *m;
  126. /* setup ELF header */
  127. elf = (struct elfhdr *) bufp;
  128. bufp += sizeof(struct elfhdr);
  129. offset += sizeof(struct elfhdr);
  130. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  131. elf->e_ident[EI_CLASS] = ELF_CLASS;
  132. elf->e_ident[EI_DATA] = ELF_DATA;
  133. elf->e_ident[EI_VERSION]= EV_CURRENT;
  134. elf->e_ident[EI_OSABI] = ELF_OSABI;
  135. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  136. elf->e_type = ET_CORE;
  137. elf->e_machine = ELF_ARCH;
  138. elf->e_version = EV_CURRENT;
  139. elf->e_entry = 0;
  140. elf->e_phoff = sizeof(struct elfhdr);
  141. elf->e_shoff = 0;
  142. #if defined(CONFIG_H8300)
  143. elf->e_flags = ELF_FLAGS;
  144. #else
  145. elf->e_flags = 0;
  146. #endif
  147. elf->e_ehsize = sizeof(struct elfhdr);
  148. elf->e_phentsize= sizeof(struct elf_phdr);
  149. elf->e_phnum = nphdr;
  150. elf->e_shentsize= 0;
  151. elf->e_shnum = 0;
  152. elf->e_shstrndx = 0;
  153. /* setup ELF PT_NOTE program header */
  154. nhdr = (struct elf_phdr *) bufp;
  155. bufp += sizeof(struct elf_phdr);
  156. offset += sizeof(struct elf_phdr);
  157. nhdr->p_type = PT_NOTE;
  158. nhdr->p_offset = 0;
  159. nhdr->p_vaddr = 0;
  160. nhdr->p_paddr = 0;
  161. nhdr->p_filesz = 0;
  162. nhdr->p_memsz = 0;
  163. nhdr->p_flags = 0;
  164. nhdr->p_align = 0;
  165. /* setup ELF PT_LOAD program header for every area */
  166. for (m=kclist; m; m=m->next) {
  167. phdr = (struct elf_phdr *) bufp;
  168. bufp += sizeof(struct elf_phdr);
  169. offset += sizeof(struct elf_phdr);
  170. phdr->p_type = PT_LOAD;
  171. phdr->p_flags = PF_R|PF_W|PF_X;
  172. phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
  173. phdr->p_vaddr = (size_t)m->addr;
  174. phdr->p_paddr = 0;
  175. phdr->p_filesz = phdr->p_memsz = m->size;
  176. phdr->p_align = PAGE_SIZE;
  177. }
  178. /*
  179. * Set up the notes in similar form to SVR4 core dumps made
  180. * with info from their /proc.
  181. */
  182. nhdr->p_offset = offset;
  183. /* set up the process status */
  184. notes[0].name = CORE_STR;
  185. notes[0].type = NT_PRSTATUS;
  186. notes[0].datasz = sizeof(struct elf_prstatus);
  187. notes[0].data = &prstatus;
  188. memset(&prstatus, 0, sizeof(struct elf_prstatus));
  189. nhdr->p_filesz = notesize(&notes[0]);
  190. bufp = storenote(&notes[0], bufp);
  191. /* set up the process info */
  192. notes[1].name = CORE_STR;
  193. notes[1].type = NT_PRPSINFO;
  194. notes[1].datasz = sizeof(struct elf_prpsinfo);
  195. notes[1].data = &prpsinfo;
  196. memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
  197. prpsinfo.pr_state = 0;
  198. prpsinfo.pr_sname = 'R';
  199. prpsinfo.pr_zomb = 0;
  200. strcpy(prpsinfo.pr_fname, "vmlinux");
  201. strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
  202. nhdr->p_filesz += notesize(&notes[1]);
  203. bufp = storenote(&notes[1], bufp);
  204. /* set up the task structure */
  205. notes[2].name = CORE_STR;
  206. notes[2].type = NT_TASKSTRUCT;
  207. notes[2].datasz = sizeof(struct task_struct);
  208. notes[2].data = current;
  209. nhdr->p_filesz += notesize(&notes[2]);
  210. bufp = storenote(&notes[2], bufp);
  211. } /* end elf_kcore_store_hdr() */
  212. /*****************************************************************************/
  213. /*
  214. * read from the ELF header and then kernel memory
  215. */
  216. static ssize_t
  217. read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
  218. {
  219. ssize_t acc = 0;
  220. size_t size, tsz;
  221. size_t elf_buflen;
  222. int nphdr;
  223. unsigned long start;
  224. read_lock(&kclist_lock);
  225. proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
  226. if (buflen == 0 || *fpos >= size) {
  227. read_unlock(&kclist_lock);
  228. return 0;
  229. }
  230. /* trim buflen to not go beyond EOF */
  231. if (buflen > size - *fpos)
  232. buflen = size - *fpos;
  233. /* construct an ELF core header if we'll need some of it */
  234. if (*fpos < elf_buflen) {
  235. char * elf_buf;
  236. tsz = elf_buflen - *fpos;
  237. if (buflen < tsz)
  238. tsz = buflen;
  239. elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
  240. if (!elf_buf) {
  241. read_unlock(&kclist_lock);
  242. return -ENOMEM;
  243. }
  244. elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
  245. read_unlock(&kclist_lock);
  246. if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
  247. kfree(elf_buf);
  248. return -EFAULT;
  249. }
  250. kfree(elf_buf);
  251. buflen -= tsz;
  252. *fpos += tsz;
  253. buffer += tsz;
  254. acc += tsz;
  255. /* leave now if filled buffer already */
  256. if (buflen == 0)
  257. return acc;
  258. } else
  259. read_unlock(&kclist_lock);
  260. /*
  261. * Check to see if our file offset matches with any of
  262. * the addresses in the elf_phdr on our list.
  263. */
  264. start = kc_offset_to_vaddr(*fpos - elf_buflen);
  265. if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
  266. tsz = buflen;
  267. while (buflen) {
  268. struct kcore_list *m;
  269. read_lock(&kclist_lock);
  270. for (m=kclist; m; m=m->next) {
  271. if (start >= m->addr && start < (m->addr+m->size))
  272. break;
  273. }
  274. read_unlock(&kclist_lock);
  275. if (m == NULL) {
  276. if (clear_user(buffer, tsz))
  277. return -EFAULT;
  278. } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
  279. char * elf_buf;
  280. struct vm_struct *m;
  281. unsigned long curstart = start;
  282. unsigned long cursize = tsz;
  283. elf_buf = kzalloc(tsz, GFP_KERNEL);
  284. if (!elf_buf)
  285. return -ENOMEM;
  286. read_lock(&vmlist_lock);
  287. for (m=vmlist; m && cursize; m=m->next) {
  288. unsigned long vmstart;
  289. unsigned long vmsize;
  290. unsigned long msize = m->size - PAGE_SIZE;
  291. if (((unsigned long)m->addr + msize) <
  292. curstart)
  293. continue;
  294. if ((unsigned long)m->addr > (curstart +
  295. cursize))
  296. break;
  297. vmstart = (curstart < (unsigned long)m->addr ?
  298. (unsigned long)m->addr : curstart);
  299. if (((unsigned long)m->addr + msize) >
  300. (curstart + cursize))
  301. vmsize = curstart + cursize - vmstart;
  302. else
  303. vmsize = (unsigned long)m->addr +
  304. msize - vmstart;
  305. curstart = vmstart + vmsize;
  306. cursize -= vmsize;
  307. /* don't dump ioremap'd stuff! (TA) */
  308. if (m->flags & VM_IOREMAP)
  309. continue;
  310. memcpy(elf_buf + (vmstart - start),
  311. (char *)vmstart, vmsize);
  312. }
  313. read_unlock(&vmlist_lock);
  314. if (copy_to_user(buffer, elf_buf, tsz)) {
  315. kfree(elf_buf);
  316. return -EFAULT;
  317. }
  318. kfree(elf_buf);
  319. } else {
  320. if (kern_addr_valid(start)) {
  321. unsigned long n;
  322. n = copy_to_user(buffer, (char *)start, tsz);
  323. /*
  324. * We cannot distingush between fault on source
  325. * and fault on destination. When this happens
  326. * we clear too and hope it will trigger the
  327. * EFAULT again.
  328. */
  329. if (n) {
  330. if (clear_user(buffer + tsz - n,
  331. n))
  332. return -EFAULT;
  333. }
  334. } else {
  335. if (clear_user(buffer, tsz))
  336. return -EFAULT;
  337. }
  338. }
  339. buflen -= tsz;
  340. *fpos += tsz;
  341. buffer += tsz;
  342. acc += tsz;
  343. start += tsz;
  344. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  345. }
  346. return acc;
  347. }