eBPF-based security platform
upatch: let a upatch of any name length carry decoders
Daniel Borkmannaf0e02484919
diff --git a/Documentation/concepts/background/luajit.rst b/Documentation/concepts/background/luajit.rstindex 5c2110e0..25f8b11e 100644--- a/Documentation/concepts/background/luajit.rst+++ b/Documentation/concepts/background/luajit.rst@@ -69,8 +69,8 @@ The BPF source declares each decoder in ``.rodata`` with a:src:`bpf/lib/decoder.h`. The manifest starts with the magic``DECD`` and names the language (``1``, Lua), the host API version, theinput format (``1``), the signal to export (``1`` for a log, ``2`` for a-metric), the upatch and map it decodes, the Lua source file relative to-the BPF source, and the entry point.+metric), the map it decodes and, where it fits, the upatch's name, the+Lua source file relative to the BPF source, and the entry point.``shirudo-build`` finds the manifests when it builds the artifact, readseach source file and embeds the sources in the ``DCDB`` section of the@@ -82,9 +82,8 @@ changed without the signing key. The format is inOn the target, ``shirudo upatch load`` keeps the artifact it loaded as``/var/lib/shirudo/upatches/<name>.upatch``, sealed state that onlyshirudo writes. At export, shirudo opens that archived copy as sealed-state, looks for the decoder whose upatch and map names match the map-being exported and runs it. There is no decoder search path and no-directory to drop a script into.+state, looks for the decoder of the map being exported and runs it.+There is no decoder search path and no directory to drop a script into.Running a decoder-----------------diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex a52d31c4..a33f0dbf 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -177,7 +177,8 @@ signed config image.- Malformed ``.upatch`` containers are refused, files over 16 MiBwithout mapping them* - ``t3002-upatch-decoders``- - Decoder metadata, Lua sandbox limits, refusal of bytecode+ - Decoder metadata, Lua sandbox limits, refusal of bytecode, a+ decoder of a upatch named with 255 characters, one decoder per map* - ``t3003-upatch-map-types``- Map type labels, arena maps only through a serializer* - ``t3004-upatch-serializer-metadata``diff --git a/Documentation/operate/plan/scalability.rst b/Documentation/operate/plan/scalability.rstindex 3759986f..4a3c6501 100644--- a/Documentation/operate/plan/scalability.rst+++ b/Documentation/operate/plan/scalability.rst@@ -246,10 +246,7 @@ One upatch- 4 GiB (``kernel/bpf/arena.c``)* - Name- 255 characters from ``A-Z``, ``a-z``, ``0-9``, ``.``, ``_`` and- ``-``, each ``.`` counting as two, not starting with ``.``. A- decoder names its upatch in 64 bytes, so only upatches with names- of at most 63 characters can have one (U19 in the- :doc:`/security/threat-model`).+ ``-``, each ``.`` counting as two, not starting with ``.``.* - Map export- :doc:`/upatches/write/maps-arenas-serializers` and:doc:`/upatches/write/decoders` give the serializer and decoder limits.diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 66f602c9..87f5ceec 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -626,7 +626,10 @@ Each decoder entry:* - +16- 64- ``upatch``- - NUL-terminated upatch name. The writer requires the artifact's name.+ - NUL-terminated, empty or the artifact's name. The writer refuses+ another name. A reader ignores the field: the decoder belongs to+ the upatch whose signed data carries it, and the reader picks it+ by ``map``.* - +80- 16- ``map``@@ -654,8 +657,8 @@ the BPF program declares in ``.rodata`` (:src:`bpf/lib/decoder.h`,192 bytes, magic ``0x44434544``, the bytes ``DECD``, ``u16`` fields``version``, ``language``, ``api_version``, ``input_format``,``export_signal`` and ``reserved``, then the same four strings). It scans-``.rodata`` at 4-byte steps, accepts one decoder per upatch and map, and-reads each source file, at most 16 MiB. A decoder whose ``language``,+``.rodata`` at 4-byte steps, accepts one decoder per map, and reads each+source file, at most 16 MiB. A decoder whose ``language``,``api_version``, ``input_format`` or ``export_signal`` differs from thevalues above fails the build. :doc:`/upatches/write/decoders` covers the decoderAPI.diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex bb392ec7..11f86dd6 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1474,7 +1474,7 @@ given (R2).:Refusal: ``EACCES`` from ``bpf()`` and from opening a pin:Counters: ``ObjGet``, ``MapFdWrite``, ``MapWrite``, ``ProgRun``:Tests: ``t6107``, ``t6108``, ``t6109``, ``t6110``-:Residual: R2, U19+:Residual: R2T14 Substituting the TPM~~~~~~~~~~~~~~~~~~~~~~~~@@ -2109,19 +2109,6 @@ U17 A stopped runtime command keeps its descriptorslist; compare the loaded links with the measured set when one isfound.-U19 A decoder cannot name a long upatch- :Affects: Lua decoders and map export (T13).- :Consequence: A decoder names its upatch in a 64-byte field, and- ``shirudo build`` and map export compare it with the whole upatch- name, which can have up to 255 characters. A upatch whose name has- 64 characters or more cannot carry a decoder, and map export sends- its maps as raw entries, not as the records a decoder would make of- them.- :Status: Confirmed in code for the field sizes; how ``shirudo build``- handles a longer name is a code-path reading, not reproduced.- :Meanwhile: Keep the names of upatches that carry decoders at 63- characters or fewer.-U20 The keepalive reports only the last 512 tree operations:Affects: The keepalive body (``base_seq``, ``base_root``, ``mask``)and what a backend can check of a node's loaded set.diff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rstindex ebe0c057..733146e9 100644--- a/Documentation/upatches/learn/concepts.rst+++ b/Documentation/upatches/learn/concepts.rst@@ -225,9 +225,6 @@ file, the eventlog record and the target of its unload directive.* - tree id- Equal to the upatch name. :doc:`/upatches/publish/publishing` explainswhy.- * - upatch with decoders- - At most 63 characters, because the decoder manifest holds the- upatch name in a 64-byte field.* - map- At most 15 characters. The kernel keeps 15 characters of a mapname, and decoders and serializers name their map in a 16-bytediff --git a/Documentation/upatches/write/decoders.rst b/Documentation/upatches/write/decoders.rstindex 651b8a16..06259eb0 100644--- a/Documentation/upatches/write/decoders.rst+++ b/Documentation/upatches/write/decoders.rst@@ -56,7 +56,8 @@ Declare the decoderThe builder finds manifests by their magic in the object's ``.rodata``,so declare the manifest ``const``. It also passes over a manifest with- an empty string field or a zero ``language``, ``api_version``,+ an empty ``map``, ``source`` or ``entrypoint``, a string field without+ a terminating NUL, or a zero ``language``, ``api_version``,``input_format`` or ``export_signal``, without an error, so check theresult as step 6 shows. Set the fields as follows:@@ -76,8 +77,11 @@ Declare the decoder- ``UPATCH_DECODER_EXPORT_LOG`` or``UPATCH_DECODER_EXPORT_METRIC``.* - ``upatch``- - The upatch name, at most 63 characters. In a package that is the- source's base name.+ - Empty, or the upatch name, which the field holds up to 63+ characters. In a package that is the source's base name. Leave+ it out for a longer name: one of 64 characters or more leaves no+ room for the terminating NUL. The decoder belongs to the upatch+ whose signed data carries it either way.* - ``map``- The name of a map of the object, at most 15 characters. Onedecoder per map.@@ -226,9 +230,10 @@ Build and check itoperator$ shirudo upatch info <file>.upatch``<file>`` is your upatch. The ``Decoders`` section lists each decoder- with its target as ``<upatch>/<map>``, the language and API version,- the input format ``shirudo.map.v1``, the export signal, and the source- file with its entry point and size.+ with its target as ``<upatch>/<map>``, ``<upatch>`` empty where the+ manifest leaves it out, the language and API version, the input format+ ``shirudo.map.v1``, the export signal, and the source file with its+ entry point and size.Run it on a target------------------@@ -242,7 +247,7 @@ Run it on a target``shirudo-runtime`` is the command in the test VM, as:doc:`/upatches/test/testing`- explains. ``<upatch>`` and ``<map>`` are the names in the manifest. The+ explains. ``<upatch>`` is the upatch name and ``<map>`` the map's. Thecommand prints what the decoder returned as one JSON document. A failureprints one of ``upatch map: decoder load failed: <message>``, ``upatchmap: decoder entrypoint '<name>' not found``, ``upatch map: decoderdiff --git a/bpf/lib/decoder.h b/bpf/lib/decoder.hindex d9a54737..4d093c04 100644--- a/bpf/lib/decoder.h+++ b/bpf/lib/decoder.h@@ -23,7 +23,7 @@ struct decoder_manifest {__u16 input_format; /* UPATCH_DECODER_INPUT_MAP_V1 */__u16 export_signal; /* UPATCH_DECODER_EXPORT_* */__u16 reserved;- char upatch[64];+ char upatch[64]; /* empty, or the upatch's name */char map[16];char source[64];char entrypoint[32];diff --git a/testing/functional/upatch/t3002-upatch-decoders.sh b/testing/functional/upatch/t3002-upatch-decoders.shindex b65f8f80..92e06d27 100755--- a/testing/functional/upatch/t3002-upatch-decoders.sh+++ b/testing/functional/upatch/t3002-upatch-decoders.sh@@ -6,7 +6,8 @@# metadata embedded in a .upatch, offline (no kernel, no root), and the# decoder runs under the sandbox limits. Builds a tiny fentry program# from a .bpf.c that declares a decoder manifest, signs it with a-# throwaway key, and exercises the decoder through the test helper.+# throwaway key, and exercises the decoder through the test helper,+# also under a name too long for the manifest's upatch field.# Needs clang + shirudo-build (skips otherwise).cd "$(dirname "$0")/../.."@@ -171,6 +172,59 @@ test_expect_success 'Lua decoder output replaces invalid UTF-8' '"\"\\ufffd\"" ]'+# A name of 255 characters, which the manifest's upatch field cannot+# hold, so the manifest leaves it out.+LONG=$(printf 't3002-%0249d' 0)+mkdir -p "$WORK/long/decoders" "$WORK/dup/decoders"+cp "$WORK/decoders/t3002.lua" "$WORK/long/decoders/"+cp "$WORK/decoders/t3002.lua" "$WORK/dup/decoders/"+grep -v '\.upatch = ' "$WORK/t.bpf.c" >"$WORK/long/t.bpf.c"+{+ cat "$WORK/t.bpf.c"+ cat <<'EOF'+const struct decoder_manifest t0005_unnamed_decoder SEC(".rodata") = {+ .magic = DECODER_MANIFEST_MAGIC,+ .version = UPATCH_DECODER_VERSION,+ .language = UPATCH_DECODER_LANG_LUA,+ .api_version = 1,+ .input_format = UPATCH_DECODER_INPUT_MAP_V1,+ .export_signal = UPATCH_DECODER_EXPORT_LOG,+ .map = "t0005_map",+ .source = "decoders/t3002.lua",+ .entrypoint = "decode",+};+EOF+} >"$WORK/dup/t.bpf.c"+cat >"$WORK/check-long.py" <<'PY'+import json, sys+info, name = json.load(open(sys.argv[1], encoding="utf-8")), sys.argv[2]+assert info["name"] == name and info["signed_name"] == name, info+decoders = [(d["upatch"], d["map"]) for d in info["decoders"]]+assert decoders == [("", "t0005_map")], decoders+records = json.load(open(sys.argv[3], encoding="utf-8"))["records"]+assert records[0]["name"] == "bash", records+PY++test_expect_success 'a upatch named with 255 characters carries a decoder that names none' '+ ( cd .. && "$B" "$WORK/long/t.bpf.c" -o "$WORK/long/u.upatch" \+ --vmlinux vendor/linux/vmlinux.h --name "$LONG" \+ --key "$WORK/k.pem" --cert "$WORK/c.der" ) >/dev/null 2>&1 &&+ "$S" upatch info "$WORK/long/u.upatch" --json >"$WORK/long/info.json" &&+ ./helper/test-upatch-decode json "$WORK/long/u.upatch" "$LONG" \+ >"$WORK/long/decoded.json" &&+ python3 "$WORK/check-long.py" "$WORK/long/info.json" "$LONG" \+ "$WORK/long/decoded.json"+'++test_expect_success 'a second decoder for a map is refused, named or not' '+ if ( cd .. && "$B" "$WORK/dup/t.bpf.c" -o "$WORK/dup/u.upatch" \+ --vmlinux vendor/linux/vmlinux.h --name t3002 \+ --key "$WORK/k.pem" --cert "$WORK/c.der" ) >"$WORK/dup/out" 2>&1+ then false; fi &&+ grep -q "duplicate decoder for .t3002/t0005_map." "$WORK/dup/out" &&+ test ! -e "$WORK/dup/u.upatch"+'+python3 - "$WORK/u.upatch" "$WORK" <<'PY'import pathlib, struct, syssrc, root = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])diff --git a/testing/fuzz/fuzz-decoder.c b/testing/fuzz/fuzz-decoder.cindex a0f65145..32ca9504 100644--- a/testing/fuzz/fuzz-decoder.c+++ b/testing/fuzz/fuzz-decoder.c@@ -80,7 +80,8 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)memcpy(&page.cursor, data + 1, sizeof(page.cursor));data += 9;size -= 9;- strscpy(v.upatch, "fuzz", sizeof(v.upatch));+ /* The longest name, which the decoder gets in its input. */+ memset(v.upatch, 'u', UPATCH_NAME_MAX - 1);strscpy(v.name, "fuzz_map", sizeof(v.name));page.data = xmalloc(size + 1);diff --git a/testing/helper/test-upatch-decode.c b/testing/helper/test-upatch-decode.cindex 0efd801f..2698a0e5 100644--- a/testing/helper/test-upatch-decode.c+++ b/testing/helper/test-upatch-decode.c@@ -43,18 +43,19 @@ int main(int argc, char **argv)if (argc == 3 && strcmp(argv[1], "dump") == 0)return dump(argv[2]);- if (argc != 3 || (strcmp(argv[1], "json") != 0 &&- strcmp(argv[1], "otel") != 0 &&- strcmp(argv[1], "loop") != 0 &&- strcmp(argv[1], "memory") != 0 &&- strcmp(argv[1], "utf8") != 0)) {+ if (argc < 3 || argc > 4 || (strcmp(argv[1], "json") != 0 &&+ strcmp(argv[1], "otel") != 0 &&+ strcmp(argv[1], "loop") != 0 &&+ strcmp(argv[1], "memory") != 0 &&+ strcmp(argv[1], "utf8") != 0)) {fprintf(stderr,- "usage: %s <json|otel|loop|memory|utf8> <file.upatch>\n"+ "usage: %s <json|otel|loop|memory|utf8> <file.upatch> [<upatch>]\n"" %s dump <decoder.lua>\n",argv[0], argv[0]);return 2;}- snprintf(view.upatch, sizeof(view.upatch), "%s", "t3002");+ snprintf(view.upatch, sizeof(view.upatch), "%s",+ argc == 4 ? argv[3] : "t3002");snprintf(view.name, sizeof(view.name), "%s", "t0005_map");if (strcmp(argv[1], "memory") == 0)view.serial_bytes_total = 997;diff --git a/testing/unit/upatch_decode.c b/testing/unit/upatch_decode.cindex d10a4731..fbe9a91a 100644--- a/testing/unit/upatch_decode.c+++ b/testing/unit/upatch_decode.c@@ -30,7 +30,12 @@ struct decoder_fixture {struct upatch_signed_trailer trailer;};-static void decode(const char *source, int expected, const char *json)+/*+ * Runs source as the decoder of map records of upatch, from an artifact+ * whose decoder entry names decoder_upatch.+ */+static void decode_named(const char *upatch, const char *decoder_upatch,+ const char *source, int expected, const char *json){const size_t data_off = offsetof(struct decoder_fixture, blob);char path[] = "/tmp/shirudo-unit-decoder-XXXXXX";@@ -38,7 +43,6 @@ static void decode(const char *source, int expected, const char *json)size_t source_len = strlen(source);struct decoder_fixture f = {};struct map_view view = {- .upatch = "unit-decoder",.name = "records",.serialized = true,.max_entries = 1,@@ -48,6 +52,7 @@ static void decode(const char *source, int expected, const char *json)char *out = NULL;int fd, ret;+ ASSERT_TRUE(strscpy(view.upatch, upatch, sizeof(view.upatch)) >= 0);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);@@ -68,7 +73,7 @@ static void decode(const char *source, int expected, const char *json)f.decoder.api_version = cpu_to_le32(1);f.decoder.input_format = cpu_to_le32(UPATCH_DECODER_INPUT_MAP_V1);f.decoder.export_signal = cpu_to_le32(UPATCH_DECODER_EXPORT_LOG);- ASSERT_TRUE(strscpy(f.decoder.upatch, view.upatch,+ ASSERT_TRUE(strscpy(f.decoder.upatch, decoder_upatch,sizeof(f.decoder.upatch)) >= 0);ASSERT_TRUE(strscpy(f.decoder.map, view.name, sizeof(f.decoder.map)) >= 0);ASSERT_TRUE(strscpy(f.decoder.source, "unit.lua", sizeof(f.decoder.source)) >= 0);@@ -99,12 +104,32 @@ static void decode(const char *source, int expected, const char *json)xfree(out, out_len + 1);}+static void decode(const char *source, int expected, const char *json)+{+ decode_named("unit-decoder", "unit-decoder", source, expected, json);+}+static void decoder_initializes_and_returns_json(void){decode("function decode(input) return {value = 42} end",0, "{\"value\":42}");}+/*+ * A name of 255 characters, which the decoder entry cannot hold and+ * leaves out: the map alone picks the decoder, which gets the name whole.+ */+static void decoder_of_a_upatch_with_the_longest_name_runs(void)+{+ char name[UPATCH_NAME_MAX];++ memset(name, 'n', sizeof(name) - 1);+ name[sizeof(name) - 1] = '\0';+ decode_named(name, "", "function decode(input) "+ "return {len = #input.upatch} end",+ 0, "{\"len\":255}");+}+static void decoder_instruction_limit_is_enforced(void){decode("function decode(input) while true do end end", -EINVAL, NULL);@@ -176,6 +201,7 @@ static void decoder_error_is_sanitized(void)static const struct unit_case cases[] = {UNIT_CASE(decoder_initializes_and_returns_json),+ UNIT_CASE(decoder_of_a_upatch_with_the_longest_name_runs),UNIT_CASE(decoder_instruction_limit_is_enforced),UNIT_CASE(decoder_memory_limit_is_enforced),UNIT_CASE(decoder_protected_loops_hit_the_limit),diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex d3505876..64221588 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -1217,7 +1217,7 @@ static bool parse_decoder_manifest(const __u8 *p, size_t n,if (get_le32(&m->magic) != DECODER_MANIFEST_MAGIC ||get_le16(&m->version) != UPATCH_DECODER_VERSION)return false;- if (!fixed_cstr_nonempty(m->upatch, sizeof(m->upatch)) ||+ if (!memchr(m->upatch, '\0', sizeof(m->upatch)) ||!fixed_cstr_nonempty(m->map, sizeof(m->map)) ||!fixed_cstr_nonempty(m->source, sizeof(m->source)) ||!fixed_cstr_nonempty(m->entrypoint, sizeof(m->entrypoint)))@@ -1308,10 +1308,15 @@ static int check_decoder_manifest(struct bpf_object *obj,(manifest->export_signal != UPATCH_DECODER_EXPORT_LOG &&manifest->export_signal != UPATCH_DECODER_EXPORT_METRIC)) {pr_err("unsupported decoder ABI for '%s/%s'",- manifest->upatch, manifest->map);+ upatch_name, manifest->map);return -EOPNOTSUPP;}- if (strcmp(manifest->upatch, upatch_name) != 0) {+ /*+ * The signature binds a decoder to the upatch whose data carries+ * it, so the name is optional: a upatch whose name the field cannot+ * hold leaves it empty. One given has to be this upatch's.+ */+ if (manifest->upatch[0] && strcmp(manifest->upatch, upatch_name) != 0) {pr_err("decoder upatch '%s' does not match package name '%s'",manifest->upatch, upatch_name);return -EINVAL;@@ -1322,12 +1327,9 @@ static int check_decoder_manifest(struct bpf_object *obj,return -EINVAL;}for (__u32 i = 0; i < set->nr_items; i++) {- if (strcmp(set->items[i].manifest.upatch,- manifest->upatch) == 0 &&- strcmp(set->items[i].manifest.map,- manifest->map) == 0) {+ if (strcmp(set->items[i].manifest.map, manifest->map) == 0) {pr_err("duplicate decoder for '%s/%s'",- manifest->upatch, manifest->map);+ upatch_name, manifest->map);return -EEXIST;}}diff --git a/util/upatch_decode.c b/util/upatch_decode.cindex b5bec451..45d8f15c 100644--- a/util/upatch_decode.c+++ b/util/upatch_decode.c@@ -1194,8 +1194,13 @@ static int decoder_matches(const struct upatch_decoder_view *d,(d->export_signal != UPATCH_DECODER_EXPORT_LOG &&d->export_signal != UPATCH_DECODER_EXPORT_METRIC))return 0;- if (strcmp(d->upatch, v->upatch) != 0)- return 0;+ /*+ * The map alone picks the decoder: the archive it comes from is the+ * upatch these maps belong to, by the payload the pin directory+ * recorded, and its signature covers the decoders. The decoder's+ * upatch field holds 63 of the 255 characters a name may have, or+ * nothing.+ */if (strcmp(d->map, v->name) != 0)return 0;return 1;