xref: /freebsd/sys/kern/kern_environment.c (revision 91c878a6935c5c2e99866eb267e5bc3028bf6d2f)
1 /*-
2  * Copyright (c) 1998 Michael Smith
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * The unified bootloader passes us a pointer to a preserved copy of
29  * bootstrap/kernel environment variables.  We convert them to a
30  * dynamic array of strings later when the VM subsystem is up.
31  *
32  * We make these available through the kenv(2) syscall for userland
33  * and through getenv()/freeenv() setenv() unsetenv() testenv() for
34  * the kernel.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_mac.h"
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/queue.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/sysent.h>
52 #include <sys/sysproto.h>
53 #include <sys/libkern.h>
54 #include <sys/kenv.h>
55 
56 #include <security/mac/mac_framework.h>
57 
58 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
59 
60 #define KENV_SIZE	512	/* Maximum number of environment strings */
61 
62 /* pointer to the static environment */
63 char		*kern_envp;
64 static char	*kernenv_next(char *);
65 
66 /* dynamic environment variables */
67 char		**kenvp;
68 struct mtx	kenv_lock;
69 
70 /*
71  * No need to protect this with a mutex
72  * since SYSINITS are single threaded.
73  */
74 int	dynamic_kenv = 0;
75 
76 #define KENV_CHECK	if (!dynamic_kenv) \
77 			    panic("%s: called before SI_SUB_KMEM", __func__)
78 
79 int
80 kenv(td, uap)
81 	struct thread *td;
82 	struct kenv_args /* {
83 		int what;
84 		const char *name;
85 		char *value;
86 		int len;
87 	} */ *uap;
88 {
89 	char *name, *value, *buffer = NULL;
90 	size_t len, done, needed;
91 	int error, i;
92 
93 	KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
94 
95 	error = 0;
96 	if (uap->what == KENV_DUMP) {
97 #ifdef MAC
98 		error = mac_check_kenv_dump(td->td_ucred);
99 		if (error)
100 			return (error);
101 #endif
102 		done = needed = 0;
103 		if (uap->len > 0 && uap->value != NULL)
104 			buffer = malloc(uap->len, M_TEMP, M_WAITOK|M_ZERO);
105 		mtx_lock(&kenv_lock);
106 		for (i = 0; kenvp[i] != NULL; i++) {
107 			len = strlen(kenvp[i]) + 1;
108 			needed += len;
109 			len = min(len, uap->len - done);
110 			/*
111 			 * If called with a NULL or insufficiently large
112 			 * buffer, just keep computing the required size.
113 			 */
114 			if (uap->value != NULL && buffer != NULL && len > 0) {
115 				bcopy(kenvp[i], buffer + done, len);
116 				done += len;
117 			}
118 		}
119 		mtx_unlock(&kenv_lock);
120 		if (buffer != NULL) {
121 			error = copyout(buffer, uap->value, done);
122 			free(buffer, M_TEMP);
123 		}
124 		td->td_retval[0] = ((done == needed) ? 0 : needed);
125 		return (error);
126 	}
127 
128 	if ((uap->what == KENV_SET) ||
129 	    (uap->what == KENV_UNSET)) {
130 		error = suser(td);
131 		if (error)
132 			return (error);
133 	}
134 
135 	name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
136 
137 	error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
138 	if (error)
139 		goto done;
140 
141 	switch (uap->what) {
142 	case KENV_GET:
143 #ifdef MAC
144 		error = mac_check_kenv_get(td->td_ucred, name);
145 		if (error)
146 			goto done;
147 #endif
148 		value = getenv(name);
149 		if (value == NULL) {
150 			error = ENOENT;
151 			goto done;
152 		}
153 		len = strlen(value) + 1;
154 		if (len > uap->len)
155 			len = uap->len;
156 		error = copyout(value, uap->value, len);
157 		freeenv(value);
158 		if (error)
159 			goto done;
160 		td->td_retval[0] = len;
161 		break;
162 	case KENV_SET:
163 		len = uap->len;
164 		if (len < 1) {
165 			error = EINVAL;
166 			goto done;
167 		}
168 		if (len > KENV_MVALLEN)
169 			len = KENV_MVALLEN;
170 		value = malloc(len, M_TEMP, M_WAITOK);
171 		error = copyinstr(uap->value, value, len, NULL);
172 		if (error) {
173 			free(value, M_TEMP);
174 			goto done;
175 		}
176 #ifdef MAC
177 		error = mac_check_kenv_set(td->td_ucred, name, value);
178 		if (error == 0)
179 #endif
180 			setenv(name, value);
181 		free(value, M_TEMP);
182 		break;
183 	case KENV_UNSET:
184 #ifdef MAC
185 		error = mac_check_kenv_unset(td->td_ucred, name);
186 		if (error)
187 			goto done;
188 #endif
189 		error = unsetenv(name);
190 		if (error)
191 			error = ENOENT;
192 		break;
193 	default:
194 		error = EINVAL;
195 		break;
196 	}
197 done:
198 	free(name, M_TEMP);
199 	return (error);
200 }
201 
202 /*
203  * Setup the dynamic kernel environment.
204  */
205 static void
206 init_dynamic_kenv(void *data __unused)
207 {
208 	char *cp;
209 	int len, i;
210 
211 	kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
212 		M_WAITOK | M_ZERO);
213 	i = 0;
214 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
215 		len = strlen(cp) + 1;
216 		if (i < KENV_SIZE) {
217 			kenvp[i] = malloc(len, M_KENV, M_WAITOK);
218 			strcpy(kenvp[i++], cp);
219 		} else
220 			printf(
221 			    "WARNING: too many kenv strings, ignoring %s\n",
222 			    cp);
223 	}
224 	kenvp[i] = NULL;
225 
226 	mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
227 	dynamic_kenv = 1;
228 }
229 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
230 
231 void
232 freeenv(char *env)
233 {
234 
235 	if (dynamic_kenv)
236 		free(env, M_KENV);
237 }
238 
239 /*
240  * Internal functions for string lookup.
241  */
242 static char *
243 _getenv_dynamic(const char *name, int *idx)
244 {
245 	char *cp;
246 	int len, i;
247 
248 	mtx_assert(&kenv_lock, MA_OWNED);
249 	len = strlen(name);
250 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
251 		if ((strncmp(cp, name, len) == 0) &&
252 		    (cp[len] == '=')) {
253 			if (idx != NULL)
254 				*idx = i;
255 			return (cp + len + 1);
256 		}
257 	}
258 	return (NULL);
259 }
260 
261 static char *
262 _getenv_static(const char *name)
263 {
264 	char *cp, *ep;
265 	int len;
266 
267 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
268 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
269 			;
270 		if (*ep != '=')
271 			continue;
272 		len = ep - cp;
273 		ep++;
274 		if (!strncmp(name, cp, len) && name[len] == 0)
275 			return (ep);
276 	}
277 	return (NULL);
278 }
279 
280 /*
281  * Look up an environment variable by name.
282  * Return a pointer to the string if found.
283  * The pointer has to be freed with freeenv()
284  * after use.
285  */
286 char *
287 getenv(const char *name)
288 {
289 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
290 	char *ret, *cp;
291 	int len;
292 
293 	if (dynamic_kenv) {
294 		mtx_lock(&kenv_lock);
295 		cp = _getenv_dynamic(name, NULL);
296 		if (cp != NULL) {
297 			strcpy(buf, cp);
298 			mtx_unlock(&kenv_lock);
299 			len = strlen(buf) + 1;
300 			ret = malloc(len, M_KENV, M_WAITOK);
301 			strcpy(ret, buf);
302 		} else {
303 			mtx_unlock(&kenv_lock);
304 			ret = NULL;
305 		}
306 	} else
307 		ret = _getenv_static(name);
308 	return (ret);
309 }
310 
311 /*
312  * Test if an environment variable is defined.
313  */
314 int
315 testenv(const char *name)
316 {
317 	char *cp;
318 
319 	if (dynamic_kenv) {
320 		mtx_lock(&kenv_lock);
321 		cp = _getenv_dynamic(name, NULL);
322 		mtx_unlock(&kenv_lock);
323 	} else
324 		cp = _getenv_static(name);
325 	if (cp != NULL)
326 		return (1);
327 	return (0);
328 }
329 
330 /*
331  * Set an environment variable by name.
332  */
333 int
334 setenv(const char *name, const char *value)
335 {
336 	char *buf, *cp, *oldenv;
337 	int namelen, vallen, i;
338 
339 	KENV_CHECK;
340 
341 	namelen = strlen(name) + 1;
342 	if (namelen > KENV_MNAMELEN)
343 		return (-1);
344 	vallen = strlen(value) + 1;
345 	if (vallen > KENV_MVALLEN)
346 		return (-1);
347 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
348 	sprintf(buf, "%s=%s", name, value);
349 
350 	mtx_lock(&kenv_lock);
351 	cp = _getenv_dynamic(name, &i);
352 	if (cp != NULL) {
353 		oldenv = kenvp[i];
354 		kenvp[i] = buf;
355 		mtx_unlock(&kenv_lock);
356 		free(oldenv, M_KENV);
357 	} else {
358 		/* We add the option if it wasn't found */
359 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
360 			;
361 
362 		/* Bounds checking */
363 		if (i < 0 || i >= KENV_SIZE) {
364 			free(buf, M_KENV);
365 			mtx_unlock(&kenv_lock);
366 			return (-1);
367 		}
368 
369 		kenvp[i] = buf;
370 		kenvp[i + 1] = NULL;
371 		mtx_unlock(&kenv_lock);
372 	}
373 	return (0);
374 }
375 
376 /*
377  * Unset an environment variable string.
378  */
379 int
380 unsetenv(const char *name)
381 {
382 	char *cp, *oldenv;
383 	int i, j;
384 
385 	KENV_CHECK;
386 
387 	mtx_lock(&kenv_lock);
388 	cp = _getenv_dynamic(name, &i);
389 	if (cp != NULL) {
390 		oldenv = kenvp[i];
391 		for (j = i + 1; kenvp[j] != NULL; j++)
392 			kenvp[i++] = kenvp[j];
393 		kenvp[i] = NULL;
394 		mtx_unlock(&kenv_lock);
395 		free(oldenv, M_KENV);
396 		return (0);
397 	}
398 	mtx_unlock(&kenv_lock);
399 	return (-1);
400 }
401 
402 /*
403  * Return a string value from an environment variable.
404  */
405 int
406 getenv_string(const char *name, char *data, int size)
407 {
408 	char *tmp;
409 
410 	tmp = getenv(name);
411 	if (tmp != NULL) {
412 		strlcpy(data, tmp, size);
413 		freeenv(tmp);
414 		return (1);
415 	} else
416 		return (0);
417 }
418 
419 /*
420  * Return an integer value from an environment variable.
421  */
422 int
423 getenv_int(const char *name, int *data)
424 {
425 	quad_t tmp;
426 	int rval;
427 
428 	rval = getenv_quad(name, &tmp);
429 	if (rval)
430 		*data = (int) tmp;
431 	return (rval);
432 }
433 
434 /*
435  * Return a long value from an environment variable.
436  */
437 long
438 getenv_long(const char *name, long *data)
439 {
440 	quad_t tmp;
441 	long rval;
442 
443 	rval = getenv_quad(name, &tmp);
444 	if (rval)
445 		*data = (long) tmp;
446 	return (rval);
447 }
448 
449 /*
450  * Return an unsigned long value from an environment variable.
451  */
452 unsigned long
453 getenv_ulong(const char *name, unsigned long *data)
454 {
455 	quad_t tmp;
456 	long rval;
457 
458 	rval = getenv_quad(name, &tmp);
459 	if (rval)
460 		*data = (unsigned long) tmp;
461 	return (rval);
462 }
463 
464 /*
465  * Return a quad_t value from an environment variable.
466  */
467 int
468 getenv_quad(const char *name, quad_t *data)
469 {
470 	char	*value;
471 	char	*vtp;
472 	quad_t	iv;
473 
474 	value = getenv(name);
475 	if (value == NULL)
476 		return (0);
477 	iv = strtoq(value, &vtp, 0);
478 	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
479 		freeenv(value);
480 		return (0);
481 	}
482 	switch (vtp[0]) {
483 	case 't': case 'T':
484 		iv *= 1024;
485 	case 'g': case 'G':
486 		iv *= 1024;
487 	case 'm': case 'M':
488 		iv *= 1024;
489 	case 'k': case 'K':
490 		iv *= 1024;
491 	case '\0':
492 		break;
493 	default:
494 		freeenv(value);
495 		return (0);
496 	}
497 	*data = iv;
498 	freeenv(value);
499 	return (1);
500 }
501 
502 /*
503  * Find the next entry after the one which (cp) falls within, return a
504  * pointer to its start or NULL if there are no more.
505  */
506 static char *
507 kernenv_next(char *cp)
508 {
509 
510 	if (cp != NULL) {
511 		while (*cp != 0)
512 			cp++;
513 		cp++;
514 		if (*cp == 0)
515 			cp = NULL;
516 	}
517 	return (cp);
518 }
519 
520 void
521 tunable_int_init(void *data)
522 {
523 	struct tunable_int *d = (struct tunable_int *)data;
524 
525 	TUNABLE_INT_FETCH(d->path, d->var);
526 }
527 
528 void
529 tunable_long_init(void *data)
530 {
531 	struct tunable_long *d = (struct tunable_long *)data;
532 
533 	TUNABLE_LONG_FETCH(d->path, d->var);
534 }
535 
536 void
537 tunable_ulong_init(void *data)
538 {
539 	struct tunable_ulong *d = (struct tunable_ulong *)data;
540 
541 	TUNABLE_ULONG_FETCH(d->path, d->var);
542 }
543 
544 void
545 tunable_str_init(void *data)
546 {
547 	struct tunable_str *d = (struct tunable_str *)data;
548 
549 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
550 }
551