2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
40 #include <sys/eventhandler.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/libkern.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/queue.h>
52 #include <sys/sysent.h>
53 #include <sys/sysproto.h>
55 #include <security/mac/mac_framework.h>
57 static char *_getenv_dynamic_locked(const char *name, int *idx);
58 static char *_getenv_dynamic(const char *name, int *idx);
60 static char *kenv_acquire(const char *name);
61 static void kenv_release(const char *buf);
63 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
65 #define KENV_SIZE 512 /* Maximum number of environment strings */
67 static uma_zone_t kenv_zone;
68 static int kenv_mvallen = KENV_MVALLEN;
70 /* pointer to the config-generated static environment */
73 /* pointer to the md-static environment */
75 static int md_env_len;
76 static int md_env_pos;
78 static char *kernenv_next(char *);
80 /* dynamic environment variables */
85 * No need to protect this with a mutex since SYSINITS are single threaded.
89 #define KENV_CHECK if (!dynamic_kenv) \
90 panic("%s: called before SI_SUB_KMEM", __func__)
93 kenv_dump(struct thread *td, char **envp, int what, char *value, int len)
96 size_t done, needed, buflen;
103 MPASS(what == KENV_DUMP || what == KENV_DUMP_LOADER ||
104 what == KENV_DUMP_STATIC);
107 * For non-dynamic kernel environment, we pass in either md_envp or
108 * kern_envp and we must traverse with kernenv_next(). This shuffling
109 * of pointers simplifies the below loop by only differing in how envp
112 if (what != KENV_DUMP) {
118 if (buflen > KENV_SIZE * (KENV_MNAMELEN + kenv_mvallen + 2))
119 buflen = KENV_SIZE * (KENV_MNAMELEN +
121 if (len > 0 && value != NULL)
122 buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
124 /* Only take the lock for the dynamic kenv. */
125 if (what == KENV_DUMP)
126 mtx_lock(&kenv_lock);
127 while (*envp != NULL) {
128 len = strlen(*envp) + 1;
130 len = min(len, buflen - done);
132 * If called with a NULL or insufficiently large
133 * buffer, just keep computing the required size.
135 if (value != NULL && buffer != NULL && len > 0) {
136 bcopy(*envp, buffer + done, len);
140 /* Advance the pointer depending on the kenv format. */
141 if (what == KENV_DUMP)
144 senv = kernenv_next(senv);
146 if (what == KENV_DUMP)
147 mtx_unlock(&kenv_lock);
148 if (buffer != NULL) {
149 error = copyout(buffer, value, done);
150 free(buffer, M_TEMP);
152 td->td_retval[0] = ((done == needed) ? 0 : needed);
157 sys_kenv(struct thread *td, struct kenv_args *uap)
163 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = false"));
170 error = mac_kenv_check_dump(td->td_ucred);
174 return (kenv_dump(td, kenvp, uap->what, uap->value, uap->len));
175 case KENV_DUMP_LOADER:
176 case KENV_DUMP_STATIC:
178 error = mac_kenv_check_dump(td->td_ucred);
182 #ifdef PRESERVE_EARLY_KENV
183 return (kenv_dump(td,
184 uap->what == KENV_DUMP_LOADER ? (char **)md_envp :
185 (char **)kern_envp, uap->what, uap->value, uap->len));
190 error = priv_check(td, PRIV_KENV_SET);
196 error = priv_check(td, PRIV_KENV_UNSET);
202 name = malloc(KENV_MNAMELEN + 1, M_TEMP, M_WAITOK);
204 error = copyinstr(uap->name, name, KENV_MNAMELEN + 1, NULL);
211 error = mac_kenv_check_get(td->td_ucred, name);
215 value = kern_getenv(name);
220 len = strlen(value) + 1;
223 error = copyout(value, uap->value, len);
227 td->td_retval[0] = len;
235 if (len > kenv_mvallen + 1)
236 len = kenv_mvallen + 1;
237 value = malloc(len, M_TEMP, M_WAITOK);
238 error = copyinstr(uap->value, value, len, NULL);
244 error = mac_kenv_check_set(td->td_ucred, name, value);
247 kern_setenv(name, value);
252 error = mac_kenv_check_unset(td->td_ucred, name);
256 error = kern_unsetenv(name);
270 * Populate the initial kernel environment.
272 * This is called very early in MD startup, either to provide a copy of the
273 * environment obtained from a boot loader, or to provide an empty buffer into
274 * which MD code can store an initial environment using kern_setenv() calls.
276 * kern_envp is set to the static_env generated by config(8). This implements
277 * the env keyword described in config(5).
279 * If len is non-zero, the caller is providing an empty buffer. The caller will
280 * subsequently use kern_setenv() to add up to len bytes of initial environment
281 * before the dynamic environment is available.
283 * If len is zero, the caller is providing a pre-loaded buffer containing
284 * environment strings. Additional strings cannot be added until the dynamic
285 * environment is available. The memory pointed to must remain stable at least
286 * until sysinit runs init_dynamic_kenv() and preferably until after SI_SUB_KMEM
287 * is finished so that subr_hints routines may continue to use it until the
288 * environments have been fully merged at the end of the pass. If no initial
289 * environment is available from the boot loader, passing a NULL pointer allows
290 * the static_env to be installed if it is configured. In this case, any call
291 * to kern_setenv() prior to the setup of the dynamic environment will result in
295 init_static_kenv(char *buf, size_t len)
298 KASSERT(!dynamic_kenv, ("kenv: dynamic_kenv already initialized"));
300 * Suitably sized means it must be able to hold at least one empty
301 * variable, otherwise things go belly up if a kern_getenv call is
302 * made without a prior call to kern_setenv as we have a malformed
305 KASSERT(len == 0 || len >= 2,
306 ("kenv: static env must be initialized or suitably sized"));
307 KASSERT(len == 0 || (*buf == '\0' && *(buf + 1) == '\0'),
308 ("kenv: sized buffer must be initially empty"));
311 * We may be called twice, with the second call needed to relocate
312 * md_envp after enabling paging. md_envp is then garbage if it is
313 * not null and the relocation will move it. Discard it so as to
314 * not crash using its old value in our first call to kern_getenv().
316 * The second call gives the same environment as the first except
317 * in silly configurations where the static env disables itself.
319 * Other env calls don't handle possibly-garbage pointers, so must
320 * not be made between enabling paging and calling here.
327 * Give the static environment a chance to disable the loader(8)
328 * environment first. This is done with loader_env.disabled=1.
330 * static_env and static_hints may both be disabled, but in slightly
331 * different ways. For static_env, we just don't setup kern_envp and
332 * it's as if a static env wasn't even provided. For static_hints,
333 * we effectively zero out the buffer to stop the rest of the kernel
334 * from being able to use it.
336 * We're intentionally setting this up so that static_hints.disabled may
337 * be specified in either the MD env or the static env. This keeps us
338 * consistent in our new world view.
340 * As a warning, the static environment may not be disabled in any way
341 * if the static environment has disabled the loader environment.
343 kern_envp = static_env;
344 if (!getenv_is_true("loader_env.disabled")) {
349 if (getenv_is_true("static_env.disabled")) {
354 if (getenv_is_true("static_hints.disabled")) {
355 static_hints[0] = '\0';
356 static_hints[1] = '\0';
360 /* Maximum suffix number appended for duplicate environment variable names. */
361 #define MAXSUFFIX 9999
362 #define SUFFIXLEN strlen("_" __XSTRING(MAXSUFFIX))
365 getfreesuffix(char *cp, size_t *n)
367 size_t len = strlen(cp);
370 ncp = malloc(len + SUFFIXLEN + 1, M_KENV, M_WAITOK);
371 memcpy(ncp, cp, len);
372 for (*n = 1; *n <= MAXSUFFIX; (*n)++) {
373 sprintf(&ncp[len], "_%zu", *n);
374 if (!_getenv_dynamic_locked(ncp, NULL))
379 panic("Too many duplicate kernel environment values: %s", cp);
383 init_dynamic_kenv_from(char *init_env, int *curpos)
385 char *cp, *cpnext, *eqpos, *found;
389 if (init_env && *init_env != '\0') {
392 for (cp = init_env; cp != NULL; cp = cpnext) {
393 cpnext = kernenv_next(cp);
394 len = strlen(cp) + 1;
397 "WARNING: too many kenv strings, ignoring %s\n",
401 if (len > KENV_MNAMELEN + 1 + kenv_mvallen + 1) {
403 "WARNING: too long kenv string, ignoring %s\n",
407 eqpos = strchr(cp, '=');
410 "WARNING: malformed static env value, ignoring %s\n",
416 * Handle duplicates in the environment as we go; we
417 * add the duplicated assignments with _N suffixes.
418 * This ensures that (a) if a variable is set in the
419 * static environment and in the "loader" environment
420 * provided by MD code, the value from the loader will
421 * have the expected variable name and the value from
422 * the static environment will have the suffix; and (b)
423 * if the "loader" environment has the same variable
424 * set multiple times (as is possible with values being
425 * passed via the kernel "command line") the extra
426 * values are visible to code which knows where to look
429 found = _getenv_dynamic_locked(cp, NULL);
431 getfreesuffix(cp, &n);
432 kenvp[i] = malloc(len + SUFFIXLEN,
434 sprintf(kenvp[i++], "%s_%zu=%s", cp, n,
437 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
439 strcpy(kenvp[i++], cp);
442 #ifdef PRESERVE_EARLY_KENV
445 explicit_bzero(cp, len - 1);
453 * Setup the dynamic kernel environment.
456 init_dynamic_kenv(void *data __unused)
461 TUNABLE_INT_FETCH("kenv_mvallen", &kenv_mvallen);
462 size = KENV_MNAMELEN + 1 + kenv_mvallen + 1;
464 kenv_zone = uma_zcreate("kenv", size, NULL, NULL, NULL, NULL,
467 kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
471 init_dynamic_kenv_from(md_envp, &dynamic_envpos);
472 init_dynamic_kenv_from(kern_envp, &dynamic_envpos);
473 kenvp[dynamic_envpos] = NULL;
475 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
478 SYSINIT(kenv, SI_SUB_KMEM + 1, SI_ORDER_FIRST, init_dynamic_kenv, NULL);
484 if (dynamic_kenv && env != NULL) {
485 explicit_bzero(env, strlen(env));
486 uma_zfree(kenv_zone, env);
491 * Internal functions for string lookup.
494 _getenv_dynamic_locked(const char *name, int *idx)
500 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
501 if ((strncmp(cp, name, len) == 0) &&
505 return (cp + len + 1);
512 _getenv_dynamic(const char *name, int *idx)
515 mtx_assert(&kenv_lock, MA_OWNED);
516 return (_getenv_dynamic_locked(name, idx));
520 _getenv_static_from(char *chkenv, const char *name)
525 for (cp = chkenv; cp != NULL; cp = kernenv_next(cp)) {
526 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
532 if (!strncmp(name, cp, len) && name[len] == 0)
539 _getenv_static(const char *name)
543 val = _getenv_static_from(md_envp, name);
546 val = _getenv_static_from(kern_envp, name);
553 * Look up an environment variable by name.
554 * Return a pointer to the string if found.
555 * The pointer has to be freed with freeenv()
559 kern_getenv(const char *name)
565 len = KENV_MNAMELEN + 1 + kenv_mvallen + 1;
566 ret = uma_zalloc(kenv_zone, M_WAITOK | M_ZERO);
567 mtx_lock(&kenv_lock);
568 cp = _getenv_dynamic(name, NULL);
570 strlcpy(ret, cp, len);
571 mtx_unlock(&kenv_lock);
573 uma_zfree(kenv_zone, ret);
577 ret = _getenv_static(name);
583 * Test if an environment variable is defined.
586 testenv(const char *name)
590 cp = kenv_acquire(name);
599 * Set an environment variable in the MD-static environment. This cannot
600 * feasibly be done on config(8)-generated static environments as they don't
601 * generally include space for extra variables.
604 setenv_static(const char *name, const char *value)
608 if (md_env_pos >= md_env_len)
611 /* Check space for x=y and two nuls */
612 len = strlen(name) + strlen(value);
613 if (len + 3 < md_env_len - md_env_pos) {
614 len = sprintf(&md_envp[md_env_pos], "%s=%s", name, value);
616 md_envp[md_env_pos] = '\0';
624 * Set an environment variable by name.
627 kern_setenv(const char *name, const char *value)
629 char *buf, *cp, *oldenv;
630 int namelen, vallen, i;
632 if (!dynamic_kenv && md_env_len > 0)
633 return (setenv_static(name, value));
637 namelen = strlen(name) + 1;
638 if (namelen > KENV_MNAMELEN + 1)
640 vallen = strlen(value) + 1;
641 if (vallen > kenv_mvallen + 1)
643 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
644 sprintf(buf, "%s=%s", name, value);
646 mtx_lock(&kenv_lock);
647 cp = _getenv_dynamic(name, &i);
651 mtx_unlock(&kenv_lock);
652 free(oldenv, M_KENV);
654 /* We add the option if it wasn't found */
655 for (i = 0; (cp = kenvp[i]) != NULL; i++)
658 /* Bounds checking */
659 if (i < 0 || i >= KENV_SIZE) {
661 mtx_unlock(&kenv_lock);
667 mtx_unlock(&kenv_lock);
669 EVENTHANDLER_INVOKE(setenv, name);
674 * Unset an environment variable string.
677 kern_unsetenv(const char *name)
684 mtx_lock(&kenv_lock);
685 cp = _getenv_dynamic(name, &i);
688 for (j = i + 1; kenvp[j] != NULL; j++)
689 kenvp[i++] = kenvp[j];
691 mtx_unlock(&kenv_lock);
692 zfree(oldenv, M_KENV);
693 EVENTHANDLER_INVOKE(unsetenv, name);
696 mtx_unlock(&kenv_lock);
701 * Return the internal kenv buffer for the variable name, if it exists.
702 * If the dynamic kenv is initialized and the name is present, return
703 * with kenv_lock held.
706 kenv_acquire(const char *name)
711 mtx_lock(&kenv_lock);
712 value = _getenv_dynamic(name, NULL);
714 mtx_unlock(&kenv_lock);
717 return (_getenv_static(name));
721 * Undo a previous kenv_acquire() operation
724 kenv_release(const char *buf)
726 if ((buf != NULL) && dynamic_kenv)
727 mtx_unlock(&kenv_lock);
731 * Return a string value from an environment variable.
734 getenv_string(const char *name, char *data, int size)
738 cp = kenv_acquire(name);
741 strlcpy(data, cp, size);
749 * Return an array of integers at the given type size and signedness.
752 getenv_array(const char *name, void *pdata, int size, int *psize,
753 int type_size, bool allow_signed)
764 rc = 0; /* assume failure */
766 buf = kenv_acquire(name);
770 /* get maximum number of elements */
775 for (ptr = buf; *ptr != 0; ) {
776 value = strtoq(ptr, &end, 0);
778 /* check if signed numbers are allowed */
779 if (value < 0 && !allow_signed)
782 /* check for invalid value */
786 /* check for valid suffix */
815 /* garbage after numeric value */
819 /* skip till next value, if any */
820 while (*end == '\t' || *end == ',' || *end == ' ')
831 if ((value >> shift) != old)
834 /* check for buffer overflow */
838 /* store value according to type size */
842 if (value < SCHAR_MIN || value > SCHAR_MAX)
845 if (value < 0 || value > UCHAR_MAX)
848 ((uint8_t *)pdata)[n] = (uint8_t)value;
852 if (value < SHRT_MIN || value > SHRT_MAX)
855 if (value < 0 || value > USHRT_MAX)
858 ((uint16_t *)pdata)[n] = (uint16_t)value;
862 if (value < INT_MIN || value > INT_MAX)
865 if (value > UINT_MAX)
868 ((uint32_t *)pdata)[n] = (uint32_t)value;
871 ((uint64_t *)pdata)[n] = (uint64_t)value;
878 *psize = n * type_size;
881 rc = 1; /* success */
888 * Return an integer value from an environment variable.
891 getenv_int(const char *name, int *data)
896 rval = getenv_quad(name, &tmp);
903 * Return an unsigned integer value from an environment variable.
906 getenv_uint(const char *name, unsigned int *data)
911 rval = getenv_quad(name, &tmp);
913 *data = (unsigned int) tmp;
918 * Return an int64_t value from an environment variable.
921 getenv_int64(const char *name, int64_t *data)
926 rval = getenv_quad(name, &tmp);
928 *data = (int64_t) tmp;
933 * Return an uint64_t value from an environment variable.
936 getenv_uint64(const char *name, uint64_t *data)
941 rval = getenv_quad(name, &tmp);
943 *data = (uint64_t) tmp;
948 * Return a long value from an environment variable.
951 getenv_long(const char *name, long *data)
956 rval = getenv_quad(name, &tmp);
963 * Return an unsigned long value from an environment variable.
966 getenv_ulong(const char *name, unsigned long *data)
971 rval = getenv_quad(name, &tmp);
973 *data = (unsigned long) tmp;
978 * Return a quad_t value from an environment variable.
981 getenv_quad(const char *name, quad_t *data)
987 value = kenv_acquire(name);
991 iv = strtoq(value, &vtp, 0);
992 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
1017 kenv_release(value);
1022 * Return a boolean value from an environment variable. This can be in
1023 * numerical or string form, i.e. "1" or "true".
1026 getenv_bool(const char *name, bool *data)
1034 val = kern_getenv(name);
1038 if ((strcmp(val, "1") == 0) || (strcasecmp(val, "true") == 0)) {
1041 } else if ((strcmp(val, "0") == 0) || (strcasecmp(val, "false") == 0)) {
1045 /* Spit out a warning for malformed boolean variables. */
1046 printf("Environment variable %s has non-boolean value \"%s\"\n",
1055 * Wrapper around getenv_bool to easily check for true.
1058 getenv_is_true(const char *name)
1062 if (getenv_bool(name, &val) != 0)
1068 * Wrapper around getenv_bool to easily check for false.
1071 getenv_is_false(const char *name)
1075 if (getenv_bool(name, &val) != 0)
1081 * Find the next entry after the one which (cp) falls within, return a
1082 * pointer to its start or NULL if there are no more.
1085 kernenv_next(char *cp)
1099 tunable_int_init(void *data)
1101 struct tunable_int *d = (struct tunable_int *)data;
1103 TUNABLE_INT_FETCH(d->path, d->var);
1107 tunable_long_init(void *data)
1109 struct tunable_long *d = (struct tunable_long *)data;
1111 TUNABLE_LONG_FETCH(d->path, d->var);
1115 tunable_ulong_init(void *data)
1117 struct tunable_ulong *d = (struct tunable_ulong *)data;
1119 TUNABLE_ULONG_FETCH(d->path, d->var);
1123 tunable_int64_init(void *data)
1125 struct tunable_int64 *d = (struct tunable_int64 *)data;
1127 TUNABLE_INT64_FETCH(d->path, d->var);
1131 tunable_uint64_init(void *data)
1133 struct tunable_uint64 *d = (struct tunable_uint64 *)data;
1135 TUNABLE_UINT64_FETCH(d->path, d->var);
1139 tunable_quad_init(void *data)
1141 struct tunable_quad *d = (struct tunable_quad *)data;
1143 TUNABLE_QUAD_FETCH(d->path, d->var);
1147 tunable_bool_init(void *data)
1149 struct tunable_bool *d = (struct tunable_bool *)data;
1151 TUNABLE_BOOL_FETCH(d->path, d->var);
1155 tunable_str_init(void *data)
1157 struct tunable_str *d = (struct tunable_str *)data;
1159 TUNABLE_STR_FETCH(d->path, d->var, d->size);