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