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>
34 #include <sys/linker.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
50 #define SJPARAM "security.jail.param"
53 #define JPS_IN6_ADDR 2
55 #define ARRAY_SANITY 5
59 static int jailparam_import_enum(const char **values, int nvalues,
60 const char *valstr, size_t valsize, int *value);
61 static int jailparam_type(struct jailparam *jp);
62 static int kldload_param(const char *name);
63 static char *noname(const char *name);
64 static char *nononame(const char *name);
66 char jail_errmsg[JAIL_ERRMSGLEN];
68 static const char *bool_values[] = { "false", "true" };
69 static const char *jailsys_values[] = { "disable", "new", "inherit" };
73 * Import a null-terminated parameter list and set a jail with the flags
77 jail_setv(int flags, ...)
81 const char *name, *value;
84 /* Create the parameter list and import the parameters. */
87 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
88 (void)va_arg(tap, char *);
90 jp = alloca(njp * sizeof(struct jailparam));
91 for (njp = 0; (name = va_arg(ap, char *)) != NULL;) {
92 value = va_arg(ap, char *);
93 if (jailparam_init(jp + njp, name) < 0)
95 if (jailparam_import(jp + njp++, value) < 0)
99 jid = jailparam_set(jp, njp, flags);
100 jailparam_free(jp, njp);
104 jailparam_free(jp, njp);
110 * Read a null-terminated parameter list, get the referenced jail, and export
111 * the parameters to the list.
114 jail_getv(int flags, ...)
117 struct jailparam *jp, *jp_lastjid, *jp_jid, *jp_name, *jp_key;
118 char *valarg, *value;
119 const char *name, *key_value, *lastjid_value, *jid_value, *name_value;
122 /* Create the parameter list and find the key. */
125 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
126 (void)va_arg(tap, char *);
129 jp = alloca(njp * sizeof(struct jailparam));
131 jp_lastjid = jp_jid = jp_name = NULL;
132 lastjid_value = jid_value = name_value = NULL;
133 for (njp = 0; (name = va_arg(tap, char *)) != NULL; njp++) {
134 value = va_arg(tap, char *);
135 if (jailparam_init(jp + njp, name) < 0) {
139 if (!strcmp(jp[njp].jp_name, "lastjid")) {
140 jp_lastjid = jp + njp;
141 lastjid_value = value;
142 } else if (!strcmp(jp[njp].jp_name, "jid")) {
145 } if (!strcmp(jp[njp].jp_name, "name")) {
151 /* Import the key parameter. */
152 if (jp_lastjid != NULL) {
154 key_value = lastjid_value;
155 } else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) {
157 key_value = jid_value;
158 } else if (jp_name != NULL) {
160 key_value = name_value;
162 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
166 if (jailparam_import(jp_key, key_value) < 0)
168 /* Get the jail and export the parameters. */
169 jid = jailparam_get(jp, njp, flags);
172 for (i = 0; i < njp; i++) {
173 (void)va_arg(ap, char *);
174 valarg = va_arg(ap, char *);
175 if (jp + i != jp_key) {
176 /* It's up to the caller to ensure there's room. */
177 if ((jp[i].jp_ctltype & CTLTYPE) == CTLTYPE_STRING)
178 strcpy(valarg, jp[i].jp_value);
180 value = jailparam_export(jp + i);
183 strcpy(valarg, value);
188 jailparam_free(jp, njp);
193 jailparam_free(jp, njp);
199 * Return a list of all known parameters.
202 jailparam_all(struct jailparam **jpp)
204 struct jailparam *jp, *tjp;
205 size_t mlen1, mlen2, buflen;
207 int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2];
208 char buf[MAXPATHLEN];
212 jp = malloc(nlist * sizeof(*jp));
214 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
219 mlen1 = CTL_MAXNAME - 2;
220 if (sysctlnametomib(SJPARAM, mib1 + 2, &mlen1) < 0) {
221 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
222 "sysctlnametomib(" SJPARAM "): %s", strerror(errno));
226 /* Get the next parameter. */
227 mlen2 = sizeof(mib2);
228 if (sysctl(mib1, mlen1 + 2, mib2, &mlen2, NULL, 0) < 0) {
229 if (errno == ENOENT) {
230 /* No more entries. */
233 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
234 "sysctl(0.2): %s", strerror(errno));
237 if (mib2[0] != mib1[2] ||
238 mib2[1] != mib1[3] ||
241 /* Convert it to an ascii name. */
242 memcpy(mib1 + 2, mib2, mlen2);
243 mlen1 = mlen2 / sizeof(int);
245 buflen = sizeof(buf);
246 if (sysctl(mib1, mlen1 + 2, buf, &buflen, NULL, 0) < 0) {
247 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
248 "sysctl(0.1): %s", strerror(errno));
251 if (buf[buflen - 2] == '.')
252 buf[buflen - 2] = '\0';
253 /* Add the parameter to the list */
256 tjp = reallocarray(jp, nlist, sizeof(*jp));
261 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
265 /* Just return the untrimmed buffer if reallocarray() somehow fails. */
266 tjp = reallocarray(jp, njp, sizeof(*jp));
273 jailparam_free(jp, njp);
279 * Clear a jail parameter and copy in its name.
282 jailparam_init(struct jailparam *jp, const char *name)
285 memset(jp, 0, sizeof(*jp));
286 jp->jp_name = strdup(name);
287 if (jp->jp_name == NULL) {
288 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
291 if (jailparam_type(jp) < 0) {
292 jailparam_free(jp, 1);
301 * Put a name and value into a jail parameter element, converting the value
305 jailparam_import(struct jailparam *jp, const char *value)
307 char *p, *ep, *tvalue;
313 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
314 jp->jp_value = strdup(value);
315 if (jp->jp_value == NULL) {
316 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
322 if (jp->jp_elemlen) {
323 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
324 jp->jp_value = strdup("");
325 if (jp->jp_value == NULL) {
326 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
332 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
334 jp->jp_valuelen = jp->jp_elemlen * nval;
336 jp->jp_value = malloc(jp->jp_valuelen);
337 if (jp->jp_value == NULL) {
338 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
342 for (i = 0; i < nval; i++) {
343 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
344 switch (jp->jp_ctltype & CTLTYPE) {
346 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
347 if (!jailparam_import_enum(bool_values, 2,
348 avalue, fw, &((int *)jp->jp_value)[i])) {
349 snprintf(jail_errmsg,
350 JAIL_ERRMSGLEN, "%s: "
351 "unknown boolean value \"%.*s\"",
352 jp->jp_name, fw, avalue);
358 if (jp->jp_flags & JP_JAILSYS) {
360 * Allow setting a jailsys parameter to "new"
361 * in a booleanesque fashion.
363 if (value[0] == '\0')
364 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
365 else if (!jailparam_import_enum(jailsys_values,
366 sizeof(jailsys_values) /
367 sizeof(jailsys_values[0]), avalue, fw,
368 &((int *)jp->jp_value)[i])) {
369 snprintf(jail_errmsg,
370 JAIL_ERRMSGLEN, "%s: "
371 "unknown jailsys value \"%.*s\"",
372 jp->jp_name, fw, avalue);
378 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
380 if (ep != avalue + fw) {
381 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
382 "%s: non-integer value \"%.*s\"",
383 jp->jp_name, fw, avalue);
389 ((unsigned *)jp->jp_value)[i] =
390 strtoul(avalue, &ep, 10);
393 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
396 ((unsigned long *)jp->jp_value)[i] =
397 strtoul(avalue, &ep, 10);
400 ((int64_t *)jp->jp_value)[i] =
401 strtoimax(avalue, &ep, 10);
404 ((uint64_t *)jp->jp_value)[i] =
405 strtoumax(avalue, &ep, 10);
408 tvalue = alloca(fw + 1);
409 strlcpy(tvalue, avalue, fw + 1);
410 switch (jp->jp_structtype) {
412 if (inet_pton(AF_INET, tvalue,
413 &((struct in_addr *)jp->jp_value)[i]) != 1)
415 snprintf(jail_errmsg,
417 "%s: not an IPv4 address: %s",
418 jp->jp_name, tvalue);
424 if (inet_pton(AF_INET6, tvalue,
425 &((struct in6_addr *)jp->jp_value)[i]) != 1)
427 snprintf(jail_errmsg,
429 "%s: not an IPv6 address: %s",
430 jp->jp_name, tvalue);
441 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
442 "unknown type for %s", jp->jp_name);
457 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
458 size_t valsize, int *value)
463 for (i = 0; i < nvalues; i++)
464 if (valsize == strlen(values[i]) &&
465 !strncasecmp(valstr, values[i], valsize)) {
469 *value = strtol(valstr, &ep, 10);
470 return (ep == valstr + valsize);
474 * Put a name and value into a jail parameter element, copying the value
475 * but not altering it.
478 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
481 jp->jp_value = value;
482 jp->jp_valuelen = valuelen;
483 jp->jp_flags |= JP_RAWVALUE;
488 * Run the jail_set and jail_get system calls on a parameter list.
491 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
498 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
500 for (i = j = 0; j < njp; j++) {
501 jiov[i].iov_base = jp[j].jp_name;
502 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
504 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
506 * Set booleans without values. If one has a value of
507 * zero, change it to (or from) its "no" counterpart.
509 jiov[i].iov_base = NULL;
511 if (jp[j].jp_value != NULL &&
512 jp[j].jp_valuelen == sizeof(int) &&
513 !*(int *)jp[j].jp_value) {
515 nname = jp[j].jp_flags & JP_BOOL
516 ? noname(jp[j].jp_name)
517 : nononame(jp[j].jp_name);
523 jiov[i - 1].iov_base = nname;
524 jiov[i - 1].iov_len = strlen(nname) + 1;
529 * Try to fill in missing values with an empty string.
531 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
532 jailparam_import(jp + j, "") < 0) {
537 jiov[i].iov_base = jp[j].jp_value;
539 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
540 ? strlen(jp[j].jp_value) + 1
545 jiov[i].iov_base = __DECONST(char *, "errmsg");
546 jiov[i].iov_len = sizeof("errmsg");
548 jiov[i].iov_base = jail_errmsg;
549 jiov[i].iov_len = JAIL_ERRMSGLEN;
552 jid = jail_set(jiov, i, flags);
553 if (jid < 0 && !jail_errmsg[0])
554 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
558 for (j = 0; j < njp; j++)
559 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
560 jp[j].jp_value != NULL &&
561 jp[j].jp_valuelen == sizeof(int) &&
562 !*(int *)jp[j].jp_value)
563 free(jiov[j * 2].iov_base);
568 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
571 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
572 int i, ai, ki, jid, arrays, sanity;
576 * Get the types for all parameters.
577 * Find the key and any array parameters.
579 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
580 jp_lastjid = jp_jid = jp_name = NULL;
582 for (ai = j = 0; j < njp; j++) {
583 if (!strcmp(jp[j].jp_name, "lastjid"))
585 else if (!strcmp(jp[j].jp_name, "jid"))
587 else if (!strcmp(jp[j].jp_name, "name"))
589 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
591 jiov[ai].iov_base = jp[j].jp_name;
592 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
594 jiov[ai].iov_base = NULL;
595 jiov[ai].iov_len = 0;
599 jp_key = jp_lastjid ? jp_lastjid :
600 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
601 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
602 if (jp_key == NULL || jp_key->jp_value == NULL) {
603 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
608 jiov[ki].iov_base = jp_key->jp_name;
609 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
611 jiov[ki].iov_base = jp_key->jp_value;
612 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
613 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
615 jiov[ki].iov_base = __DECONST(char *, "errmsg");
616 jiov[ki].iov_len = sizeof("errmsg");
618 jiov[ki].iov_base = jail_errmsg;
619 jiov[ki].iov_len = JAIL_ERRMSGLEN;
622 if (arrays && jail_get(jiov, ki, flags) < 0) {
624 snprintf(jail_errmsg, sizeof(jail_errmsg),
625 "jail_get: %s", strerror(errno));
628 /* Allocate storage for all parameters. */
629 for (ai = j = 0, i = ki; j < njp; j++) {
630 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
632 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
633 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
634 jiov[ai].iov_len = jp[j].jp_valuelen;
636 jp[j].jp_valuelen = jiov[ai].iov_len;
637 if (jp[j].jp_value != NULL)
638 free(jp[j].jp_value);
639 jp[j].jp_value = malloc(jp[j].jp_valuelen);
640 if (jp[j].jp_value == NULL) {
641 strerror_r(errno, jail_errmsg,
646 jiov[ai].iov_base = jp[j].jp_value;
647 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
649 } else if (jp + j != jp_key) {
650 jiov[i].iov_base = jp[j].jp_name;
651 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
653 if (jp[j].jp_value == NULL &&
654 !(jp[j].jp_flags & JP_RAWVALUE)) {
655 jp[j].jp_value = malloc(jp[j].jp_valuelen);
656 if (jp[j].jp_value == NULL) {
657 strerror_r(errno, jail_errmsg,
662 jiov[i].iov_base = jp[j].jp_value;
663 jiov[i].iov_len = jp[j].jp_valuelen;
664 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
669 * Get the prison. If there are array elements, retry a few times
670 * in case their sizes changed from under us.
672 for (sanity = 0;; sanity++) {
673 jid = jail_get(jiov, i, flags);
674 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
675 errno != EINVAL || jail_errmsg[0])
677 for (ai = j = 0; j < njp; j++) {
678 if (jp[j].jp_elemlen &&
679 !(jp[j].jp_flags & JP_RAWVALUE)) {
681 jiov[ai].iov_base = NULL;
682 jiov[ai].iov_len = 0;
686 if (jail_get(jiov, ki, flags) < 0)
688 for (ai = j = 0; j < njp; j++) {
689 if (jp[j].jp_elemlen &&
690 !(jp[j].jp_flags & JP_RAWVALUE)) {
693 jp[j].jp_elemlen * ARRAY_SLOP;
694 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
695 jiov[ai].iov_len = jp[j].jp_valuelen;
697 jp[j].jp_valuelen = jiov[ai].iov_len;
698 if (jp[j].jp_value != NULL)
699 free(jp[j].jp_value);
701 malloc(jiov[ai].iov_len);
702 if (jp[j].jp_value == NULL) {
703 strerror_r(errno, jail_errmsg,
708 jiov[ai].iov_base = jp[j].jp_value;
709 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
714 if (jid < 0 && !jail_errmsg[0])
715 snprintf(jail_errmsg, sizeof(jail_errmsg),
716 "jail_get: %s", strerror(errno));
717 for (ai = j = 0, i = ki; j < njp; j++) {
718 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
720 jp[j].jp_valuelen = jiov[ai].iov_len;
722 } else if (jp + j != jp_key) {
724 jp[j].jp_valuelen = jiov[i].iov_len;
732 * Convert a jail parameter's value to external form.
735 jailparam_export(struct jailparam *jp)
738 char *value, *tvalue, **values;
741 char valbuf[INET6_ADDRSTRLEN];
743 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
744 value = strdup(jp->jp_value);
746 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
749 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
753 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
756 values = alloca(nval * sizeof(char *));
757 valuelens = alloca(nval * sizeof(size_t));
759 for (i = 0; i < nval; i++) {
760 switch (jp->jp_ctltype & CTLTYPE) {
762 ival = ((int *)jp->jp_value)[i];
763 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
764 (unsigned)ival < 2) {
765 strlcpy(valbuf, bool_values[ival],
769 if ((jp->jp_flags & JP_JAILSYS) &&
770 (unsigned)ival < sizeof(jailsys_values) /
771 sizeof(jailsys_values[0])) {
772 strlcpy(valbuf, jailsys_values[ival],
776 snprintf(valbuf, sizeof(valbuf), "%d", ival);
779 snprintf(valbuf, sizeof(valbuf), "%u",
780 ((unsigned *)jp->jp_value)[i]);
783 snprintf(valbuf, sizeof(valbuf), "%ld",
784 ((long *)jp->jp_value)[i]);
787 snprintf(valbuf, sizeof(valbuf), "%lu",
788 ((unsigned long *)jp->jp_value)[i]);
791 snprintf(valbuf, sizeof(valbuf), "%jd",
792 (intmax_t)((int64_t *)jp->jp_value)[i]);
795 snprintf(valbuf, sizeof(valbuf), "%ju",
796 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
799 switch (jp->jp_structtype) {
801 if (inet_ntop(AF_INET,
802 &((struct in_addr *)jp->jp_value)[i],
803 valbuf, sizeof(valbuf)) == NULL) {
804 strerror_r(errno, jail_errmsg,
810 if (inet_ntop(AF_INET6,
811 &((struct in6_addr *)jp->jp_value)[i],
812 valbuf, sizeof(valbuf)) == NULL) {
813 strerror_r(errno, jail_errmsg,
824 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
825 "unknown type for %s", jp->jp_name);
829 valuelens[i] = strlen(valbuf) + 1;
830 valuelen += valuelens[i];
831 values[i] = alloca(valuelens[i]);
832 strcpy(values[i], valbuf);
834 value = malloc(valuelen);
836 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
839 for (i = 0; i < nval; i++) {
840 strcpy(tvalue, values[i]);
842 tvalue += valuelens[i];
851 * Free the contents of a jail parameter list (but not the list itself).
854 jailparam_free(struct jailparam *jp, unsigned njp)
858 for (j = 0; j < njp; j++) {
860 if (!(jp[j].jp_flags & JP_RAWVALUE))
861 free(jp[j].jp_value);
866 * Find a parameter's type and size from its MIB.
869 jailparam_type(struct jailparam *jp)
871 char *p, *name, *nname;
872 size_t miblen, desclen;
878 int mib[CTL_MAXNAME];
880 /* The "lastjid" parameter isn't real. */
882 if (!strcmp(name, "lastjid")) {
883 jp->jp_valuelen = sizeof(int);
884 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
888 /* Find the sysctl that describes the parameter. */
891 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
892 miblen = sizeof(mib) - 2 * sizeof(int);
893 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
894 if (errno != ENOENT) {
895 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
896 "sysctl(0.3.%s): %s", name, strerror(errno));
899 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
900 desc.s, strlen(desc.s)) >= 0)
903 * The parameter probably doesn't exist. But it might be
904 * the "no" counterpart to a boolean.
906 nname = nononame(name);
909 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
910 "unknown parameter: %s", jp->jp_name);
914 name = alloca(strlen(nname) + 1);
917 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
918 miblen = sizeof(mib) - 2 * sizeof(int);
919 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
921 goto unknown_parameter;
922 jp->jp_flags |= JP_NOBOOL;
926 desclen = sizeof(desc);
927 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
929 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
930 "sysctl(0.4.%s): %s", name, strerror(errno));
933 jp->jp_ctltype = desc.i;
934 /* If this came from removing a "no", it better be a boolean. */
935 if (jp->jp_flags & JP_NOBOOL) {
936 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
937 jp->jp_valuelen = sizeof(int);
940 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
941 goto unknown_parameter;
943 /* See if this is an array type. */
944 p = strchr(desc.s, '\0');
946 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
952 /* Look for types we understand. */
953 switch (desc.i & CTLTYPE) {
955 if (desc.s[0] == 'B')
956 jp->jp_flags |= JP_BOOL;
957 else if (!strcmp(desc.s, "E,jailsys"))
958 jp->jp_flags |= JP_JAILSYS;
960 jp->jp_valuelen = sizeof(int);
964 jp->jp_valuelen = sizeof(long);
968 jp->jp_valuelen = sizeof(int64_t);
972 desclen = sizeof(desc.s);
973 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
975 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
976 "sysctl(" SJPARAM ".%s): %s", name,
980 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
983 if (!strcmp(desc.s, "S,in_addr")) {
984 jp->jp_structtype = JPS_IN_ADDR;
985 jp->jp_valuelen = sizeof(struct in_addr);
986 } else if (!strcmp(desc.s, "S,in6_addr")) {
987 jp->jp_structtype = JPS_IN6_ADDR;
988 jp->jp_valuelen = sizeof(struct in6_addr);
991 if (sysctl(mib + 2, miblen / sizeof(int),
992 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
993 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
994 "sysctl(" SJPARAM ".%s): %s", name,
1002 * A node might be described by an empty-named child,
1003 * which would be immediately before or after the node itself.
1006 miblen += sizeof(int);
1007 for (i = -1; i <= 1; i += 2) {
1008 mib[(miblen / sizeof(int)) + 1] =
1009 mib[(miblen / sizeof(int))] + i;
1010 desclen = sizeof(desc.s);
1011 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1012 &desclen, NULL, 0) < 0) {
1013 if (errno == ENOENT)
1015 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1016 "sysctl(0.1): %s", strerror(errno));
1019 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1020 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1021 memcmp(name, desc.s + sizeof(SJPARAM),
1022 desclen - sizeof(SJPARAM) - 2) == 0 &&
1023 desc.s[desclen - 2] == '.')
1026 goto unknown_parameter;
1028 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1029 "unknown type for %s", jp->jp_name);
1034 jp->jp_elemlen = jp->jp_valuelen;
1035 jp->jp_valuelen = 0;
1041 * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1044 kldload_param(const char *name)
1048 if (strcmp(name, "linux") == 0 || strncmp(name, "linux.", 6) == 0)
1049 kl = kldload("linux");
1050 else if (strcmp(name, "sysvmsg") == 0 || strcmp(name, "sysvsem") == 0 ||
1051 strcmp(name, "sysvshm") == 0)
1053 else if (strncmp(name, "allow.mount.", 12) == 0) {
1054 /* Load the matching filesystem */
1055 const char *modname = name + 12;
1057 kl = kldload(modname);
1058 if (kl < 0 && errno == ENOENT &&
1059 strncmp(modname, "no", 2) == 0)
1060 kl = kldload(modname + 2);
1065 if (kl < 0 && errno == EEXIST) {
1067 * In the module is already loaded, then it must not contain
1076 * Change a boolean parameter name into its "no" counterpart or vice versa.
1079 noname(const char *name)
1083 nname = malloc(strlen(name) + 3);
1084 if (nname == NULL) {
1085 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1088 p = strrchr(name, '.');
1090 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1092 sprintf(nname, "no%s", name);
1097 nononame(const char *name)
1101 p = strrchr(name, '.');
1102 if (strncmp(p ? p + 1 : name, "no", 2)) {
1103 snprintf(jail_errmsg, sizeof(jail_errmsg),
1104 "mismatched boolean: %s", name);
1108 nname = malloc(strlen(name) - 1);
1109 if (nname == NULL) {
1110 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1114 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1116 strcpy(nname, name + 2);