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