eBPF-based security platform
harden: refuse trusted files others could change
Daniel Borkmannc8c3939c3e97
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 9fa96912..b4716c2b 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -576,7 +576,7 @@ testing/helper/test-chain: testing/helper/test-chain.c $(REGISTER_OBJS)$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)testing/helper/test-tpm-wire: testing/helper/test-tpm-wire.c util/tpm_wire.o util/hex.o \- util/crypto_openssl.o util/malloc.o util/log.o+ util/crypto_openssl.o util/io.o util/malloc.o util/log.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ -lcrypto -lssl@@ -656,7 +656,8 @@ testing/helper/test-upatch-decode: testing/helper/test-upatch-decode.c \@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)-testing/helper/test-exec-churn: testing/helper/test-exec-churn.c util/io.o util/malloc.o+testing/helper/test-exec-churn: testing/helper/test-exec-churn.c util/io.o util/malloc.o \+ util/log.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^@@ -693,7 +694,7 @@ testing/fuzz/fuzz-oci-manifest: testing/fuzz/fuzz-oci-manifest.c \testing/fuzz/fuzz-bpm-index: testing/fuzz/fuzz-bpm-index.c \$(FUZZ_BASE) $(FUZZ_PACKAGE)testing/fuzz/fuzz-tree: testing/fuzz/fuzz-tree.c $(FUZZ_BASE) \- util/bpm_tree.c util/crypto_openssl.c+ util/bpm_tree.c util/crypto_openssl.c util/io.ctesting/fuzz/fuzz-upatch: testing/fuzz/fuzz-upatch.c $(FUZZ_BASE) \util/upatch.ctesting/fuzz/fuzz-bpm-tar: testing/fuzz/fuzz-bpm-tar.c $(FUZZ_BASE) \diff --git a/builtin-bpm.c b/builtin-bpm.cindex 61ff3e90..9d7b0ff0 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -282,6 +282,10 @@ static int load_source(const char *name, struct bpm_source *s)fp = fopen(path, "r");if (!fp)return -errno;+ if (shirudo_check_trusted(fileno(fp), path, false)) {+ fclose(fp);+ return -EPERM;+ }snprintf(s->name, sizeof(s->name), "%s", name);while (fgets(line, sizeof(line), fp)) {rc = shirudo_bpm_parse_source_line(line,@@ -1474,6 +1478,10 @@ static int load_tree_config(struct bpm_tree_config *cfg)fp = fopen(SHIRUDO_BPM_TREE_ETC_PATH, "r");if (!fp)return -errno;+ if (shirudo_check_trusted(fileno(fp), SHIRUDO_BPM_TREE_ETC_PATH, false)) {+ fclose(fp);+ return -EPERM;+ }while (fgets(line, sizeof(line), fp)) {char key[64], value[PATH_MAX];char *eq = strchr(line, '=');diff --git a/builtin-keys.c b/builtin-keys.cindex 9d2bd789..2e0f9da0 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -34,6 +34,7 @@#include "util/help.h"#include "util/hex.h"#include "util/identity.h"+#include "util/io.h"#include "util/json.h"#include "util/keyring.h"#include "util/keys.h"@@ -46,12 +47,22 @@static X509 *read_cert_der(const char *path){+ int fd = open(path, O_RDONLY | O_CLOEXEC);BIO *bio;X509 *c;- bio = BIO_new_file(path, "rb");+ if (fd < 0) {+ pr_err("open %s: %s", path, strerror(errno));+ return NULL;+ }+ if (shirudo_check_trusted(fd, path, false)) {+ close(fd);+ return NULL;+ }+ bio = BIO_new_fd(fd, BIO_CLOSE);if (!bio) {- shirudo_openssl_complain("BIO_new_file");+ close(fd);+ shirudo_openssl_complain("BIO_new_fd");return NULL;}c = d2i_X509_bio(bio, NULL);diff --git a/util/config.c b/util/config.cindex d3dc2efb..d8c2701c 100644--- a/util/config.c+++ b/util/config.c@@ -16,6 +16,7 @@#include "config.h"#include "hex.h"+#include "io.h"#include "json.h"#include "log.h"#include "paths.h"@@ -130,6 +131,9 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)if (!f) {if (errno != ENOENT)rc = -errno; /* still apply env override below */+ } else if (shirudo_check_trusted(fileno(f), path, false)) {+ fclose(f);+ return -EPERM;} else {while (fgets(line, sizeof(line), f)) {char *p, *eq, *key, *val;@@ -241,6 +245,14 @@ int shirudo_config_load_path(struct shirudo_config *cfg, const char *path)}if (ferror(f) && !rc)rc = -EIO;+ /* The TPM owner's authorization is for no other user's eyes. */+ if (cfg->tpm_owner_auth_len &&+ shirudo_check_trusted(fileno(f), path, true)) {+ explicit_bzero(cfg->tpm_owner_auth,+ sizeof(cfg->tpm_owner_auth));+ cfg->tpm_owner_auth_len = 0;+ rc = -EPERM;+ }if (fclose(f) && !rc)rc = -errno;}diff --git a/util/crypto_openssl.c b/util/crypto_openssl.cindex 9a939a4a..248e8257 100644--- a/util/crypto_openssl.c+++ b/util/crypto_openssl.c@@ -6,10 +6,12 @@*/#include <errno.h>+#include <fcntl.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>+#include <unistd.h>#include <openssl/bio.h>#include <openssl/cms.h>@@ -27,16 +29,27 @@#include <shirudo/misc.h>#include "crypto.h"+#include "io.h"#include "log.h"#include "malloc.h"static X509 *read_x509(const char *path){- BIO *bio = BIO_new_file(path, "rb");+ int fd = open(path, O_RDONLY | O_CLOEXEC);X509 *cert;+ BIO *bio;- if (!bio)+ if (fd < 0)return NULL;+ if (shirudo_check_trusted(fd, path, false)) {+ close(fd);+ return NULL;+ }+ bio = BIO_new_fd(fd, BIO_CLOSE);+ if (!bio) {+ close(fd);+ return NULL;+ }cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);if (!cert) {ERR_clear_error();diff --git a/util/io.c b/util/io.cindex f054d812..035b92a5 100644--- a/util/io.c+++ b/util/io.c@@ -3,14 +3,19 @@#include <errno.h>#include <fcntl.h>+#include <limits.h>#include <stdint.h>+#include <stdlib.h>+#include <string.h>#include <sys/stat.h>#include <unistd.h>#include <shirudo/misc.h>#include "io.h"+#include "log.h"#include "malloc.h"+#include "paths.h"int shirudo_write_all(int fd, const void *buf, size_t len){@@ -50,6 +55,9 @@ int shirudo_read_file(const char *path, size_t max, void *out, size_t *len)rc = -EINVAL;goto out;}+ rc = shirudo_check_trusted(fd, path, false);+ if (rc)+ goto out;if ((uintmax_t)st.st_size > max) {rc = -EFBIG;goto out;@@ -79,3 +87,55 @@ out:close(fd);return rc;}++static bool under(const char *path, const char *dir)+{+ size_t n = strlen(dir);++ return !strncmp(path, dir, n) && path[n] == '/';+}++static bool trusted_owner(uid_t uid)+{+ return uid == 0 || uid == geteuid();+}++int shirudo_check_trusted(int fd, const char *path, bool secret)+{+ char real[PATH_MAX], *slash;+ struct stat st;++ if (!under(path, SHIRUDO_ETC_DIR) && !under(path, SHIRUDO_VAR_LIB))+ return 0;+ if (fstat(fd, &st))+ return -errno;+ if (!S_ISREG(st.st_mode) || !trusted_owner(st.st_uid) ||+ (st.st_mode & (secret ? 077 : 022))) {+ pr_err("%s: owner %u, mode %04o: refusing a file another user "+ "could %s", path, (__u32)st.st_uid,+ (__u32)(st.st_mode & 07777),+ secret ? "read or change" : "change");+ return -EPERM;+ }+ if (!realpath(path, real))+ return -errno;+ do {+ slash = strrchr(real, '/');+ if (slash == real)+ slash[1] = '\0';+ else+ *slash = '\0';+ if (stat(real, &st))+ return -errno;+ if (!S_ISDIR(st.st_mode) || !trusted_owner(st.st_uid) ||+ ((st.st_mode & 022) &&+ !((st.st_mode & S_ISVTX) && st.st_uid == 0))) {+ pr_err("%s: directory %s, owner %u, mode %04o: "+ "refusing a file another user could replace",+ path, real, (__u32)st.st_uid,+ (__u32)(st.st_mode & 07777));+ return -EPERM;+ }+ } while (slash != real);+ return 0;+}diff --git a/util/io.h b/util/io.hindex 02875822..2deb9fb7 100644--- a/util/io.h+++ b/util/io.h@@ -4,6 +4,7 @@#ifndef SHIRUDO_IO_H#define SHIRUDO_IO_H+#include <stdbool.h>#include <stddef.h>int shirudo_write_all(int fd, const void *buf, size_t len);@@ -15,4 +16,15 @@ int shirudo_write_all(int fd, const void *buf, size_t len);*/int shirudo_read_file(const char *path, size_t max, void *out, size_t *len);+/*+ * Whether the file open on fd at path may steer what shirudo trusts.+ * Under /etc/shirudo and /var/lib/shirudo, where an installed shirudo+ * keeps such files, it must be a regular file of root's or of the user+ * running shirudo that nobody else can write, nor with secret read, in+ * directories up to / that nobody else can change; a sticky directory of+ * root's guards its files the same way. A path elsewhere, where a test+ * points shirudo, is the caller's to vouch for. 0, or -EPERM logged.+ */+int shirudo_check_trusted(int fd, const char *path, bool secret);+#endif /* SHIRUDO_IO_H */diff --git a/util/keys.c b/util/keys.cindex 464d831f..58b29001 100644--- a/util/keys.c+++ b/util/keys.c@@ -58,6 +58,10 @@ int shirudo_keys_read_der(const char *path, __u8 **out,close(fd);return -EINVAL;}+ if (shirudo_check_trusted(fd, path, false)) {+ close(fd);+ return -EPERM;+ }buf = xmalloc((size_t)st.st_size);if (!buf) {close(fd);diff --git a/util/paths.h b/util/paths.hindex 4509d407..89ea21a2 100644--- a/util/paths.h+++ b/util/paths.h@@ -15,6 +15,7 @@#include <string.h>/* Target-side. */+#define SHIRUDO_ETC_DIR "/etc/shirudo"#define SHIRUDO_VAR_LIB "/var/lib/shirudo"#define SHIRUDO_KEYS_DIR SHIRUDO_VAR_LIB "/keys"#define SHIRUDO_KEYS_DIR_ENV "SHIRUDO_KEYS_DIR"diff --git a/util/tls_openssl.c b/util/tls_openssl.cindex bd050494..e44dff0e 100644--- a/util/tls_openssl.c+++ b/util/tls_openssl.c@@ -6,6 +6,8 @@*/#include <errno.h>+#include <fcntl.h>+#include <unistd.h>#include <openssl/bio.h>#include <openssl/err.h>@@ -14,6 +16,7 @@#include <openssl/x509.h>#include "crypto.h"+#include "io.h"/** Hybrid ML-KEM only: X25519MLKEM768 with a key share, the P-256 hybrid@@ -36,6 +39,7 @@ static CURLcode shirudo_sslctx_callback(CURL *curl, void *sslctx, void *userptr)SSL_CTX *ctx = sslctx;X509 *cert;BIO *bio;+ int fd;(void)curl;if (SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION) != 1 ||@@ -45,9 +49,18 @@ static CURLcode shirudo_sslctx_callback(CURL *curl, void *sslctx, void *userptr)}if (!path || !path[0])return CURLE_OK;- bio = BIO_new_file(path, "rb");- if (!bio)+ fd = open(path, O_RDONLY | O_CLOEXEC);+ if (fd < 0)return CURLE_SSL_CACERT_BADFILE;+ if (shirudo_check_trusted(fd, path, false)) {+ close(fd);+ return CURLE_SSL_CACERT_BADFILE;+ }+ bio = BIO_new_fd(fd, BIO_CLOSE);+ if (!bio) {+ close(fd);+ return CURLE_SSL_CACERT_BADFILE;+ }cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);BIO_free(bio);if (!cert)