1 /* $NetBSD: kern_module_vfs.c,v 1.7 2010/06/24 13:03:11 hannken 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.7 2010/06/24 13:03:11 hannken Exp $"); 38 39 #define _MODULE_INTERNAL 40 #include <sys/param.h> 41 #include <sys/fcntl.h> 42 #include <sys/kmem.h> 43 #include <sys/kobj.h> 44 #include <sys/module.h> 45 #include <sys/namei.h> 46 #include <sys/pool.h> 47 #include <sys/stat.h> 48 #include <sys/vnode.h> 49 50 #include <prop/proplib.h> 51 52 static int module_load_plist_vfs(const char *, const bool, 53 prop_dictionary_t *); 54 55 void 56 module_load_vfs_init(void) 57 { 58 module_load_vfs_vec = module_load_vfs; 59 } 60 61 int 62 module_load_vfs(const char *name, int flags, bool autoload, 63 module_t *mod, prop_dictionary_t *filedictp) 64 { 65 char *path; 66 bool nochroot; 67 int error; 68 69 nochroot = false; 70 error = 0; 71 path = NULL; 72 if (filedictp) 73 *filedictp = NULL; 74 path = PNBUF_GET(); 75 76 if (!autoload) { 77 nochroot = false; 78 snprintf(path, MAXPATHLEN, "%s", name); 79 error = kobj_load_vfs(&mod->mod_kobj, path, nochroot); 80 } 81 if (autoload || (error == ENOENT)) { 82 nochroot = true; 83 snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod", 84 module_base, name, name); 85 error = kobj_load_vfs(&mod->mod_kobj, path, nochroot); 86 } 87 if (error != 0) { 88 PNBUF_PUT(path); 89 if (autoload) { 90 module_print("Cannot load kernel object `%s'" 91 " error=%d", name, error); 92 } else { 93 module_error("Cannot load kernel object `%s'" 94 " error=%d", name, error); 95 } 96 return error; 97 } 98 99 /* 100 * Load and process <module>.prop if it exists. 101 */ 102 if ((flags & MODCTL_NO_PROP) == 0 && filedictp) { 103 error = module_load_plist_vfs(path, nochroot, filedictp); 104 if (error != 0) { 105 module_print("plist load returned error %d for `%s'", 106 error, path); 107 if (error != ENOENT) 108 goto fail; 109 } 110 } 111 112 PNBUF_PUT(path); 113 return 0; 114 115 fail: 116 kobj_unload(mod->mod_kobj); 117 PNBUF_PUT(path); 118 return error; 119 } 120 121 /* 122 * module_load_plist_vfs: 123 * 124 * Load a plist located in the file system into memory. 125 */ 126 static int 127 module_load_plist_vfs(const char *modpath, const bool nochroot, 128 prop_dictionary_t *filedictp) 129 { 130 struct nameidata nd; 131 struct stat sb; 132 void *base; 133 char *proppath; 134 const size_t plistsize = 8192; 135 size_t resid; 136 int error, pathlen; 137 138 KASSERT(filedictp != NULL); 139 base = NULL; 140 141 proppath = PNBUF_GET(); 142 strcpy(proppath, modpath); 143 pathlen = strlen(proppath); 144 if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) { 145 strcpy(&proppath[pathlen - 5], ".prop"); 146 } else if (pathlen < MAXPATHLEN - 5) { 147 strcat(proppath, ".prop"); 148 } else { 149 error = ENOENT; 150 goto out1; 151 } 152 153 NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), 154 UIO_SYSSPACE, proppath); 155 156 error = vn_open(&nd, FREAD, 0); 157 if (error != 0) { 158 goto out1; 159 } 160 161 error = vn_stat(nd.ni_vp, &sb); 162 if (error != 0) { 163 goto out; 164 } 165 if (sb.st_size >= (plistsize - 1)) { /* leave space for term \0 */ 166 error = EFBIG; 167 goto out; 168 } 169 170 base = kmem_alloc(plistsize, KM_SLEEP); 171 if (base == NULL) { 172 error = ENOMEM; 173 goto out; 174 } 175 176 error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0, 177 UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp); 178 *((uint8_t *)base + sb.st_size) = '\0'; 179 if (error == 0 && resid != 0) { 180 error = EFBIG; 181 } 182 if (error != 0) { 183 kmem_free(base, plistsize); 184 base = NULL; 185 goto out; 186 } 187 188 *filedictp = prop_dictionary_internalize(base); 189 if (*filedictp == NULL) { 190 error = EINVAL; 191 } 192 kmem_free(base, plistsize); 193 base = NULL; 194 KASSERT(error == 0); 195 196 out: 197 VOP_UNLOCK(nd.ni_vp); 198 vn_close(nd.ni_vp, FREAD, kauth_cred_get()); 199 200 out1: 201 PNBUF_PUT(proppath); 202 return error; 203 } 204