eBPF-based security platform
upatch: read signed sections only from a directory ending at the trailer
Daniel Borkmannf918b91fd967
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex b1be2206..57192df4 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -617,10 +617,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.* - ``upatch-signed-sections``- ``parse_signed_sections()``- Data of 15, 16, 40 and 64 bytes- - Sections kept only behind a valid trailer, the directory below- the trailer, each section inside the data below the directory;- none of a directory with a reserved word set or a tag unknown or- repeated; nothing kept on refusal+ - Sections kept only behind a valid trailer, the directory ending+ where the trailer begins, so that the signed trailer gives the+ length of the data, each section inside the data below the+ directory; none of a directory with a reserved word set or a tag+ unknown or repeated; nothing kept on refusal* - ``upatch-signed-name``- The ``NAME`` and ``DECODERS`` sections of``parse_signed_sections()``diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex aa757974..2bc441e3 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -161,7 +161,8 @@ signed config image.- ML-DSA-87 only, certificate key identifiers, ``--force``* - ``t2002-mldsa-sign``- The CMS form the kernel verifies, refusal of RSA and ML-DSA-65- keys, a one-bit change in the signed bytes+ keys, a one-bit change in the signed bytes, the signed bytes+ under a header that ends the loader elsewhere* - ``t2003-keys-set``- The trusted set and its digest, revoked certificates left out,``attest predict`` agreement, signatures accepted from eitherdiff --git a/Documentation/internals/signed-loader.rst b/Documentation/internals/signed-loader.rstindex e3f81423..5b57b10c 100644--- a/Documentation/internals/signed-loader.rst+++ b/Documentation/internals/signed-loader.rst@@ -127,7 +127,11 @@ made inside the test run, reach it with ``kernel`` true.* - check the artifact- The signed name must equal the header name, ``payload_sha256``must equal SHA-256 of ``loader_insns || data``, and the signed- attach metadata must parse. Each ``uprobe``, ``uretprobe``,+ attach metadata must parse. The header, which is not signed, says+ where the loader ends: the signed sections are read only when+ their directory ends at the trailer in the last bytes of the+ data, which no other division of the signed bytes leaves it+ doing. Each ``uprobe``, ``uretprobe``,``uprobe.multi`` and ``uretprobe.multi`` target is opened and itsGNU build ID compared before any BPF loads.* - ``BPF_MAP_CREATE`` of ``__loader.map``diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 87f5ceec..5d0ffd0f 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -217,7 +217,17 @@ What the target needs beyond the loader rides inside ``data``, behind theloader's own bytes, so the signature covers it, as Figure 1 shows. Theloader addresses only its own prefix of ``data``. The writer aligns eachsection and the directory to 8 bytes from the start of ``data`` and fills-the padding with zeros. The trailer is the last 16 bytes of ``data``.+the padding with zeros. The trailer is the last 16 bytes of ``data``, and+the directory ends where the trailer begins.++The signature and ``payload_sha256`` cover ``loader_insns`` followed by+``data`` with nothing between the two, and the header that gives their+lengths is not signed. The trailer is the last 16 of the signed bytes+wherever a header ends the loader, and ``dir_off + 16 * nr_sections + 16``+is the length of ``data``. A header that divides the same signed bytes+elsewhere therefore names a directory that does not end at the trailer.+The reader treats that directory as absent, and the load refuses for+the signed name it lacks.Trailer~~~~~~~@@ -241,7 +251,8 @@ Trailer* - +8- 4- ``dir_off``- - Offset of the directory in ``data``.+ - Offset of the directory in ``data``. The directory's last entry+ ends at the trailer.* - +12- 4- ``nr_sections``@@ -349,7 +360,7 @@ too, and the reader refuses a file whose header ``type`` differs from it. ABPF payload's header cannot be relabelled ``2``, since its signed sectionscarry no ``PTYP``. A Java payload cannot pass for a BPF one either: its``data`` has no loader, and any split of its signed bytes into a loader and-data moves the directory away from where the trailer points, so the reader+data leaves a directory that does not end at the trailer, so the readerfinds no ``NAME`` and the load refuses.UPGM section@@ -790,8 +801,8 @@ unload later refuses for the section it lacks, and#. ``data`` is at least 16 bytes long and its last 16 bytes are a trailerwith magic ``SGTR`` and version 1.-#. ``nr_sections`` is 1 to 8, and the directory lies between ``dir_off``- and the trailer.+#. ``nr_sections`` is 1 to 8, and the directory's entries start at+ ``dir_off`` and end where the trailer begins.#. Each entry has ``reserved`` 0 and a section that ends at or before``dir_off``.#. Each tag is ``NAME``, ``UPGM``, ``UNLD``, ``DCDB``, ``SHPL``, ``PTYP``diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 8861abdf..aa47be26 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -974,6 +974,21 @@ signature does not cover or whose header program and map counts, whichplace each program's descriptor in the loader context, differ from thesigned ones (``parser_rejected``).+The signature covers the instructions and the data as one run of bytes,+and the container header that says where the instructions end is not+signed. Root could move that end: the kernel checks the same signature+over the same bytes, and the pin, the register and the evidence would+record the same payload digest for another loader over other data. The+signed bytes decide the end themselves: their last 16 are the trailer of+the signed sections, the section directory must end where that trailer+begins, and its offset and count then give the length of the data. A+header that divides the signed bytes elsewhere leaves an artifact+without signed sections, which a load refuses for its missing signed+name (``parser_rejected``) before the kernel sees a loader. Past+shirudo, root without runtime authority can neither create nor fill a+metadata map, and the signed bytes as instructions alone end in data no+instruction reaches, which the verifier refuses.+An unload directive is a signed loader of its own and goes through thesame ``BPF_PROG_LOAD``. Before it removes anything, shirudo compares thepayload digest recorded on the upatch's pin directory, in the xattr@@ -999,7 +1014,7 @@ test makes the two differ, which takes runtime authority.``payload_mismatch`` and ``parser_rejected`` from ``shirudo upatch``:Counters: ``PinXattr`` for a forged payload binding; none for amodified loader, which the kernel refuses first-:Tests: ``t3104``, ``t6117``, ``t6138``+:Tests: ``t2002``, ``t3104``, ``t6117``, ``t6138``:Proofs: ``upatch-header``, ``upatch-signed-sections``,``upatch-signed-name``, ``upatch-name``, ``upatch-prog-specs``,``upatch-prog-uprobes``, ``upatch-uprobe-targets``,@@ -1542,10 +1557,10 @@ so the reader refuses a file whose type differs from the signed ``PTYP``section; a BPF artifact carries none and cannot be relabelled Java. AJava artifact relabelled BPF is refused by that check too. The twosignatures cover one byte stream under one key with no type in it,-though: signed Java data split into a loader and data of another-length loses its section directory, reads as BPF, and is then refused-only because it has no signed name and its bytes are no loader the-verifier accepts (D10). The jar is written+though: signed Java data split into a loader and data has a section+directory that no longer ends at its trailer, reads as BPF without+signed sections, and is then refused because it has no signed name,+and its bytes are no loader the verifier accepts (D10). The jar is writtensealed under ``/run/shirudo-java``, which ``init`` makes and labelsbefore anyone else can, outside the state root, which stays ``0700``.The seal refuses a write, rename, unlink or label change of the jar, anddiff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex 294b8fc9..43da1cb3 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -70,7 +70,8 @@ BUILD_BUG_ON(sizeof(struct upatch_header) != UPATCH_HEADER_SIZE);* The signed sections: what the target needs beyond the loader and its* data, appended to the data section before signing so the kernel's* signature check covers them, and located through a trailer in the- * data's last 16 bytes. The loader ignores the bytes behind its own.+ * data's last 16 bytes, which the section directory ends at. The loader+ * ignores the bytes behind its own.* A LOAD carries its name and its attach metadata, an UNLOAD directive* the name of its target and the payload digest it expects, and a LOAD* carries decoders when its programs declare any. A Java upatch carriesdiff --git a/testing/functional/signing/t2002-mldsa-sign.sh b/testing/functional/signing/t2002-mldsa-sign.shindex eadfb2b2..e076b32d 100755--- a/testing/functional/signing/t2002-mldsa-sign.sh+++ b/testing/functional/signing/t2002-mldsa-sign.sh@@ -9,7 +9,9 @@# without signingTime on OpenSSL 3.x) by verifying it with openssl over# the exact loader_insns || data bytes the kernel folds. Also checks# that the builder refuses a classical (RSA) signing key outright, and-# an ML-DSA key of a smaller parameter set than ML-DSA-87.+# an ML-DSA key of a smaller parameter set than ML-DSA-87, and that the+# same signed bytes under a header that ends the loader elsewhere keep+# no signed name.# Needs clang and shirudo-build; the ML-DSA cases need OpenSSL >= 3.5.cd "$(dirname "$0")/../.."@@ -98,6 +100,14 @@ content = b[ioff:ioff + ilen] + (b[doff:doff + dlen] if doff else b"")open(out + "/content.bin", "wb").write(content)open(out + "/tampered.bin", "wb").write(content[:-1] + bytes([content[-1] ^ 1]))open(out + "/siglen", "w").write(str(slen))+# The header is not signed: one that gives the loader's last instruction+# to the data describes the same signed bytes and the same digest.+header = bytearray(b[:360])+loader, data = content[:ilen - 8], content[ilen - 8:]+struct.pack_into("<8I", header, 304, 360, len(loader), 360 + len(content),+ slen, 0, 0, 360 + len(loader), len(data))+open(out + "/split.upatch", "wb").write(bytes(header) + loader + data ++ b[soff:soff + slen])PYopenssl x509 -inform DER -in "$WORK/keys/cert.der" -out "$WORK/cert.pem" 2>/dev/null@@ -134,6 +144,23 @@ test_expect_success 'a one-bit change in the signed bytes is rejected' '-CAfile "$WORK/cert.pem" -purpose any -out /dev/null 2>/dev/null'+test_expect_success 'the signed bytes split after another instruction keep no signed name' '+ "$S" upatch info "$WORK/u.upatch" --json 2>/dev/null | python3 -c "+import sys, json+d = json.load(sys.stdin)+assert d[\"signed_name\"] == \"t2002\", d.get(\"signed_name\")+print(d[\"payload_sha256\"], d[\"loader_insns_len\"] - 8, d[\"data_len\"] + 8)+" >"$WORK/signed" &&+ "$S" upatch info "$WORK/split.upatch" --json 2>/dev/null | python3 -c "+import sys, json+d = json.load(sys.stdin)+assert d[\"signed_name\"] is None, d.get(\"signed_name\")+assert d[\"attach_metadata_len\"] == 0, d.get(\"attach_metadata_len\")+print(d[\"payload_sha256\"], d[\"loader_insns_len\"], d[\"data_len\"])+" >"$WORK/split" &&+ cmp "$WORK/signed" "$WORK/split"+'+test_expect_success 'upatch info --json reports the artifact and signature length' '"$S" upatch info "$WORK/u.upatch" --json 2>/dev/null | python3 -c "import sys, jsondiff --git a/testing/proofs/cbmc/upatch-signed-sections.c b/testing/proofs/cbmc/upatch-signed-sections.cindex dbaf4091..73354d7f 100644--- a/testing/proofs/cbmc/upatch-signed-sections.c+++ b/testing/proofs/cbmc/upatch-signed-sections.c@@ -5,9 +5,10 @@* The signed sections of a .upatch, read from its data before any* signature is checked, for data of LEN bytes, each length proofs.list* names, with one section where it can hold a name: util/upatch.c keeps- * sections only behind a valid trailer, each inside the data and below the- * directory, the name terminated, none of a directory with a reserved word- * set or a tag unknown or repeated, and no length or count without them.+ * sections only behind a valid trailer that the directory ends at, each+ * inside the data and below the directory, the name terminated, none of a+ * directory with a reserved word set or a tag unknown or repeated, and no+ * length or count without them.*/#define PROOF_MEMCHR_QUANTIFIED@@ -117,8 +118,9 @@ void harness(void)nr && nr <= UPATCH_SIGNED_SECTIONS_MAX,"a valid trailer");__CPROVER_assert((__u64)dir_off + nr *- sizeof(struct upatch_signed_section) <=- len - sizeof(*tr), "the directory below the trailer");+ sizeof(struct upatch_signed_section) ==+ len - sizeof(*tr),+ "the directory ends where the trailer begins");__CPROVER_assert(!directory_broken(&u, dir_off, nr),"nothing kept of a directory with a reserved word, unknown or repeated tag");__CPROVER_assert(!u.signed_name ||diff --git a/testing/unit/upatch.c b/testing/unit/upatch.cindex c326c428..769f5623 100644--- a/testing/unit/upatch.c+++ b/testing/unit/upatch.c@@ -445,6 +445,65 @@ static void java_header_carries_no_loader(void)EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);}+/*+ * A BPF upatch whose signed bytes, the loader and then the data, hold a+ * second directory where a split 16 bytes earlier looks for one.+ */+struct split_fixture {+ struct upatch_header header;+ __u8 signature[8];+ __u8 insns[32];+ char name[UPATCH_NAME_MAX];+ struct upatch_signed_section other_dir;+ struct upatch_signed_section dir;+ struct upatch_signed_trailer trailer;+};++#define SPLIT_DATA_OFF offsetof(struct split_fixture, name)++/* The header gives the last shift bytes of the loader to the data. */+static void split_load(struct split_fixture *f, __u32 shift)+{+ memset(f, 0, sizeof(*f));+ f->header.magic = cpu_to_le64(UPATCH_MAGIC);+ f->header.version = cpu_to_le32(UPATCH_VERSION);+ f->header.type = cpu_to_le32(UPATCH_TYPE_BPF);+ memcpy(f->header.name, "fix", sizeof("fix"));+ f->header.signature_off =+ cpu_to_le32(offsetof(struct split_fixture, signature));+ f->header.signature_len = cpu_to_le32(sizeof(f->signature));+ f->header.loader_insns_off =+ cpu_to_le32(offsetof(struct split_fixture, insns));+ f->header.loader_insns_len = cpu_to_le32(sizeof(f->insns) - shift);+ f->header.data_off = cpu_to_le32(SPLIT_DATA_OFF - shift);+ f->header.data_len = cpu_to_le32(sizeof(*f) - SPLIT_DATA_OFF + shift);+ memset(f->insns, 'x', sizeof(f->insns));+ memcpy(f->name, "fix", sizeof("fix"));+ f->other_dir.tag = cpu_to_le32(UPATCH_SECTION_NAME);+ f->other_dir.len = cpu_to_le32(sizeof(f->name));+ f->dir = f->other_dir;+ f->trailer.magic = cpu_to_le32(UPATCH_SIGNED_TRAILER_MAGIC);+ f->trailer.version = cpu_to_le32(UPATCH_SIGNED_VERSION);+ f->trailer.dir_off =+ cpu_to_le32(offsetof(struct split_fixture, dir) - SPLIT_DATA_OFF);+ f->trailer.nr_sections = cpu_to_le32(1);+}++static void one_signature_keeps_its_sections_in_one_split(void)+{+ struct split_fixture f;+ struct upatch u;++ split_load(&f, 0);+ ASSERT_EQ(upatch_open_memory(&f, sizeof(f), &u), 0);+ EXPECT_TRUE(upatch_name_signed(&u));+ upatch_close(&u);+ split_load(&f, 16);+ ASSERT_EQ(upatch_open_memory(&f, sizeof(f), &u), 0);+ EXPECT_TRUE(!upatch_signed_name(&u));+ upatch_close(&u);+}+static const struct unit_case cases[] = {UNIT_CASE(prog_specs_take_the_signed_counts),UNIT_CASE(prog_specs_refuse_another_header_count),@@ -463,6 +522,7 @@ static const struct unit_case cases[] = {UNIT_CASE(java_load_opens_with_its_jar),UNIT_CASE(java_header_needs_the_signed_type),UNIT_CASE(java_header_carries_no_loader),+ UNIT_CASE(one_signature_keeps_its_sections_in_one_split),{}};diff --git a/util/upatch.c b/util/upatch.cindex 59386f28..fe1d98df 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -252,9 +252,16 @@ static void forget_signed_sections(struct upatch *u)/** The signed sections behind the loader data. Each lies below the- * directory, the directory below the trailer, and each tag appears- * once; a directory that breaks any of that is taken for none, so a- * load or unload refuses for what it then lacks.+ * directory, the directory ends where the trailer begins, and each tag+ * appears once; a directory that breaks any of that is taken for none,+ * so a load or unload refuses for what it then lacks.+ *+ * The signature covers insns || data with nothing between the two, and+ * the header that says where one ends is not signed. The trailer is the+ * last of the signed bytes wherever the header puts that end, and a+ * directory that ends at the trailer makes dir_off and the count the+ * length of the data: of all the ways to split what one signature+ * covers, only the signer's keeps its sections.*/static void parse_signed_sections(struct upatch *u){@@ -275,7 +282,7 @@ static void parse_signed_sections(struct upatch *u)end = u->hdr.data_len - sizeof(tr);if (nr == 0 || nr > UPATCH_SIGNED_SECTIONS_MAX ||dir_off > end ||- (__u64)nr * sizeof(struct upatch_signed_section) >+ (__u64)nr * sizeof(struct upatch_signed_section) !=end - dir_off)return;dir = data + dir_off;