1 /* $NetBSD: kern_module_vfs.c,v 1.20 2026/01/04 01:36:11 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software developed for The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Kernel module file system interaction. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: kern_module_vfs.c,v 1.20 2026/01/04 01:36:11 riastradh Exp $"); 38 39 #define _MODULE_INTERNAL 40 41 #include <sys/param.h> 42 43 #include <sys/fcntl.h> 44 #include <sys/kmem.h> 45 #include <sys/kobj.h> 46 #include <sys/module.h> 47 #include <sys/namei.h> 48 #include <sys/pool.h> 49 #include <sys/stat.h> 50 #include <sys/sdt.h> 51 #include <sys/vnode.h> 52 53 #include <prop/proplib.h> 54 55 static int module_load_plist_vfs(const char *, const bool, 56 prop_dictionary_t *); 57 58 void 59 module_load_vfs_init(void) 60 { 61 module_load_vfs_vec = module_load_vfs; 62 aprint_normal("kern.module.path=%s\n", module_base); 63 } 64 65 int 66 module_load_vfs(const char *name, int flags, bool autoload, 67 module_t *mod, prop_dictionary_t *filedictp) 68 { 69 char *path; 70 bool nochroot; 71 int error; 72 prop_bool_t noload; 73 prop_dictionary_t moduledict; 74 75 nochroot = false; 76 error = 0; 77 path = NULL; 78 moduledict = NULL; 79 if (filedictp) 80 *filedictp = NULL; 81 path = PNBUF_GET(); 82 83 if (!autoload) { 84 if (strchr(name, '/') != NULL) { 85 nochroot = false; 86 snprintf(path, MAXPATHLEN, "%s", name); 87 module_print("Loading module from %s", path); 88 error = kobj_load_vfs(&mod->mod_kobj, path, nochroot); 89 } else 90 error = SET_ERROR(ENOENT); 91 } 92 if (autoload || (error == ENOENT)) { 93 if (strchr(name, '/') == NULL) { 94 nochroot = true; 95 snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod", 96 module_base, name, name); 97 module_print("Loading module from %s", path); 98 error = kobj_load_vfs(&mod->mod_kobj, path, nochroot); 99 } else 100 error = SET_ERROR(ENOENT); 101 } 102 if (error != 0) { 103 PNBUF_PUT(path); 104 module_print("Cannot %sload kernel object `%s'" 105 " error=%d", autoload ? "auto" : "", name, error); 106 return error; 107 } 108 109 /* 110 * Load and process <module>.plist if it exists. 111 */ 112 if ((!ISSET(flags, MODCTL_NO_PROP) && filedictp) || autoload) { 113 error = module_load_plist_vfs(path, nochroot, &moduledict); 114 if (error != 0) { 115 module_print("plist load returned error %d for `%s'", 116 error, path); 117 if (error != ENOENT) 118 goto fail; 119 } else if (autoload) { 120 noload = prop_dictionary_get(moduledict, "noautoload"); 121 if (noload != NULL && prop_bool_true(noload)) { 122 module_error("autoloading is disallowed for " 123 "`%s'", path); 124 prop_object_release(moduledict); 125 error = SET_ERROR(EPERM); 126 goto fail; 127 } 128 } 129 if (error == 0) { /* can get here if error == ENOENT */ 130 if (!ISSET(flags, MODCTL_NO_PROP) && filedictp) 131 *filedictp = moduledict; 132 else 133 prop_object_release(moduledict); 134 } 135 } 136 137 PNBUF_PUT(path); 138 return 0; 139 140 fail: 141 kobj_unload(mod->mod_kobj); 142 PNBUF_PUT(path); 143 return error; 144 } 145 146 /* 147 * module_load_plist_vfs: 148 * 149 * Load a plist located in the file system into memory. 150 */ 151 static int 152 module_load_plist_vfs(const char *modpath, const bool nochroot, 153 prop_dictionary_t *filedictp) 154 { 155 struct pathbuf *pb; 156 struct vnode *vp; 157 struct stat sb; 158 void *base; 159 char *proppath; 160 const size_t plistsize = 8192; 161 size_t resid; 162 int error, pathlen; 163 164 KASSERT(filedictp != NULL); 165 base = NULL; 166 167 proppath = PNBUF_GET(); 168 strlcpy(proppath, modpath, MAXPATHLEN); 169 pathlen = strlen(proppath); 170 if ((pathlen >= 6) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) { 171 strcpy(&proppath[pathlen - 5], ".plist"); 172 } else if (pathlen < MAXPATHLEN - 6) { 173 strcat(proppath, ".plist"); 174 } else { 175 error = SET_ERROR(ENOENT); 176 goto out1; 177 } 178 179 /* XXX this makes an unnecessary extra copy of the path */ 180 pb = pathbuf_create(proppath); 181 if (pb == NULL) { 182 error = SET_ERROR(ENOMEM); 183 goto out1; 184 } 185 module_print("Loading plist from %s", proppath); 186 187 error = vn_open(NULL, pb, (nochroot ? NOCHROOT : 0), FREAD, 0, 188 &vp, NULL, NULL); 189 if (error != 0) { 190 goto out2; 191 } 192 193 error = vn_stat(vp, &sb); 194 if (error != 0) { 195 goto out3; 196 } 197 if (sb.st_size >= (plistsize - 1)) { /* leave space for term \0 */ 198 error = SET_ERROR(EFBIG); 199 goto out3; 200 } 201 202 base = kmem_alloc(plistsize, KM_SLEEP); 203 error = vn_rdwr(UIO_READ, vp, base, sb.st_size, 0, 204 UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp); 205 *((uint8_t *)base + sb.st_size) = '\0'; 206 if (error == 0 && resid != 0) { 207 error = SET_ERROR(EFBIG); 208 } 209 if (error != 0) { 210 kmem_free(base, plistsize); 211 base = NULL; 212 goto out3; 213 } 214 215 *filedictp = prop_dictionary_internalize(base); 216 if (*filedictp == NULL) { 217 error = SET_ERROR(EINVAL); 218 } 219 kmem_free(base, plistsize); 220 base = NULL; 221 KASSERT(error == 0); 222 223 out3: 224 VOP_UNLOCK(vp); 225 vn_close(vp, FREAD, kauth_cred_get()); 226 227 out2: 228 pathbuf_destroy(pb); 229 230 out1: 231 PNBUF_PUT(proppath); 232 return error; 233 } 234