kcore.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/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. 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. #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
  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) + 4) +
  74. sizeof(struct elf_prstatus) +
  75. sizeof(struct elf_prpsinfo) +
  76. sizeof(struct task_struct);
  77. *elf_buflen = PAGE_ALIGN(*elf_buflen);
  78. return size + *elf_buflen;
  79. }
  80. /*****************************************************************************/
  81. /*
  82. * determine size of ELF note
  83. */
  84. static int notesize(struct memelfnote *en)
  85. {
  86. int sz;
  87. sz = sizeof(struct elf_note);
  88. sz += roundup(strlen(en->name), 4);
  89. sz += roundup(en->datasz, 4);
  90. return sz;
  91. } /* end notesize() */
  92. /*****************************************************************************/
  93. /*
  94. * store a note in the header buffer
  95. */
  96. static char *storenote(struct memelfnote *men, char *bufp)
  97. {
  98. struct elf_note en;
  99. #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
  100. en.n_namesz = strlen(men->name);
  101. en.n_descsz = men->datasz;
  102. en.n_type = men->type;
  103. DUMP_WRITE(&en, sizeof(en));
  104. DUMP_WRITE(men->name, en.n_namesz);
  105. /* XXX - cast from long long to long to avoid need for libgcc.a */
  106. bufp = (char*) roundup((unsigned long)bufp,4);
  107. DUMP_WRITE(men->data, men->datasz);
  108. bufp = (char*) roundup((unsigned long)bufp,4);
  109. #undef DUMP_WRITE
  110. return bufp;
  111. } /* end storenote() */
  112. /*
  113. * store an ELF coredump header in the supplied buffer
  114. * nphdr is the number of elf_phdr to insert
  115. */
  116. static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
  117. {
  118. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  119. struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
  120. struct elf_phdr *nhdr, *phdr;
  121. struct elfhdr *elf;
  122. struct memelfnote notes[3];
  123. off_t offset = 0;
  124. struct kcore_list *m;
  125. /* setup ELF header */
  126. elf = (struct elfhdr *) bufp;
  127. bufp += sizeof(struct elfhdr);
  128. offset += sizeof(struct elfhdr);
  129. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  130. elf->e_ident[EI_CLASS] = ELF_CLASS;
  131. elf->e_ident[EI_DATA] = ELF_DATA;
  132. elf->e_ident[EI_VERSION]= EV_CURRENT;
  133. elf->e_ident[EI_OSABI] = ELF_OSABI;
  134. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  135. elf->e_type = ET_CORE;
  136. elf->e_machine = ELF_ARCH;
  137. elf->e_version = EV_CURRENT;
  138. elf->e_entry = 0;
  139. elf->e_phoff = sizeof(struct elfhdr);
  140. elf->e_shoff = 0;
  141. #if defined(CONFIG_H8300)
  142. elf->e_flags = ELF_FLAGS;
  143. #else
  144. elf->e_flags = 0;
  145. #endif
  146. elf->e_ehsize = sizeof(struct elfhdr);
  147. elf->e_phentsize= sizeof(struct elf_phdr);
  148. elf->e_phnum = nphdr;
  149. elf->e_shentsize= 0;
  150. elf->e_shnum = 0;
  151. elf->e_shstrndx = 0;
  152. /* setup ELF PT_NOTE program header */
  153. nhdr = (struct elf_phdr *) bufp;
  154. bufp += sizeof(struct elf_phdr);
  155. offset += sizeof(struct elf_phdr);
  156. nhdr->p_type = PT_NOTE;
  157. nhdr->p_offset = 0;
  158. nhdr->p_vaddr = 0;
  159. nhdr->p_paddr = 0;
  160. nhdr->p_filesz = 0;
  161. nhdr->p_memsz = 0;
  162. nhdr->p_flags = 0;
  163. nhdr->p_align = 0;
  164. /* setup ELF PT_LOAD program header for every area */
  165. for (m=kclist; m; m=m->next) {
  166. phdr = (struct elf_phdr *) bufp;
  167. bufp += sizeof(struct elf_phdr);
  168. offset += sizeof(struct elf_phdr);
  169. phdr->p_type = PT_LOAD;
  170. phdr->p_flags = PF_R|PF_W|PF_X;
  171. phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
  172. phdr->p_vaddr = (size_t)m->addr;
  173. phdr->p_paddr = 0;
  174. phdr->p_filesz = phdr->p_memsz = m->size;
  175. phdr->p_align = PAGE_SIZE;
  176. }
  177. /*
  178. * Set up the notes in similar form to SVR4 core dumps made
  179. * with info from their /proc.
  180. */
  181. nhdr->p_offset = offset;
  182. /* set up the process status */
  183. notes[0].name = "CORE";
  184. notes[0].type = NT_PRSTATUS;
  185. notes[0].datasz = sizeof(struct elf_prstatus);
  186. notes[0].data = &prstatus;
  187. memset(&prstatus, 0, sizeof(struct elf_prstatus));
  188. nhdr->p_filesz = notesize(&notes[0]);
  189. bufp = storenote(&notes[0], bufp);
  190. /* set up the process info */
  191. notes[1].name = "CORE";
  192. notes[1].type = NT_PRPSINFO;
  193. notes[1].datasz = sizeof(struct elf_prpsinfo);
  194. notes[1].data = &prpsinfo;
  195. memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
  196. prpsinfo.pr_state = 0;
  197. prpsinfo.pr_sname = 'R';
  198. prpsinfo.pr_zomb = 0;
  199. strcpy(prpsinfo.pr_fname, "vmlinux");
  200. strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
  201. nhdr->p_filesz += notesize(&notes[1]);
  202. bufp = storenote(&notes[1], bufp);
  203. /* set up the task structure */
  204. notes[2].name = "CORE";
  205. notes[2].type = NT_TASKSTRUCT;
  206. notes[2].datasz = sizeof(struct task_struct);
  207. notes[2].data = current;
  208. nhdr->p_filesz += notesize(&notes[2]);
  209. bufp = storenote(&notes[2], bufp);
  210. } /* end elf_kcore_store_hdr() */
  211. /*****************************************************************************/
  212. /*
  213. * read from the ELF header and then kernel memory
  214. */
  215. static ssize_t
  216. read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
  217. {
  218. ssize_t acc = 0;
  219. size_t size, tsz;
  220. size_t elf_buflen;
  221. int nphdr;
  222. unsigned long start;
  223. read_lock(&kclist_lock);
  224. proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
  225. if (buflen == 0 || *fpos >= size) {
  226. read_unlock(&kclist_lock);
  227. return 0;
  228. }
  229. /* trim buflen to not go beyond EOF */
  230. if (buflen > size - *fpos)
  231. buflen = size - *fpos;
  232. /* construct an ELF core header if we'll need some of it */
  233. if (*fpos < elf_buflen) {
  234. char * elf_buf;
  235. tsz = elf_buflen - *fpos;
  236. if (buflen < tsz)
  237. tsz = buflen;
  238. elf_buf = kmalloc(elf_buflen, GFP_ATOMIC);
  239. if (!elf_buf) {
  240. read_unlock(&kclist_lock);
  241. return -ENOMEM;
  242. }
  243. memset(elf_buf, 0, elf_buflen);
  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 = kmalloc(tsz, GFP_KERNEL);
  284. if (!elf_buf)
  285. return -ENOMEM;
  286. memset(elf_buf, 0, tsz);
  287. read_lock(&vmlist_lock);
  288. for (m=vmlist; m && cursize; m=m->next) {
  289. unsigned long vmstart;
  290. unsigned long vmsize;
  291. unsigned long msize = m->size - PAGE_SIZE;
  292. if (((unsigned long)m->addr + msize) <
  293. curstart)
  294. continue;
  295. if ((unsigned long)m->addr > (curstart +
  296. cursize))
  297. break;
  298. vmstart = (curstart < (unsigned long)m->addr ?
  299. (unsigned long)m->addr : curstart);
  300. if (((unsigned long)m->addr + msize) >
  301. (curstart + cursize))
  302. vmsize = curstart + cursize - vmstart;
  303. else
  304. vmsize = (unsigned long)m->addr +
  305. msize - vmstart;
  306. curstart = vmstart + vmsize;
  307. cursize -= vmsize;
  308. /* don't dump ioremap'd stuff! (TA) */
  309. if (m->flags & VM_IOREMAP)
  310. continue;
  311. memcpy(elf_buf + (vmstart - start),
  312. (char *)vmstart, vmsize);
  313. }
  314. read_unlock(&vmlist_lock);
  315. if (copy_to_user(buffer, elf_buf, tsz)) {
  316. kfree(elf_buf);
  317. return -EFAULT;
  318. }
  319. kfree(elf_buf);
  320. } else {
  321. if (kern_addr_valid(start)) {
  322. unsigned long n;
  323. n = copy_to_user(buffer, (char *)start, tsz);
  324. /*
  325. * We cannot distingush between fault on source
  326. * and fault on destination. When this happens
  327. * we clear too and hope it will trigger the
  328. * EFAULT again.
  329. */
  330. if (n) {
  331. if (clear_user(buffer + tsz - n,
  332. tsz - n))
  333. return -EFAULT;
  334. }
  335. } else {
  336. if (clear_user(buffer, tsz))
  337. return -EFAULT;
  338. }
  339. }
  340. buflen -= tsz;
  341. *fpos += tsz;
  342. buffer += tsz;
  343. acc += tsz;
  344. start += tsz;
  345. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  346. }
  347. return acc;
  348. }