eBPF-based security platform
upatch: refuse a name whose pin directory would not fit
Daniel Borkmann5ae8ef6e3a6c
diff --git a/Documentation/concepts/upatches-and-packages.rst b/Documentation/concepts/upatches-and-packages.rstindex 49f6de2c..ade555bc 100644--- a/Documentation/concepts/upatches-and-packages.rst+++ b/Documentation/concepts/upatches-and-packages.rst@@ -26,7 +26,8 @@ attaches. The target loads, pins, archives and measures it as a whole.Every upatch has a name, by default the base name of its source file. Aname consists of letters, digits, ``.``, ``_`` and ``-``, does not start-with a dot, and has at most 255 characters. It names the pin directory+with a dot, and has at most 255 characters, each dot counting as two,+because the pin directory spells it ``~d``. It names the pin directoryunder ``/run/shirudo/bpffs/upatches/``, the archived copy``/var/lib/shirudo/upatches/<name>.upatch`` that ``init`` restores at boot,and the events in the measurement register.diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 5d4b1966..c21b1688 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -243,7 +243,8 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- ``upatch_name_valid()``, ``upatch_select()``,``upatch_prog_name()``- Names up to 24 bytes, archive names up to 31- - Valid exactly for ``[A-Za-z0-9._-]+`` not starting with a dot;+ - Valid exactly for ``[A-Za-z0-9._-]+`` not starting with a dot+ whose pin directory name, each dot two bytes, fits ``NAME_MAX``;an archive entry exactly for a name and ``.upatch``; a programname zero-padded, of the header's bytes or ``_`` in their place* - ``upatch-prog-specs``diff --git a/Documentation/operate/plan/scalability.rst b/Documentation/operate/plan/scalability.rstindex 52128f05..1288ec5d 100644--- a/Documentation/operate/plan/scalability.rst+++ b/Documentation/operate/plan/scalability.rst@@ -237,9 +237,10 @@ One upatch- 4 GiB (``kernel/bpf/arena.c``)* - Name- 255 characters from ``A-Z``, ``a-z``, ``0-9``, ``.``, ``_`` and- ``-``, 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 ``.``. 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`).* - Map export- :doc:`/upatches/write/maps-arenas-serializers` and:doc:`/upatches/write/decoders` give the serializer and decoder limits.diff --git a/Documentation/reference/cli/attest-predict.rst b/Documentation/reference/cli/attest-predict.rstindex f2469870..fa04d2c9 100644--- a/Documentation/reference/cli/attest-predict.rst+++ b/Documentation/reference/cli/attest-predict.rst@@ -116,8 +116,8 @@ Options* - ``--expect``- ``<name>=<digest>``- An archived upatch by name and payload digest. Repeatable. The- name is one an upatch can have, ``[A-Za-z0-9._-]`` not starting- with a dot, and comes once.+ name is one an upatch can have, see+ :doc:`/concepts/upatches-and-packages`, and comes once.* - ``--tree-root``- ``<digest>``, 64 hex characters- The upatch tree projection root at boot. Default: all zero.diff --git a/Documentation/reference/cli/attest-verify.rst b/Documentation/reference/cli/attest-verify.rstindex e2b26328..a5038913 100644--- a/Documentation/reference/cli/attest-verify.rst+++ b/Documentation/reference/cli/attest-verify.rst@@ -104,8 +104,9 @@ Options* - ``--expect``- ``<name>=<digest>``- An archived upatch by name and payload digest. Repeatable. The- name is one an upatch can have, ``[A-Za-z0-9._-]`` not starting- with a dot, and comes once. Needs ``--cert``.+ name is one an upatch can have, see+ :doc:`/concepts/upatches-and-packages`, and comes once. Needs+ ``--cert``.* - ``--tree-root``- ``<digest>``, 64 hex characters- The upatch tree projection root at boot. Needs ``--cert``.diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rstindex 2b5969bc..25e45c24 100644--- a/Documentation/reference/formats/bpm-formats.rst+++ b/Documentation/reference/formats/bpm-formats.rst@@ -99,7 +99,8 @@ Names* - upatch name- The source file's basename without ``.bpf.c``, ``.bpf.o`` or ``.o``- The Beefile cannot override it. 1 to 255 characters of- ``[A-Za-z0-9._-]``, not starting with ``.``.+ ``[A-Za-z0-9._-]``, each ``.`` counting as two, not starting with+ ``.``.* - Tree operation ``id``- Beefile ``[upatches.tree] id``- Must equal the upatch name of the artifact it publishes, so thediff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex e30c0786..02c504bd 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -89,7 +89,8 @@ Header- 256- ``name``- The upatch name, NUL-padded. Writers accept 1 to 255 characters of- ``[A-Za-z0-9._-]`` that do not start with ``.``. For an unload+ ``[A-Za-z0-9._-]``, each ``.`` counting as two, that do not start+ with ``.``. For an unloaddirective, the name of the upatch to unload.* - 272- 32@@ -736,8 +737,9 @@ of checks 1 to 6 is reported as ``parser_rejected``, see#. ``flags`` bit 0 is clear.#. ``loader_insns_len`` is a multiple of 8.-#. The header ``name`` is not empty, does not start with ``.`` and- contains only ``[A-Za-z0-9._-]``.+#. The header ``name`` is not empty, does not start with ``.``, contains+ only ``[A-Za-z0-9._-]`` and has at most 255 characters with each ``.``+ counted twice, so that its pin directory name fits.#. A ``NAME`` section is present and equals the header ``name``.#. ``payload_sha256`` equals SHA-256 of ``loader_insns || data``.#. A ``UPGM`` section is present and parses: version 1 (any other version@@ -813,7 +815,8 @@ Limits* - Signed sections- 1 to 8* - Name of an upatch- - 255 characters of ``[A-Za-z0-9._-]``, not starting with ``.``+ - 255 characters of ``[A-Za-z0-9._-]``, each ``.`` counting as two,+ not starting with ``.``* - ``kprobe``, ``kretprobe`` or ``uprobe.multi`` target- 127 bytes* - Serializer map namediff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rstindex 94337a27..da5de776 100644--- a/Documentation/upatches/learn/concepts.rst+++ b/Documentation/upatches/learn/concepts.rst@@ -192,7 +192,8 @@ file, the eventlog record and the target of its unload directive.- Rule* - upatch name- 1 to 255 characters of ``A-Z``, ``a-z``, ``0-9``, ``.``, ``_`` and- ``-``, not starting with ``.``. The builder refuses anything else,+ ``-``, each ``.`` counting as two, not starting with ``.``. The+ builder refuses anything else,and the target refuses it again at load.* - name from ``shirudo build``- The input's base name without ``.bpf.c``, ``.bpf.o`` or ``.o``.diff --git a/testing/functional/upatch/t3011-string-bounds.sh b/testing/functional/upatch/t3011-string-bounds.shindex 9230cef3..e030a599 100755--- a/testing/functional/upatch/t3011-string-bounds.sh+++ b/testing/functional/upatch/t3011-string-bounds.sh@@ -54,6 +54,18 @@ test_expect_success 'an upatch name the header cannot hold is not cut and signed--keys-dir "$WORK/keys" >/dev/null 2>&1'+test_expect_success 'an upatch name whose pin directory would not fit is not signed' '+ dotted=n$(printf ".n%.0s" $(seq 127)) &&+ ! "$B" --unload "$dotted" -o "$WORK/dotted.upatch" \+ --payload-sha256 "$PAYLOAD" --keys-dir "$WORK/keys" \+ >/dev/null 2>"$WORK/dotted.err" &&+ grep -q "each .\.. counting as two" "$WORK/dotted.err" &&+ [ ! -e "$WORK/dotted.upatch" ] &&+ fits=n$(printf ".n%.0s" $(seq 84)) &&+ "$B" --unload "$fits" -o "$WORK/fits.upatch" --payload-sha256 "$PAYLOAD" \+ --keys-dir "$WORK/keys" >/dev/null 2>&1+'+test_expect_success 'a signed name section without its terminator is no name' '"$B" --unload good -o "$WORK/good.upatch" --payload-sha256 "$PAYLOAD" \--keys-dir "$WORK/keys" >/dev/null 2>&1 &&diff --git a/testing/proofs/cbmc/upatch-name.c b/testing/proofs/cbmc/upatch-name.cindex b79244a5..1da1fa5f 100644--- a/testing/proofs/cbmc/upatch-name.c+++ b/testing/proofs/cbmc/upatch-name.c@@ -4,7 +4,8 @@** The upatch names util/upatch.c takes from outside, for every name of up* to LEN_MAX bytes: upatch_name_valid() accepts exactly [A-Za-z0-9._-]+- * not starting with a dot, so no path separator, "." or "..",+ * not starting with a dot whose pin directory, each dot two bytes, fits+ * NAME_MAX, so no path separator, "." or "..",* upatch_select() exactly the archive names, something then ".upatch",* and upatch_prog_name() gives the loader program a zero-padded name the* kernel takes, the header's with every other byte made '_'.@@ -24,18 +25,21 @@ static bool name_char(char c)static void name_valid(void){+ size_t n, i, pin = 0;bool valid, chars = true;- size_t n, i;char *name;__CPROVER_assume(n <= LEN_MAX);name = any_bytes(n + 1);__CPROVER_assume(!name[n] && strnlen(name, n) == n);valid = upatch_name_valid(name);- for (i = 0; i < n; i++)+ for (i = 0; i < n; i++) {chars &= name_char(name[i]);- __CPROVER_assert(valid == (n > 0 && name[0] != '.' && chars),- "exactly the names of the pattern");+ pin += name[i] == '.' ? 2 : 1;+ }+ __CPROVER_assert(valid == (n > 0 && name[0] != '.' && chars &&+ pin <= NAME_MAX),+ "exactly the names of the pattern whose pin fits");for (i = 0; i < n; i++)__CPROVER_assert(!valid || name[i] != '/', "no separator");__CPROVER_assert(!upatch_name_valid(NULL), "no name is invalid");diff --git a/testing/proofs/lean4/Shirudo/Events.lean b/testing/proofs/lean4/Shirudo/Events.leanindex 4eda7b94..d088467a 100644--- a/testing/proofs/lean4/Shirudo/Events.lean+++ b/testing/proofs/lean4/Shirudo/Events.lean@@ -149,11 +149,20 @@ def nameChar (c : UInt8) : Bool :=(97 ≤ c && c ≤ 122) || (65 ≤ c && c ≤ 90) || (48 ≤ c && c ≤ 57) ||c == 46 || c == 95 || c == 45+/-- `NAME_MAX`, the longest name of a pin directory. -/+def pinMax : Nat := 255++/-- The length of `upatch_bpffs_name()` of util/paths.h, which spells each+dot in two bytes. -/+def pinLength (n : List UInt8) : Nat :=+ (n.map fun c => if c == 46 then 2 else 1).sum+/-- `upatch_name_valid()` of util/upatch.c: not empty, not starting with a-dot, and only bytes of `nameChar`. -/+dot, only bytes of `nameChar`, and a pin directory name that fits. -/def nameValid : List UInt8 → Bool| [] => false- | c :: cs => c != 46 && (c :: cs).all nameChar+ | c :: cs => c != 46 && (c :: cs).all nameChar &&+ decide (pinLength (c :: cs) ≤ pinMax)/-- The names that reach `chain_upatch_event()` and `chain_init_event()`on a target and in `attest verify`: what `upatch_name_valid()` accepts,@@ -184,7 +193,7 @@ theorem nameValid_mem {n : List UInt8} (h : nameValid n = true) :| nil => simp [nameValid] at h| cons c cs =>simp only [nameValid, Bool.and_eq_true, List.all_eq_true] at h- exact h.2+ exact h.1.2theorem nameChar_ne {c : UInt8} (h : nameChar c = true) : c ≠ 0 ∧ c ≠ 10 := byconstructor <;> (rintro rfl; exact absurd h (by decide))diff --git a/testing/unit/upatch.c b/testing/unit/upatch.cindex c1e68dcf..f21d3113 100644--- a/testing/unit/upatch.c+++ b/testing/unit/upatch.c@@ -2,6 +2,7 @@/* Copyright (c) 2026 Authors of shirudo */#include <errno.h>+#include <limits.h>#include <string.h>#include "unit.h"@@ -106,6 +107,24 @@ static void member_is_a_name_and_a_payload(void)EXPECT_FALSE(upatch_member_valid("fix", NULL));}+/* A name's pin directory spells each dot in two bytes, within NAME_MAX. */+static void name_fits_its_pin_directory(void)+{+ char name[NAME_MAX + 2];++ memset(name, 'a', NAME_MAX);+ name[NAME_MAX] = '\0';+ EXPECT_TRUE(upatch_name_valid(name));+ name[NAME_MAX - 1] = '.';+ EXPECT_FALSE(upatch_name_valid(name));+ name[NAME_MAX - 1] = '\0';+ name[NAME_MAX - 2] = '.';+ EXPECT_TRUE(upatch_name_valid(name));+ memset(name, 'a', NAME_MAX + 1);+ name[NAME_MAX + 1] = '\0';+ EXPECT_FALSE(upatch_name_valid(name));+}+static const struct unit_case cases[] = {UNIT_CASE(prog_specs_take_the_signed_counts),UNIT_CASE(prog_specs_refuse_another_header_count),@@ -113,6 +132,7 @@ static const struct unit_case cases[] = {UNIT_CASE(prog_name_is_what_the_kernel_takes),UNIT_CASE(name_signed_is_the_header_name),UNIT_CASE(member_is_a_name_and_a_payload),+ UNIT_CASE(name_fits_its_pin_directory),{}};diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex d947a63e..c6b1fe62 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -1752,18 +1752,13 @@ out:return err;}-/*- * Every target refuses a name outside the upatch character set, and a- * name the header cannot hold would be cut and signed as another one.- */+/* Every target refuses a name upatch_name_valid() does not take. */static int check_upatch_name(const char *name){- if (name && upatch_name_valid(name) &&- strlen(name) < UPATCH_NAME_MAX)+ if (upatch_name_valid(name))return 0;- pr_err("upatch name '%s' is not 1 to %d characters of [A-Za-z0-9._-] "- "not starting with '.'", name ? name : "",- UPATCH_NAME_MAX - 1);+ pr_err("upatch name '%s' is not 1 to %d characters of [A-Za-z0-9._-], each '.' counting as two, not starting with '.'",+ name ? name : "", NAME_MAX);return -EINVAL;}diff --git a/util/upatch.c b/util/upatch.cindex ea1f6596..6bbbd46d 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -7,6 +7,7 @@#include <errno.h>#include <fcntl.h>+#include <limits.h>#include <stddef.h>#include <stdint.h>#include <string.h>@@ -541,7 +542,7 @@ int upatch_decoder_view(const struct upatch *u, __u32 idx,bool upatch_name_valid(const char *name){- size_t i;+ size_t i, pin = 0;if (!name || name[0] == '\0' || name[0] == '.')return false;@@ -552,6 +553,9 @@ bool upatch_name_valid(const char *name)(c >= '0' && c <= '9') || c == '.' || c == '_' ||c == '-'))return false;+ pin += c == '.' ? 2 : 1;+ if (pin > NAME_MAX)+ return false;}return true;}diff --git a/util/upatch.h b/util/upatch.hindex dc5689dd..60e74cf5 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -136,9 +136,9 @@ const void *upatch_signed_policy_labels(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- * could escape the bpffs / on-disk path namespace.+ * An upatch name: [A-Za-z0-9._-]+ not starting with '.', so nothing that+ * leaves the bpffs or the archive directory, and short enough for its pin+ * directory, which spells each '.' in two bytes, to fit NAME_MAX.*/bool upatch_name_valid(const char *name);