eBPF-based security platform
status: verify each XDP archive once for the three checks it feeds
John Fastabendd669fa10e718
diff --git a/builtin-status.c b/builtin-status.cindex 31031764..2603e1ff 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -870,6 +870,14 @@ int cmd_status(int argc, const char **argv)char xdp_tampered_names[UNMEASURED_NAMES] = "";char links_lost_names[UNMEASURED_NAMES] = "";char xdp_uncovered_names[UNMEASURED_NAMES] = "";+ struct upatch_checks checks = {+ .changed = note_unmeasured,+ .changed_ctx = xdp_tampered_names,+ .lost = note_unmeasured,+ .lost_ctx = links_lost_names,+ .uncovered = note_uncovered,+ .uncovered_ctx = xdp_uncovered_names,+ };struct measured_set *mset = NULL;int xdp_tampered = 0;int xdp_coverage_rc = 0;@@ -987,14 +995,12 @@ int cmd_status(int argc, const char **argv)unmeasured = archived < 0 ? archived :unmeasured + archived;}- xdp_tampered = upatch_xdp_archives_changed(mset, note_unmeasured,- xdp_tampered_names,- &checks_rc);- links_lost = upatch_links_lost(mset, note_unmeasured,- links_lost_names, &checks_rc);- xdp_uncovered = upatch_xdp_uncovered(mset, note_uncovered,- xdp_uncovered_names,- &xdp_coverage_rc);+ upatch_checks_run(mset, &checks);+ xdp_tampered = checks.nr_changed;+ links_lost = checks.nr_lost;+ xdp_uncovered = checks.nr_uncovered;+ checks_rc = checks.check_err;+ xdp_coverage_rc = checks.coverage_err;measured_set_free(mset);}eventlog_unlock(lock);@@ -1067,8 +1073,7 @@ int cmd_status(int argc, const char **argv)cin.checks_rc = checks_rc;cin.xdp_uncovered = max(xdp_uncovered, 0);cin.xdp_uncovered_detail = xdp_uncovered_names;- cin.xdp_coverage_rc = xdp_uncovered < 0 ? xdp_uncovered :- xdp_coverage_rc;+ cin.xdp_coverage_rc = xdp_coverage_rc;keyring_cond_input(&cin, &bpf, &dmv);if (json) {diff --git a/testing/unit/upatch_verify.c b/testing/unit/upatch_verify.cindex 9b7115a3..fb503fe2 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 "paths.h"#include "unit.h"#include "upatch.h"#include "upatch_verify.h"@@ -152,6 +153,23 @@ static void an_unpinned_xdp_program_does_not_open(void)EXPECT_EQ(id, 0);}+static int never(const char *name, void *ctx)+{+ return 1;+}++static void nothing_pinned_checks_clean(void)+{+ struct upatch_checks c = { .changed = never, .lost = never };++ if (access(BPFFS_UPATCHES_DIR, F_OK) == 0)+ unit_skip("upatches are pinned on this host");+ upatch_checks_run(NULL, &c);+ EXPECT_EQ(c.nr_changed + c.nr_lost + c.nr_uncovered, 0);+ EXPECT_EQ(c.check_err, 0);+ EXPECT_EQ(c.coverage_err, 0);+}+static const struct unit_case cases[] = {UNIT_CASE(the_loaded_bytes_verify),UNIT_CASE(another_payload_is_stale),@@ -161,6 +179,7 @@ static const struct unit_case cases[] = {UNIT_CASE(links_of_a_missing_archive_are_stale),UNIT_CASE(specs_of_a_missing_archive_are_stale),UNIT_CASE(an_unpinned_xdp_program_does_not_open),+ UNIT_CASE(nothing_pinned_checks_clean),{}};diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex f8c306dc..559c426e 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -124,14 +124,32 @@ static int xdp_upatch_complete(const char *name, const struct prog_spec *specs,return found == nr_attach - nr_xdp;}-int upatch_links_complete(const char *name, const char *path)+static int links_complete_specs(const char *name,+ const struct prog_spec *specs, __u32 nr_progs){- __u32 len, i, nr_attach = 0;- struct prog_spec *specs = NULL;struct upatch_pins pins;+ __u32 i, nr_attach = 0;bool has_xdp = false;+ int ret;++ for (i = 0; i < nr_progs; i++) {+ if (specs[i].role == UPATCH_PROG_ROLE_ATTACH)+ nr_attach++;+ if (specs[i].kind == UPATCH_ATTACH_XDP)+ has_xdp = true;+ }+ if (has_xdp)+ return xdp_upatch_complete(name, specs, nr_progs, nr_attach);+ ret = upatch_pins_read(name, &pins);+ return ret ? ret : upatch_pins_complete(&pins, nr_attach);+}++int upatch_links_complete(const char *name, const char *path)+{+ struct prog_spec *specs = NULL;const void *attach;struct upatch u;+ __u32 len;int ret;ret = upatch_open(path, &u);@@ -144,44 +162,40 @@ int upatch_links_complete(const char *name, const char *path)ret = stale_unless_resources(ret);goto out;}- for (i = 0; i < u.hdr.nr_progs; i++) {- if (specs[i].role == UPATCH_PROG_ROLE_ATTACH)- nr_attach++;- if (specs[i].kind == UPATCH_ATTACH_XDP)- has_xdp = true;- }- if (has_xdp) {- ret = xdp_upatch_complete(name, specs, u.hdr.nr_progs, nr_attach);- } else {- ret = upatch_pins_read(name, &pins);- if (!ret)- ret = upatch_pins_complete(&pins, nr_attach);- }+ ret = links_complete_specs(name, specs, u.hdr.nr_progs);prog_specs_free(specs, u.hdr.nr_progs);out:upatch_close(&u);return ret;}+/* -ESTALE for metadata that does not parse, -ENOMEM; u stays open. */+static int upatch_verified_specs(const struct upatch *u,+ struct prog_spec **specs)+{+ const void *attach;+ __u32 len;+ int ret;++ attach = upatch_signed_attach(u, &len);+ ret = attach ? prog_specs_parse(attach, len, u->hdr.nr_maps,+ u->hdr.nr_progs, specs) : -ESTALE;+ return !ret || ret == -ENOMEM ? ret : -ESTALE;+}+int upatch_archive_specs(const char *name,const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],struct upatch *u, struct prog_spec **specs){- const void *attach;- __u32 len;int ret;ret = upatch_archive_verify(name, payload, u);if (ret)return ret;- attach = upatch_signed_attach(u, &len);- ret = attach ? prog_specs_parse(attach, len, u->hdr.nr_maps,- u->hdr.nr_progs, specs) : -ESTALE;- if (ret) {+ ret = upatch_verified_specs(u, specs);+ if (ret)upatch_close(u);- return ret == -ENOMEM ? ret : -ESTALE;- }- return 0;+ return ret;}int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id)@@ -262,121 +276,60 @@ bool upatch_pin_measured(const struct measured_set *s, const char *name,return measured_holds(s, name, hex);}-/* These return 1 for a hit, 0 for none, or -errno when they could not tell. */-static int xdp_archive_changed(const struct measured_set *s, const char *name,- void *ctx)-{- __u8 payload[UPATCH_PAYLOAD_HASH_LEN];- struct upatch_pins pins;- struct upatch u;- int ret;-- if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs ||- !upatch_pin_measured(s, name, payload))- return 0;- ret = upatch_archive_verify(name, payload, &u);- if (!ret)- upatch_close(&u);- return ret == -ESTALE ? 1 : ret;-}--static int links_lost(const struct measured_set *s, const char *name,- void *ctx)-{- __u8 payload[UPATCH_PAYLOAD_HASH_LEN];- char path[PATH_MAX];- int ret;-- if (!upatch_pin_measured(s, name, payload))- return 0;- if (strsprintf(path, sizeof(path), "%s/%s.upatch", UPATCHES_DIR,- name) < 0)- return -ENAMETOOLONG;- ret = upatch_links_complete(name, path);- if (ret == 1)- return 0;- return !ret || ret == -ESTALE ? 1 : ret;-}--struct pinned_walk {+struct checks_walk {const struct measured_set *s;- int (*check)(const struct measured_set *s,- const char *name, void *ctx);- int (*cb)(const char *name, void *ctx);- void *ctx;- int *check_err;- int found;- int stopped;+ struct upatch_checks *c;+ struct xdp_device *devices;+ size_t count;+ bool devices_tried;+ bool devices_ok;+ const char *name;+ char why[128];};-static int walk_one(const char *name, void *ctx)+static void note_err(int *slot, int err){- struct pinned_walk *w = ctx;- int result;-- result = w->check(w->s, name, w->ctx);- if (result < 0 && !*w->check_err)- *w->check_err = result;- if (result <= 0)- return 0;- w->found++;- if (w->cb)- w->stopped = w->cb(name, w->ctx);- return w->stopped;+ if (err < 0 && !*slot)+ *slot = err;}-/* check gets ctx as well, and cb may be NULL. */-static int walk_pinned(const struct measured_set *s,- int (*check)(const struct measured_set *s, const char *name,- void *ctx),- int (*cb)(const char *name, void *ctx), void *ctx,- int *check_err)+static int note_changed(struct checks_walk *w, const char *name){- struct pinned_walk w = {- .s = s, .check = check, .cb = cb, .ctx = ctx,- .check_err = check_err,- };- int ret;-- ret = upatch_each_pinned(walk_one, &w);- if (w.stopped)- return w.stopped;- if (ret && !*check_err)- *check_err = ret;- return w.found;+ w->c->nr_changed++;+ return w->c->changed ? w->c->changed(name, w->c->changed_ctx) : 0;}-int upatch_xdp_archives_changed(const struct measured_set *s,- int (*cb)(const char *name, void *ctx),- void *ctx, int *check_err)+/* complete is 1, 0 or -errno, as upatch_links_complete() returns. */+static int note_links(struct checks_walk *w, const char *name, int complete){- return walk_pinned(s, xdp_archive_changed, cb, ctx, check_err);+ if (complete == 1)+ return 0;+ if (complete < 0 && complete != -ESTALE) {+ note_err(&w->c->check_err, complete);+ return 0;+ }+ w->c->nr_lost++;+ return w->c->lost ? w->c->lost(name, w->c->lost_ctx) : 0;}-int upatch_links_lost(const struct measured_set *s,- int (*cb)(const char *name, void *ctx), void *ctx,- int *check_err)+/* Read from the archive as it is, for an upatch the hash does not vouch for. */+static int note_links_unverified(struct checks_walk *w, const char *name){- return walk_pinned(s, links_lost, cb, ctx, check_err);-}+ char path[PATH_MAX];-struct uncovered_walk {- int (*cb)(const char *name, const char *dev,- const char *why, void *ctx);- void *ctx;- const char *name;- struct xdp_device *devices;- size_t count;- bool devices_read;- int found;- char why[128];-};+ if (strsprintf(path, sizeof(path), "%s/%s.upatch", UPATCHES_DIR,+ name) < 0) {+ note_err(&w->c->check_err, -ENAMETOOLONG);+ return 0;+ }+ return note_links(w, name, upatch_links_complete(name, path));+}static int note_uncovered(int prog_fd, const struct prog_spec *spec,const struct xdp_device *dev, __u32 attached,void *ctx){- struct uncovered_walk *w = ctx;+ struct checks_walk *w = ctx;__u64 features = 0;int err = 0;@@ -387,9 +340,10 @@ static int note_uncovered(int prog_fd, const struct prog_spec *spec,return 0;err = xdp_device_features(dev->ifindex, &features);}- w->found++;- return w->cb(w->name, dev->name,- xdp_uncovered_reason(attached, err, features), w->ctx);+ w->c->nr_uncovered++;+ return w->c->uncovered(w->name, dev->name,+ xdp_uncovered_reason(attached, err, features),+ w->c->uncovered_ctx);}static bool runs_program_of(const char *name, const struct prog_spec *specs,@@ -415,7 +369,7 @@ static bool runs_program_of(const char *name, const struct prog_spec *specs,* Whether name selects it is not known, so unless it runs name's program* it may run without it.*/-static int note_unclassified(struct uncovered_walk *w, const char *name,+static int note_unclassified(struct checks_walk *w, const char *name,const struct prog_spec *specs, __u32 nr_progs){size_t i;@@ -430,65 +384,112 @@ static int note_unclassified(struct uncovered_walk *w, const char *name,if (strsprintf(w->why, sizeof(w->why), "not classified: %s",strerror(-dev->unclassified_err)) < 0)return -E2BIG;- w->found++;- ret = w->cb(name, dev->name, w->why, w->ctx);+ w->c->nr_uncovered++;+ ret = w->c->uncovered(name, dev->name, w->why,+ w->c->uncovered_ctx);if (ret)return ret;}return 0;}-/* An archive that changed is upatch_xdp_archives_changed()'s to report. */-static int xdp_uncovered(const struct measured_set *s, const char *name,- void *ctx)+static int check_coverage(struct checks_walk *w, const char *name,+ const struct prog_spec *specs, __u32 nr_progs)+{+ int ret;++ if (!w->c->uncovered)+ return 0;+ /*+ * Under the lock, as the pins are: a load that attaches between the+ * two reads would otherwise show its device as not attached.+ */+ if (!w->devices_tried) {+ w->devices_tried = true;+ ret = xdp_devices_read(&w->devices, &w->count);+ note_err(&w->c->coverage_err, ret);+ w->devices_ok = !ret;+ }+ if (!w->devices_ok)+ return 0;+ w->name = name;+ ret = upatch_xdp_each_uncovered(name, specs, nr_progs, w->devices,+ w->count, note_uncovered, w);+ if (!ret)+ ret = note_unclassified(w, name, specs, nr_progs);+ if (ret < 0) {+ note_err(&w->c->coverage_err, ret);+ return 0;+ }+ return ret;+}++/*+ * One verified read of an XDP upatch's archive serves the three checks:+ * its hash, the links its metadata implies and the devices it selects.+ */+static int check_one(const char *name, void *ctx){__u8 payload[UPATCH_PAYLOAD_HASH_LEN];- struct uncovered_walk *w = ctx;+ struct checks_walk *w = ctx;struct prog_spec *specs = NULL;struct upatch_pins pins;struct upatch u;int ret;- if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs ||- !upatch_pin_measured(s, name, payload))+ if (!upatch_pin_measured(w->s, name, payload))return 0;- ret = upatch_archive_specs(name, payload, &u, &specs);- if (ret)- return ret == -ESTALE ? 0 : ret;- if (!w->devices_read) {- ret = xdp_devices_read(&w->devices, &w->count);- w->devices_read = !ret;+ if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs)+ return note_links_unverified(w, name);+ ret = upatch_archive_verify(name, payload, &u);+ if (ret == -ESTALE) {+ ret = note_changed(w, name);+ return ret ? ret : note_links_unverified(w, name);}- if (!ret) {- w->name = name;- ret = upatch_xdp_each_uncovered(name, specs, u.hdr.nr_progs,- w->devices, w->count,- note_uncovered, w);- if (!ret)- ret = note_unclassified(w, name, specs,- u.hdr.nr_progs);+ if (ret) {+ note_err(&w->c->check_err, ret);+ note_err(&w->c->coverage_err, ret);+ return note_links_unverified(w, name);}+ /* The bytes are what was loaded; metadata it cannot parse is a lost link. */+ ret = upatch_verified_specs(&u, &specs);+ if (ret) {+ upatch_close(&u);+ if (ret != -ESTALE) {+ note_err(&w->c->check_err, ret);+ note_err(&w->c->coverage_err, ret);+ return 0;+ }+ return note_links(w, name, ret);+ }+ ret = note_links(w, name, links_complete_specs(name, specs,+ u.hdr.nr_progs));+ if (!ret)+ ret = check_coverage(w, name, specs, u.hdr.nr_progs);prog_specs_free(specs, u.hdr.nr_progs);upatch_close(&u);return ret;}-int upatch_xdp_uncovered(const struct measured_set *s,- int (*cb)(const char *name, const char *dev,- const char *why, void *ctx),- void *ctx, int *check_err)+void upatch_checks_run(const struct measured_set *s, struct upatch_checks *c){- struct uncovered_walk w = { .cb = cb, .ctx = ctx };+ struct checks_walk w = { .s = s, .c = c };int ret;- /* Elsewhere the devices and their indexes are another namespace's. */- ret = netns_initial();- if (ret) {- if (!*check_err)- *check_err = ret;- return 0;+ /*+ * Elsewhere the devices are another namespace's, whether or not an+ * XDP upatch is found to check them against.+ */+ if (c->uncovered) {+ ret = netns_initial();+ note_err(&c->coverage_err, ret);+ w.devices_tried = ret != 0;+ }+ ret = upatch_each_pinned(check_one, &w);+ /* Nothing walked is nothing checked, coverage included. */+ if (ret < 0) {+ note_err(&c->check_err, ret);+ note_err(&c->coverage_err, ret);}- ret = walk_pinned(s, xdp_uncovered, NULL, &w, check_err);xfree(w.devices, size_mul(w.count, sizeof(*w.devices)));- return ret < 0 ? ret : w.found;}diff --git a/util/upatch_verify.h b/util/upatch_verify.hindex 0ed948d5..bfb0b5a2 100644--- a/util/upatch_verify.h+++ b/util/upatch_verify.h@@ -71,28 +71,31 @@ bool upatch_pin_measured(const struct measured_set *s, const char *name,__u8 payload[UPATCH_PAYLOAD_HASH_LEN]);/*- * The XDP upatches the set holds whose archive is not what was loaded. cb- * runs for each and a non-zero return ends the walk with that value. The- * number found. The first check that could not run leaves its -errno in- * *check_err, unless that already holds one.+ * The checks status makes of the upatches the set holds, in one walk that+ * reads and hashes each XDP archive once. Each callback that is not NULL+ * runs for a hit, and a non-zero return ends the walk.+ * changed: an XDP upatch whose archive is not what was loaded.+ * lost: an upatch missing a link, or whose archive is gone or unusable.+ * uncovered: a device an XDP upatch selects that does not run it, and why.*/-int upatch_xdp_archives_changed(const struct measured_set *s,- int (*cb)(const char *name, void *ctx),- void *ctx, int *check_err);+struct upatch_checks {+ int (*changed)(const char *name, void *ctx);+ void *changed_ctx;+ int (*lost)(const char *name, void *ctx);+ void *lost_ctx;+ int (*uncovered)(const char *name, const char *dev,+ const char *why, void *ctx);+ void *uncovered_ctx;+ int nr_changed;+ int nr_lost;+ int nr_uncovered;+ /* The first changed or lost check that could not run. */+ int check_err;+ /* The first coverage check that could not run. */+ int coverage_err;+};-/* As upatch_xdp_archives_changed(), for a lost link or an unusable archive. */-int upatch_links_lost(const struct measured_set *s,- int (*cb)(const char *name, void *ctx), void *ctx,- int *check_err);--/*- * The devices an XDP upatch the set holds selects and that do not run it.- * cb runs for each with the upatch, the device and why. The number found,- * with *check_err as above.- */-int upatch_xdp_uncovered(const struct measured_set *s,- int (*cb)(const char *name, const char *dev,- const char *why, void *ctx),- void *ctx, int *check_err);+/* The counts hold what was found even when a callback ends the walk. */+void upatch_checks_run(const struct measured_set *s, struct upatch_checks *c);#endif /* SHIRUDO_UTIL_UPATCH_VERIFY_H */