Skip to content

eBPF-based security platform

upatch: refuse XDP programs that share a class where the artifact is read

John Fastabend379408878d31
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 49ef6fc8..7f9c0a59 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -752,6 +752,8 @@ Within the ``.upatch`` reader, the proofs cover its parts but not their
composition: ``upatch_parse()`` and ``upatch_open_memory()`` join the
header, the signed sections, the decoders and the policy labels the
proofs cover one by one, and a proof of the whole file runs out of
-memory. A program table of two or more programs, ``find_rodata_data()``
+memory. A program table of two or more programs, and with it the rule
+that two XDP programs select disjoint device classes, which the
+``upatch`` unit cases check, ``find_rodata_data()``
on loaders of other shapes, ``btf_dump_cost()``, the ``.rodata`` printers
and the libbpf calls behind ``btf_rodata_btf()`` have no proof.
diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rst
index 3c3886dd..7d6a3177 100644
--- a/Documentation/reference/formats/upatch-format.rst
+++ b/Documentation/reference/formats/upatch-format.rst
@@ -435,7 +435,8 @@ XDP retains the 160-byte entry and UPGM version 1; readers that do not
know kind 8 refuse it. Its ``target`` and ``map`` are empty. Device-class
bits mean network-facing (1), virtual (2), VM (8) and unknown (16); 27
selects all classes. Bit 4 is not a class, and a reader refuses a mask
-that sets it or any bit above 16.
+that sets it or any bit above 16, and a table whose XDP entries share a
+class.
When any entry has kind 7, one ``UPUT`` block follows the program entries
and ends the section. Without a kind 7 entry the section ends after the
diff --git a/testing/unit/upatch.c b/testing/unit/upatch.c
index 44726c19..8350eb85 100644
--- a/testing/unit/upatch.c
+++ b/testing/unit/upatch.c
@@ -185,6 +185,39 @@ static void xdp_masks_are_signed_and_bounded(void)
}
}
+static int parse_two_xdp(__u32 first, __u32 second, struct prog_spec **specs)
+{
+ __u8 buf[sizeof(struct upatch_prog_metadata) +
+ 2 * sizeof(struct upatch_prog_entry)];
+ struct upatch_prog_metadata md = {};
+ struct upatch_prog_entry prog = {};
+
+ md.magic = cpu_to_le32(UPATCH_PROG_MAGIC);
+ md.version = cpu_to_le32(UPATCH_PROG_VERSION);
+ md.nr_progs = cpu_to_le32(2);
+ memcpy(buf, &md, sizeof(md));
+ prog.role = cpu_to_le32(UPATCH_PROG_ROLE_ATTACH);
+ prog.kind = cpu_to_le32(UPATCH_ATTACH_XDP);
+ prog.xdp.device_classes = cpu_to_le32(first);
+ memcpy(buf + sizeof(md), &prog, sizeof(prog));
+ prog.xdp.device_classes = cpu_to_le32(second);
+ memcpy(buf + sizeof(md) + sizeof(prog), &prog, sizeof(prog));
+ return prog_specs_parse(buf, sizeof(buf), 0, 2, specs);
+}
+
+static void xdp_programs_select_disjoint_classes(void)
+{
+ struct prog_spec *specs = NULL;
+
+ ASSERT_EQ(parse_two_xdp(UPATCH_XDP_NETWORK, UPATCH_XDP_VIRTUAL, &specs),
+ 0);
+ prog_specs_free(specs, 2);
+ specs = NULL;
+ EXPECT_EQ(parse_two_xdp(UPATCH_XDP_NETWORK | UPATCH_XDP_VIRTUAL,
+ UPATCH_XDP_VIRTUAL, &specs), -EINVAL);
+ EXPECT_FALSE(specs);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(prog_specs_take_the_signed_counts),
UNIT_CASE(prog_specs_refuse_another_header_count),
@@ -195,6 +228,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(name_fits_its_pin_directory),
UNIT_CASE(xdp_classes_come_from_xdp_programs_alone),
UNIT_CASE(xdp_masks_are_signed_and_bounded),
+ UNIT_CASE(xdp_programs_select_disjoint_classes),
{}
};
diff --git a/util/upatch.c b/util/upatch.c
index ac54fa33..27f22696 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -603,7 +603,7 @@ __u32 prog_specs_xdp_classes(const struct prog_spec *specs, __u32 nr_progs)
int prog_specs_parse(const void *metadata, __u32 metadata_len,
__u32 nr_maps, __u32 nr_progs, struct prog_spec **out)
{
- __u32 magic, version, count, i, nr_uprobes = 0;
+ __u32 magic, version, count, i, nr_uprobes = 0, xdp_classes = 0;
const struct upatch_prog_entry empty = {};
struct upatch_prog_metadata md;
struct prog_spec *specs = NULL;
@@ -699,12 +699,15 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,
break;
case UPATCH_ATTACH_XDP:
spec->device_classes = le32_to_cpu(prog.xdp.device_classes);
+ /* A device runs one native XDP program at a time. */
if (!spec->device_classes ||
(spec->device_classes & ~(__u32)UPATCH_XDP_ALL) ||
+ (spec->device_classes & xdp_classes) ||
prog.xdp.reserved ||
memcmp(prog.target, empty.target, sizeof(prog.target)) ||
memcmp(prog.map, empty.map, sizeof(prog.map)))
goto out;
+ xdp_classes |= spec->device_classes;
spec->kind = kind;
continue;
default: