xref: /linux/fs/proc/kcore.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
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/config.h>
13 #include <linux/mm.h>
14 #include <linux/proc_fs.h>
15 #include <linux/user.h>
16 #include <linux/a.out.h>
17 #include <linux/capability.h>
18 #include <linux/elf.h>
19 #include <linux/elfcore.h>
20 #include <linux/vmalloc.h>
21 #include <linux/highmem.h>
22 #include <linux/init.h>
23 #include <asm/uaccess.h>
24 #include <asm/io.h>
25 
26 
27 static int open_kcore(struct inode * inode, struct file * filp)
28 {
29 	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
30 }
31 
32 static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *);
33 
34 const struct file_operations proc_kcore_operations = {
35 	.read		= read_kcore,
36 	.open		= open_kcore,
37 };
38 
39 #ifndef kc_vaddr_to_offset
40 #define	kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
41 #endif
42 #ifndef	kc_offset_to_vaddr
43 #define	kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
44 #endif
45 
46 #define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))
47 
48 /* An ELF note in memory */
49 struct memelfnote
50 {
51 	const char *name;
52 	int type;
53 	unsigned int datasz;
54 	void *data;
55 };
56 
57 static struct kcore_list *kclist;
58 static DEFINE_RWLOCK(kclist_lock);
59 
60 void
61 kclist_add(struct kcore_list *new, void *addr, size_t size)
62 {
63 	new->addr = (unsigned long)addr;
64 	new->size = size;
65 
66 	write_lock(&kclist_lock);
67 	new->next = kclist;
68 	kclist = new;
69 	write_unlock(&kclist_lock);
70 }
71 
72 static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
73 {
74 	size_t try, size;
75 	struct kcore_list *m;
76 
77 	*nphdr = 1; /* PT_NOTE */
78 	size = 0;
79 
80 	for (m=kclist; m; m=m->next) {
81 		try = kc_vaddr_to_offset((size_t)m->addr + m->size);
82 		if (try > size)
83 			size = try;
84 		*nphdr = *nphdr + 1;
85 	}
86 	*elf_buflen =	sizeof(struct elfhdr) +
87 			(*nphdr + 2)*sizeof(struct elf_phdr) +
88 			3 * (sizeof(struct elf_note) + 4) +
89 			sizeof(struct elf_prstatus) +
90 			sizeof(struct elf_prpsinfo) +
91 			sizeof(struct task_struct);
92 	*elf_buflen = PAGE_ALIGN(*elf_buflen);
93 	return size + *elf_buflen;
94 }
95 
96 
97 /*****************************************************************************/
98 /*
99  * determine size of ELF note
100  */
101 static int notesize(struct memelfnote *en)
102 {
103 	int sz;
104 
105 	sz = sizeof(struct elf_note);
106 	sz += roundup(strlen(en->name), 4);
107 	sz += roundup(en->datasz, 4);
108 
109 	return sz;
110 } /* end notesize() */
111 
112 /*****************************************************************************/
113 /*
114  * store a note in the header buffer
115  */
116 static char *storenote(struct memelfnote *men, char *bufp)
117 {
118 	struct elf_note en;
119 
120 #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
121 
122 	en.n_namesz = strlen(men->name);
123 	en.n_descsz = men->datasz;
124 	en.n_type = men->type;
125 
126 	DUMP_WRITE(&en, sizeof(en));
127 	DUMP_WRITE(men->name, en.n_namesz);
128 
129 	/* XXX - cast from long long to long to avoid need for libgcc.a */
130 	bufp = (char*) roundup((unsigned long)bufp,4);
131 	DUMP_WRITE(men->data, men->datasz);
132 	bufp = (char*) roundup((unsigned long)bufp,4);
133 
134 #undef DUMP_WRITE
135 
136 	return bufp;
137 } /* end storenote() */
138 
139 /*
140  * store an ELF coredump header in the supplied buffer
141  * nphdr is the number of elf_phdr to insert
142  */
143 static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
144 {
145 	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
146 	struct elf_prpsinfo prpsinfo;	/* NT_PRPSINFO */
147 	struct elf_phdr *nhdr, *phdr;
148 	struct elfhdr *elf;
149 	struct memelfnote notes[3];
150 	off_t offset = 0;
151 	struct kcore_list *m;
152 
153 	/* setup ELF header */
154 	elf = (struct elfhdr *) bufp;
155 	bufp += sizeof(struct elfhdr);
156 	offset += sizeof(struct elfhdr);
157 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
158 	elf->e_ident[EI_CLASS]	= ELF_CLASS;
159 	elf->e_ident[EI_DATA]	= ELF_DATA;
160 	elf->e_ident[EI_VERSION]= EV_CURRENT;
161 	elf->e_ident[EI_OSABI] = ELF_OSABI;
162 	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
163 	elf->e_type	= ET_CORE;
164 	elf->e_machine	= ELF_ARCH;
165 	elf->e_version	= EV_CURRENT;
166 	elf->e_entry	= 0;
167 	elf->e_phoff	= sizeof(struct elfhdr);
168 	elf->e_shoff	= 0;
169 #if defined(CONFIG_H8300)
170 	elf->e_flags	= ELF_FLAGS;
171 #else
172 	elf->e_flags	= 0;
173 #endif
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";
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";
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";
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 = kmalloc(elf_buflen, GFP_ATOMIC);
286 		if (!elf_buf) {
287 			read_unlock(&kclist_lock);
288 			return -ENOMEM;
289 		}
290 		memset(elf_buf, 0, elf_buflen);
291 		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);
321 		for (m=kclist; m; m=m->next) {
322 			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;
330 		} else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) {
331 			char * elf_buf;
332 			struct vm_struct *m;
333 			unsigned long curstart = start;
334 			unsigned long cursize = tsz;
335 
336 			elf_buf = kmalloc(tsz, GFP_KERNEL);
337 			if (!elf_buf)
338 				return -ENOMEM;
339 			memset(elf_buf, 0, tsz);
340 
341 			read_lock(&vmlist_lock);
342 			for (m=vmlist; m && cursize; m=m->next) {
343 				unsigned long vmstart;
344 				unsigned long vmsize;
345 				unsigned long msize = m->size - PAGE_SIZE;
346 
347 				if (((unsigned long)m->addr + msize) <
348 								curstart)
349 					continue;
350 				if ((unsigned long)m->addr > (curstart +
351 								cursize))
352 					break;
353 				vmstart = (curstart < (unsigned long)m->addr ?
354 					(unsigned long)m->addr : curstart);
355 				if (((unsigned long)m->addr + msize) >
356 							(curstart + cursize))
357 					vmsize = curstart + cursize - vmstart;
358 				else
359 					vmsize = (unsigned long)m->addr +
360 							msize - vmstart;
361 				curstart = vmstart + vmsize;
362 				cursize -= vmsize;
363 				/* don't dump ioremap'd stuff! (TA) */
364 				if (m->flags & VM_IOREMAP)
365 					continue;
366 				memcpy(elf_buf + (vmstart - start),
367 					(char *)vmstart, vmsize);
368 			}
369 			read_unlock(&vmlist_lock);
370 			if (copy_to_user(buffer, elf_buf, tsz)) {
371 				kfree(elf_buf);
372 				return -EFAULT;
373 			}
374 			kfree(elf_buf);
375 		} else {
376 			if (kern_addr_valid(start)) {
377 				unsigned long n;
378 
379 				n = copy_to_user(buffer, (char *)start, tsz);
380 				/*
381 				 * We cannot distingush between fault on source
382 				 * and fault on destination. When this happens
383 				 * we clear too and hope it will trigger the
384 				 * EFAULT again.
385 				 */
386 				if (n) {
387 					if (clear_user(buffer + tsz - n,
388 								tsz - n))
389 						return -EFAULT;
390 				}
391 			} else {
392 				if (clear_user(buffer, tsz))
393 					return -EFAULT;
394 			}
395 		}
396 		buflen -= tsz;
397 		*fpos += tsz;
398 		buffer += tsz;
399 		acc += tsz;
400 		start += tsz;
401 		tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
402 	}
403 
404 	return acc;
405 }
406