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