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