2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "From: @(#)swapon.c 8.1 (Berkeley) 6/5/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/capsicum.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
54 #include <capsicum_helpers.h>
69 #include <arpa/inet.h>
72 #include <net/if_dl.h>
73 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/netdump/netdump.h>
79 #include <openssl/err.h>
80 #include <openssl/pem.h>
81 #include <openssl/rand.h>
82 #include <openssl/rsa.h>
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"
100 * Look for a default route on the specified interface.
103 find_gateway(const char *ifname)
105 struct ifaddrs *ifa, *ifap;
106 struct rt_msghdr *rtm;
108 struct sockaddr_dl *sdl;
109 struct sockaddr_in *dst, *mask, *gw;
110 char *buf, *next, *ret;
112 int error, i, ifindex, mib[7];
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)
120 if (strcmp(ifa->ifa_name, ifname) == 0) {
121 sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
122 ifindex = sdl->sdl_index;
127 errx(1, "couldn't find interface index for '%s'", ifname);
130 /* Now get the IPv4 routing table. */
135 mib[4] = NET_RT_DUMP;
137 mib[6] = -1; /* FIB */
140 if (sysctl(mib, nitems(mib), NULL, &sz, NULL, 0) != 0)
141 err(EX_OSERR, "sysctl(NET_RT_DUMP)");
143 error = sysctl(mib, nitems(mib), buf, &sz, NULL, 0);
147 err(EX_OSERR, "sysctl(NET_RT_DUMP)");
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)
156 if ((rtm->rtm_flags & RTF_GATEWAY) == 0 ||
157 rtm->rtm_index != ifindex)
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) {
176 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
179 if (dst->sin_addr.s_addr == INADDR_ANY &&
180 mask->sin_addr.s_addr == 0) {
181 ret = inet_ntoa(gw->sin_addr);
190 check_size(int fd, const char *fn)
192 int name[] = { CTL_HW, HW_PHYSMEM };
193 size_t namelen = nitems(name);
194 unsigned long physmem;
199 len = sizeof(minidump);
200 if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 &&
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);
214 _genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap)
219 assert(pubkeyfile != NULL);
220 assert(kdap != NULL);
225 fp = fopen(pubkeyfile, "r");
227 err(1, "Unable to open %s", pubkeyfile);
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.
234 #if OPENSSL_VERSION_NUMBER < 0x10100000L
241 if (caph_enter() < 0)
242 err(1, "Unable to enter capability mode");
245 if (pubkey == NULL) {
246 errx(1, "Unable to allocate an RSA structure: %s",
247 ERR_error_string(ERR_get_error(), NULL));
250 pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL);
254 errx(1, "Unable to read data from %s.", pubkeyfile);
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
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.
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
271 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
272 if (RSA_security_bits(pubkey) < 112)
274 if (RSA_size(pubkey) * 8 < 2048)
276 errx(1, "Small RSA keys (you provided: %db) can be "
277 "factored cheaply. Please generate a larger key.",
278 RSA_size(pubkey) * 8);
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);
286 kdap->kda_encryptedkey = calloc(1, kdap->kda_encryptedkeysize);
287 if (kdap->kda_encryptedkey == NULL)
288 err(1, "Unable to allocate encrypted key");
291 * If no cipher was specified, choose a reasonable default.
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.");
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));
311 * Run genkey() in a child so it can use capability mode without affecting
312 * the rest of the runtime.
315 genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap)
318 int error, filedes[2], status;
321 if (pipe2(filedes, O_CLOEXEC) != 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 bytes = write(filedes[1], kdap->kda_encryptedkey,
336 kdap->kda_encryptedkeysize);
337 if (bytes != (ssize_t)kdap->kda_encryptedkeysize)
338 err(1, "genkey pipe write kda_encryptedkey");
342 /* Read in the child's genkey() result into kdap. */
343 bytes = read(filedes[0], kdap, sizeof(*kdap));
344 if (bytes != sizeof(*kdap))
345 errx(1, "genkey pipe read");
346 if (kdap->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE)
347 errx(1, "Public key has to be at most %db long.",
348 8 * KERNELDUMP_ENCKEY_MAX_SIZE);
349 kdap->kda_encryptedkey = calloc(1, kdap->kda_encryptedkeysize);
350 if (kdap->kda_encryptedkey == NULL)
351 err(1, "Unable to allocate encrypted key");
352 bytes = read(filedes[0], kdap->kda_encryptedkey,
353 kdap->kda_encryptedkeysize);
354 if (bytes != (ssize_t)kdap->kda_encryptedkeysize)
355 errx(1, "genkey pipe read kda_encryptedkey");
356 error = waitpid(pid, &status, WEXITED);
359 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
360 errx(1, "genkey child exited with status %d",
361 WEXITSTATUS(status));
362 else if (WIFSIGNALED(status))
363 errx(1, "genkey child exited with signal %d",
374 char dumpdev[PATH_MAX];
375 struct diocskerneldump_arg ndconf;
377 const char *sysctlname = "kern.shutdown.dumpdevname";
380 len = sizeof(dumpdev);
381 if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) {
382 if (errno == ENOMEM) {
383 err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n",
386 err(EX_OSERR, "Sysctl get '%s'\n", sysctlname);
389 if (strlen(dumpdev) == 0)
390 (void)strlcpy(dumpdev, _PATH_DEVNULL, sizeof(dumpdev));
396 printf("kernel dumps on priority: device\n");
399 while ((dd = strsep(&ctx, ",")) != NULL)
400 printf("%u: %s\n", idx++, dd);
402 printf("%s\n", dumpdev);
404 /* If netdump is enabled, print the configuration parameters. */
406 fd = open(_PATH_NETDUMP, O_RDONLY);
409 err(EX_OSERR, "opening %s", _PATH_NETDUMP);
412 if (ioctl(fd, DIOCGKERNELDUMP, &ndconf) != 0) {
414 err(EX_OSERR, "ioctl(DIOCGKERNELDUMP)");
419 printf("server address: %s\n",
420 inet_ntop(ndconf.kda_af, &ndconf.kda_server, ip,
422 printf("client address: %s\n",
423 inet_ntop(ndconf.kda_af, &ndconf.kda_client, ip,
425 printf("gateway address: %s\n",
426 inet_ntop(ndconf.kda_af, &ndconf.kda_gateway, ip,
433 opendumpdev(const char *arg, char *dumpdev)
437 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
438 strlcpy(dumpdev, arg, PATH_MAX);
440 i = snprintf(dumpdev, PATH_MAX, "%s%s", _PATH_DEV, arg);
442 err(EX_OSERR, "%s", arg);
444 errc(EX_DATAERR, EINVAL, "%s", arg);
447 fd = open(dumpdev, O_RDONLY);
449 err(EX_OSFILE, "%s", dumpdev);
454 main(int argc, char *argv[])
456 char dumpdev[PATH_MAX];
457 struct diocskerneldump_arg ndconf, *kdap;
458 struct addrinfo hints, *res;
459 const char *dev, *pubkeyfile, *server, *client, *gateway;
460 int ch, error, fd, cipher;
461 bool gzip, list, netdump, zstd, insert, rflag;
464 gzip = list = netdump = zstd = insert = rflag = false;
467 server = client = gateway = NULL;
468 ins_idx = KDA_APPEND;
469 cipher = KERNELDUMP_ENC_NONE;
471 while ((ch = getopt(argc, argv, "C:c:g:i:k:lrs:vZz")) != -1)
474 if (strcasecmp(optarg, "chacha") == 0 ||
475 strcasecmp(optarg, "chacha20") == 0)
476 cipher = KERNELDUMP_ENC_CHACHA20;
477 else if (strcasecmp(optarg, "aes-cbc") == 0 ||
478 strcasecmp(optarg, "aes256-cbc") == 0)
479 cipher = KERNELDUMP_ENC_AES_256_CBC;
481 errx(EX_USAGE, "Unrecognized cipher algorithm "
495 if (i < 0 || i >= KDA_APPEND - 1)
497 "-i index must be between zero and %d.",
498 (int)KDA_APPEND - 2);
529 errx(EX_USAGE, "The -z and -Z options are mutually exclusive.");
532 errx(EX_USAGE, "The -i and -r options are mutually exclusive.");
546 if (cipher != KERNELDUMP_ENC_NONE && pubkeyfile == NULL) {
547 errx(EX_USAGE, "-C option requires a public key file.");
548 } else if (pubkeyfile != NULL) {
549 ERR_load_crypto_strings();
552 if (pubkeyfile != NULL)
553 errx(EX_UNAVAILABLE,"Unable to use the public key."
554 " Recompile dumpon with OpenSSL support.");
557 if (server != NULL && client != NULL) {
560 } else if (server == NULL && client == NULL && argc > 0) {
561 if (strcmp(argv[0], "off") == 0) {
568 if (strcmp(dev, _PATH_DEVNULL) == 0) {
570 * Netdump has its own configuration tracking that
571 * is not removed when using /dev/null.
573 fd = open(_PATH_NETDUMP, O_RDONLY);
575 bzero(&ndconf, sizeof(ndconf));
576 ndconf.kda_index = KDA_REMOVE_ALL;
577 ndconf.kda_af = AF_INET;
578 error = ioctl(fd, DIOCSKERNELDUMP, &ndconf);
580 err(1, "ioctl(%s, DIOCSKERNELDUMP)",
588 fd = opendumpdev(dev, dumpdev);
589 if (!netdump && !gzip && !zstd && !rflag)
590 check_size(fd, dumpdev);
593 bzero(kdap, sizeof(*kdap));
596 kdap->kda_index = KDA_REMOVE;
598 kdap->kda_index = ins_idx;
600 kdap->kda_compression = KERNELDUMP_COMP_NONE;
602 kdap->kda_compression = KERNELDUMP_COMP_ZSTD;
604 kdap->kda_compression = KERNELDUMP_COMP_GZIP;
607 memset(&hints, 0, sizeof(hints));
608 hints.ai_family = AF_INET;
609 hints.ai_protocol = IPPROTO_UDP;
611 error = getaddrinfo(server, NULL, &hints, &res);
613 if (error == EAI_SYSTEM)
614 err(EX_OSERR, "%s", gai_strerror(error));
615 errx(EX_NOHOST, "%s", gai_strerror(error));
618 ((struct sockaddr_in *)(void *)res->ai_addr)->sin_addr);
621 if (strlcpy(ndconf.kda_iface, argv[0],
622 sizeof(ndconf.kda_iface)) >= sizeof(ndconf.kda_iface))
623 errx(EX_USAGE, "invalid interface name '%s'", argv[0]);
624 if (inet_aton(server, &ndconf.kda_server.in4) == 0)
625 errx(EX_USAGE, "invalid server address '%s'", server);
626 if (inet_aton(client, &ndconf.kda_client.in4) == 0)
627 errx(EX_USAGE, "invalid client address '%s'", client);
629 if (gateway == NULL) {
630 gateway = find_gateway(argv[0]);
631 if (gateway == NULL) {
634 "failed to look up gateway for %s\n",
639 if (inet_aton(gateway, &ndconf.kda_gateway.in4) == 0)
640 errx(EX_USAGE, "invalid gateway address '%s'", gateway);
641 ndconf.kda_af = AF_INET;
645 if (pubkeyfile != NULL) {
646 kdap->kda_encryption = cipher;
647 genkey(pubkeyfile, kdap);
650 error = ioctl(fd, DIOCSKERNELDUMP, kdap);
653 explicit_bzero(kdap->kda_encryptedkey, kdap->kda_encryptedkeysize);
654 free(kdap->kda_encryptedkey);
655 explicit_bzero(kdap, sizeof(*kdap));
659 * Be slightly less user-hostile for some common
660 * errors, especially as users don't have any great
661 * discoverability into which NICs support netdump.
664 errx(EX_OSERR, "Unable to configure netdump "
665 "because the interface's link is down.");
666 else if (error == ENODEV)
667 errx(EX_OSERR, "Unable to configure netdump "
668 "because the interface driver does not yet "
671 errc(EX_OSERR, error, "ioctl(DIOCSKERNELDUMP)");