kcore.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/config.h>
  12. #include <linux/mm.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/user.h>
  15. #include <linux/a.out.h>
  16. #include <linux/capability.h>
  17. #include <linux/elf.h>
  18. #include <linux/elfcore.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/highmem.h>
  21. #include <linux/init.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/io.h>
  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. #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
  40. /* An ELF note in memory */
  41. struct memelfnote
  42. {
  43. const char *name;
  44. int type;
  45. unsigned int datasz;
  46. void *data;
  47. };
  48. static struct kcore_list *kclist;
  49. static DEFINE_RWLOCK(kclist_lock);
  50. void
  51. kclist_add(struct kcore_list *new, void *addr, size_t size)
  52. {
  53. new->addr = (unsigned long)addr;
  54. new->size = size;
  55. write_lock(&kclist_lock);
  56. new->next = kclist;
  57. kclist = new;
  58. write_unlock(&kclist_lock);
  59. }
  60. static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
  61. {
  62. size_t try, size;
  63. struct kcore_list *m;
  64. *nphdr = 1; /* PT_NOTE */
  65. size = 0;
  66. for (m=kclist; m; m=m->next) {
  67. try = kc_vaddr_to_offset((size_t)m->addr + m->size);
  68. if (try > size)
  69. size = try;
  70. *nphdr = *nphdr + 1;
  71. }
  72. *elf_buflen = sizeof(struct elfhdr) +
  73. (*nphdr + 2)*sizeof(struct elf_phdr) +
  74. 3 * (sizeof(struct elf_note) + 4) +
  75. sizeof(struct elf_prstatus) +
  76. sizeof(struct elf_prpsinfo) +
  77. sizeof(struct task_struct);
  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), 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);
  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";
  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";
  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";
  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 = kmalloc(elf_buflen, GFP_ATOMIC);
  240. if (!elf_buf) {
  241. read_unlock(&kclist_lock);
  242. return -ENOMEM;
  243. }
  244. memset(elf_buf, 0, elf_buflen);
  245. elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
  246. read_unlock(&kclist_lock);
  247. if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
  248. kfree(elf_buf);
  249. return -EFAULT;
  250. }
  251. kfree(elf_buf);
  252. buflen -= tsz;
  253. *fpos += tsz;
  254. buffer += tsz;
  255. acc += tsz;
  256. /* leave now if filled buffer already */
  257. if (buflen == 0)
  258. return acc;
  259. } else
  260. read_unlock(&kclist_lock);
  261. /*
  262. * Check to see if our file offset matches with any of
  263. * the addresses in the elf_phdr on our list.
  264. */
  265. start = kc_offset_to_vaddr(*fpos - elf_buflen);
  266. if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
  267. tsz = buflen;
  268. while (buflen) {
  269. struct kcore_list *m;
  270. read_lock(&kclist_lock);
  271. for (m=kclist; m; m=m->next) {
  272. if (start >= m->addr && start < (m->addr+m->size))
  273. break;
  274. }
  275. read_unlock(&kclist_lock);
  276. if (m == NULL) {
  277. if (clear_user(buffer, tsz))
  278. return -EFAULT;
  279. } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
  280. char * elf_buf;
  281. struct vm_struct *m;
  282. unsigned long curstart = start;
  283. unsigned long cursize = tsz;
  284. elf_buf = kmalloc(tsz, GFP_KERNEL);
  285. if (!elf_buf)
  286. return -ENOMEM;
  287. memset(elf_buf, 0, tsz);
  288. read_lock(&vmlist_lock);
  289. for (m=vmlist; m && cursize; m=m->next) {
  290. unsigned long vmstart;
  291. unsigned long vmsize;
  292. unsigned long msize = m->size - PAGE_SIZE;
  293. if (((unsigned long)m->addr + msize) <
  294. curstart)
  295. continue;
  296. if ((unsigned long)m->addr > (curstart +
  297. cursize))
  298. break;
  299. vmstart = (curstart < (unsigned long)m->addr ?
  300. (unsigned long)m->addr : curstart);
  301. if (((unsigned long)m->addr + msize) >
  302. (curstart + cursize))
  303. vmsize = curstart + cursize - vmstart;
  304. else
  305. vmsize = (unsigned long)m->addr +
  306. msize - vmstart;
  307. curstart = vmstart + vmsize;
  308. cursize -= vmsize;
  309. /* don't dump ioremap'd stuff! (TA) */
  310. if (m->flags & VM_IOREMAP)
  311. continue;
  312. memcpy(elf_buf + (vmstart - start),
  313. (char *)vmstart, vmsize);
  314. }
  315. read_unlock(&vmlist_lock);
  316. if (copy_to_user(buffer, elf_buf, tsz)) {
  317. kfree(elf_buf);
  318. return -EFAULT;
  319. }
  320. kfree(elf_buf);
  321. } else {
  322. if (kern_addr_valid(start)) {
  323. unsigned long n;
  324. n = copy_to_user(buffer, (char *)start, tsz);
  325. /*
  326. * We cannot distingush between fault on source
  327. * and fault on destination. When this happens
  328. * we clear too and hope it will trigger the
  329. * EFAULT again.
  330. */
  331. if (n) {
  332. if (clear_user(buffer + tsz - n,
  333. tsz - n))
  334. return -EFAULT;
  335. }
  336. } else {
  337. if (clear_user(buffer, tsz))
  338. return -EFAULT;
  339. }
  340. }
  341. buflen -= tsz;
  342. *fpos += tsz;
  343. buffer += tsz;
  344. acc += tsz;
  345. start += tsz;
  346. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  347. }
  348. return acc;
  349. }