kcore.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/capability.h>
  15. #include <linux/elf.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/highmem.h>
  19. #include <linux/init.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/io.h>
  22. #define CORE_STR "CORE"
  23. #ifndef ELF_CORE_EFLAGS
  24. #define ELF_CORE_EFLAGS 0
  25. #endif
  26. static struct proc_dir_entry *proc_root_kcore;
  27. static int open_kcore(struct inode * inode, struct file * filp)
  28. {
  29. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  30. }
  31. static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
  32. static const struct file_operations proc_kcore_operations = {
  33. .read = read_kcore,
  34. .open = open_kcore,
  35. };
  36. #ifndef kc_vaddr_to_offset
  37. #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
  38. #endif
  39. #ifndef kc_offset_to_vaddr
  40. #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
  41. #endif
  42. /* An ELF note in memory */
  43. struct memelfnote
  44. {
  45. const char *name;
  46. int type;
  47. unsigned int datasz;
  48. void *data;
  49. };
  50. static struct kcore_list *kclist;
  51. static DEFINE_RWLOCK(kclist_lock);
  52. void
  53. kclist_add(struct kcore_list *new, void *addr, size_t size)
  54. {
  55. new->addr = (unsigned long)addr;
  56. new->size = size;
  57. write_lock(&kclist_lock);
  58. new->next = kclist;
  59. kclist = new;
  60. write_unlock(&kclist_lock);
  61. }
  62. static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
  63. {
  64. size_t try, size;
  65. struct kcore_list *m;
  66. *nphdr = 1; /* PT_NOTE */
  67. size = 0;
  68. for (m=kclist; m; m=m->next) {
  69. try = kc_vaddr_to_offset((size_t)m->addr + m->size);
  70. if (try > size)
  71. size = try;
  72. *nphdr = *nphdr + 1;
  73. }
  74. *elf_buflen = sizeof(struct elfhdr) +
  75. (*nphdr + 2)*sizeof(struct elf_phdr) +
  76. 3 * ((sizeof(struct elf_note)) +
  77. roundup(sizeof(CORE_STR), 4)) +
  78. roundup(sizeof(struct elf_prstatus), 4) +
  79. roundup(sizeof(struct elf_prpsinfo), 4) +
  80. roundup(sizeof(struct task_struct), 4);
  81. *elf_buflen = PAGE_ALIGN(*elf_buflen);
  82. return size + *elf_buflen;
  83. }
  84. /*****************************************************************************/
  85. /*
  86. * determine size of ELF note
  87. */
  88. static int notesize(struct memelfnote *en)
  89. {
  90. int sz;
  91. sz = sizeof(struct elf_note);
  92. sz += roundup((strlen(en->name) + 1), 4);
  93. sz += roundup(en->datasz, 4);
  94. return sz;
  95. } /* end notesize() */
  96. /*****************************************************************************/
  97. /*
  98. * store a note in the header buffer
  99. */
  100. static char *storenote(struct memelfnote *men, char *bufp)
  101. {
  102. struct elf_note en;
  103. #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
  104. en.n_namesz = strlen(men->name) + 1;
  105. en.n_descsz = men->datasz;
  106. en.n_type = men->type;
  107. DUMP_WRITE(&en, sizeof(en));
  108. DUMP_WRITE(men->name, en.n_namesz);
  109. /* XXX - cast from long long to long to avoid need for libgcc.a */
  110. bufp = (char*) roundup((unsigned long)bufp,4);
  111. DUMP_WRITE(men->data, men->datasz);
  112. bufp = (char*) roundup((unsigned long)bufp,4);
  113. #undef DUMP_WRITE
  114. return bufp;
  115. } /* end storenote() */
  116. /*
  117. * store an ELF coredump header in the supplied buffer
  118. * nphdr is the number of elf_phdr to insert
  119. */
  120. static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
  121. {
  122. struct elf_prstatus prstatus; /* NT_PRSTATUS */
  123. struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
  124. struct elf_phdr *nhdr, *phdr;
  125. struct elfhdr *elf;
  126. struct memelfnote notes[3];
  127. off_t offset = 0;
  128. struct kcore_list *m;
  129. /* setup ELF header */
  130. elf = (struct elfhdr *) bufp;
  131. bufp += sizeof(struct elfhdr);
  132. offset += sizeof(struct elfhdr);
  133. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  134. elf->e_ident[EI_CLASS] = ELF_CLASS;
  135. elf->e_ident[EI_DATA] = ELF_DATA;
  136. elf->e_ident[EI_VERSION]= EV_CURRENT;
  137. elf->e_ident[EI_OSABI] = ELF_OSABI;
  138. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  139. elf->e_type = ET_CORE;
  140. elf->e_machine = ELF_ARCH;
  141. elf->e_version = EV_CURRENT;
  142. elf->e_entry = 0;
  143. elf->e_phoff = sizeof(struct elfhdr);
  144. elf->e_shoff = 0;
  145. elf->e_flags = ELF_CORE_EFLAGS;
  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_STR;
  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_STR;
  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_STR;
  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 = kzalloc(elf_buflen, GFP_ATOMIC);
  239. if (!elf_buf) {
  240. read_unlock(&kclist_lock);
  241. return -ENOMEM;
  242. }
  243. elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
  244. read_unlock(&kclist_lock);
  245. if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
  246. kfree(elf_buf);
  247. return -EFAULT;
  248. }
  249. kfree(elf_buf);
  250. buflen -= tsz;
  251. *fpos += tsz;
  252. buffer += tsz;
  253. acc += tsz;
  254. /* leave now if filled buffer already */
  255. if (buflen == 0)
  256. return acc;
  257. } else
  258. read_unlock(&kclist_lock);
  259. /*
  260. * Check to see if our file offset matches with any of
  261. * the addresses in the elf_phdr on our list.
  262. */
  263. start = kc_offset_to_vaddr(*fpos - elf_buflen);
  264. if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
  265. tsz = buflen;
  266. while (buflen) {
  267. struct kcore_list *m;
  268. read_lock(&kclist_lock);
  269. for (m=kclist; m; m=m->next) {
  270. if (start >= m->addr && start < (m->addr+m->size))
  271. break;
  272. }
  273. read_unlock(&kclist_lock);
  274. if (m == NULL) {
  275. if (clear_user(buffer, tsz))
  276. return -EFAULT;
  277. } else if (is_vmalloc_addr((void *)start)) {
  278. char * elf_buf;
  279. struct vm_struct *m;
  280. unsigned long curstart = start;
  281. unsigned long cursize = tsz;
  282. elf_buf = kzalloc(tsz, GFP_KERNEL);
  283. if (!elf_buf)
  284. return -ENOMEM;
  285. read_lock(&vmlist_lock);
  286. for (m=vmlist; m && cursize; m=m->next) {
  287. unsigned long vmstart;
  288. unsigned long vmsize;
  289. unsigned long msize = m->size - PAGE_SIZE;
  290. if (((unsigned long)m->addr + msize) <
  291. curstart)
  292. continue;
  293. if ((unsigned long)m->addr > (curstart +
  294. cursize))
  295. break;
  296. vmstart = (curstart < (unsigned long)m->addr ?
  297. (unsigned long)m->addr : curstart);
  298. if (((unsigned long)m->addr + msize) >
  299. (curstart + cursize))
  300. vmsize = curstart + cursize - vmstart;
  301. else
  302. vmsize = (unsigned long)m->addr +
  303. msize - vmstart;
  304. curstart = vmstart + vmsize;
  305. cursize -= vmsize;
  306. /* don't dump ioremap'd stuff! (TA) */
  307. if (m->flags & VM_IOREMAP)
  308. continue;
  309. memcpy(elf_buf + (vmstart - start),
  310. (char *)vmstart, vmsize);
  311. }
  312. read_unlock(&vmlist_lock);
  313. if (copy_to_user(buffer, elf_buf, tsz)) {
  314. kfree(elf_buf);
  315. return -EFAULT;
  316. }
  317. kfree(elf_buf);
  318. } else {
  319. if (kern_addr_valid(start)) {
  320. unsigned long n;
  321. n = copy_to_user(buffer, (char *)start, tsz);
  322. /*
  323. * We cannot distingush between fault on source
  324. * and fault on destination. When this happens
  325. * we clear too and hope it will trigger the
  326. * EFAULT again.
  327. */
  328. if (n) {
  329. if (clear_user(buffer + tsz - n,
  330. n))
  331. return -EFAULT;
  332. }
  333. } else {
  334. if (clear_user(buffer, tsz))
  335. return -EFAULT;
  336. }
  337. }
  338. buflen -= tsz;
  339. *fpos += tsz;
  340. buffer += tsz;
  341. acc += tsz;
  342. start += tsz;
  343. tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
  344. }
  345. return acc;
  346. }
  347. static int __init proc_kcore_init(void)
  348. {
  349. proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
  350. if (proc_root_kcore)
  351. proc_root_kcore->size =
  352. (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
  353. return 0;
  354. }
  355. module_init(proc_kcore_init);