Skip to content

eBPF-based security platform

upatch: require every link of a loaded upatch

John Fastabende9c5921d1698
diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rst
index 8dab05f2..6ca6337a 100644
--- a/Documentation/operate/run/read-status.rst
+++ b/Documentation/operate/run/read-status.rst
@@ -237,7 +237,10 @@ verdict is derived from them.
the loaded payload: <names>``: the sealed archive of a loaded XDP
upatch has changed, or is gone, so ``upatch reconcile`` refuses to
run and new devices stay uncovered. Handle it as an incident:
- :doc:`/operate/troubleshoot/incident-tampered`. ``WARN`` ``no
+ :doc:`/operate/troubleshoot/incident-tampered`. ``ALERT`` ``<n>
+ upatches are missing a link: <names>``: a loaded upatch has fewer
+ link pins than its signed metadata implies, so part of it is
+ detached. Handle it the same way. ``WARN`` ``no
record of this boot to check the pins against`` before ``init``, or
``pins not checked against the records: <error>``.
* - ``TpmMeasured``
diff --git a/Documentation/reference/cli/init.rst b/Documentation/reference/cli/init.rst
index 0b9c8eb0..27bd2a8c 100644
--- a/Documentation/reference/cli/init.rst
+++ b/Documentation/reference/cli/init.rst
@@ -57,8 +57,9 @@ Description
nothing. An archive without such a record, one root brought back
after its unload or one a load stopped before its extend left, is
ignored with a warning. An upatch already loaded with the archived
- payload is kept. An archived upatch that is not loaded after this
- boot's register was bound is refused as tampered.
+ payload and every link its signed metadata implies is kept; one
+ missing a link is refused as tampered. An archived upatch that is not
+ loaded after this boot's register was bound is refused as tampered.
#. It binds the measurement register under PCRs 9 and 11 and records one
init event that covers the certificate set, the restored upatches and
the upatch tree root. Without tree state the root is 64 zeros; tree
diff --git a/builtin-init.c b/builtin-init.c
index 2f2dafd2..12abaec6 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -59,6 +59,7 @@
#include "util/upatch.h"
#include "util/upatch_pins.h"
#include "util/upatch_resolve.h"
+#include "util/upatch_verify.h"
static void print_preflight(const struct preflight_result *r)
{
@@ -1016,6 +1017,14 @@ static int boot_restore_locked(bool recorded,
ret = -EINVAL;
goto next;
}
+ if (upatch_links_complete(set[cnt].name, path) != 1) {
+ pr_err("boot-restore: %s is loaded without every "
+ "link its signed metadata implies "
+ "(tampered); unload it or reboot",
+ set[cnt].name);
+ ret = -EPERM;
+ goto next;
+ }
pr_info("boot-restore: %s already loaded",
set[cnt].name);
cnt++;
diff --git a/builtin-status.c b/builtin-status.c
index b9f479f5..d0e38d24 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -856,8 +856,10 @@ int cmd_status(int argc, const char **argv)
bool otel_endpoint_configured = false;
char unmeasured_names[UNMEASURED_NAMES] = "";
char xdp_tampered_names[UNMEASURED_NAMES] = "";
+ char links_lost_names[UNMEASURED_NAMES] = "";
struct measured_set *mset = NULL;
int xdp_tampered = 0;
+ int links_lost = 0;
char config_root_hex[65] = "";
__u8 config_root[32];
int unmeasured, lock;
@@ -971,6 +973,8 @@ int cmd_status(int argc, const char **argv)
}
xdp_tampered = upatch_xdp_archives_changed(mset, note_unmeasured,
xdp_tampered_names);
+ links_lost = upatch_links_lost(mset, note_unmeasured,
+ links_lost_names);
measured_set_free(mset);
}
eventlog_unlock(lock);
@@ -1038,6 +1042,8 @@ int cmd_status(int argc, const char **argv)
cin.pins_detail = unmeasured_names;
cin.xdp_tampered = max(xdp_tampered, 0);
cin.xdp_tampered_detail = xdp_tampered_names;
+ cin.links_lost = max(links_lost, 0);
+ cin.links_lost_detail = links_lost_names;
keyring_cond_input(&cin, &bpf, &dmv);
if (json) {
diff --git a/testing/unit/conditions.c b/testing/unit/conditions.c
index 6cd5b2a5..6db89b7a 100644
--- a/testing/unit/conditions.c
+++ b/testing/unit/conditions.c
@@ -54,6 +54,25 @@ static void a_changed_archive_outranks_a_pin_warning(void)
EXPECT_STREQ(row->status, "ALERT");
}
+static void a_lost_link_is_tampered(void)
+{
+ struct conditions_input in = {
+ .reachable = true,
+ .parsed = true,
+ .pins_rc = -ENOENT,
+ .links_lost = 1,
+ .links_lost_detail = "fix",
+ };
+ struct condition c[CONDITIONS_MAX];
+ const struct condition *row;
+
+ row = upatches_measured(&in, c);
+ ASSERT_TRUE(row);
+ EXPECT_STREQ(row->status, "ALERT");
+ EXPECT_TRUE(strstr(row->detail, "missing a link: fix"));
+ EXPECT_STREQ(status_verdict(&in), "tampered");
+}
+
static void no_change_leaves_the_condition_alone(void)
{
struct conditions_input in = {
@@ -73,6 +92,7 @@ static void no_change_leaves_the_condition_alone(void)
static const struct unit_case cases[] = {
UNIT_CASE(changed_xdp_archive_is_tampered),
UNIT_CASE(a_changed_archive_outranks_a_pin_warning),
+ UNIT_CASE(a_lost_link_is_tampered),
UNIT_CASE(no_change_leaves_the_condition_alone),
{}
};
diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.c
index 623356d6..23ffdef0 100644
--- a/testing/unit/upatch_pins.c
+++ b/testing/unit/upatch_pins.c
@@ -58,6 +58,34 @@ static void loaded_needs_the_first_link_or_a_program(void)
EXPECT_TRUE(upatch_pins_loaded(&pins));
}
+static void complete_means_every_link(void)
+{
+ struct upatch_pins pins = {};
+
+ EXPECT_TRUE(upatch_pins_complete(&pins, 0));
+ EXPECT_FALSE(upatch_pins_complete(&pins, 1));
+ upatch_pins_note(&pins, "link");
+ EXPECT_TRUE(upatch_pins_complete(&pins, 1));
+
+ memset(&pins, 0, sizeof(pins));
+ upatch_pins_note(&pins, "link_0");
+ EXPECT_FALSE(upatch_pins_complete(&pins, 1));
+ EXPECT_FALSE(upatch_pins_complete(&pins, 2));
+ upatch_pins_note(&pins, "link_1");
+ EXPECT_TRUE(upatch_pins_complete(&pins, 2));
+ EXPECT_FALSE(upatch_pins_complete(&pins, 3));
+
+ memset(&pins, 0, sizeof(pins));
+ upatch_pins_note(&pins, "link_1");
+ upatch_pins_note(&pins, "link_2");
+ EXPECT_FALSE(upatch_pins_complete(&pins, 2));
+
+ memset(&pins, 0, sizeof(pins));
+ upatch_pins_note(&pins, "link_0");
+ upatch_pins_note(&pins, "link_2");
+ EXPECT_FALSE(upatch_pins_complete(&pins, 2));
+}
+
static void absent_upatch_has_no_pins(void)
{
struct upatch_pins pins = { .has_link = true };
@@ -72,6 +100,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(names_are_parsed_strictly),
UNIT_CASE(lowest_index_is_kept_in_any_order),
UNIT_CASE(loaded_needs_the_first_link_or_a_program),
+ UNIT_CASE(complete_means_every_link),
UNIT_CASE(absent_upatch_has_no_pins),
{}
};
diff --git a/util/conditions.c b/util/conditions.c
index c0e4416b..e4c82644 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -146,6 +146,12 @@ size_t conditions(const struct conditions_input *in,
in->xdp_tampered,
in->xdp_tampered_detail ?
in->xdp_tampered_detail : "?");
+ else if (in->links_lost)
+ set_detail(&c[n], "ALERT",
+ "%d upatches are missing a link: %s",
+ in->links_lost,
+ in->links_lost_detail ?
+ in->links_lost_detail : "?");
else if (in->pins_rc == -ENOENT)
set_detail(&c[n], "WARN",
"no record of this boot to check the pins against");
@@ -228,7 +234,8 @@ const char *status_verdict(const struct conditions_input *in)
if (!in->parsed)
return "unparseable";
if ((in->cm_seen && !in->chain_matches) || in->pins_unmeasured ||
- in->xdp_tampered || keyring_tampered(in) || config_untrusted(in))
+ in->xdp_tampered || in->links_lost || keyring_tampered(in) ||
+ config_untrusted(in))
return "tampered";
if (!in->lsm_attached)
return "lsm-off";
@@ -258,6 +265,8 @@ void status_reason(const struct conditions_input *in,
else if (in->xdp_tampered)
strscpy(buf, "the archive of a loaded XDP upatch differs from what was loaded",
cap);
+ else if (in->links_lost)
+ strscpy(buf, "a loaded upatch is missing a link", cap);
else if (in->pins_unmeasured)
strscpy(buf, "a upatch is pinned or archived without a record of this boot",
cap);
diff --git a/util/conditions.h b/util/conditions.h
index 4f625b83..dc8fdc0e 100644
--- a/util/conditions.h
+++ b/util/conditions.h
@@ -51,6 +51,8 @@ struct conditions_input {
const char *pins_detail; /* their names, when any */
int xdp_tampered; /* loaded XDP upatches whose archive changed */
const char *xdp_tampered_detail; /* their names, when any */
+ int links_lost; /* loaded upatches missing a link */
+ const char *links_lost_detail; /* their names, when any */
};
struct condition {
diff --git a/util/upatch_pins.c b/util/upatch_pins.c
index 4bb476b9..47ee4422 100644
--- a/util/upatch_pins.c
+++ b/util/upatch_pins.c
@@ -41,6 +41,8 @@ void upatch_pins_note(struct upatch_pins *pins, const char *entry)
} else if (pin_index(entry, "link_", &index)) {
if (!pins->nr_links || index < pins->first_link)
pins->first_link = index;
+ if (!pins->nr_links || index > pins->last_link)
+ pins->last_link = index;
pins->nr_links++;
} else if (pin_index(entry, XDP_PROG_PIN_PREFIX, &index)) {
if (!pins->nr_xdp_progs || index < pins->first_xdp_prog)
@@ -61,6 +63,16 @@ bool upatch_pins_loaded(const struct upatch_pins *pins)
pins->nr_xdp_progs;
}
+bool upatch_pins_complete(const struct upatch_pins *pins, __u32 nr_attach)
+{
+ if (!nr_attach)
+ return true;
+ if (nr_attach == 1)
+ return pins->has_link;
+ return pins->nr_links == nr_attach && !pins->first_link &&
+ pins->last_link == nr_attach - 1;
+}
+
int upatch_pins_read(const char *name, struct upatch_pins *out)
{
char dir[PATH_MAX];
diff --git a/util/upatch_pins.h b/util/upatch_pins.h
index a1aa705c..e9fd34c5 100644
--- a/util/upatch_pins.h
+++ b/util/upatch_pins.h
@@ -16,6 +16,7 @@ struct upatch_pins {
bool has_link;
__u32 nr_links;
__u32 first_link;
+ __u32 last_link;
__u32 nr_xdp_progs;
__u32 first_xdp_prog;
};
@@ -23,6 +24,9 @@ struct upatch_pins {
void upatch_pins_note(struct upatch_pins *pins, const char *entry);
bool upatch_pins_loaded(const struct upatch_pins *pins);
+/* Whether the pins are the links of an upatch of nr_attach non-XDP programs. */
+bool upatch_pins_complete(const struct upatch_pins *pins, __u32 nr_attach);
+
/* 0, or a negative errno; -ENOENT when the upatch has no pin directory. */
int upatch_pins_read(const char *name, struct upatch_pins *out);
diff --git a/util/upatch_verify.c b/util/upatch_verify.c
index dd0f6649..6a42ef90 100644
--- a/util/upatch_verify.c
+++ b/util/upatch_verify.c
@@ -74,22 +74,63 @@ int upatch_archive_verify(const char *name,
return 0;
}
+int upatch_links_complete(const char *name, const char *path)
+{
+ struct prog_spec *specs = NULL;
+ struct upatch_pins pins;
+ __u32 len, i, nr_attach = 0;
+ const void *attach;
+ bool has_xdp = false;
+ struct upatch u;
+ int ret;
+
+ ret = upatch_open(path, &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) : -EINVAL;
+ if (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;
+ }
+ ret = upatch_pins_read(name, &pins);
+ if (!ret)
+ ret = has_xdp || upatch_pins_complete(&pins, nr_attach);
+ prog_specs_free(specs, u.hdr.nr_progs);
+out:
+ upatch_close(&u);
+ return ret;
+}
+
+/* Whether the set holds name with the payload its pin directory records. */
+static bool 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)
+ return false;
+ hex_encode(payload, UPATCH_PAYLOAD_HASH_LEN, hex);
+ return measured_holds(s, name, hex);
+}
+
static bool xdp_archive_changed(const struct measured_set *s, const char *name)
{
__u8 payload[UPATCH_PAYLOAD_HASH_LEN];
- char hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];
struct upatch_pins pins;
- char dir[PATH_MAX];
struct upatch u;
int ret;
if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs ||
- upatch_pin_path(dir, sizeof(dir), name, NULL) ||
- getxattr(dir, XATTR_PAYLOAD, payload, sizeof(payload)) !=
- (ssize_t)sizeof(payload))
- return false;
- hex_encode(payload, sizeof(payload), hex);
- if (!measured_holds(s, name, hex))
+ !pin_measured(s, name, payload))
return false;
ret = upatch_archive_verify(name, payload, &u);
if (!ret)
@@ -97,12 +138,24 @@ static bool xdp_archive_changed(const struct measured_set *s, const char *name)
return ret == -ESTALE;
}
-int upatch_xdp_archives_changed(const struct measured_set *s,
- int (*cb)(const char *name, void *ctx),
- void *ctx)
+static bool links_lost(const struct measured_set *s, const char *name)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ char path[PATH_MAX];
+
+ if (!pin_measured(s, name, payload) ||
+ strsprintf(path, sizeof(path), "%s/%s.upatch", UPATCHES_DIR,
+ name) < 0)
+ return false;
+ return !upatch_links_complete(name, path);
+}
+
+static int walk_pinned(const struct measured_set *s,
+ bool (*hit)(const struct measured_set *s, const char *name),
+ int (*cb)(const char *name, void *ctx), void *ctx)
{
struct dirent *e;
- int changed = 0;
+ int found = 0;
int ret = 0;
DIR *d;
@@ -114,11 +167,24 @@ int upatch_xdp_archives_changed(const struct measured_set *s,
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") ||
upatch_name_from_bpffs(name, sizeof(name), e->d_name) ||
- !xdp_archive_changed(s, name))
+ !hit(s, name))
continue;
- changed++;
+ found++;
ret = cb(name, ctx);
}
closedir(d);
- return ret ? ret : changed;
+ return ret ? ret : found;
+}
+
+int upatch_xdp_archives_changed(const struct measured_set *s,
+ int (*cb)(const char *name, void *ctx),
+ void *ctx)
+{
+ return walk_pinned(s, xdp_archive_changed, cb, ctx);
+}
+
+int upatch_links_lost(const struct measured_set *s,
+ int (*cb)(const char *name, void *ctx), void *ctx)
+{
+ return walk_pinned(s, links_lost, cb, ctx);
}
diff --git a/util/upatch_verify.h b/util/upatch_verify.h
index 5ab6730b..99b6896d 100644
--- a/util/upatch_verify.h
+++ b/util/upatch_verify.h
@@ -24,6 +24,13 @@ int upatch_archive_verify(const char *name,
const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],
struct upatch *out);
+/*
+ * 1 when every link the signed metadata of the archive at path implies is
+ * pinned for name, 0 when one is missing, or a negative errno when that
+ * cannot be told.
+ */
+int upatch_links_complete(const char *name, const char *path);
+
struct measured_set;
/*
@@ -36,4 +43,8 @@ int upatch_xdp_archives_changed(const struct measured_set *s,
int (*cb)(const char *name, void *ctx),
void *ctx);
+/* As upatch_xdp_archives_changed(), for upatches that have lost a link. */
+int upatch_links_lost(const struct measured_set *s,
+ int (*cb)(const char *name, void *ctx), void *ctx);
+
#endif /* SHIRUDO_UTIL_UPATCH_VERIFY_H */