2 * Copyright (c) 2016 Konrad Witaszczyk <def@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/capsicum.h>
32 #include <sys/endian.h>
33 #include <sys/kerneldump.h>
34 #include <sys/sysctl.h>
44 #include <openssl/evp.h>
45 #include <openssl/pem.h>
46 #include <openssl/rsa.h>
47 #include <openssl/engine.h>
51 #define DECRYPTCORE_CRASHDIR "/var/crash"
58 "usage: decryptcore [-fLv] -p privatekeyfile -k keyfile -e encryptedcore -c core\n"
59 " decryptcore [-fLv] [-d crashdir] -p privatekeyfile -n dumpnr");
63 wait_for_process(pid_t pid)
67 if (waitpid(pid, &status, WUNTRACED | WEXITED) == -1) {
68 pjdlog_errno(LOG_ERR, "Unable to wait for a child process");
72 if (WIFEXITED(status))
73 return (WEXITSTATUS(status));
78 static struct kerneldumpkey *
81 struct kerneldumpkey *kdk;
85 PJDLOG_ASSERT(kfd >= 0);
87 kdksize = sizeof(*kdk);
88 kdk = calloc(1, kdksize);
90 pjdlog_errno(LOG_ERR, "Unable to allocate kernel dump key");
94 size = read(kfd, kdk, kdksize);
95 if (size == (ssize_t)kdksize) {
96 kdk->kdk_encryptedkeysize = dtoh32(kdk->kdk_encryptedkeysize);
97 kdksize += (size_t)kdk->kdk_encryptedkeysize;
98 kdk = realloc(kdk, kdksize);
100 pjdlog_errno(LOG_ERR, "Unable to reallocate kernel dump key");
103 size += read(kfd, &kdk->kdk_encryptedkey,
104 kdk->kdk_encryptedkeysize);
106 if (size != (ssize_t)kdksize) {
107 pjdlog_errno(LOG_ERR, "Unable to read key");
118 decrypt(int ofd, const char *privkeyfile, const char *keyfile,
121 uint8_t buf[KERNELDUMP_BUFFER_SIZE], key[KERNELDUMP_KEY_MAX_SIZE];
123 const EVP_CIPHER *cipher;
125 struct kerneldumpkey *kdk;
127 int ifd, kfd, olen, privkeysize;
131 PJDLOG_ASSERT(ofd >= 0);
132 PJDLOG_ASSERT(privkeyfile != NULL);
133 PJDLOG_ASSERT(keyfile != NULL);
134 PJDLOG_ASSERT(input != NULL);
139 * Decrypt a core dump in a child process so we can unlink a partially
140 * decrypted core if the child process fails.
144 pjdlog_errno(LOG_ERR, "Unable to create child process");
151 return (wait_for_process(pid) == 0);
154 kfd = open(keyfile, O_RDONLY);
156 pjdlog_errno(LOG_ERR, "Unable to open %s", keyfile);
159 ifd = open(input, O_RDONLY);
161 pjdlog_errno(LOG_ERR, "Unable to open %s", input);
164 fp = fopen(privkeyfile, "r");
166 pjdlog_errno(LOG_ERR, "Unable to open %s", privkeyfile);
170 if (cap_enter() < 0 && errno != ENOSYS) {
171 pjdlog_errno(LOG_ERR, "Unable to enter capability mode");
176 if (privkey == NULL) {
177 pjdlog_error("Unable to allocate an RSA structure: %s",
178 ERR_error_string(ERR_get_error(), NULL));
181 EVP_CIPHER_CTX_init(&ctx);
188 privkey = PEM_read_RSAPrivateKey(fp, &privkey, NULL, NULL);
190 if (privkey == NULL) {
191 pjdlog_error("Unable to read data from %s.", privkeyfile);
195 privkeysize = RSA_size(privkey);
196 if (privkeysize != (int)kdk->kdk_encryptedkeysize) {
197 pjdlog_error("RSA modulus size mismatch: equals %db and should be %ub.",
198 8 * privkeysize, 8 * kdk->kdk_encryptedkeysize);
202 switch (kdk->kdk_encryption) {
203 case KERNELDUMP_ENC_AES_256_CBC:
204 cipher = EVP_aes_256_cbc();
207 pjdlog_error("Invalid encryption algorithm.");
211 if (RSA_private_decrypt(kdk->kdk_encryptedkeysize,
212 kdk->kdk_encryptedkey, key, privkey,
213 RSA_PKCS1_PADDING) != sizeof(key)) {
214 pjdlog_error("Unable to decrypt key: %s",
215 ERR_error_string(ERR_get_error(), NULL));
221 EVP_DecryptInit_ex(&ctx, cipher, NULL, key, kdk->kdk_iv);
222 EVP_CIPHER_CTX_set_padding(&ctx, 0);
224 explicit_bzero(key, sizeof(key));
227 bytes = read(ifd, buf, sizeof(buf));
229 pjdlog_errno(LOG_ERR, "Unable to read data from %s",
235 if (EVP_DecryptUpdate(&ctx, buf, &olen, buf,
237 pjdlog_error("Unable to decrypt core.");
241 if (EVP_DecryptFinal_ex(&ctx, buf, &olen) == 0) {
242 pjdlog_error("Unable to decrypt core.");
247 if (olen > 0 && write(ofd, buf, olen) != olen) {
248 pjdlog_errno(LOG_ERR, "Unable to write core");
253 explicit_bzero(buf, sizeof(buf));
254 EVP_CIPHER_CTX_cleanup(&ctx);
257 explicit_bzero(key, sizeof(key));
258 explicit_bzero(buf, sizeof(buf));
260 EVP_CIPHER_CTX_cleanup(&ctx);
265 main(int argc, char **argv)
267 char core[PATH_MAX], encryptedcore[PATH_MAX], keyfile[PATH_MAX];
268 const char *crashdir, *dumpnr, *privatekey;
269 int ch, debug, error, ofd;
271 bool force, usesyslog;
275 pjdlog_init(PJDLOG_MODE_STD);
276 pjdlog_prefix_set("(decryptcore) ");
282 *encryptedcore = '\0';
287 while ((ch = getopt(argc, argv, "Lc:d:e:fk:n:p:v")) != -1) {
293 if (strlcpy(core, optarg, sizeof(core)) >= sizeof(core))
294 pjdlog_exitx(1, "Core file path is too long.");
300 if (strlcpy(encryptedcore, optarg,
301 sizeof(encryptedcore)) >= sizeof(encryptedcore)) {
302 pjdlog_exitx(1, "Encrypted core file path is too long.");
309 if (strlcpy(keyfile, optarg, sizeof(keyfile)) >=
311 pjdlog_exitx(1, "Key file path is too long.");
333 /* Verify mutually exclusive options. */
334 if ((crashdir != NULL || dumpnr != NULL) &&
335 (*keyfile != '\0' || *encryptedcore != '\0' || *core != '\0')) {
340 * Set key, encryptedcore and core file names using crashdir and dumpnr.
342 if (dumpnr != NULL) {
343 for (ii = 0; ii < strnlen(dumpnr, PATH_MAX); ii++) {
344 if (isdigit((int)dumpnr[ii]) == 0)
348 if (crashdir == NULL)
349 crashdir = DECRYPTCORE_CRASHDIR;
350 PJDLOG_VERIFY(snprintf(keyfile, sizeof(keyfile),
351 "%s/key.%s", crashdir, dumpnr) > 0);
352 PJDLOG_VERIFY(snprintf(core, sizeof(core),
353 "%s/vmcore.%s", crashdir, dumpnr) > 0);
354 PJDLOG_VERIFY(snprintf(encryptedcore, sizeof(encryptedcore),
355 "%s/vmcore_encrypted.%s", crashdir, dumpnr) > 0);
358 if (privatekey == NULL || *keyfile == '\0' || *encryptedcore == '\0' ||
364 pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
365 pjdlog_debug_set(debug);
367 if (force && unlink(core) == -1 && errno != ENOENT) {
368 pjdlog_errno(LOG_ERR, "Unable to remove old core");
371 ofd = open(core, O_WRONLY | O_CREAT | O_EXCL, 0600);
373 pjdlog_errno(LOG_ERR, "Unable to open %s", core);
377 if (!decrypt(ofd, privatekey, keyfile, encryptedcore)) {
378 if (unlink(core) == -1 && errno != ENOENT)
379 pjdlog_errno(LOG_ERR, "Unable to remove core");