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/socket.h>
34 #include <sys/sysctl.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
48 #define SJPARAM "security.jail.param"
51 #define JPS_IN6_ADDR 2
53 #define ARRAY_SANITY 5
57 static int jailparam_import_enum(const char **values
, int nvalues
,
58 const char *valstr
, size_t valsize
, int *value
);
59 static int jailparam_type(struct jailparam
*jp
);
60 static char *noname(const char *name
);
61 static char *nononame(const char *name
);
63 char jail_errmsg
[JAIL_ERRMSGLEN
];
65 static const char *bool_values
[] = { "false", "true" };
66 static const char *jailsys_values
[] = { "disable", "new", "inherit" };
70 * Import a null-terminated parameter list and set a jail with the flags
74 jail_setv(int flags
, ...)
78 const char *name
, *value
;
81 /* Create the parameter list and import the parameters. */
84 for (njp
= 0; va_arg(tap
, char *) != NULL
; njp
++)
85 (void)va_arg(tap
, char *);
87 jp
= alloca(njp
* sizeof(struct jailparam
));
88 for (njp
= 0; (name
= va_arg(ap
, char *)) != NULL
;) {
89 value
= va_arg(ap
, char *);
90 if (jailparam_init(jp
+ njp
, name
) < 0)
92 if (jailparam_import(jp
+ njp
++, value
) < 0)
96 jid
= jailparam_set(jp
, njp
, flags
);
97 jailparam_free(jp
, njp
);
101 jailparam_free(jp
, njp
);
107 * Read a null-terminated parameter list, get the referenced jail, and export
108 * the parameters to the list.
111 jail_getv(int flags
, ...)
114 struct jailparam
*jp
, *jp_lastjid
, *jp_jid
, *jp_name
, *jp_key
;
115 char *valarg
, *value
;
116 const char *name
, *key_value
, *lastjid_value
, *jid_value
, *name_value
;
119 /* Create the parameter list and find the key. */
122 for (njp
= 0; va_arg(tap
, char *) != NULL
; njp
++)
123 (void)va_arg(tap
, char *);
126 jp
= alloca(njp
* sizeof(struct jailparam
));
128 jp_lastjid
= jp_jid
= jp_name
= NULL
;
129 lastjid_value
= jid_value
= name_value
= NULL
;
130 for (njp
= 0; (name
= va_arg(tap
, char *)) != NULL
; njp
++) {
131 value
= va_arg(tap
, char *);
132 if (jailparam_init(jp
+ njp
, name
) < 0) {
136 if (!strcmp(jp
[njp
].jp_name
, "lastjid")) {
137 jp_lastjid
= jp
+ njp
;
138 lastjid_value
= value
;
139 } else if (!strcmp(jp
[njp
].jp_name
, "jid")) {
142 } if (!strcmp(jp
[njp
].jp_name
, "name")) {
148 /* Import the key parameter. */
149 if (jp_lastjid
!= NULL
) {
151 key_value
= lastjid_value
;
152 } else if (jp_jid
!= NULL
&& strtol(jid_value
, NULL
, 10) != 0) {
154 key_value
= jid_value
;
155 } else if (jp_name
!= NULL
) {
157 key_value
= name_value
;
159 strlcpy(jail_errmsg
, "no jail specified", JAIL_ERRMSGLEN
);
163 if (jailparam_import(jp_key
, key_value
) < 0)
165 /* Get the jail and export the parameters. */
166 jid
= jailparam_get(jp
, njp
, flags
);
169 for (i
= 0; i
< njp
; i
++) {
170 (void)va_arg(ap
, char *);
171 valarg
= va_arg(ap
, char *);
172 if (jp
+ i
!= jp_key
) {
173 /* It's up to the caller to ensure there's room. */
174 if ((jp
[i
].jp_ctltype
& CTLTYPE
) == CTLTYPE_STRING
)
175 strcpy(valarg
, jp
[i
].jp_value
);
177 value
= jailparam_export(jp
+ i
);
180 strcpy(valarg
, value
);
185 jailparam_free(jp
, njp
);
190 jailparam_free(jp
, njp
);
196 * Return a list of all known parameters.
199 jailparam_all(struct jailparam
**jpp
)
201 struct jailparam
*jp
, *tjp
;
202 size_t mlen1
, mlen2
, buflen
;
204 int mib1
[CTL_MAXNAME
], mib2
[CTL_MAXNAME
- 2];
205 char buf
[MAXPATHLEN
];
209 jp
= malloc(nlist
* sizeof(*jp
));
211 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
216 mlen1
= CTL_MAXNAME
- 2;
217 if (sysctlnametomib(SJPARAM
, mib1
+ 2, &mlen1
) < 0) {
218 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
219 "sysctlnametomib(" SJPARAM
"): %s", strerror(errno
));
223 /* Get the next parameter. */
224 mlen2
= sizeof(mib2
);
225 if (sysctl(mib1
, mlen1
+ 2, mib2
, &mlen2
, NULL
, 0) < 0) {
226 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
227 "sysctl(0.2): %s", strerror(errno
));
230 if (mib2
[0] != mib1
[2] || mib2
[1] != mib1
[3] ||
233 /* Convert it to an ascii name. */
234 memcpy(mib1
+ 2, mib2
, mlen2
);
235 mlen1
= mlen2
/ sizeof(int);
237 buflen
= sizeof(buf
);
238 if (sysctl(mib1
, mlen1
+ 2, buf
, &buflen
, NULL
, 0) < 0) {
239 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
240 "sysctl(0.1): %s", strerror(errno
));
243 if (buf
[buflen
- 2] == '.')
244 buf
[buflen
- 2] = '\0';
245 /* Add the parameter to the list */
248 tjp
= realloc(jp
, nlist
* sizeof(*jp
));
253 if (jailparam_init(jp
+ njp
, buf
+ sizeof(SJPARAM
)) < 0)
257 jp
= realloc(jp
, njp
* sizeof(*jp
));
262 jailparam_free(jp
, njp
);
268 * Clear a jail parameter and copy in its name.
271 jailparam_init(struct jailparam
*jp
, const char *name
)
274 memset(jp
, 0, sizeof(*jp
));
275 jp
->jp_name
= strdup(name
);
276 if (jp
->jp_name
== NULL
) {
277 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
280 if (jailparam_type(jp
) < 0) {
281 jailparam_free(jp
, 1);
290 * Put a name and value into a jail parameter element, converting the value
294 jailparam_import(struct jailparam
*jp
, const char *value
)
296 char *p
, *ep
, *tvalue
;
302 if ((jp
->jp_ctltype
& CTLTYPE
) == CTLTYPE_STRING
) {
303 jp
->jp_value
= strdup(value
);
304 if (jp
->jp_value
== NULL
) {
305 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
311 if (jp
->jp_elemlen
) {
312 if (value
[0] == '\0' || (value
[0] == '-' && value
[1] == '\0')) {
313 jp
->jp_value
= strdup("");
314 if (jp
->jp_value
== NULL
) {
315 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
321 for (p
= strchr(value
, ','); p
; p
= strchr(p
+ 1, ','))
323 jp
->jp_valuelen
= jp
->jp_elemlen
* nval
;
325 jp
->jp_value
= malloc(jp
->jp_valuelen
);
326 if (jp
->jp_value
== NULL
) {
327 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
331 for (i
= 0; i
< nval
; i
++) {
332 fw
= nval
== 1 ? strlen(avalue
) : strcspn(avalue
, ",");
333 switch (jp
->jp_ctltype
& CTLTYPE
) {
335 if (jp
->jp_flags
& (JP_BOOL
| JP_NOBOOL
)) {
336 if (!jailparam_import_enum(bool_values
, 2,
337 avalue
, fw
, &((int *)jp
->jp_value
)[i
])) {
338 snprintf(jail_errmsg
,
339 JAIL_ERRMSGLEN
, "%s: "
340 "unknown boolean value \"%.*s\"",
341 jp
->jp_name
, fw
, avalue
);
347 if (jp
->jp_flags
& JP_JAILSYS
) {
349 * Allow setting a jailsys parameter to "new"
350 * in a booleanesque fashion.
352 if (value
[0] == '\0')
353 ((int *)jp
->jp_value
)[i
] = JAIL_SYS_NEW
;
354 else if (!jailparam_import_enum(jailsys_values
,
355 sizeof(jailsys_values
) /
356 sizeof(jailsys_values
[0]), avalue
, fw
,
357 &((int *)jp
->jp_value
)[i
])) {
358 snprintf(jail_errmsg
,
359 JAIL_ERRMSGLEN
, "%s: "
360 "unknown jailsys value \"%.*s\"",
361 jp
->jp_name
, fw
, avalue
);
367 ((int *)jp
->jp_value
)[i
] = strtol(avalue
, &ep
, 10);
369 if (ep
!= avalue
+ fw
) {
370 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
371 "%s: non-integer value \"%.*s\"",
372 jp
->jp_name
, fw
, avalue
);
378 ((unsigned *)jp
->jp_value
)[i
] =
379 strtoul(avalue
, &ep
, 10);
382 ((long *)jp
->jp_value
)[i
] = strtol(avalue
, &ep
, 10);
385 ((unsigned long *)jp
->jp_value
)[i
] =
386 strtoul(avalue
, &ep
, 10);
389 ((int64_t *)jp
->jp_value
)[i
] =
390 strtoimax(avalue
, &ep
, 10);
393 ((uint64_t *)jp
->jp_value
)[i
] =
394 strtoumax(avalue
, &ep
, 10);
397 tvalue
= alloca(fw
+ 1);
398 strlcpy(tvalue
, avalue
, fw
+ 1);
399 switch (jp
->jp_structtype
) {
401 if (inet_pton(AF_INET
, tvalue
,
402 &((struct in_addr
*)jp
->jp_value
)[i
]) != 1)
404 snprintf(jail_errmsg
,
406 "%s: not an IPv4 address: %s",
407 jp
->jp_name
, tvalue
);
413 if (inet_pton(AF_INET6
, tvalue
,
414 &((struct in6_addr
*)jp
->jp_value
)[i
]) != 1)
416 snprintf(jail_errmsg
,
418 "%s: not an IPv6 address: %s",
419 jp
->jp_name
, tvalue
);
430 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
431 "unknown type for %s", jp
->jp_name
);
446 jailparam_import_enum(const char **values
, int nvalues
, const char *valstr
,
447 size_t valsize
, int *value
)
452 for (i
= 0; i
< nvalues
; i
++)
453 if (valsize
== strlen(values
[i
]) &&
454 !strncasecmp(valstr
, values
[i
], valsize
)) {
458 *value
= strtol(valstr
, &ep
, 10);
459 return (ep
== valstr
+ valsize
);
463 * Put a name and value into a jail parameter element, copying the value
464 * but not altering it.
467 jailparam_import_raw(struct jailparam
*jp
, void *value
, size_t valuelen
)
470 jp
->jp_value
= value
;
471 jp
->jp_valuelen
= valuelen
;
472 jp
->jp_flags
|= JP_RAWVALUE
;
477 * Run the jail_set and jail_get system calls on a parameter list.
480 jailparam_set(struct jailparam
*jp
, unsigned njp
, int flags
)
487 jiov
= alloca(sizeof(struct iovec
) * 2 * (njp
+ 1));
489 for (i
= j
= 0; j
< njp
; j
++) {
490 jiov
[i
].iov_base
= jp
[j
].jp_name
;
491 jiov
[i
].iov_len
= strlen(jp
[j
].jp_name
) + 1;
493 if (jp
[j
].jp_flags
& (JP_BOOL
| JP_NOBOOL
)) {
495 * Set booleans without values. If one has a value of
496 * zero, change it to (or from) its "no" counterpart.
498 jiov
[i
].iov_base
= NULL
;
500 if (jp
[j
].jp_value
!= NULL
&&
501 jp
[j
].jp_valuelen
== sizeof(int) &&
502 !*(int *)jp
[j
].jp_value
) {
504 nname
= jp
[j
].jp_flags
& JP_BOOL
505 ? noname(jp
[j
].jp_name
)
506 : nononame(jp
[j
].jp_name
);
512 jiov
[i
- 1].iov_base
= nname
;
513 jiov
[i
- 1].iov_len
= strlen(nname
) + 1;
518 * Try to fill in missing values with an empty string.
520 if (jp
[j
].jp_value
== NULL
&& jp
[j
].jp_valuelen
> 0 &&
521 jailparam_import(jp
+ j
, "") < 0) {
526 jiov
[i
].iov_base
= jp
[j
].jp_value
;
528 (jp
[j
].jp_ctltype
& CTLTYPE
) == CTLTYPE_STRING
529 ? strlen(jp
[j
].jp_value
) + 1
534 jiov
[i
].iov_base
= __DECONST(char *, "errmsg");
535 jiov
[i
].iov_len
= sizeof("errmsg");
537 jiov
[i
].iov_base
= jail_errmsg
;
538 jiov
[i
].iov_len
= JAIL_ERRMSGLEN
;
541 jid
= jail_set(jiov
, i
, flags
);
542 if (jid
< 0 && !jail_errmsg
[0])
543 snprintf(jail_errmsg
, sizeof(jail_errmsg
), "jail_set: %s",
547 for (j
= 0; j
< njp
; j
++)
548 if ((jp
[j
].jp_flags
& (JP_BOOL
| JP_NOBOOL
)) &&
549 jp
[j
].jp_value
!= NULL
&&
550 jp
[j
].jp_valuelen
== sizeof(int) &&
551 !*(int *)jp
[j
].jp_value
)
552 free(jiov
[j
* 2].iov_base
);
557 jailparam_get(struct jailparam
*jp
, unsigned njp
, int flags
)
560 struct jailparam
*jp_lastjid
, *jp_jid
, *jp_name
, *jp_key
;
561 int i
, ai
, ki
, jid
, arrays
, sanity
;
565 * Get the types for all parameters.
566 * Find the key and any array parameters.
568 jiov
= alloca(sizeof(struct iovec
) * 2 * (njp
+ 1));
569 jp_lastjid
= jp_jid
= jp_name
= NULL
;
571 for (ai
= j
= 0; j
< njp
; j
++) {
572 if (!strcmp(jp
[j
].jp_name
, "lastjid"))
574 else if (!strcmp(jp
[j
].jp_name
, "jid"))
576 else if (!strcmp(jp
[j
].jp_name
, "name"))
578 else if (jp
[j
].jp_elemlen
&& !(jp
[j
].jp_flags
& JP_RAWVALUE
)) {
580 jiov
[ai
].iov_base
= jp
[j
].jp_name
;
581 jiov
[ai
].iov_len
= strlen(jp
[j
].jp_name
) + 1;
583 jiov
[ai
].iov_base
= NULL
;
584 jiov
[ai
].iov_len
= 0;
588 jp_key
= jp_lastjid
? jp_lastjid
:
589 jp_jid
&& jp_jid
->jp_valuelen
== sizeof(int) &&
590 jp_jid
->jp_value
&& *(int *)jp_jid
->jp_value
? jp_jid
: jp_name
;
591 if (jp_key
== NULL
|| jp_key
->jp_value
== NULL
) {
592 strlcpy(jail_errmsg
, "no jail specified", JAIL_ERRMSGLEN
);
597 jiov
[ki
].iov_base
= jp_key
->jp_name
;
598 jiov
[ki
].iov_len
= strlen(jp_key
->jp_name
) + 1;
600 jiov
[ki
].iov_base
= jp_key
->jp_value
;
601 jiov
[ki
].iov_len
= (jp_key
->jp_ctltype
& CTLTYPE
) == CTLTYPE_STRING
602 ? strlen(jp_key
->jp_value
) + 1 : jp_key
->jp_valuelen
;
604 jiov
[ki
].iov_base
= __DECONST(char *, "errmsg");
605 jiov
[ki
].iov_len
= sizeof("errmsg");
607 jiov
[ki
].iov_base
= jail_errmsg
;
608 jiov
[ki
].iov_len
= JAIL_ERRMSGLEN
;
611 if (arrays
&& jail_get(jiov
, ki
, flags
) < 0) {
613 snprintf(jail_errmsg
, sizeof(jail_errmsg
),
614 "jail_get: %s", strerror(errno
));
617 /* Allocate storage for all parameters. */
618 for (ai
= j
= 0, i
= ki
; j
< njp
; j
++) {
619 if (jp
[j
].jp_elemlen
&& !(jp
[j
].jp_flags
& JP_RAWVALUE
)) {
621 jiov
[ai
].iov_len
+= jp
[j
].jp_elemlen
* ARRAY_SLOP
;
622 if (jp
[j
].jp_valuelen
>= jiov
[ai
].iov_len
)
623 jiov
[ai
].iov_len
= jp
[j
].jp_valuelen
;
625 jp
[j
].jp_valuelen
= jiov
[ai
].iov_len
;
626 if (jp
[j
].jp_value
!= NULL
)
627 free(jp
[j
].jp_value
);
628 jp
[j
].jp_value
= malloc(jp
[j
].jp_valuelen
);
629 if (jp
[j
].jp_value
== NULL
) {
630 strerror_r(errno
, jail_errmsg
,
635 jiov
[ai
].iov_base
= jp
[j
].jp_value
;
636 memset(jiov
[ai
].iov_base
, 0, jiov
[ai
].iov_len
);
638 } else if (jp
+ j
!= jp_key
) {
639 jiov
[i
].iov_base
= jp
[j
].jp_name
;
640 jiov
[i
].iov_len
= strlen(jp
[j
].jp_name
) + 1;
642 if (jp
[j
].jp_value
== NULL
&&
643 !(jp
[j
].jp_flags
& JP_RAWVALUE
)) {
644 jp
[j
].jp_value
= malloc(jp
[j
].jp_valuelen
);
645 if (jp
[j
].jp_value
== NULL
) {
646 strerror_r(errno
, jail_errmsg
,
651 jiov
[i
].iov_base
= jp
[j
].jp_value
;
652 jiov
[i
].iov_len
= jp
[j
].jp_valuelen
;
653 memset(jiov
[i
].iov_base
, 0, jiov
[i
].iov_len
);
658 * Get the prison. If there are array elements, retry a few times
659 * in case their sizes changed from under us.
661 for (sanity
= 0;; sanity
++) {
662 jid
= jail_get(jiov
, i
, flags
);
663 if (jid
>= 0 || !arrays
|| sanity
== ARRAY_SANITY
||
664 errno
!= EINVAL
|| jail_errmsg
[0])
666 for (ai
= j
= 0; j
< njp
; j
++) {
667 if (jp
[j
].jp_elemlen
&&
668 !(jp
[j
].jp_flags
& JP_RAWVALUE
)) {
670 jiov
[ai
].iov_base
= NULL
;
671 jiov
[ai
].iov_len
= 0;
675 if (jail_get(jiov
, ki
, flags
) < 0)
677 for (ai
= j
= 0; j
< njp
; j
++) {
678 if (jp
[j
].jp_elemlen
&&
679 !(jp
[j
].jp_flags
& JP_RAWVALUE
)) {
682 jp
[j
].jp_elemlen
* ARRAY_SLOP
;
683 if (jp
[j
].jp_valuelen
>= jiov
[ai
].iov_len
)
684 jiov
[ai
].iov_len
= jp
[j
].jp_valuelen
;
686 jp
[j
].jp_valuelen
= jiov
[ai
].iov_len
;
687 if (jp
[j
].jp_value
!= NULL
)
688 free(jp
[j
].jp_value
);
690 malloc(jiov
[ai
].iov_len
);
691 if (jp
[j
].jp_value
== NULL
) {
692 strerror_r(errno
, jail_errmsg
,
697 jiov
[ai
].iov_base
= jp
[j
].jp_value
;
698 memset(jiov
[ai
].iov_base
, 0, jiov
[ai
].iov_len
);
703 if (jid
< 0 && !jail_errmsg
[0])
704 snprintf(jail_errmsg
, sizeof(jail_errmsg
),
705 "jail_get: %s", strerror(errno
));
706 for (ai
= j
= 0, i
= ki
; j
< njp
; j
++) {
707 if (jp
[j
].jp_elemlen
&& !(jp
[j
].jp_flags
& JP_RAWVALUE
)) {
709 jp
[j
].jp_valuelen
= jiov
[ai
].iov_len
;
711 } else if (jp
+ j
!= jp_key
) {
713 jp
[j
].jp_valuelen
= jiov
[i
].iov_len
;
721 * Convert a jail parameter's value to external form.
724 jailparam_export(struct jailparam
*jp
)
727 char *value
, *tvalue
, **values
;
730 char valbuf
[INET6_ADDRSTRLEN
];
732 if ((jp
->jp_ctltype
& CTLTYPE
) == CTLTYPE_STRING
) {
733 value
= strdup(jp
->jp_value
);
735 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
738 nval
= jp
->jp_elemlen
? jp
->jp_valuelen
/ jp
->jp_elemlen
: 1;
742 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
745 values
= alloca(nval
* sizeof(char *));
746 valuelens
= alloca(nval
* sizeof(size_t));
748 for (i
= 0; i
< nval
; i
++) {
749 switch (jp
->jp_ctltype
& CTLTYPE
) {
751 ival
= ((int *)jp
->jp_value
)[i
];
752 if ((jp
->jp_flags
& (JP_BOOL
| JP_NOBOOL
)) &&
753 (unsigned)ival
< 2) {
754 strlcpy(valbuf
, bool_values
[ival
],
758 if ((jp
->jp_flags
& JP_JAILSYS
) &&
759 (unsigned)ival
< sizeof(jailsys_values
) /
760 sizeof(jailsys_values
[0])) {
761 strlcpy(valbuf
, jailsys_values
[ival
],
765 snprintf(valbuf
, sizeof(valbuf
), "%d", ival
);
768 snprintf(valbuf
, sizeof(valbuf
), "%u",
769 ((unsigned *)jp
->jp_value
)[i
]);
772 snprintf(valbuf
, sizeof(valbuf
), "%ld",
773 ((long *)jp
->jp_value
)[i
]);
776 snprintf(valbuf
, sizeof(valbuf
), "%lu",
777 ((unsigned long *)jp
->jp_value
)[i
]);
780 snprintf(valbuf
, sizeof(valbuf
), "%jd",
781 (intmax_t)((int64_t *)jp
->jp_value
)[i
]);
784 snprintf(valbuf
, sizeof(valbuf
), "%ju",
785 (uintmax_t)((uint64_t *)jp
->jp_value
)[i
]);
788 switch (jp
->jp_structtype
) {
790 if (inet_ntop(AF_INET
,
791 &((struct in_addr
*)jp
->jp_value
)[i
],
792 valbuf
, sizeof(valbuf
)) == NULL
) {
793 strerror_r(errno
, jail_errmsg
,
799 if (inet_ntop(AF_INET6
,
800 &((struct in6_addr
*)jp
->jp_value
)[i
],
801 valbuf
, sizeof(valbuf
)) == NULL
) {
802 strerror_r(errno
, jail_errmsg
,
813 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
814 "unknown type for %s", jp
->jp_name
);
818 valuelens
[i
] = strlen(valbuf
) + 1;
819 valuelen
+= valuelens
[i
];
820 values
[i
] = alloca(valuelens
[i
]);
821 strcpy(values
[i
], valbuf
);
823 value
= malloc(valuelen
);
825 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
828 for (i
= 0; i
< nval
; i
++) {
829 strcpy(tvalue
, values
[i
]);
831 tvalue
+= valuelens
[i
];
840 * Free the contents of a jail parameter list (but not the list itself).
843 jailparam_free(struct jailparam
*jp
, unsigned njp
)
847 for (j
= 0; j
< njp
; j
++) {
849 if (!(jp
[j
].jp_flags
& JP_RAWVALUE
))
850 free(jp
[j
].jp_value
);
855 * Find a parameter's type and size from its MIB.
858 jailparam_type(struct jailparam
*jp
)
860 char *p
, *name
, *nname
;
861 size_t miblen
, desclen
;
867 int mib
[CTL_MAXNAME
];
869 /* The "lastjid" parameter isn't real. */
871 if (!strcmp(name
, "lastjid")) {
872 jp
->jp_valuelen
= sizeof(int);
873 jp
->jp_ctltype
= CTLTYPE_INT
| CTLFLAG_WR
;
877 /* Find the sysctl that describes the parameter. */
880 snprintf(desc
.s
, sizeof(desc
.s
), SJPARAM
".%s", name
);
881 miblen
= sizeof(mib
) - 2 * sizeof(int);
882 if (sysctl(mib
, 2, mib
+ 2, &miblen
, desc
.s
, strlen(desc
.s
)) < 0) {
883 if (errno
!= ENOENT
) {
884 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
885 "sysctl(0.3.%s): %s", name
, strerror(errno
));
889 * The parameter probably doesn't exist. But it might be
890 * the "no" counterpart to a boolean.
892 nname
= nononame(name
);
895 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
896 "unknown parameter: %s", jp
->jp_name
);
900 name
= alloca(strlen(nname
) + 1);
903 snprintf(desc
.s
, sizeof(desc
.s
), SJPARAM
".%s", name
);
904 miblen
= sizeof(mib
) - 2 * sizeof(int);
905 if (sysctl(mib
, 2, mib
+ 2, &miblen
, desc
.s
,
907 goto unknown_parameter
;
908 jp
->jp_flags
|= JP_NOBOOL
;
912 desclen
= sizeof(desc
);
913 if (sysctl(mib
, (miblen
/ sizeof(int)) + 2, &desc
, &desclen
,
915 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
916 "sysctl(0.4.%s): %s", name
, strerror(errno
));
919 jp
->jp_ctltype
= desc
.i
;
920 /* If this came from removing a "no", it better be a boolean. */
921 if (jp
->jp_flags
& JP_NOBOOL
) {
922 if ((desc
.i
& CTLTYPE
) == CTLTYPE_INT
&& desc
.s
[0] == 'B') {
923 jp
->jp_valuelen
= sizeof(int);
926 else if ((desc
.i
& CTLTYPE
) != CTLTYPE_NODE
)
927 goto unknown_parameter
;
929 /* See if this is an array type. */
930 p
= strchr(desc
.s
, '\0');
932 if (p
- 2 < desc
.s
|| strcmp(p
- 2, ",a"))
938 /* Look for types we understand. */
939 switch (desc
.i
& CTLTYPE
) {
941 if (desc
.s
[0] == 'B')
942 jp
->jp_flags
|= JP_BOOL
;
943 else if (!strcmp(desc
.s
, "E,jailsys"))
944 jp
->jp_flags
|= JP_JAILSYS
;
946 jp
->jp_valuelen
= sizeof(int);
950 jp
->jp_valuelen
= sizeof(long);
954 jp
->jp_valuelen
= sizeof(int64_t);
958 desclen
= sizeof(desc
.s
);
959 if (sysctl(mib
+ 2, miblen
/ sizeof(int), desc
.s
, &desclen
,
961 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
962 "sysctl(" SJPARAM
".%s): %s", name
,
966 jp
->jp_valuelen
= strtoul(desc
.s
, NULL
, 10);
969 if (!strcmp(desc
.s
, "S,in_addr")) {
970 jp
->jp_structtype
= JPS_IN_ADDR
;
971 jp
->jp_valuelen
= sizeof(struct in_addr
);
972 } else if (!strcmp(desc
.s
, "S,in6_addr")) {
973 jp
->jp_structtype
= JPS_IN6_ADDR
;
974 jp
->jp_valuelen
= sizeof(struct in6_addr
);
977 if (sysctl(mib
+ 2, miblen
/ sizeof(int),
978 NULL
, &jp
->jp_valuelen
, NULL
, 0) < 0) {
979 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
980 "sysctl(" SJPARAM
".%s): %s", name
,
988 * A node might be described by an empty-named child,
989 * which would be immediately before or after the node itself.
992 miblen
+= sizeof(int);
993 for (i
= -1; i
<= 1; i
+= 2) {
994 mib
[(miblen
/ sizeof(int)) + 1] =
995 mib
[(miblen
/ sizeof(int))] + i
;
996 desclen
= sizeof(desc
.s
);
997 if (sysctl(mib
, (miblen
/ sizeof(int)) + 2, desc
.s
,
998 &desclen
, NULL
, 0) < 0) {
1001 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
1002 "sysctl(0.1): %s", strerror(errno
));
1005 if (desclen
== sizeof(SJPARAM
) + strlen(name
) + 2 &&
1006 memcmp(SJPARAM
".", desc
.s
, sizeof(SJPARAM
)) == 0 &&
1007 memcmp(name
, desc
.s
+ sizeof(SJPARAM
),
1008 desclen
- sizeof(SJPARAM
) - 2) == 0 &&
1009 desc
.s
[desclen
- 2] == '.')
1012 goto unknown_parameter
;
1014 snprintf(jail_errmsg
, JAIL_ERRMSGLEN
,
1015 "unknown type for %s", jp
->jp_name
);
1020 jp
->jp_elemlen
= jp
->jp_valuelen
;
1021 jp
->jp_valuelen
= 0;
1027 * Change a boolean parameter name into its "no" counterpart or vice versa.
1030 noname(const char *name
)
1034 nname
= malloc(strlen(name
) + 3);
1035 if (nname
== NULL
) {
1036 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
1039 p
= strrchr(name
, '.');
1041 sprintf(nname
, "%.*s.no%s", (int)(p
- name
), name
, p
+ 1);
1043 sprintf(nname
, "no%s", name
);
1048 nononame(const char *name
)
1052 p
= strrchr(name
, '.');
1053 if (strncmp(p
? p
+ 1 : name
, "no", 2)) {
1054 snprintf(jail_errmsg
, sizeof(jail_errmsg
),
1055 "mismatched boolean: %s", name
);
1059 nname
= malloc(strlen(name
) - 1);
1060 if (nname
== NULL
) {
1061 strerror_r(errno
, jail_errmsg
, JAIL_ERRMSGLEN
);
1065 sprintf(nname
, "%.*s.%s", (int)(p
- name
), name
, p
+ 3);
1067 strcpy(nname
, name
+ 2);