eBPF-based security platform
sign: check the signing key's mode on the descriptor it is read from
Daniel Borkmann03a875603ce1
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex a1901a81..98452ca6 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -12,7 +12,6 @@#include <stdio.h>#include <stdlib.h>#include <string.h>-#include <sys/stat.h>#include <unistd.h>#include <shirudo/misc.h>@@ -25,6 +24,7 @@#include "malloc.h"#include "package.h"#include "paths.h"+#include "sign.h"#include "upatch.h"#include "vmlinux.h"@@ -165,38 +165,14 @@ static int compile_to_temp(const char *source, const char *vmlinux_h,int shirudo_build_check_keys(const char *pkey, const char *cert){- if (access(pkey, R_OK) != 0) {- pr_err("signing key not readable: %s: %s",- pkey, strerror(errno));- pr_err("run `shirudo genkey` on the operator (or pass --key).");- return 1;- }- {- struct stat st;+ int fd = shirudo_open_signing_key(pkey);- if (stat(pkey, &st) != 0) {- pr_err("signing key not statable: %s: %s",- pkey, strerror(errno));- return 1;- }- if (!S_ISREG(st.st_mode)) {- pr_err("signing key %s is not a regular file", pkey);- return 1;- }- /*- * Refuse to sign with a private key that any other user- * on this host can read. 0600 is what `shirudo genkey`- * produces; anything broader has been loosened after the- * fact, deliberately or accidentally, and we'd rather fail- * loudly than silently sign with a leakable key.- */- if ((st.st_mode & 07777) != 0600) {- pr_err("signing key %s has mode 0%o, must be 0600 "- "(run `chmod 0600 %s`)",- pkey, st.st_mode & 07777, pkey);- return 1;- }+ if (fd < 0) {+ if (fd != -EPERM)+ pr_err("run `shirudo genkey` on the operator (or pass --key).");+ return 1;}+ close(fd);if (access(cert, R_OK) != 0) {pr_err("cert not readable: %s: %s",cert, strerror(errno));diff --git a/util/sign.c b/util/sign.cindex eaa5f54b..4a00b467 100644--- a/util/sign.c+++ b/util/sign.c@@ -16,10 +16,13 @@*/#include <errno.h>+#include <fcntl.h>#include <stdbool.h>#include <stdio.h>#include <string.h>#include <strings.h>+#include <sys/stat.h>+#include <unistd.h>#include <openssl/bio.h>#include <openssl/cms.h>@@ -65,15 +68,52 @@ static void display_openssl_errors(void)display_openssl_errors(); \} while (0)+int shirudo_open_signing_key(const char *pkey_path)+{+ struct stat st;+ int fd, rc;++ /* A FIFO in its place would block open() before fstat() refuses it. */+ fd = open(pkey_path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);+ if (fd < 0) {+ rc = -errno;+ pr_err("open private key %s: %s", pkey_path, strerror(errno));+ return rc;+ }+ if (fstat(fd, &st)) {+ rc = -errno;+ pr_err("stat private key %s: %s", pkey_path, strerror(errno));+ goto err;+ }+ rc = -EPERM;+ if (!S_ISREG(st.st_mode)) {+ pr_err("signing key %s is not a regular file", pkey_path);+ goto err;+ }+ if ((st.st_mode & 07777) != 0600) {+ pr_err("signing key %s has mode 0%o, must be 0600 "+ "(run `chmod 0600 %s`)",+ pkey_path, (__u32)(st.st_mode & 07777), pkey_path);+ goto err;+ }+ return fd;+err:+ close(fd);+ return rc;+}+static EVP_PKEY *read_private_key(const char *pkey_path){EVP_PKEY *private_key = NULL;BIO *b;+ int fd;- b = BIO_new_file(pkey_path, "rb");+ fd = shirudo_open_signing_key(pkey_path);+ if (fd < 0)+ return NULL;+ b = BIO_new_fd(fd, BIO_CLOSE);if (!b) {- pr_err("open private key %s: %s",- pkey_path, strerror(errno));+ close(fd);DISPLAY_OSSL_ERR(1);return NULL;}diff --git a/util/sign.h b/util/sign.hindex 2bbd1e05..de37ebf8 100644--- a/util/sign.h+++ b/util/sign.h@@ -39,4 +39,10 @@ struct shirudo_sign_io {int shirudo_cms_sign(struct shirudo_sign_io *io,const char *pkey_path, const char *cert_path);+/*+ * Opens the private key and checks, on that descriptor, that it is a+ * regular file of mode 0600. The descriptor, or -errno logged.+ */+int shirudo_open_signing_key(const char *pkey_path);+#endif /* SHIRUDO_SIGN_H */