eBPF-based security platform
upatch: read the payload hash of a pin directory in one place
John Fastabend4a70bbfe8303
diff --git a/builtin-upatch.c b/builtin-upatch.cindex 0861244a..b879b4ce 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1790,21 +1790,6 @@ static int set_pin_payload(const char *name, const __u8 payload[32])return ret;}-static int get_pin_payload(const char *name, __u8 out[32])-{- char dir[PATH_MAX];- ssize_t n;-- if (upatch_pin_path(dir, sizeof(dir), name, NULL))- return -ENAMETOOLONG;- n = getxattr(dir, XATTR_PAYLOAD, out, 32);- if (n < 0)- return -errno;- if (n != 32)- return -EINVAL;- return 0;-}-/* Resolve the kernel BPF keyring and compare it with the frozen record. */static int trust_anchor_keyring(key_serial_t *keyring, key_serial_t *serial,struct op_report *rep)@@ -3031,7 +3016,7 @@ static int upatch_load(const struct load_args *args)* it. An install of the very payload loaded is done: bpm retries one* that a stop cut off between this load and its own record.*/- if (have_id && !get_pin_payload(name, live)) {+ if (have_id && !upatch_pin_payload(name, live)) {if (args->loaded_is_done &&!memcmp(live, payload, sizeof(live))) {pr_info("upatch load: %s is already loaded with this payload",@@ -3252,7 +3237,7 @@ static int cmd_upatch_unload(int argc, const char **argv)__u8 live[UPATCH_PAYLOAD_HASH_LEN];int xrc;- xrc = get_pin_payload(name, live);+ xrc = upatch_pin_payload(name, live);if (xrc == 0) {if (memneq_explicit(live, payload, sizeof(payload))) {pr_err("upatch unload: payload_mismatch, the "@@ -4239,7 +4224,7 @@ static int pin_is_measured(const char *name, __u8 *payload, bool *held)static int reconcile_read(struct xdp_work *w){- if (get_pin_payload(w->name, w->payload))+ if (upatch_pin_payload(w->name, w->payload))return -ENODATA;return upatch_archive_specs(w->name, w->payload, &w->u, &w->specs);}diff --git a/testing/proofs/lean4/Shirudo/UpatchLoad.lean b/testing/proofs/lean4/Shirudo/UpatchLoad.leanindex bcb6f081..e16ce646 100644--- a/testing/proofs/lean4/Shirudo/UpatchLoad.lean+++ b/testing/proofs/lean4/Shirudo/UpatchLoad.lean@@ -465,7 +465,7 @@ inductive Step (V : Variant) :(s.procs i = some ⟨c, .undoPins m todo f⟩ ∨s.procs i = some ⟨c, .undoArch m todo f⟩) → V.refuse = false →Step V s (s.go i c .acted)- /-- A load past `get_pin_payload()`, which refuses a directory with an+ /-- A load past `upatch_pin_payload()`, which refuses a directory with anxattr: `mkdir_if_absent()` of its directory. -/| loadMkdir (s) (i n p) : s.procs i = some ⟨.load n p, .acted⟩ →(∀ d, s.pins n = some d → d.xattr = none) →diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.cindex 28844c19..893ba7e9 100644--- a/testing/unit/upatch_pins.c+++ b/testing/unit/upatch_pins.c@@ -127,6 +127,18 @@ static void link_pins_read_back_as_links(void)-ENAMETOOLONG);}+static void payload_of_absent_upatch_is_enoent(void)+{+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];+ char name[PATH_MAX];++ EXPECT_EQ(upatch_pin_payload("shirudo-unit-no-such-upatch", payload),+ -ENOENT);+ memset(name, 'a', sizeof(name) - 1);+ name[sizeof(name) - 1] = '\0';+ EXPECT_EQ(upatch_pin_payload(name, payload), -ENAMETOOLONG);+}+static int count_pinned(const char *name, void *ctx){(*(int *)ctx)++;@@ -151,6 +163,7 @@ static const struct unit_case cases[] = {UNIT_CASE(absent_upatch_has_no_pins),UNIT_CASE(xdp_program_pins_have_one_name),UNIT_CASE(link_pins_read_back_as_links),+ UNIT_CASE(payload_of_absent_upatch_is_enoent),UNIT_CASE(no_pin_directory_is_no_upatch),{}};diff --git a/util/upatch_pins.c b/util/upatch_pins.cindex 3ff023ac..dfe3fe06 100644--- a/util/upatch_pins.c+++ b/util/upatch_pins.c@@ -10,6 +10,7 @@#include <limits.h>#include <stdint.h>#include <string.h>+#include <sys/xattr.h>#include <shirudo/string.h>#include <shirudo/upatch.h>@@ -143,6 +144,19 @@ int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index)return 0;}+int upatch_pin_payload(const char *name, __u8 out[UPATCH_PAYLOAD_HASH_LEN])+{+ char dir[PATH_MAX];+ ssize_t n;++ if (upatch_pin_path(dir, sizeof(dir), name, NULL))+ return -ENAMETOOLONG;+ n = getxattr(dir, XATTR_PAYLOAD, out, UPATCH_PAYLOAD_HASH_LEN);+ if (n < 0)+ return -errno;+ return n == UPATCH_PAYLOAD_HASH_LEN ? 0 : -EINVAL;+}+int upatch_each_pinned(int (*cb)(const char *name, void *ctx), void *ctx){struct dirent *e;diff --git a/util/upatch_pins.h b/util/upatch_pins.hindex 45826f77..fb5de6b5 100644--- a/util/upatch_pins.h+++ b/util/upatch_pins.h@@ -12,6 +12,7 @@#include <stddef.h>#include <shirudo/types.h>+#include <shirudo/upatch.h>struct upatch_pins {bool has_link;@@ -51,6 +52,12 @@ int upatch_link_pin(char *out, size_t cap, const char *dir, __u32 index);/* The pin that keeps XDP program index of name loaded: 0 or -ENAMETOOLONG. */int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index);+/*+ * The payload hash a load set on the pin directory of name: 0,+ * -ENAMETOOLONG, -EINVAL when it is not a hash, or the -errno of the read.+ */+int upatch_pin_payload(const char *name, __u8 out[UPATCH_PAYLOAD_HASH_LEN]);+/** Calls cb with the name of each upatch that has a pin directory and stops* at the first nonzero return, which it returns. No directory at all is 0,diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex 3d691ee2..707e30de 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -12,7 +12,6 @@#include <stdio.h>#include <string.h>#include <sys/stat.h>-#include <sys/xattr.h>#include <time.h>#include <unistd.h>@@ -328,17 +327,10 @@ void upatch_loaded_at(const char *name, char *out, size_t cap)void upatch_payload_sha256(const char *name, char *out, size_t cap){- char dir[PATH_MAX];- __u8 raw[32];- ssize_t n;+ __u8 raw[UPATCH_PAYLOAD_HASH_LEN];out[0] = '\0';- if (cap < 65)- return;- if (upatch_pin_path(dir, sizeof(dir), name, NULL))- return;- n = getxattr(dir, XATTR_PAYLOAD, raw, sizeof(raw));- if (n != (ssize_t)sizeof(raw))+ if (cap < 2 * sizeof(raw) + 1 || upatch_pin_payload(name, raw))return;hex_encode(raw, sizeof(raw), out);}diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex 559c426e..9d5008b4 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -9,7 +9,6 @@#include <limits.h>#include <string.h>#include <sys/stat.h>-#include <sys/xattr.h>#include <unistd.h>#include <bpf/bpf.h>@@ -266,11 +265,8 @@ bool upatch_pin_measured(const struct measured_set *s, const char *name,__u8 payload[UPATCH_PAYLOAD_HASH_LEN]){char hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];- char dir[PATH_MAX];- if (upatch_pin_path(dir, sizeof(dir), name, NULL) ||- getxattr(dir, XATTR_PAYLOAD, payload, UPATCH_PAYLOAD_HASH_LEN) !=- UPATCH_PAYLOAD_HASH_LEN)+ if (upatch_pin_payload(name, payload))return false;hex_encode(payload, UPATCH_PAYLOAD_HASH_LEN, hex);return measured_holds(s, name, hex);