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>
53 #include <capsicum_helpers.h>
68 #include <arpa/inet.h>
71 #include <net/if_dl.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/netdump/netdump.h>
78 #include <openssl/err.h>
79 #include <openssl/pem.h>
80 #include <openssl/rand.h>
81 #include <openssl/rsa.h>
90 "usage: dumpon [-i index] [-r] [-v] [-k <pubkey>] [-Zz] <device>\n"
91 " dumpon [-i index] [-r] [-v] [-k <pubkey>] [-Zz]\n"
92 " [-g <gateway>] -s <server> -c <client> <iface>\n"
99 * Look for a default route on the specified interface.
102 find_gateway(const char *ifname)
104 struct ifaddrs *ifa, *ifap;
105 struct rt_msghdr *rtm;
107 struct sockaddr_dl *sdl;
108 struct sockaddr_in *dst, *mask, *gw;
109 char *buf, *next, *ret;
111 int error, i, ifindex, mib[7];
113 /* First look up the interface index. */
114 if (getifaddrs(&ifap) != 0)
115 err(EX_OSERR, "getifaddrs");
116 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
117 if (ifa->ifa_addr->sa_family != AF_LINK)
119 if (strcmp(ifa->ifa_name, ifname) == 0) {
120 sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
121 ifindex = sdl->sdl_index;
126 errx(1, "couldn't find interface index for '%s'", ifname);
129 /* Now get the IPv4 routing table. */
134 mib[4] = NET_RT_DUMP;
136 mib[6] = -1; /* FIB */
139 if (sysctl(mib, nitems(mib), NULL, &sz, NULL, 0) != 0)
140 err(EX_OSERR, "sysctl(NET_RT_DUMP)");
142 error = sysctl(mib, nitems(mib), buf, &sz, NULL, 0);
146 err(EX_OSERR, "sysctl(NET_RT_DUMP)");
151 for (next = buf; next < buf + sz; next += rtm->rtm_msglen) {
152 rtm = (struct rt_msghdr *)(void *)next;
153 if (rtm->rtm_version != RTM_VERSION)
155 if ((rtm->rtm_flags & RTF_GATEWAY) == 0 ||
156 rtm->rtm_index != ifindex)
159 dst = gw = mask = NULL;
160 sa = (struct sockaddr *)(rtm + 1);
161 for (i = 0; i < RTAX_MAX; i++) {
162 if ((rtm->rtm_addrs & (1 << i)) != 0) {
175 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
178 if (dst->sin_addr.s_addr == INADDR_ANY &&
179 mask->sin_addr.s_addr == 0) {
180 ret = inet_ntoa(gw->sin_addr);
189 check_size(int fd, const char *fn)
191 int name[] = { CTL_HW, HW_PHYSMEM };
192 size_t namelen = nitems(name);
193 unsigned long physmem;
198 len = sizeof(minidump);
199 if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 &&
202 len = sizeof(physmem);
203 if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0)
204 err(EX_OSERR, "can't get memory size");
205 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0)
206 err(EX_OSERR, "%s: can't get size", fn);
207 if ((uintmax_t)mediasize < (uintmax_t)physmem)
208 errx(EX_IOERR, "%s is smaller than physical memory", fn);
213 genkey(const char *pubkeyfile, struct diocskerneldump_arg *kdap)
218 assert(pubkeyfile != NULL);
219 assert(kdap != NULL);
224 fp = fopen(pubkeyfile, "r");
226 err(1, "Unable to open %s", pubkeyfile);
229 * Obsolescent OpenSSL only knows about /dev/random, and needs to
230 * pre-seed before entering cap mode. For whatever reason,
231 * RSA_pub_encrypt uses the internal PRNG.
233 #if OPENSSL_VERSION_NUMBER < 0x10100000L
240 if (caph_enter() < 0)
241 err(1, "Unable to enter capability mode");
244 if (pubkey == NULL) {
245 errx(1, "Unable to allocate an RSA structure: %s",
246 ERR_error_string(ERR_get_error(), NULL));
249 pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL);
253 errx(1, "Unable to read data from %s.", pubkeyfile);
256 * RSA keys under ~1024 bits are trivially factorable (2018). OpenSSL
257 * provides an API for RSA keys to estimate the symmetric-cipher
258 * "equivalent" bits of security (defined in NIST SP800-57), which as
259 * of this writing equates a 2048-bit RSA key to 112 symmetric cipher
262 * Use this API as a seatbelt to avoid suggesting to users that their
263 * privacy is protected by encryption when the key size is insufficient
264 * to prevent compromise via factoring.
266 * Future work: Sanity check for weak 'e', and sanity check for absence
267 * of 'd' (i.e., the supplied key is a public key rather than a full
270 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
271 if (RSA_security_bits(pubkey) < 112)
273 if (RSA_size(pubkey) * 8 < 2048)
275 errx(1, "Small RSA keys (you provided: %db) can be "
276 "factored cheaply. Please generate a larger key.",
277 RSA_size(pubkey) * 8);
279 kdap->kda_encryptedkeysize = RSA_size(pubkey);
280 if (kdap->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE) {
281 errx(1, "Public key has to be at most %db long.",
282 8 * KERNELDUMP_ENCKEY_MAX_SIZE);
285 kdap->kda_encryptedkey = calloc(1, kdap->kda_encryptedkeysize);
286 if (kdap->kda_encryptedkey == NULL)
287 err(1, "Unable to allocate encrypted key");
290 * If no cipher was specified, choose a reasonable default.
292 if (kdap->kda_encryption == KERNELDUMP_ENC_NONE)
293 kdap->kda_encryption = KERNELDUMP_ENC_CHACHA20;
294 else if (kdap->kda_encryption == KERNELDUMP_ENC_AES_256_CBC &&
295 kdap->kda_compression != KERNELDUMP_COMP_NONE)
296 errx(EX_USAGE, "Unpadded AES256-CBC mode cannot be used "
297 "with compression.");
299 arc4random_buf(kdap->kda_key, sizeof(kdap->kda_key));
300 if (RSA_public_encrypt(sizeof(kdap->kda_key), kdap->kda_key,
301 kdap->kda_encryptedkey, pubkey,
302 RSA_PKCS1_OAEP_PADDING) != (int)kdap->kda_encryptedkeysize) {
303 errx(1, "Unable to encrypt the one-time key: %s",
304 ERR_error_string(ERR_get_error(), NULL));
315 char dumpdev[PATH_MAX];
316 struct diocskerneldump_arg ndconf;
318 const char *sysctlname = "kern.shutdown.dumpdevname";
321 len = sizeof(dumpdev);
322 if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) {
323 if (errno == ENOMEM) {
324 err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n",
327 err(EX_OSERR, "Sysctl get '%s'\n", sysctlname);
330 if (strlen(dumpdev) == 0)
331 (void)strlcpy(dumpdev, _PATH_DEVNULL, sizeof(dumpdev));
337 printf("kernel dumps on priority: device\n");
340 while ((dd = strsep(&ctx, ",")) != NULL)
341 printf("%u: %s\n", idx++, dd);
343 printf("%s\n", dumpdev);
345 /* If netdump is enabled, print the configuration parameters. */
347 fd = open(_PATH_NETDUMP, O_RDONLY);
350 err(EX_OSERR, "opening %s", _PATH_NETDUMP);
353 if (ioctl(fd, DIOCGKERNELDUMP, &ndconf) != 0) {
355 err(EX_OSERR, "ioctl(DIOCGKERNELDUMP)");
360 printf("server address: %s\n",
361 inet_ntop(ndconf.kda_af, &ndconf.kda_server, ip,
363 printf("client address: %s\n",
364 inet_ntop(ndconf.kda_af, &ndconf.kda_client, ip,
366 printf("gateway address: %s\n",
367 inet_ntop(ndconf.kda_af, &ndconf.kda_gateway, ip,
374 opendumpdev(const char *arg, char *dumpdev)
378 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
379 strlcpy(dumpdev, arg, PATH_MAX);
381 i = snprintf(dumpdev, PATH_MAX, "%s%s", _PATH_DEV, arg);
383 err(EX_OSERR, "%s", arg);
385 errc(EX_DATAERR, EINVAL, "%s", arg);
388 fd = open(dumpdev, O_RDONLY);
390 err(EX_OSFILE, "%s", dumpdev);
395 main(int argc, char *argv[])
397 char dumpdev[PATH_MAX];
398 struct diocskerneldump_arg ndconf, *kdap;
399 struct addrinfo hints, *res;
400 const char *dev, *pubkeyfile, *server, *client, *gateway;
401 int ch, error, fd, cipher;
402 bool gzip, list, netdump, zstd, insert, rflag;
405 gzip = list = netdump = zstd = insert = rflag = false;
408 server = client = gateway = NULL;
409 ins_idx = KDA_APPEND;
410 cipher = KERNELDUMP_ENC_NONE;
412 while ((ch = getopt(argc, argv, "C:c:g:i:k:lrs:vZz")) != -1)
415 if (strcasecmp(optarg, "chacha") == 0 ||
416 strcasecmp(optarg, "chacha20") == 0)
417 cipher = KERNELDUMP_ENC_CHACHA20;
418 else if (strcasecmp(optarg, "aes-cbc") == 0 ||
419 strcasecmp(optarg, "aes256-cbc") == 0)
420 cipher = KERNELDUMP_ENC_AES_256_CBC;
422 errx(EX_USAGE, "Unrecognized cipher algorithm "
436 if (i < 0 || i >= KDA_APPEND - 1)
438 "-i index must be between zero and %d.",
439 (int)KDA_APPEND - 2);
470 errx(EX_USAGE, "The -z and -Z options are mutually exclusive.");
473 errx(EX_USAGE, "The -i and -r options are mutually exclusive.");
487 if (cipher != KERNELDUMP_ENC_NONE && pubkeyfile == NULL) {
488 errx(EX_USAGE, "-C option requires a public key file.");
489 } else if (pubkeyfile != NULL) {
490 ERR_load_crypto_strings();
493 if (pubkeyfile != NULL)
494 errx(EX_UNAVAILABLE,"Unable to use the public key."
495 " Recompile dumpon with OpenSSL support.");
498 if (server != NULL && client != NULL) {
501 } else if (server == NULL && client == NULL && argc > 0) {
502 if (strcmp(argv[0], "off") == 0) {
511 fd = opendumpdev(dev, dumpdev);
512 if (!netdump && !gzip && !zstd && !rflag)
513 check_size(fd, dumpdev);
516 bzero(kdap, sizeof(*kdap));
519 kdap->kda_index = KDA_REMOVE;
521 kdap->kda_index = ins_idx;
523 kdap->kda_compression = KERNELDUMP_COMP_NONE;
525 kdap->kda_compression = KERNELDUMP_COMP_ZSTD;
527 kdap->kda_compression = KERNELDUMP_COMP_GZIP;
530 memset(&hints, 0, sizeof(hints));
531 hints.ai_family = AF_INET;
532 hints.ai_protocol = IPPROTO_UDP;
534 error = getaddrinfo(server, NULL, &hints, &res);
536 err(1, "%s", gai_strerror(error));
538 errx(1, "failed to resolve '%s'", server);
540 ((struct sockaddr_in *)(void *)res->ai_addr)->sin_addr);
543 if (strlcpy(ndconf.kda_iface, argv[0],
544 sizeof(ndconf.kda_iface)) >= sizeof(ndconf.kda_iface))
545 errx(EX_USAGE, "invalid interface name '%s'", argv[0]);
546 if (inet_aton(server, &ndconf.kda_server.in4) == 0)
547 errx(EX_USAGE, "invalid server address '%s'", server);
548 if (inet_aton(client, &ndconf.kda_client.in4) == 0)
549 errx(EX_USAGE, "invalid client address '%s'", client);
551 if (gateway == NULL) {
552 gateway = find_gateway(argv[0]);
553 if (gateway == NULL) {
556 "failed to look up gateway for %s\n",
561 if (inet_aton(gateway, &ndconf.kda_gateway.in4) == 0)
562 errx(EX_USAGE, "invalid gateway address '%s'", gateway);
563 ndconf.kda_af = AF_INET;
567 if (pubkeyfile != NULL) {
568 kdap->kda_encryption = cipher;
569 genkey(pubkeyfile, kdap);
572 error = ioctl(fd, DIOCSKERNELDUMP, kdap);
575 explicit_bzero(kdap->kda_encryptedkey, kdap->kda_encryptedkeysize);
576 free(kdap->kda_encryptedkey);
577 explicit_bzero(kdap, sizeof(*kdap));
581 * Be slightly less user-hostile for some common
582 * errors, especially as users don't have any great
583 * discoverability into which NICs support netdump.
586 errx(EX_OSERR, "Unable to configure netdump "
587 "because the interface's link is down.");
588 else if (error == ENODEV)
589 errx(EX_OSERR, "Unable to configure netdump "
590 "because the interface driver does not yet "
593 errc(EX_OSERR, error, "ioctl(DIOCSKERNELDUMP)");