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