eBPF-based security platform
config: scrub the TPM owner's authorization on every path
Daniel Borkmannf7c98cc12bfb
diff --git a/builtin-config.c b/builtin-config.cindex 867438c3..b4a28e7a 100644--- a/builtin-config.c+++ b/builtin-config.c@@ -36,8 +36,8 @@ static void usage(void)int cmd_config(int argc, const char **argv){+ struct config cfg __cleanup(config_clear);bool image_mode = image_config_active();- struct config cfg;bool file_present;int ret;int i;diff --git a/builtin-init.c b/builtin-init.cindex 707ae6fd..873b3bc5 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -1144,20 +1144,18 @@ static int chain_extend_init(struct measured_upatch *set, size_t n)int ret;{- struct config cfg;+ struct config cfg __cleanup(config_clear);__u64 max_bytes;bool full;ret = config_load(&cfg);if (ret) {- memzero_explicit(&cfg, sizeof(cfg));pr_err("chain: configuration unreadable: %s",strerror(-ret));return ret;}max_bytes = cfg.eventlog_max_bytes;full = eventlog_would_exceed(max_bytes);- memzero_explicit(&cfg, sizeof(cfg));if (full) {pr_err("eventlog %s would exceed eventlog.max_bytes=%llu; ""refusing chain extend (eventlog_full)",diff --git a/builtin-status.c b/builtin-status.cindex ac78f969..69ad48c1 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -870,7 +870,7 @@ int cmd_status(int argc, const char **argv)struct status_keyring bpf = {}, dmv = {};struct identity ident = {};int certs_rc = -ENOENT;- struct config otel_cfg = {};+ struct config otel_cfg __cleanup(config_clear) = {};struct otel_opts otel_opts = {};struct asset_view assets;struct tamper_counts tamper = {};diff --git a/builtin-upatch.c b/builtin-upatch.cindex 8d5ac7c0..eeeb706b 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2132,7 +2132,7 @@ static int upatch_preflight(const char *expect_chain,*/static int eventlog_room(struct op_report *rep, const char *op){- struct config cfg;+ struct config cfg __cleanup(config_clear);config_load(&cfg);if (eventlog_would_exceed(cfg.eventlog_max_bytes)) {@@ -3216,8 +3216,8 @@ static int export_one_map(const char *upatch, const char *map,static int cmd_upatch_map(int argc, const char **argv){+ struct config export_cfg __cleanup(config_clear) = {};struct map_summary *list = NULL;- struct config export_cfg = {};struct otel_opts otel_opts = {.retries = 3,};diff --git a/util/chain.c b/util/chain.cindex d1112ffe..f62a75a6 100644--- a/util/chain.c+++ b/util/chain.c@@ -220,7 +220,7 @@ static bool name_equals(const struct tpm_nv_info *info,static int register_define(struct tpm *tpm, bool defined,const __u8 policy[32]){- struct config cfg = {};+ struct config cfg __cleanup(config_clear) = {};int ret;config_load(&cfg);@@ -234,7 +234,6 @@ static int register_define(struct tpm *tpm, bool defined,ret = tpm_nv_define(tpm, NV_INDEX, policy,cfg.tpm_owner_auth, cfg.tpm_owner_auth_len);out:- memzero_explicit(&cfg, sizeof(cfg));return ret;}diff --git a/util/config.c b/util/config.cindex b66735f7..73f98b9c 100644--- a/util/config.c+++ b/util/config.c@@ -152,6 +152,7 @@ static int config_parse(struct config *cfg, FILE *f,const char *path){const size_t max = MAP_EXPORT_PATH_MAX + 127;+ char buffer[BUFSIZ];size_t capacity = 0;char *line = NULL;int got, ret = 0;@@ -160,6 +161,8 @@ static int config_parse(struct config *cfg, FILE *f,fclose(f);return -EPERM;}+ /* stdio's own buffer would keep the owner's authorization after fclose(). */+ setvbuf(f, buffer, _IOFBF, sizeof(buffer));while ((got = read_text_line(f, &line, &capacity, max)) != 0) {char *p, *eq, *key, *val;@@ -289,9 +292,15 @@ static int config_parse(struct config *cfg, FILE *f,}if (fclose(f) && !ret)ret = -errno;+ memzero_explicit(buffer, sizeof(buffer));return ret;}+void config_clear(struct config *cfg)+{+ memzero_explicit(cfg, sizeof(*cfg));+}+static int config_finish(struct config *cfg, int ret){if (cfg->map_export_s3_uri[0] && !cfg->map_export_otel_endpoint[0])diff --git a/util/config.h b/util/config.hindex 9fa36dfe..3c66545b 100644--- a/util/config.h+++ b/util/config.h@@ -55,6 +55,12 @@ struct config {/* A missing file is not an error. */int config_load(struct config *cfg);+/*+ * Scrub a config, which holds the TPM owner's authorization; declare one+ * on the stack with __cleanup(config_clear).+ */+void config_clear(struct config *cfg);+/* Testable path-specific loader; unlike config_load(), no env. */int config_load_path(struct config *cfg, const char *path);/* The same from an open descriptor, read from its start. */diff --git a/util/image_config.c b/util/image_config.cindex c578841f..8b6d3c31 100644--- a/util/image_config.c+++ b/util/image_config.c@@ -208,7 +208,7 @@ static int validate_settings(int root_fd)int image_config_validate(int root_fd){- struct config candidate;+ struct config candidate __cleanup(config_clear);__u32 entries = 0;struct stat st;int ret;@@ -225,7 +225,6 @@ int image_config_validate(int root_fd)if (!ret && (!candidate.map_export_valid ||!candidate.tls_classical_hosts_valid))ret = -EINVAL;- memzero_explicit(&candidate, sizeof(candidate));}return ret;}@@ -233,7 +232,7 @@ int image_config_validate(int root_fd)int image_config_set(const struct config *bootstrap,int root_fd){- struct config candidate;+ struct config candidate __cleanup(config_clear);int fd = -1, ret;candidate = *bootstrap;diff --git a/util/preflight.c b/util/preflight.cindex 4f3790c4..c6613655 100644--- a/util/preflight.c+++ b/util/preflight.c@@ -346,7 +346,7 @@ static void check_tpm(struct preflight_result *r)* produces, so this is a convenience, not a relaxation.*/{- struct config cfg = {};+ struct config cfg __cleanup(config_clear) = {};bool override = config_load(&cfg) == 0 && cfg.tpm_tcti[0];if (override) {@@ -356,7 +356,6 @@ static void check_tpm(struct preflight_result *r)strscpy(c->detail, "TCTI override",sizeof(c->detail));}- memzero_explicit(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */if (override)return;}diff --git a/util/tls_openssl.c b/util/tls_openssl.cindex 3ad70433..4132c699 100644--- a/util/tls_openssl.c+++ b/util/tls_openssl.c@@ -40,7 +40,7 @@*/static bool tls_classical_host_allowed(CURL *curl){- struct config cfg = {};+ struct config cfg __cleanup(config_clear) = {};const char *allowlist, *effective_url;CURLU *url = NULL;char *scheme = NULL, *host = NULL;diff --git a/util/tpm.c b/util/tpm.cindex 5467acfe..1a489c9b 100644--- a/util/tpm.c+++ b/util/tpm.c@@ -33,7 +33,7 @@*/int tpm_tcti(char *buf, size_t size){- struct config cfg = {};+ struct config cfg __cleanup(config_clear) = {};const char *tcti;ssize_t n;@@ -44,12 +44,9 @@ int tpm_tcti(char *buf, size_t size)tcti = cfg.tpm_tcti;if (!tcti || !*tcti)tcti = "device:/dev/tpmrm0";- if (image_config_active() && strncmp(tcti, "device:", 7)) {- memzero_explicit(&cfg, sizeof(cfg));+ if (image_config_active() && strncmp(tcti, "device:", 7))return -EACCES;- }n = strscpy(buf, tcti, size);- memzero_explicit(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */return n < 0 ? (int)n : 0;}@@ -493,7 +490,7 @@ static int tpm_writer_primary(struct tpm *t,TPML_PCR_SELECTION creation_pcr = {};TPM2B_SENSITIVE_CREATE sensitive = {};TPM2B_PUBLIC *out_pub = NULL;- struct config cfg = {};+ struct config cfg __cleanup(config_clear) = {};TPM2B_PUBLIC in_pub = {};TPM2B_NAME *name = NULL;TPM2B_DATA outside = {};