eBPF-based security platform
eventlog: take an upatch set from an init record alone
Daniel Borkmann1d018cc6ae50
diff --git a/Makefile.shirudo b/Makefile.shirudoindex d7873bf7..7c2283d1 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -701,7 +701,7 @@ testing/helper/test-harden: testing/helper/test-harden.c util/harden.o util/log.# label writer and the JSON codec.REGISTER_OBJS := util/config_attest.o util/config_update.o util/config_measure.o util/config_image.o \util/chain.o util/tpm.o util/tpm_wire.o util/eventlog.o \- util/timefmt.o \+ util/timefmt.o util/upatch.o \util/hex.o util/crypto_openssl.o util/json.o util/string.o \util/io.o util/seal.o util/config.o util/image_config.o \util/log.o util/malloc.o \@@ -817,7 +817,8 @@ testing/helper/test-upatch-map-types: testing/helper/test-upatch-map-types.c$(Q)$(CC) $(CFLAGS) -o $@ $^testing/helper/test-upatch-prog-metadata: testing/helper/test-upatch-prog-metadata.c \- util/upatch.o util/malloc.o util/string.o+ util/upatch.o util/malloc.o util/string.o \+ util/hex.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^@@ -1028,8 +1029,9 @@ testing/fuzz/fuzz-attest: testing/fuzz/fuzz-attest.c $(FUZZ_BASE) \testing/fuzz/fuzz-attest: FUZZ_CFLAGS += $(TSS2_CFLAGS) $(LIBELF_CFLAGS)testing/fuzz/fuzz-attest: FUZZ_LIBS += $(TSS2_LIBS) $(LIBBPF_A) $(LIBELF_LIBS) $(ZLIB_LIBS)testing/fuzz/fuzz-eventlog: testing/fuzz/fuzz-eventlog.c $(FUZZ_BASE) \- util/eventlog.c util/timefmt.c $(FUZZ_SEAL) \- util/io.c util/crypto_openssl.c testing/fuzz/chain.c+ util/eventlog.c util/timefmt.c util/upatch.c \+ $(FUZZ_SEAL) util/io.c util/crypto_openssl.c \+ testing/fuzz/chain.ctesting/fuzz/fuzz-kernel: testing/fuzz/fuzz-kernel.c $(FUZZ_BASE) \util/kernel_build_id.ctesting/fuzz/fuzz-elf: testing/fuzz/fuzz-elf.c $(FUZZ_BASE) \diff --git a/testing/unit/eventlog.c b/testing/unit/eventlog.cindex 561fe5a9..39d1ddf2 100644--- a/testing/unit/eventlog.c+++ b/testing/unit/eventlog.c@@ -122,7 +122,8 @@ static void nul_in_the_log_is_refused(void)drop_log();}-#define OPERATION "sha256:abababababababababababababababababababababababababababababababab"+#define DIGEST_HEX "abababababababababababababababababababababababababababababababab"+#define OPERATION "sha256:" DIGEST_HEXstatic void tree_record(char *buf, size_t cap, __s64 seq, const char *op){@@ -165,10 +166,74 @@ static void tree_op_measured_this_boot(void)drop_log();}+struct seen_upatches {+ int inits;+ int loads;+ char last[64];+};++static int count_upatch(enum eventlog_upatch_event event, const char *name,+ const char *payload_hex, void *ctx)+{+ struct seen_upatches *s = ctx;++ if (event == EVENTLOG_UPATCH_INIT) {+ s->inits++;+ return 0;+ }+ s->loads += event == EVENTLOG_UPATCH_LOAD;+ ASSERT_TRUE(strscpy(s->last, name, sizeof(s->last)) > 0);+ return 0;+}++static void set_record(char *buf, size_t cap, const char *type,+ const char *name)+{+ struct json_emit e = { .buf = buf, .cap = cap };+ __u8 digest[32];++ memset(digest, 2, sizeof(digest));+ eventlog_record_open(&e, type, digest);+ json_arr_open(&e, "set");+ json_obj_open(&e);+ json_kv_string(&e, "name", name);+ json_kv_string(&e, "payload_sha256", DIGEST_HEX);+ json_obj_close(&e);+ json_arr_close(&e);+ json_obj_close(&e);+ ASSERT_EQ(e.err, 0);+}++/* Only an init record's set says what boot restore loaded. */+static void set_counts_in_an_init_record_alone(void)+{+ char init[1024], other[1024], text[2048];+ struct seen_upatches s = {};+ int len;++ use_log();+ set_record(init, sizeof(init), "init", "fix");+ set_record(other, sizeof(other), "key_revoke", "ghost");+ len = strsprintf(text, sizeof(text), "%s\n%s\n", init, other);+ ASSERT_TRUE(len > 0);+ write_log(text, (size_t)len);+ ASSERT_EQ(eventlog_boot_upatches(count_upatch, &s), 0);+ EXPECT_EQ(s.inits, 1);+ EXPECT_EQ(s.loads, 1);+ EXPECT_STREQ(s.last, "fix");+ set_record(init, sizeof(init), "init", "../fix");+ len = strsprintf(text, sizeof(text), "%s\n", init);+ ASSERT_TRUE(len > 0);+ write_log(text, (size_t)len);+ EXPECT_EQ(eventlog_boot_upatches(count_upatch, &s), -EINVAL);+ drop_log();+}+static const struct unit_case cases[] = {UNIT_CASE(unfinished_append_is_no_record),UNIT_CASE(nul_in_the_log_is_refused),UNIT_CASE(tree_op_measured_this_boot),+ UNIT_CASE(set_counts_in_an_init_record_alone),{}};diff --git a/testing/unit/upatch.c b/testing/unit/upatch.cindex e9c16094..c1e68dcf 100644--- a/testing/unit/upatch.c+++ b/testing/unit/upatch.c@@ -93,12 +93,26 @@ static void name_signed_is_the_header_name(void)EXPECT_FALSE(upatch_name_signed(&u));}+#define PAYLOAD "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"++static void member_is_a_name_and_a_payload(void)+{+ EXPECT_TRUE(upatch_member_valid("fix", PAYLOAD));+ EXPECT_FALSE(upatch_member_valid("a\nb", PAYLOAD));+ EXPECT_FALSE(upatch_member_valid("../fix", PAYLOAD));+ EXPECT_FALSE(upatch_member_valid("", PAYLOAD));+ EXPECT_FALSE(upatch_member_valid("fix", PAYLOAD "0"));+ EXPECT_FALSE(upatch_member_valid("fix", "00"));+ EXPECT_FALSE(upatch_member_valid("fix", NULL));+}+static const struct unit_case cases[] = {UNIT_CASE(prog_specs_take_the_signed_counts),UNIT_CASE(prog_specs_refuse_another_header_count),UNIT_CASE(header_name_is_terminated),UNIT_CASE(prog_name_is_what_the_kernel_takes),UNIT_CASE(name_signed_is_the_header_name),+ UNIT_CASE(member_is_a_name_and_a_payload),{}};diff --git a/util/eventlog.c b/util/eventlog.cindex 667a4711..432f6734 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -519,7 +519,7 @@ static int upatches_set_entry(void *arg){struct upatches_ctx *u = arg;- if (!u->name[0] || strlen(u->payload_hex) != 2 * UPATCH_PAYLOAD_HASH_LEN)+ if (!upatch_member_valid(u->name, u->payload_hex))return -EINVAL;u->ret = u->cb(EVENTLOG_UPATCH_LOAD, u->name, u->payload_hex, u->ctx);u->name[0] = '\0';@@ -539,9 +539,11 @@ static int upatches_line(const char *line, size_t len, bool is_init,};struct json_array set = { upatches_set_entry, u };char type[32];+ /* Only an init record's set lists what boot restore loaded. */struct json_field f[] = {{ "type", JSON_T_STRING, type, sizeof(type), 0, NULL, 0 },- { "set", JSON_T_ARRAY, &set, 0, 0, entry, ARRAY_SIZE(entry) },+ { is_init ? "set" : NULL, JSON_T_ARRAY, &set, 0, 0, entry,+ ARRAY_SIZE(entry) },{ "upatch", JSON_T_OBJECT, NULL, 0, 0, entry,ARRAY_SIZE(entry) },};@@ -561,8 +563,7 @@ static int upatches_line(const char *line, size_t len, bool is_init,return 0;if (strcmp(type, "upatch_load") && strcmp(type, "upatch_unload"))return 0;- if (!f[2].seen || !u->name[0] ||- strlen(u->payload_hex) != 2 * UPATCH_PAYLOAD_HASH_LEN)+ if (!f[2].seen || !upatch_member_valid(u->name, u->payload_hex))return -EINVAL;return u->cb(type[7] == 'l' ? EVENTLOG_UPATCH_LOAD :EVENTLOG_UPATCH_UNLOAD, u->name, u->payload_hex, u->ctx);diff --git a/util/upatch.c b/util/upatch.cindex 6694ec48..ea1f6596 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -17,6 +17,7 @@#include <shirudo/overflow.h>#include <shirudo/string.h>+#include "hex.h"#include "malloc.h"#include "upatch.h"@@ -555,6 +556,14 @@ bool upatch_name_valid(const char *name)return true;}+bool upatch_member_valid(const char *name, const char *payload_hex)+{+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];++ return upatch_name_valid(name) && payload_hex &&+ !hex_decode(payload_hex, payload, sizeof(payload));+}+int upatch_select(const struct dirent *e){return name_has_suffix(e->d_name, strlen(e->d_name), ".upatch");diff --git a/util/upatch.h b/util/upatch.hindex cc70dcb7..dc5689dd 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -108,6 +108,13 @@ int upatch_decoder_view(const struct upatch *u, __u32 idx,*/const char *upatch_signed_name(const struct upatch *u);+/*+ * A member of a set of measured upatches, as an init record and the+ * commands that predict one list it: a name upatch_name_valid() takes and+ * the 64 hex digits of its payload's SHA-256.+ */+bool upatch_member_valid(const char *name, const char *payload_hex);+/* The name in the header, which the file need not terminate, with a NUL. */void upatch_header_name(const struct upatch *u, char out[UPATCH_NAME_MAX + 1]);/*