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 = reallocarray(jp, nlist, sizeof(*jp));
255 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
259 jp = reallocarray(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;
518 * Load filesystem modules associated with allow.mount
519 * permissions. Ignore failure, since the module may
520 * be static, and even a failure to load is not a jail
523 if (strncmp(jp[j].jp_name, "allow.mount.", 12) == 0) {
524 if (kldload(jp[j].jp_name + 12) < 0 &&
526 strncmp(jp[j].jp_name + 12, "no", 2) == 0)
527 (void)kldload(jp[j].jp_name + 14);
531 * Try to fill in missing values with an empty string.
533 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
534 jailparam_import(jp + j, "") < 0) {
539 jiov[i].iov_base = jp[j].jp_value;
541 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
542 ? strlen(jp[j].jp_value) + 1
547 jiov[i].iov_base = __DECONST(char *, "errmsg");
548 jiov[i].iov_len = sizeof("errmsg");
550 jiov[i].iov_base = jail_errmsg;
551 jiov[i].iov_len = JAIL_ERRMSGLEN;
554 jid = jail_set(jiov, i, flags);
555 if (jid < 0 && !jail_errmsg[0])
556 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
560 for (j = 0; j < njp; j++)
561 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
562 jp[j].jp_value != NULL &&
563 jp[j].jp_valuelen == sizeof(int) &&
564 !*(int *)jp[j].jp_value)
565 free(jiov[j * 2].iov_base);
570 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
573 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
574 int i, ai, ki, jid, arrays, sanity;
578 * Get the types for all parameters.
579 * Find the key and any array parameters.
581 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
582 jp_lastjid = jp_jid = jp_name = NULL;
584 for (ai = j = 0; j < njp; j++) {
585 if (!strcmp(jp[j].jp_name, "lastjid"))
587 else if (!strcmp(jp[j].jp_name, "jid"))
589 else if (!strcmp(jp[j].jp_name, "name"))
591 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
593 jiov[ai].iov_base = jp[j].jp_name;
594 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
596 jiov[ai].iov_base = NULL;
597 jiov[ai].iov_len = 0;
601 jp_key = jp_lastjid ? jp_lastjid :
602 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
603 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
604 if (jp_key == NULL || jp_key->jp_value == NULL) {
605 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
610 jiov[ki].iov_base = jp_key->jp_name;
611 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
613 jiov[ki].iov_base = jp_key->jp_value;
614 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
615 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
617 jiov[ki].iov_base = __DECONST(char *, "errmsg");
618 jiov[ki].iov_len = sizeof("errmsg");
620 jiov[ki].iov_base = jail_errmsg;
621 jiov[ki].iov_len = JAIL_ERRMSGLEN;
624 if (arrays && jail_get(jiov, ki, flags) < 0) {
626 snprintf(jail_errmsg, sizeof(jail_errmsg),
627 "jail_get: %s", strerror(errno));
630 /* Allocate storage for all parameters. */
631 for (ai = j = 0, i = ki; j < njp; j++) {
632 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
634 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
635 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
636 jiov[ai].iov_len = jp[j].jp_valuelen;
638 jp[j].jp_valuelen = jiov[ai].iov_len;
639 if (jp[j].jp_value != NULL)
640 free(jp[j].jp_value);
641 jp[j].jp_value = malloc(jp[j].jp_valuelen);
642 if (jp[j].jp_value == NULL) {
643 strerror_r(errno, jail_errmsg,
648 jiov[ai].iov_base = jp[j].jp_value;
649 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
651 } else if (jp + j != jp_key) {
652 jiov[i].iov_base = jp[j].jp_name;
653 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
655 if (jp[j].jp_value == NULL &&
656 !(jp[j].jp_flags & JP_RAWVALUE)) {
657 jp[j].jp_value = malloc(jp[j].jp_valuelen);
658 if (jp[j].jp_value == NULL) {
659 strerror_r(errno, jail_errmsg,
664 jiov[i].iov_base = jp[j].jp_value;
665 jiov[i].iov_len = jp[j].jp_valuelen;
666 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
671 * Get the prison. If there are array elements, retry a few times
672 * in case their sizes changed from under us.
674 for (sanity = 0;; sanity++) {
675 jid = jail_get(jiov, i, flags);
676 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
677 errno != EINVAL || jail_errmsg[0])
679 for (ai = j = 0; j < njp; j++) {
680 if (jp[j].jp_elemlen &&
681 !(jp[j].jp_flags & JP_RAWVALUE)) {
683 jiov[ai].iov_base = NULL;
684 jiov[ai].iov_len = 0;
688 if (jail_get(jiov, ki, flags) < 0)
690 for (ai = j = 0; j < njp; j++) {
691 if (jp[j].jp_elemlen &&
692 !(jp[j].jp_flags & JP_RAWVALUE)) {
695 jp[j].jp_elemlen * ARRAY_SLOP;
696 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
697 jiov[ai].iov_len = jp[j].jp_valuelen;
699 jp[j].jp_valuelen = jiov[ai].iov_len;
700 if (jp[j].jp_value != NULL)
701 free(jp[j].jp_value);
703 malloc(jiov[ai].iov_len);
704 if (jp[j].jp_value == NULL) {
705 strerror_r(errno, jail_errmsg,
710 jiov[ai].iov_base = jp[j].jp_value;
711 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
716 if (jid < 0 && !jail_errmsg[0])
717 snprintf(jail_errmsg, sizeof(jail_errmsg),
718 "jail_get: %s", strerror(errno));
719 for (ai = j = 0, i = ki; j < njp; j++) {
720 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
722 jp[j].jp_valuelen = jiov[ai].iov_len;
724 } else if (jp + j != jp_key) {
726 jp[j].jp_valuelen = jiov[i].iov_len;
734 * Convert a jail parameter's value to external form.
737 jailparam_export(struct jailparam *jp)
740 char *value, *tvalue, **values;
743 char valbuf[INET6_ADDRSTRLEN];
745 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
746 value = strdup(jp->jp_value);
748 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
751 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
755 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
758 values = alloca(nval * sizeof(char *));
759 valuelens = alloca(nval * sizeof(size_t));
761 for (i = 0; i < nval; i++) {
762 switch (jp->jp_ctltype & CTLTYPE) {
764 ival = ((int *)jp->jp_value)[i];
765 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
766 (unsigned)ival < 2) {
767 strlcpy(valbuf, bool_values[ival],
771 if ((jp->jp_flags & JP_JAILSYS) &&
772 (unsigned)ival < sizeof(jailsys_values) /
773 sizeof(jailsys_values[0])) {
774 strlcpy(valbuf, jailsys_values[ival],
778 snprintf(valbuf, sizeof(valbuf), "%d", ival);
781 snprintf(valbuf, sizeof(valbuf), "%u",
782 ((unsigned *)jp->jp_value)[i]);
785 snprintf(valbuf, sizeof(valbuf), "%ld",
786 ((long *)jp->jp_value)[i]);
789 snprintf(valbuf, sizeof(valbuf), "%lu",
790 ((unsigned long *)jp->jp_value)[i]);
793 snprintf(valbuf, sizeof(valbuf), "%jd",
794 (intmax_t)((int64_t *)jp->jp_value)[i]);
797 snprintf(valbuf, sizeof(valbuf), "%ju",
798 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
801 switch (jp->jp_structtype) {
803 if (inet_ntop(AF_INET,
804 &((struct in_addr *)jp->jp_value)[i],
805 valbuf, sizeof(valbuf)) == NULL) {
806 strerror_r(errno, jail_errmsg,
812 if (inet_ntop(AF_INET6,
813 &((struct in6_addr *)jp->jp_value)[i],
814 valbuf, sizeof(valbuf)) == NULL) {
815 strerror_r(errno, jail_errmsg,
826 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
827 "unknown type for %s", jp->jp_name);
831 valuelens[i] = strlen(valbuf) + 1;
832 valuelen += valuelens[i];
833 values[i] = alloca(valuelens[i]);
834 strcpy(values[i], valbuf);
836 value = malloc(valuelen);
838 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
841 for (i = 0; i < nval; i++) {
842 strcpy(tvalue, values[i]);
844 tvalue += valuelens[i];
853 * Free the contents of a jail parameter list (but not the list itself).
856 jailparam_free(struct jailparam *jp, unsigned njp)
860 for (j = 0; j < njp; j++) {
862 if (!(jp[j].jp_flags & JP_RAWVALUE))
863 free(jp[j].jp_value);
868 * Find a parameter's type and size from its MIB.
871 jailparam_type(struct jailparam *jp)
873 char *p, *name, *nname;
874 size_t miblen, desclen;
880 int mib[CTL_MAXNAME];
882 /* The "lastjid" parameter isn't real. */
884 if (!strcmp(name, "lastjid")) {
885 jp->jp_valuelen = sizeof(int);
886 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
890 /* Find the sysctl that describes the parameter. */
893 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
894 miblen = sizeof(mib) - 2 * sizeof(int);
895 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
896 if (errno != ENOENT) {
897 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
898 "sysctl(0.3.%s): %s", name, strerror(errno));
901 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
902 desc.s, strlen(desc.s)) >= 0)
905 * The parameter probably doesn't exist. But it might be
906 * the "no" counterpart to a boolean.
908 nname = nononame(name);
911 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
912 "unknown parameter: %s", jp->jp_name);
916 name = alloca(strlen(nname) + 1);
919 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
920 miblen = sizeof(mib) - 2 * sizeof(int);
921 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
923 goto unknown_parameter;
924 jp->jp_flags |= JP_NOBOOL;
928 desclen = sizeof(desc);
929 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
931 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
932 "sysctl(0.4.%s): %s", name, strerror(errno));
935 jp->jp_ctltype = desc.i;
936 /* If this came from removing a "no", it better be a boolean. */
937 if (jp->jp_flags & JP_NOBOOL) {
938 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
939 jp->jp_valuelen = sizeof(int);
942 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
943 goto unknown_parameter;
945 /* See if this is an array type. */
946 p = strchr(desc.s, '\0');
948 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
954 /* Look for types we understand. */
955 switch (desc.i & CTLTYPE) {
957 if (desc.s[0] == 'B')
958 jp->jp_flags |= JP_BOOL;
959 else if (!strcmp(desc.s, "E,jailsys"))
960 jp->jp_flags |= JP_JAILSYS;
962 jp->jp_valuelen = sizeof(int);
966 jp->jp_valuelen = sizeof(long);
970 jp->jp_valuelen = sizeof(int64_t);
974 desclen = sizeof(desc.s);
975 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
977 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
978 "sysctl(" SJPARAM ".%s): %s", name,
982 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
985 if (!strcmp(desc.s, "S,in_addr")) {
986 jp->jp_structtype = JPS_IN_ADDR;
987 jp->jp_valuelen = sizeof(struct in_addr);
988 } else if (!strcmp(desc.s, "S,in6_addr")) {
989 jp->jp_structtype = JPS_IN6_ADDR;
990 jp->jp_valuelen = sizeof(struct in6_addr);
993 if (sysctl(mib + 2, miblen / sizeof(int),
994 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
995 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
996 "sysctl(" SJPARAM ".%s): %s", name,
1004 * A node might be described by an empty-named child,
1005 * which would be immediately before or after the node itself.
1008 miblen += sizeof(int);
1009 for (i = -1; i <= 1; i += 2) {
1010 mib[(miblen / sizeof(int)) + 1] =
1011 mib[(miblen / sizeof(int))] + i;
1012 desclen = sizeof(desc.s);
1013 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1014 &desclen, NULL, 0) < 0) {
1015 if (errno == ENOENT)
1017 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1018 "sysctl(0.1): %s", strerror(errno));
1021 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1022 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1023 memcmp(name, desc.s + sizeof(SJPARAM),
1024 desclen - sizeof(SJPARAM) - 2) == 0 &&
1025 desc.s[desclen - 2] == '.')
1028 goto unknown_parameter;
1030 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1031 "unknown type for %s", jp->jp_name);
1036 jp->jp_elemlen = jp->jp_valuelen;
1037 jp->jp_valuelen = 0;
1043 * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1046 kldload_param(const char *name)
1050 if (strcmp(name, "linux") == 0 || strncmp(name, "linux.", 6) == 0)
1051 kl = kldload("linux");
1052 else if (strcmp(name, "sysvmsg") == 0 || strcmp(name, "sysvsem") == 0 ||
1053 strcmp(name, "sysvshm") == 0)
1059 if (kl < 0 && errno == EEXIST) {
1061 * In the module is already loaded, then it must not contain
1070 * Change a boolean parameter name into its "no" counterpart or vice versa.
1073 noname(const char *name)
1077 nname = malloc(strlen(name) + 3);
1078 if (nname == NULL) {
1079 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1082 p = strrchr(name, '.');
1084 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1086 sprintf(nname, "no%s", name);
1091 nononame(const char *name)
1095 p = strrchr(name, '.');
1096 if (strncmp(p ? p + 1 : name, "no", 2)) {
1097 snprintf(jail_errmsg, sizeof(jail_errmsg),
1098 "mismatched boolean: %s", name);
1102 nname = malloc(strlen(name) - 1);
1103 if (nname == NULL) {
1104 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1108 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1110 strcpy(nname, name + 2);