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 jp = reallocarray(jp, njp, sizeof(*jp));
270 jailparam_free(jp, njp);
276 * Clear a jail parameter and copy in its name.
279 jailparam_init(struct jailparam *jp, const char *name)
282 memset(jp, 0, sizeof(*jp));
283 jp->jp_name = strdup(name);
284 if (jp->jp_name == NULL) {
285 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
288 if (jailparam_type(jp) < 0) {
289 jailparam_free(jp, 1);
298 * Put a name and value into a jail parameter element, converting the value
302 jailparam_import(struct jailparam *jp, const char *value)
304 char *p, *ep, *tvalue;
310 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
311 jp->jp_value = strdup(value);
312 if (jp->jp_value == NULL) {
313 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
319 if (jp->jp_elemlen) {
320 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
321 jp->jp_value = strdup("");
322 if (jp->jp_value == NULL) {
323 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
329 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
331 jp->jp_valuelen = jp->jp_elemlen * nval;
333 jp->jp_value = malloc(jp->jp_valuelen);
334 if (jp->jp_value == NULL) {
335 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
339 for (i = 0; i < nval; i++) {
340 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
341 switch (jp->jp_ctltype & CTLTYPE) {
343 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
344 if (!jailparam_import_enum(bool_values, 2,
345 avalue, fw, &((int *)jp->jp_value)[i])) {
346 snprintf(jail_errmsg,
347 JAIL_ERRMSGLEN, "%s: "
348 "unknown boolean value \"%.*s\"",
349 jp->jp_name, fw, avalue);
355 if (jp->jp_flags & JP_JAILSYS) {
357 * Allow setting a jailsys parameter to "new"
358 * in a booleanesque fashion.
360 if (value[0] == '\0')
361 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
362 else if (!jailparam_import_enum(jailsys_values,
363 sizeof(jailsys_values) /
364 sizeof(jailsys_values[0]), avalue, fw,
365 &((int *)jp->jp_value)[i])) {
366 snprintf(jail_errmsg,
367 JAIL_ERRMSGLEN, "%s: "
368 "unknown jailsys value \"%.*s\"",
369 jp->jp_name, fw, avalue);
375 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
377 if (ep != avalue + fw) {
378 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
379 "%s: non-integer value \"%.*s\"",
380 jp->jp_name, fw, avalue);
386 ((unsigned *)jp->jp_value)[i] =
387 strtoul(avalue, &ep, 10);
390 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
393 ((unsigned long *)jp->jp_value)[i] =
394 strtoul(avalue, &ep, 10);
397 ((int64_t *)jp->jp_value)[i] =
398 strtoimax(avalue, &ep, 10);
401 ((uint64_t *)jp->jp_value)[i] =
402 strtoumax(avalue, &ep, 10);
405 tvalue = alloca(fw + 1);
406 strlcpy(tvalue, avalue, fw + 1);
407 switch (jp->jp_structtype) {
409 if (inet_pton(AF_INET, tvalue,
410 &((struct in_addr *)jp->jp_value)[i]) != 1)
412 snprintf(jail_errmsg,
414 "%s: not an IPv4 address: %s",
415 jp->jp_name, tvalue);
421 if (inet_pton(AF_INET6, tvalue,
422 &((struct in6_addr *)jp->jp_value)[i]) != 1)
424 snprintf(jail_errmsg,
426 "%s: not an IPv6 address: %s",
427 jp->jp_name, tvalue);
438 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
439 "unknown type for %s", jp->jp_name);
454 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
455 size_t valsize, int *value)
460 for (i = 0; i < nvalues; i++)
461 if (valsize == strlen(values[i]) &&
462 !strncasecmp(valstr, values[i], valsize)) {
466 *value = strtol(valstr, &ep, 10);
467 return (ep == valstr + valsize);
471 * Put a name and value into a jail parameter element, copying the value
472 * but not altering it.
475 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
478 jp->jp_value = value;
479 jp->jp_valuelen = valuelen;
480 jp->jp_flags |= JP_RAWVALUE;
485 * Run the jail_set and jail_get system calls on a parameter list.
488 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
495 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
497 for (i = j = 0; j < njp; j++) {
498 jiov[i].iov_base = jp[j].jp_name;
499 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
501 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
503 * Set booleans without values. If one has a value of
504 * zero, change it to (or from) its "no" counterpart.
506 jiov[i].iov_base = NULL;
508 if (jp[j].jp_value != NULL &&
509 jp[j].jp_valuelen == sizeof(int) &&
510 !*(int *)jp[j].jp_value) {
512 nname = jp[j].jp_flags & JP_BOOL
513 ? noname(jp[j].jp_name)
514 : nononame(jp[j].jp_name);
520 jiov[i - 1].iov_base = nname;
521 jiov[i - 1].iov_len = strlen(nname) + 1;
526 * Try to fill in missing values with an empty string.
528 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
529 jailparam_import(jp + j, "") < 0) {
534 jiov[i].iov_base = jp[j].jp_value;
536 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
537 ? strlen(jp[j].jp_value) + 1
542 jiov[i].iov_base = __DECONST(char *, "errmsg");
543 jiov[i].iov_len = sizeof("errmsg");
545 jiov[i].iov_base = jail_errmsg;
546 jiov[i].iov_len = JAIL_ERRMSGLEN;
549 jid = jail_set(jiov, i, flags);
550 if (jid < 0 && !jail_errmsg[0])
551 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
555 for (j = 0; j < njp; j++)
556 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
557 jp[j].jp_value != NULL &&
558 jp[j].jp_valuelen == sizeof(int) &&
559 !*(int *)jp[j].jp_value)
560 free(jiov[j * 2].iov_base);
565 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
568 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
569 int i, ai, ki, jid, arrays, sanity;
573 * Get the types for all parameters.
574 * Find the key and any array parameters.
576 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
577 jp_lastjid = jp_jid = jp_name = NULL;
579 for (ai = j = 0; j < njp; j++) {
580 if (!strcmp(jp[j].jp_name, "lastjid"))
582 else if (!strcmp(jp[j].jp_name, "jid"))
584 else if (!strcmp(jp[j].jp_name, "name"))
586 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
588 jiov[ai].iov_base = jp[j].jp_name;
589 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
591 jiov[ai].iov_base = NULL;
592 jiov[ai].iov_len = 0;
596 jp_key = jp_lastjid ? jp_lastjid :
597 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
598 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
599 if (jp_key == NULL || jp_key->jp_value == NULL) {
600 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
605 jiov[ki].iov_base = jp_key->jp_name;
606 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
608 jiov[ki].iov_base = jp_key->jp_value;
609 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
610 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
612 jiov[ki].iov_base = __DECONST(char *, "errmsg");
613 jiov[ki].iov_len = sizeof("errmsg");
615 jiov[ki].iov_base = jail_errmsg;
616 jiov[ki].iov_len = JAIL_ERRMSGLEN;
619 if (arrays && jail_get(jiov, ki, flags) < 0) {
621 snprintf(jail_errmsg, sizeof(jail_errmsg),
622 "jail_get: %s", strerror(errno));
625 /* Allocate storage for all parameters. */
626 for (ai = j = 0, i = ki; j < njp; j++) {
627 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
629 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
630 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
631 jiov[ai].iov_len = jp[j].jp_valuelen;
633 jp[j].jp_valuelen = jiov[ai].iov_len;
634 if (jp[j].jp_value != NULL)
635 free(jp[j].jp_value);
636 jp[j].jp_value = malloc(jp[j].jp_valuelen);
637 if (jp[j].jp_value == NULL) {
638 strerror_r(errno, jail_errmsg,
643 jiov[ai].iov_base = jp[j].jp_value;
644 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
646 } else if (jp + j != jp_key) {
647 jiov[i].iov_base = jp[j].jp_name;
648 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
650 if (jp[j].jp_value == NULL &&
651 !(jp[j].jp_flags & JP_RAWVALUE)) {
652 jp[j].jp_value = malloc(jp[j].jp_valuelen);
653 if (jp[j].jp_value == NULL) {
654 strerror_r(errno, jail_errmsg,
659 jiov[i].iov_base = jp[j].jp_value;
660 jiov[i].iov_len = jp[j].jp_valuelen;
661 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
666 * Get the prison. If there are array elements, retry a few times
667 * in case their sizes changed from under us.
669 for (sanity = 0;; sanity++) {
670 jid = jail_get(jiov, i, flags);
671 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
672 errno != EINVAL || jail_errmsg[0])
674 for (ai = j = 0; j < njp; j++) {
675 if (jp[j].jp_elemlen &&
676 !(jp[j].jp_flags & JP_RAWVALUE)) {
678 jiov[ai].iov_base = NULL;
679 jiov[ai].iov_len = 0;
683 if (jail_get(jiov, ki, flags) < 0)
685 for (ai = j = 0; j < njp; j++) {
686 if (jp[j].jp_elemlen &&
687 !(jp[j].jp_flags & JP_RAWVALUE)) {
690 jp[j].jp_elemlen * ARRAY_SLOP;
691 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
692 jiov[ai].iov_len = jp[j].jp_valuelen;
694 jp[j].jp_valuelen = jiov[ai].iov_len;
695 if (jp[j].jp_value != NULL)
696 free(jp[j].jp_value);
698 malloc(jiov[ai].iov_len);
699 if (jp[j].jp_value == NULL) {
700 strerror_r(errno, jail_errmsg,
705 jiov[ai].iov_base = jp[j].jp_value;
706 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
711 if (jid < 0 && !jail_errmsg[0])
712 snprintf(jail_errmsg, sizeof(jail_errmsg),
713 "jail_get: %s", strerror(errno));
714 for (ai = j = 0, i = ki; j < njp; j++) {
715 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
717 jp[j].jp_valuelen = jiov[ai].iov_len;
719 } else if (jp + j != jp_key) {
721 jp[j].jp_valuelen = jiov[i].iov_len;
729 * Convert a jail parameter's value to external form.
732 jailparam_export(struct jailparam *jp)
735 char *value, *tvalue, **values;
738 char valbuf[INET6_ADDRSTRLEN];
740 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
741 value = strdup(jp->jp_value);
743 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
746 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
750 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
753 values = alloca(nval * sizeof(char *));
754 valuelens = alloca(nval * sizeof(size_t));
756 for (i = 0; i < nval; i++) {
757 switch (jp->jp_ctltype & CTLTYPE) {
759 ival = ((int *)jp->jp_value)[i];
760 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
761 (unsigned)ival < 2) {
762 strlcpy(valbuf, bool_values[ival],
766 if ((jp->jp_flags & JP_JAILSYS) &&
767 (unsigned)ival < sizeof(jailsys_values) /
768 sizeof(jailsys_values[0])) {
769 strlcpy(valbuf, jailsys_values[ival],
773 snprintf(valbuf, sizeof(valbuf), "%d", ival);
776 snprintf(valbuf, sizeof(valbuf), "%u",
777 ((unsigned *)jp->jp_value)[i]);
780 snprintf(valbuf, sizeof(valbuf), "%ld",
781 ((long *)jp->jp_value)[i]);
784 snprintf(valbuf, sizeof(valbuf), "%lu",
785 ((unsigned long *)jp->jp_value)[i]);
788 snprintf(valbuf, sizeof(valbuf), "%jd",
789 (intmax_t)((int64_t *)jp->jp_value)[i]);
792 snprintf(valbuf, sizeof(valbuf), "%ju",
793 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
796 switch (jp->jp_structtype) {
798 if (inet_ntop(AF_INET,
799 &((struct in_addr *)jp->jp_value)[i],
800 valbuf, sizeof(valbuf)) == NULL) {
801 strerror_r(errno, jail_errmsg,
807 if (inet_ntop(AF_INET6,
808 &((struct in6_addr *)jp->jp_value)[i],
809 valbuf, sizeof(valbuf)) == NULL) {
810 strerror_r(errno, jail_errmsg,
821 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
822 "unknown type for %s", jp->jp_name);
826 valuelens[i] = strlen(valbuf) + 1;
827 valuelen += valuelens[i];
828 values[i] = alloca(valuelens[i]);
829 strcpy(values[i], valbuf);
831 value = malloc(valuelen);
833 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
836 for (i = 0; i < nval; i++) {
837 strcpy(tvalue, values[i]);
839 tvalue += valuelens[i];
848 * Free the contents of a jail parameter list (but not the list itself).
851 jailparam_free(struct jailparam *jp, unsigned njp)
855 for (j = 0; j < njp; j++) {
857 if (!(jp[j].jp_flags & JP_RAWVALUE))
858 free(jp[j].jp_value);
863 * Find a parameter's type and size from its MIB.
866 jailparam_type(struct jailparam *jp)
868 char *p, *name, *nname;
869 size_t miblen, desclen;
875 int mib[CTL_MAXNAME];
877 /* The "lastjid" parameter isn't real. */
879 if (!strcmp(name, "lastjid")) {
880 jp->jp_valuelen = sizeof(int);
881 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
885 /* Find the sysctl that describes the parameter. */
888 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
889 miblen = sizeof(mib) - 2 * sizeof(int);
890 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
891 if (errno != ENOENT) {
892 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
893 "sysctl(0.3.%s): %s", name, strerror(errno));
896 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
897 desc.s, strlen(desc.s)) >= 0)
900 * The parameter probably doesn't exist. But it might be
901 * the "no" counterpart to a boolean.
903 nname = nononame(name);
906 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
907 "unknown parameter: %s", jp->jp_name);
911 name = alloca(strlen(nname) + 1);
914 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
915 miblen = sizeof(mib) - 2 * sizeof(int);
916 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
918 goto unknown_parameter;
919 jp->jp_flags |= JP_NOBOOL;
923 desclen = sizeof(desc);
924 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
926 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
927 "sysctl(0.4.%s): %s", name, strerror(errno));
930 jp->jp_ctltype = desc.i;
931 /* If this came from removing a "no", it better be a boolean. */
932 if (jp->jp_flags & JP_NOBOOL) {
933 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
934 jp->jp_valuelen = sizeof(int);
937 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
938 goto unknown_parameter;
940 /* See if this is an array type. */
941 p = strchr(desc.s, '\0');
943 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
949 /* Look for types we understand. */
950 switch (desc.i & CTLTYPE) {
952 if (desc.s[0] == 'B')
953 jp->jp_flags |= JP_BOOL;
954 else if (!strcmp(desc.s, "E,jailsys"))
955 jp->jp_flags |= JP_JAILSYS;
957 jp->jp_valuelen = sizeof(int);
961 jp->jp_valuelen = sizeof(long);
965 jp->jp_valuelen = sizeof(int64_t);
969 desclen = sizeof(desc.s);
970 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
972 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
973 "sysctl(" SJPARAM ".%s): %s", name,
977 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
980 if (!strcmp(desc.s, "S,in_addr")) {
981 jp->jp_structtype = JPS_IN_ADDR;
982 jp->jp_valuelen = sizeof(struct in_addr);
983 } else if (!strcmp(desc.s, "S,in6_addr")) {
984 jp->jp_structtype = JPS_IN6_ADDR;
985 jp->jp_valuelen = sizeof(struct in6_addr);
988 if (sysctl(mib + 2, miblen / sizeof(int),
989 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
990 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
991 "sysctl(" SJPARAM ".%s): %s", name,
999 * A node might be described by an empty-named child,
1000 * which would be immediately before or after the node itself.
1003 miblen += sizeof(int);
1004 for (i = -1; i <= 1; i += 2) {
1005 mib[(miblen / sizeof(int)) + 1] =
1006 mib[(miblen / sizeof(int))] + i;
1007 desclen = sizeof(desc.s);
1008 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1009 &desclen, NULL, 0) < 0) {
1010 if (errno == ENOENT)
1012 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1013 "sysctl(0.1): %s", strerror(errno));
1016 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1017 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1018 memcmp(name, desc.s + sizeof(SJPARAM),
1019 desclen - sizeof(SJPARAM) - 2) == 0 &&
1020 desc.s[desclen - 2] == '.')
1023 goto unknown_parameter;
1025 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1026 "unknown type for %s", jp->jp_name);
1031 jp->jp_elemlen = jp->jp_valuelen;
1032 jp->jp_valuelen = 0;
1038 * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1041 kldload_param(const char *name)
1045 if (strcmp(name, "linux") == 0 || strncmp(name, "linux.", 6) == 0)
1046 kl = kldload("linux");
1047 else if (strcmp(name, "sysvmsg") == 0 || strcmp(name, "sysvsem") == 0 ||
1048 strcmp(name, "sysvshm") == 0)
1050 else if (strncmp(name, "allow.mount.", 12) == 0) {
1051 /* Load the matching filesystem */
1052 const char *modname = name + 12;
1054 kl = kldload(modname);
1055 if (kl < 0 && errno == ENOENT &&
1056 strncmp(modname, "no", 2) == 0)
1057 kl = kldload(modname + 2);
1062 if (kl < 0 && errno == EEXIST) {
1064 * In the module is already loaded, then it must not contain
1073 * Change a boolean parameter name into its "no" counterpart or vice versa.
1076 noname(const char *name)
1080 nname = malloc(strlen(name) + 3);
1081 if (nname == NULL) {
1082 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1085 p = strrchr(name, '.');
1087 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1089 sprintf(nname, "no%s", name);
1094 nononame(const char *name)
1098 p = strrchr(name, '.');
1099 if (strncmp(p ? p + 1 : name, "no", 2)) {
1100 snprintf(jail_errmsg, sizeof(jail_errmsg),
1101 "mismatched boolean: %s", name);
1105 nname = malloc(strlen(name) - 1);
1106 if (nname == NULL) {
1107 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1111 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1113 strcpy(nname, name + 2);