2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 James Gritton.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <sys/linker.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
39 #include <arpa/inet.h>
40 #include <netinet/in.h>
51 #define SJPARAM "security.jail.param"
54 #define JPS_IN6_ADDR 2
56 #define ARRAY_SANITY 5
60 static int jailparam_import_enum(const char **values, int nvalues,
61 const char *valstr, size_t valsize, int *value);
62 static int jailparam_type(struct jailparam *jp);
63 static int kldload_param(const char *name);
64 static char *noname(const char *name);
65 static char *nononame(const char *name);
67 char jail_errmsg[JAIL_ERRMSGLEN];
69 static const char *bool_values[] = { "false", "true" };
70 static const char *jailsys_values[] = { "disable", "new", "inherit" };
74 * Import a null-terminated parameter list and set a jail with the flags
78 jail_setv(int flags, ...)
82 const char *name, *value;
85 /* Create the parameter list and import the parameters. */
88 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
89 (void)va_arg(tap, char *);
91 jp = alloca(njp * sizeof(struct jailparam));
92 for (njp = 0; (name = va_arg(ap, char *)) != NULL;) {
93 value = va_arg(ap, char *);
94 if (jailparam_init(jp + njp, name) < 0)
96 if (jailparam_import(jp + njp++, value) < 0)
100 jid = jailparam_set(jp, njp, flags);
101 jailparam_free(jp, njp);
105 jailparam_free(jp, njp);
111 * Read a null-terminated parameter list, get the referenced jail, and export
112 * the parameters to the list.
115 jail_getv(int flags, ...)
118 struct jailparam *jp, *jp_lastjid, *jp_jid, *jp_name, *jp_key;
119 char *valarg, *value;
120 const char *name, *key_value, *lastjid_value, *jid_value, *name_value;
123 /* Create the parameter list and find the key. */
126 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
127 (void)va_arg(tap, char *);
130 jp = alloca(njp * sizeof(struct jailparam));
132 jp_lastjid = jp_jid = jp_name = NULL;
133 lastjid_value = jid_value = name_value = NULL;
134 for (njp = 0; (name = va_arg(tap, char *)) != NULL; njp++) {
135 value = va_arg(tap, char *);
136 if (jailparam_init(jp + njp, name) < 0) {
140 if (!strcmp(jp[njp].jp_name, "lastjid")) {
141 jp_lastjid = jp + njp;
142 lastjid_value = value;
143 } else if (!strcmp(jp[njp].jp_name, "jid")) {
146 } if (!strcmp(jp[njp].jp_name, "name")) {
152 /* Import the key parameter. */
153 if (jp_lastjid != NULL) {
155 key_value = lastjid_value;
156 } else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) {
158 key_value = jid_value;
159 } else if (jp_name != NULL) {
161 key_value = name_value;
163 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
167 if (jailparam_import(jp_key, key_value) < 0)
169 /* Get the jail and export the parameters. */
170 jid = jailparam_get(jp, njp, flags);
173 for (i = 0; i < njp; i++) {
174 (void)va_arg(ap, char *);
175 valarg = va_arg(ap, char *);
176 if (jp + i != jp_key) {
177 /* It's up to the caller to ensure there's room. */
178 if ((jp[i].jp_ctltype & CTLTYPE) == CTLTYPE_STRING)
179 strcpy(valarg, jp[i].jp_value);
181 value = jailparam_export(jp + i);
184 strcpy(valarg, value);
189 jailparam_free(jp, njp);
194 jailparam_free(jp, njp);
200 * Return a list of all known parameters.
203 jailparam_all(struct jailparam **jpp)
205 struct jailparam *jp, *tjp;
206 size_t mlen1, mlen2, buflen;
208 int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2];
209 char buf[MAXPATHLEN];
213 jp = malloc(nlist * sizeof(*jp));
215 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
220 mlen1 = CTL_MAXNAME - 2;
221 if (sysctlnametomib(SJPARAM, mib1 + 2, &mlen1) < 0) {
222 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
223 "sysctlnametomib(" SJPARAM "): %s", strerror(errno));
227 /* Get the next parameter. */
228 mlen2 = sizeof(mib2);
229 if (sysctl(mib1, mlen1 + 2, mib2, &mlen2, NULL, 0) < 0) {
230 if (errno == ENOENT) {
231 /* No more entries. */
234 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
235 "sysctl(0.2): %s", strerror(errno));
238 if (mib2[0] != mib1[2] ||
239 mib2[1] != mib1[3] ||
242 /* Convert it to an ascii name. */
243 memcpy(mib1 + 2, mib2, mlen2);
244 mlen1 = mlen2 / sizeof(int);
246 buflen = sizeof(buf);
247 if (sysctl(mib1, mlen1 + 2, buf, &buflen, NULL, 0) < 0) {
248 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
249 "sysctl(0.1): %s", strerror(errno));
252 if (buf[buflen - 2] == '.')
253 buf[buflen - 2] = '\0';
254 /* Add the parameter to the list */
257 tjp = reallocarray(jp, nlist, sizeof(*jp));
262 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
266 jp = reallocarray(jp, njp, sizeof(*jp));
271 jailparam_free(jp, njp);
277 * Clear a jail parameter and copy in its name.
280 jailparam_init(struct jailparam *jp, const char *name)
283 memset(jp, 0, sizeof(*jp));
284 jp->jp_name = strdup(name);
285 if (jp->jp_name == NULL) {
286 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
289 if (jailparam_type(jp) < 0) {
290 jailparam_free(jp, 1);
299 * Put a name and value into a jail parameter element, converting the value
303 jailparam_import(struct jailparam *jp, const char *value)
305 char *p, *ep, *tvalue;
311 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
312 jp->jp_value = strdup(value);
313 if (jp->jp_value == NULL) {
314 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
320 if (jp->jp_elemlen) {
321 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
322 jp->jp_value = strdup("");
323 if (jp->jp_value == NULL) {
324 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
330 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
332 jp->jp_valuelen = jp->jp_elemlen * nval;
334 jp->jp_value = malloc(jp->jp_valuelen);
335 if (jp->jp_value == NULL) {
336 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
340 for (i = 0; i < nval; i++) {
341 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
342 switch (jp->jp_ctltype & CTLTYPE) {
344 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
345 if (!jailparam_import_enum(bool_values, 2,
346 avalue, fw, &((int *)jp->jp_value)[i])) {
347 snprintf(jail_errmsg,
348 JAIL_ERRMSGLEN, "%s: "
349 "unknown boolean value \"%.*s\"",
350 jp->jp_name, fw, avalue);
356 if (jp->jp_flags & JP_JAILSYS) {
358 * Allow setting a jailsys parameter to "new"
359 * in a booleanesque fashion.
361 if (value[0] == '\0')
362 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
363 else if (!jailparam_import_enum(jailsys_values,
364 sizeof(jailsys_values) /
365 sizeof(jailsys_values[0]), avalue, fw,
366 &((int *)jp->jp_value)[i])) {
367 snprintf(jail_errmsg,
368 JAIL_ERRMSGLEN, "%s: "
369 "unknown jailsys value \"%.*s\"",
370 jp->jp_name, fw, avalue);
376 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
378 if (ep != avalue + fw) {
379 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
380 "%s: non-integer value \"%.*s\"",
381 jp->jp_name, fw, avalue);
387 ((unsigned *)jp->jp_value)[i] =
388 strtoul(avalue, &ep, 10);
391 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
394 ((unsigned long *)jp->jp_value)[i] =
395 strtoul(avalue, &ep, 10);
398 ((int64_t *)jp->jp_value)[i] =
399 strtoimax(avalue, &ep, 10);
402 ((uint64_t *)jp->jp_value)[i] =
403 strtoumax(avalue, &ep, 10);
406 tvalue = alloca(fw + 1);
407 strlcpy(tvalue, avalue, fw + 1);
408 switch (jp->jp_structtype) {
410 if (inet_pton(AF_INET, tvalue,
411 &((struct in_addr *)jp->jp_value)[i]) != 1)
413 snprintf(jail_errmsg,
415 "%s: not an IPv4 address: %s",
416 jp->jp_name, tvalue);
422 if (inet_pton(AF_INET6, tvalue,
423 &((struct in6_addr *)jp->jp_value)[i]) != 1)
425 snprintf(jail_errmsg,
427 "%s: not an IPv6 address: %s",
428 jp->jp_name, tvalue);
439 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
440 "unknown type for %s", jp->jp_name);
455 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
456 size_t valsize, int *value)
461 for (i = 0; i < nvalues; i++)
462 if (valsize == strlen(values[i]) &&
463 !strncasecmp(valstr, values[i], valsize)) {
467 *value = strtol(valstr, &ep, 10);
468 return (ep == valstr + valsize);
472 * Put a name and value into a jail parameter element, copying the value
473 * but not altering it.
476 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
479 jp->jp_value = value;
480 jp->jp_valuelen = valuelen;
481 jp->jp_flags |= JP_RAWVALUE;
486 * Run the jail_set and jail_get system calls on a parameter list.
489 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
496 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
498 for (i = j = 0; j < njp; j++) {
499 jiov[i].iov_base = jp[j].jp_name;
500 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
502 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
504 * Set booleans without values. If one has a value of
505 * zero, change it to (or from) its "no" counterpart.
507 jiov[i].iov_base = NULL;
509 if (jp[j].jp_value != NULL &&
510 jp[j].jp_valuelen == sizeof(int) &&
511 !*(int *)jp[j].jp_value) {
513 nname = jp[j].jp_flags & JP_BOOL
514 ? noname(jp[j].jp_name)
515 : nononame(jp[j].jp_name);
521 jiov[i - 1].iov_base = nname;
522 jiov[i - 1].iov_len = strlen(nname) + 1;
527 * Try to fill in missing values with an empty string.
529 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
530 jailparam_import(jp + j, "") < 0) {
535 jiov[i].iov_base = jp[j].jp_value;
537 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
538 ? strlen(jp[j].jp_value) + 1
543 jiov[i].iov_base = __DECONST(char *, "errmsg");
544 jiov[i].iov_len = sizeof("errmsg");
546 jiov[i].iov_base = jail_errmsg;
547 jiov[i].iov_len = JAIL_ERRMSGLEN;
550 jid = jail_set(jiov, i, flags);
551 if (jid < 0 && !jail_errmsg[0])
552 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
556 for (j = 0; j < njp; j++)
557 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
558 jp[j].jp_value != NULL &&
559 jp[j].jp_valuelen == sizeof(int) &&
560 !*(int *)jp[j].jp_value)
561 free(jiov[j * 2].iov_base);
566 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
569 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
570 int i, ai, ki, jid, arrays, sanity;
574 * Get the types for all parameters.
575 * Find the key and any array parameters.
577 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
578 jp_lastjid = jp_jid = jp_name = NULL;
580 for (ai = j = 0; j < njp; j++) {
581 if (!strcmp(jp[j].jp_name, "lastjid"))
583 else if (!strcmp(jp[j].jp_name, "jid"))
585 else if (!strcmp(jp[j].jp_name, "name"))
587 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
589 jiov[ai].iov_base = jp[j].jp_name;
590 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
592 jiov[ai].iov_base = NULL;
593 jiov[ai].iov_len = 0;
597 jp_key = jp_lastjid ? jp_lastjid :
598 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
599 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
600 if (jp_key == NULL || jp_key->jp_value == NULL) {
601 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
606 jiov[ki].iov_base = jp_key->jp_name;
607 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
609 jiov[ki].iov_base = jp_key->jp_value;
610 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
611 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
613 jiov[ki].iov_base = __DECONST(char *, "errmsg");
614 jiov[ki].iov_len = sizeof("errmsg");
616 jiov[ki].iov_base = jail_errmsg;
617 jiov[ki].iov_len = JAIL_ERRMSGLEN;
620 if (arrays && jail_get(jiov, ki, flags) < 0) {
622 snprintf(jail_errmsg, sizeof(jail_errmsg),
623 "jail_get: %s", strerror(errno));
626 /* Allocate storage for all parameters. */
627 for (ai = j = 0, i = ki; j < njp; j++) {
628 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
630 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
631 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
632 jiov[ai].iov_len = jp[j].jp_valuelen;
634 jp[j].jp_valuelen = jiov[ai].iov_len;
635 if (jp[j].jp_value != NULL)
636 free(jp[j].jp_value);
637 jp[j].jp_value = malloc(jp[j].jp_valuelen);
638 if (jp[j].jp_value == NULL) {
639 strerror_r(errno, jail_errmsg,
644 jiov[ai].iov_base = jp[j].jp_value;
645 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
647 } else if (jp + j != jp_key) {
648 jiov[i].iov_base = jp[j].jp_name;
649 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
651 if (jp[j].jp_value == NULL &&
652 !(jp[j].jp_flags & JP_RAWVALUE)) {
653 jp[j].jp_value = malloc(jp[j].jp_valuelen);
654 if (jp[j].jp_value == NULL) {
655 strerror_r(errno, jail_errmsg,
660 jiov[i].iov_base = jp[j].jp_value;
661 jiov[i].iov_len = jp[j].jp_valuelen;
662 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
667 * Get the prison. If there are array elements, retry a few times
668 * in case their sizes changed from under us.
670 for (sanity = 0;; sanity++) {
671 jid = jail_get(jiov, i, flags);
672 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
673 errno != EINVAL || jail_errmsg[0])
675 for (ai = j = 0; j < njp; j++) {
676 if (jp[j].jp_elemlen &&
677 !(jp[j].jp_flags & JP_RAWVALUE)) {
679 jiov[ai].iov_base = NULL;
680 jiov[ai].iov_len = 0;
684 if (jail_get(jiov, ki, flags) < 0)
686 for (ai = j = 0; j < njp; j++) {
687 if (jp[j].jp_elemlen &&
688 !(jp[j].jp_flags & JP_RAWVALUE)) {
691 jp[j].jp_elemlen * ARRAY_SLOP;
692 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
693 jiov[ai].iov_len = jp[j].jp_valuelen;
695 jp[j].jp_valuelen = jiov[ai].iov_len;
696 if (jp[j].jp_value != NULL)
697 free(jp[j].jp_value);
699 malloc(jiov[ai].iov_len);
700 if (jp[j].jp_value == NULL) {
701 strerror_r(errno, jail_errmsg,
706 jiov[ai].iov_base = jp[j].jp_value;
707 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
712 if (jid < 0 && !jail_errmsg[0])
713 snprintf(jail_errmsg, sizeof(jail_errmsg),
714 "jail_get: %s", strerror(errno));
715 for (ai = j = 0, i = ki; j < njp; j++) {
716 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
718 jp[j].jp_valuelen = jiov[ai].iov_len;
720 } else if (jp + j != jp_key) {
722 jp[j].jp_valuelen = jiov[i].iov_len;
730 * Convert a jail parameter's value to external form.
733 jailparam_export(struct jailparam *jp)
736 char *value, *tvalue, **values;
739 char valbuf[INET6_ADDRSTRLEN];
741 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
742 value = strdup(jp->jp_value);
744 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
747 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
751 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
754 values = alloca(nval * sizeof(char *));
755 valuelens = alloca(nval * sizeof(size_t));
757 for (i = 0; i < nval; i++) {
758 switch (jp->jp_ctltype & CTLTYPE) {
760 ival = ((int *)jp->jp_value)[i];
761 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
762 (unsigned)ival < 2) {
763 strlcpy(valbuf, bool_values[ival],
767 if ((jp->jp_flags & JP_JAILSYS) &&
768 (unsigned)ival < sizeof(jailsys_values) /
769 sizeof(jailsys_values[0])) {
770 strlcpy(valbuf, jailsys_values[ival],
774 snprintf(valbuf, sizeof(valbuf), "%d", ival);
777 snprintf(valbuf, sizeof(valbuf), "%u",
778 ((unsigned *)jp->jp_value)[i]);
781 snprintf(valbuf, sizeof(valbuf), "%ld",
782 ((long *)jp->jp_value)[i]);
785 snprintf(valbuf, sizeof(valbuf), "%lu",
786 ((unsigned long *)jp->jp_value)[i]);
789 snprintf(valbuf, sizeof(valbuf), "%jd",
790 (intmax_t)((int64_t *)jp->jp_value)[i]);
793 snprintf(valbuf, sizeof(valbuf), "%ju",
794 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
797 switch (jp->jp_structtype) {
799 if (inet_ntop(AF_INET,
800 &((struct in_addr *)jp->jp_value)[i],
801 valbuf, sizeof(valbuf)) == NULL) {
802 strerror_r(errno, jail_errmsg,
808 if (inet_ntop(AF_INET6,
809 &((struct in6_addr *)jp->jp_value)[i],
810 valbuf, sizeof(valbuf)) == NULL) {
811 strerror_r(errno, jail_errmsg,
822 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
823 "unknown type for %s", jp->jp_name);
827 valuelens[i] = strlen(valbuf) + 1;
828 valuelen += valuelens[i];
829 values[i] = alloca(valuelens[i]);
830 strcpy(values[i], valbuf);
832 value = malloc(valuelen);
834 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
837 for (i = 0; i < nval; i++) {
838 strcpy(tvalue, values[i]);
840 tvalue += valuelens[i];
849 * Free the contents of a jail parameter list (but not the list itself).
852 jailparam_free(struct jailparam *jp, unsigned njp)
856 for (j = 0; j < njp; j++) {
858 if (!(jp[j].jp_flags & JP_RAWVALUE))
859 free(jp[j].jp_value);
864 * Find a parameter's type and size from its MIB.
867 jailparam_type(struct jailparam *jp)
869 char *p, *name, *nname;
870 size_t miblen, desclen;
876 int mib[CTL_MAXNAME];
878 /* The "lastjid" parameter isn't real. */
880 if (!strcmp(name, "lastjid")) {
881 jp->jp_valuelen = sizeof(int);
882 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
886 /* Find the sysctl that describes the parameter. */
889 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
890 miblen = sizeof(mib) - 2 * sizeof(int);
891 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
892 if (errno != ENOENT) {
893 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
894 "sysctl(0.3.%s): %s", name, strerror(errno));
897 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
898 desc.s, strlen(desc.s)) >= 0)
901 * The parameter probably doesn't exist. But it might be
902 * the "no" counterpart to a boolean.
904 nname = nononame(name);
907 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
908 "unknown parameter: %s", jp->jp_name);
912 name = alloca(strlen(nname) + 1);
915 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
916 miblen = sizeof(mib) - 2 * sizeof(int);
917 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
919 goto unknown_parameter;
920 jp->jp_flags |= JP_NOBOOL;
924 desclen = sizeof(desc);
925 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
927 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
928 "sysctl(0.4.%s): %s", name, strerror(errno));
931 jp->jp_ctltype = desc.i;
932 /* If this came from removing a "no", it better be a boolean. */
933 if (jp->jp_flags & JP_NOBOOL) {
934 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
935 jp->jp_valuelen = sizeof(int);
938 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
939 goto unknown_parameter;
941 /* See if this is an array type. */
942 p = strchr(desc.s, '\0');
944 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
950 /* Look for types we understand. */
951 switch (desc.i & CTLTYPE) {
953 if (desc.s[0] == 'B')
954 jp->jp_flags |= JP_BOOL;
955 else if (!strcmp(desc.s, "E,jailsys"))
956 jp->jp_flags |= JP_JAILSYS;
958 jp->jp_valuelen = sizeof(int);
962 jp->jp_valuelen = sizeof(long);
966 jp->jp_valuelen = sizeof(int64_t);
970 desclen = sizeof(desc.s);
971 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
973 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
974 "sysctl(" SJPARAM ".%s): %s", name,
978 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
981 if (!strcmp(desc.s, "S,in_addr")) {
982 jp->jp_structtype = JPS_IN_ADDR;
983 jp->jp_valuelen = sizeof(struct in_addr);
984 } else if (!strcmp(desc.s, "S,in6_addr")) {
985 jp->jp_structtype = JPS_IN6_ADDR;
986 jp->jp_valuelen = sizeof(struct in6_addr);
989 if (sysctl(mib + 2, miblen / sizeof(int),
990 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
991 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
992 "sysctl(" SJPARAM ".%s): %s", name,
1000 * A node might be described by an empty-named child,
1001 * which would be immediately before or after the node itself.
1004 miblen += sizeof(int);
1005 for (i = -1; i <= 1; i += 2) {
1006 mib[(miblen / sizeof(int)) + 1] =
1007 mib[(miblen / sizeof(int))] + i;
1008 desclen = sizeof(desc.s);
1009 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1010 &desclen, NULL, 0) < 0) {
1011 if (errno == ENOENT)
1013 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1014 "sysctl(0.1): %s", strerror(errno));
1017 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1018 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1019 memcmp(name, desc.s + sizeof(SJPARAM),
1020 desclen - sizeof(SJPARAM) - 2) == 0 &&
1021 desc.s[desclen - 2] == '.')
1024 goto unknown_parameter;
1026 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1027 "unknown type for %s", jp->jp_name);
1032 jp->jp_elemlen = jp->jp_valuelen;
1033 jp->jp_valuelen = 0;
1039 * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1042 kldload_param(const char *name)
1046 if (strcmp(name, "linux") == 0 || strncmp(name, "linux.", 6) == 0)
1047 kl = kldload("linux");
1048 else if (strcmp(name, "sysvmsg") == 0 || strcmp(name, "sysvsem") == 0 ||
1049 strcmp(name, "sysvshm") == 0)
1051 else if (strncmp(name, "allow.mount.", 12) == 0) {
1052 /* Load the matching filesystem */
1053 const char *modname;
1055 if (strcmp("fusefs", name + 12) == 0 ||
1056 strcmp("nofusefs", name + 12) == 0) {
1059 modname = name + 12;
1061 kl = kldload(modname);
1062 if (kl < 0 && errno == ENOENT &&
1063 strncmp(modname, "no", 2) == 0)
1064 kl = kldload(modname + 2);
1069 if (kl < 0 && errno == EEXIST) {
1071 * In the module is already loaded, then it must not contain
1080 * Change a boolean parameter name into its "no" counterpart or vice versa.
1083 noname(const char *name)
1087 nname = malloc(strlen(name) + 3);
1088 if (nname == NULL) {
1089 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1092 p = strrchr(name, '.');
1094 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1096 sprintf(nname, "no%s", name);
1101 nononame(const char *name)
1105 p = strrchr(name, '.');
1106 if (strncmp(p ? p + 1 : name, "no", 2)) {
1107 snprintf(jail_errmsg, sizeof(jail_errmsg),
1108 "mismatched boolean: %s", name);
1112 nname = malloc(strlen(name) - 1);
1113 if (nname == NULL) {
1114 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1118 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1120 strcpy(nname, name + 2);