xref: /freebsd/sys/kern/kern_linker.c (revision df114daef4c48548c3c2b86717612761185ae18f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1997-2000 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_ddb.h"
31 #include "opt_kld.h"
32 #include "opt_hwpmc_hooks.h"
33 #include "opt_hwt_hooks.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/boottrace.h>
38 #include <sys/eventhandler.h>
39 #include <sys/fcntl.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/libkern.h>
43 #include <sys/linker.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/sx.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysctl.h>
55 #include <sys/sysproto.h>
56 #include <sys/vnode.h>
57 
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #endif
61 
62 #include <net/vnet.h>
63 
64 #include <security/mac/mac_framework.h>
65 
66 #include "linker_if.h"
67 
68 #if defined(HWPMC_HOOKS) || defined(HWT_HOOKS)
69 #include <sys/pmckern.h>
70 #endif
71 
72 #ifdef KLD_DEBUG
73 int kld_debug = 0;
74 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN,
75     &kld_debug, 0, "Set various levels of KLD debug");
76 #endif
77 
78 /* These variables are used by kernel debuggers to enumerate loaded files. */
79 const int kld_off_address = offsetof(struct linker_file, address);
80 const int kld_off_filename = offsetof(struct linker_file, filename);
81 const int kld_off_pathname = offsetof(struct linker_file, pathname);
82 const int kld_off_next = offsetof(struct linker_file, link.tqe_next);
83 
84 /*
85  * static char *linker_search_path(const char *name, struct mod_depend
86  * *verinfo);
87  */
88 static const char 	*linker_basename(const char *path);
89 
90 /*
91  * Find a currently loaded file given its filename.
92  */
93 static linker_file_t linker_find_file_by_name(const char* _filename);
94 
95 /*
96  * Find a currently loaded file given its file id.
97  */
98 static linker_file_t linker_find_file_by_id(int _fileid);
99 
100 /* Metadata from the static kernel */
101 SET_DECLARE(modmetadata_set, struct mod_metadata);
102 
103 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker");
104 
105 linker_file_t linker_kernel_file;
106 
107 static struct sx kld_sx;	/* kernel linker lock */
108 static u_int kld_busy;
109 static struct thread *kld_busy_owner;
110 
111 /*
112  * Load counter used by clients to determine if a linker file has been
113  * re-loaded. This counter is incremented for each file load.
114  */
115 static int loadcnt;
116 
117 static linker_class_list_t classes;
118 static linker_file_list_t linker_files;
119 static int next_file_id = 1;
120 static int linker_no_more_classes = 0;
121 
122 #define	LINKER_GET_NEXT_FILE_ID(a) do {					\
123 	linker_file_t lftmp;						\
124 									\
125 	if (!cold)							\
126 		sx_assert(&kld_sx, SA_XLOCKED);				\
127 retry:									\
128 	TAILQ_FOREACH(lftmp, &linker_files, link) {			\
129 		if (next_file_id == lftmp->id) {			\
130 			next_file_id++;					\
131 			goto retry;					\
132 		}							\
133 	}								\
134 	(a) = next_file_id;						\
135 } while (0)
136 
137 /* XXX wrong name; we're looking at version provision tags here, not modules */
138 typedef TAILQ_HEAD(, modlist) modlisthead_t;
139 struct modlist {
140 	TAILQ_ENTRY(modlist) link;	/* chain together all modules */
141 	linker_file_t   container;
142 	const char 	*name;
143 	int             version;
144 };
145 typedef struct modlist *modlist_t;
146 static modlisthead_t found_modules;
147 
148 static void	linker_file_add_dependency(linker_file_t file,
149 		    linker_file_t dep);
150 static caddr_t	linker_file_lookup_symbol_internal(linker_file_t file,
151 		    const char* name, int deps);
152 static int	linker_load_module(const char *kldname,
153 		    const char *modname, struct linker_file *parent,
154 		    const struct mod_depend *verinfo, struct linker_file **lfpp);
155 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo);
156 
157 static void
linker_init(void * arg)158 linker_init(void *arg)
159 {
160 
161 	sx_init(&kld_sx, "kernel linker");
162 	TAILQ_INIT(&classes);
163 	TAILQ_INIT(&linker_files);
164 }
165 
166 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL);
167 
168 static void
linker_stop_class_add(void * arg)169 linker_stop_class_add(void *arg)
170 {
171 
172 	linker_no_more_classes = 1;
173 }
174 
175 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL);
176 
177 int
linker_add_class(linker_class_t lc)178 linker_add_class(linker_class_t lc)
179 {
180 
181 	/*
182 	 * We disallow any class registration past SI_ORDER_ANY
183 	 * of SI_SUB_KLD.  We bump the reference count to keep the
184 	 * ops from being freed.
185 	 */
186 	if (linker_no_more_classes == 1)
187 		return (EPERM);
188 	kobj_class_compile((kobj_class_t) lc);
189 	((kobj_class_t)lc)->refs++;	/* XXX: kobj_mtx */
190 	TAILQ_INSERT_TAIL(&classes, lc, link);
191 	return (0);
192 }
193 
194 static void
linker_file_sysinit(linker_file_t lf)195 linker_file_sysinit(linker_file_t lf)
196 {
197 	struct sysinit **start, **stop, **sipp, **xipp, *save;
198 	int last;
199 
200 	KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n",
201 	    lf->filename));
202 
203 	sx_assert(&kld_sx, SA_XLOCKED);
204 
205 	if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0)
206 		return;
207 	/*
208 	 * Perform a bubble sort of the system initialization objects by
209 	 * their subsystem (primary key) and order (secondary key).
210 	 *
211 	 * Since some things care about execution order, this is the operation
212 	 * which ensures continued function.
213 	 */
214 	for (sipp = start; sipp < stop; sipp++) {
215 		for (xipp = sipp + 1; xipp < stop; xipp++) {
216 			if ((*sipp)->subsystem < (*xipp)->subsystem ||
217 			    ((*sipp)->subsystem == (*xipp)->subsystem &&
218 			    (*sipp)->order <= (*xipp)->order))
219 				continue;	/* skip */
220 			save = *sipp;
221 			*sipp = *xipp;
222 			*xipp = save;
223 		}
224 	}
225 
226 	/*
227 	 * Traverse the (now) ordered list of system initialization tasks.
228 	 * Perform each task, and continue on to the next task.
229 	 */
230 	last = SI_SUB_DUMMY;
231 	sx_xunlock(&kld_sx);
232 	mtx_lock(&Giant);
233 	for (sipp = start; sipp < stop; sipp++) {
234 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
235 			continue;	/* skip dummy task(s) */
236 
237 		if ((*sipp)->subsystem > last)
238 			BOOTTRACE("%s: sysinit 0x%7x", lf->filename,
239 			    (*sipp)->subsystem);
240 
241 		/* Call function */
242 		(*((*sipp)->func)) ((*sipp)->udata);
243 		last = (*sipp)->subsystem;
244 	}
245 	mtx_unlock(&Giant);
246 	sx_xlock(&kld_sx);
247 }
248 
249 static void
linker_file_sysuninit(linker_file_t lf)250 linker_file_sysuninit(linker_file_t lf)
251 {
252 	struct sysinit **start, **stop, **sipp, **xipp, *save;
253 	int last;
254 
255 	KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n",
256 	    lf->filename));
257 
258 	sx_assert(&kld_sx, SA_XLOCKED);
259 
260 	if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop,
261 	    NULL) != 0)
262 		return;
263 
264 	/*
265 	 * Perform a reverse bubble sort of the system initialization objects
266 	 * by their subsystem (primary key) and order (secondary key).
267 	 *
268 	 * Since some things care about execution order, this is the operation
269 	 * which ensures continued function.
270 	 */
271 	for (sipp = start; sipp < stop; sipp++) {
272 		for (xipp = sipp + 1; xipp < stop; xipp++) {
273 			if ((*sipp)->subsystem > (*xipp)->subsystem ||
274 			    ((*sipp)->subsystem == (*xipp)->subsystem &&
275 			    (*sipp)->order >= (*xipp)->order))
276 				continue;	/* skip */
277 			save = *sipp;
278 			*sipp = *xipp;
279 			*xipp = save;
280 		}
281 	}
282 
283 	/*
284 	 * Traverse the (now) ordered list of system initialization tasks.
285 	 * Perform each task, and continue on to the next task.
286 	 */
287 	sx_xunlock(&kld_sx);
288 	mtx_lock(&Giant);
289 	last = SI_SUB_DUMMY;
290 	for (sipp = start; sipp < stop; sipp++) {
291 		if ((*sipp)->subsystem == SI_SUB_DUMMY)
292 			continue;	/* skip dummy task(s) */
293 
294 		if ((*sipp)->subsystem > last)
295 			BOOTTRACE("%s: sysuninit 0x%7x", lf->filename,
296 			    (*sipp)->subsystem);
297 
298 		/* Call function */
299 		(*((*sipp)->func)) ((*sipp)->udata);
300 		last = (*sipp)->subsystem;
301 	}
302 	mtx_unlock(&Giant);
303 	sx_xlock(&kld_sx);
304 }
305 
306 static void
linker_file_register_sysctls(linker_file_t lf,bool enable)307 linker_file_register_sysctls(linker_file_t lf, bool enable)
308 {
309 	struct sysctl_oid **start, **stop, **oidp;
310 
311 	KLD_DPF(FILE,
312 	    ("linker_file_register_sysctls: registering SYSCTLs for %s\n",
313 	    lf->filename));
314 
315 	sx_assert(&kld_sx, SA_XLOCKED);
316 
317 	if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
318 		return;
319 
320 	sx_xunlock(&kld_sx);
321 	sysctl_wlock();
322 	for (oidp = start; oidp < stop; oidp++) {
323 		if (enable)
324 			sysctl_register_oid(*oidp);
325 		else
326 			sysctl_register_disabled_oid(*oidp);
327 	}
328 	sysctl_wunlock();
329 	sx_xlock(&kld_sx);
330 }
331 
332 /*
333  * Invoke the LINKER_CTF_GET implementation for this file.  Existing
334  * implementations will load CTF info from the filesystem upon the first call
335  * and cache it in the kernel thereafter.
336  */
337 static void
linker_ctf_load_file(linker_file_t file)338 linker_ctf_load_file(linker_file_t file)
339 {
340 	linker_ctf_t lc;
341 	int error;
342 
343 	error = linker_ctf_get(file, &lc);
344 	if (error == 0)
345 		return;
346 	if (bootverbose) {
347 		printf("failed to load CTF for %s: %d\n", file->filename,
348 		    error);
349 	}
350 }
351 
352 static void
linker_file_enable_sysctls(linker_file_t lf)353 linker_file_enable_sysctls(linker_file_t lf)
354 {
355 	struct sysctl_oid **start, **stop, **oidp;
356 
357 	KLD_DPF(FILE,
358 	    ("linker_file_enable_sysctls: enable SYSCTLs for %s\n",
359 	    lf->filename));
360 
361 	sx_assert(&kld_sx, SA_XLOCKED);
362 
363 	if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
364 		return;
365 
366 	sx_xunlock(&kld_sx);
367 	sysctl_wlock();
368 	for (oidp = start; oidp < stop; oidp++)
369 		sysctl_enable_oid(*oidp);
370 	sysctl_wunlock();
371 	sx_xlock(&kld_sx);
372 }
373 
374 static void
linker_file_unregister_sysctls(linker_file_t lf)375 linker_file_unregister_sysctls(linker_file_t lf)
376 {
377 	struct sysctl_oid **start, **stop, **oidp;
378 
379 	KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs"
380 	    " for %s\n", lf->filename));
381 
382 	sx_assert(&kld_sx, SA_XLOCKED);
383 
384 	if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0)
385 		return;
386 
387 	sx_xunlock(&kld_sx);
388 	sysctl_wlock();
389 	for (oidp = start; oidp < stop; oidp++)
390 		sysctl_unregister_oid(*oidp);
391 	sysctl_wunlock();
392 	sx_xlock(&kld_sx);
393 }
394 
395 static int
linker_file_register_modules(linker_file_t lf)396 linker_file_register_modules(linker_file_t lf)
397 {
398 	struct mod_metadata **start, **stop, **mdp;
399 	const moduledata_t *moddata;
400 	int first_error, error;
401 
402 	KLD_DPF(FILE, ("linker_file_register_modules: registering modules"
403 	    " in %s\n", lf->filename));
404 
405 	sx_assert(&kld_sx, SA_XLOCKED);
406 
407 	if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL) != 0) {
408 		/*
409 		 * This fallback should be unnecessary, but if we get booted
410 		 * from boot2 instead of loader and we are missing our
411 		 * metadata then we have to try the best we can.
412 		 */
413 		if (lf == linker_kernel_file) {
414 			start = SET_BEGIN(modmetadata_set);
415 			stop = SET_LIMIT(modmetadata_set);
416 		} else
417 			return (0);
418 	}
419 	first_error = 0;
420 	for (mdp = start; mdp < stop; mdp++) {
421 		if ((*mdp)->md_type != MDT_MODULE)
422 			continue;
423 		moddata = (*mdp)->md_data;
424 		KLD_DPF(FILE, ("Registering module %s in %s\n",
425 		    moddata->name, lf->filename));
426 		error = module_register(moddata, lf);
427 		if (error) {
428 			printf("Module %s failed to register: %d\n",
429 			    moddata->name, error);
430 			if (first_error == 0)
431 				first_error = error;
432 		}
433 	}
434 	return (first_error);
435 }
436 
437 static void
linker_init_kernel_modules(void)438 linker_init_kernel_modules(void)
439 {
440 
441 	sx_xlock(&kld_sx);
442 	linker_file_register_modules(linker_kernel_file);
443 	sx_xunlock(&kld_sx);
444 }
445 
446 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules,
447     NULL);
448 
449 static int
linker_load_file(const char * filename,linker_file_t * result)450 linker_load_file(const char *filename, linker_file_t *result)
451 {
452 	linker_class_t lc;
453 	linker_file_t lf;
454 	int foundfile, error, modules;
455 
456 	/* Refuse to load modules if securelevel raised */
457 	if (prison0.pr_securelevel > 0)
458 		return (EPERM);
459 
460 	sx_assert(&kld_sx, SA_XLOCKED);
461 	lf = linker_find_file_by_name(filename);
462 	if (lf) {
463 		KLD_DPF(FILE, ("linker_load_file: file %s is already loaded,"
464 		    " incrementing refs\n", filename));
465 		*result = lf;
466 		lf->refs++;
467 		return (0);
468 	}
469 	foundfile = 0;
470 	error = 0;
471 
472 	/*
473 	 * We do not need to protect (lock) classes here because there is
474 	 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY)
475 	 * and there is no class deregistration mechanism at this time.
476 	 */
477 	TAILQ_FOREACH(lc, &classes, link) {
478 		KLD_DPF(FILE, ("linker_load_file: trying to load %s\n",
479 		    filename));
480 		error = LINKER_LOAD_FILE(lc, filename, &lf);
481 		/*
482 		 * If we got something other than ENOENT, then it exists but
483 		 * we cannot load it for some other reason.
484 		 */
485 		if (error != ENOENT) {
486 			foundfile = 1;
487 			if (error == EEXIST)
488 				break;
489 		}
490 		if (lf) {
491 			error = linker_file_register_modules(lf);
492 			if (error == EEXIST) {
493 				linker_file_unload(lf, LINKER_UNLOAD_FORCE);
494 				return (error);
495 			}
496 			modules = !TAILQ_EMPTY(&lf->modules);
497 			linker_file_register_sysctls(lf, false);
498 #ifdef VIMAGE
499 			LINKER_PROPAGATE_VNETS(lf);
500 #endif
501 			linker_file_sysinit(lf);
502 			lf->flags |= LINKER_FILE_LINKED;
503 
504 			/*
505 			 * If all of the modules in this file failed
506 			 * to load, unload the file and return an
507 			 * error of ENOEXEC.
508 			 */
509 			if (modules && TAILQ_EMPTY(&lf->modules)) {
510 				linker_file_unload(lf, LINKER_UNLOAD_FORCE);
511 				return (ENOEXEC);
512 			}
513 			linker_file_enable_sysctls(lf);
514 
515 			/*
516 			 * Ask the linker to load CTF data for this file.
517 			 */
518 			linker_ctf_load_file(lf);
519 			EVENTHANDLER_INVOKE(kld_load, lf);
520 			*result = lf;
521 			return (0);
522 		}
523 	}
524 	/*
525 	 * Less than ideal, but tells the user whether it failed to load or
526 	 * the module was not found.
527 	 */
528 	if (foundfile) {
529 		/*
530 		 * If the file type has not been recognized by the last try
531 		 * printout a message before to fail.
532 		 */
533 		if (error == ENOSYS)
534 			printf("%s: %s - unsupported file type\n",
535 			    __func__, filename);
536 
537 		/*
538 		 * Format not recognized or otherwise unloadable.
539 		 * When loading a module that is statically built into
540 		 * the kernel EEXIST percolates back up as the return
541 		 * value.  Preserve this so that apps like sysinstall
542 		 * can recognize this special case and not post bogus
543 		 * dialog boxes.
544 		 */
545 		if (error != EEXIST)
546 			error = ENOEXEC;
547 	} else
548 		error = ENOENT;		/* Nothing found */
549 	return (error);
550 }
551 
552 int
linker_reference_module(const char * modname,struct mod_depend * verinfo,linker_file_t * result)553 linker_reference_module(const char *modname, struct mod_depend *verinfo,
554     linker_file_t *result)
555 {
556 	modlist_t mod;
557 	int error;
558 
559 	sx_xlock(&kld_sx);
560 	if ((mod = modlist_lookup2(modname, verinfo)) != NULL) {
561 		*result = mod->container;
562 		(*result)->refs++;
563 		sx_xunlock(&kld_sx);
564 		return (0);
565 	}
566 
567 	error = linker_load_module(NULL, modname, NULL, verinfo, result);
568 	sx_xunlock(&kld_sx);
569 	return (error);
570 }
571 
572 int
linker_release_module(const char * modname,struct mod_depend * verinfo,linker_file_t lf)573 linker_release_module(const char *modname, struct mod_depend *verinfo,
574     linker_file_t lf)
575 {
576 	modlist_t mod;
577 	int error;
578 
579 	sx_xlock(&kld_sx);
580 	if (lf == NULL) {
581 		KASSERT(modname != NULL,
582 		    ("linker_release_module: no file or name"));
583 		mod = modlist_lookup2(modname, verinfo);
584 		if (mod == NULL) {
585 			sx_xunlock(&kld_sx);
586 			return (ESRCH);
587 		}
588 		lf = mod->container;
589 	} else
590 		KASSERT(modname == NULL && verinfo == NULL,
591 		    ("linker_release_module: both file and name"));
592 	error =	linker_file_unload(lf, LINKER_UNLOAD_NORMAL);
593 	sx_xunlock(&kld_sx);
594 	return (error);
595 }
596 
597 static linker_file_t
linker_find_file_by_name(const char * filename)598 linker_find_file_by_name(const char *filename)
599 {
600 	linker_file_t lf;
601 	char *koname;
602 
603 	koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK);
604 	sprintf(koname, "%s.ko", filename);
605 
606 	sx_assert(&kld_sx, SA_XLOCKED);
607 	TAILQ_FOREACH(lf, &linker_files, link) {
608 		if (strcmp(lf->filename, koname) == 0)
609 			break;
610 		if (strcmp(lf->filename, filename) == 0)
611 			break;
612 	}
613 	free(koname, M_LINKER);
614 	return (lf);
615 }
616 
617 static linker_file_t
linker_find_file_by_id(int fileid)618 linker_find_file_by_id(int fileid)
619 {
620 	linker_file_t lf;
621 
622 	sx_assert(&kld_sx, SA_XLOCKED);
623 	TAILQ_FOREACH(lf, &linker_files, link)
624 		if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED)
625 			break;
626 	return (lf);
627 }
628 
629 int
linker_file_foreach(linker_predicate_t * predicate,void * context)630 linker_file_foreach(linker_predicate_t *predicate, void *context)
631 {
632 	linker_file_t lf;
633 	int retval = 0;
634 
635 	sx_xlock(&kld_sx);
636 	TAILQ_FOREACH(lf, &linker_files, link) {
637 		retval = predicate(lf, context);
638 		if (retval != 0)
639 			break;
640 	}
641 	sx_xunlock(&kld_sx);
642 	return (retval);
643 }
644 
645 linker_file_t
linker_make_file(const char * pathname,linker_class_t lc)646 linker_make_file(const char *pathname, linker_class_t lc)
647 {
648 	linker_file_t lf;
649 	const char *filename;
650 
651 	if (!cold)
652 		sx_assert(&kld_sx, SA_XLOCKED);
653 	filename = linker_basename(pathname);
654 
655 	KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname));
656 	lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK);
657 	if (lf == NULL)
658 		return (NULL);
659 	lf->ctors_addr = 0;
660 	lf->ctors_size = 0;
661 	lf->ctors_invoked = LF_NONE;
662 	lf->dtors_addr = 0;
663 	lf->dtors_size = 0;
664 	lf->refs = 1;
665 	lf->userrefs = 0;
666 	lf->flags = 0;
667 	lf->filename = strdup(filename, M_LINKER);
668 	lf->pathname = strdup(pathname, M_LINKER);
669 	LINKER_GET_NEXT_FILE_ID(lf->id);
670 	lf->ndeps = 0;
671 	lf->deps = NULL;
672 	lf->loadcnt = ++loadcnt;
673 #ifdef __arm__
674 	lf->exidx_addr = 0;
675 	lf->exidx_size = 0;
676 #endif
677 	STAILQ_INIT(&lf->common);
678 	TAILQ_INIT(&lf->modules);
679 	TAILQ_INSERT_TAIL(&linker_files, lf, link);
680 	return (lf);
681 }
682 
683 int
linker_file_unload(linker_file_t file,int flags)684 linker_file_unload(linker_file_t file, int flags)
685 {
686 	module_t mod, next;
687 	modlist_t ml, nextml;
688 	struct common_symbol *cp;
689 	int error, i;
690 
691 	/* Refuse to unload modules if securelevel raised. */
692 	if (prison0.pr_securelevel > 0)
693 		return (EPERM);
694 
695 	sx_assert(&kld_sx, SA_XLOCKED);
696 	KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs));
697 
698 	/* Easy case of just dropping a reference. */
699 	if (file->refs > 1) {
700 		file->refs--;
701 		return (0);
702 	}
703 
704 	/* Give eventhandlers a chance to prevent the unload. */
705 	error = 0;
706 	EVENTHANDLER_INVOKE(kld_unload_try, file, &error);
707 	if (error != 0)
708 		return (EBUSY);
709 
710 	KLD_DPF(FILE, ("linker_file_unload: file is unloading,"
711 	    " informing modules\n"));
712 
713 	/*
714 	 * Quiesce all the modules to give them a chance to veto the unload.
715 	 */
716 	MOD_SLOCK;
717 	for (mod = TAILQ_FIRST(&file->modules); mod;
718 	     mod = module_getfnext(mod)) {
719 		error = module_quiesce(mod);
720 		if (error != 0 && flags != LINKER_UNLOAD_FORCE) {
721 			KLD_DPF(FILE, ("linker_file_unload: module %s"
722 			    " vetoed unload\n", module_getname(mod)));
723 			/*
724 			 * XXX: Do we need to tell all the quiesced modules
725 			 * that they can resume work now via a new module
726 			 * event?
727 			 */
728 			MOD_SUNLOCK;
729 			return (error);
730 		}
731 	}
732 	MOD_SUNLOCK;
733 
734 	/*
735 	 * Inform any modules associated with this file that they are
736 	 * being unloaded.
737 	 */
738 	MOD_XLOCK;
739 	for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) {
740 		next = module_getfnext(mod);
741 		MOD_XUNLOCK;
742 
743 		/*
744 		 * Give the module a chance to veto the unload.
745 		 */
746 		if ((error = module_unload(mod)) != 0) {
747 #ifdef KLD_DEBUG
748 			MOD_SLOCK;
749 			KLD_DPF(FILE, ("linker_file_unload: module %s"
750 			    " failed unload\n", module_getname(mod)));
751 			MOD_SUNLOCK;
752 #endif
753 			return (error);
754 		}
755 		MOD_XLOCK;
756 		module_release(mod);
757 	}
758 	MOD_XUNLOCK;
759 
760 	TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) {
761 		if (ml->container == file) {
762 			TAILQ_REMOVE(&found_modules, ml, link);
763 			free(ml, M_LINKER);
764 		}
765 	}
766 
767 	/*
768 	 * Don't try to run SYSUNINITs if we are unloaded due to a
769 	 * link error.
770 	 */
771 	if (file->flags & LINKER_FILE_LINKED) {
772 		file->flags &= ~LINKER_FILE_LINKED;
773 		linker_file_unregister_sysctls(file);
774 		linker_file_sysuninit(file);
775 	}
776 	TAILQ_REMOVE(&linker_files, file, link);
777 
778 	if (file->deps) {
779 		for (i = 0; i < file->ndeps; i++)
780 			linker_file_unload(file->deps[i], flags);
781 		free(file->deps, M_LINKER);
782 		file->deps = NULL;
783 	}
784 	while ((cp = STAILQ_FIRST(&file->common)) != NULL) {
785 		STAILQ_REMOVE_HEAD(&file->common, link);
786 		free(cp, M_LINKER);
787 	}
788 
789 	LINKER_UNLOAD(file);
790 
791 	EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address,
792 	    file->size);
793 
794 	if (file->filename) {
795 		free(file->filename, M_LINKER);
796 		file->filename = NULL;
797 	}
798 	if (file->pathname) {
799 		free(file->pathname, M_LINKER);
800 		file->pathname = NULL;
801 	}
802 	kobj_delete((kobj_t) file, M_LINKER);
803 	return (0);
804 }
805 
806 int
linker_ctf_get(linker_file_t file,linker_ctf_t * lc)807 linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
808 {
809 	return (LINKER_CTF_GET(file, lc));
810 }
811 
812 int
linker_ctf_lookup_typename_ddb(linker_ctf_t * lc,const char * typename)813 linker_ctf_lookup_typename_ddb(linker_ctf_t *lc, const char *typename)
814 {
815 #ifdef DDB
816 	linker_file_t lf;
817 
818 	TAILQ_FOREACH (lf, &linker_files, link){
819 		if (LINKER_CTF_LOOKUP_TYPENAME(lf, lc, typename) == 0)
820 			return (0);
821 	}
822 #endif
823 	return (ENOENT);
824 }
825 
826 int
linker_ctf_lookup_sym_ddb(const char * symname,c_linker_sym_t * sym,linker_ctf_t * lc)827 linker_ctf_lookup_sym_ddb(const char *symname, c_linker_sym_t *sym,
828     linker_ctf_t *lc)
829 {
830 #ifdef DDB
831 	linker_file_t lf;
832 
833 	TAILQ_FOREACH (lf, &linker_files, link){
834 		if (LINKER_LOOKUP_DEBUG_SYMBOL_CTF(lf, symname, sym, lc) == 0)
835 			return (0);
836 	}
837 #endif
838 	return (ENOENT);
839 }
840 
841 static void
linker_file_add_dependency(linker_file_t file,linker_file_t dep)842 linker_file_add_dependency(linker_file_t file, linker_file_t dep)
843 {
844 	linker_file_t *newdeps;
845 
846 	sx_assert(&kld_sx, SA_XLOCKED);
847 	file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps),
848 	    M_LINKER, M_WAITOK | M_ZERO);
849 	file->deps[file->ndeps] = dep;
850 	file->ndeps++;
851 	KLD_DPF(FILE, ("linker_file_add_dependency:"
852 	    " adding %s as dependency for %s\n",
853 	    dep->filename, file->filename));
854 }
855 
856 /*
857  * Locate a linker set and its contents.  This is a helper function to avoid
858  * linker_if.h exposure elsewhere.  Note: firstp and lastp are really void **.
859  * This function is used in this file so we can avoid having lots of (void **)
860  * casts.
861  */
862 int
linker_file_lookup_set(linker_file_t file,const char * name,void * firstp,void * lastp,int * countp)863 linker_file_lookup_set(linker_file_t file, const char *name,
864     void *firstp, void *lastp, int *countp)
865 {
866 
867 	sx_assert(&kld_sx, SA_LOCKED);
868 	return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp));
869 }
870 
871 /*
872  * List all functions in a file.
873  */
874 int
linker_file_function_listall(linker_file_t lf,linker_function_nameval_callback_t callback_func,void * arg)875 linker_file_function_listall(linker_file_t lf,
876     linker_function_nameval_callback_t callback_func, void *arg)
877 {
878 	return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg));
879 }
880 
881 caddr_t
linker_file_lookup_symbol(linker_file_t file,const char * name,int deps)882 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps)
883 {
884 	caddr_t sym;
885 	int locked;
886 
887 	locked = sx_xlocked(&kld_sx);
888 	if (!locked)
889 		sx_xlock(&kld_sx);
890 	sym = linker_file_lookup_symbol_internal(file, name, deps);
891 	if (!locked)
892 		sx_xunlock(&kld_sx);
893 	return (sym);
894 }
895 
896 static caddr_t
linker_file_lookup_symbol_internal(linker_file_t file,const char * name,int deps)897 linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
898     int deps)
899 {
900 	c_linker_sym_t sym;
901 	linker_symval_t symval;
902 	caddr_t address;
903 	size_t common_size = 0;
904 	int i;
905 
906 	sx_assert(&kld_sx, SA_XLOCKED);
907 	KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
908 	    file, name, deps));
909 
910 	/*
911 	 * Treat the __this_linker_file as a special symbol. This is a
912 	 * global that linuxkpi uses to populate the THIS_MODULE
913 	 * value.  In this case we can simply return the linker_file_t.
914 	 *
915 	 * Modules compiled statically into the kernel are assigned NULL.
916 	 */
917 	if (strcmp(name, "__this_linker_file") == 0) {
918 		address = (file == linker_kernel_file) ? NULL : (caddr_t)file;
919 		KLD_DPF(SYM, ("linker_file_lookup_symbol: resolving special "
920 		    "symbol __this_linker_file to %p\n", address));
921 		return (address);
922 	}
923 
924 	if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
925 		LINKER_SYMBOL_VALUES(file, sym, &symval);
926 		if (symval.value == 0)
927 			/*
928 			 * For commons, first look them up in the
929 			 * dependencies and only allocate space if not found
930 			 * there.
931 			 */
932 			common_size = symval.size;
933 		else {
934 			KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol"
935 			    ".value=%p\n", symval.value));
936 			return (symval.value);
937 		}
938 	}
939 	if (deps) {
940 		for (i = 0; i < file->ndeps; i++) {
941 			address = linker_file_lookup_symbol_internal(
942 			    file->deps[i], name, 0);
943 			if (address) {
944 				KLD_DPF(SYM, ("linker_file_lookup_symbol:"
945 				    " deps value=%p\n", address));
946 				return (address);
947 			}
948 		}
949 	}
950 	if (common_size > 0) {
951 		/*
952 		 * This is a common symbol which was not found in the
953 		 * dependencies.  We maintain a simple common symbol table in
954 		 * the file object.
955 		 */
956 		struct common_symbol *cp;
957 
958 		STAILQ_FOREACH(cp, &file->common, link) {
959 			if (strcmp(cp->name, name) == 0) {
960 				KLD_DPF(SYM, ("linker_file_lookup_symbol:"
961 				    " old common value=%p\n", cp->address));
962 				return (cp->address);
963 			}
964 		}
965 		/*
966 		 * Round the symbol size up to align.
967 		 */
968 		common_size = (common_size + sizeof(int) - 1) & -sizeof(int);
969 		cp = malloc(sizeof(struct common_symbol)
970 		    + common_size + strlen(name) + 1, M_LINKER,
971 		    M_WAITOK | M_ZERO);
972 		cp->address = (caddr_t)(cp + 1);
973 		cp->name = cp->address + common_size;
974 		strcpy(cp->name, name);
975 		bzero(cp->address, common_size);
976 		STAILQ_INSERT_TAIL(&file->common, cp, link);
977 
978 		KLD_DPF(SYM, ("linker_file_lookup_symbol: new common"
979 		    " value=%p\n", cp->address));
980 		return (cp->address);
981 	}
982 	KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n"));
983 	return (0);
984 }
985 
986 /*
987  * Both DDB and stack(9) rely on the kernel linker to provide forward and
988  * backward lookup of symbols.  However, DDB and sometimes stack(9) need to
989  * do this in a lockfree manner.  We provide a set of internal helper
990  * routines to perform these operations without locks, and then wrappers that
991  * optionally lock.
992  *
993  * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB.
994  */
995 #ifdef DDB
996 static int
linker_debug_lookup(const char * symstr,c_linker_sym_t * sym)997 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym)
998 {
999 	linker_file_t lf;
1000 
1001 	TAILQ_FOREACH(lf, &linker_files, link) {
1002 		if (LINKER_LOOKUP_DEBUG_SYMBOL(lf, symstr, sym) == 0)
1003 			return (0);
1004 	}
1005 	return (ENOENT);
1006 }
1007 #endif
1008 
1009 static int
linker_debug_search_symbol(caddr_t value,c_linker_sym_t * sym,long * diffp)1010 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1011 {
1012 	linker_file_t lf;
1013 	c_linker_sym_t best, es;
1014 	u_long diff, bestdiff, off;
1015 
1016 	best = 0;
1017 	off = (uintptr_t)value;
1018 	bestdiff = off;
1019 	TAILQ_FOREACH(lf, &linker_files, link) {
1020 		if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0)
1021 			continue;
1022 		if (es != 0 && diff < bestdiff) {
1023 			best = es;
1024 			bestdiff = diff;
1025 		}
1026 		if (bestdiff == 0)
1027 			break;
1028 	}
1029 	if (best) {
1030 		*sym = best;
1031 		*diffp = bestdiff;
1032 		return (0);
1033 	} else {
1034 		*sym = 0;
1035 		*diffp = off;
1036 		return (ENOENT);
1037 	}
1038 }
1039 
1040 static int
linker_debug_symbol_values(c_linker_sym_t sym,linker_symval_t * symval)1041 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1042 {
1043 	linker_file_t lf;
1044 
1045 	TAILQ_FOREACH(lf, &linker_files, link) {
1046 		if (LINKER_DEBUG_SYMBOL_VALUES(lf, sym, symval) == 0)
1047 			return (0);
1048 	}
1049 	return (ENOENT);
1050 }
1051 
1052 static int
linker_debug_search_symbol_name(caddr_t value,char * buf,u_int buflen,long * offset)1053 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1054     long *offset)
1055 {
1056 	linker_symval_t symval;
1057 	c_linker_sym_t sym;
1058 	int error;
1059 
1060 	*offset = 0;
1061 	error = linker_debug_search_symbol(value, &sym, offset);
1062 	if (error)
1063 		return (error);
1064 	error = linker_debug_symbol_values(sym, &symval);
1065 	if (error)
1066 		return (error);
1067 	strlcpy(buf, symval.name, buflen);
1068 	return (0);
1069 }
1070 
1071 /*
1072  * DDB Helpers.  DDB has to look across multiple files with their own symbol
1073  * tables and string tables.
1074  *
1075  * Note that we do not obey list locking protocols here.  We really don't need
1076  * DDB to hang because somebody's got the lock held.  We'll take the chance
1077  * that the files list is inconsistent instead.
1078  */
1079 #ifdef DDB
1080 int
linker_ddb_lookup(const char * symstr,c_linker_sym_t * sym)1081 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym)
1082 {
1083 
1084 	return (linker_debug_lookup(symstr, sym));
1085 }
1086 #endif
1087 
1088 int
linker_ddb_search_symbol(caddr_t value,c_linker_sym_t * sym,long * diffp)1089 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp)
1090 {
1091 
1092 	return (linker_debug_search_symbol(value, sym, diffp));
1093 }
1094 
1095 int
linker_ddb_symbol_values(c_linker_sym_t sym,linker_symval_t * symval)1096 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
1097 {
1098 
1099 	return (linker_debug_symbol_values(sym, symval));
1100 }
1101 
1102 int
linker_ddb_search_symbol_name(caddr_t value,char * buf,u_int buflen,long * offset)1103 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1104     long *offset)
1105 {
1106 
1107 	return (linker_debug_search_symbol_name(value, buf, buflen, offset));
1108 }
1109 
1110 /*
1111  * stack(9) helper for non-debugging environemnts.  Unlike DDB helpers, we do
1112  * obey locking protocols, and offer a significantly less complex interface.
1113  */
1114 int
linker_search_symbol_name_flags(caddr_t value,char * buf,u_int buflen,long * offset,int flags)1115 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
1116     long *offset, int flags)
1117 {
1118 	int error;
1119 
1120 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
1121 	    (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
1122 	    ("%s: bad flags: 0x%x", __func__, flags));
1123 
1124 	if (flags & M_NOWAIT) {
1125 		if (!sx_try_slock(&kld_sx))
1126 			return (EWOULDBLOCK);
1127 	} else
1128 		sx_slock(&kld_sx);
1129 
1130 	error = linker_debug_search_symbol_name(value, buf, buflen, offset);
1131 	sx_sunlock(&kld_sx);
1132 	return (error);
1133 }
1134 
1135 int
linker_search_symbol_name(caddr_t value,char * buf,u_int buflen,long * offset)1136 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
1137     long *offset)
1138 {
1139 
1140 	return (linker_search_symbol_name_flags(value, buf, buflen, offset,
1141 	    M_WAITOK));
1142 }
1143 
1144 int
linker_kldload_busy(int flags)1145 linker_kldload_busy(int flags)
1146 {
1147 	int error;
1148 
1149 	MPASS((flags & ~(LINKER_UB_UNLOCK | LINKER_UB_LOCKED |
1150 	    LINKER_UB_PCATCH)) == 0);
1151 	if ((flags & LINKER_UB_LOCKED) != 0)
1152 		sx_assert(&kld_sx, SA_XLOCKED);
1153 
1154 	if ((flags & LINKER_UB_LOCKED) == 0)
1155 		sx_xlock(&kld_sx);
1156 	while (kld_busy > 0) {
1157 		if (kld_busy_owner == curthread)
1158 			break;
1159 		error = sx_sleep(&kld_busy, &kld_sx,
1160 		    (flags & LINKER_UB_PCATCH) != 0 ? PCATCH : 0,
1161 		    "kldbusy", 0);
1162 		if (error != 0) {
1163 			if ((flags & LINKER_UB_UNLOCK) != 0)
1164 				sx_xunlock(&kld_sx);
1165 			return (error);
1166 		}
1167 	}
1168 	kld_busy++;
1169 	kld_busy_owner = curthread;
1170 	if ((flags & LINKER_UB_UNLOCK) != 0)
1171 		sx_xunlock(&kld_sx);
1172 	return (0);
1173 }
1174 
1175 void
linker_kldload_unbusy(int flags)1176 linker_kldload_unbusy(int flags)
1177 {
1178 	MPASS((flags & ~LINKER_UB_LOCKED) == 0);
1179 	if ((flags & LINKER_UB_LOCKED) != 0)
1180 		sx_assert(&kld_sx, SA_XLOCKED);
1181 
1182 	if ((flags & LINKER_UB_LOCKED) == 0)
1183 		sx_xlock(&kld_sx);
1184 	MPASS(kld_busy > 0);
1185 	if (kld_busy_owner != curthread)
1186 		panic("linker_kldload_unbusy done by not owning thread %p",
1187 		    kld_busy_owner);
1188 	kld_busy--;
1189 	if (kld_busy == 0) {
1190 		kld_busy_owner = NULL;
1191 		wakeup(&kld_busy);
1192 	}
1193 	sx_xunlock(&kld_sx);
1194 }
1195 
1196 /*
1197  * Syscalls.
1198  */
1199 int
kern_kldload(struct thread * td,const char * file,int * fileid)1200 kern_kldload(struct thread *td, const char *file, int *fileid)
1201 {
1202 	const char *kldname, *modname;
1203 	linker_file_t lf;
1204 	int error;
1205 
1206 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1207 		return (error);
1208 
1209 	if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0)
1210 		return (error);
1211 
1212 	/*
1213 	 * If file does not contain a qualified name or any dot in it
1214 	 * (kldname.ko, or kldname.ver.ko) treat it as an interface
1215 	 * name.
1216 	 */
1217 	if (strchr(file, '/') || strchr(file, '.')) {
1218 		kldname = file;
1219 		modname = NULL;
1220 	} else {
1221 		kldname = NULL;
1222 		modname = file;
1223 	}
1224 
1225 	error = linker_kldload_busy(LINKER_UB_PCATCH);
1226 	if (error != 0) {
1227 		sx_xunlock(&kld_sx);
1228 		return (error);
1229 	}
1230 
1231 	/*
1232 	 * It is possible that kldloaded module will attach a new ifnet,
1233 	 * so vnet context must be set when this ocurs.
1234 	 */
1235 	CURVNET_SET(TD_TO_VNET(td));
1236 
1237 	error = linker_load_module(kldname, modname, NULL, NULL, &lf);
1238 	CURVNET_RESTORE();
1239 
1240 	if (error == 0) {
1241 		lf->userrefs++;
1242 		if (fileid != NULL)
1243 			*fileid = lf->id;
1244 	}
1245 	linker_kldload_unbusy(LINKER_UB_LOCKED);
1246 	return (error);
1247 }
1248 
1249 int
sys_kldload(struct thread * td,struct kldload_args * uap)1250 sys_kldload(struct thread *td, struct kldload_args *uap)
1251 {
1252 	char *pathname = NULL;
1253 	int error, fileid;
1254 
1255 	td->td_retval[0] = -1;
1256 
1257 	pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1258 	error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL);
1259 	if (error == 0) {
1260 		error = kern_kldload(td, pathname, &fileid);
1261 		if (error == 0)
1262 			td->td_retval[0] = fileid;
1263 	}
1264 	free(pathname, M_TEMP);
1265 	return (error);
1266 }
1267 
1268 int
kern_kldunload(struct thread * td,int fileid,int flags)1269 kern_kldunload(struct thread *td, int fileid, int flags)
1270 {
1271 	linker_file_t lf;
1272 	int error = 0;
1273 
1274 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
1275 		return (error);
1276 
1277 	if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0)
1278 		return (error);
1279 
1280 	error = linker_kldload_busy(LINKER_UB_PCATCH);
1281 	if (error != 0) {
1282 		sx_xunlock(&kld_sx);
1283 		return (error);
1284 	}
1285 
1286 	CURVNET_SET(TD_TO_VNET(td));
1287 	lf = linker_find_file_by_id(fileid);
1288 	if (lf) {
1289 		KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
1290 
1291 		if (lf->userrefs == 0) {
1292 			/*
1293 			 * XXX: maybe LINKER_UNLOAD_FORCE should override ?
1294 			 */
1295 			printf("kldunload: attempt to unload file that was"
1296 			    " loaded by the kernel\n");
1297 			error = EBUSY;
1298 		} else if (lf->refs > 1) {
1299 			error = EBUSY;
1300 		} else {
1301 			lf->userrefs--;
1302 			error = linker_file_unload(lf, flags);
1303 			if (error)
1304 				lf->userrefs++;
1305 		}
1306 	} else
1307 		error = ENOENT;
1308 	CURVNET_RESTORE();
1309 	linker_kldload_unbusy(LINKER_UB_LOCKED);
1310 	return (error);
1311 }
1312 
1313 int
sys_kldunload(struct thread * td,struct kldunload_args * uap)1314 sys_kldunload(struct thread *td, struct kldunload_args *uap)
1315 {
1316 
1317 	return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL));
1318 }
1319 
1320 int
sys_kldunloadf(struct thread * td,struct kldunloadf_args * uap)1321 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap)
1322 {
1323 
1324 	if (uap->flags != LINKER_UNLOAD_NORMAL &&
1325 	    uap->flags != LINKER_UNLOAD_FORCE)
1326 		return (EINVAL);
1327 	return (kern_kldunload(td, uap->fileid, uap->flags));
1328 }
1329 
1330 int
sys_kldfind(struct thread * td,struct kldfind_args * uap)1331 sys_kldfind(struct thread *td, struct kldfind_args *uap)
1332 {
1333 	char *pathname;
1334 	const char *filename;
1335 	linker_file_t lf;
1336 	int error;
1337 
1338 #ifdef MAC
1339 	error = mac_kld_check_stat(td->td_ucred);
1340 	if (error)
1341 		return (error);
1342 #endif
1343 
1344 	td->td_retval[0] = -1;
1345 
1346 	pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1347 	if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0)
1348 		goto out;
1349 
1350 	filename = linker_basename(pathname);
1351 	sx_xlock(&kld_sx);
1352 	lf = linker_find_file_by_name(filename);
1353 	if (lf)
1354 		td->td_retval[0] = lf->id;
1355 	else
1356 		error = ENOENT;
1357 	sx_xunlock(&kld_sx);
1358 out:
1359 	free(pathname, M_TEMP);
1360 	return (error);
1361 }
1362 
1363 int
sys_kldnext(struct thread * td,struct kldnext_args * uap)1364 sys_kldnext(struct thread *td, struct kldnext_args *uap)
1365 {
1366 	linker_file_t lf;
1367 	int error = 0;
1368 
1369 #ifdef MAC
1370 	error = mac_kld_check_stat(td->td_ucred);
1371 	if (error)
1372 		return (error);
1373 #endif
1374 
1375 	sx_xlock(&kld_sx);
1376 	if (uap->fileid == 0)
1377 		lf = TAILQ_FIRST(&linker_files);
1378 	else {
1379 		lf = linker_find_file_by_id(uap->fileid);
1380 		if (lf == NULL) {
1381 			error = ENOENT;
1382 			goto out;
1383 		}
1384 		lf = TAILQ_NEXT(lf, link);
1385 	}
1386 
1387 	/* Skip partially loaded files. */
1388 	while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED))
1389 		lf = TAILQ_NEXT(lf, link);
1390 
1391 	if (lf)
1392 		td->td_retval[0] = lf->id;
1393 	else
1394 		td->td_retval[0] = 0;
1395 out:
1396 	sx_xunlock(&kld_sx);
1397 	return (error);
1398 }
1399 
1400 int
sys_kldstat(struct thread * td,struct kldstat_args * uap)1401 sys_kldstat(struct thread *td, struct kldstat_args *uap)
1402 {
1403 	struct kld_file_stat *stat;
1404 	int error, version;
1405 
1406 	/*
1407 	 * Check the version of the user's structure.
1408 	 */
1409 	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
1410 	    != 0)
1411 		return (error);
1412 	if (version != sizeof(struct kld_file_stat_1) &&
1413 	    version != sizeof(struct kld_file_stat))
1414 		return (EINVAL);
1415 
1416 	stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
1417 	error = kern_kldstat(td, uap->fileid, stat);
1418 	if (error == 0)
1419 		error = copyout(stat, uap->stat, version);
1420 	free(stat, M_TEMP);
1421 	return (error);
1422 }
1423 
1424 int
kern_kldstat(struct thread * td,int fileid,struct kld_file_stat * stat)1425 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat)
1426 {
1427 	linker_file_t lf;
1428 	int namelen;
1429 #ifdef MAC
1430 	int error;
1431 
1432 	error = mac_kld_check_stat(td->td_ucred);
1433 	if (error)
1434 		return (error);
1435 #endif
1436 
1437 	sx_xlock(&kld_sx);
1438 	lf = linker_find_file_by_id(fileid);
1439 	if (lf == NULL) {
1440 		sx_xunlock(&kld_sx);
1441 		return (ENOENT);
1442 	}
1443 
1444 	/* Version 1 fields: */
1445 	namelen = strlen(lf->filename) + 1;
1446 	if (namelen > sizeof(stat->name))
1447 		namelen = sizeof(stat->name);
1448 	bcopy(lf->filename, &stat->name[0], namelen);
1449 	stat->refs = lf->refs;
1450 	stat->id = lf->id;
1451 	stat->address = lf->address;
1452 	stat->size = lf->size;
1453 	/* Version 2 fields: */
1454 	namelen = strlen(lf->pathname) + 1;
1455 	if (namelen > sizeof(stat->pathname))
1456 		namelen = sizeof(stat->pathname);
1457 	bcopy(lf->pathname, &stat->pathname[0], namelen);
1458 	sx_xunlock(&kld_sx);
1459 
1460 	td->td_retval[0] = 0;
1461 	return (0);
1462 }
1463 
1464 #ifdef DDB
DB_COMMAND_FLAGS(kldstat,db_kldstat,DB_CMD_MEMSAFE)1465 DB_COMMAND_FLAGS(kldstat, db_kldstat, DB_CMD_MEMSAFE)
1466 {
1467 	linker_file_t lf;
1468 
1469 #define	POINTER_WIDTH	((int)(sizeof(void *) * 2 + 2))
1470 	db_printf("Id Refs Address%*c Size     Name\n", POINTER_WIDTH - 7, ' ');
1471 #undef	POINTER_WIDTH
1472 	TAILQ_FOREACH(lf, &linker_files, link) {
1473 		if (db_pager_quit)
1474 			return;
1475 		db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs,
1476 		    lf->address, lf->size, lf->filename);
1477 	}
1478 }
1479 #endif /* DDB */
1480 
1481 int
sys_kldfirstmod(struct thread * td,struct kldfirstmod_args * uap)1482 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap)
1483 {
1484 	linker_file_t lf;
1485 	module_t mp;
1486 	int error = 0;
1487 
1488 #ifdef MAC
1489 	error = mac_kld_check_stat(td->td_ucred);
1490 	if (error)
1491 		return (error);
1492 #endif
1493 
1494 	sx_xlock(&kld_sx);
1495 	lf = linker_find_file_by_id(uap->fileid);
1496 	if (lf) {
1497 		MOD_SLOCK;
1498 		mp = TAILQ_FIRST(&lf->modules);
1499 		if (mp != NULL)
1500 			td->td_retval[0] = module_getid(mp);
1501 		else
1502 			td->td_retval[0] = 0;
1503 		MOD_SUNLOCK;
1504 	} else
1505 		error = ENOENT;
1506 	sx_xunlock(&kld_sx);
1507 	return (error);
1508 }
1509 
1510 int
sys_kldsym(struct thread * td,struct kldsym_args * uap)1511 sys_kldsym(struct thread *td, struct kldsym_args *uap)
1512 {
1513 	char *symstr = NULL;
1514 	c_linker_sym_t sym;
1515 	linker_symval_t symval;
1516 	linker_file_t lf;
1517 	struct kld_sym_lookup lookup;
1518 	int error = 0;
1519 
1520 #ifdef MAC
1521 	error = mac_kld_check_stat(td->td_ucred);
1522 	if (error)
1523 		return (error);
1524 #endif
1525 
1526 	if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0)
1527 		return (error);
1528 	if (lookup.version != sizeof(lookup) ||
1529 	    uap->cmd != KLDSYM_LOOKUP)
1530 		return (EINVAL);
1531 	symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1532 	if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0)
1533 		goto out;
1534 	sx_xlock(&kld_sx);
1535 	if (uap->fileid != 0) {
1536 		lf = linker_find_file_by_id(uap->fileid);
1537 		if (lf == NULL)
1538 			error = ENOENT;
1539 		else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1540 		    LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1541 			lookup.symvalue = (uintptr_t) symval.value;
1542 			lookup.symsize = symval.size;
1543 			error = copyout(&lookup, uap->data, sizeof(lookup));
1544 		} else
1545 			error = ENOENT;
1546 	} else {
1547 		TAILQ_FOREACH(lf, &linker_files, link) {
1548 			if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 &&
1549 			    LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) {
1550 				lookup.symvalue = (uintptr_t)symval.value;
1551 				lookup.symsize = symval.size;
1552 				error = copyout(&lookup, uap->data,
1553 				    sizeof(lookup));
1554 				break;
1555 			}
1556 		}
1557 		if (lf == NULL)
1558 			error = ENOENT;
1559 	}
1560 	sx_xunlock(&kld_sx);
1561 out:
1562 	free(symstr, M_TEMP);
1563 	return (error);
1564 }
1565 
1566 /*
1567  * Preloaded module support
1568  */
1569 
1570 static modlist_t
modlist_lookup(const char * name,int ver)1571 modlist_lookup(const char *name, int ver)
1572 {
1573 	modlist_t mod;
1574 
1575 	TAILQ_FOREACH(mod, &found_modules, link) {
1576 		if (strcmp(mod->name, name) == 0 &&
1577 		    (ver == 0 || mod->version == ver))
1578 			return (mod);
1579 	}
1580 	return (NULL);
1581 }
1582 
1583 static modlist_t
modlist_lookup2(const char * name,const struct mod_depend * verinfo)1584 modlist_lookup2(const char *name, const struct mod_depend *verinfo)
1585 {
1586 	modlist_t mod, bestmod;
1587 	int ver;
1588 
1589 	if (verinfo == NULL)
1590 		return (modlist_lookup(name, 0));
1591 	bestmod = NULL;
1592 	TAILQ_FOREACH(mod, &found_modules, link) {
1593 		if (strcmp(mod->name, name) != 0)
1594 			continue;
1595 		ver = mod->version;
1596 		if (ver == verinfo->md_ver_preferred)
1597 			return (mod);
1598 		if (ver >= verinfo->md_ver_minimum &&
1599 		    ver <= verinfo->md_ver_maximum &&
1600 		    (bestmod == NULL || ver > bestmod->version))
1601 			bestmod = mod;
1602 	}
1603 	return (bestmod);
1604 }
1605 
1606 static modlist_t
modlist_newmodule(const char * modname,int version,linker_file_t container)1607 modlist_newmodule(const char *modname, int version, linker_file_t container)
1608 {
1609 	modlist_t mod;
1610 
1611 	mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO);
1612 	if (mod == NULL)
1613 		panic("no memory for module list");
1614 	mod->container = container;
1615 	mod->name = modname;
1616 	mod->version = version;
1617 	TAILQ_INSERT_TAIL(&found_modules, mod, link);
1618 	return (mod);
1619 }
1620 
1621 static void
linker_addmodules(linker_file_t lf,struct mod_metadata ** start,struct mod_metadata ** stop,int preload)1622 linker_addmodules(linker_file_t lf, struct mod_metadata **start,
1623     struct mod_metadata **stop, int preload)
1624 {
1625 	struct mod_metadata *mp, **mdp;
1626 	const char *modname;
1627 	int ver;
1628 
1629 	for (mdp = start; mdp < stop; mdp++) {
1630 		mp = *mdp;
1631 		if (mp->md_type != MDT_VERSION)
1632 			continue;
1633 		modname = mp->md_cval;
1634 		ver = ((const struct mod_version *)mp->md_data)->mv_version;
1635 		if (modlist_lookup(modname, ver) != NULL) {
1636 			printf("module %s already present!\n", modname);
1637 			/* XXX what can we do? this is a build error. :-( */
1638 			continue;
1639 		}
1640 		modlist_newmodule(modname, ver, lf);
1641 	}
1642 }
1643 
1644 static void
linker_preload(void * arg)1645 linker_preload(void *arg)
1646 {
1647 	caddr_t modptr;
1648 	const char *modname, *nmodname;
1649 	char *modtype;
1650 	linker_file_t lf, nlf;
1651 	linker_class_t lc;
1652 	int error;
1653 	linker_file_list_t loaded_files;
1654 	linker_file_list_t depended_files;
1655 	struct mod_metadata *mp, *nmp;
1656 	struct mod_metadata **start, **stop, **mdp, **nmdp;
1657 	const struct mod_depend *verinfo;
1658 	int nver;
1659 	int resolves;
1660 	modlist_t mod;
1661 	struct sysinit **si_start, **si_stop;
1662 
1663 	TAILQ_INIT(&loaded_files);
1664 	TAILQ_INIT(&depended_files);
1665 	TAILQ_INIT(&found_modules);
1666 	error = 0;
1667 
1668 	modptr = NULL;
1669 	sx_xlock(&kld_sx);
1670 	while ((modptr = preload_search_next_name(modptr)) != NULL) {
1671 		modname = (char *)preload_search_info(modptr, MODINFO_NAME);
1672 		modtype = (char *)preload_search_info(modptr, MODINFO_TYPE);
1673 		if (modname == NULL) {
1674 			printf("Preloaded module at %p does not have a"
1675 			    " name!\n", modptr);
1676 			continue;
1677 		}
1678 		if (modtype == NULL) {
1679 			printf("Preloaded module at %p does not have a type!\n",
1680 			    modptr);
1681 			continue;
1682 		}
1683 		if (bootverbose)
1684 			printf("Preloaded %s \"%s\" at %p.\n", modtype, modname,
1685 			    modptr);
1686 		lf = NULL;
1687 		TAILQ_FOREACH(lc, &classes, link) {
1688 			error = LINKER_LINK_PRELOAD(lc, modname, &lf);
1689 			if (!error)
1690 				break;
1691 			lf = NULL;
1692 		}
1693 		if (lf)
1694 			TAILQ_INSERT_TAIL(&loaded_files, lf, loaded);
1695 	}
1696 
1697 	/*
1698 	 * First get a list of stuff in the kernel.
1699 	 */
1700 	if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start,
1701 	    &stop, NULL) == 0)
1702 		linker_addmodules(linker_kernel_file, start, stop, 1);
1703 
1704 	/*
1705 	 * This is a once-off kinky bubble sort to resolve relocation
1706 	 * dependency requirements.
1707 	 */
1708 restart:
1709 	TAILQ_FOREACH(lf, &loaded_files, loaded) {
1710 		error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1711 		    &stop, NULL);
1712 		/*
1713 		 * First, look to see if we would successfully link with this
1714 		 * stuff.
1715 		 */
1716 		resolves = 1;	/* unless we know otherwise */
1717 		if (!error) {
1718 			for (mdp = start; mdp < stop; mdp++) {
1719 				mp = *mdp;
1720 				if (mp->md_type != MDT_DEPEND)
1721 					continue;
1722 				modname = mp->md_cval;
1723 				verinfo = mp->md_data;
1724 				for (nmdp = start; nmdp < stop; nmdp++) {
1725 					nmp = *nmdp;
1726 					if (nmp->md_type != MDT_VERSION)
1727 						continue;
1728 					nmodname = nmp->md_cval;
1729 					if (strcmp(modname, nmodname) == 0)
1730 						break;
1731 				}
1732 				if (nmdp < stop)   /* it's a self reference */
1733 					continue;
1734 
1735 				/*
1736 				 * ok, the module isn't here yet, we
1737 				 * are not finished
1738 				 */
1739 				if (modlist_lookup2(modname, verinfo) == NULL)
1740 					resolves = 0;
1741 			}
1742 		}
1743 		/*
1744 		 * OK, if we found our modules, we can link.  So, "provide"
1745 		 * the modules inside and add it to the end of the link order
1746 		 * list.
1747 		 */
1748 		if (resolves) {
1749 			if (!error) {
1750 				for (mdp = start; mdp < stop; mdp++) {
1751 					mp = *mdp;
1752 					if (mp->md_type != MDT_VERSION)
1753 						continue;
1754 					modname = mp->md_cval;
1755 					nver = ((const struct mod_version *)
1756 					    mp->md_data)->mv_version;
1757 					if (modlist_lookup(modname,
1758 					    nver) != NULL) {
1759 						printf("module %s already"
1760 						    " present!\n", modname);
1761 						TAILQ_REMOVE(&loaded_files,
1762 						    lf, loaded);
1763 						linker_file_unload(lf,
1764 						    LINKER_UNLOAD_FORCE);
1765 						/* we changed tailq next ptr */
1766 						goto restart;
1767 					}
1768 					modlist_newmodule(modname, nver, lf);
1769 				}
1770 			}
1771 			TAILQ_REMOVE(&loaded_files, lf, loaded);
1772 			TAILQ_INSERT_TAIL(&depended_files, lf, loaded);
1773 			/*
1774 			 * Since we provided modules, we need to restart the
1775 			 * sort so that the previous files that depend on us
1776 			 * have a chance. Also, we've busted the tailq next
1777 			 * pointer with the REMOVE.
1778 			 */
1779 			goto restart;
1780 		}
1781 	}
1782 
1783 	/*
1784 	 * At this point, we check to see what could not be resolved..
1785 	 */
1786 	while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) {
1787 		TAILQ_REMOVE(&loaded_files, lf, loaded);
1788 		printf("KLD file %s is missing dependencies\n", lf->filename);
1789 		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1790 	}
1791 
1792 	/*
1793 	 * We made it. Finish off the linking in the order we determined.
1794 	 */
1795 	TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) {
1796 		if (linker_kernel_file) {
1797 			linker_kernel_file->refs++;
1798 			linker_file_add_dependency(lf, linker_kernel_file);
1799 		}
1800 		error = linker_file_lookup_set(lf, MDT_SETNAME, &start,
1801 		    &stop, NULL);
1802 		if (!error) {
1803 			for (mdp = start; mdp < stop; mdp++) {
1804 				mp = *mdp;
1805 				if (mp->md_type != MDT_DEPEND)
1806 					continue;
1807 				modname = mp->md_cval;
1808 				verinfo = mp->md_data;
1809 				mod = modlist_lookup2(modname, verinfo);
1810 				if (mod == NULL) {
1811 					printf("KLD file %s - cannot find "
1812 					    "dependency \"%s\"\n",
1813 					    lf->filename, modname);
1814 					goto fail;
1815 				}
1816 				/* Don't count self-dependencies */
1817 				if (lf == mod->container)
1818 					continue;
1819 				mod->container->refs++;
1820 				linker_file_add_dependency(lf, mod->container);
1821 			}
1822 		}
1823 		/*
1824 		 * Now do relocation etc using the symbol search paths
1825 		 * established by the dependencies
1826 		 */
1827 		error = LINKER_LINK_PRELOAD_FINISH(lf);
1828 		if (error) {
1829 			printf("KLD file %s - could not finalize loading\n",
1830 			    lf->filename);
1831 			goto fail;
1832 		}
1833 		linker_file_register_modules(lf);
1834 		if (!TAILQ_EMPTY(&lf->modules))
1835 			lf->flags |= LINKER_FILE_MODULES;
1836 		if (linker_file_lookup_set(lf, "sysinit_set", &si_start,
1837 		    &si_stop, NULL) == 0)
1838 			sysinit_add(si_start, si_stop);
1839 		linker_file_register_sysctls(lf, true);
1840 		lf->flags |= LINKER_FILE_LINKED;
1841 		continue;
1842 fail:
1843 		TAILQ_REMOVE(&depended_files, lf, loaded);
1844 		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1845 	}
1846 	sx_xunlock(&kld_sx);
1847 	/* woohoo! we made it! */
1848 }
1849 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL);
1850 
1851 static void
linker_mountroot(void * arg __unused)1852 linker_mountroot(void *arg __unused)
1853 {
1854 	linker_file_t lf;
1855 
1856 	sx_xlock(&kld_sx);
1857 	TAILQ_FOREACH (lf, &linker_files, link) {
1858 		linker_ctf_load_file(lf);
1859 	}
1860 	sx_xunlock(&kld_sx);
1861 }
1862 EVENTHANDLER_DEFINE(mountroot, linker_mountroot, NULL, 0);
1863 
1864 /*
1865  * Handle preload files that failed to load any modules.
1866  */
1867 static void
linker_preload_finish(void * arg)1868 linker_preload_finish(void *arg)
1869 {
1870 	linker_file_t lf, nlf;
1871 
1872 	sx_xlock(&kld_sx);
1873 	TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
1874 		if (lf == linker_kernel_file)
1875 			continue;
1876 
1877 		/*
1878 		 * If all of the modules in this file failed to load, unload
1879 		 * the file and return an error of ENOEXEC.  (Parity with
1880 		 * linker_load_file.)
1881 		 */
1882 		if ((lf->flags & LINKER_FILE_MODULES) != 0 &&
1883 		    TAILQ_EMPTY(&lf->modules)) {
1884 			linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1885 			continue;
1886 		}
1887 
1888 		lf->flags &= ~LINKER_FILE_MODULES;
1889 		lf->userrefs++;	/* so we can (try to) kldunload it */
1890 	}
1891 	sx_xunlock(&kld_sx);
1892 }
1893 
1894 /*
1895  * Attempt to run after all DECLARE_MODULE SYSINITs.  Unfortunately they can be
1896  * scheduled at any subsystem and order, so run this as late as possible.  init
1897  * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that.
1898  */
1899 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE,
1900     linker_preload_finish, NULL);
1901 
1902 /*
1903  * Search for a not-loaded module by name.
1904  *
1905  * Modules may be found in the following locations:
1906  *
1907  * - preloaded (result is just the module name) - on disk (result is full path
1908  * to module)
1909  *
1910  * If the module name is qualified in any way (contains path, etc.) the we
1911  * simply return a copy of it.
1912  *
1913  * The search path can be manipulated via sysctl.  Note that we use the ';'
1914  * character as a separator to be consistent with the bootloader.
1915  */
1916 
1917 static char linker_hintfile[] = "linker.hints";
1918 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules";
1919 
1920 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path,
1921     sizeof(linker_path), "module load search path");
1922 
1923 TUNABLE_STR("module_path", linker_path, sizeof(linker_path));
1924 
1925 static const char * const linker_ext_list[] = {
1926 	"",
1927 	".ko",
1928 	NULL
1929 };
1930 
1931 /*
1932  * Check if file actually exists either with or without extension listed in
1933  * the linker_ext_list. (probably should be generic for the rest of the
1934  * kernel)
1935  */
1936 static char *
linker_lookup_file(const char * path,int pathlen,const char * name,int namelen,struct vattr * vap)1937 linker_lookup_file(const char *path, int pathlen, const char *name,
1938     int namelen, struct vattr *vap)
1939 {
1940 	struct nameidata nd;
1941 	struct thread *td = curthread;	/* XXX */
1942 	const char * const *cpp, *sep;
1943 	char *result;
1944 	int error, len, extlen, reclen, flags;
1945 	__enum_uint8(vtype) type;
1946 
1947 	extlen = 0;
1948 	for (cpp = linker_ext_list; *cpp; cpp++) {
1949 		len = strlen(*cpp);
1950 		if (len > extlen)
1951 			extlen = len;
1952 	}
1953 	extlen++;		/* trailing '\0' */
1954 	sep = (path[pathlen - 1] != '/') ? "/" : "";
1955 
1956 	reclen = pathlen + strlen(sep) + namelen + extlen + 1;
1957 	result = malloc(reclen, M_LINKER, M_WAITOK);
1958 	for (cpp = linker_ext_list; *cpp; cpp++) {
1959 		snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep,
1960 		    namelen, name, *cpp);
1961 		/*
1962 		 * Attempt to open the file, and return the path if
1963 		 * we succeed and it's a regular file.
1964 		 */
1965 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result);
1966 		flags = FREAD;
1967 		error = vn_open(&nd, &flags, 0, NULL);
1968 		if (error == 0) {
1969 			NDFREE_PNBUF(&nd);
1970 			type = nd.ni_vp->v_type;
1971 			if (vap)
1972 				VOP_GETATTR(nd.ni_vp, vap, td->td_ucred);
1973 			VOP_UNLOCK(nd.ni_vp);
1974 			vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1975 			if (type == VREG)
1976 				return (result);
1977 		}
1978 	}
1979 	free(result, M_LINKER);
1980 	return (NULL);
1981 }
1982 
1983 #define	INT_ALIGN(base, ptr)	ptr =					\
1984 	(base) + roundup2((ptr) - (base), sizeof(int))
1985 
1986 /*
1987  * Lookup KLD which contains requested module in the "linker.hints" file. If
1988  * version specification is available, then try to find the best KLD.
1989  * Otherwise just find the latest one.
1990  */
1991 static char *
linker_hints_lookup(const char * path,int pathlen,const char * modname,int modnamelen,const struct mod_depend * verinfo)1992 linker_hints_lookup(const char *path, int pathlen, const char *modname,
1993     int modnamelen, const struct mod_depend *verinfo)
1994 {
1995 	struct thread *td = curthread;	/* XXX */
1996 	struct ucred *cred = td ? td->td_ucred : NULL;
1997 	struct nameidata nd;
1998 	struct vattr vattr, mattr;
1999 	const char *best, *sep;
2000 	u_char *hints = NULL;
2001 	u_char *cp, *recptr, *bufend, *result, *pathbuf;
2002 	int error, ival, bestver, *intp, found, flags, clen, blen;
2003 	ssize_t reclen;
2004 
2005 	result = NULL;
2006 	bestver = found = 0;
2007 
2008 	sep = (path[pathlen - 1] != '/') ? "/" : "";
2009 	reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen +
2010 	    strlen(sep) + 1;
2011 	pathbuf = malloc(reclen, M_LINKER, M_WAITOK);
2012 	snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep,
2013 	    linker_hintfile);
2014 
2015 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf);
2016 	flags = FREAD;
2017 	error = vn_open(&nd, &flags, 0, NULL);
2018 	if (error)
2019 		goto bad;
2020 	NDFREE_PNBUF(&nd);
2021 	if (nd.ni_vp->v_type != VREG)
2022 		goto bad;
2023 	best = cp = NULL;
2024 	error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
2025 	if (error)
2026 		goto bad;
2027 	/*
2028 	 * XXX: we need to limit this number to some reasonable value
2029 	 */
2030 	if (vattr.va_size > LINKER_HINTS_MAX) {
2031 		printf("linker.hints file too large %ld\n", (long)vattr.va_size);
2032 		goto bad;
2033 	}
2034 	if (vattr.va_size < sizeof(ival)) {
2035 		printf("linker.hints file truncated\n");
2036 		goto bad;
2037 	}
2038 	hints = malloc(vattr.va_size, M_TEMP, M_WAITOK);
2039 	error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0,
2040 	    UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td);
2041 	if (error)
2042 		goto bad;
2043 	VOP_UNLOCK(nd.ni_vp);
2044 	vn_close(nd.ni_vp, FREAD, cred, td);
2045 	nd.ni_vp = NULL;
2046 	if (reclen != 0) {
2047 		printf("can't read %zd\n", reclen);
2048 		goto bad;
2049 	}
2050 	intp = (int *)hints;
2051 	ival = *intp++;
2052 	if (ival != LINKER_HINTS_VERSION) {
2053 		printf("linker.hints file version mismatch %d\n", ival);
2054 		goto bad;
2055 	}
2056 	bufend = hints + vattr.va_size;
2057 	recptr = (u_char *)intp;
2058 	clen = blen = 0;
2059 	while (recptr < bufend && !found) {
2060 		intp = (int *)recptr;
2061 		reclen = *intp++;
2062 		ival = *intp++;
2063 		cp = (char *)intp;
2064 		switch (ival) {
2065 		case MDT_VERSION:
2066 			clen = *cp++;
2067 			if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
2068 				break;
2069 			cp += clen;
2070 			INT_ALIGN(hints, cp);
2071 			ival = *(int *)cp;
2072 			cp += sizeof(int);
2073 			clen = *cp++;
2074 			if (verinfo == NULL ||
2075 			    ival == verinfo->md_ver_preferred) {
2076 				found = 1;
2077 				break;
2078 			}
2079 			if (ival >= verinfo->md_ver_minimum &&
2080 			    ival <= verinfo->md_ver_maximum &&
2081 			    ival > bestver) {
2082 				bestver = ival;
2083 				best = cp;
2084 				blen = clen;
2085 			}
2086 			break;
2087 		default:
2088 			break;
2089 		}
2090 		recptr += reclen + sizeof(int);
2091 	}
2092 	/*
2093 	 * Finally check if KLD is in the place
2094 	 */
2095 	if (found)
2096 		result = linker_lookup_file(path, pathlen, cp, clen, &mattr);
2097 	else if (best)
2098 		result = linker_lookup_file(path, pathlen, best, blen, &mattr);
2099 
2100 	/*
2101 	 * KLD is newer than hints file. What we should do now?
2102 	 */
2103 	if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >))
2104 		printf("warning: KLD '%s' is newer than the linker.hints"
2105 		    " file\n", result);
2106 bad:
2107 	free(pathbuf, M_LINKER);
2108 	if (hints)
2109 		free(hints, M_TEMP);
2110 	if (nd.ni_vp != NULL) {
2111 		VOP_UNLOCK(nd.ni_vp);
2112 		vn_close(nd.ni_vp, FREAD, cred, td);
2113 	}
2114 	/*
2115 	 * If nothing found or hints is absent - fallback to the old
2116 	 * way by using "kldname[.ko]" as module name.
2117 	 */
2118 	if (!found && !bestver && result == NULL)
2119 		result = linker_lookup_file(path, pathlen, modname,
2120 		    modnamelen, NULL);
2121 	return (result);
2122 }
2123 
2124 /*
2125  * Lookup KLD which contains requested module in the all directories.
2126  */
2127 static char *
linker_search_module(const char * modname,int modnamelen,const struct mod_depend * verinfo)2128 linker_search_module(const char *modname, int modnamelen,
2129     const struct mod_depend *verinfo)
2130 {
2131 	char *cp, *ep, *result;
2132 
2133 	/*
2134 	 * traverse the linker path
2135 	 */
2136 	for (cp = linker_path; *cp; cp = ep + 1) {
2137 		/* find the end of this component */
2138 		for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
2139 		result = linker_hints_lookup(cp, ep - cp, modname,
2140 		    modnamelen, verinfo);
2141 		if (result != NULL)
2142 			return (result);
2143 		if (*ep == 0)
2144 			break;
2145 	}
2146 	return (NULL);
2147 }
2148 
2149 /*
2150  * Search for module in all directories listed in the linker_path.
2151  */
2152 static char *
linker_search_kld(const char * name)2153 linker_search_kld(const char *name)
2154 {
2155 	char *cp, *ep, *result;
2156 	int len;
2157 
2158 	/* qualified at all? */
2159 	if (strchr(name, '/'))
2160 		return (strdup(name, M_LINKER));
2161 
2162 	/* traverse the linker path */
2163 	len = strlen(name);
2164 	for (ep = linker_path; *ep; ep++) {
2165 		cp = ep;
2166 		/* find the end of this component */
2167 		for (; *ep != 0 && *ep != ';'; ep++);
2168 		result = linker_lookup_file(cp, ep - cp, name, len, NULL);
2169 		if (result != NULL)
2170 			return (result);
2171 	}
2172 	return (NULL);
2173 }
2174 
2175 static const char *
linker_basename(const char * path)2176 linker_basename(const char *path)
2177 {
2178 	const char *filename;
2179 
2180 	filename = strrchr(path, '/');
2181 	if (filename == NULL)
2182 		return path;
2183 	if (filename[1])
2184 		filename++;
2185 	return (filename);
2186 }
2187 
2188 #if defined(HWPMC_HOOKS) || defined(HWT_HOOKS)
2189 /*
2190  * Inform hwpmc about the set of kernel modules currently loaded.
2191  */
2192 void *
linker_hwpmc_list_objects(void)2193 linker_hwpmc_list_objects(void)
2194 {
2195 	linker_file_t lf;
2196 	struct pmckern_map_in *kobase;
2197 	int i, nmappings;
2198 
2199 	nmappings = 0;
2200 	sx_slock(&kld_sx);
2201 	TAILQ_FOREACH(lf, &linker_files, link)
2202 		nmappings++;
2203 
2204 	/* Allocate nmappings + 1 entries. */
2205 	kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in),
2206 	    M_LINKER, M_WAITOK | M_ZERO);
2207 	i = 0;
2208 	TAILQ_FOREACH(lf, &linker_files, link) {
2209 		/* Save the info for this linker file. */
2210 		kobase[i].pm_file = lf->pathname;
2211 		kobase[i].pm_address = (uintptr_t)lf->address;
2212 		i++;
2213 	}
2214 	sx_sunlock(&kld_sx);
2215 
2216 	KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?"));
2217 
2218 	/* The last entry of the malloced area comprises of all zeros. */
2219 	KASSERT(kobase[i].pm_file == NULL,
2220 	    ("linker_hwpmc_list_objects: last object not NULL"));
2221 
2222 	return ((void *)kobase);
2223 }
2224 #endif
2225 
2226 /* check if root file system is not mounted */
2227 static bool
linker_root_mounted(void)2228 linker_root_mounted(void)
2229 {
2230 	struct pwd *pwd;
2231 	bool ret;
2232 
2233 	if (rootvnode == NULL)
2234 		return (false);
2235 
2236 	pwd = pwd_hold(curthread);
2237 	ret = pwd->pwd_rdir != NULL;
2238 	pwd_drop(pwd);
2239 	return (ret);
2240 }
2241 
2242 /*
2243  * Find a file which contains given module and load it, if "parent" is not
2244  * NULL, register a reference to it.
2245  */
2246 static int
linker_load_module(const char * kldname,const char * modname,struct linker_file * parent,const struct mod_depend * verinfo,struct linker_file ** lfpp)2247 linker_load_module(const char *kldname, const char *modname,
2248     struct linker_file *parent, const struct mod_depend *verinfo,
2249     struct linker_file **lfpp)
2250 {
2251 	linker_file_t lfdep;
2252 	const char *filename;
2253 	char *pathname;
2254 	int error;
2255 
2256 	sx_assert(&kld_sx, SA_XLOCKED);
2257 	if (modname == NULL) {
2258 		/*
2259  		 * We have to load KLD
2260  		 */
2261 		KASSERT(verinfo == NULL, ("linker_load_module: verinfo"
2262 		    " is not NULL"));
2263 		if (!linker_root_mounted())
2264 			return (ENXIO);
2265 		pathname = linker_search_kld(kldname);
2266 	} else {
2267 		if (modlist_lookup2(modname, verinfo) != NULL)
2268 			return (EEXIST);
2269 		if (!linker_root_mounted())
2270 			return (ENXIO);
2271 		if (kldname != NULL)
2272 			pathname = strdup(kldname, M_LINKER);
2273 		else
2274 			/*
2275 			 * Need to find a KLD with required module
2276 			 */
2277 			pathname = linker_search_module(modname,
2278 			    strlen(modname), verinfo);
2279 	}
2280 	if (pathname == NULL)
2281 		return (ENOENT);
2282 
2283 	/*
2284 	 * Can't load more than one file with the same basename XXX:
2285 	 * Actually it should be possible to have multiple KLDs with
2286 	 * the same basename but different path because they can
2287 	 * provide different versions of the same modules.
2288 	 */
2289 	filename = linker_basename(pathname);
2290 	if (linker_find_file_by_name(filename))
2291 		error = EEXIST;
2292 	else do {
2293 		error = linker_load_file(pathname, &lfdep);
2294 		if (error)
2295 			break;
2296 		if (modname && verinfo &&
2297 		    modlist_lookup2(modname, verinfo) == NULL) {
2298 			linker_file_unload(lfdep, LINKER_UNLOAD_FORCE);
2299 			error = ENOENT;
2300 			break;
2301 		}
2302 		if (parent)
2303 			linker_file_add_dependency(parent, lfdep);
2304 		if (lfpp)
2305 			*lfpp = lfdep;
2306 	} while (0);
2307 	free(pathname, M_LINKER);
2308 	return (error);
2309 }
2310 
2311 /*
2312  * This routine is responsible for finding dependencies of userland initiated
2313  * kldload(2)'s of files.
2314  */
2315 int
linker_load_dependencies(linker_file_t lf)2316 linker_load_dependencies(linker_file_t lf)
2317 {
2318 	linker_file_t lfdep;
2319 	struct mod_metadata **start, **stop, **mdp, **nmdp;
2320 	struct mod_metadata *mp, *nmp;
2321 	const struct mod_depend *verinfo;
2322 	modlist_t mod;
2323 	const char *modname, *nmodname;
2324 	int ver, error = 0;
2325 
2326 	/*
2327 	 * All files are dependent on /kernel.
2328 	 */
2329 	sx_assert(&kld_sx, SA_XLOCKED);
2330 	if (linker_kernel_file) {
2331 		linker_kernel_file->refs++;
2332 		linker_file_add_dependency(lf, linker_kernel_file);
2333 	}
2334 	if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop,
2335 	    NULL) != 0)
2336 		return (0);
2337 	for (mdp = start; mdp < stop; mdp++) {
2338 		mp = *mdp;
2339 		if (mp->md_type != MDT_VERSION)
2340 			continue;
2341 		modname = mp->md_cval;
2342 		ver = ((const struct mod_version *)mp->md_data)->mv_version;
2343 		mod = modlist_lookup(modname, ver);
2344 		if (mod != NULL) {
2345 			printf("interface %s.%d already present in the KLD"
2346 			    " '%s'!\n", modname, ver,
2347 			    mod->container->filename);
2348 			return (EEXIST);
2349 		}
2350 	}
2351 
2352 	for (mdp = start; mdp < stop; mdp++) {
2353 		mp = *mdp;
2354 		if (mp->md_type != MDT_DEPEND)
2355 			continue;
2356 		modname = mp->md_cval;
2357 		verinfo = mp->md_data;
2358 		nmodname = NULL;
2359 		for (nmdp = start; nmdp < stop; nmdp++) {
2360 			nmp = *nmdp;
2361 			if (nmp->md_type != MDT_VERSION)
2362 				continue;
2363 			nmodname = nmp->md_cval;
2364 			if (strcmp(modname, nmodname) == 0)
2365 				break;
2366 		}
2367 		if (nmdp < stop)/* early exit, it's a self reference */
2368 			continue;
2369 		mod = modlist_lookup2(modname, verinfo);
2370 		if (mod) {	/* woohoo, it's loaded already */
2371 			lfdep = mod->container;
2372 			lfdep->refs++;
2373 			linker_file_add_dependency(lf, lfdep);
2374 			continue;
2375 		}
2376 		error = linker_load_module(NULL, modname, lf, verinfo, NULL);
2377 		if (error) {
2378 			printf("KLD %s: depends on %s - not available or"
2379 			    " version mismatch\n", lf->filename, modname);
2380 			break;
2381 		}
2382 	}
2383 
2384 	if (error)
2385 		return (error);
2386 	linker_addmodules(lf, start, stop, 0);
2387 	return (error);
2388 }
2389 
2390 static int
sysctl_kern_function_list_iterate(const char * name,void * opaque)2391 sysctl_kern_function_list_iterate(const char *name, void *opaque)
2392 {
2393 	struct sysctl_req *req;
2394 
2395 	req = opaque;
2396 	return (SYSCTL_OUT(req, name, strlen(name) + 1));
2397 }
2398 
2399 /*
2400  * Export a nul-separated, double-nul-terminated list of all function names
2401  * in the kernel.
2402  */
2403 static int
sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)2404 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS)
2405 {
2406 	linker_file_t lf;
2407 	int error;
2408 
2409 #ifdef MAC
2410 	error = mac_kld_check_stat(req->td->td_ucred);
2411 	if (error)
2412 		return (error);
2413 #endif
2414 	error = sysctl_wire_old_buffer(req, 0);
2415 	if (error != 0)
2416 		return (error);
2417 	sx_xlock(&kld_sx);
2418 	TAILQ_FOREACH(lf, &linker_files, link) {
2419 		error = LINKER_EACH_FUNCTION_NAME(lf,
2420 		    sysctl_kern_function_list_iterate, req);
2421 		if (error) {
2422 			sx_xunlock(&kld_sx);
2423 			return (error);
2424 		}
2425 	}
2426 	sx_xunlock(&kld_sx);
2427 	return (SYSCTL_OUT(req, "", 1));
2428 }
2429 
2430 SYSCTL_PROC(_kern, OID_AUTO, function_list,
2431     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2432     sysctl_kern_function_list, "",
2433     "kernel function list");
2434