2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Michael Smith
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * The unified bootloader passes us a pointer to a preserved copy of
31 * bootstrap/kernel environment variables. We convert them to a
32 * dynamic array of strings later when the VM subsystem is up.
34 * We make these available through the kenv(2) syscall for userland
35 * and through kern_getenv()/freeenv() kern_setenv() kern_unsetenv() testenv() for
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
43 #include <sys/param.h>
45 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.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>
56 #include <sys/limits.h>
58 #include <security/mac/mac_framework.h>
60 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
62 #define KENV_SIZE 512 /* Maximum number of environment strings */
64 /* pointer to the static environment */
68 static char *kernenv_next(char *);
70 /* dynamic environment variables */
75 * No need to protect this with a mutex since SYSINITS are single threaded.
79 #define KENV_CHECK if (!dynamic_kenv) \
80 panic("%s: called before SI_SUB_KMEM", __func__)
92 char *name, *value, *buffer = NULL;
93 size_t len, done, needed, buflen;
96 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
99 if (uap->what == KENV_DUMP) {
101 error = mac_kenv_check_dump(td->td_ucred);
107 if (buflen > KENV_SIZE * (KENV_MNAMELEN + KENV_MVALLEN + 2))
108 buflen = KENV_SIZE * (KENV_MNAMELEN +
110 if (uap->len > 0 && uap->value != NULL)
111 buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
112 mtx_lock(&kenv_lock);
113 for (i = 0; kenvp[i] != NULL; i++) {
114 len = strlen(kenvp[i]) + 1;
116 len = min(len, buflen - done);
118 * If called with a NULL or insufficiently large
119 * buffer, just keep computing the required size.
121 if (uap->value != NULL && buffer != NULL && len > 0) {
122 bcopy(kenvp[i], buffer + done, len);
126 mtx_unlock(&kenv_lock);
127 if (buffer != NULL) {
128 error = copyout(buffer, uap->value, done);
129 free(buffer, M_TEMP);
131 td->td_retval[0] = ((done == needed) ? 0 : needed);
137 error = priv_check(td, PRIV_KENV_SET);
143 error = priv_check(td, PRIV_KENV_UNSET);
149 name = malloc(KENV_MNAMELEN + 1, M_TEMP, M_WAITOK);
151 error = copyinstr(uap->name, name, KENV_MNAMELEN + 1, NULL);
158 error = mac_kenv_check_get(td->td_ucred, name);
162 value = kern_getenv(name);
167 len = strlen(value) + 1;
170 error = copyout(value, uap->value, len);
174 td->td_retval[0] = len;
182 if (len > KENV_MVALLEN + 1)
183 len = KENV_MVALLEN + 1;
184 value = malloc(len, M_TEMP, M_WAITOK);
185 error = copyinstr(uap->value, value, len, NULL);
191 error = mac_kenv_check_set(td->td_ucred, name, value);
194 kern_setenv(name, value);
199 error = mac_kenv_check_unset(td->td_ucred, name);
203 error = kern_unsetenv(name);
217 * Populate the initial kernel environment.
219 * This is called very early in MD startup, either to provide a copy of the
220 * environment obtained from a boot loader, or to provide an empty buffer into
221 * which MD code can store an initial environment using kern_setenv() calls.
223 * When a copy of an initial environment is passed in, we start by scanning that
224 * env for overrides to the compiled-in envmode and hintmode variables.
226 * If the global envmode is 1, the environment is initialized from the global
227 * static_env[], regardless of the arguments passed. This implements the env
228 * keyword described in config(5). In this case env_pos is set to env_len,
229 * causing kern_setenv() to return -1 (if len > 0) or panic (if len == 0) until
230 * the dynamic environment is available. The envmode and static_env variables
231 * are defined in env.c which is generated by config(8).
233 * If len is non-zero, the caller is providing an empty buffer. The caller will
234 * subsequently use kern_setenv() to add up to len bytes of initial environment
235 * before the dynamic environment is available.
237 * If len is zero, the caller is providing a pre-loaded buffer containing
238 * environment strings. Additional strings cannot be added until the dynamic
239 * environment is available. The memory pointed to must remain stable at least
240 * until sysinit runs init_dynamic_kenv(). If no initial environment is
241 * available from the boot loader, passing a NULL pointer allows the static_env
242 * to be installed if it is configured.
245 init_static_kenv(char *buf, size_t len)
249 for (cp = buf; cp != NULL && cp[0] != '\0'; cp += strlen(cp) + 1) {
250 if (strcmp(cp, "static_env.disabled=1") == 0)
252 if (strcmp(cp, "static_hints.disabled=1") == 0)
257 kern_envp = static_env;
268 * Setup the dynamic kernel environment.
271 init_dynamic_kenv(void *data __unused)
277 kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
280 if (kern_envp && *kern_envp != '\0') {
281 for (cp = kern_envp; cp != NULL; cp = cpnext) {
282 cpnext = kernenv_next(cp);
283 len = strlen(cp) + 1;
284 if (len > KENV_MNAMELEN + 1 + KENV_MVALLEN + 1) {
286 "WARNING: too long kenv string, ignoring %s\n",
291 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
292 strcpy(kenvp[i++], cp);
293 explicit_bzero(cp, strlen(cp));
296 "WARNING: too many kenv strings, ignoring %s\n",
302 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
305 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
311 if (dynamic_kenv && env != NULL) {
312 explicit_bzero(env, strlen(env));
318 * Internal functions for string lookup.
321 _getenv_dynamic(const char *name, int *idx)
326 mtx_assert(&kenv_lock, MA_OWNED);
328 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
329 if ((strncmp(cp, name, len) == 0) &&
333 return (cp + len + 1);
340 _getenv_static(const char *name)
345 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
346 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
352 if (!strncmp(name, cp, len) && name[len] == 0)
359 * Look up an environment variable by name.
360 * Return a pointer to the string if found.
361 * The pointer has to be freed with freeenv()
365 kern_getenv(const char *name)
367 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
371 if (getenv_string(name, buf, sizeof(buf))) {
372 ret = strdup(buf, M_KENV);
375 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
379 ret = _getenv_static(name);
384 * Test if an environment variable is defined.
387 testenv(const char *name)
392 mtx_lock(&kenv_lock);
393 cp = _getenv_dynamic(name, NULL);
394 mtx_unlock(&kenv_lock);
396 cp = _getenv_static(name);
403 setenv_static(const char *name, const char *value)
407 if (env_pos >= env_len)
410 /* Check space for x=y and two nuls */
411 len = strlen(name) + strlen(value);
412 if (len + 3 < env_len - env_pos) {
413 len = sprintf(&kern_envp[env_pos], "%s=%s", name, value);
415 kern_envp[env_pos] = '\0';
423 * Set an environment variable by name.
426 kern_setenv(const char *name, const char *value)
428 char *buf, *cp, *oldenv;
429 int namelen, vallen, i;
431 if (dynamic_kenv == 0 && env_len > 0)
432 return (setenv_static(name, value));
436 namelen = strlen(name) + 1;
437 if (namelen > KENV_MNAMELEN + 1)
439 vallen = strlen(value) + 1;
440 if (vallen > KENV_MVALLEN + 1)
442 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
443 sprintf(buf, "%s=%s", name, value);
445 mtx_lock(&kenv_lock);
446 cp = _getenv_dynamic(name, &i);
450 mtx_unlock(&kenv_lock);
451 free(oldenv, M_KENV);
453 /* We add the option if it wasn't found */
454 for (i = 0; (cp = kenvp[i]) != NULL; i++)
457 /* Bounds checking */
458 if (i < 0 || i >= KENV_SIZE) {
460 mtx_unlock(&kenv_lock);
466 mtx_unlock(&kenv_lock);
472 * Unset an environment variable string.
475 kern_unsetenv(const char *name)
482 mtx_lock(&kenv_lock);
483 cp = _getenv_dynamic(name, &i);
486 for (j = i + 1; kenvp[j] != NULL; j++)
487 kenvp[i++] = kenvp[j];
489 mtx_unlock(&kenv_lock);
490 explicit_bzero(oldenv, strlen(oldenv));
491 free(oldenv, M_KENV);
494 mtx_unlock(&kenv_lock);
499 * Return a string value from an environment variable.
502 getenv_string(const char *name, char *data, int size)
507 mtx_lock(&kenv_lock);
508 cp = _getenv_dynamic(name, NULL);
510 strlcpy(data, cp, size);
511 mtx_unlock(&kenv_lock);
513 cp = _getenv_static(name);
515 strlcpy(data, cp, size);
521 * Return an array of integers at the given type size and signedness.
524 getenv_array(const char *name, void *pdata, int size, int *psize,
525 int type_size, bool allow_signed)
527 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
535 if (getenv_string(name, buf, sizeof(buf)) == 0)
538 /* get maximum number of elements */
543 for (ptr = buf; *ptr != 0; ) {
545 value = strtoq(ptr, &end, 0);
547 /* check if signed numbers are allowed */
548 if (value < 0 && !allow_signed)
551 /* check for invalid value */
555 /* check for valid suffix */
584 /* garbage after numeric value */
588 /* skip till next value, if any */
589 while (*end == '\t' || *end == ',' || *end == ' ')
600 if ((value >> shift) != old)
603 /* check for buffer overflow */
607 /* store value according to type size */
611 if (value < SCHAR_MIN || value > SCHAR_MAX)
614 if (value < 0 || value > UCHAR_MAX)
617 ((uint8_t *)pdata)[n] = (uint8_t)value;
621 if (value < SHRT_MIN || value > SHRT_MAX)
624 if (value < 0 || value > USHRT_MAX)
627 ((uint16_t *)pdata)[n] = (uint16_t)value;
631 if (value < INT_MIN || value > INT_MAX)
634 if (value > UINT_MAX)
637 ((uint32_t *)pdata)[n] = (uint32_t)value;
640 ((uint64_t *)pdata)[n] = (uint64_t)value;
647 *psize = n * type_size;
650 return (1); /* success */
652 return (0); /* failure */
656 * Return an integer value from an environment variable.
659 getenv_int(const char *name, int *data)
664 rval = getenv_quad(name, &tmp);
671 * Return an unsigned integer value from an environment variable.
674 getenv_uint(const char *name, unsigned int *data)
679 rval = getenv_quad(name, &tmp);
681 *data = (unsigned int) tmp;
686 * Return an int64_t value from an environment variable.
689 getenv_int64(const char *name, int64_t *data)
694 rval = getenv_quad(name, &tmp);
696 *data = (int64_t) tmp;
701 * Return an uint64_t value from an environment variable.
704 getenv_uint64(const char *name, uint64_t *data)
709 rval = getenv_quad(name, &tmp);
711 *data = (uint64_t) tmp;
716 * Return a long value from an environment variable.
719 getenv_long(const char *name, long *data)
724 rval = getenv_quad(name, &tmp);
731 * Return an unsigned long value from an environment variable.
734 getenv_ulong(const char *name, unsigned long *data)
739 rval = getenv_quad(name, &tmp);
741 *data = (unsigned long) tmp;
746 * Return a quad_t value from an environment variable.
749 getenv_quad(const char *name, quad_t *data)
751 char value[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
755 if (!getenv_string(name, value, sizeof(value)))
757 iv = strtoq(value, &vtp, 0);
758 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0'))
779 * Find the next entry after the one which (cp) falls within, return a
780 * pointer to its start or NULL if there are no more.
783 kernenv_next(char *cp)
797 tunable_int_init(void *data)
799 struct tunable_int *d = (struct tunable_int *)data;
801 TUNABLE_INT_FETCH(d->path, d->var);
805 tunable_long_init(void *data)
807 struct tunable_long *d = (struct tunable_long *)data;
809 TUNABLE_LONG_FETCH(d->path, d->var);
813 tunable_ulong_init(void *data)
815 struct tunable_ulong *d = (struct tunable_ulong *)data;
817 TUNABLE_ULONG_FETCH(d->path, d->var);
821 tunable_int64_init(void *data)
823 struct tunable_int64 *d = (struct tunable_int64 *)data;
825 TUNABLE_INT64_FETCH(d->path, d->var);
829 tunable_uint64_init(void *data)
831 struct tunable_uint64 *d = (struct tunable_uint64 *)data;
833 TUNABLE_UINT64_FETCH(d->path, d->var);
837 tunable_quad_init(void *data)
839 struct tunable_quad *d = (struct tunable_quad *)data;
841 TUNABLE_QUAD_FETCH(d->path, d->var);
845 tunable_str_init(void *data)
847 struct tunable_str *d = (struct tunable_str *)data;
849 TUNABLE_STR_FETCH(d->path, d->var, d->size);