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 /* Just return the untrimmed buffer if reallocarray() somehow fails. */
267 tjp = reallocarray(jp, njp, sizeof(*jp));
274 jailparam_free(jp, njp);
280 * Clear a jail parameter and copy in its name.
283 jailparam_init(struct jailparam *jp, const char *name)
286 memset(jp, 0, sizeof(*jp));
287 jp->jp_name = strdup(name);
288 if (jp->jp_name == NULL) {
289 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
292 if (jailparam_type(jp) < 0) {
293 jailparam_free(jp, 1);
302 * Put a name and value into a jail parameter element, converting the value
306 jailparam_import(struct jailparam *jp, const char *value)
308 char *p, *ep, *tvalue;
314 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
315 jp->jp_value = strdup(value);
316 if (jp->jp_value == NULL) {
317 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
323 if (jp->jp_elemlen) {
324 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
325 jp->jp_value = strdup("");
326 if (jp->jp_value == NULL) {
327 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
333 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
335 jp->jp_valuelen = jp->jp_elemlen * nval;
337 jp->jp_value = malloc(jp->jp_valuelen);
338 if (jp->jp_value == NULL) {
339 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
343 for (i = 0; i < nval; i++) {
344 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
345 switch (jp->jp_ctltype & CTLTYPE) {
347 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
348 if (!jailparam_import_enum(bool_values, 2,
349 avalue, fw, &((int *)jp->jp_value)[i])) {
350 snprintf(jail_errmsg,
351 JAIL_ERRMSGLEN, "%s: "
352 "unknown boolean value \"%.*s\"",
353 jp->jp_name, fw, avalue);
359 if (jp->jp_flags & JP_JAILSYS) {
361 * Allow setting a jailsys parameter to "new"
362 * in a booleanesque fashion.
364 if (value[0] == '\0')
365 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
366 else if (!jailparam_import_enum(jailsys_values,
367 sizeof(jailsys_values) /
368 sizeof(jailsys_values[0]), avalue, fw,
369 &((int *)jp->jp_value)[i])) {
370 snprintf(jail_errmsg,
371 JAIL_ERRMSGLEN, "%s: "
372 "unknown jailsys value \"%.*s\"",
373 jp->jp_name, fw, avalue);
379 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
381 if (ep != avalue + fw) {
382 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
383 "%s: non-integer value \"%.*s\"",
384 jp->jp_name, fw, avalue);
390 ((unsigned *)jp->jp_value)[i] =
391 strtoul(avalue, &ep, 10);
394 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
397 ((unsigned long *)jp->jp_value)[i] =
398 strtoul(avalue, &ep, 10);
401 ((int64_t *)jp->jp_value)[i] =
402 strtoimax(avalue, &ep, 10);
405 ((uint64_t *)jp->jp_value)[i] =
406 strtoumax(avalue, &ep, 10);
409 tvalue = alloca(fw + 1);
410 strlcpy(tvalue, avalue, fw + 1);
411 switch (jp->jp_structtype) {
413 if (inet_pton(AF_INET, tvalue,
414 &((struct in_addr *)jp->jp_value)[i]) != 1)
416 snprintf(jail_errmsg,
418 "%s: not an IPv4 address: %s",
419 jp->jp_name, tvalue);
425 if (inet_pton(AF_INET6, tvalue,
426 &((struct in6_addr *)jp->jp_value)[i]) != 1)
428 snprintf(jail_errmsg,
430 "%s: not an IPv6 address: %s",
431 jp->jp_name, tvalue);
442 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
443 "unknown type for %s", jp->jp_name);
458 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
459 size_t valsize, int *value)
464 for (i = 0; i < nvalues; i++)
465 if (valsize == strlen(values[i]) &&
466 !strncasecmp(valstr, values[i], valsize)) {
470 *value = strtol(valstr, &ep, 10);
471 return (ep == valstr + valsize);
475 * Put a name and value into a jail parameter element, copying the value
476 * but not altering it.
479 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
482 jp->jp_value = value;
483 jp->jp_valuelen = valuelen;
484 jp->jp_flags |= JP_RAWVALUE;
489 * Run the jail_set and jail_get system calls on a parameter list.
492 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
499 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
501 for (i = j = 0; j < njp; j++) {
502 jiov[i].iov_base = jp[j].jp_name;
503 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
505 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
507 * Set booleans without values. If one has a value of
508 * zero, change it to (or from) its "no" counterpart.
510 jiov[i].iov_base = NULL;
512 if (jp[j].jp_value != NULL &&
513 jp[j].jp_valuelen == sizeof(int) &&
514 !*(int *)jp[j].jp_value) {
516 nname = jp[j].jp_flags & JP_BOOL
517 ? noname(jp[j].jp_name)
518 : nononame(jp[j].jp_name);
524 jiov[i - 1].iov_base = nname;
525 jiov[i - 1].iov_len = strlen(nname) + 1;
530 * Try to fill in missing values with an empty string.
532 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
533 jailparam_import(jp + j, "") < 0) {
538 jiov[i].iov_base = jp[j].jp_value;
540 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
541 ? strlen(jp[j].jp_value) + 1
546 jiov[i].iov_base = __DECONST(char *, "errmsg");
547 jiov[i].iov_len = sizeof("errmsg");
549 jiov[i].iov_base = jail_errmsg;
550 jiov[i].iov_len = JAIL_ERRMSGLEN;
553 jid = jail_set(jiov, i, flags);
554 if (jid < 0 && !jail_errmsg[0])
555 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
559 for (j = 0; j < njp; j++)
560 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
561 jp[j].jp_value != NULL &&
562 jp[j].jp_valuelen == sizeof(int) &&
563 !*(int *)jp[j].jp_value)
564 free(jiov[j * 2].iov_base);
569 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
572 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
573 int i, ai, ki, jid, arrays, sanity;
577 * Get the types for all parameters.
578 * Find the key and any array parameters.
580 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
581 jp_lastjid = jp_jid = jp_name = NULL;
583 for (ai = j = 0; j < njp; j++) {
584 if (!strcmp(jp[j].jp_name, "lastjid"))
586 else if (!strcmp(jp[j].jp_name, "jid"))
588 else if (!strcmp(jp[j].jp_name, "name"))
590 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
592 jiov[ai].iov_base = jp[j].jp_name;
593 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
595 jiov[ai].iov_base = NULL;
596 jiov[ai].iov_len = 0;
600 jp_key = jp_lastjid ? jp_lastjid :
601 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
602 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
603 if (jp_key == NULL || jp_key->jp_value == NULL) {
604 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
609 jiov[ki].iov_base = jp_key->jp_name;
610 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
612 jiov[ki].iov_base = jp_key->jp_value;
613 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
614 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
616 jiov[ki].iov_base = __DECONST(char *, "errmsg");
617 jiov[ki].iov_len = sizeof("errmsg");
619 jiov[ki].iov_base = jail_errmsg;
620 jiov[ki].iov_len = JAIL_ERRMSGLEN;
623 if (arrays && jail_get(jiov, ki, flags) < 0) {
625 snprintf(jail_errmsg, sizeof(jail_errmsg),
626 "jail_get: %s", strerror(errno));
629 /* Allocate storage for all parameters. */
630 for (ai = j = 0, i = ki; j < njp; j++) {
631 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
633 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
634 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
635 jiov[ai].iov_len = jp[j].jp_valuelen;
637 jp[j].jp_valuelen = jiov[ai].iov_len;
638 if (jp[j].jp_value != NULL)
639 free(jp[j].jp_value);
640 jp[j].jp_value = malloc(jp[j].jp_valuelen);
641 if (jp[j].jp_value == NULL) {
642 strerror_r(errno, jail_errmsg,
647 jiov[ai].iov_base = jp[j].jp_value;
648 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
650 } else if (jp + j != jp_key) {
651 jiov[i].iov_base = jp[j].jp_name;
652 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
654 if (jp[j].jp_value == NULL &&
655 !(jp[j].jp_flags & JP_RAWVALUE)) {
656 jp[j].jp_value = malloc(jp[j].jp_valuelen);
657 if (jp[j].jp_value == NULL) {
658 strerror_r(errno, jail_errmsg,
663 jiov[i].iov_base = jp[j].jp_value;
664 jiov[i].iov_len = jp[j].jp_valuelen;
665 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
670 * Get the prison. If there are array elements, retry a few times
671 * in case their sizes changed from under us.
673 for (sanity = 0;; sanity++) {
674 jid = jail_get(jiov, i, flags);
675 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
676 errno != EINVAL || jail_errmsg[0])
678 for (ai = j = 0; j < njp; j++) {
679 if (jp[j].jp_elemlen &&
680 !(jp[j].jp_flags & JP_RAWVALUE)) {
682 jiov[ai].iov_base = NULL;
683 jiov[ai].iov_len = 0;
687 if (jail_get(jiov, ki, flags) < 0)
689 for (ai = j = 0; j < njp; j++) {
690 if (jp[j].jp_elemlen &&
691 !(jp[j].jp_flags & JP_RAWVALUE)) {
694 jp[j].jp_elemlen * ARRAY_SLOP;
695 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
696 jiov[ai].iov_len = jp[j].jp_valuelen;
698 jp[j].jp_valuelen = jiov[ai].iov_len;
699 if (jp[j].jp_value != NULL)
700 free(jp[j].jp_value);
702 malloc(jiov[ai].iov_len);
703 if (jp[j].jp_value == NULL) {
704 strerror_r(errno, jail_errmsg,
709 jiov[ai].iov_base = jp[j].jp_value;
710 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
715 if (jid < 0 && !jail_errmsg[0])
716 snprintf(jail_errmsg, sizeof(jail_errmsg),
717 "jail_get: %s", strerror(errno));
718 for (ai = j = 0, i = ki; j < njp; j++) {
719 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
721 jp[j].jp_valuelen = jiov[ai].iov_len;
723 } else if (jp + j != jp_key) {
725 jp[j].jp_valuelen = jiov[i].iov_len;
733 * Convert a jail parameter's value to external form.
736 jailparam_export(struct jailparam *jp)
739 char *value, *tvalue, **values;
742 char valbuf[INET6_ADDRSTRLEN];
744 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
745 value = strdup(jp->jp_value);
747 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
750 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
754 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
757 values = alloca(nval * sizeof(char *));
758 valuelens = alloca(nval * sizeof(size_t));
760 for (i = 0; i < nval; i++) {
761 switch (jp->jp_ctltype & CTLTYPE) {
763 ival = ((int *)jp->jp_value)[i];
764 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
765 (unsigned)ival < 2) {
766 strlcpy(valbuf, bool_values[ival],
770 if ((jp->jp_flags & JP_JAILSYS) &&
771 (unsigned)ival < sizeof(jailsys_values) /
772 sizeof(jailsys_values[0])) {
773 strlcpy(valbuf, jailsys_values[ival],
777 snprintf(valbuf, sizeof(valbuf), "%d", ival);
780 snprintf(valbuf, sizeof(valbuf), "%u",
781 ((unsigned *)jp->jp_value)[i]);
784 snprintf(valbuf, sizeof(valbuf), "%ld",
785 ((long *)jp->jp_value)[i]);
788 snprintf(valbuf, sizeof(valbuf), "%lu",
789 ((unsigned long *)jp->jp_value)[i]);
792 snprintf(valbuf, sizeof(valbuf), "%jd",
793 (intmax_t)((int64_t *)jp->jp_value)[i]);
796 snprintf(valbuf, sizeof(valbuf), "%ju",
797 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
800 switch (jp->jp_structtype) {
802 if (inet_ntop(AF_INET,
803 &((struct in_addr *)jp->jp_value)[i],
804 valbuf, sizeof(valbuf)) == NULL) {
805 strerror_r(errno, jail_errmsg,
811 if (inet_ntop(AF_INET6,
812 &((struct in6_addr *)jp->jp_value)[i],
813 valbuf, sizeof(valbuf)) == NULL) {
814 strerror_r(errno, jail_errmsg,
825 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
826 "unknown type for %s", jp->jp_name);
830 valuelens[i] = strlen(valbuf) + 1;
831 valuelen += valuelens[i];
832 values[i] = alloca(valuelens[i]);
833 strcpy(values[i], valbuf);
835 value = malloc(valuelen);
837 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
840 for (i = 0; i < nval; i++) {
841 strcpy(tvalue, values[i]);
843 tvalue += valuelens[i];
852 * Free the contents of a jail parameter list (but not the list itself).
855 jailparam_free(struct jailparam *jp, unsigned njp)
859 for (j = 0; j < njp; j++) {
861 if (!(jp[j].jp_flags & JP_RAWVALUE))
862 free(jp[j].jp_value);
867 * Find a parameter's type and size from its MIB.
870 jailparam_type(struct jailparam *jp)
872 char *p, *name, *nname;
873 size_t miblen, desclen;
879 int mib[CTL_MAXNAME];
881 /* The "lastjid" parameter isn't real. */
883 if (!strcmp(name, "lastjid")) {
884 jp->jp_valuelen = sizeof(int);
885 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
889 /* Find the sysctl that describes the parameter. */
892 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
893 miblen = sizeof(mib) - 2 * sizeof(int);
894 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
895 if (errno != ENOENT) {
896 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
897 "sysctl(0.3.%s): %s", name, strerror(errno));
900 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
901 desc.s, strlen(desc.s)) >= 0)
904 * The parameter probably doesn't exist. But it might be
905 * the "no" counterpart to a boolean.
907 nname = nononame(name);
910 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
911 "unknown parameter: %s", jp->jp_name);
915 name = alloca(strlen(nname) + 1);
918 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
919 miblen = sizeof(mib) - 2 * sizeof(int);
920 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
922 goto unknown_parameter;
923 jp->jp_flags |= JP_NOBOOL;
927 desclen = sizeof(desc);
928 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
930 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
931 "sysctl(0.4.%s): %s", name, strerror(errno));
934 jp->jp_ctltype = desc.i;
935 /* If this came from removing a "no", it better be a boolean. */
936 if (jp->jp_flags & JP_NOBOOL) {
937 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
938 jp->jp_valuelen = sizeof(int);
941 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
942 goto unknown_parameter;
944 /* See if this is an array type. */
945 p = strchr(desc.s, '\0');
947 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
953 /* Look for types we understand. */
954 switch (desc.i & CTLTYPE) {
956 if (desc.s[0] == 'B')
957 jp->jp_flags |= JP_BOOL;
958 else if (!strcmp(desc.s, "E,jailsys"))
959 jp->jp_flags |= JP_JAILSYS;
961 jp->jp_valuelen = sizeof(int);
965 jp->jp_valuelen = sizeof(long);
969 jp->jp_valuelen = sizeof(int64_t);
973 desclen = sizeof(desc.s);
974 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
976 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
977 "sysctl(" SJPARAM ".%s): %s", name,
981 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
984 if (!strcmp(desc.s, "S,in_addr")) {
985 jp->jp_structtype = JPS_IN_ADDR;
986 jp->jp_valuelen = sizeof(struct in_addr);
987 } else if (!strcmp(desc.s, "S,in6_addr")) {
988 jp->jp_structtype = JPS_IN6_ADDR;
989 jp->jp_valuelen = sizeof(struct in6_addr);
992 if (sysctl(mib + 2, miblen / sizeof(int),
993 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
994 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
995 "sysctl(" SJPARAM ".%s): %s", name,
1003 * A node might be described by an empty-named child,
1004 * which would be immediately before or after the node itself.
1007 miblen += sizeof(int);
1008 for (i = -1; i <= 1; i += 2) {
1009 mib[(miblen / sizeof(int)) + 1] =
1010 mib[(miblen / sizeof(int))] + i;
1011 desclen = sizeof(desc.s);
1012 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1013 &desclen, NULL, 0) < 0) {
1014 if (errno == ENOENT)
1016 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1017 "sysctl(0.1): %s", strerror(errno));
1020 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1021 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1022 memcmp(name, desc.s + sizeof(SJPARAM),
1023 desclen - sizeof(SJPARAM) - 2) == 0 &&
1024 desc.s[desclen - 2] == '.')
1027 goto unknown_parameter;
1029 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1030 "unknown type for %s", jp->jp_name);
1035 jp->jp_elemlen = jp->jp_valuelen;
1036 jp->jp_valuelen = 0;
1042 * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1045 kldload_param(const char *name)
1049 if (strcmp(name, "linux") == 0 || strncmp(name, "linux.", 6) == 0)
1050 kl = kldload("linux");
1051 else if (strcmp(name, "sysvmsg") == 0 || strcmp(name, "sysvsem") == 0 ||
1052 strcmp(name, "sysvshm") == 0)
1054 else if (strncmp(name, "allow.mount.", 12) == 0) {
1055 /* Load the matching filesystem */
1056 const char *modname = name + 12;
1058 kl = kldload(modname);
1059 if (kl < 0 && errno == ENOENT &&
1060 strncmp(modname, "no", 2) == 0)
1061 kl = kldload(modname + 2);
1066 if (kl < 0 && errno == EEXIST) {
1068 * In the module is already loaded, then it must not contain
1077 * Change a boolean parameter name into its "no" counterpart or vice versa.
1080 noname(const char *name)
1084 nname = malloc(strlen(name) + 3);
1085 if (nname == NULL) {
1086 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1089 p = strrchr(name, '.');
1091 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1093 sprintf(nname, "no%s", name);
1098 nononame(const char *name)
1102 p = strrchr(name, '.');
1103 if (strncmp(p ? p + 1 : name, "no", 2)) {
1104 snprintf(jail_errmsg, sizeof(jail_errmsg),
1105 "mismatched boolean: %s", name);
1109 nname = malloc(strlen(name) - 1);
1110 if (nname == NULL) {
1111 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1115 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1117 strcpy(nname, name + 2);