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