xref: /freebsd/sys/kern/kern_environment.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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/priv.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysent.h>
53 #include <sys/sysproto.h>
54 #include <sys/libkern.h>
55 #include <sys/kenv.h>
56 
57 #include <security/mac/mac_framework.h>
58 
59 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
60 
61 #define KENV_SIZE	512	/* Maximum number of environment strings */
62 
63 /* pointer to the static environment */
64 char		*kern_envp;
65 static char	*kernenv_next(char *);
66 
67 /* dynamic environment variables */
68 char		**kenvp;
69 struct mtx	kenv_lock;
70 
71 /*
72  * No need to protect this with a mutex 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_kenv_check_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 	switch (uap->what) {
129 	case KENV_SET:
130 		error = priv_check(td, PRIV_KENV_SET);
131 		if (error)
132 			return (error);
133 		break;
134 
135 	case KENV_UNSET:
136 		error = priv_check(td, PRIV_KENV_UNSET);
137 		if (error)
138 			return (error);
139 		break;
140 	}
141 
142 	name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
143 
144 	error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
145 	if (error)
146 		goto done;
147 
148 	switch (uap->what) {
149 	case KENV_GET:
150 #ifdef MAC
151 		error = mac_kenv_check_get(td->td_ucred, name);
152 		if (error)
153 			goto done;
154 #endif
155 		value = getenv(name);
156 		if (value == NULL) {
157 			error = ENOENT;
158 			goto done;
159 		}
160 		len = strlen(value) + 1;
161 		if (len > uap->len)
162 			len = uap->len;
163 		error = copyout(value, uap->value, len);
164 		freeenv(value);
165 		if (error)
166 			goto done;
167 		td->td_retval[0] = len;
168 		break;
169 	case KENV_SET:
170 		len = uap->len;
171 		if (len < 1) {
172 			error = EINVAL;
173 			goto done;
174 		}
175 		if (len > KENV_MVALLEN)
176 			len = KENV_MVALLEN;
177 		value = malloc(len, M_TEMP, M_WAITOK);
178 		error = copyinstr(uap->value, value, len, NULL);
179 		if (error) {
180 			free(value, M_TEMP);
181 			goto done;
182 		}
183 #ifdef MAC
184 		error = mac_kenv_check_set(td->td_ucred, name, value);
185 		if (error == 0)
186 #endif
187 			setenv(name, value);
188 		free(value, M_TEMP);
189 		break;
190 	case KENV_UNSET:
191 #ifdef MAC
192 		error = mac_kenv_check_unset(td->td_ucred, name);
193 		if (error)
194 			goto done;
195 #endif
196 		error = unsetenv(name);
197 		if (error)
198 			error = ENOENT;
199 		break;
200 	default:
201 		error = EINVAL;
202 		break;
203 	}
204 done:
205 	free(name, M_TEMP);
206 	return (error);
207 }
208 
209 /*
210  * Setup the dynamic kernel environment.
211  */
212 static void
213 init_dynamic_kenv(void *data __unused)
214 {
215 	char *cp;
216 	int len, i;
217 
218 	kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
219 		M_WAITOK | M_ZERO);
220 	i = 0;
221 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
222 		len = strlen(cp) + 1;
223 		if (i < KENV_SIZE) {
224 			kenvp[i] = malloc(len, M_KENV, M_WAITOK);
225 			strcpy(kenvp[i++], cp);
226 		} else
227 			printf(
228 			    "WARNING: too many kenv strings, ignoring %s\n",
229 			    cp);
230 	}
231 	kenvp[i] = NULL;
232 
233 	mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
234 	dynamic_kenv = 1;
235 }
236 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
237 
238 void
239 freeenv(char *env)
240 {
241 
242 	if (dynamic_kenv)
243 		free(env, M_KENV);
244 }
245 
246 /*
247  * Internal functions for string lookup.
248  */
249 static char *
250 _getenv_dynamic(const char *name, int *idx)
251 {
252 	char *cp;
253 	int len, i;
254 
255 	mtx_assert(&kenv_lock, MA_OWNED);
256 	len = strlen(name);
257 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
258 		if ((strncmp(cp, name, len) == 0) &&
259 		    (cp[len] == '=')) {
260 			if (idx != NULL)
261 				*idx = i;
262 			return (cp + len + 1);
263 		}
264 	}
265 	return (NULL);
266 }
267 
268 static char *
269 _getenv_static(const char *name)
270 {
271 	char *cp, *ep;
272 	int len;
273 
274 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
275 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
276 			;
277 		if (*ep != '=')
278 			continue;
279 		len = ep - cp;
280 		ep++;
281 		if (!strncmp(name, cp, len) && name[len] == 0)
282 			return (ep);
283 	}
284 	return (NULL);
285 }
286 
287 /*
288  * Look up an environment variable by name.
289  * Return a pointer to the string if found.
290  * The pointer has to be freed with freeenv()
291  * after use.
292  */
293 char *
294 getenv(const char *name)
295 {
296 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
297 	char *ret, *cp;
298 	int len;
299 
300 	if (dynamic_kenv) {
301 		mtx_lock(&kenv_lock);
302 		cp = _getenv_dynamic(name, NULL);
303 		if (cp != NULL) {
304 			strcpy(buf, cp);
305 			mtx_unlock(&kenv_lock);
306 			len = strlen(buf) + 1;
307 			ret = malloc(len, M_KENV, M_WAITOK);
308 			strcpy(ret, buf);
309 		} else {
310 			mtx_unlock(&kenv_lock);
311 			ret = NULL;
312 		}
313 	} else
314 		ret = _getenv_static(name);
315 	return (ret);
316 }
317 
318 /*
319  * Test if an environment variable is defined.
320  */
321 int
322 testenv(const char *name)
323 {
324 	char *cp;
325 
326 	if (dynamic_kenv) {
327 		mtx_lock(&kenv_lock);
328 		cp = _getenv_dynamic(name, NULL);
329 		mtx_unlock(&kenv_lock);
330 	} else
331 		cp = _getenv_static(name);
332 	if (cp != NULL)
333 		return (1);
334 	return (0);
335 }
336 
337 /*
338  * Set an environment variable by name.
339  */
340 int
341 setenv(const char *name, const char *value)
342 {
343 	char *buf, *cp, *oldenv;
344 	int namelen, vallen, i;
345 
346 	KENV_CHECK;
347 
348 	namelen = strlen(name) + 1;
349 	if (namelen > KENV_MNAMELEN)
350 		return (-1);
351 	vallen = strlen(value) + 1;
352 	if (vallen > KENV_MVALLEN)
353 		return (-1);
354 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
355 	sprintf(buf, "%s=%s", name, value);
356 
357 	mtx_lock(&kenv_lock);
358 	cp = _getenv_dynamic(name, &i);
359 	if (cp != NULL) {
360 		oldenv = kenvp[i];
361 		kenvp[i] = buf;
362 		mtx_unlock(&kenv_lock);
363 		free(oldenv, M_KENV);
364 	} else {
365 		/* We add the option if it wasn't found */
366 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
367 			;
368 
369 		/* Bounds checking */
370 		if (i < 0 || i >= KENV_SIZE) {
371 			free(buf, M_KENV);
372 			mtx_unlock(&kenv_lock);
373 			return (-1);
374 		}
375 
376 		kenvp[i] = buf;
377 		kenvp[i + 1] = NULL;
378 		mtx_unlock(&kenv_lock);
379 	}
380 	return (0);
381 }
382 
383 /*
384  * Unset an environment variable string.
385  */
386 int
387 unsetenv(const char *name)
388 {
389 	char *cp, *oldenv;
390 	int i, j;
391 
392 	KENV_CHECK;
393 
394 	mtx_lock(&kenv_lock);
395 	cp = _getenv_dynamic(name, &i);
396 	if (cp != NULL) {
397 		oldenv = kenvp[i];
398 		for (j = i + 1; kenvp[j] != NULL; j++)
399 			kenvp[i++] = kenvp[j];
400 		kenvp[i] = NULL;
401 		mtx_unlock(&kenv_lock);
402 		free(oldenv, M_KENV);
403 		return (0);
404 	}
405 	mtx_unlock(&kenv_lock);
406 	return (-1);
407 }
408 
409 /*
410  * Return a string value from an environment variable.
411  */
412 int
413 getenv_string(const char *name, char *data, int size)
414 {
415 	char *tmp;
416 
417 	tmp = getenv(name);
418 	if (tmp != NULL) {
419 		strlcpy(data, tmp, size);
420 		freeenv(tmp);
421 		return (1);
422 	} else
423 		return (0);
424 }
425 
426 /*
427  * Return an integer value from an environment variable.
428  */
429 int
430 getenv_int(const char *name, int *data)
431 {
432 	quad_t tmp;
433 	int rval;
434 
435 	rval = getenv_quad(name, &tmp);
436 	if (rval)
437 		*data = (int) tmp;
438 	return (rval);
439 }
440 
441 /*
442  * Return an unsigned integer value from an environment variable.
443  */
444 int
445 getenv_uint(const char *name, unsigned int *data)
446 {
447 	quad_t tmp;
448 	int rval;
449 
450 	rval = getenv_quad(name, &tmp);
451 	if (rval)
452 		*data = (unsigned int) tmp;
453 	return (rval);
454 }
455 
456 /*
457  * Return a long value from an environment variable.
458  */
459 int
460 getenv_long(const char *name, long *data)
461 {
462 	quad_t tmp;
463 	int rval;
464 
465 	rval = getenv_quad(name, &tmp);
466 	if (rval)
467 		*data = (long) tmp;
468 	return (rval);
469 }
470 
471 /*
472  * Return an unsigned long value from an environment variable.
473  */
474 int
475 getenv_ulong(const char *name, unsigned long *data)
476 {
477 	quad_t tmp;
478 	int rval;
479 
480 	rval = getenv_quad(name, &tmp);
481 	if (rval)
482 		*data = (unsigned long) tmp;
483 	return (rval);
484 }
485 
486 /*
487  * Return a quad_t value from an environment variable.
488  */
489 int
490 getenv_quad(const char *name, quad_t *data)
491 {
492 	char	*value;
493 	char	*vtp;
494 	quad_t	iv;
495 
496 	value = getenv(name);
497 	if (value == NULL)
498 		return (0);
499 	iv = strtoq(value, &vtp, 0);
500 	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
501 		freeenv(value);
502 		return (0);
503 	}
504 	switch (vtp[0]) {
505 	case 't': case 'T':
506 		iv *= 1024;
507 	case 'g': case 'G':
508 		iv *= 1024;
509 	case 'm': case 'M':
510 		iv *= 1024;
511 	case 'k': case 'K':
512 		iv *= 1024;
513 	case '\0':
514 		break;
515 	default:
516 		freeenv(value);
517 		return (0);
518 	}
519 	*data = iv;
520 	freeenv(value);
521 	return (1);
522 }
523 
524 /*
525  * Find the next entry after the one which (cp) falls within, return a
526  * pointer to its start or NULL if there are no more.
527  */
528 static char *
529 kernenv_next(char *cp)
530 {
531 
532 	if (cp != NULL) {
533 		while (*cp != 0)
534 			cp++;
535 		cp++;
536 		if (*cp == 0)
537 			cp = NULL;
538 	}
539 	return (cp);
540 }
541 
542 void
543 tunable_int_init(void *data)
544 {
545 	struct tunable_int *d = (struct tunable_int *)data;
546 
547 	TUNABLE_INT_FETCH(d->path, d->var);
548 }
549 
550 void
551 tunable_long_init(void *data)
552 {
553 	struct tunable_long *d = (struct tunable_long *)data;
554 
555 	TUNABLE_LONG_FETCH(d->path, d->var);
556 }
557 
558 void
559 tunable_ulong_init(void *data)
560 {
561 	struct tunable_ulong *d = (struct tunable_ulong *)data;
562 
563 	TUNABLE_ULONG_FETCH(d->path, d->var);
564 }
565 
566 void
567 tunable_quad_init(void *data)
568 {
569 	struct tunable_quad *d = (struct tunable_quad *)data;
570 
571 	TUNABLE_QUAD_FETCH(d->path, d->var);
572 }
573 
574 void
575 tunable_str_init(void *data)
576 {
577 	struct tunable_str *d = (struct tunable_str *)data;
578 
579 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
580 }
581