1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if 0 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1980, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 static char sccsid[] = "From: @(#)swapon.c 8.1 (Berkeley) 6/5/93"; 41 #endif /* not lint */ 42 #endif 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/capsicum.h> 48 #include <sys/disk.h> 49 #include <sys/socket.h> 50 #include <sys/sysctl.h> 51 #include <sys/wait.h> 52 53 #include <assert.h> 54 #include <capsicum_helpers.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <fcntl.h> 58 #include <ifaddrs.h> 59 #include <netdb.h> 60 #include <paths.h> 61 #include <stdbool.h> 62 #include <stdint.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <sysexits.h> 67 #include <unistd.h> 68 69 #include <arpa/inet.h> 70 71 #include <net/if.h> 72 #include <net/if_dl.h> 73 #include <net/route.h> 74 75 #include <netinet/in.h> 76 #include <netinet/netdump/netdump.h> 77 78 #ifdef HAVE_CRYPTO 79 #include <openssl/err.h> 80 #include <openssl/pem.h> 81 #include <openssl/rand.h> 82 #include <openssl/rsa.h> 83 #endif 84 85 static int verbose; 86 87 static void _Noreturn 88 usage(void) 89 { 90 fprintf(stderr, 91 "usage: dumpon [-i index] [-r] [-v] [-k <pubkey>] [-Zz] <device>\n" 92 " dumpon [-i index] [-r] [-v] [-k <pubkey>] [-Zz]\n" 93 " [-g <gateway>] -s <server> -c <client> <iface>\n" 94 " dumpon [-v] off\n" 95 " dumpon [-v] -l\n"); 96 exit(EX_USAGE); 97 } 98 99 /* 100 * Look for a default route on the specified interface. 101 */ 102 static char * 103 find_gateway(const char *ifname) 104 { 105 struct ifaddrs *ifa, *ifap; 106 struct rt_msghdr *rtm; 107 struct sockaddr *sa; 108 struct sockaddr_dl *sdl; 109 struct sockaddr_in *dst, *mask, *gw; 110 char *buf, *next, *ret; 111 size_t sz; 112 int error, i, ifindex, mib[7]; 113 114 /* First look up the interface index. */ 115 if (getifaddrs(&ifap) != 0) 116 err(EX_OSERR, "getifaddrs"); 117 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 118 if (ifa->ifa_addr->sa_family != AF_LINK) 119 continue; 120 if (strcmp(ifa->ifa_name, ifname) == 0) { 121 sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr; 122 ifindex = sdl->sdl_index; 123 break; 124 } 125 } 126 if (ifa == NULL) 127 errx(1, "couldn't find interface index for '%s'", ifname); 128 freeifaddrs(ifap); 129 130 /* Now get the IPv4 routing table. */ 131 mib[0] = CTL_NET; 132 mib[1] = PF_ROUTE; 133 mib[2] = 0; 134 mib[3] = AF_INET; 135 mib[4] = NET_RT_DUMP; 136 mib[5] = 0; 137 mib[6] = -1; /* FIB */ 138 139 for (;;) { 140 if (sysctl(mib, nitems(mib), NULL, &sz, NULL, 0) != 0) 141 err(EX_OSERR, "sysctl(NET_RT_DUMP)"); 142 buf = malloc(sz); 143 error = sysctl(mib, nitems(mib), buf, &sz, NULL, 0); 144 if (error == 0) 145 break; 146 if (errno != ENOMEM) 147 err(EX_OSERR, "sysctl(NET_RT_DUMP)"); 148 free(buf); 149 } 150 151 ret = NULL; 152 for (next = buf; next < buf + sz; next += rtm->rtm_msglen) { 153 rtm = (struct rt_msghdr *)(void *)next; 154 if (rtm->rtm_version != RTM_VERSION) 155 continue; 156 if ((rtm->rtm_flags & RTF_GATEWAY) == 0 || 157 rtm->rtm_index != ifindex) 158 continue; 159 160 dst = gw = mask = NULL; 161 sa = (struct sockaddr *)(rtm + 1); 162 for (i = 0; i < RTAX_MAX; i++) { 163 if ((rtm->rtm_addrs & (1 << i)) != 0) { 164 switch (i) { 165 case RTAX_DST: 166 dst = (void *)sa; 167 break; 168 case RTAX_GATEWAY: 169 gw = (void *)sa; 170 break; 171 case RTAX_NETMASK: 172 mask = (void *)sa; 173 break; 174 } 175 } 176 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa)); 177 } 178 179 if (dst->sin_addr.s_addr == INADDR_ANY && 180 mask->sin_addr.s_addr == 0) { 181 ret = inet_ntoa(gw->sin_addr); 182 break; 183 } 184 } 185 free(buf); 186 return (ret); 187 } 188 189 static void 190 check_size(int fd, const char *fn) 191 { 192 int name[] = { CTL_HW, HW_PHYSMEM }; 193 size_t namelen = nitems(name); 194 unsigned long physmem; 195 size_t len; 196 off_t mediasize; 197 int minidump; 198 199 len = sizeof(minidump); 200 if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 && 201 minidump == 1) 202 return; 203 len = sizeof(physmem); 204 if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0) 205 err(EX_OSERR, "can't get memory size"); 206 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0) 207 err(EX_OSERR, "%s: can't get size", fn); 208 if ((uintmax_t)mediasize < (uintmax_t)physmem) 209 errx(EX_IOERR, "%s is smaller than physical memory", fn); 210 } 211 212 #ifdef HAVE_CRYPTO 213 static void 214 _genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap) 215 { 216 FILE *fp; 217 RSA *pubkey; 218 219 assert(pubkeyfile != NULL); 220 assert(kdap != NULL); 221 222 fp = NULL; 223 pubkey = NULL; 224 225 fp = fopen(pubkeyfile, "r"); 226 if (fp == NULL) 227 err(1, "Unable to open %s", pubkeyfile); 228 229 /* 230 * Obsolescent OpenSSL only knows about /dev/random, and needs to 231 * pre-seed before entering cap mode. For whatever reason, 232 * RSA_pub_encrypt uses the internal PRNG. 233 */ 234 #if OPENSSL_VERSION_NUMBER < 0x10100000L 235 { 236 unsigned char c[1]; 237 RAND_bytes(c, 1); 238 } 239 #endif 240 241 if (caph_enter() < 0) 242 err(1, "Unable to enter capability mode"); 243 244 pubkey = RSA_new(); 245 if (pubkey == NULL) { 246 errx(1, "Unable to allocate an RSA structure: %s", 247 ERR_error_string(ERR_get_error(), NULL)); 248 } 249 250 pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL); 251 fclose(fp); 252 fp = NULL; 253 if (pubkey == NULL) 254 errx(1, "Unable to read data from %s.", pubkeyfile); 255 256 /* 257 * RSA keys under ~1024 bits are trivially factorable (2018). OpenSSL 258 * provides an API for RSA keys to estimate the symmetric-cipher 259 * "equivalent" bits of security (defined in NIST SP800-57), which as 260 * of this writing equates a 2048-bit RSA key to 112 symmetric cipher 261 * bits. 262 * 263 * Use this API as a seatbelt to avoid suggesting to users that their 264 * privacy is protected by encryption when the key size is insufficient 265 * to prevent compromise via factoring. 266 * 267 * Future work: Sanity check for weak 'e', and sanity check for absence 268 * of 'd' (i.e., the supplied key is a public key rather than a full 269 * keypair). 270 */ 271 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 272 if (RSA_security_bits(pubkey) < 112) 273 #else 274 if (RSA_size(pubkey) * 8 < 2048) 275 #endif 276 errx(1, "Small RSA keys (you provided: %db) can be " 277 "factored cheaply. Please generate a larger key.", 278 RSA_size(pubkey) * 8); 279 280 kdap->kda_encryptedkeysize = RSA_size(pubkey); 281 if (kdap->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE) { 282 errx(1, "Public key has to be at most %db long.", 283 8 * KERNELDUMP_ENCKEY_MAX_SIZE); 284 } 285 286 kdap->kda_encryptedkey = calloc(1, kdap->kda_encryptedkeysize); 287 if (kdap->kda_encryptedkey == NULL) 288 err(1, "Unable to allocate encrypted key"); 289 290 /* 291 * If no cipher was specified, choose a reasonable default. 292 */ 293 if (kdap->kda_encryption == KERNELDUMP_ENC_NONE) 294 kdap->kda_encryption = KERNELDUMP_ENC_CHACHA20; 295 else if (kdap->kda_encryption == KERNELDUMP_ENC_AES_256_CBC && 296 kdap->kda_compression != KERNELDUMP_COMP_NONE) 297 errx(EX_USAGE, "Unpadded AES256-CBC mode cannot be used " 298 "with compression."); 299 300 arc4random_buf(kdap->kda_key, sizeof(kdap->kda_key)); 301 if (RSA_public_encrypt(sizeof(kdap->kda_key), kdap->kda_key, 302 kdap->kda_encryptedkey, pubkey, 303 RSA_PKCS1_OAEP_PADDING) != (int)kdap->kda_encryptedkeysize) { 304 errx(1, "Unable to encrypt the one-time key: %s", 305 ERR_error_string(ERR_get_error(), NULL)); 306 } 307 RSA_free(pubkey); 308 } 309 310 /* 311 * Run genkey() in a child so it can use capability mode without affecting 312 * the rest of the runtime. 313 */ 314 static void 315 genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap) 316 { 317 pid_t pid; 318 int error, filedes[2], status; 319 ssize_t bytes; 320 321 if (pipe2(filedes, O_CLOEXEC) != 0) 322 err(1, "pipe"); 323 pid = fork(); 324 switch (pid) { 325 case -1: 326 err(1, "fork"); 327 break; 328 case 0: 329 close(filedes[0]); 330 _genkey(pubkeyfile, kdap); 331 /* Write the new kdap back to the parent. */ 332 bytes = write(filedes[1], kdap, sizeof(*kdap)); 333 if (bytes != sizeof(*kdap)) 334 err(1, "genkey pipe write"); 335 _exit(0); 336 } 337 close(filedes[1]); 338 /* Read in the child's genkey() result into kdap. */ 339 bytes = read(filedes[0], kdap, sizeof(*kdap)); 340 if (bytes != sizeof(*kdap)) 341 errx(1, "genkey pipe read"); 342 error = waitpid(pid, &status, WEXITED); 343 if (error == -1) 344 err(1, "waitpid"); 345 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) 346 errx(1, "genkey child exited with status %d", 347 WEXITSTATUS(status)); 348 else if (WIFSIGNALED(status)) 349 errx(1, "genkey child exited with signal %d", 350 WTERMSIG(status)); 351 close(filedes[0]); 352 } 353 #endif 354 355 static void 356 listdumpdev(void) 357 { 358 static char ip[200]; 359 360 char dumpdev[PATH_MAX]; 361 struct diocskerneldump_arg ndconf; 362 size_t len; 363 const char *sysctlname = "kern.shutdown.dumpdevname"; 364 int fd; 365 366 len = sizeof(dumpdev); 367 if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) { 368 if (errno == ENOMEM) { 369 err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n", 370 sysctlname); 371 } else { 372 err(EX_OSERR, "Sysctl get '%s'\n", sysctlname); 373 } 374 } 375 if (strlen(dumpdev) == 0) 376 (void)strlcpy(dumpdev, _PATH_DEVNULL, sizeof(dumpdev)); 377 378 if (verbose) { 379 char *ctx, *dd; 380 unsigned idx; 381 382 printf("kernel dumps on priority: device\n"); 383 idx = 0; 384 ctx = dumpdev; 385 while ((dd = strsep(&ctx, ",")) != NULL) 386 printf("%u: %s\n", idx++, dd); 387 } else 388 printf("%s\n", dumpdev); 389 390 /* If netdump is enabled, print the configuration parameters. */ 391 if (verbose) { 392 fd = open(_PATH_NETDUMP, O_RDONLY); 393 if (fd < 0) { 394 if (errno != ENOENT) 395 err(EX_OSERR, "opening %s", _PATH_NETDUMP); 396 return; 397 } 398 if (ioctl(fd, DIOCGKERNELDUMP, &ndconf) != 0) { 399 if (errno != ENXIO) 400 err(EX_OSERR, "ioctl(DIOCGKERNELDUMP)"); 401 (void)close(fd); 402 return; 403 } 404 405 printf("server address: %s\n", 406 inet_ntop(ndconf.kda_af, &ndconf.kda_server, ip, 407 sizeof(ip))); 408 printf("client address: %s\n", 409 inet_ntop(ndconf.kda_af, &ndconf.kda_client, ip, 410 sizeof(ip))); 411 printf("gateway address: %s\n", 412 inet_ntop(ndconf.kda_af, &ndconf.kda_gateway, ip, 413 sizeof(ip))); 414 (void)close(fd); 415 } 416 } 417 418 static int 419 opendumpdev(const char *arg, char *dumpdev) 420 { 421 int fd, i; 422 423 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 424 strlcpy(dumpdev, arg, PATH_MAX); 425 else { 426 i = snprintf(dumpdev, PATH_MAX, "%s%s", _PATH_DEV, arg); 427 if (i < 0) 428 err(EX_OSERR, "%s", arg); 429 if (i >= PATH_MAX) 430 errc(EX_DATAERR, EINVAL, "%s", arg); 431 } 432 433 fd = open(dumpdev, O_RDONLY); 434 if (fd < 0) 435 err(EX_OSFILE, "%s", dumpdev); 436 return (fd); 437 } 438 439 int 440 main(int argc, char *argv[]) 441 { 442 char dumpdev[PATH_MAX]; 443 struct diocskerneldump_arg ndconf, *kdap; 444 struct addrinfo hints, *res; 445 const char *dev, *pubkeyfile, *server, *client, *gateway; 446 int ch, error, fd, cipher; 447 bool gzip, list, netdump, zstd, insert, rflag; 448 uint8_t ins_idx; 449 450 gzip = list = netdump = zstd = insert = rflag = false; 451 kdap = NULL; 452 pubkeyfile = NULL; 453 server = client = gateway = NULL; 454 ins_idx = KDA_APPEND; 455 cipher = KERNELDUMP_ENC_NONE; 456 457 while ((ch = getopt(argc, argv, "C:c:g:i:k:lrs:vZz")) != -1) 458 switch ((char)ch) { 459 case 'C': 460 if (strcasecmp(optarg, "chacha") == 0 || 461 strcasecmp(optarg, "chacha20") == 0) 462 cipher = KERNELDUMP_ENC_CHACHA20; 463 else if (strcasecmp(optarg, "aes-cbc") == 0 || 464 strcasecmp(optarg, "aes256-cbc") == 0) 465 cipher = KERNELDUMP_ENC_AES_256_CBC; 466 else 467 errx(EX_USAGE, "Unrecognized cipher algorithm " 468 "'%s'", optarg); 469 break; 470 case 'c': 471 client = optarg; 472 break; 473 case 'g': 474 gateway = optarg; 475 break; 476 case 'i': 477 { 478 int i; 479 480 i = atoi(optarg); 481 if (i < 0 || i >= KDA_APPEND - 1) 482 errx(EX_USAGE, 483 "-i index must be between zero and %d.", 484 (int)KDA_APPEND - 2); 485 insert = true; 486 ins_idx = i; 487 } 488 break; 489 case 'k': 490 pubkeyfile = optarg; 491 break; 492 case 'l': 493 list = true; 494 break; 495 case 'r': 496 rflag = true; 497 break; 498 case 's': 499 server = optarg; 500 break; 501 case 'v': 502 verbose = 1; 503 break; 504 case 'Z': 505 zstd = true; 506 break; 507 case 'z': 508 gzip = true; 509 break; 510 default: 511 usage(); 512 } 513 514 if (gzip && zstd) 515 errx(EX_USAGE, "The -z and -Z options are mutually exclusive."); 516 517 if (insert && rflag) 518 errx(EX_USAGE, "The -i and -r options are mutually exclusive."); 519 520 argc -= optind; 521 argv += optind; 522 523 if (list) { 524 listdumpdev(); 525 exit(EX_OK); 526 } 527 528 if (argc != 1) 529 usage(); 530 531 #ifdef HAVE_CRYPTO 532 if (cipher != KERNELDUMP_ENC_NONE && pubkeyfile == NULL) { 533 errx(EX_USAGE, "-C option requires a public key file."); 534 } else if (pubkeyfile != NULL) { 535 ERR_load_crypto_strings(); 536 } 537 #else 538 if (pubkeyfile != NULL) 539 errx(EX_UNAVAILABLE,"Unable to use the public key." 540 " Recompile dumpon with OpenSSL support."); 541 #endif 542 543 if (server != NULL && client != NULL) { 544 dev = _PATH_NETDUMP; 545 netdump = true; 546 } else if (server == NULL && client == NULL && argc > 0) { 547 if (strcmp(argv[0], "off") == 0) { 548 rflag = true; 549 dev = _PATH_DEVNULL; 550 } else 551 dev = argv[0]; 552 netdump = false; 553 554 if (strcmp(dev, _PATH_DEVNULL) == 0) { 555 /* 556 * Netdump has its own configuration tracking that 557 * is not removed when using /dev/null. 558 */ 559 fd = open(_PATH_NETDUMP, O_RDONLY); 560 if (fd != -1) { 561 bzero(&ndconf, sizeof(ndconf)); 562 ndconf.kda_index = KDA_REMOVE_ALL; 563 ndconf.kda_af = AF_INET; 564 error = ioctl(fd, DIOCSKERNELDUMP, &ndconf); 565 if (error != 0) 566 err(1, "ioctl(%s, DIOCSKERNELDUMP)", 567 _PATH_NETDUMP); 568 close(fd); 569 } 570 } 571 } else 572 usage(); 573 574 fd = opendumpdev(dev, dumpdev); 575 if (!netdump && !gzip && !zstd && !rflag) 576 check_size(fd, dumpdev); 577 578 kdap = &ndconf; 579 bzero(kdap, sizeof(*kdap)); 580 581 if (rflag) 582 kdap->kda_index = KDA_REMOVE; 583 else 584 kdap->kda_index = ins_idx; 585 586 kdap->kda_compression = KERNELDUMP_COMP_NONE; 587 if (zstd) 588 kdap->kda_compression = KERNELDUMP_COMP_ZSTD; 589 else if (gzip) 590 kdap->kda_compression = KERNELDUMP_COMP_GZIP; 591 592 if (netdump) { 593 memset(&hints, 0, sizeof(hints)); 594 hints.ai_family = AF_INET; 595 hints.ai_protocol = IPPROTO_UDP; 596 res = NULL; 597 error = getaddrinfo(server, NULL, &hints, &res); 598 if (error != 0) { 599 if (error == EAI_SYSTEM) 600 err(EX_OSERR, "%s", gai_strerror(error)); 601 errx(EX_NOHOST, "%s", gai_strerror(error)); 602 } 603 server = inet_ntoa( 604 ((struct sockaddr_in *)(void *)res->ai_addr)->sin_addr); 605 freeaddrinfo(res); 606 607 if (strlcpy(ndconf.kda_iface, argv[0], 608 sizeof(ndconf.kda_iface)) >= sizeof(ndconf.kda_iface)) 609 errx(EX_USAGE, "invalid interface name '%s'", argv[0]); 610 if (inet_aton(server, &ndconf.kda_server.in4) == 0) 611 errx(EX_USAGE, "invalid server address '%s'", server); 612 if (inet_aton(client, &ndconf.kda_client.in4) == 0) 613 errx(EX_USAGE, "invalid client address '%s'", client); 614 615 if (gateway == NULL) { 616 gateway = find_gateway(argv[0]); 617 if (gateway == NULL) { 618 if (verbose) 619 printf( 620 "failed to look up gateway for %s\n", 621 server); 622 gateway = server; 623 } 624 } 625 if (inet_aton(gateway, &ndconf.kda_gateway.in4) == 0) 626 errx(EX_USAGE, "invalid gateway address '%s'", gateway); 627 ndconf.kda_af = AF_INET; 628 } 629 630 #ifdef HAVE_CRYPTO 631 if (pubkeyfile != NULL) { 632 kdap->kda_encryption = cipher; 633 genkey(pubkeyfile, kdap); 634 } 635 #endif 636 error = ioctl(fd, DIOCSKERNELDUMP, kdap); 637 if (error != 0) 638 error = errno; 639 explicit_bzero(kdap->kda_encryptedkey, kdap->kda_encryptedkeysize); 640 free(kdap->kda_encryptedkey); 641 explicit_bzero(kdap, sizeof(*kdap)); 642 if (error != 0) { 643 if (netdump) { 644 /* 645 * Be slightly less user-hostile for some common 646 * errors, especially as users don't have any great 647 * discoverability into which NICs support netdump. 648 */ 649 if (error == ENXIO) 650 errx(EX_OSERR, "Unable to configure netdump " 651 "because the interface's link is down."); 652 else if (error == ENODEV) 653 errx(EX_OSERR, "Unable to configure netdump " 654 "because the interface driver does not yet " 655 "support netdump."); 656 } 657 errc(EX_OSERR, error, "ioctl(DIOCSKERNELDUMP)"); 658 } 659 660 if (verbose) 661 listdumpdev(); 662 663 exit(EX_OK); 664 } 665