eBPF-based security platform
keys: keep the private key out of stdio's buffers
Daniel Borkmann2ba6f0e7c18c
diff --git a/builtin-genkey.c b/builtin-genkey.cindex f0d44700..5991497e 100644--- a/builtin-genkey.c+++ b/builtin-genkey.c@@ -238,7 +238,7 @@ static int build_cert(X509 *c, EVP_PKEY *pkey)static int write_private_pem(EVP_PKEY *pkey, const char *path){int fd, ret = -1;- FILE *fp = NULL;+ BIO *b = NULL;fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);if (fd < 0) {@@ -249,26 +249,24 @@ static int write_private_pem(EVP_PKEY *pkey, const char *path)pr_err("fchmod %s: %s", path, strerror(errno));goto out;}- fp = fdopen(fd, "w");- if (!fp) {- pr_err("fdopen %s: %s", path, strerror(errno));+ /* Unbuffered, where a FILE would keep the key in memory freed unscrubbed. */+ b = BIO_new_fd(fd, BIO_NOCLOSE);+ if (!b) {+ openssl_complain("BIO_new_fd");goto out;}- fd = -1; /* now owned by fp */- if (PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL) != 1) {- openssl_complain("PEM_write_PrivateKey");+ if (PEM_write_bio_PrivateKey(b, pkey, NULL, NULL, 0, NULL, NULL) != 1) {+ openssl_complain("PEM_write_bio_PrivateKey");goto out;}- if (fflush(fp) != 0 || fsync(fileno(fp)) != 0) {+ if (fsync(fd) != 0) {pr_err("flush %s: %s", path, strerror(errno));goto out;}ret = 0;out:- if (fp)- fclose(fp);- else if (fd >= 0)- close(fd);+ BIO_free(b);+ close(fd);if (ret != 0)unlink(path);return ret;diff --git a/builtin-keys.c b/builtin-keys.cindex 1c5b9f64..157496f2 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -76,17 +76,25 @@ static X509 *read_cert_der(const char *path)static EVP_PKEY *read_priv_pem(const char *path){EVP_PKEY *k;- FILE *fp;+ BIO *b;+ int fd;- fp = fopen(path, "r");- if (!fp) {+ fd = open(path, O_RDONLY | O_CLOEXEC);+ if (fd < 0) {pr_err("open %s: %s", path, strerror(errno));return NULL;}- k = PEM_read_PrivateKey(fp, NULL, NULL, NULL);- fclose(fp);+ /* Unbuffered, where a FILE would keep the key in memory freed unscrubbed. */+ b = BIO_new_fd(fd, BIO_CLOSE);+ if (!b) {+ close(fd);+ openssl_complain("BIO_new_fd");+ return NULL;+ }+ k = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);+ BIO_free(b);if (!k)- openssl_complain("PEM_read_PrivateKey");+ openssl_complain("PEM_read_bio_PrivateKey");return k;}