blob: e10d360110bdf4e84fcc484972b18f6e611c6a5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/proc_fs.h>
14#include <linux/user.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070023#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Magnus Damm36027602006-12-06 20:38:00 -080025#define CORE_STR "CORE"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Edgar E. Iglesias79885b22008-07-25 01:48:10 -070027#ifndef ELF_CORE_EFLAGS
28#define ELF_CORE_EFLAGS 0
29#endif
30
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +040031static struct proc_dir_entry *proc_root_kcore;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int open_kcore(struct inode * inode, struct file * filp)
34{
35 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
36}
37
38static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
39
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +040040static const struct file_operations proc_kcore_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 .read = read_kcore,
42 .open = open_kcore,
43};
44
45#ifndef kc_vaddr_to_offset
46#define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
47#endif
48#ifndef kc_offset_to_vaddr
49#define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
50#endif
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* An ELF note in memory */
53struct memelfnote
54{
55 const char *name;
56 int type;
57 unsigned int datasz;
58 void *data;
59};
60
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070061static LIST_HEAD(kclist_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_RWLOCK(kclist_lock);
63
64void
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -070065kclist_add(struct kcore_list *new, void *addr, size_t size, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 new->addr = (unsigned long)addr;
68 new->size = size;
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -070069 new->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 write_lock(&kclist_lock);
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070072 list_add_tail(&new->list, &kclist_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 write_unlock(&kclist_lock);
74}
75
76static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
77{
78 size_t try, size;
79 struct kcore_list *m;
80
81 *nphdr = 1; /* PT_NOTE */
82 size = 0;
83
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070084 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
86 if (try > size)
87 size = try;
88 *nphdr = *nphdr + 1;
89 }
90 *elf_buflen = sizeof(struct elfhdr) +
91 (*nphdr + 2)*sizeof(struct elf_phdr) +
Magnus Damm36027602006-12-06 20:38:00 -080092 3 * ((sizeof(struct elf_note)) +
93 roundup(sizeof(CORE_STR), 4)) +
94 roundup(sizeof(struct elf_prstatus), 4) +
95 roundup(sizeof(struct elf_prpsinfo), 4) +
96 roundup(sizeof(struct task_struct), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *elf_buflen = PAGE_ALIGN(*elf_buflen);
98 return size + *elf_buflen;
99}
100
101
102/*****************************************************************************/
103/*
104 * determine size of ELF note
105 */
106static int notesize(struct memelfnote *en)
107{
108 int sz;
109
110 sz = sizeof(struct elf_note);
Vivek Goyal632dd202006-09-29 02:01:45 -0700111 sz += roundup((strlen(en->name) + 1), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 sz += roundup(en->datasz, 4);
113
114 return sz;
115} /* end notesize() */
116
117/*****************************************************************************/
118/*
119 * store a note in the header buffer
120 */
121static char *storenote(struct memelfnote *men, char *bufp)
122{
123 struct elf_note en;
124
125#define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
126
Vivek Goyal632dd202006-09-29 02:01:45 -0700127 en.n_namesz = strlen(men->name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 en.n_descsz = men->datasz;
129 en.n_type = men->type;
130
131 DUMP_WRITE(&en, sizeof(en));
132 DUMP_WRITE(men->name, en.n_namesz);
133
134 /* XXX - cast from long long to long to avoid need for libgcc.a */
135 bufp = (char*) roundup((unsigned long)bufp,4);
136 DUMP_WRITE(men->data, men->datasz);
137 bufp = (char*) roundup((unsigned long)bufp,4);
138
139#undef DUMP_WRITE
140
141 return bufp;
142} /* end storenote() */
143
144/*
145 * store an ELF coredump header in the supplied buffer
146 * nphdr is the number of elf_phdr to insert
147 */
148static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
149{
150 struct elf_prstatus prstatus; /* NT_PRSTATUS */
151 struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */
152 struct elf_phdr *nhdr, *phdr;
153 struct elfhdr *elf;
154 struct memelfnote notes[3];
155 off_t offset = 0;
156 struct kcore_list *m;
157
158 /* setup ELF header */
159 elf = (struct elfhdr *) bufp;
160 bufp += sizeof(struct elfhdr);
161 offset += sizeof(struct elfhdr);
162 memcpy(elf->e_ident, ELFMAG, SELFMAG);
163 elf->e_ident[EI_CLASS] = ELF_CLASS;
164 elf->e_ident[EI_DATA] = ELF_DATA;
165 elf->e_ident[EI_VERSION]= EV_CURRENT;
166 elf->e_ident[EI_OSABI] = ELF_OSABI;
167 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
168 elf->e_type = ET_CORE;
169 elf->e_machine = ELF_ARCH;
170 elf->e_version = EV_CURRENT;
171 elf->e_entry = 0;
172 elf->e_phoff = sizeof(struct elfhdr);
173 elf->e_shoff = 0;
Edgar E. Iglesias79885b22008-07-25 01:48:10 -0700174 elf->e_flags = ELF_CORE_EFLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 elf->e_ehsize = sizeof(struct elfhdr);
176 elf->e_phentsize= sizeof(struct elf_phdr);
177 elf->e_phnum = nphdr;
178 elf->e_shentsize= 0;
179 elf->e_shnum = 0;
180 elf->e_shstrndx = 0;
181
182 /* setup ELF PT_NOTE program header */
183 nhdr = (struct elf_phdr *) bufp;
184 bufp += sizeof(struct elf_phdr);
185 offset += sizeof(struct elf_phdr);
186 nhdr->p_type = PT_NOTE;
187 nhdr->p_offset = 0;
188 nhdr->p_vaddr = 0;
189 nhdr->p_paddr = 0;
190 nhdr->p_filesz = 0;
191 nhdr->p_memsz = 0;
192 nhdr->p_flags = 0;
193 nhdr->p_align = 0;
194
195 /* setup ELF PT_LOAD program header for every area */
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -0700196 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 phdr = (struct elf_phdr *) bufp;
198 bufp += sizeof(struct elf_phdr);
199 offset += sizeof(struct elf_phdr);
200
201 phdr->p_type = PT_LOAD;
202 phdr->p_flags = PF_R|PF_W|PF_X;
203 phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff;
204 phdr->p_vaddr = (size_t)m->addr;
205 phdr->p_paddr = 0;
206 phdr->p_filesz = phdr->p_memsz = m->size;
207 phdr->p_align = PAGE_SIZE;
208 }
209
210 /*
211 * Set up the notes in similar form to SVR4 core dumps made
212 * with info from their /proc.
213 */
214 nhdr->p_offset = offset;
215
216 /* set up the process status */
Magnus Damm36027602006-12-06 20:38:00 -0800217 notes[0].name = CORE_STR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 notes[0].type = NT_PRSTATUS;
219 notes[0].datasz = sizeof(struct elf_prstatus);
220 notes[0].data = &prstatus;
221
222 memset(&prstatus, 0, sizeof(struct elf_prstatus));
223
224 nhdr->p_filesz = notesize(&notes[0]);
225 bufp = storenote(&notes[0], bufp);
226
227 /* set up the process info */
Magnus Damm36027602006-12-06 20:38:00 -0800228 notes[1].name = CORE_STR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 notes[1].type = NT_PRPSINFO;
230 notes[1].datasz = sizeof(struct elf_prpsinfo);
231 notes[1].data = &prpsinfo;
232
233 memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
234 prpsinfo.pr_state = 0;
235 prpsinfo.pr_sname = 'R';
236 prpsinfo.pr_zomb = 0;
237
238 strcpy(prpsinfo.pr_fname, "vmlinux");
239 strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ);
240
241 nhdr->p_filesz += notesize(&notes[1]);
242 bufp = storenote(&notes[1], bufp);
243
244 /* set up the task structure */
Magnus Damm36027602006-12-06 20:38:00 -0800245 notes[2].name = CORE_STR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 notes[2].type = NT_TASKSTRUCT;
247 notes[2].datasz = sizeof(struct task_struct);
248 notes[2].data = current;
249
250 nhdr->p_filesz += notesize(&notes[2]);
251 bufp = storenote(&notes[2], bufp);
252
253} /* end elf_kcore_store_hdr() */
254
255/*****************************************************************************/
256/*
257 * read from the ELF header and then kernel memory
258 */
259static ssize_t
260read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
261{
262 ssize_t acc = 0;
263 size_t size, tsz;
264 size_t elf_buflen;
265 int nphdr;
266 unsigned long start;
267
268 read_lock(&kclist_lock);
269 proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
270 if (buflen == 0 || *fpos >= size) {
271 read_unlock(&kclist_lock);
272 return 0;
273 }
274
275 /* trim buflen to not go beyond EOF */
276 if (buflen > size - *fpos)
277 buflen = size - *fpos;
278
279 /* construct an ELF core header if we'll need some of it */
280 if (*fpos < elf_buflen) {
281 char * elf_buf;
282
283 tsz = elf_buflen - *fpos;
284 if (buflen < tsz)
285 tsz = buflen;
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700286 elf_buf = kzalloc(elf_buflen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!elf_buf) {
288 read_unlock(&kclist_lock);
289 return -ENOMEM;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
292 read_unlock(&kclist_lock);
293 if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
294 kfree(elf_buf);
295 return -EFAULT;
296 }
297 kfree(elf_buf);
298 buflen -= tsz;
299 *fpos += tsz;
300 buffer += tsz;
301 acc += tsz;
302
303 /* leave now if filled buffer already */
304 if (buflen == 0)
305 return acc;
306 } else
307 read_unlock(&kclist_lock);
308
309 /*
310 * Check to see if our file offset matches with any of
311 * the addresses in the elf_phdr on our list.
312 */
313 start = kc_offset_to_vaddr(*fpos - elf_buflen);
314 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
315 tsz = buflen;
316
317 while (buflen) {
318 struct kcore_list *m;
319
320 read_lock(&kclist_lock);
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -0700321 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (start >= m->addr && start < (m->addr+m->size))
323 break;
324 }
325 read_unlock(&kclist_lock);
326
327 if (m == NULL) {
328 if (clear_user(buffer, tsz))
329 return -EFAULT;
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800330 } else if (is_vmalloc_addr((void *)start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 char * elf_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700333 elf_buf = kzalloc(tsz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!elf_buf)
335 return -ENOMEM;
KAMEZAWA Hiroyuki73d7c332009-09-21 17:02:35 -0700336 vread(elf_buf, (char *)start, tsz);
337 /* we have to zero-fill user buffer even if no read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (copy_to_user(buffer, elf_buf, tsz)) {
339 kfree(elf_buf);
340 return -EFAULT;
341 }
342 kfree(elf_buf);
343 } else {
344 if (kern_addr_valid(start)) {
345 unsigned long n;
346
347 n = copy_to_user(buffer, (char *)start, tsz);
348 /*
349 * We cannot distingush between fault on source
350 * and fault on destination. When this happens
351 * we clear too and hope it will trigger the
352 * EFAULT again.
353 */
354 if (n) {
355 if (clear_user(buffer + tsz - n,
Adam B. Jerome06351702006-07-12 09:03:07 -0700356 n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EFAULT;
358 }
359 } else {
360 if (clear_user(buffer, tsz))
361 return -EFAULT;
362 }
363 }
364 buflen -= tsz;
365 *fpos += tsz;
366 buffer += tsz;
367 acc += tsz;
368 start += tsz;
369 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
370 }
371
372 return acc;
373}
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400374
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700375static struct kcore_list kcore_vmalloc;
376
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400377static int __init proc_kcore_init(void)
378{
379 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700380
381 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
382 VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400383 return 0;
384}
385module_init(proc_kcore_init);