]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libjail/jail.c
MFC r331332:
[FreeBSD/stable/10.git] / lib / libjail / jail.c
1 /*-
2  * Copyright (c) 2009 James Gritton.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/jail.h>
33 #include <sys/linker.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "jail.h"
48
49 #define SJPARAM         "security.jail.param"
50
51 #define JPS_IN_ADDR     1
52 #define JPS_IN6_ADDR    2
53
54 #define ARRAY_SANITY    5
55 #define ARRAY_SLOP      5
56
57
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);
64
65 char jail_errmsg[JAIL_ERRMSGLEN];
66
67 static const char *bool_values[] = { "false", "true" };
68 static const char *jailsys_values[] = { "disable", "new", "inherit" };
69
70
71 /*
72  * Import a null-terminated parameter list and set a jail with the flags
73  * and parameters.
74  */
75 int
76 jail_setv(int flags, ...)
77 {
78         va_list ap, tap;
79         struct jailparam *jp;
80         const char *name, *value;
81         int njp, jid;
82
83         /* Create the parameter list and import the parameters. */
84         va_start(ap, flags);
85         va_copy(tap, ap);
86         for (njp = 0; va_arg(tap, char *) != NULL; njp++)
87                 (void)va_arg(tap, char *);
88         va_end(tap);
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)
93                         goto error;
94                 if (jailparam_import(jp + njp++, value) < 0)
95                         goto error;
96         }
97         va_end(ap);
98         jid = jailparam_set(jp, njp, flags);
99         jailparam_free(jp, njp);
100         return (jid);
101
102  error:
103         jailparam_free(jp, njp);
104         va_end(ap);
105         return (-1);
106 }
107
108 /*
109  * Read a null-terminated parameter list, get the referenced jail, and export
110  * the parameters to the list.
111  */
112 int
113 jail_getv(int flags, ...)
114 {
115         va_list ap, tap;
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;
119         int njp, i, jid;
120
121         /* Create the parameter list and find the key. */
122         va_start(ap, flags);
123         va_copy(tap, ap);
124         for (njp = 0; va_arg(tap, char *) != NULL; njp++)
125                 (void)va_arg(tap, char *);
126         va_end(tap);
127
128         jp = alloca(njp * sizeof(struct jailparam));
129         va_copy(tap, ap);
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) {
135                         va_end(tap);
136                         goto error;
137                 }
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")) {
142                         jp_jid = jp + njp;
143                         jid_value = value;
144                 } if (!strcmp(jp[njp].jp_name, "name")) {
145                         jp_name = jp + njp;
146                         name_value = value;
147                 }
148         }
149         va_end(tap);
150         /* Import the key parameter. */
151         if (jp_lastjid != NULL) {
152                 jp_key = jp_lastjid;
153                 key_value = lastjid_value;
154         } else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) {
155                 jp_key = jp_jid;
156                 key_value = jid_value;
157         } else if (jp_name != NULL) {
158                 jp_key = jp_name;
159                 key_value = name_value;
160         } else {
161                 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
162                 errno = ENOENT;
163                 goto error;
164         }
165         if (jailparam_import(jp_key, key_value) < 0)
166                 goto error;
167         /* Get the jail and export the parameters. */
168         jid = jailparam_get(jp, njp, flags);
169         if (jid < 0)
170                 goto error;
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);
178                         else {
179                                 value = jailparam_export(jp + i);
180                                 if (value == NULL)
181                                         goto error;
182                                 strcpy(valarg, value);
183                                 free(value);
184                         }
185                 }
186         }
187         jailparam_free(jp, njp);
188         va_end(ap);
189         return (jid);
190
191  error:
192         jailparam_free(jp, njp);
193         va_end(ap);
194         return (-1);
195 }
196
197 /*
198  * Return a list of all known parameters.
199  */
200 int
201 jailparam_all(struct jailparam **jpp)
202 {
203         struct jailparam *jp, *tjp;
204         size_t mlen1, mlen2, buflen;
205         int njp, nlist;
206         int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2];
207         char buf[MAXPATHLEN];
208
209         njp = 0;
210         nlist = 32;
211         jp = malloc(nlist * sizeof(*jp));
212         if (jp == NULL) {
213                 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
214                 return (-1);
215         }
216         mib1[0] = 0;
217         mib1[1] = 2;
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));
222                 goto error;
223         }
224         for (;; njp++) {
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));
230                         goto error;
231                 }
232                 if (mib2[0] != mib1[2] || mib2[1] != mib1[3] ||
233                     mib2[2] != mib1[4])
234                         break;
235                 /* Convert it to an ascii name. */
236                 memcpy(mib1 + 2, mib2, mlen2);
237                 mlen1 = mlen2 / sizeof(int);
238                 mib1[1] = 1;
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));
243                         goto error;
244                 }
245                 if (buf[buflen - 2] == '.')
246                         buf[buflen - 2] = '\0';
247                 /* Add the parameter to the list */
248                 if (njp >= nlist) {
249                         nlist *= 2;
250                         tjp = realloc(jp, nlist * sizeof(*jp));
251                         if (tjp == NULL)
252                                 goto error;
253                         jp = tjp;
254                 }
255                 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
256                         goto error;
257                 mib1[1] = 2;
258         }
259         jp = realloc(jp, njp * sizeof(*jp));
260         *jpp = jp;
261         return (njp);
262
263  error:
264         jailparam_free(jp, njp);
265         free(jp);
266         return (-1);
267 }
268
269 /*
270  * Clear a jail parameter and copy in its name.
271  */
272 int
273 jailparam_init(struct jailparam *jp, const char *name)
274 {
275
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);
280                 return (-1);
281         }
282         if (jailparam_type(jp) < 0) {
283                 jailparam_free(jp, 1);
284                 jp->jp_name = NULL;
285                 jp->jp_value = NULL;
286                 return (-1);
287         }
288         return (0);
289 }
290
291 /*
292  * Put a name and value into a jail parameter element, converting the value
293  * to internal form.
294  */
295 int
296 jailparam_import(struct jailparam *jp, const char *value)
297 {
298         char *p, *ep, *tvalue;
299         const char *avalue;
300         int i, nval, fw;
301
302         if (value == NULL)
303                 return (0);
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);
308                         return (-1);
309                 }
310                 return (0);
311         }
312         nval = 1;
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);
318                                 return (-1);
319                         }
320                         jp->jp_valuelen = 0;
321                         return (0);
322                 }
323                 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
324                         nval++;
325                 jp->jp_valuelen = jp->jp_elemlen * nval;
326         }
327         jp->jp_value = malloc(jp->jp_valuelen);
328         if (jp->jp_value == NULL) {
329                 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
330                 return (-1);
331         }
332         avalue = value;
333         for (i = 0; i < nval; i++) {
334                 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
335                 switch (jp->jp_ctltype & CTLTYPE) {
336                 case CTLTYPE_INT:
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);
344                                         errno = EINVAL;
345                                         goto error;
346                                 }
347                                 break;
348                         }
349                         if (jp->jp_flags & JP_JAILSYS) {
350                                 /*
351                                  * Allow setting a jailsys parameter to "new"
352                                  * in a booleanesque fashion.
353                                  */
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);
364                                         errno = EINVAL;
365                                         goto error;
366                                 }
367                                 break;
368                         }
369                         ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
370                 integer_test:
371                         if (ep != avalue + fw) {
372                                 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
373                                     "%s: non-integer value \"%.*s\"",
374                                     jp->jp_name, fw, avalue);
375                                 errno = EINVAL;
376                                 goto error;
377                         }
378                         break;
379                 case CTLTYPE_UINT:
380                         ((unsigned *)jp->jp_value)[i] =
381                             strtoul(avalue, &ep, 10);
382                         goto integer_test;
383                 case CTLTYPE_LONG:
384                         ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
385                         goto integer_test;
386                 case CTLTYPE_ULONG:
387                         ((unsigned long *)jp->jp_value)[i] =
388                             strtoul(avalue, &ep, 10);
389                         goto integer_test;
390                 case CTLTYPE_S64:
391                         ((int64_t *)jp->jp_value)[i] =
392                             strtoimax(avalue, &ep, 10);
393                         goto integer_test;
394                 case CTLTYPE_U64:
395                         ((uint64_t *)jp->jp_value)[i] =
396                             strtoumax(avalue, &ep, 10);
397                         goto integer_test;
398                 case CTLTYPE_STRUCT:
399                         tvalue = alloca(fw + 1);
400                         strlcpy(tvalue, avalue, fw + 1);
401                         switch (jp->jp_structtype) {
402                         case JPS_IN_ADDR:
403                                 if (inet_pton(AF_INET, tvalue,
404                                     &((struct in_addr *)jp->jp_value)[i]) != 1)
405                                 {
406                                         snprintf(jail_errmsg,
407                                             JAIL_ERRMSGLEN,
408                                             "%s: not an IPv4 address: %s",
409                                             jp->jp_name, tvalue);
410                                         errno = EINVAL;
411                                         goto error;
412                                 }
413                                 break;
414                         case JPS_IN6_ADDR:
415                                 if (inet_pton(AF_INET6, tvalue,
416                                     &((struct in6_addr *)jp->jp_value)[i]) != 1)
417                                 {
418                                         snprintf(jail_errmsg,
419                                             JAIL_ERRMSGLEN,
420                                             "%s: not an IPv6 address: %s",
421                                             jp->jp_name, tvalue);
422                                         errno = EINVAL;
423                                         goto error;
424                                 }
425                                 break;
426                         default:
427                                 goto unknown_type;
428                         }
429                         break;
430                 default:
431                 unknown_type:
432                         snprintf(jail_errmsg, JAIL_ERRMSGLEN,
433                             "unknown type for %s", jp->jp_name);
434                         errno = ENOENT;
435                         goto error;
436                 }
437                 avalue += fw + 1;
438         }
439         return (0);
440
441  error:
442         free(jp->jp_value);
443         jp->jp_value = NULL;
444         return (-1);
445 }
446
447 static int
448 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
449     size_t valsize, int *value)
450 {
451         char *ep;
452         int i;
453
454         for (i = 0; i < nvalues; i++)
455                 if (valsize == strlen(values[i]) &&
456                     !strncasecmp(valstr, values[i], valsize)) {
457                         *value = i;
458                         return 1;
459                 }
460         *value = strtol(valstr, &ep, 10);
461         return (ep == valstr + valsize);
462 }
463
464 /*
465  * Put a name and value into a jail parameter element, copying the value
466  * but not altering it.
467  */
468 int
469 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
470 {
471
472         jp->jp_value = value;
473         jp->jp_valuelen = valuelen;
474         jp->jp_flags |= JP_RAWVALUE;
475         return (0);
476 }
477
478 /*
479  * Run the jail_set and jail_get system calls on a parameter list.
480  */
481 int
482 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
483 {
484         struct iovec *jiov;
485         char *nname;
486         int i, jid, bool0;
487         unsigned j;
488
489         jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
490         bool0 = 0;
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;
494                 i++;
495                 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
496                         /*
497                          * Set booleans without values.  If one has a value of
498                          * zero, change it to (or from) its "no" counterpart.
499                          */
500                         jiov[i].iov_base = NULL;
501                         jiov[i].iov_len = 0;
502                         if (jp[j].jp_value != NULL &&
503                             jp[j].jp_valuelen == sizeof(int) &&
504                             !*(int *)jp[j].jp_value) {
505                                 bool0 = 1;
506                                 nname = jp[j].jp_flags & JP_BOOL
507                                     ? noname(jp[j].jp_name)
508                                     : nononame(jp[j].jp_name);
509                                 if (nname == NULL) {
510                                         njp = j;
511                                         jid = -1;
512                                         goto done;
513                                 }
514                                 jiov[i - 1].iov_base = nname;
515                                 jiov[i - 1].iov_len = strlen(nname) + 1;
516                                 
517                         }
518                 } else {
519                         /*
520                          * Try to fill in missing values with an empty string.
521                          */
522                         if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
523                             jailparam_import(jp + j, "") < 0) {
524                                 njp = j;
525                                 jid = -1;
526                                 goto done;
527                         }
528                         jiov[i].iov_base = jp[j].jp_value;
529                         jiov[i].iov_len =
530                             (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
531                             ? strlen(jp[j].jp_value) + 1
532                             : jp[j].jp_valuelen;
533                 }
534                 i++;
535         }
536         *(const void **)&jiov[i].iov_base = "errmsg";
537         jiov[i].iov_len = sizeof("errmsg");
538         i++;
539         jiov[i].iov_base = jail_errmsg;
540         jiov[i].iov_len = JAIL_ERRMSGLEN;
541         i++;
542         jail_errmsg[0] = 0;
543         jid = jail_set(jiov, i, flags);
544         if (jid < 0 && !jail_errmsg[0])
545                 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
546                     strerror(errno));
547  done:
548         if (bool0)
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);
555         return (jid);
556 }
557
558 int
559 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
560 {
561         struct iovec *jiov;
562         struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
563         int i, ai, ki, jid, arrays, sanity;
564         unsigned j;
565
566         /*
567          * Get the types for all parameters.
568          * Find the key and any array parameters.
569          */
570         jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
571         jp_lastjid = jp_jid = jp_name = NULL;
572         arrays = 0;
573         for (ai = j = 0; j < njp; j++) {
574                 if (!strcmp(jp[j].jp_name, "lastjid"))
575                         jp_lastjid = jp + j;
576                 else if (!strcmp(jp[j].jp_name, "jid"))
577                         jp_jid = jp + j;
578                 else if (!strcmp(jp[j].jp_name, "name"))
579                         jp_name = jp + j;
580                 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
581                         arrays = 1;
582                         jiov[ai].iov_base = jp[j].jp_name;
583                         jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
584                         ai++;
585                         jiov[ai].iov_base = NULL;
586                         jiov[ai].iov_len = 0;
587                         ai++;
588                 }
589         }
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);
595                 errno = ENOENT;
596                 return (-1);
597         }
598         ki = ai;
599         jiov[ki].iov_base = jp_key->jp_name;
600         jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
601         ki++;
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;
605         ki++;
606         *(const void **)&jiov[ki].iov_base = "errmsg";
607         jiov[ki].iov_len = sizeof("errmsg");
608         ki++;
609         jiov[ki].iov_base = jail_errmsg;
610         jiov[ki].iov_len = JAIL_ERRMSGLEN;
611         ki++;
612         jail_errmsg[0] = 0;
613         if (arrays && jail_get(jiov, ki, flags) < 0) {
614                 if (!jail_errmsg[0])
615                         snprintf(jail_errmsg, sizeof(jail_errmsg),
616                             "jail_get: %s", strerror(errno));
617                 return (-1);
618         }
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)) {
622                         ai++;
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;
626                         else {
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,
633                                             JAIL_ERRMSGLEN);
634                                         return (-1);
635                                 }
636                         }
637                         jiov[ai].iov_base = jp[j].jp_value;
638                         memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
639                         ai++;
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;
643                         i++;
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,
649                                             JAIL_ERRMSGLEN);
650                                         return (-1);
651                                 }
652                         }
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);
656                         i++;
657                 }
658         }
659         /*
660          * Get the prison.  If there are array elements, retry a few times
661          * in case their sizes changed from under us.
662          */
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])
667                         break;
668                 for (ai = j = 0; j < njp; j++) {
669                         if (jp[j].jp_elemlen &&
670                             !(jp[j].jp_flags & JP_RAWVALUE)) {
671                                 ai++;
672                                 jiov[ai].iov_base = NULL;
673                                 jiov[ai].iov_len = 0;
674                                 ai++;
675                         }
676                 }
677                 if (jail_get(jiov, ki, flags) < 0)
678                         break;
679                 for (ai = j = 0; j < njp; j++) {
680                         if (jp[j].jp_elemlen &&
681                             !(jp[j].jp_flags & JP_RAWVALUE)) {
682                                 ai++;
683                                 jiov[ai].iov_len +=
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;
687                                 else {
688                                         jp[j].jp_valuelen = jiov[ai].iov_len;
689                                         if (jp[j].jp_value != NULL)
690                                                 free(jp[j].jp_value);
691                                         jp[j].jp_value =
692                                             malloc(jiov[ai].iov_len);
693                                         if (jp[j].jp_value == NULL) {
694                                                 strerror_r(errno, jail_errmsg,
695                                                     JAIL_ERRMSGLEN);
696                                                 return (-1);
697                                         }
698                                 }
699                                 jiov[ai].iov_base = jp[j].jp_value;
700                                 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
701                                 ai++;
702                         }
703                 }
704         }
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)) {
710                         ai++;
711                         jp[j].jp_valuelen = jiov[ai].iov_len;
712                         ai++;
713                 } else if (jp + j != jp_key) {
714                         i++;
715                         jp[j].jp_valuelen = jiov[i].iov_len;
716                         i++;
717                 }
718         }
719         return (jid);
720 }
721
722 /*
723  * Convert a jail parameter's value to external form.
724  */
725 char *
726 jailparam_export(struct jailparam *jp)
727 {
728         size_t *valuelens;
729         char *value, *tvalue, **values;
730         size_t valuelen;
731         int i, nval, ival;
732         char valbuf[INET6_ADDRSTRLEN];
733
734         if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
735                 value = strdup(jp->jp_value);
736                 if (value == NULL)
737                         strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
738                 return (value);
739         }
740         nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
741         if (nval == 0) {
742                 value = strdup("");
743                 if (value == NULL)
744                         strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
745                 return (value);
746         }
747         values = alloca(nval * sizeof(char *));
748         valuelens = alloca(nval * sizeof(size_t));
749         valuelen = 0;
750         for (i = 0; i < nval; i++) {
751                 switch (jp->jp_ctltype & CTLTYPE) {
752                 case CTLTYPE_INT:
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],
757                                     sizeof(valbuf));
758                                 break;
759                         }
760                         if ((jp->jp_flags & JP_JAILSYS) &&
761                             (unsigned)ival < sizeof(jailsys_values) /
762                             sizeof(jailsys_values[0])) {
763                                 strlcpy(valbuf, jailsys_values[ival],
764                                     sizeof(valbuf));
765                                 break;
766                         }
767                         snprintf(valbuf, sizeof(valbuf), "%d", ival);
768                         break;
769                 case CTLTYPE_UINT:
770                         snprintf(valbuf, sizeof(valbuf), "%u",
771                             ((unsigned *)jp->jp_value)[i]);
772                         break;
773                 case CTLTYPE_LONG:
774                         snprintf(valbuf, sizeof(valbuf), "%ld",
775                             ((long *)jp->jp_value)[i]);
776                         break;
777                 case CTLTYPE_ULONG:
778                         snprintf(valbuf, sizeof(valbuf), "%lu",
779                             ((unsigned long *)jp->jp_value)[i]);
780                         break;
781                 case CTLTYPE_S64:
782                         snprintf(valbuf, sizeof(valbuf), "%jd",
783                             (intmax_t)((int64_t *)jp->jp_value)[i]);
784                         break;
785                 case CTLTYPE_U64:
786                         snprintf(valbuf, sizeof(valbuf), "%ju",
787                             (uintmax_t)((uint64_t *)jp->jp_value)[i]);
788                         break;
789                 case CTLTYPE_STRUCT:
790                         switch (jp->jp_structtype) {
791                         case JPS_IN_ADDR:
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,
796                                             JAIL_ERRMSGLEN);
797                                         return (NULL);
798                                 }
799                                 break;
800                         case JPS_IN6_ADDR:
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,
805                                             JAIL_ERRMSGLEN);
806                                         return (NULL);
807                                 }
808                                 break;
809                         default:
810                                 goto unknown_type;
811                         }
812                         break;
813                 default:
814                 unknown_type:
815                         snprintf(jail_errmsg, JAIL_ERRMSGLEN,
816                             "unknown type for %s", jp->jp_name);
817                         errno = ENOENT;
818                         return (NULL);
819                 }
820                 valuelens[i] = strlen(valbuf) + 1;
821                 valuelen += valuelens[i];
822                 values[i] = alloca(valuelens[i]);
823                 strcpy(values[i], valbuf);
824         }
825         value = malloc(valuelen);
826         if (value == NULL)
827                 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
828         else {
829                 tvalue = value;
830                 for (i = 0; i < nval; i++) {
831                         strcpy(tvalue, values[i]);
832                         if (i < nval - 1) {
833                                 tvalue += valuelens[i];
834                                 tvalue[-1] = ',';
835                         }
836                 }
837         }
838         return (value);
839 }
840
841 /*
842  * Free the contents of a jail parameter list (but not the list itself).
843  */
844 void
845 jailparam_free(struct jailparam *jp, unsigned njp)
846 {
847         unsigned j;
848
849         for (j = 0; j < njp; j++) {
850                 free(jp[j].jp_name);
851                 if (!(jp[j].jp_flags & JP_RAWVALUE))
852                         free(jp[j].jp_value);
853         }
854 }
855
856 /*
857  * Find a parameter's type and size from its MIB.
858  */
859 static int
860 jailparam_type(struct jailparam *jp)
861 {
862         char *p, *name, *nname;
863         size_t miblen, desclen;
864         int i, isarray;
865         struct {
866             int i;
867             char s[MAXPATHLEN];
868         } desc;
869         int mib[CTL_MAXNAME];
870
871         /* The "lastjid" parameter isn't real. */
872         name = jp->jp_name;
873         if (!strcmp(name, "lastjid")) {
874                 jp->jp_valuelen = sizeof(int);
875                 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
876                 return (0);
877         }
878
879         /* Find the sysctl that describes the parameter. */
880         mib[0] = 0;
881         mib[1] = 3;
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));
888                         return (-1);
889                 }
890                 if (kldload_param(name) >= 0 && sysctl(mib, 2, mib + 2, &miblen,
891                     desc.s, strlen(desc.s)) >= 0)
892                         goto mib_desc;
893                 /*
894                  * The parameter probably doesn't exist.  But it might be
895                  * the "no" counterpart to a boolean.
896                  */
897                 nname = nononame(name);
898                 if (nname == NULL) {
899                 unknown_parameter:
900                         snprintf(jail_errmsg, JAIL_ERRMSGLEN,
901                             "unknown parameter: %s", jp->jp_name);
902                         errno = ENOENT;
903                         return (-1);
904                 }
905                 name = alloca(strlen(nname) + 1);
906                 strcpy(name, nname);
907                 free(nname);
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,
911                     strlen(desc.s)) < 0)
912                         goto unknown_parameter;
913                 jp->jp_flags |= JP_NOBOOL;
914         }
915  mib_desc:
916         mib[1] = 4;
917         desclen = sizeof(desc);
918         if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
919             NULL, 0) < 0) {
920                 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
921                     "sysctl(0.4.%s): %s", name, strerror(errno));
922                 return (-1);
923         }
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);
929                         return (0);
930                 }
931                 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
932                         goto unknown_parameter;
933         }
934         /* See if this is an array type. */
935         p = strchr(desc.s, '\0');
936         isarray  = 0;
937         if (p - 2 < desc.s || strcmp(p - 2, ",a"))
938                 isarray = 0;
939         else {
940                 isarray = 1;
941                 p[-2] = 0;
942         }
943         /* Look for types we understand. */
944         switch (desc.i & CTLTYPE) {
945         case CTLTYPE_INT:
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;
950         case CTLTYPE_UINT:
951                 jp->jp_valuelen = sizeof(int);
952                 break;
953         case CTLTYPE_LONG:
954         case CTLTYPE_ULONG:
955                 jp->jp_valuelen = sizeof(long);
956                 break;
957         case CTLTYPE_S64:
958         case CTLTYPE_U64:
959                 jp->jp_valuelen = sizeof(int64_t);
960                 break;
961         case CTLTYPE_STRING:
962                 desc.s[0] = 0;
963                 desclen = sizeof(desc.s);
964                 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
965                     NULL, 0) < 0) {
966                         snprintf(jail_errmsg, JAIL_ERRMSGLEN,
967                             "sysctl(" SJPARAM ".%s): %s", name,
968                             strerror(errno));
969                         return (-1);
970                 }
971                 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
972                 break;
973         case CTLTYPE_STRUCT:
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);
980                 } else {
981                         desclen = 0;
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,
986                                     strerror(errno));
987                                 return (-1);
988                         }
989                 }
990                 break;
991         case CTLTYPE_NODE:
992                 /*
993                  * A node might be described by an empty-named child,
994                  * which would be immediately before or after the node itself.
995                  */
996                 mib[1] = 1;
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)
1005                                         continue;
1006                                 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1007                                     "sysctl(0.1): %s", strerror(errno));
1008                                 return (-1);
1009                         }
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] == '.')
1015                                 goto mib_desc;
1016                 }
1017                 goto unknown_parameter;
1018         default:
1019                 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1020                     "unknown type for %s", jp->jp_name);
1021                 errno = ENOENT;
1022                 return (-1);
1023         }
1024         if (isarray) {
1025                 jp->jp_elemlen = jp->jp_valuelen;
1026                 jp->jp_valuelen = 0;
1027         }
1028         return (0);
1029 }
1030
1031 /*
1032  * Attempt to load a kernel module matching an otherwise nonexistent parameter.
1033  */
1034 static int
1035 kldload_param(const char *name)
1036 {
1037         int kl;
1038
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)
1043                 kl = kldload(name);
1044         else {
1045                 errno = ENOENT;
1046                 return (-1);
1047         }
1048         if (kl < 0 && errno == EEXIST) {
1049                 /*
1050                  * In the module is already loaded, then it must not contain
1051                  * the parameter.
1052                  */
1053                 errno = ENOENT;
1054         }
1055         return kl;
1056 }
1057
1058 /*
1059  * Change a boolean parameter name into its "no" counterpart or vice versa.
1060  */
1061 static char *
1062 noname(const char *name)
1063 {
1064         char *nname, *p;
1065
1066         nname = malloc(strlen(name) + 3);
1067         if (nname == NULL) {
1068                 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1069                 return (NULL);
1070         }
1071         p = strrchr(name, '.');
1072         if (p != NULL)
1073                 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1074         else
1075                 sprintf(nname, "no%s", name);
1076         return (nname);
1077 }
1078
1079 static char *
1080 nononame(const char *name)
1081 {
1082         char *p, *nname;
1083
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);
1088                 errno = EINVAL;
1089                 return (NULL);
1090         }
1091         nname = malloc(strlen(name) - 1);
1092         if (nname == NULL) {
1093                 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1094                 return (NULL);
1095         }
1096         if (p != NULL)
1097                 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1098         else
1099                 strcpy(nname, name + 2);
1100         return (nname);
1101 }