Skip to content

eBPF-based security platform

upatch: carry signed policy file labels

John Fastabendb85459861311
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index d897bb0d..61e1b36c 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -81,6 +81,31 @@ enum upatch_section_tag {
UPATCH_SECTION_ATTACH = 0x4d475055u, /* "UPGM" le */
UPATCH_SECTION_UNLOAD = 0x444c4e55u, /* "UNLD" le */
UPATCH_SECTION_DECODERS = 0x42444344u, /* "DCDB" le */
+ SHIRUDO_UPATCH_SECTION_POLICY_LABELS = 0x4c504853u, /* "SHPL" le */
+ SHIRUDO_UPATCH_SECTION_POLICY_UNLABELS = 0x554c5053u, /* "SPLU" le */
+};
+
+#define SHIRUDO_UPATCH_SECTION_NAME UPATCH_SECTION_NAME
+#define SHIRUDO_UPATCH_SECTION_ATTACH UPATCH_SECTION_ATTACH
+#define SHIRUDO_UPATCH_SECTION_UNLOAD UPATCH_SECTION_UNLOAD
+#define SHIRUDO_UPATCH_SECTION_DECODERS UPATCH_SECTION_DECODERS
+
+#define SHIRUDO_UPATCH_POLICY_LABELS_MAGIC 0x4c504853u /* "SHPL" le */
+#define SHIRUDO_UPATCH_POLICY_LABELS_VERSION 1u
+#define SHIRUDO_UPATCH_POLICY_NAME_MAX 64u
+#define SHIRUDO_UPATCH_POLICY_PATH_MAX 4096u
+
+struct shirudo_upatch_policy_label_header {
+ __u32 magic;
+ __u32 version;
+ __u32 count;
+ __u32 reserved;
+};
+
+struct shirudo_upatch_policy_label_entry {
+ __u64 bit;
+ char policy[SHIRUDO_UPATCH_POLICY_NAME_MAX];
+ char path[SHIRUDO_UPATCH_POLICY_PATH_MAX];
};
struct upatch_signed_section {
diff --git a/util/upatch.c b/util/upatch.c
index 5bc64cab..473c7868 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -120,6 +120,35 @@ static bool has_nul(const char *s, size_t n)
return memchr(s, '\0', n);
}
+static bool parse_policy_labels(const __u8 *p, __u32 len)
+{
+ struct shirudo_upatch_policy_label_header h;
+ __u32 count;
+
+ if (len < sizeof(h))
+ return false;
+ memcpy(&h, p, sizeof(h));
+ count = le32toh(h.count);
+ if (le32toh(h.magic) != SHIRUDO_UPATCH_POLICY_LABELS_MAGIC ||
+ le32toh(h.version) != SHIRUDO_UPATCH_POLICY_LABELS_VERSION ||
+ le32toh(h.reserved) || !count ||
+ (__u64)sizeof(h) + (__u64)count *
+ sizeof(struct shirudo_upatch_policy_label_entry) != len)
+ return false;
+ for (__u32 i = 0; i < count; i++) {
+ struct shirudo_upatch_policy_label_entry e;
+ __u64 bit;
+
+ memcpy(&e, p + sizeof(h) + i * sizeof(e), sizeof(e));
+ bit = le64toh(e.bit);
+ if (!bit || (bit & (bit - 1)) ||
+ !has_nul(e.policy, sizeof(e.policy)) || !e.policy[0] ||
+ !has_nul(e.path, sizeof(e.path)) || e.path[0] != '/')
+ return false;
+ }
+ return true;
+}
+
/* The DECODERS section: entries and sources within the blob. */
static bool parse_decoders(struct upatch *u, const __u8 *bp,
__u32 decoder_len)
@@ -184,6 +213,10 @@ static void forget_signed_sections(struct upatch *u)
u->signed_attach = NULL;
u->signed_attach_len = 0;
u->signed_unload_payload = NULL;
+ u->signed_policy_labels = NULL;
+ u->signed_policy_labels_len = 0;
+ u->signed_policy_unlabels = NULL;
+ u->signed_policy_unlabels_len = 0;
}
/*
@@ -252,6 +285,20 @@ static void parse_signed_sections(struct upatch *u)
if (!parse_decoders(u, data + off, len))
goto reject;
break;
+ case SHIRUDO_UPATCH_SECTION_POLICY_LABELS:
+ bit = 16;
+ if (!parse_policy_labels(data + off, len))
+ goto reject;
+ u->signed_policy_labels = data + off;
+ u->signed_policy_labels_len = len;
+ break;
+ case SHIRUDO_UPATCH_SECTION_POLICY_UNLABELS:
+ bit = 32;
+ if (!parse_policy_labels(data + off, len))
+ goto reject;
+ u->signed_policy_unlabels = data + off;
+ u->signed_policy_unlabels_len = len;
+ break;
default:
goto reject;
}
@@ -363,6 +410,20 @@ const __u8 *upatch_signed_unload_payload(const struct upatch *u)
return u->map ? u->signed_unload_payload : NULL;
}
+const void *upatch_signed_policy_labels(const struct upatch *u,
+ __u32 *len)
+{
+ *len = u->map ? u->signed_policy_labels_len : 0;
+ return u->map ? u->signed_policy_labels : NULL;
+}
+
+const void *upatch_signed_policy_unlabels(const struct upatch *u,
+ __u32 *len)
+{
+ *len = u->map ? u->signed_policy_unlabels_len : 0;
+ return u->map ? u->signed_policy_unlabels : NULL;
+}
+
const void *upatch_loader(const struct upatch *u)
{
if (!u->map)
diff --git a/util/upatch.h b/util/upatch.h
index 4dae49a7..2d1578c7 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -54,6 +54,10 @@ struct upatch {
const __u8 *signed_attach;
__u32 signed_attach_len;
const __u8 *signed_unload_payload; /* 32 bytes */
+ const void *signed_policy_labels;
+ __u32 signed_policy_labels_len;
+ const void *signed_policy_unlabels;
+ __u32 signed_policy_unlabels_len;
};
struct upatch_decoder_view {
@@ -107,6 +111,12 @@ const void *upatch_signed_attach(const struct upatch *u,
__u32 *len);
const __u8 *upatch_signed_unload_payload(const struct upatch *u);
+const void *upatch_signed_policy_labels(const struct upatch *u,
+ __u32 *len);
+
+const void *upatch_signed_policy_unlabels(const struct upatch *u,
+ __u32 *len);
+
/*
* Validate an externally-provided upatch name: non-empty, doesn't
* start with '.', and matches [A-Za-z0-9._-]+. Rejects anything that