From owner-svn-src-all@freebsd.org Sat Dec 10 16:20:41 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9958AC709F3; Sat, 10 Dec 2016 16:20:41 +0000 (UTC) (envelope-from def@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 35AFF1C43; Sat, 10 Dec 2016 16:20:41 +0000 (UTC) (envelope-from def@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uBAGKe5O033787; Sat, 10 Dec 2016 16:20:40 GMT (envelope-from def@FreeBSD.org) Received: (from def@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uBAGKdUg033773; Sat, 10 Dec 2016 16:20:39 GMT (envelope-from def@FreeBSD.org) Message-Id: <201612101620.uBAGKdUg033773@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: def set sender to def@FreeBSD.org using -f From: Konrad Witaszczyk Date: Sat, 10 Dec 2016 16:20:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r309818 - in head: etc/defaults etc/rc.d sbin sbin/decryptcore sbin/dumpon sbin/savecore share/man/man5 sys/amd64/amd64 sys/arm/arm sys/arm64/arm64 sys/conf sys/ddb sys/dev/null sys/geo... X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 10 Dec 2016 16:20:41 -0000 Author: def Date: Sat Dec 10 16:20:39 2016 New Revision: 309818 URL: https://svnweb.freebsd.org/changeset/base/309818 Log: Add support for encrypted kernel crash dumps. Changes include modifications in kernel crash dump routines, dumpon(8) and savecore(8). A new tool called decryptcore(8) was added. A new DIOCSKERNELDUMP I/O control was added to send a kernel crash dump configuration in the diocskerneldump_arg structure to the kernel. The old DIOCSKERNELDUMP I/O control was renamed to DIOCSKERNELDUMP_FREEBSD11 for backward ABI compatibility. dumpon(8) generates an one-time random symmetric key and encrypts it using an RSA public key in capability mode. Currently only AES-256-CBC is supported but EKCD was designed to implement support for other algorithms in the future. The public key is chosen using the -k flag. The dumpon rc(8) script can do this automatically during startup using the dumppubkey rc.conf(5) variable. Once the keys are calculated dumpon sends them to the kernel via DIOCSKERNELDUMP I/O control. When the kernel receives the DIOCSKERNELDUMP I/O control it generates a random IV and sets up the key schedule for the specified algorithm. Each time the kernel tries to write a crash dump to the dump device, the IV is replaced by a SHA-256 hash of the previous value. This is intended to make a possible differential cryptanalysis harder since it is possible to write multiple crash dumps without reboot by repeating the following commands: # sysctl debug.kdb.enter=1 db> call doadump(0) db> continue # savecore A kernel dump key consists of an algorithm identifier, an IV and an encrypted symmetric key. The kernel dump key size is included in a kernel dump header. The size is an unsigned 32-bit integer and it is aligned to a block size. The header structure has 512 bytes to match the block size so it was required to make a panic string 4 bytes shorter to add a new field to the header structure. If the kernel dump key size in the header is nonzero it is assumed that the kernel dump key is placed after the first header on the dump device and the core dump is encrypted. Separate functions were implemented to write the kernel dump header and the kernel dump key as they need to be unencrypted. The dump_write function encrypts data if the kernel was compiled with the EKCD option. Encrypted kernel textdumps are not supported due to the way they are constructed which makes it impossible to use the CBC mode for encryption. It should be also noted that textdumps don't contain sensitive data by design as a user decides what information should be dumped. savecore(8) writes the kernel dump key to a key.# file if its size in the header is nonzero. # is the number of the current core dump. decryptcore(8) decrypts the core dump using a private RSA key and the kernel dump key. This is performed by a child process in capability mode. If the decryption was not successful the parent process removes a partially decrypted core dump. Description on how to encrypt crash dumps was added to the decryptcore(8), dumpon(8), rc.conf(5) and savecore(8) manual pages. EKCD was tested on amd64 using bhyve and i386, mipsel and sparc64 using QEMU. The feature still has to be tested on arm and arm64 as it wasn't possible to run FreeBSD due to the problems with QEMU emulation and lack of hardware. Designed by: def, pjd Reviewed by: cem, oshogbo, pjd Partial review: delphij, emaste, jhb, kib Approved by: pjd (mentor) Differential Revision: https://reviews.freebsd.org/D4712 Added: head/sbin/decryptcore/ head/sbin/decryptcore/Makefile (contents, props changed) head/sbin/decryptcore/decryptcore.8 (contents, props changed) head/sbin/decryptcore/decryptcore.c (contents, props changed) Modified: head/etc/defaults/rc.conf head/etc/rc.d/dumpon head/sbin/Makefile head/sbin/dumpon/Makefile head/sbin/dumpon/dumpon.8 head/sbin/dumpon/dumpon.c head/sbin/savecore/savecore.8 head/sbin/savecore/savecore.c head/share/man/man5/rc.conf.5 head/sys/amd64/amd64/minidump_machdep.c head/sys/arm/arm/minidump_machdep.c head/sys/arm64/arm64/minidump_machdep.c head/sys/conf/NOTES head/sys/conf/files head/sys/conf/options head/sys/ddb/db_textdump.c head/sys/dev/null/null.c head/sys/geom/geom_dev.c head/sys/i386/i386/minidump_machdep.c head/sys/kern/kern_dump.c head/sys/kern/kern_shutdown.c head/sys/mips/mips/minidump_machdep.c head/sys/sparc64/sparc64/dump_machdep.c head/sys/sys/conf.h head/sys/sys/disk.h head/sys/sys/kerneldump.h Modified: head/etc/defaults/rc.conf ============================================================================== --- head/etc/defaults/rc.conf Sat Dec 10 15:33:36 2016 (r309817) +++ head/etc/defaults/rc.conf Sat Dec 10 16:20:39 2016 (r309818) @@ -607,6 +607,8 @@ chkprintcap_enable="NO" # Run chkprintca chkprintcap_flags="-d" # Create missing directories by default. dumpdev="AUTO" # Device to crashdump to (device name, AUTO, or NO). dumpdir="/var/crash" # Directory where crash dumps are to be stored +dumppubkey="" # Public key for encrypted kernel crash dumps. + # See dumpon(8) for more details. savecore_enable="YES" # Extract core from dump devices if any savecore_flags="-m 10" # Used if dumpdev is enabled above, and present. # By default, only the 10 most recent kernel dumps Modified: head/etc/rc.d/dumpon ============================================================================== --- head/etc/rc.d/dumpon Sat Dec 10 15:33:36 2016 (r309817) +++ head/etc/rc.d/dumpon Sat Dec 10 16:20:39 2016 (r309818) @@ -16,7 +16,12 @@ stop_cmd="dumpon_stop" dumpon_try() { - if /sbin/dumpon "${1}" ; then + if [ -n "${dumppubkey}" ]; then + /sbin/dumpon -k "${dumppubkey}" "${1}" + else + /sbin/dumpon "${1}" + fi + if [ $? -eq 0 ]; then # Make a symlink in devfs for savecore ln -fs "${1}" /dev/dumpdev return 0 Modified: head/sbin/Makefile ============================================================================== --- head/sbin/Makefile Sat Dec 10 15:33:36 2016 (r309817) +++ head/sbin/Makefile Sat Dec 10 16:20:39 2016 (r309818) @@ -83,6 +83,7 @@ SUBDIR.${MK_IPFW}+= natd SUBDIR.${MK_ISCSI}+= iscontrol SUBDIR.${MK_NAND}+= nandfs SUBDIR.${MK_NAND}+= newfs_nandfs +SUBDIR.${MK_OPENSSL}+= decryptcore SUBDIR.${MK_PF}+= pfctl SUBDIR.${MK_PF}+= pflogd SUBDIR.${MK_QUOTAS}+= quotacheck Added: head/sbin/decryptcore/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/decryptcore/Makefile Sat Dec 10 16:20:39 2016 (r309818) @@ -0,0 +1,13 @@ +# $FreeBSD$ + +PROG= decryptcore + +LIBADD= crypto pjdlog + +MAN= decryptcore.8 + +CFLAGS+=-I${.CURDIR}/../../lib/libpjdlog + +WARNS?= 6 + +.include Added: head/sbin/decryptcore/decryptcore.8 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/decryptcore/decryptcore.8 Sat Dec 10 16:20:39 2016 (r309818) @@ -0,0 +1,114 @@ +.\" Copyright (c) 2016 Konrad Witaszczyk +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd December 10, 2016 +.Dt DECRYPTCORE 8 +.Os +.Sh NAME +.Nm decryptcore +.Nd "decrypt a core dump of the operating system" +.Sh SYNOPSIS +.Nm +.Op Fl Lv +.Fl p Ar privatekeyfile +.Fl k Ar keyfile +.Fl e Ar encryptedcore +.Fl c Ar core +.Nm +.Op Fl Lv +.Op Fl d Ar crashdir +.Fl p Ar privatekeyfile +.Fl n Ar dumpnr +.Sh DESCRIPTION +The +.Nm +first decrypts +.Ar keyfile +using +.Ar privatekeyfile +and then uses the resulting key to decrypt +.Ar encryptedcore +saved by +.Xr savecore 8 . +Result is saved in +.Ar core . +.Pp +Alternatively a user can decrypt a core dump numbered +.Ar dumpnr +from the +.Ar crashdir +directory. +In this case a dump key from the +.Pa key.# +file is used and the result is saved in the +.Pa vmcore.# +file where +.Dq # +corresponds to +.Ar dumpnr . +.Pp +The +.Nm +utility can be started with the following command line arguments: +.Bl -tag -width ".Fl e Ar encryptedcore" +.It Fl L +Write log messages to +.Xr syslogd 8 . +.It Fl v +Print or log verbose/debugging information. +This option can be specified multiple times to raise the verbosity +level. +.It Fl p Ar privatekeyfile +Specify location of a private key file which will be used to decrypt a dump key +file. +.It Fl k Ar keyfile +Specify location of a dump key file. +.It Fl e Ar encrytpedcore +Specify location of an encrypted core. +.It Fl c Ar core +Specify location of a resulting decrypted core dump. +.It Fl d Ar crashdir +Specify an alternative crash dump directory. The default crash dump directory is +.Pa /var/crash . +.It Fl n Ar dumpnr +Specify a number of a crash dump to be decrypted. +.El +.Sh EXIT STATUS +The +.Nm +utility exits 0 on success, and >0 if an error occurs. +.Sh SEE ALSO +.Xr capsicum 4 , +.Xr dumpon 8 , +.Xr kgdb 1 , +.Xr savecore 8 , +.Xr syslogd 8 +.Sh AUTHORS +The +.Nm +was implemented by +.An -nosplit +.An Konrad Witaszczyk Aq Mt def@FreeBSD.org . Added: head/sbin/decryptcore/decryptcore.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/decryptcore/decryptcore.c Sat Dec 10 16:20:39 2016 (r309818) @@ -0,0 +1,373 @@ +/*- + * Copyright (c) 2016 Konrad Witaszczyk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "pjdlog.h" + +#define DECRYPTCORE_CRASHDIR "/var/crash" + +static void +usage(void) +{ + + pjdlog_exitx(1, + "usage: decryptcore [-Lv] -p privatekeyfile -k keyfile -e encryptedcore -c core\n" + " decryptcore [-Lv] [-d crashdir] -p privatekeyfile -n dumpnr"); +} + +static int +wait_for_process(pid_t pid) +{ + int status; + + if (waitpid(pid, &status, WUNTRACED | WEXITED) == -1) { + pjdlog_errno(LOG_ERR, "Unable to wait for a child process"); + return (1); + } + + if (WIFEXITED(status)) + return (WEXITSTATUS(status)); + + return (1); +} + +static struct kerneldumpkey * +read_key(int kfd) +{ + struct kerneldumpkey *kdk; + ssize_t size; + size_t kdksize; + + PJDLOG_ASSERT(kfd >= 0); + + kdksize = sizeof(*kdk); + kdk = calloc(1, kdksize); + if (kdk == NULL) { + pjdlog_errno(LOG_ERR, "Unable to allocate kernel dump key"); + goto failed; + } + + size = read(kfd, kdk, kdksize); + if (size == (ssize_t)kdksize) { + kdk->kdk_encryptedkeysize = dtoh32(kdk->kdk_encryptedkeysize); + kdksize += (size_t)kdk->kdk_encryptedkeysize; + kdk = realloc(kdk, kdksize); + if (kdk == NULL) { + pjdlog_errno(LOG_ERR, "Unable to reallocate kernel dump key"); + goto failed; + } + size += read(kfd, &kdk->kdk_encryptedkey, + kdk->kdk_encryptedkeysize); + } + if (size != (ssize_t)kdksize) { + pjdlog_errno(LOG_ERR, "Unable to read key"); + goto failed; + } + + return (kdk); +failed: + free(kdk); + return (NULL); +} + +static bool +decrypt(const char *privkeyfile, const char *keyfile, const char *input, + const char *output) +{ + uint8_t buf[KERNELDUMP_BUFFER_SIZE], key[KERNELDUMP_KEY_MAX_SIZE]; + EVP_CIPHER_CTX ctx; + const EVP_CIPHER *cipher; + FILE *fp; + struct kerneldumpkey *kdk; + RSA *privkey; + int ifd, kfd, ofd, olen, privkeysize; + ssize_t bytes; + pid_t pid; + + PJDLOG_ASSERT(privkeyfile != NULL); + PJDLOG_ASSERT(keyfile != NULL); + PJDLOG_ASSERT(input != NULL); + PJDLOG_ASSERT(output != NULL); + + privkey = NULL; + + /* + * Decrypt a core dump in a child process so we can unlink a partially + * decrypted core if the child process fails. + */ + pid = fork(); + if (pid == -1) { + pjdlog_errno(LOG_ERR, "Unable to create child process"); + return (false); + } + + if (pid > 0) + return (wait_for_process(pid) == 0); + + kfd = open(keyfile, O_RDONLY); + if (kfd == -1) { + pjdlog_errno(LOG_ERR, "Unable to open %s", keyfile); + goto failed; + } + ifd = open(input, O_RDONLY); + if (ifd == -1) { + pjdlog_errno(LOG_ERR, "Unable to open %s", input); + goto failed; + } + ofd = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (ofd == -1) { + pjdlog_errno(LOG_ERR, "Unable to open %s", output); + goto failed; + } + fp = fopen(privkeyfile, "r"); + if (fp == NULL) { + pjdlog_errno(LOG_ERR, "Unable to open %s", privkeyfile); + goto failed; + } + + if (cap_enter() < 0 && errno != ENOSYS) { + pjdlog_errno(LOG_ERR, "Unable to enter capability mode"); + goto failed; + } + + privkey = RSA_new(); + if (privkey == NULL) { + pjdlog_error("Unable to allocate an RSA structure: %s", + ERR_error_string(ERR_get_error(), NULL)); + goto failed; + } + EVP_CIPHER_CTX_init(&ctx); + + kdk = read_key(kfd); + close(kfd); + if (kdk == NULL) + goto failed; + + privkey = PEM_read_RSAPrivateKey(fp, &privkey, NULL, NULL); + fclose(fp); + if (privkey == NULL) { + pjdlog_error("Unable to read data from %s.", privkeyfile); + goto failed; + } + + privkeysize = RSA_size(privkey); + if (privkeysize != (int)kdk->kdk_encryptedkeysize) { + pjdlog_error("RSA modulus size mismatch: equals %db and should be %ub.", + 8 * privkeysize, 8 * kdk->kdk_encryptedkeysize); + goto failed; + } + + switch (kdk->kdk_encryption) { + case KERNELDUMP_ENC_AES_256_CBC: + cipher = EVP_aes_256_cbc(); + break; + default: + pjdlog_error("Invalid encryption algorithm."); + goto failed; + } + + if (RSA_private_decrypt(kdk->kdk_encryptedkeysize, + kdk->kdk_encryptedkey, key, privkey, + RSA_PKCS1_PADDING) != sizeof(key)) { + pjdlog_error("Unable to decrypt key: %s", + ERR_error_string(ERR_get_error(), NULL)); + goto failed; + } + RSA_free(privkey); + privkey = NULL; + + EVP_DecryptInit_ex(&ctx, cipher, NULL, key, kdk->kdk_iv); + EVP_CIPHER_CTX_set_padding(&ctx, 0); + + explicit_bzero(key, sizeof(key)); + + do { + bytes = read(ifd, buf, sizeof(buf)); + if (bytes < 0) { + pjdlog_errno(LOG_ERR, "Unable to read data from %s", + input); + goto failed; + } else if (bytes == 0) { + break; + } + + if (bytes > 0) { + if (EVP_DecryptUpdate(&ctx, buf, &olen, buf, + bytes) == 0) { + pjdlog_error("Unable to decrypt core."); + goto failed; + } + } else { + if (EVP_DecryptFinal_ex(&ctx, buf, &olen) == 0) { + pjdlog_error("Unable to decrypt core."); + goto failed; + } + } + + if (olen == 0) + continue; + + if (write(ofd, buf, olen) != olen) { + pjdlog_errno(LOG_ERR, "Unable to write data to %s", + output); + goto failed; + } + } while (bytes > 0); + + explicit_bzero(buf, sizeof(buf)); + EVP_CIPHER_CTX_cleanup(&ctx); + exit(0); +failed: + explicit_bzero(key, sizeof(key)); + explicit_bzero(buf, sizeof(buf)); + RSA_free(privkey); + EVP_CIPHER_CTX_cleanup(&ctx); + exit(1); +} + +int +main(int argc, char **argv) +{ + char core[PATH_MAX], encryptedcore[PATH_MAX], keyfile[PATH_MAX]; + struct stat sb; + const char *crashdir, *dumpnr, *privatekey; + int ch, debug; + size_t ii; + bool usesyslog; + + pjdlog_init(PJDLOG_MODE_STD); + pjdlog_prefix_set("(decryptcore) "); + + debug = 0; + *core = '\0'; + crashdir = NULL; + dumpnr = NULL; + *encryptedcore = '\0'; + *keyfile = '\0'; + privatekey = NULL; + usesyslog = false; + while ((ch = getopt(argc, argv, "Lc:d:e:k:n:p:v")) != -1) { + switch (ch) { + case 'L': + usesyslog = true; + break; + case 'c': + strncpy(core, optarg, sizeof(core)); + break; + case 'd': + crashdir = optarg; + break; + case 'e': + strncpy(encryptedcore, optarg, sizeof(encryptedcore)); + break; + case 'k': + strncpy(keyfile, optarg, sizeof(keyfile)); + break; + case 'n': + dumpnr = optarg; + break; + case 'p': + privatekey = optarg; + break; + case 'v': + debug++; + break; + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if (argc != 0) + usage(); + + /* Verify mutually exclusive options. */ + if ((crashdir != NULL || dumpnr != NULL) && + (*keyfile != '\0' || *encryptedcore != '\0' || *core != '\0')) { + usage(); + } + + /* + * Set key, encryptedcore and core file names using crashdir and dumpnr. + */ + if (dumpnr != NULL) { + for (ii = 0; ii < strnlen(dumpnr, PATH_MAX); ii++) { + if (isdigit((int)dumpnr[ii]) == 0) + usage(); + } + + if (crashdir == NULL) + crashdir = DECRYPTCORE_CRASHDIR; + PJDLOG_VERIFY(snprintf(keyfile, sizeof(keyfile), + "%s/key.%s", crashdir, dumpnr) > 0); + PJDLOG_VERIFY(snprintf(core, sizeof(core), + "%s/vmcore.%s", crashdir, dumpnr) > 0); + PJDLOG_VERIFY(snprintf(encryptedcore, sizeof(encryptedcore), + "%s/vmcore_encrypted.%s", crashdir, dumpnr) > 0); + } + + if (privatekey == NULL || *keyfile == '\0' || *encryptedcore == '\0' || + *core == '\0') { + usage(); + } + + if (usesyslog) + pjdlog_mode_set(PJDLOG_MODE_SYSLOG); + pjdlog_debug_set(debug); + + if (!decrypt(privatekey, keyfile, encryptedcore, core)) { + if (stat(core, &sb) == 0 && unlink(core) != 0) + pjdlog_exit(1, "Unable to remove core"); + exit(1); + } + + pjdlog_fini(); + + exit(0); +} Modified: head/sbin/dumpon/Makefile ============================================================================== --- head/sbin/dumpon/Makefile Sat Dec 10 15:33:36 2016 (r309817) +++ head/sbin/dumpon/Makefile Sat Dec 10 16:20:39 2016 (r309818) @@ -1,7 +1,15 @@ # $FreeBSD$ +.include + PACKAGE=runtime PROG= dumpon + +.if ${MK_OPENSSL} != "no" +LIBADD= crypto +CFLAGS+=-DHAVE_CRYPTO +.endif + MAN= dumpon.8 .include Modified: head/sbin/dumpon/dumpon.8 ============================================================================== --- head/sbin/dumpon/dumpon.8 Sat Dec 10 15:33:36 2016 (r309817) +++ head/sbin/dumpon/dumpon.8 Sat Dec 10 16:20:39 2016 (r309818) @@ -28,7 +28,7 @@ .\" From: @(#)swapon.8 8.1 (Berkeley) 6/5/93 .\" $FreeBSD$ .\" -.Dd October 3, 2016 +.Dd December 10, 2016 .Dt DUMPON 8 .Os .Sh NAME @@ -37,6 +37,7 @@ .Sh SYNOPSIS .Nm .Op Fl v +.Op Fl k Ar public_key_file .Ar special_file .Nm .Op Fl v @@ -56,7 +57,9 @@ normally occur from the system multi-use .Pa /etc/rc , controlled by the .Dq dumpdev -variable in the boot time configuration file +and +.Dq dumppubkey +variables in the boot time configuration file .Pa /etc/rc.conf . .Pp The default type of kernel crash dump is the mini crash dump. @@ -82,6 +85,35 @@ total amount of physical memory as repor variable. .Pp The +.Op Fl k Ar public_key_file +flag causes +.Nm +to generate a one-time key for kernel crash dump encryption. +The key will be replaced by a new one when the +.Nm +utility is run again. +The key is encrypted using +.Ar public_key_file . +This process is sandboxed using +.Xr capsicum 4 . +Both plain and encrypted keys are sent to the kernel using +.Dv DIOCSKERNELDUMP +.Xr ioctl 2 . +A user can specify the +.Ar public_key_file +in the +.Dq dumppubkey +variable defined in +.Pa /etc/rc.conf +for use with the +.Pa /etc/rc.d/dumpon +.Xr rc 8 +script. +This flag requires a kernel compiled with the +.Dv EKCD +kernel option. +.Pp +The .Fl l flag causes .Nm @@ -140,13 +172,95 @@ standard swap areas .It Pa /etc/rc.conf boot-time system configuration .El +.Sh EXAMPLES +In order to generate an RSA private key a user can use the +.Xr genrsa 1 +tool: +.Pp +.Dl # openssl genrsa -out private.pem 4096 +.Pp +A public key can be extracted from the private key using the +.Xr rsa 1 +tool: +.Pp +.Dl # openssl rsa -in private.pem -out public.pem -pubout +.Pp +Once the RSA keys are created the private key should be moved to a safe place. +Now +.Pa public.pem +can be used by +.Nm +to configure encrypted kernel crash dumps: +.Pp +.Dl # dumpon -k public.pem /dev/ada0s1b +.Pp +It is recommended to test if the kernel saves encrypted crash dumps using the +current configuration. +The easiest way to do that is to cause a kernel panic using the +.Xr ddb 4 +debugger: +.Pp +.Dl # sysctl debug.kdb.panic=1 +.Pp +In the debugger the following commands should be typed to write a core dump and +reboot: +.Pp +.Dl db> call doadump(0) +.Dl db> reset +.Pp +After reboot +.Xr savecore 8 +should be able to save the core dump in the core directory which is +.Pa /var/crash +by default: +.Pp +.Dl # savecore /var/crash /dev/ada0s1b +.Pp +Three files should be created in the core directory: +.Pa info.# , +.Pa key.# +and +.Pa vmcore_encrypted.# +where +.Dq # +is the number of the last core dump saved by +.Xr savecore 8 . +The +.Pa vmcore_encrypted.# +can be decrypted using the +.Xr decryptcore 8 +utility: +.Pp +.Dl # decryptcore -p private.pem -k key.# -e vmcore_encrypted.# -c vmcore.# +.Pp +or shorter: +.Pp +.Dl # decryptcore -p private.pem -n # +.Pp +The +.Pa vmcore.# +can be now examined using +.Xr kgdb 1 : +.Pp +.Dl # kgdb /usr/obj/sys/GENERIC/kernel.debug vmcore.# +.Pp +or shorter: +.Pp +.Dl # kgdb -n # /usr/obj/sys/GENERIC/kernel.debug +.Pp +The core was decrypted properly if +.Xr kgdb 1 +does not print any errors. .Sh SEE ALSO +.Xr kgdb 1 , +.Xr ddb 4 , .Xr fstab 5 , .Xr rc.conf 5 , .Xr config 8 , .Xr init 8 , .Xr loader 8 , .Xr rc 8 , +.Xr decryptcore 8 , .Xr savecore 8 , .Xr swapon 8 , .Xr panic 9 Modified: head/sbin/dumpon/dumpon.c ============================================================================== --- head/sbin/dumpon/dumpon.c Sat Dec 10 15:33:36 2016 (r309817) +++ head/sbin/dumpon/dumpon.c Sat Dec 10 16:20:39 2016 (r309818) @@ -42,13 +42,16 @@ static char sccsid[] = "From: @(#)swapon __FBSDID("$FreeBSD$"); #include +#include #include #include +#include #include #include #include #include +#include #include #include #include @@ -56,13 +59,19 @@ __FBSDID("$FreeBSD$"); #include #include +#ifdef HAVE_CRYPTO +#include +#include +#include +#endif + static int verbose; static void usage(void) { fprintf(stderr, "%s\n%s\n%s\n", - "usage: dumpon [-v] special_file", + "usage: dumpon [-v] [-k public_key_file] special_file", " dumpon [-v] off", " dumpon [-v] -l"); exit(EX_USAGE); @@ -94,6 +103,59 @@ check_size(int fd, const char *fn) } } +#ifdef HAVE_CRYPTO +static void +genkey(const char *pubkeyfile, struct diocskerneldump_arg *kda) +{ + FILE *fp; + RSA *pubkey; + + assert(pubkeyfile != NULL); + assert(kda != NULL); + + fp = NULL; + pubkey = NULL; + + fp = fopen(pubkeyfile, "r"); + if (fp == NULL) + err(1, "Unable to open %s", pubkeyfile); + + if (cap_enter() < 0 && errno != ENOSYS) + err(1, "Unable to enter capability mode"); + + pubkey = RSA_new(); + if (pubkey == NULL) { + errx(1, "Unable to allocate an RSA structure: %s", + ERR_error_string(ERR_get_error(), NULL)); + } + + pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL); + fclose(fp); + fp = NULL; + if (pubkey == NULL) + errx(1, "Unable to read data from %s.", pubkeyfile); + + kda->kda_encryptedkeysize = RSA_size(pubkey); + if (kda->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE) { + errx(1, "Public key has to be at most %db long.", + 8 * KERNELDUMP_ENCKEY_MAX_SIZE); + } + + kda->kda_encryptedkey = calloc(1, kda->kda_encryptedkeysize); + if (kda->kda_encryptedkey == NULL) + err(1, "Unable to allocate encrypted key"); + + kda->kda_encryption = KERNELDUMP_ENC_AES_256_CBC; + arc4random_buf(kda->kda_key, sizeof(kda->kda_key)); + if (RSA_public_encrypt(sizeof(kda->kda_key), kda->kda_key, + kda->kda_encryptedkey, pubkey, + RSA_PKCS1_PADDING) != (int)kda->kda_encryptedkeysize) { + errx(1, "Unable to encrypt the one-time key."); + } + RSA_free(pubkey); +} +#endif + static void listdumpdev(void) { @@ -123,13 +185,20 @@ listdumpdev(void) int main(int argc, char *argv[]) { + struct diocskerneldump_arg kda; + const char *pubkeyfile; int ch; int i, fd; - u_int u; int do_listdumpdev = 0; + bool enable; + + pubkeyfile = NULL; - while ((ch = getopt(argc, argv, "lv")) != -1) + while ((ch = getopt(argc, argv, "k:lv")) != -1) switch((char)ch) { + case 'k': + pubkeyfile = optarg; + break; case 'l': do_listdumpdev = 1; break; @@ -151,7 +220,15 @@ main(int argc, char *argv[]) if (argc != 1) usage(); - if (strcmp(argv[0], "off") != 0) { + enable = (strcmp(argv[0], "off") != 0); +#ifndef HAVE_CRYPTO + if (pubkeyfile != NULL) { + enable = false; + warnx("Unable to use the public key. Recompile dumpon with OpenSSL support."); + } +#endif + + if (enable) { char tmp[PATH_MAX]; char *dumpdev; @@ -171,18 +248,32 @@ main(int argc, char *argv[]) if (fd < 0) err(EX_OSFILE, "%s", dumpdev); check_size(fd, dumpdev); - u = 0; - i = ioctl(fd, DIOCSKERNELDUMP, &u); - u = 1; - i = ioctl(fd, DIOCSKERNELDUMP, &u); + bzero(&kda, sizeof(kda)); + + kda.kda_enable = 0; + i = ioctl(fd, DIOCSKERNELDUMP, &kda); + explicit_bzero(&kda, sizeof(kda)); + +#ifdef HAVE_CRYPTO + if (pubkeyfile != NULL) + genkey(pubkeyfile, &kda); +#endif + + kda.kda_enable = 1; + i = ioctl(fd, DIOCSKERNELDUMP, &kda); + explicit_bzero(kda.kda_encryptedkey, kda.kda_encryptedkeysize); + free(kda.kda_encryptedkey); + explicit_bzero(&kda, sizeof(kda)); if (i == 0 && verbose) printf("kernel dumps on %s\n", dumpdev); } else { fd = open(_PATH_DEVNULL, O_RDONLY); if (fd < 0) err(EX_OSFILE, "%s", _PATH_DEVNULL); - u = 0; - i = ioctl(fd, DIOCSKERNELDUMP, &u); + + kda.kda_enable = 0; + i = ioctl(fd, DIOCSKERNELDUMP, &kda); + explicit_bzero(&kda, sizeof(kda)); if (i == 0 && verbose) printf("kernel dumps disabled\n"); } Modified: head/sbin/savecore/savecore.8 ============================================================================== --- head/sbin/savecore/savecore.8 Sat Dec 10 15:33:36 2016 (r309817) +++ head/sbin/savecore/savecore.8 Sat Dec 10 16:20:39 2016 (r309818) @@ -28,7 +28,7 @@ .\" From: @(#)savecore.8 8.1 (Berkeley) 6/5/93 .\" $FreeBSD$ .\" -.Dd December 1, 2015 +.Dd December 10, 2016 .Dt SAVECORE 8 .Os .Sh NAME @@ -119,6 +119,10 @@ If it passes these checks, it saves the .Ar directory Ns Pa /vmcore.# and information about the core in .Ar directory Ns Pa /info.# . +If the core is encrypted, it saves the dump key in +.Ar directory Ns Pa /key.# . +The core can be later decrypted using +.Xr decryptcore 8 . For kernel textdumps generated with the .Xr textdump 4 facility, output will be stored in the @@ -166,6 +170,7 @@ is meant to be called near the end of th .Xr xo_parse_args 3 , .Xr textdump 4 , .Xr tar 5 , +.Xr decryptcore 8 , .Xr dumpon 8 , .Xr syslogd 8 .Sh HISTORY Modified: head/sbin/savecore/savecore.c ============================================================================== --- head/sbin/savecore/savecore.c Sat Dec 10 15:33:36 2016 (r309817) +++ head/sbin/savecore/savecore.c Sat Dec 10 16:20:39 2016 (r309818) @@ -74,6 +74,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -183,6 +184,28 @@ writebounds(int bounds) { fclose(fp); } +static bool +writekey(const char *keyname, uint8_t *dumpkey, uint32_t dumpkeysize) +{ + int fd; + + fd = open(keyname, O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (fd == -1) { + syslog(LOG_ERR, "Unable to open %s to write the key: %m.", + keyname); + return (false); + } + + if (write(fd, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) { + syslog(LOG_ERR, "Unable to write the key to %s: %m.", keyname); + close(fd); + return (false); + } + + close(fd); + return (true); +} + static off_t file_size(const char *path) { @@ -238,8 +261,11 @@ symlinks_remove(void) { (void)unlink("info.last"); + (void)unlink("key.last"); (void)unlink("vmcore.last"); (void)unlink("vmcore.last.gz"); + (void)unlink("vmcore_encrypted.last"); + (void)unlink("vmcore_encrypted.last.gz"); (void)unlink("textdump.tar.last"); (void)unlink("textdump.tar.last.gz"); } @@ -292,8 +318,8 @@ check_space(const char *savedir, off_t d #define BLOCKMASK (~(BLOCKSIZE-1)) static int -DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device, - const char *filename, FILE *fp) +DoRegularFile(int fd, bool isencrypted, off_t dumpsize, char *buf, + const char *device, const char *filename, FILE *fp) { int he, hs, nr, nw, wl; off_t dmpcnt, origsize; @@ -315,7 +341,7 @@ DoRegularFile(int fd, off_t dumpsize, ch nerr++; return (-1); *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***