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