eBPF-based security platform
upatch: hash an upatch's payload in one place
Daniel Borkmann7c5c3e1cb793
diff --git a/builtin-upatch.c b/builtin-upatch.cindex 3b9e1ea5..c2477ca7 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2123,8 +2123,7 @@ static int upatch_apply(struct upatch *u, const char *label, bool verify_only,__u8 content_digest[UPATCH_PAYLOAD_HASH_LEN];int err;- err = sha256_2(insns, u->hdr.loader_insns_len,- data, u->hdr.data_len, content_digest);+ err = upatch_payload_digest(u, content_digest);if (err) {pr_err("upatch: cannot hash the payload of '%s': %s",upatch_name, strerror(-err));diff --git a/testing/unit/upatch_verify.c b/testing/unit/upatch_verify.cindex f2da5c7a..69b2ec61 100644--- a/testing/unit/upatch_verify.c+++ b/testing/unit/upatch_verify.c@@ -8,6 +8,7 @@#include <sys/stat.h>#include <unistd.h>+#include "crypto.h"#include "paths.h"#include "unit.h"#include "upatch.h"@@ -52,6 +53,51 @@ static void payload_of_fixture(const __u8 *buf, size_t len,upatch_close(&u);}+static void open_fixture(struct upatch *u)+{+ size_t len = 0;+ const __u8 *buf = read_fixture(&len);+ int fd = memfd_of(buf, len);++ ASSERT_EQ(upatch_open_fd(fd, u), 0);+ close(fd);+}++static void the_payload_digest_hashes_the_loader_then_the_data(void)+{+ __u8 want[UPATCH_PAYLOAD_HASH_LEN], got[UPATCH_PAYLOAD_HASH_LEN];+ static __u8 cat[1 << 16];+ size_t loader_len;+ struct upatch u;++ open_fixture(&u);+ loader_len = u.hdr.loader_insns_len;+ ASSERT_TRUE(u.hdr.data_len > 0);+ ASSERT_TRUE(loader_len + u.hdr.data_len <= sizeof(cat));+ memcpy(cat, upatch_loader(&u), loader_len);+ memcpy(cat + loader_len, upatch_data(&u), u.hdr.data_len);+ ASSERT_EQ(sha256_buffer(cat, loader_len + u.hdr.data_len, want), 0);+ ASSERT_EQ(upatch_payload_digest(&u, got), 0);+ EXPECT_MEMEQ(got, want, sizeof(got));+ EXPECT_MEMEQ(got, u.hdr.payload_sha256, sizeof(got));+ upatch_close(&u);+}++static void a_payload_without_data_hashes_the_loader_alone(void)+{+ __u8 want[UPATCH_PAYLOAD_HASH_LEN], got[UPATCH_PAYLOAD_HASH_LEN];+ struct upatch u;++ open_fixture(&u);+ u.hdr.data_len = 0;+ ASSERT_TRUE(!upatch_data(&u));+ ASSERT_EQ(sha256_buffer(upatch_loader(&u), u.hdr.loader_insns_len,+ want), 0);+ ASSERT_EQ(upatch_payload_digest(&u, got), 0);+ EXPECT_MEMEQ(got, want, sizeof(got));+ upatch_close(&u);+}+static void the_loaded_bytes_verify(void){__u8 payload[UPATCH_PAYLOAD_HASH_LEN];@@ -162,6 +208,8 @@ static void nothing_pinned_checks_clean(void)}static const struct unit_case cases[] = {+ UNIT_CASE(the_payload_digest_hashes_the_loader_then_the_data),+ UNIT_CASE(a_payload_without_data_hashes_the_loader_alone),UNIT_CASE(the_loaded_bytes_verify),UNIT_CASE(another_payload_is_stale),UNIT_CASE(changed_data_under_an_intact_header_is_stale),diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex 4639d703..2f228ab8 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -34,6 +34,13 @@ static int stale_unless_resources(int err)return err == -ENOMEM || err == -EMFILE || err == -ENFILE ? err : -ESTALE;}+int upatch_payload_digest(const struct upatch *u,+ __u8 out[UPATCH_PAYLOAD_HASH_LEN])+{+ return sha256_2(upatch_loader(u), u->hdr.loader_insns_len,+ upatch_data(u), u->hdr.data_len, out);+}+int upatch_verify_fd(int fd, const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],struct upatch *out){@@ -43,8 +50,7 @@ int upatch_verify_fd(int fd, const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],ret = upatch_open_fd(fd, out);if (ret)return stale_unless_resources(ret);- ret = sha256_2(upatch_loader(out), out->hdr.loader_insns_len,- upatch_data(out), out->hdr.data_len, digest);+ ret = upatch_payload_digest(out, digest);if (!ret && (memneq_explicit(digest, payload, sizeof(digest)) ||memneq_explicit(out->hdr.payload_sha256, payload,sizeof(digest))))diff --git a/util/upatch_verify.h b/util/upatch_verify.hindex 39ff0e78..25a63ed8 100644--- a/util/upatch_verify.h+++ b/util/upatch_verify.h@@ -18,6 +18,14 @@ struct prog_spec;struct upatch;struct xdp_device;+/*+ * SHA-256 of the loader's instructions, then the data: what a header's+ * payload_sha256 claims and the pin directory, the register and an+ * unload directive record. 0, or a negative errno with out zeroed.+ */+int upatch_payload_digest(const struct upatch *u,+ __u8 out[UPATCH_PAYLOAD_HASH_LEN]) __must_check;+/** 0 with the artifact open in *out, -ESTALE when it is not what payload* names, or another negative errno when the check itself could not run.