2 * Copyright (c) 2009 James Gritton.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #include <sys/linker.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
49 #define SJPARAM "security.jail.param"
52 #define JPS_IN6_ADDR 2
54 #define ARRAY_SANITY 5
58 static int jailparam_import_enum(const char **values, int nvalues,
59 const char *valstr, size_t valsize, int *value);
60 static int jailparam_type(struct jailparam *jp);
61 static int kldload_param(const char *name);
62 static char *noname(const char *name);
63 static char *nononame(const char *name);
65 char jail_errmsg[JAIL_ERRMSGLEN];
67 static const char *bool_values[] = { "false", "true" };
68 static const char *jailsys_values[] = { "disable", "new", "inherit" };
72 * Import a null-terminated parameter list and set a jail with the flags
76 jail_setv(int flags, ...)
80 const char *name, *value;
83 /* Create the parameter list and import the parameters. */
86 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
87 (void)va_arg(tap, char *);
89 jp = alloca(njp * sizeof(struct jailparam));
90 for (njp = 0; (name = va_arg(ap, char *)) != NULL;) {
91 value = va_arg(ap, char *);
92 if (jailparam_init(jp + njp, name) < 0)
94 if (jailparam_import(jp + njp++, value) < 0)
98 jid = jailparam_set(jp, njp, flags);
99 jailparam_free(jp, njp);
103 jailparam_free(jp, njp);
109 * Read a null-terminated parameter list, get the referenced jail, and export
110 * the parameters to the list.
113 jail_getv(int flags, ...)
116 struct jailparam *jp, *jp_lastjid, *jp_jid, *jp_name, *jp_key;
117 char *valarg, *value;
118 const char *name, *key_value, *lastjid_value, *jid_value, *name_value;
121 /* Create the parameter list and find the key. */
124 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
125 (void)va_arg(tap, char *);
128 jp = alloca(njp * sizeof(struct jailparam));
130 jp_lastjid = jp_jid = jp_name = NULL;
131 lastjid_value = jid_value = name_value = NULL;
132 for (njp = 0; (name = va_arg(tap, char *)) != NULL; njp++) {
133 value = va_arg(tap, char *);
134 if (jailparam_init(jp + njp, name) < 0) {
138 if (!strcmp(jp[njp].jp_name, "lastjid")) {
139 jp_lastjid = jp + njp;
140 lastjid_value = value;
141 } else if (!strcmp(jp[njp].jp_name, "jid")) {
144 } if (!strcmp(jp[njp].jp_name, "name")) {
150 /* Import the key parameter. */
151 if (jp_lastjid != NULL) {
153 key_value = lastjid_value;
154 } else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) {
156 key_value = jid_value;
157 } else if (jp_name != NULL) {
159 key_value = name_value;
161 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
165 if (jailparam_import(jp_key, key_value) < 0)
167 /* Get the jail and export the parameters. */
168 jid = jailparam_get(jp, njp, flags);
171 for (i = 0; i < njp; i++) {
172 (void)va_arg(ap, char *);
173 valarg = va_arg(ap, char *);
174 if (jp + i != jp_key) {
175 /* It's up to the caller to ensure there's room. */
176 if ((jp[i].jp_ctltype & CTLTYPE) == CTLTYPE_STRING)
177 strcpy(valarg, jp[i].jp_value);
179 value = jailparam_export(jp + i);
182 strcpy(valarg, value);
187 jailparam_free(jp, njp);
192 jailparam_free(jp, njp);
198 * Return a list of all known parameters.
201 jailparam_all(struct jailparam **jpp)
203 struct jailparam *jp, *tjp;
204 size_t mlen1, mlen2, buflen;
206 int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2];
207 char buf[MAXPATHLEN];
211 jp = malloc(nlist * sizeof(*jp));
213 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
218 mlen1 = CTL_MAXNAME - 2;
219 if (sysctlnametomib(SJPARAM, mib1 + 2, &mlen1) < 0) {
220 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
221 "sysctlnametomib(" SJPARAM "): %s", strerror(errno));
225 /* Get the next parameter. */
226 mlen2 = sizeof(mib2);
227 if (sysctl(mib1, mlen1 + 2, mib2, &mlen2, NULL, 0) < 0) {
228 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
229 "sysctl(0.2): %s", strerror(errno));
232 if (mib2[0] != mib1[2] || mib2[1] != mib1[3] ||
235 /* Convert it to an ascii name. */
236 memcpy(mib1 + 2, mib2, mlen2);
237 mlen1 = mlen2 / sizeof(int);
239 buflen = sizeof(buf);
240 if (sysctl(mib1, mlen1 + 2, buf, &buflen, NULL, 0) < 0) {
241 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
242 "sysctl(0.1): %s", strerror(errno));
245 if (buf[buflen - 2] == '.')
246 buf[buflen - 2] = '\0';
247 /* Add the parameter to the list */
250 tjp = realloc(jp, nlist * sizeof(*jp));
255 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
259 jp = realloc(jp, njp * sizeof(*jp));
264 jailparam_free(jp, njp);
270 * Clear a jail parameter and copy in its name.
273 jailparam_init(struct jailparam *jp, const char *name)
276 memset(jp, 0, sizeof(*jp));
277 jp->jp_name = strdup(name);
278 if (jp->jp_name == NULL) {
279 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
282 if (jailparam_type(jp) < 0) {
283 jailparam_free(jp, 1);
292 * Put a name and value into a jail parameter element, converting the value
296 jailparam_import(struct jailparam *jp, const char *value)
298 char *p, *ep, *tvalue;
304 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
305 jp->jp_value = strdup(value);
306 if (jp->jp_value == NULL) {
307 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
313 if (jp->jp_elemlen) {
314 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
315 jp->jp_value = strdup("");
316 if (jp->jp_value == NULL) {
317 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
323 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
325 jp->jp_valuelen = jp->jp_elemlen * nval;
327 jp->jp_value = malloc(jp->jp_valuelen);
328 if (jp->jp_value == NULL) {
329 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
333 for (i = 0; i < nval; i++) {
334 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
335 switch (jp->jp_ctltype & CTLTYPE) {
337 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
338 if (!jailparam_import_enum(bool_values, 2,
339 avalue, fw, &((int *)jp->jp_value)[i])) {
340 snprintf(jail_errmsg,
341 JAIL_ERRMSGLEN, "%s: "
342 "unknown boolean value \"%.*s\"",
343 jp->jp_name, fw, avalue);
349 if (jp->jp_flags & JP_JAILSYS) {
351 * Allow setting a jailsys parameter to "new"
352 * in a booleanesque fashion.
354 if (value[0] == '\0')
355 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
356 else if (!jailparam_import_enum(jailsys_values,
357 sizeof(jailsys_values) /
358 sizeof(jailsys_values[0]), avalue, fw,
359 &((int *)jp->jp_value)[i])) {
360 snprintf(jail_errmsg,
361 JAIL_ERRMSGLEN, "%s: "
362 "unknown jailsys value \"%.*s\"",
363 jp->jp_name, fw, avalue);
369 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
371 if (ep != avalue + fw) {
372 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
373 "%s: non-integer value \"%.*s\"",
374 jp->jp_name, fw, avalue);
380 ((unsigned *)jp->jp_value)[i] =
381 strtoul(avalue, &ep, 10);
384 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
387 ((unsigned long *)jp->jp_value)[i] =
388 strtoul(avalue, &ep, 10);
391 ((int64_t *)jp->jp_value)[i] =
392 strtoimax(avalue, &ep, 10);
395 ((uint64_t *)jp->jp_value)[i] =
396 strtoumax(avalue, &ep, 10);
399 tvalue = alloca(fw + 1);
400 strlcpy(tvalue, avalue, fw + 1);
401 switch (jp->jp_structtype) {
403 if (inet_pton(AF_INET, tvalue,
404 &((struct in_addr *)jp->jp_value)[i]) != 1)
406 snprintf(jail_errmsg,
408 "%s: not an IPv4 address: %s",
409 jp->jp_name, tvalue);
415 if (inet_pton(AF_INET6, tvalue,
416 &((struct in6_addr *)jp->jp_value)[i]) != 1)
418 snprintf(jail_errmsg,
420 "%s: not an IPv6 address: %s",
421 jp->jp_name, tvalue);
432 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
433 "unknown type for %s", jp->jp_name);
448 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
449 size_t valsize, int *value)
454 for (i = 0; i < nvalues; i++)
455 if (valsize == strlen(values[i]) &&
456 !strncasecmp(valstr, values[i], valsize)) {
460 *value = strtol(valstr, &ep, 10);
461 return (ep == valstr + valsize);
465 * Put a name and value into a jail parameter element, copying the value
466 * but not altering it.
469 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
472 jp->jp_value = value;
473 jp->jp_valuelen = valuelen;
474 jp->jp_flags |= JP_RAWVALUE;
479 * Run the jail_set and jail_get system calls on a parameter list.
482 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
489 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
491 for (i = j = 0; j < njp; j++) {
492 jiov[i].iov_base = jp[j].jp_name;
493 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
495 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
497 * Set booleans without values. If one has a value of
498 * zero, change it to (or from) its "no" counterpart.
500 jiov[i].iov_base = NULL;
502 if (jp[j].jp_value != NULL &&
503 jp[j].jp_valuelen == sizeof(int) &&
504 !*(int *)jp[j].jp_value) {
506 nname = jp[j].jp_flags & JP_BOOL
507 ? noname(jp[j].jp_name)
508 : nononame(jp[j].jp_name);
514 jiov[i - 1].iov_base = nname;
515 jiov[i - 1].iov_len = strlen(nname) + 1;
520 * Try to fill in missing values with an empty string.
522 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
523 jailparam_import(jp + j, "") < 0) {
528 jiov[i].iov_base = jp[j].jp_value;
530 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
531 ? strlen(jp[j].jp_value) + 1
536 *(const void **)&jiov[i].iov_base = "errmsg";
537 jiov[i].iov_len = sizeof("errmsg");
539 jiov[i].iov_base = jail_errmsg;
540 jiov[i].iov_len = JAIL_ERRMSGLEN;
543 jid = jail_set(jiov, i, flags);
544 if (jid < 0 && !jail_errmsg[0])
545 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
549 for (j = 0; j < njp; j++)
550 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
551 jp[j].jp_value != NULL &&
552 jp[j].jp_valuelen == sizeof(int) &&
553 !*(int *)jp[j].jp_value)
554 free(jiov[j * 2].iov_base);
559 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
562 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
563 int i, ai, ki, jid, arrays, sanity;
567 * Get the types for all parameters.
568 * Find the key and any array parameters.
570 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
571 jp_lastjid = jp_jid = jp_name = NULL;
573 for (ai = j = 0; j < njp; j++) {
574 if (!strcmp(jp[j].jp_name, "lastjid"))
576 else if (!strcmp(jp[j].jp_name, "jid"))
578 else if (!strcmp(jp[j].jp_name, "name"))
580 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
582 jiov[ai].iov_base = jp[j].jp_name;
583 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
585 jiov[ai].iov_base = NULL;
586 jiov[ai].iov_len = 0;
590 jp_key = jp_lastjid ? jp_lastjid :
591 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
592 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
593 if (jp_key == NULL || jp_key->jp_value == NULL) {
594 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
599 jiov[ki].iov_base = jp_key->jp_name;
600 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
602 jiov[ki].iov_base = jp_key->jp_value;
603 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
604 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
606 *(const void **)&jiov[ki].iov_base = "errmsg";
607 jiov[ki].iov_len = sizeof("errmsg");
609 jiov[ki].iov_base = jail_errmsg;
610 jiov[ki].iov_len = JAIL_ERRMSGLEN;
613 if (arrays && jail_get(jiov, ki, flags) < 0) {
615 snprintf(jail_errmsg, sizeof(jail_errmsg),
616 "jail_get: %s", strerror(errno));
619 /* Allocate storage for all parameters. */
620 for (ai = j = 0, i = ki; j < njp; j++) {
621 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
623 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
624 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
625 jiov[ai].iov_len = jp[j].jp_valuelen;
627 jp[j].jp_valuelen = jiov[ai].iov_len;
628 if (jp[j].jp_value != NULL)
629 free(jp[j].jp_value);
630 jp[j].jp_value = malloc(jp[j].jp_valuelen);
631 if (jp[j].jp_value == NULL) {
632 strerror_r(errno, jail_errmsg,
637 jiov[ai].iov_base = jp[j].jp_value;
638 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
640 } else if (jp + j != jp_key) {
641 jiov[i].iov_base = jp[j].jp_name;
642 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
644 if (jp[j].jp_value == NULL &&
645 !(jp[j].jp_flags & JP_RAWVALUE)) {
646 jp[j].jp_value = malloc(jp[j].jp_valuelen);
647 if (jp[j].jp_value == NULL) {
648 strerror_r(errno, jail_errmsg,
653 jiov[i].iov_base = jp[j].jp_value;
654 jiov[i].iov_len = jp[j].jp_valuelen;
655 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
660 * Get the prison. If there are array elements, retry a few times
661 * in case their sizes changed from under us.
663 for (sanity = 0;; sanity++) {
664 jid = jail_get(jiov, i, flags);
665 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
666 errno != EINVAL || jail_errmsg[0])
668 for (ai = j = 0; j < njp; j++) {
669 if (jp[j].jp_elemlen &&
670 !(jp[j].jp_flags & JP_RAWVALUE)) {
672 jiov[ai].iov_base = NULL;
673 jiov[ai].iov_len = 0;
677 if (jail_get(jiov, ki, flags) < 0)
679 for (ai = j = 0; j < njp; j++) {
680 if (jp[j].jp_elemlen &&
681 !(jp[j].jp_flags & JP_RAWVALUE)) {
684 jp[j].jp_elemlen * ARRAY_SLOP;
685 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
686 jiov[ai].iov_len = jp[j].jp_valuelen;
688 jp[j].jp_valuelen = jiov[ai].iov_len;
689 if (jp[j].jp_value != NULL)
690 free(jp[j].jp_value);
692 malloc(jiov[ai].iov_len);
693 if (jp[j].jp_value == NULL) {
694 strerror_r(errno, jail_errmsg,
699 jiov[ai].iov_base = jp[j].jp_value;
700 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
705 if (jid < 0 && !jail_errmsg[0])
706 snprintf(jail_errmsg, sizeof(jail_errmsg),
707 "jail_get: %s", strerror(errno));
708 for (ai = j = 0, i = ki; j < njp; j++) {
709 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
711 jp[j].jp_valuelen = jiov[ai].iov_len;
713 } else if (jp + j != jp_key) {
715 jp[j].jp_valuelen = jiov[i].iov_len;
723 * Convert a jail parameter's value to external form.
726 jailparam_export(struct jailparam *jp)
729 char *value, *tvalue, **values;
732 char valbuf[INET6_ADDRSTRLEN];
734 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
735 value = strdup(jp->jp_value);
737 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
740 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
744 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
747 values = alloca(nval * sizeof(char *));
748 valuelens = alloca(nval * sizeof(size_t));
750 for (i = 0; i < nval; i++) {
751 switch (jp->jp_ctltype & CTLTYPE) {
753 ival = ((int *)jp->jp_value)[i];
754 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
755 (unsigned)ival < 2) {
756 strlcpy(valbuf, bool_values[ival],
760 if ((jp->jp_flags & JP_JAILSYS) &&
761 (unsigned)ival < sizeof(jailsys_values) /
762 sizeof(jailsys_values[0])) {
763 strlcpy(valbuf, jailsys_values[ival],
767 snprintf(valbuf, sizeof(valbuf), "%d", ival);
770 snprintf(valbuf, sizeof(valbuf), "%u",
771 ((unsigned *)jp->jp_value)[i]);
774 snprintf(valbuf, sizeof(valbuf), "%ld",
775 ((long *)jp->jp_value)[i]);
778 snprintf(valbuf, sizeof(valbuf), "%lu",
779 ((unsigned long *)jp->jp_value)[i]);
782 snprintf(valbuf, sizeof(valbuf), "%jd",
783 (intmax_t)((int64_t *)jp->jp_value)[i]);
786 snprintf(valbuf, sizeof(valbuf), "%ju",
787 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
790 switch (jp->jp_structtype) {
792 if (inet_ntop(AF_INET,
793 &((struct in_addr *)jp->jp_value)[i],
794 valbuf, sizeof(valbuf)) == NULL) {
795 strerror_r(errno, jail_errmsg,
801 if (inet_ntop(AF_INET6,
802 &((struct in6_addr *)jp->jp_value)[i],
803 valbuf, sizeof(valbuf)) == NULL) {
804 strerror_r(errno, jail_errmsg,
815 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
816 "unknown type for %s", jp->jp_name);
820 valuelens[i] = strlen(valbuf) + 1;
821 valuelen += valuelens[i];
822 values[i] = alloca(valuelens[i]);
823 strcpy(values[i], valbuf);
825 value = malloc(valuelen);
827 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
830 for (i = 0; i < nval; i++) {
831 strcpy(tvalue, values[i]);
833 tvalue += valuelens[i];
842 * Free the contents of a jail parameter list (but not the list itself).
845 jailparam_free(struct jailparam *jp, unsigned njp)
849 for (j = 0; j < njp; j++) {
851 if (!(jp[j].jp_flags & JP_RAWVALUE))
852 free(jp[j].jp_value);
857 * Find a parameter's type and size from its MIB.
860 jailparam_type(struct jailparam *jp)
862 char *p, *name, *nname;
863 size_t miblen, desclen;
869 int mib[CTL_MAXNAME];
871 /* The "lastjid" parameter isn't real. */
873 if (!strcmp(name, "lastjid")) {
874 jp->jp_valuelen = sizeof(int);
875 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
879 /* Find the sysctl that describes the parameter. */
882 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
883 miblen = sizeof(mib) - 2 * sizeof(int);
884 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
885 if (errno != ENOENT) {
886 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
887 "sysctl(0.3.%s): %s", name, strerror(errno));
890 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
891 desc.s, strlen(desc.s)) >= 0)
894 * The parameter probably doesn't exist. But it might be
895 * the "no" counterpart to a boolean.
897 nname = nononame(name);
900 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
901 "unknown parameter: %s", jp->jp_name);
905 name = alloca(strlen(nname) + 1);
908 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
909 miblen = sizeof(mib) - 2 * sizeof(int);
910 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
912 goto unknown_parameter;
913 jp->jp_flags |= JP_NOBOOL;
917 desclen = sizeof(desc);
918 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
920 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
921 "sysctl(0.4.%s): %s", name, strerror(errno));
924 jp->jp_ctltype = desc.i;
925 /* If this came from removing a "no", it better be a boolean. */
926 if (jp->jp_flags & JP_NOBOOL) {
927 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
928 jp->jp_valuelen = sizeof(int);
931 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
932 goto unknown_parameter;
934 /* See if this is an array type. */
935 p = strchr(desc.s, '\0');
937 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
943 /* Look for types we understand. */
944 switch (desc.i & CTLTYPE) {
946 if (desc.s[0] == 'B')
947 jp->jp_flags |= JP_BOOL;
948 else if (!strcmp(desc.s, "E,jailsys"))
949 jp->jp_flags |= JP_JAILSYS;
951 jp->jp_valuelen = sizeof(int);
955 jp->jp_valuelen = sizeof(long);
959 jp->jp_valuelen = sizeof(int64_t);
963 desclen = sizeof(desc.s);
964 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
966 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
967 "sysctl(" SJPARAM ".%s): %s", name,
971 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
974 if (!strcmp(desc.s, "S,in_addr")) {
975 jp->jp_structtype = JPS_IN_ADDR;
976 jp->jp_valuelen = sizeof(struct in_addr);
977 } else if (!strcmp(desc.s, "S,in6_addr")) {
978 jp->jp_structtype = JPS_IN6_ADDR;
979 jp->jp_valuelen = sizeof(struct in6_addr);
982 if (sysctl(mib + 2, miblen / sizeof(int),
983 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
984 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
985 "sysctl(" SJPARAM ".%s): %s", name,
993 * A node might be described by an empty-named child,
994 * which would be immediately before or after the node itself.
997 miblen += sizeof(int);
998 for (i = -1; i <= 1; i += 2) {
999 mib[(miblen / sizeof(int)) + 1] =
1000 mib[(miblen / sizeof(int))] + i;
1001 desclen = sizeof(desc.s);
1002 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1003 &desclen, NULL, 0) < 0) {
1004 if (errno == ENOENT)
1006 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1007 "sysctl(0.1): %s", strerror(errno));
1010 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1011 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1012 memcmp(name, desc.s + sizeof(SJPARAM),
1013 desclen - sizeof(SJPARAM) - 2) == 0 &&
1014 desc.s[desclen - 2] == '.')
1017 goto unknown_parameter;
1019 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1020 "unknown type for %s", jp->jp_name);
1025 jp->jp_elemlen = jp->jp_valuelen;
1026 jp->jp_valuelen = 0;
1032 * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1035 kldload_param(const char *name)
1039 if (strcmp(name, "linux") == 0 || strncmp(name, "linux.", 6) == 0)
1040 kl = kldload("linux");
1041 else if (strcmp(name, "sysvmsg") == 0 || strcmp(name, "sysvsem") == 0 ||
1042 strcmp(name, "sysvshm") == 0)
1048 if (kl < 0 && errno == EEXIST) {
1050 * In the module is already loaded, then it must not contain
1059 * Change a boolean parameter name into its "no" counterpart or vice versa.
1062 noname(const char *name)
1066 nname = malloc(strlen(name) + 3);
1067 if (nname == NULL) {
1068 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1071 p = strrchr(name, '.');
1073 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1075 sprintf(nname, "no%s", name);
1080 nononame(const char *name)
1084 p = strrchr(name, '.');
1085 if (strncmp(p ? p + 1 : name, "no", 2)) {
1086 snprintf(jail_errmsg, sizeof(jail_errmsg),
1087 "mismatched boolean: %s", name);
1091 nname = malloc(strlen(name) - 1);
1092 if (nname == NULL) {
1093 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1097 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1099 strcpy(nname, name + 2);