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