kcore.c 9.7 KB

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