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