summaryrefslogtreecommitdiff
path: root/sys/kern/sys_module.c
blob: 46f4ca059ca0e9e177be32e1fa7725a980759e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*	$NetBSD: sys_module.c,v 1.29 2019/03/01 11:06:57 pgoyette Exp $	*/

/*-
 * Copyright (c) 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * System calls relating to loadable modules.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.29 2019/03/01 11:06:57 pgoyette Exp $");

#ifdef _KERNEL_OPT
#include "opt_modular.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/namei.h>
#include <sys/kauth.h>
#include <sys/kmem.h>
#include <sys/kobj.h>
#include <sys/module.h>
#include <sys/syscall.h>
#include <sys/syscallargs.h>
#include <sys/compat_stub.h>

/*
 * Arbitrary limit to avoid DoS for excessive memory allocation.
 */
#define MAXPROPSLEN	4096

int
handle_modctl_load(const char *ml_filename, int ml_flags, const char *ml_props,
    size_t ml_propslen)
{
	char *path;
	char *props;
	int error;
	prop_dictionary_t dict;
	size_t propslen = 0;

	if ((ml_props != NULL && ml_propslen == 0) ||
	    (ml_props == NULL && ml_propslen > 0)) {
		return EINVAL;
	}

	path = PNBUF_GET();
	error = copyinstr(ml_filename, path, MAXPATHLEN, NULL);
	if (error != 0)
		goto out1;

	if (ml_props != NULL) {
		if (ml_propslen > MAXPROPSLEN) {
			error = ENOMEM;
			goto out1;
		}
		propslen = ml_propslen + 1;

		props = kmem_alloc(propslen, KM_SLEEP);
		error = copyinstr(ml_props, props, propslen, NULL);
		if (error != 0)
			goto out2;

		dict = prop_dictionary_internalize(props);
		if (dict == NULL) {
			error = EINVAL;
			goto out2;
		}
	} else {
		dict = NULL;
		props = NULL;
	}

	error = module_load(path, ml_flags, dict, MODULE_CLASS_ANY);

	if (dict != NULL) {
		prop_object_release(dict);
	}

out2:
	if (props != NULL) {
		kmem_free(props, propslen);
	}
out1:
	PNBUF_PUT(path);
	return error;
}

static int
handle_modctl_stat(struct iovec *iov, void *arg)
{
	int ms_cnt;
	modstat_t *ms, *mso;
	size_t ms_len;
	char *req, *reqo;
	size_t req_len;
	char *out_p;
	size_t out_s;

	modinfo_t *mi;
	module_t *mod;
	vaddr_t addr;
	size_t size;
	size_t used;
	int off;
	int error;
	bool stataddr;

	/* If not privileged, don't expose kernel addresses. */
	error = kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANSEE,
	    curproc, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_KPTR), NULL, NULL);
	stataddr = (error == 0);

	kernconfig_lock();
	ms_cnt = 0;
	req_len = 1;

	/*
	 * Count up the number of modstat_t needed, and total size of
	 * require_module lists on both active and built-in lists
	 */
	TAILQ_FOREACH(mod, &module_list, mod_chain) {
		ms_cnt++;
		mi = mod->mod_info;
		if (mi->mi_required != NULL) {
			req_len += strlen(mi->mi_required) + 1;
		}
	}
	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
		ms_cnt++;
		mi = mod->mod_info;
		if (mi->mi_required != NULL) {
			req_len += strlen(mi->mi_required) + 1;
		}
	}

	/* Allocate internal buffers to hold all the output data */
	ms_len = ms_cnt * sizeof(modstat_t);
	ms = kmem_zalloc(ms_len, KM_SLEEP);
	req = kmem_zalloc(req_len, KM_SLEEP);

	mso = ms;
	reqo = req++;
	off = 1;

	/*
	 * Load data into our internal buffers for both active and
	 * build-in module lists
	 */
	TAILQ_FOREACH(mod, &module_list, mod_chain) {
		mi = mod->mod_info;
		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
		if (mi->mi_required != NULL) {
			ms->ms_reqoffset = off;
			used = strlcpy(req,  mi->mi_required, req_len - off);
			KASSERTMSG(used < req_len - off, "reqlist grew!");
			off += used + 1;
			req += used + 1;
		} else
			ms->ms_reqoffset = 0;
		if (mod->mod_kobj != NULL && stataddr) {
			kobj_stat(mod->mod_kobj, &addr, &size);
			ms->ms_addr = addr;
			ms->ms_size = size;
		}
		ms->ms_class = mi->mi_class;
		ms->ms_refcnt = mod->mod_refcnt;
		ms->ms_source = mod->mod_source;
		ms->ms_flags = mod->mod_flags;
		ms++;
	}
	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
		mi = mod->mod_info;
		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
		if (mi->mi_required != NULL) {
			ms->ms_reqoffset = off;
			used = strlcpy(req,  mi->mi_required, req_len - off);
			KASSERTMSG(used < req_len - off, "reqlist grew!");
			off += used + 1;
			req += used + 1;
		} else
			ms->ms_reqoffset = 0;
		if (mod->mod_kobj != NULL && stataddr) {
			kobj_stat(mod->mod_kobj, &addr, &size);
			ms->ms_addr = addr;
			ms->ms_size = size;
		}
		ms->ms_class = mi->mi_class;
		ms->ms_refcnt = -1;
		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
		ms->ms_source = mod->mod_source;
		ms++;
	}
	kernconfig_unlock();

	/*
	 * Now copyout our internal buffers back to userland
	 */
	out_p = iov->iov_base;
	out_s = iov->iov_len;
	size = sizeof(ms_cnt);

	/* Copy out the count of modstat_t */
	if (out_s) {
		size = uimin(sizeof(ms_cnt), out_s);
		error = copyout(&ms_cnt, out_p, size);
		out_p += size;
		out_s -= size;
	}
	/* Copy out the modstat_t array */
	if (out_s && error == 0) {
		size = uimin(ms_len, out_s);
		error = copyout(mso, out_p, size);
		out_p += size;
		out_s -= size;
	}
	/* Copy out the "required" strings */
	if (out_s && error == 0) {
		size = uimin(req_len, out_s);
		error = copyout(reqo, out_p, size);
		out_p += size;
		out_s -= size;
	}
	kmem_free(mso, ms_len);
	kmem_free(reqo, req_len);

	/* Finally, update the userland copy of the iovec's length */
	if (error == 0) {
		iov->iov_len = ms_len + req_len + sizeof(ms_cnt);
		error = copyout(iov, arg, sizeof(*iov));
	}

	return error;
}

int
sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
	   register_t *retval)
{
	/* {
		syscallarg(int)		cmd;
		syscallarg(void *)	arg;
	} */
	char buf[MAXMODNAME];
	struct iovec iov;
	modctl_load_t ml;
	int error;
	void *arg;
#ifdef MODULAR
	uintptr_t loadtype;
#endif

	arg = SCARG(uap, arg);

	switch (SCARG(uap, cmd)) {
	case MODCTL_LOAD:
		error = copyin(arg, &ml, sizeof(ml));
		if (error != 0)
			break;
		error = handle_modctl_load(ml.ml_filename, ml.ml_flags,
		    ml.ml_props, ml.ml_propslen);
		break;

	case MODCTL_UNLOAD:
		error = copyinstr(arg, buf, sizeof(buf), NULL);
		if (error == 0) {
			error = module_unload(buf);
		}
		break;

	case MODCTL_STAT:
		error = copyin(arg, &iov, sizeof(iov));
		if (error != 0) {
			break;
		}
		error = handle_modctl_stat(&iov, arg);
		break;

	case MODCTL_EXISTS:
#ifndef MODULAR
		error = ENOSYS;
#else
		loadtype = (uintptr_t)arg;
		switch (loadtype) {	/* 0 = modload, 1 = autoload */
		case 0:			/* FALLTHROUGH */
		case 1:
			error = kauth_authorize_system(kauth_cred_get(),
			     KAUTH_SYSTEM_MODULE, 0,
			     (void *)(uintptr_t)MODCTL_LOAD,
			     (void *)loadtype, NULL);
			break;
		default:
			error = EINVAL;
			break;
		}
#endif
		break;

	default:
		(void)module_autoload("compat_80", MODULE_CLASS_EXEC);
		MODULE_HOOK_CALL(compat_modstat_80_hook,
		    (SCARG(uap, cmd), &iov, arg), enosys(), error);
		if (error == ENOSYS)
			error = EINVAL;
		break;
	}

	return error;
}