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/socket.h> 34 #include <sys/sysctl.h> 35 36 #include <arpa/inet.h> 37 #include <netinet/in.h> 38 39 #include <errno.h> 40 #include <inttypes.h> 41 #include <stdio.h> 42 #include <stdarg.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "jail.h" 47 48 #define SJPARAM "security.jail.param" 49 50 #define JPS_IN_ADDR 1 51 #define JPS_IN6_ADDR 2 52 53 #define ARRAY_SANITY 5 54 #define ARRAY_SLOP 5 55 56 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); 62 63 char jail_errmsg[JAIL_ERRMSGLEN]; 64 65 static const char *bool_values[] = { "false", "true" }; 66 static const char *jailsys_values[] = { "disable", "new", "inherit" }; 67 68 69 /* 70 * Import a null-terminated parameter list and set a jail with the flags 71 * and parameters. 72 */ 73 int 74 jail_setv(int flags, ...) 75 { 76 va_list ap, tap; 77 struct jailparam *jp; 78 const char *name, *value; 79 int njp, jid; 80 81 /* Create the parameter list and import the parameters. */ 82 va_start(ap, flags); 83 va_copy(tap, ap); 84 for (njp = 0; va_arg(tap, char *) != NULL; njp++) 85 (void)va_arg(tap, char *); 86 va_end(tap); 87 jp = alloca(njp * sizeof(struct jailparam)); 88 for (njp = 0; (name = va_arg(ap, char *)) != NULL; njp++) { 89 value = va_arg(ap, char *); 90 if (jailparam_init(jp + njp, name) < 0 || 91 jailparam_import(jp + njp, value) < 0) { 92 jailparam_free(jp, njp); 93 va_end(ap); 94 return (-1); 95 } 96 } 97 va_end(ap); 98 jid = jailparam_set(jp, njp, flags); 99 jailparam_free(jp, njp); 100 return (jid); 101 } 102 103 /* 104 * Read a null-terminated parameter list, get the referenced jail, and export 105 * the parameters to the list. 106 */ 107 int 108 jail_getv(int flags, ...) 109 { 110 va_list ap, tap; 111 struct jailparam *jp, *jp_lastjid, *jp_jid, *jp_name, *jp_key; 112 char *valarg, *value; 113 const char *name, *key_value, *lastjid_value, *jid_value, *name_value; 114 int njp, i, jid; 115 116 /* Create the parameter list and find the key. */ 117 va_start(ap, flags); 118 va_copy(tap, ap); 119 for (njp = 0; va_arg(tap, char *) != NULL; njp++) 120 (void)va_arg(tap, char *); 121 va_end(tap); 122 123 jp = alloca(njp * sizeof(struct jailparam)); 124 va_copy(tap, ap); 125 jp_lastjid = jp_jid = jp_name = NULL; 126 lastjid_value = jid_value = name_value = NULL; 127 for (njp = 0; (name = va_arg(tap, char *)) != NULL; njp++) { 128 value = va_arg(tap, char *); 129 if (jailparam_init(jp + njp, name) < 0) { 130 va_end(tap); 131 goto error; 132 } 133 if (!strcmp(jp[njp].jp_name, "lastjid")) { 134 jp_lastjid = jp + njp; 135 lastjid_value = value; 136 } else if (!strcmp(jp[njp].jp_name, "jid")) { 137 jp_jid = jp + njp; 138 jid_value = value; 139 } if (!strcmp(jp[njp].jp_name, "name")) { 140 jp_name = jp + njp; 141 name_value = value; 142 } 143 } 144 va_end(tap); 145 /* Import the key parameter. */ 146 if (jp_lastjid != NULL) { 147 jp_key = jp_lastjid; 148 key_value = lastjid_value; 149 } else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) { 150 jp_key = jp_jid; 151 key_value = jid_value; 152 } else if (jp_name != NULL) { 153 jp_key = jp_name; 154 key_value = name_value; 155 } else { 156 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN); 157 errno = ENOENT; 158 goto error; 159 } 160 if (jailparam_import(jp_key, key_value) < 0) 161 goto error; 162 /* Get the jail and export the parameters. */ 163 jid = jailparam_get(jp, njp, flags); 164 if (jid < 0) 165 goto error; 166 for (i = 0; i < njp; i++) { 167 (void)va_arg(ap, char *); 168 valarg = va_arg(ap, char *); 169 if (jp + i != jp_key) { 170 /* It's up to the caller to ensure there's room. */ 171 if ((jp[i].jp_ctltype & CTLTYPE) == CTLTYPE_STRING) 172 strcpy(valarg, jp[i].jp_value); 173 else { 174 value = jailparam_export(jp + i); 175 if (value == NULL) 176 goto error; 177 strcpy(valarg, value); 178 free(value); 179 } 180 } 181 } 182 jailparam_free(jp, njp); 183 va_end(ap); 184 return (jid); 185 186 error: 187 jailparam_free(jp, njp); 188 va_end(ap); 189 return (-1); 190 } 191 192 /* 193 * Return a list of all known parameters. 194 */ 195 int 196 jailparam_all(struct jailparam **jpp) 197 { 198 struct jailparam *jp; 199 size_t mlen1, mlen2, buflen; 200 int njp, nlist; 201 int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2]; 202 char buf[MAXPATHLEN]; 203 204 njp = 0; 205 nlist = 32; 206 jp = malloc(nlist * sizeof(*jp)); 207 if (jp == NULL) { 208 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 209 return (-1); 210 } 211 mib1[0] = 0; 212 mib1[1] = 2; 213 mlen1 = CTL_MAXNAME - 2; 214 if (sysctlnametomib(SJPARAM, mib1 + 2, &mlen1) < 0) { 215 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 216 "sysctlnametomib(" SJPARAM "): %s", strerror(errno)); 217 goto error; 218 } 219 for (;; njp++) { 220 /* Get the next parameter. */ 221 mlen2 = sizeof(mib2); 222 if (sysctl(mib1, mlen1 + 2, mib2, &mlen2, NULL, 0) < 0) { 223 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 224 "sysctl(0.2): %s", strerror(errno)); 225 goto error; 226 } 227 if (mib2[0] != mib1[2] || mib2[1] != mib1[3] || 228 mib2[2] != mib1[4]) 229 break; 230 /* Convert it to an ascii name. */ 231 memcpy(mib1 + 2, mib2, mlen2); 232 mlen1 = mlen2 / sizeof(int); 233 mib1[1] = 1; 234 buflen = sizeof(buf); 235 if (sysctl(mib1, mlen1 + 2, buf, &buflen, NULL, 0) < 0) { 236 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 237 "sysctl(0.1): %s", strerror(errno)); 238 goto error; 239 } 240 if (buf[buflen - 2] == '.') 241 buf[buflen - 2] = '\0'; 242 /* Add the parameter to the list */ 243 if (njp >= nlist) { 244 nlist *= 2; 245 jp = realloc(jp, nlist * sizeof(*jp)); 246 if (jp == NULL) { 247 jailparam_free(jp, njp); 248 return (-1); 249 } 250 } 251 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0) 252 goto error; 253 mib1[1] = 2; 254 } 255 jp = realloc(jp, njp * sizeof(*jp)); 256 *jpp = jp; 257 return (njp); 258 259 error: 260 jailparam_free(jp, njp); 261 free(jp); 262 return (-1); 263 } 264 265 /* 266 * Clear a jail parameter and copy in its name. 267 */ 268 int 269 jailparam_init(struct jailparam *jp, const char *name) 270 { 271 272 memset(jp, 0, sizeof(*jp)); 273 jp->jp_name = strdup(name); 274 if (jp->jp_name == NULL) { 275 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 276 return (-1); 277 } 278 if (jailparam_type(jp) < 0) { 279 jailparam_free(jp, 1); 280 return (-1); 281 } 282 return (0); 283 } 284 285 /* 286 * Put a name and value into a jail parameter element, converting the value 287 * to internal form. 288 */ 289 int 290 jailparam_import(struct jailparam *jp, const char *value) 291 { 292 char *p, *ep, *tvalue; 293 const char *avalue; 294 int i, nval, fw; 295 296 if (value == NULL) 297 return (0); 298 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) { 299 jp->jp_value = strdup(value); 300 if (jp->jp_value == NULL) { 301 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 302 return (-1); 303 } 304 return (0); 305 } 306 nval = 1; 307 if (jp->jp_elemlen) { 308 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) { 309 jp->jp_value = strdup(""); 310 if (jp->jp_value == NULL) { 311 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 312 return (-1); 313 } 314 jp->jp_valuelen = 0; 315 return (0); 316 } 317 for (p = strchr(value, ','); p; p = strchr(p + 1, ',')) 318 nval++; 319 jp->jp_valuelen = jp->jp_elemlen * nval; 320 } 321 jp->jp_value = malloc(jp->jp_valuelen); 322 if (jp->jp_value == NULL) { 323 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 324 return (-1); 325 } 326 avalue = value; 327 for (i = 0; i < nval; i++) { 328 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ","); 329 switch (jp->jp_ctltype & CTLTYPE) { 330 case CTLTYPE_INT: 331 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) { 332 if (!jailparam_import_enum(bool_values, 2, 333 avalue, fw, &((int *)jp->jp_value)[i])) { 334 snprintf(jail_errmsg, 335 JAIL_ERRMSGLEN, "%s: " 336 "unknown boolean value \"%.*s\"", 337 jp->jp_name, fw, avalue); 338 errno = EINVAL; 339 goto error; 340 } 341 break; 342 } 343 if (jp->jp_flags & JP_JAILSYS) { 344 /* 345 * Allow setting a jailsys parameter to "new" 346 * in a booleanesque fashion. 347 */ 348 if (value[0] == '\0') 349 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW; 350 else if (!jailparam_import_enum(jailsys_values, 351 sizeof(jailsys_values) / 352 sizeof(jailsys_values[0]), avalue, fw, 353 &((int *)jp->jp_value)[i])) { 354 snprintf(jail_errmsg, 355 JAIL_ERRMSGLEN, "%s: " 356 "unknown jailsys value \"%.*s\"", 357 jp->jp_name, fw, avalue); 358 errno = EINVAL; 359 goto error; 360 } 361 break; 362 } 363 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10); 364 integer_test: 365 if (ep != avalue + fw) { 366 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 367 "%s: non-integer value \"%.*s\"", 368 jp->jp_name, fw, avalue); 369 errno = EINVAL; 370 goto error; 371 } 372 break; 373 case CTLTYPE_UINT: 374 ((unsigned *)jp->jp_value)[i] = 375 strtoul(avalue, &ep, 10); 376 goto integer_test; 377 case CTLTYPE_LONG: 378 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10); 379 goto integer_test; 380 case CTLTYPE_ULONG: 381 ((unsigned long *)jp->jp_value)[i] = 382 strtoul(avalue, &ep, 10); 383 goto integer_test; 384 case CTLTYPE_S64: 385 ((int64_t *)jp->jp_value)[i] = 386 strtoimax(avalue, &ep, 10); 387 goto integer_test; 388 case CTLTYPE_U64: 389 ((uint64_t *)jp->jp_value)[i] = 390 strtoumax(avalue, &ep, 10); 391 goto integer_test; 392 case CTLTYPE_STRUCT: 393 tvalue = alloca(fw + 1); 394 strlcpy(tvalue, avalue, fw + 1); 395 switch (jp->jp_structtype) { 396 case JPS_IN_ADDR: 397 if (inet_pton(AF_INET, tvalue, 398 &((struct in_addr *)jp->jp_value)[i]) != 1) 399 { 400 snprintf(jail_errmsg, 401 JAIL_ERRMSGLEN, 402 "%s: not an IPv4 address: %s", 403 jp->jp_name, tvalue); 404 errno = EINVAL; 405 goto error; 406 } 407 break; 408 case JPS_IN6_ADDR: 409 if (inet_pton(AF_INET6, tvalue, 410 &((struct in6_addr *)jp->jp_value)[i]) != 1) 411 { 412 snprintf(jail_errmsg, 413 JAIL_ERRMSGLEN, 414 "%s: not an IPv6 address: %s", 415 jp->jp_name, tvalue); 416 errno = EINVAL; 417 goto error; 418 } 419 break; 420 default: 421 goto unknown_type; 422 } 423 break; 424 default: 425 unknown_type: 426 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 427 "unknown type for %s", jp->jp_name); 428 errno = ENOENT; 429 goto error; 430 } 431 avalue += fw + 1; 432 } 433 return (0); 434 435 error: 436 free(jp->jp_value); 437 jp->jp_value = NULL; 438 return (-1); 439 } 440 441 static int 442 jailparam_import_enum(const char **values, int nvalues, const char *valstr, 443 size_t valsize, int *value) 444 { 445 char *ep; 446 int i; 447 448 for (i = 0; i < nvalues; i++) 449 if (valsize == strlen(values[i]) && 450 !strncasecmp(valstr, values[i], valsize)) { 451 *value = i; 452 return 1; 453 } 454 *value = strtol(valstr, &ep, 10); 455 return (ep == valstr + valsize); 456 } 457 458 /* 459 * Put a name and value into a jail parameter element, copying the value 460 * but not altering it. 461 */ 462 int 463 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen) 464 { 465 466 jp->jp_value = value; 467 jp->jp_valuelen = valuelen; 468 jp->jp_flags |= JP_RAWVALUE; 469 return (0); 470 } 471 472 /* 473 * Run the jail_set and jail_get system calls on a parameter list. 474 */ 475 int 476 jailparam_set(struct jailparam *jp, unsigned njp, int flags) 477 { 478 struct iovec *jiov; 479 char *nname; 480 int i, jid, bool0; 481 unsigned j; 482 483 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1)); 484 bool0 = 0; 485 for (i = j = 0; j < njp; j++) { 486 jiov[i].iov_base = jp[j].jp_name; 487 jiov[i].iov_len = strlen(jp[j].jp_name) + 1; 488 i++; 489 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) { 490 /* 491 * Set booleans without values. If one has a value of 492 * zero, change it to (or from) its "no" counterpart. 493 */ 494 jiov[i].iov_base = NULL; 495 jiov[i].iov_len = 0; 496 if (jp[j].jp_value != NULL && 497 jp[j].jp_valuelen == sizeof(int) && 498 !*(int *)jp[j].jp_value) { 499 bool0 = 1; 500 nname = jp[j].jp_flags & JP_BOOL 501 ? noname(jp[j].jp_name) 502 : nononame(jp[j].jp_name); 503 if (nname == NULL) { 504 njp = j; 505 jid = -1; 506 goto done; 507 } 508 jiov[i - 1].iov_base = nname; 509 jiov[i - 1].iov_len = strlen(nname) + 1; 510 511 } 512 } else { 513 /* 514 * Try to fill in missing values with an empty string. 515 */ 516 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 && 517 jailparam_import(jp + j, "") < 0) { 518 njp = j; 519 jid = -1; 520 goto done; 521 } 522 jiov[i].iov_base = jp[j].jp_value; 523 jiov[i].iov_len = 524 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING 525 ? strlen(jp[j].jp_value) + 1 526 : jp[j].jp_valuelen; 527 } 528 i++; 529 } 530 *(const void **)&jiov[i].iov_base = "errmsg"; 531 jiov[i].iov_len = sizeof("errmsg"); 532 i++; 533 jiov[i].iov_base = jail_errmsg; 534 jiov[i].iov_len = JAIL_ERRMSGLEN; 535 i++; 536 jail_errmsg[0] = 0; 537 jid = jail_set(jiov, i, flags); 538 if (jid < 0 && !jail_errmsg[0]) 539 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s", 540 strerror(errno)); 541 done: 542 if (bool0) 543 for (j = 0; j < njp; j++) 544 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) && 545 jp[j].jp_value != NULL && 546 jp[j].jp_valuelen == sizeof(int) && 547 !*(int *)jp[j].jp_value) 548 free(jiov[j * 2].iov_base); 549 return (jid); 550 } 551 552 int 553 jailparam_get(struct jailparam *jp, unsigned njp, int flags) 554 { 555 struct iovec *jiov; 556 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key; 557 int i, ai, ki, jid, arrays, sanity; 558 unsigned j; 559 560 /* 561 * Get the types for all parameters. 562 * Find the key and any array parameters. 563 */ 564 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1)); 565 jp_lastjid = jp_jid = jp_name = NULL; 566 arrays = 0; 567 for (ai = j = 0; j < njp; j++) { 568 if (!strcmp(jp[j].jp_name, "lastjid")) 569 jp_lastjid = jp + j; 570 else if (!strcmp(jp[j].jp_name, "jid")) 571 jp_jid = jp + j; 572 else if (!strcmp(jp[j].jp_name, "name")) 573 jp_name = jp + j; 574 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) { 575 arrays = 1; 576 jiov[ai].iov_base = jp[j].jp_name; 577 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1; 578 ai++; 579 jiov[ai].iov_base = NULL; 580 jiov[ai].iov_len = 0; 581 ai++; 582 } 583 } 584 jp_key = jp_lastjid ? jp_lastjid : 585 jp_jid && jp_jid->jp_valuelen == sizeof(int) && 586 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name; 587 if (jp_key == NULL || jp_key->jp_value == NULL) { 588 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN); 589 errno = ENOENT; 590 return (-1); 591 } 592 ki = ai; 593 jiov[ki].iov_base = jp_key->jp_name; 594 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1; 595 ki++; 596 jiov[ki].iov_base = jp_key->jp_value; 597 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING 598 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen; 599 ki++; 600 *(const void **)&jiov[ki].iov_base = "errmsg"; 601 jiov[ki].iov_len = sizeof("errmsg"); 602 ki++; 603 jiov[ki].iov_base = jail_errmsg; 604 jiov[ki].iov_len = JAIL_ERRMSGLEN; 605 ki++; 606 jail_errmsg[0] = 0; 607 if (arrays && jail_get(jiov, ki, flags) < 0) { 608 if (!jail_errmsg[0]) 609 snprintf(jail_errmsg, sizeof(jail_errmsg), 610 "jail_get: %s", strerror(errno)); 611 return (-1); 612 } 613 /* Allocate storage for all parameters. */ 614 for (ai = j = 0, i = ki; j < njp; j++) { 615 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) { 616 ai++; 617 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP; 618 if (jp[j].jp_valuelen >= jiov[ai].iov_len) 619 jiov[ai].iov_len = jp[j].jp_valuelen; 620 else { 621 jp[j].jp_valuelen = jiov[ai].iov_len; 622 if (jp[j].jp_value != NULL) 623 free(jp[j].jp_value); 624 jp[j].jp_value = malloc(jp[j].jp_valuelen); 625 if (jp[j].jp_value == NULL) { 626 strerror_r(errno, jail_errmsg, 627 JAIL_ERRMSGLEN); 628 return (-1); 629 } 630 } 631 jiov[ai].iov_base = jp[j].jp_value; 632 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len); 633 ai++; 634 } else if (jp + j != jp_key) { 635 jiov[i].iov_base = jp[j].jp_name; 636 jiov[i].iov_len = strlen(jp[j].jp_name) + 1; 637 i++; 638 if (jp[j].jp_value == NULL && 639 !(jp[j].jp_flags & JP_RAWVALUE)) { 640 jp[j].jp_value = malloc(jp[j].jp_valuelen); 641 if (jp[j].jp_value == NULL) { 642 strerror_r(errno, jail_errmsg, 643 JAIL_ERRMSGLEN); 644 return (-1); 645 } 646 } 647 jiov[i].iov_base = jp[j].jp_value; 648 jiov[i].iov_len = jp[j].jp_valuelen; 649 memset(jiov[i].iov_base, 0, jiov[i].iov_len); 650 i++; 651 } 652 } 653 /* 654 * Get the prison. If there are array elements, retry a few times 655 * in case their sizes changed from under us. 656 */ 657 for (sanity = 0;; sanity++) { 658 jid = jail_get(jiov, i, flags); 659 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY || 660 errno != EINVAL || jail_errmsg[0]) 661 break; 662 for (ai = j = 0; j < njp; j++) { 663 if (jp[j].jp_elemlen && 664 !(jp[j].jp_flags & JP_RAWVALUE)) { 665 ai++; 666 jiov[ai].iov_base = NULL; 667 jiov[ai].iov_len = 0; 668 ai++; 669 } 670 } 671 if (jail_get(jiov, ki, flags) < 0) 672 break; 673 for (ai = j = 0; j < njp; j++) { 674 if (jp[j].jp_elemlen && 675 !(jp[j].jp_flags & JP_RAWVALUE)) { 676 ai++; 677 jiov[ai].iov_len += 678 jp[j].jp_elemlen * ARRAY_SLOP; 679 if (jp[j].jp_valuelen >= jiov[ai].iov_len) 680 jiov[ai].iov_len = jp[j].jp_valuelen; 681 else { 682 jp[j].jp_valuelen = jiov[ai].iov_len; 683 if (jp[j].jp_value != NULL) 684 free(jp[j].jp_value); 685 jp[j].jp_value = 686 malloc(jiov[ai].iov_len); 687 if (jp[j].jp_value == NULL) { 688 strerror_r(errno, jail_errmsg, 689 JAIL_ERRMSGLEN); 690 return (-1); 691 } 692 } 693 jiov[ai].iov_base = jp[j].jp_value; 694 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len); 695 ai++; 696 } 697 } 698 } 699 if (jid < 0 && !jail_errmsg[0]) 700 snprintf(jail_errmsg, sizeof(jail_errmsg), 701 "jail_get: %s", strerror(errno)); 702 for (ai = j = 0, i = ki; j < njp; j++) { 703 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) { 704 ai++; 705 jp[j].jp_valuelen = jiov[ai].iov_len; 706 ai++; 707 } else if (jp + j != jp_key) { 708 i++; 709 jp[j].jp_valuelen = jiov[i].iov_len; 710 i++; 711 } 712 } 713 return (jid); 714 } 715 716 /* 717 * Convert a jail parameter's value to external form. 718 */ 719 char * 720 jailparam_export(struct jailparam *jp) 721 { 722 size_t *valuelens; 723 char *value, *tvalue, **values; 724 size_t valuelen; 725 int i, nval, ival; 726 char valbuf[INET6_ADDRSTRLEN]; 727 728 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) { 729 value = strdup(jp->jp_value); 730 if (value == NULL) 731 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 732 return (value); 733 } 734 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1; 735 if (nval == 0) { 736 value = strdup(""); 737 if (value == NULL) 738 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 739 return (value); 740 } 741 values = alloca(nval * sizeof(char *)); 742 valuelens = alloca(nval * sizeof(size_t)); 743 valuelen = 0; 744 for (i = 0; i < nval; i++) { 745 switch (jp->jp_ctltype & CTLTYPE) { 746 case CTLTYPE_INT: 747 ival = ((int *)jp->jp_value)[i]; 748 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) && 749 (unsigned)ival < 2) { 750 strlcpy(valbuf, bool_values[ival], 751 sizeof(valbuf)); 752 break; 753 } 754 if ((jp->jp_flags & JP_JAILSYS) && 755 (unsigned)ival < sizeof(jailsys_values) / 756 sizeof(jailsys_values[0])) { 757 strlcpy(valbuf, jailsys_values[ival], 758 sizeof(valbuf)); 759 break; 760 } 761 snprintf(valbuf, sizeof(valbuf), "%d", ival); 762 break; 763 case CTLTYPE_UINT: 764 snprintf(valbuf, sizeof(valbuf), "%u", 765 ((unsigned *)jp->jp_value)[i]); 766 break; 767 case CTLTYPE_LONG: 768 snprintf(valbuf, sizeof(valbuf), "%ld", 769 ((long *)jp->jp_value)[i]); 770 break; 771 case CTLTYPE_ULONG: 772 snprintf(valbuf, sizeof(valbuf), "%lu", 773 ((unsigned long *)jp->jp_value)[i]); 774 break; 775 case CTLTYPE_S64: 776 snprintf(valbuf, sizeof(valbuf), "%jd", 777 (intmax_t)((int64_t *)jp->jp_value)[i]); 778 break; 779 case CTLTYPE_U64: 780 snprintf(valbuf, sizeof(valbuf), "%ju", 781 (uintmax_t)((uint64_t *)jp->jp_value)[i]); 782 break; 783 case CTLTYPE_STRUCT: 784 switch (jp->jp_structtype) { 785 case JPS_IN_ADDR: 786 if (inet_ntop(AF_INET, 787 &((struct in_addr *)jp->jp_value)[i], 788 valbuf, sizeof(valbuf)) == NULL) { 789 strerror_r(errno, jail_errmsg, 790 JAIL_ERRMSGLEN); 791 return (NULL); 792 } 793 break; 794 case JPS_IN6_ADDR: 795 if (inet_ntop(AF_INET6, 796 &((struct in6_addr *)jp->jp_value)[i], 797 valbuf, sizeof(valbuf)) == NULL) { 798 strerror_r(errno, jail_errmsg, 799 JAIL_ERRMSGLEN); 800 return (NULL); 801 } 802 break; 803 default: 804 goto unknown_type; 805 } 806 break; 807 default: 808 unknown_type: 809 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 810 "unknown type for %s", jp->jp_name); 811 errno = ENOENT; 812 return (NULL); 813 } 814 valuelens[i] = strlen(valbuf) + 1; 815 valuelen += valuelens[i]; 816 values[i] = alloca(valuelens[i]); 817 strcpy(values[i], valbuf); 818 } 819 value = malloc(valuelen); 820 if (value == NULL) 821 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 822 else { 823 tvalue = value; 824 for (i = 0; i < nval; i++) { 825 strcpy(tvalue, values[i]); 826 if (i < nval - 1) { 827 tvalue += valuelens[i]; 828 tvalue[-1] = ','; 829 } 830 } 831 } 832 return (value); 833 } 834 835 /* 836 * Free the contents of a jail parameter list (but not the list itself). 837 */ 838 void 839 jailparam_free(struct jailparam *jp, unsigned njp) 840 { 841 unsigned j; 842 843 for (j = 0; j < njp; j++) { 844 free(jp[j].jp_name); 845 if (!(jp[j].jp_flags & JP_RAWVALUE)) 846 free(jp[j].jp_value); 847 } 848 } 849 850 /* 851 * Find a parameter's type and size from its MIB. 852 */ 853 static int 854 jailparam_type(struct jailparam *jp) 855 { 856 char *p, *nname; 857 size_t miblen, desclen; 858 int isarray; 859 struct { 860 int i; 861 char s[MAXPATHLEN]; 862 } desc; 863 int mib[CTL_MAXNAME]; 864 865 /* The "lastjid" parameter isn't real. */ 866 if (!strcmp(jp->jp_name, "lastjid")) { 867 jp->jp_valuelen = sizeof(int); 868 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR; 869 return (0); 870 } 871 872 /* Find the sysctl that describes the parameter. */ 873 mib[0] = 0; 874 mib[1] = 3; 875 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", jp->jp_name); 876 miblen = sizeof(mib) - 2 * sizeof(int); 877 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) { 878 if (errno != ENOENT) { 879 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 880 "sysctl(0.3.%s): %s", jp->jp_name, strerror(errno)); 881 return (-1); 882 } 883 /* 884 * The parameter probably doesn't exist. But it might be 885 * the "no" counterpart to a boolean. 886 */ 887 nname = nononame(jp->jp_name); 888 if (nname != NULL) { 889 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", nname); 890 free(nname); 891 miblen = sizeof(mib) - 2 * sizeof(int); 892 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, 893 strlen(desc.s)) >= 0) { 894 mib[1] = 4; 895 desclen = sizeof(desc); 896 if (sysctl(mib, (miblen / sizeof(int)) + 2, 897 &desc, &desclen, NULL, 0) < 0) { 898 snprintf(jail_errmsg, 899 JAIL_ERRMSGLEN, 900 "sysctl(0.4.%s): %s", desc.s, 901 strerror(errno)); 902 return (-1); 903 } 904 if ((desc.i & CTLTYPE) == CTLTYPE_INT && 905 desc.s[0] == 'B') { 906 jp->jp_ctltype = desc.i; 907 jp->jp_flags |= JP_NOBOOL; 908 jp->jp_valuelen = sizeof(int); 909 return (0); 910 } 911 } 912 } 913 unknown_parameter: 914 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 915 "unknown parameter: %s", jp->jp_name); 916 errno = ENOENT; 917 return (-1); 918 } 919 mib_desc: 920 mib[1] = 4; 921 desclen = sizeof(desc); 922 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen, 923 NULL, 0) < 0) { 924 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 925 "sysctl(0.4.%s): %s", jp->jp_name, strerror(errno)); 926 return (-1); 927 } 928 /* See if this is an array type. */ 929 p = strchr(desc.s, '\0'); 930 isarray = 0; 931 if (p - 2 < desc.s || strcmp(p - 2, ",a")) 932 isarray = 0; 933 else { 934 isarray = 1; 935 p[-2] = 0; 936 } 937 /* Look for types we understand. */ 938 jp->jp_ctltype = desc.i; 939 switch (desc.i & CTLTYPE) { 940 case CTLTYPE_INT: 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; 945 case CTLTYPE_UINT: 946 jp->jp_valuelen = sizeof(int); 947 break; 948 case CTLTYPE_LONG: 949 case CTLTYPE_ULONG: 950 jp->jp_valuelen = sizeof(long); 951 break; 952 case CTLTYPE_S64: 953 case CTLTYPE_U64: 954 jp->jp_valuelen = sizeof(int64_t); 955 break; 956 case CTLTYPE_STRING: 957 desc.s[0] = 0; 958 desclen = sizeof(desc.s); 959 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen, 960 NULL, 0) < 0) { 961 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 962 "sysctl(" SJPARAM ".%s): %s", jp->jp_name, 963 strerror(errno)); 964 return (-1); 965 } 966 jp->jp_valuelen = strtoul(desc.s, NULL, 10); 967 break; 968 case CTLTYPE_STRUCT: 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); 975 } else { 976 desclen = 0; 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", jp->jp_name, 981 strerror(errno)); 982 return (-1); 983 } 984 } 985 break; 986 case CTLTYPE_NODE: 987 /* A node might be described by an empty-named child. */ 988 mib[1] = 1; 989 mib[(miblen / sizeof(int)) + 2] = 990 mib[(miblen / sizeof(int)) + 1] - 1; 991 miblen += sizeof(int); 992 desclen = sizeof(desc.s); 993 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s, &desclen, 994 NULL, 0) < 0) { 995 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 996 "sysctl(0.1): %s", strerror(errno)); 997 return (-1); 998 } 999 if (desc.s[desclen - 2] != '.') 1000 goto unknown_parameter; 1001 goto mib_desc; 1002 default: 1003 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 1004 "unknown type for %s", jp->jp_name); 1005 errno = ENOENT; 1006 return (-1); 1007 } 1008 if (isarray) { 1009 jp->jp_elemlen = jp->jp_valuelen; 1010 jp->jp_valuelen = 0; 1011 } 1012 return (0); 1013 } 1014 1015 /* 1016 * Change a boolean parameter name into its "no" counterpart or vice versa. 1017 */ 1018 static char * 1019 noname(const char *name) 1020 { 1021 char *nname, *p; 1022 1023 nname = malloc(strlen(name) + 3); 1024 if (nname == NULL) { 1025 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 1026 return (NULL); 1027 } 1028 p = strrchr(name, '.'); 1029 if (p != NULL) 1030 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1); 1031 else 1032 sprintf(nname, "no%s", name); 1033 return (nname); 1034 } 1035 1036 static char * 1037 nononame(const char *name) 1038 { 1039 char *p, *nname; 1040 1041 p = strrchr(name, '.'); 1042 if (strncmp(p ? p + 1 : name, "no", 2)) { 1043 snprintf(jail_errmsg, sizeof(jail_errmsg), 1044 "mismatched boolean: %s", name); 1045 errno = EINVAL; 1046 return (NULL); 1047 } 1048 nname = malloc(strlen(name) - 1); 1049 if (nname == NULL) { 1050 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 1051 return (NULL); 1052 } 1053 if (p != NULL) 1054 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3); 1055 else 1056 strcpy(nname, name + 2); 1057 return (nname); 1058 } 1059