xref: /freebsd/sys/dev/ksyms/ksyms.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 2008-2009, Stacey Son <sson@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 
33 #include <sys/conf.h>
34 #include <sys/elf.h>
35 #include <sys/linker.h>
36 #include <sys/malloc.h>
37 #include <sys/mman.h>
38 #include <sys/module.h>
39 #include <sys/proc.h>
40 #include <sys/queue.h>
41 #include <sys/resourcevar.h>
42 #include <sys/stat.h>
43 #include <sys/sx.h>
44 #include <sys/uio.h>
45 
46 #include <machine/elf.h>
47 
48 #include <vm/pmap.h>
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_object.h>
52 
53 #include "linker_if.h"
54 
55 #define SHDR_NULL	0
56 #define SHDR_SYMTAB	1
57 #define SHDR_STRTAB	2
58 #define SHDR_SHSTRTAB	3
59 
60 #define SHDR_NUM	4
61 
62 #define STR_SYMTAB	".symtab"
63 #define STR_STRTAB	".strtab"
64 #define STR_SHSTRTAB	".shstrtab"
65 
66 #define KSYMS_DNAME	"ksyms"
67 
68 static d_open_t ksyms_open;
69 static d_read_t ksyms_read;
70 static d_mmap_single_t ksyms_mmap_single;
71 
72 static struct cdevsw ksyms_cdevsw = {
73 	.d_version =	D_VERSION,
74 	.d_flags =	0,
75 	.d_open =	ksyms_open,
76 	.d_read =	ksyms_read,
77 	.d_mmap_single = ksyms_mmap_single,
78 	.d_name =	KSYMS_DNAME
79 };
80 
81 struct ksyms_softc {
82 	LIST_ENTRY(ksyms_softc)	sc_list;
83 	vm_offset_t		sc_uaddr;
84 	size_t			sc_usize;
85 	vm_object_t		sc_obj;
86 	vm_size_t		sc_objsz;
87 	struct proc	       *sc_proc;
88 };
89 
90 static struct sx		 ksyms_mtx;
91 static struct cdev		*ksyms_dev;
92 static LIST_HEAD(, ksyms_softc)	 ksyms_list = LIST_HEAD_INITIALIZER(ksyms_list);
93 
94 static const char	ksyms_shstrtab[] =
95 	"\0" STR_SYMTAB "\0" STR_STRTAB "\0" STR_SHSTRTAB "\0";
96 
97 struct ksyms_hdr {
98 	Elf_Ehdr	kh_ehdr;
99 	Elf_Phdr	kh_txtphdr;
100 	Elf_Phdr	kh_datphdr;
101 	Elf_Shdr	kh_shdr[SHDR_NUM];
102 	char		kh_shstrtab[sizeof(ksyms_shstrtab)];
103 };
104 
105 struct tsizes {
106 	size_t		ts_symsz;
107 	size_t		ts_strsz;
108 };
109 
110 struct toffsets {
111 	struct ksyms_softc *to_sc;
112 	vm_offset_t	to_symoff;
113 	vm_offset_t	to_stroff;
114 	unsigned	to_stridx;
115 	size_t		to_resid;
116 };
117 
118 static MALLOC_DEFINE(M_KSYMS, "KSYMS", "Kernel Symbol Table");
119 
120 /*
121  * Get the symbol and string table sizes for a kernel module. Add it to the
122  * running total.
123  */
124 static int
125 ksyms_size_permod(linker_file_t lf, void *arg)
126 {
127 	struct tsizes *ts;
128 	const Elf_Sym *symtab;
129 	caddr_t strtab;
130 	long syms;
131 
132 	ts = arg;
133 
134 	syms = LINKER_SYMTAB_GET(lf, &symtab);
135 	ts->ts_symsz += syms * sizeof(Elf_Sym);
136 	ts->ts_strsz += LINKER_STRTAB_GET(lf, &strtab);
137 
138 	return (0);
139 }
140 
141 /*
142  * For kernel module get the symbol and string table sizes, returning the
143  * totals in *ts.
144  */
145 static void
146 ksyms_size_calc(struct tsizes *ts)
147 {
148 
149 	ts->ts_symsz = 0;
150 	ts->ts_strsz = 0;
151 
152 	(void)linker_file_foreach(ksyms_size_permod, ts);
153 }
154 
155 static int
156 ksyms_emit(struct ksyms_softc *sc, void *buf, off_t off, size_t sz)
157 {
158 	struct iovec iov;
159 	struct uio uio;
160 
161 	iov.iov_base = buf;
162 	iov.iov_len = sz;
163 	uio.uio_iov = &iov;
164 	uio.uio_iovcnt = 1;
165 	uio.uio_offset = off;
166 	uio.uio_resid = (ssize_t)sz;
167 	uio.uio_segflg = UIO_SYSSPACE;
168 	uio.uio_rw = UIO_WRITE;
169 	uio.uio_td = curthread;
170 
171 	return (uiomove_object(sc->sc_obj, sc->sc_objsz, &uio));
172 }
173 
174 #define SYMBLKSZ	(256 * sizeof(Elf_Sym))
175 
176 /*
177  * For a kernel module, add the symbol and string tables into the
178  * snapshot buffer.  Fix up the offsets in the tables.
179  */
180 static int
181 ksyms_add(linker_file_t lf, void *arg)
182 {
183 	char *buf;
184 	struct ksyms_softc *sc;
185 	struct toffsets *to;
186 	const Elf_Sym *symtab;
187 	Elf_Sym *symp;
188 	caddr_t strtab;
189 	size_t len, numsyms, strsz, symsz;
190 	linker_symval_t symval;
191 	int error, i, nsyms;
192 
193 	buf = malloc(SYMBLKSZ, M_KSYMS, M_WAITOK);
194 	to = arg;
195 	sc = to->to_sc;
196 
197 	MOD_SLOCK;
198 	numsyms =  LINKER_SYMTAB_GET(lf, &symtab);
199 	strsz = LINKER_STRTAB_GET(lf, &strtab);
200 	symsz = numsyms * sizeof(Elf_Sym);
201 
202 	while (symsz > 0) {
203 		len = min(SYMBLKSZ, symsz);
204 		bcopy(symtab, buf, len);
205 
206 		/*
207 		 * Fix up symbol table for kernel modules:
208 		 *   string offsets need adjusted
209 		 *   symbol values made absolute
210 		 */
211 		symp = (Elf_Sym *) buf;
212 		nsyms = len / sizeof(Elf_Sym);
213 		for (i = 0; i < nsyms; i++) {
214 			symp[i].st_name += to->to_stridx;
215 			if (lf->id > 1 && LINKER_SYMBOL_VALUES(lf,
216 			    (c_linker_sym_t)&symtab[i], &symval) == 0) {
217 				symp[i].st_value = (uintptr_t)symval.value;
218 			}
219 		}
220 
221 		if (len > to->to_resid) {
222 			MOD_SUNLOCK;
223 			free(buf, M_KSYMS);
224 			return (ENXIO);
225 		}
226 		to->to_resid -= len;
227 		error = ksyms_emit(sc, buf, to->to_symoff, len);
228 		to->to_symoff += len;
229 		if (error != 0) {
230 			MOD_SUNLOCK;
231 			free(buf, M_KSYMS);
232 			return (error);
233 		}
234 
235 		symtab += nsyms;
236 		symsz -= len;
237 	}
238 	free(buf, M_KSYMS);
239 	MOD_SUNLOCK;
240 
241 	if (strsz > to->to_resid)
242 		return (ENXIO);
243 	to->to_resid -= strsz;
244 	error = ksyms_emit(sc, strtab, to->to_stroff, strsz);
245 	to->to_stroff += strsz;
246 	to->to_stridx += strsz;
247 
248 	return (error);
249 }
250 
251 /*
252  * Create a single ELF symbol table for the kernel and kernel modules loaded
253  * at this time. Write this snapshot out in the process address space. Return
254  * 0 on success, otherwise error.
255  */
256 static int
257 ksyms_snapshot(struct ksyms_softc *sc, struct tsizes *ts)
258 {
259 	struct toffsets	to;
260 	struct ksyms_hdr *hdr;
261 	int error;
262 
263 	hdr = malloc(sizeof(*hdr), M_KSYMS, M_WAITOK | M_ZERO);
264 
265 	/*
266 	 * Create the ELF header.
267 	 */
268 	hdr->kh_ehdr.e_ident[EI_PAD] = 0;
269 	hdr->kh_ehdr.e_ident[EI_MAG0] = ELFMAG0;
270 	hdr->kh_ehdr.e_ident[EI_MAG1] = ELFMAG1;
271 	hdr->kh_ehdr.e_ident[EI_MAG2] = ELFMAG2;
272 	hdr->kh_ehdr.e_ident[EI_MAG3] = ELFMAG3;
273 	hdr->kh_ehdr.e_ident[EI_DATA] = ELF_DATA;
274 	hdr->kh_ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
275 	hdr->kh_ehdr.e_ident[EI_CLASS] = ELF_CLASS;
276 	hdr->kh_ehdr.e_ident[EI_VERSION] = EV_CURRENT;
277 	hdr->kh_ehdr.e_ident[EI_ABIVERSION] = 0;
278 	hdr->kh_ehdr.e_type = ET_EXEC;
279 	hdr->kh_ehdr.e_machine = ELF_ARCH;
280 	hdr->kh_ehdr.e_version = EV_CURRENT;
281 	hdr->kh_ehdr.e_entry = 0;
282 	hdr->kh_ehdr.e_phoff = offsetof(struct ksyms_hdr, kh_txtphdr);
283 	hdr->kh_ehdr.e_shoff = offsetof(struct ksyms_hdr, kh_shdr);
284 	hdr->kh_ehdr.e_flags = 0;
285 	hdr->kh_ehdr.e_ehsize = sizeof(Elf_Ehdr);
286 	hdr->kh_ehdr.e_phentsize = sizeof(Elf_Phdr);
287 	hdr->kh_ehdr.e_phnum = 2;	/* Text and Data */
288 	hdr->kh_ehdr.e_shentsize = sizeof(Elf_Shdr);
289 	hdr->kh_ehdr.e_shnum = SHDR_NUM;
290 	hdr->kh_ehdr.e_shstrndx = SHDR_SHSTRTAB;
291 
292 	/*
293 	 * Add both the text and data program headers.
294 	 */
295 	hdr->kh_txtphdr.p_type = PT_LOAD;
296 	/* XXX - is there a way to put the actual .text addr/size here? */
297 	hdr->kh_txtphdr.p_vaddr = 0;
298 	hdr->kh_txtphdr.p_memsz = 0;
299 	hdr->kh_txtphdr.p_flags = PF_R | PF_X;
300 
301 	hdr->kh_datphdr.p_type = PT_LOAD;
302 	/* XXX - is there a way to put the actual .data addr/size here? */
303 	hdr->kh_datphdr.p_vaddr = 0;
304 	hdr->kh_datphdr.p_memsz = 0;
305 	hdr->kh_datphdr.p_flags = PF_R | PF_W | PF_X;
306 
307 	/*
308 	 * Add the section headers: null, symtab, strtab, shstrtab.
309 	 */
310 
311 	/* First section header - null */
312 
313 	/* Second section header - symtab */
314 	hdr->kh_shdr[SHDR_SYMTAB].sh_name = 1; /* String offset (skip null) */
315 	hdr->kh_shdr[SHDR_SYMTAB].sh_type = SHT_SYMTAB;
316 	hdr->kh_shdr[SHDR_SYMTAB].sh_flags = 0;
317 	hdr->kh_shdr[SHDR_SYMTAB].sh_addr = 0;
318 	hdr->kh_shdr[SHDR_SYMTAB].sh_offset = sizeof(*hdr);
319 	hdr->kh_shdr[SHDR_SYMTAB].sh_size = ts->ts_symsz;
320 	hdr->kh_shdr[SHDR_SYMTAB].sh_link = SHDR_STRTAB;
321 	hdr->kh_shdr[SHDR_SYMTAB].sh_info = ts->ts_symsz / sizeof(Elf_Sym);
322 	hdr->kh_shdr[SHDR_SYMTAB].sh_addralign = sizeof(long);
323 	hdr->kh_shdr[SHDR_SYMTAB].sh_entsize = sizeof(Elf_Sym);
324 
325 	/* Third section header - strtab */
326 	hdr->kh_shdr[SHDR_STRTAB].sh_name = 1 + sizeof(STR_SYMTAB);
327 	hdr->kh_shdr[SHDR_STRTAB].sh_type = SHT_STRTAB;
328 	hdr->kh_shdr[SHDR_STRTAB].sh_flags = 0;
329 	hdr->kh_shdr[SHDR_STRTAB].sh_addr = 0;
330 	hdr->kh_shdr[SHDR_STRTAB].sh_offset =
331 	    hdr->kh_shdr[SHDR_SYMTAB].sh_offset + ts->ts_symsz;
332 	hdr->kh_shdr[SHDR_STRTAB].sh_size = ts->ts_strsz;
333 	hdr->kh_shdr[SHDR_STRTAB].sh_link = 0;
334 	hdr->kh_shdr[SHDR_STRTAB].sh_info = 0;
335 	hdr->kh_shdr[SHDR_STRTAB].sh_addralign = sizeof(char);
336 	hdr->kh_shdr[SHDR_STRTAB].sh_entsize = 0;
337 
338 	/* Fourth section - shstrtab */
339 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_name = 1 + sizeof(STR_SYMTAB) +
340 	    sizeof(STR_STRTAB);
341 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_type = SHT_STRTAB;
342 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_flags = 0;
343 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_addr = 0;
344 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_offset =
345 	    offsetof(struct ksyms_hdr, kh_shstrtab);
346 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_size = sizeof(ksyms_shstrtab);
347 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_link = 0;
348 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_info = 0;
349 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_addralign = 0 /* sizeof(char) */;
350 	hdr->kh_shdr[SHDR_SHSTRTAB].sh_entsize = 0;
351 
352 	/* Copy shstrtab into the header. */
353 	bcopy(ksyms_shstrtab, hdr->kh_shstrtab, sizeof(ksyms_shstrtab));
354 
355 	to.to_sc = sc;
356 	to.to_symoff = hdr->kh_shdr[SHDR_SYMTAB].sh_offset;
357 	to.to_stroff = hdr->kh_shdr[SHDR_STRTAB].sh_offset;
358 	to.to_stridx = 0;
359 	to.to_resid = sc->sc_objsz - sizeof(struct ksyms_hdr);
360 
361 	/* emit header */
362 	error = ksyms_emit(sc, hdr, 0, sizeof(*hdr));
363 	free(hdr, M_KSYMS);
364 	if (error != 0)
365 		return (error);
366 
367 	/* Add symbol and string tables for each kernel module. */
368 	error = linker_file_foreach(ksyms_add, &to);
369 	if (error != 0)
370 		return (error);
371 	if (to.to_resid != 0)
372 		return (ENXIO);
373 	return (0);
374 }
375 
376 static void
377 ksyms_cdevpriv_dtr(void *data)
378 {
379 	struct ksyms_softc *sc;
380 	vm_object_t obj;
381 
382 	sc = (struct ksyms_softc *)data;
383 
384 	sx_xlock(&ksyms_mtx);
385 	LIST_REMOVE(sc, sc_list);
386 	sx_xunlock(&ksyms_mtx);
387 	obj = sc->sc_obj;
388 	if (obj != NULL)
389 		vm_object_deallocate(obj);
390 	free(sc, M_KSYMS);
391 }
392 
393 static int
394 ksyms_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
395 {
396 	struct tsizes ts;
397 	struct ksyms_softc *sc;
398 	vm_size_t elfsz;
399 	int error, try;
400 
401 	/*
402 	 * Limit one open() per process. The process must close()
403 	 * before open()'ing again.
404 	 */
405 	sx_xlock(&ksyms_mtx);
406 	LIST_FOREACH(sc, &ksyms_list, sc_list) {
407 		if (sc->sc_proc == td->td_proc) {
408 			sx_xunlock(&ksyms_mtx);
409 			return (EBUSY);
410 		}
411 	}
412 
413 	sc = malloc(sizeof(*sc), M_KSYMS, M_WAITOK | M_ZERO);
414 	sc->sc_proc = td->td_proc;
415 	LIST_INSERT_HEAD(&ksyms_list, sc, sc_list);
416 	sx_xunlock(&ksyms_mtx);
417 
418 	error = devfs_set_cdevpriv(sc, ksyms_cdevpriv_dtr);
419 	if (error != 0) {
420 		ksyms_cdevpriv_dtr(sc);
421 		return (error);
422 	}
423 
424 	/*
425 	 * MOD_SLOCK doesn't work here (because of a lock reversal with
426 	 * KLD_SLOCK).  Therefore, simply try up to 3 times to get a "clean"
427 	 * snapshot of the kernel symbol table.  This should work fine in the
428 	 * rare case of a kernel module being loaded/unloaded at the same
429 	 * time.
430 	 */
431 	for (try = 0; try < 3; try++) {
432 		ksyms_size_calc(&ts);
433 		elfsz = sizeof(struct ksyms_hdr) + ts.ts_symsz + ts.ts_strsz;
434 
435 		sc->sc_obj = vm_object_allocate(OBJT_DEFAULT,
436 		    OFF_TO_IDX(round_page(elfsz)));
437 		sc->sc_objsz = elfsz;
438 
439 		error = ksyms_snapshot(sc, &ts);
440 		if (error == 0)
441 			break;
442 
443 		vm_object_deallocate(sc->sc_obj);
444 		sc->sc_obj = NULL;
445 	}
446 	return (error);
447 }
448 
449 static int
450 ksyms_read(struct cdev *dev, struct uio *uio, int flags __unused)
451 {
452 	struct ksyms_softc *sc;
453 	int error;
454 
455 	error = devfs_get_cdevpriv((void **)&sc);
456 	if (error != 0)
457 		return (error);
458 	return (uiomove_object(sc->sc_obj, sc->sc_objsz, uio));
459 }
460 
461 static int
462 ksyms_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
463     vm_object_t *objp, int nprot)
464 {
465 	struct ksyms_softc *sc;
466 	vm_object_t obj;
467 	int error;
468 
469 	error = devfs_get_cdevpriv((void **)&sc);
470 	if (error != 0)
471 		return (error);
472 
473 	if (*offset < 0 || *offset >= round_page(sc->sc_objsz) ||
474 	    size > round_page(sc->sc_objsz) - *offset ||
475 	    (nprot & ~PROT_READ) != 0)
476 		return (EINVAL);
477 
478 	obj = sc->sc_obj;
479 	vm_object_reference(obj);
480 	*objp = obj;
481 	return (0);
482 }
483 
484 static int
485 ksyms_modevent(module_t mod __unused, int type, void *data __unused)
486 {
487 	int error;
488 
489 	error = 0;
490 	switch (type) {
491 	case MOD_LOAD:
492 		sx_init(&ksyms_mtx, "KSyms mtx");
493 		ksyms_dev = make_dev(&ksyms_cdevsw, 0, UID_ROOT, GID_WHEEL,
494 		    0400, KSYMS_DNAME);
495 		break;
496 	case MOD_UNLOAD:
497 		if (!LIST_EMPTY(&ksyms_list))
498 			return (EBUSY);
499 		destroy_dev(ksyms_dev);
500 		sx_destroy(&ksyms_mtx);
501 		break;
502 	case MOD_SHUTDOWN:
503 		break;
504 	default:
505 		error = EOPNOTSUPP;
506 		break;
507 	}
508 	return (error);
509 }
510 
511 DEV_MODULE(ksyms, ksyms_modevent, NULL);
512 MODULE_VERSION(ksyms, 1);
513