kern_module_vfs.c revision 1.8 1 /* $NetBSD: kern_module_vfs.c,v 1.8 2010/11/18 09:50:47 jnemeth 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.8 2010/11/18 09:50:47 jnemeth 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 prop_bool_t noload;
69 prop_dictionary_t moduledict;
70
71 nochroot = false;
72 error = 0;
73 path = NULL;
74 moduledict = NULL;
75 if (filedictp)
76 *filedictp = NULL;
77 path = PNBUF_GET();
78
79 if (!autoload) {
80 nochroot = false;
81 snprintf(path, MAXPATHLEN, "%s", name);
82 error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
83 }
84 if (autoload || (error == ENOENT)) {
85 nochroot = true;
86 snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod",
87 module_base, name, name);
88 error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
89 }
90 if (error != 0) {
91 PNBUF_PUT(path);
92 if (autoload) {
93 module_print("Cannot load kernel object `%s'"
94 " error=%d", name, error);
95 } else {
96 module_error("Cannot load kernel object `%s'"
97 " error=%d", name, error);
98 }
99 return error;
100 }
101
102 /*
103 * Load and process <module>.prop if it exists.
104 */
105 if (((flags & MODCTL_NO_PROP) == 0 && filedictp) || autoload) {
106 error = module_load_plist_vfs(path, nochroot, &moduledict);
107 if (error != 0) {
108 module_print("plist load returned error %d for `%s'",
109 error, path);
110 if (error != ENOENT)
111 goto fail;
112 } else if (autoload) {
113 noload = prop_dictionary_get(moduledict, "noautoload");
114 if (noload != NULL && prop_bool_true(noload)) {
115 module_error("autoloading is disallowed for %s",
116 path);
117 error = EPERM;
118 goto fail;
119 }
120 }
121 if (error == 0) { /* can get here if error == ENOENT */
122 if ((flags & MODCTL_NO_PROP) == 0 && filedictp)
123 *filedictp = moduledict;
124 else
125 prop_object_release(moduledict);
126 }
127 }
128
129 PNBUF_PUT(path);
130 return 0;
131
132 fail:
133 kobj_unload(mod->mod_kobj);
134 PNBUF_PUT(path);
135 return error;
136 }
137
138 /*
139 * module_load_plist_vfs:
140 *
141 * Load a plist located in the file system into memory.
142 */
143 static int
144 module_load_plist_vfs(const char *modpath, const bool nochroot,
145 prop_dictionary_t *filedictp)
146 {
147 struct nameidata nd;
148 struct stat sb;
149 void *base;
150 char *proppath;
151 const size_t plistsize = 8192;
152 size_t resid;
153 int error, pathlen;
154
155 KASSERT(filedictp != NULL);
156 base = NULL;
157
158 proppath = PNBUF_GET();
159 strcpy(proppath, modpath);
160 pathlen = strlen(proppath);
161 if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) {
162 strcpy(&proppath[pathlen - 5], ".prop");
163 } else if (pathlen < MAXPATHLEN - 5) {
164 strcat(proppath, ".prop");
165 } else {
166 error = ENOENT;
167 goto out1;
168 }
169
170 NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0),
171 UIO_SYSSPACE, proppath);
172
173 error = vn_open(&nd, FREAD, 0);
174 if (error != 0) {
175 goto out1;
176 }
177
178 error = vn_stat(nd.ni_vp, &sb);
179 if (error != 0) {
180 goto out;
181 }
182 if (sb.st_size >= (plistsize - 1)) { /* leave space for term \0 */
183 error = EFBIG;
184 goto out;
185 }
186
187 base = kmem_alloc(plistsize, KM_SLEEP);
188 if (base == NULL) {
189 error = ENOMEM;
190 goto out;
191 }
192
193 error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0,
194 UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
195 *((uint8_t *)base + sb.st_size) = '\0';
196 if (error == 0 && resid != 0) {
197 error = EFBIG;
198 }
199 if (error != 0) {
200 kmem_free(base, plistsize);
201 base = NULL;
202 goto out;
203 }
204
205 *filedictp = prop_dictionary_internalize(base);
206 if (*filedictp == NULL) {
207 error = EINVAL;
208 }
209 kmem_free(base, plistsize);
210 base = NULL;
211 KASSERT(error == 0);
212
213 out:
214 VOP_UNLOCK(nd.ni_vp);
215 vn_close(nd.ni_vp, FREAD, kauth_cred_get());
216
217 out1:
218 PNBUF_PUT(proppath);
219 return error;
220 }
221