kern_module_vfs.c revision 1.9 1 /* $NetBSD: kern_module_vfs.c,v 1.9 2010/11/19 06:44:43 dholland 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.9 2010/11/19 06:44:43 dholland 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 pathbuf *pb;
148 struct nameidata nd;
149 struct stat sb;
150 void *base;
151 char *proppath;
152 const size_t plistsize = 8192;
153 size_t resid;
154 int error, pathlen;
155
156 KASSERT(filedictp != NULL);
157 base = NULL;
158
159 proppath = PNBUF_GET();
160 strcpy(proppath, modpath);
161 pathlen = strlen(proppath);
162 if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) {
163 strcpy(&proppath[pathlen - 5], ".prop");
164 } else if (pathlen < MAXPATHLEN - 5) {
165 strcat(proppath, ".prop");
166 } else {
167 error = ENOENT;
168 goto out1;
169 }
170
171 /* XXX this makes an unnecessary extra copy of the path */
172 pb = pathbuf_create(proppath);
173 if (pb == NULL) {
174 error = ENOMEM;
175 goto out1;
176 }
177
178 NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), pb);
179
180 error = vn_open(&nd, FREAD, 0);
181 if (error != 0) {
182 goto out2;
183 }
184
185 error = vn_stat(nd.ni_vp, &sb);
186 if (error != 0) {
187 goto out3;
188 }
189 if (sb.st_size >= (plistsize - 1)) { /* leave space for term \0 */
190 error = EFBIG;
191 goto out3;
192 }
193
194 base = kmem_alloc(plistsize, KM_SLEEP);
195 if (base == NULL) {
196 error = ENOMEM;
197 goto out3;
198 }
199
200 error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0,
201 UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
202 *((uint8_t *)base + sb.st_size) = '\0';
203 if (error == 0 && resid != 0) {
204 error = EFBIG;
205 }
206 if (error != 0) {
207 kmem_free(base, plistsize);
208 base = NULL;
209 goto out3;
210 }
211
212 *filedictp = prop_dictionary_internalize(base);
213 if (*filedictp == NULL) {
214 error = EINVAL;
215 }
216 kmem_free(base, plistsize);
217 base = NULL;
218 KASSERT(error == 0);
219
220 out3:
221 VOP_UNLOCK(nd.ni_vp);
222 vn_close(nd.ni_vp, FREAD, kauth_cred_get());
223
224 out2:
225 pathbuf_destroy(pb);
226
227 out1:
228 PNBUF_PUT(proppath);
229 return error;
230 }
231