1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char copyright[] = 33 "@(#) Copyright (c) 1980, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "From: @(#)swapon.c 8.1 (Berkeley) 6/5/93"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/capsicum.h> 46 #include <sys/disk.h> 47 #include <sys/sysctl.h> 48 49 #include <assert.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <paths.h> 54 #include <stdbool.h> 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <sysexits.h> 60 #include <unistd.h> 61 62 #ifdef HAVE_CRYPTO 63 #include <openssl/err.h> 64 #include <openssl/pem.h> 65 #include <openssl/rsa.h> 66 #endif 67 68 static int verbose; 69 70 static void 71 usage(void) 72 { 73 fprintf(stderr, "%s\n%s\n%s\n", 74 "usage: dumpon [-v] [-k public_key_file] special_file", 75 " dumpon [-v] off", 76 " dumpon [-v] -l"); 77 exit(EX_USAGE); 78 } 79 80 static void 81 check_size(int fd, const char *fn) 82 { 83 int name[] = { CTL_HW, HW_PHYSMEM }; 84 size_t namelen = nitems(name); 85 unsigned long physmem; 86 size_t len; 87 off_t mediasize; 88 int minidump; 89 90 len = sizeof(minidump); 91 if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 && 92 minidump == 1) 93 return; 94 len = sizeof(physmem); 95 if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0) 96 err(EX_OSERR, "can't get memory size"); 97 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0) 98 err(EX_OSERR, "%s: can't get size", fn); 99 if ((uintmax_t)mediasize < (uintmax_t)physmem) { 100 if (verbose) 101 printf("%s is smaller than physical memory\n", fn); 102 exit(EX_IOERR); 103 } 104 } 105 106 #ifdef HAVE_CRYPTO 107 static void 108 genkey(const char *pubkeyfile, struct diocskerneldump_arg *kda) 109 { 110 FILE *fp; 111 RSA *pubkey; 112 113 assert(pubkeyfile != NULL); 114 assert(kda != NULL); 115 116 fp = NULL; 117 pubkey = NULL; 118 119 fp = fopen(pubkeyfile, "r"); 120 if (fp == NULL) 121 err(1, "Unable to open %s", pubkeyfile); 122 123 if (cap_enter() < 0 && errno != ENOSYS) 124 err(1, "Unable to enter capability mode"); 125 126 pubkey = RSA_new(); 127 if (pubkey == NULL) { 128 errx(1, "Unable to allocate an RSA structure: %s", 129 ERR_error_string(ERR_get_error(), NULL)); 130 } 131 132 pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL); 133 fclose(fp); 134 fp = NULL; 135 if (pubkey == NULL) 136 errx(1, "Unable to read data from %s.", pubkeyfile); 137 138 kda->kda_encryptedkeysize = RSA_size(pubkey); 139 if (kda->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE) { 140 errx(1, "Public key has to be at most %db long.", 141 8 * KERNELDUMP_ENCKEY_MAX_SIZE); 142 } 143 144 kda->kda_encryptedkey = calloc(1, kda->kda_encryptedkeysize); 145 if (kda->kda_encryptedkey == NULL) 146 err(1, "Unable to allocate encrypted key"); 147 148 kda->kda_encryption = KERNELDUMP_ENC_AES_256_CBC; 149 arc4random_buf(kda->kda_key, sizeof(kda->kda_key)); 150 if (RSA_public_encrypt(sizeof(kda->kda_key), kda->kda_key, 151 kda->kda_encryptedkey, pubkey, 152 RSA_PKCS1_PADDING) != (int)kda->kda_encryptedkeysize) { 153 errx(1, "Unable to encrypt the one-time key."); 154 } 155 RSA_free(pubkey); 156 } 157 #endif 158 159 static void 160 listdumpdev(void) 161 { 162 char dumpdev[PATH_MAX]; 163 size_t len; 164 const char *sysctlname = "kern.shutdown.dumpdevname"; 165 166 len = sizeof(dumpdev); 167 if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) { 168 if (errno == ENOMEM) { 169 err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n", 170 sysctlname); 171 } else { 172 err(EX_OSERR, "Sysctl get '%s'\n", sysctlname); 173 } 174 } 175 if (verbose) { 176 printf("kernel dumps on "); 177 } 178 if (strlen(dumpdev) == 0) { 179 printf("%s\n", _PATH_DEVNULL); 180 } else { 181 printf("%s\n", dumpdev); 182 } 183 } 184 185 int 186 main(int argc, char *argv[]) 187 { 188 struct diocskerneldump_arg kda; 189 const char *pubkeyfile; 190 int ch; 191 int i, fd; 192 int do_listdumpdev = 0; 193 bool enable; 194 195 pubkeyfile = NULL; 196 197 while ((ch = getopt(argc, argv, "k:lv")) != -1) 198 switch((char)ch) { 199 case 'k': 200 pubkeyfile = optarg; 201 break; 202 case 'l': 203 do_listdumpdev = 1; 204 break; 205 case 'v': 206 verbose = 1; 207 break; 208 default: 209 usage(); 210 } 211 212 argc -= optind; 213 argv += optind; 214 215 if (do_listdumpdev) { 216 listdumpdev(); 217 exit(EX_OK); 218 } 219 220 if (argc != 1) 221 usage(); 222 223 enable = (strcmp(argv[0], "off") != 0); 224 #ifndef HAVE_CRYPTO 225 if (pubkeyfile != NULL) { 226 enable = false; 227 warnx("Unable to use the public key. Recompile dumpon with OpenSSL support."); 228 } 229 #endif 230 231 if (enable) { 232 char tmp[PATH_MAX]; 233 char *dumpdev; 234 235 if (strncmp(argv[0], _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) { 236 dumpdev = argv[0]; 237 } else { 238 i = snprintf(tmp, PATH_MAX, "%s%s", _PATH_DEV, argv[0]); 239 if (i < 0) { 240 err(EX_OSERR, "%s", argv[0]); 241 } else if (i >= PATH_MAX) { 242 errno = EINVAL; 243 err(EX_DATAERR, "%s", argv[0]); 244 } 245 dumpdev = tmp; 246 } 247 fd = open(dumpdev, O_RDONLY); 248 if (fd < 0) 249 err(EX_OSFILE, "%s", dumpdev); 250 check_size(fd, dumpdev); 251 bzero(&kda, sizeof(kda)); 252 253 kda.kda_enable = 0; 254 i = ioctl(fd, DIOCSKERNELDUMP, &kda); 255 explicit_bzero(&kda, sizeof(kda)); 256 257 #ifdef HAVE_CRYPTO 258 if (pubkeyfile != NULL) 259 genkey(pubkeyfile, &kda); 260 #endif 261 262 kda.kda_enable = 1; 263 i = ioctl(fd, DIOCSKERNELDUMP, &kda); 264 explicit_bzero(kda.kda_encryptedkey, kda.kda_encryptedkeysize); 265 free(kda.kda_encryptedkey); 266 explicit_bzero(&kda, sizeof(kda)); 267 if (i == 0 && verbose) 268 printf("kernel dumps on %s\n", dumpdev); 269 } else { 270 fd = open(_PATH_DEVNULL, O_RDONLY); 271 if (fd < 0) 272 err(EX_OSFILE, "%s", _PATH_DEVNULL); 273 274 kda.kda_enable = 0; 275 i = ioctl(fd, DIOCSKERNELDUMP, &kda); 276 explicit_bzero(&kda, sizeof(kda)); 277 if (i == 0 && verbose) 278 printf("kernel dumps disabled\n"); 279 } 280 if (i < 0) 281 err(EX_OSERR, "ioctl(DIOCSKERNELDUMP)"); 282 283 exit (0); 284 } 285