Skip to content

eBPF-based security platform

upatch: check a signed name and derive a program name in util/upatch.c

Daniel Borkmannd3d7610e63b8
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 7720f365..35b0a8c3 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -140,10 +140,12 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
per label and a terminated name and absolute path; a refusal
writes nothing
* - ``upatch-name``
- - ``upatch_name_valid()``, ``upatch_select()``
+ - ``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;
- an archive entry exactly for a name and ``.upatch``
+ an archive entry exactly for a name and ``.upatch``; a program
+ name zero-padded, of the header's bytes or ``_`` in their place
* - ``upatch-prog-specs``
- ``prog_specs_parse()``, ``prog_specs_free()``
- Tables of 3, 15, 16 and 176 bytes, as many programs as fit, every
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 6626d090..c84e793b 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1793,15 +1793,6 @@ static int __upatch_apply(const char *path, bool verify_only,
ret = 1;
goto out;
}
- if (u.hdr.loader_insns_len % sizeof(struct bpf_insn)) {
- pr_err("upatch load: loader_insns_len=%u not a multiple of %zu",
- u.hdr.loader_insns_len, sizeof(struct bpf_insn));
- rep_fail(rep, "parse", "parser_rejected", 0,
- "loader_insns_len misaligned");
- ret = 1;
- goto out;
- }
-
if (trust_anchor_keyring(&keyring, &serial, rep)) {
ret = 1;
goto out;
@@ -1812,22 +1803,7 @@ static int __upatch_apply(const char *path, bool verify_only,
data = upatch_data(&u);
insn_cnt = u.hdr.loader_insns_len / sizeof(struct bpf_insn);
- /* bpf_obj_name_cpy() takes alphanumerics, '_' and '.' alone. */
- memset(prog_name, 0, sizeof(prog_name));
- {
- size_t i, n = strnlen(u.hdr.name, sizeof(u.hdr.name));
-
- if (n > sizeof(prog_name) - 1)
- n = sizeof(prog_name) - 1;
- for (i = 0; i < n; i++) {
- char c = u.hdr.name[i];
- int ok = (c >= 'a' && c <= 'z') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') ||
- c == '_' || c == '.';
- prog_name[i] = ok ? c : '_';
- }
- }
+ upatch_prog_name(&u, prog_name, sizeof(prog_name));
/*
* The pin directory, the archive and unload share the raw name. Boot
@@ -1843,9 +1819,7 @@ static int __upatch_apply(const char *path, bool verify_only,
goto out;
}
/* The header's copy of the name is what everything else reads. */
- if (!upatch_signed_name(&u) ||
- memcmp(upatch_signed_name(&u), u.hdr.name,
- UPATCH_NAME_MAX)) {
+ if (!upatch_name_signed(&u)) {
pr_err("upatch: the name of '%s' is not signature-covered or "
"differs from the signed one; rebuild the artifact",
upatch_name);
@@ -3007,9 +2981,7 @@ static int cmd_upatch_unload(int argc, const char **argv)
* directive's signed sections; the header's name must be the same,
* since status and the package tooling read it.
*/
- if (!upatch_signed_name(&u) || !upatch_signed_unload_payload(&u) ||
- memcmp(upatch_signed_name(&u), u.hdr.name,
- UPATCH_NAME_MAX)) {
+ if (!upatch_name_signed(&u) || !upatch_signed_unload_payload(&u)) {
pr_err("upatch unload: the target of %s is not "
"signature-covered or differs from the header; rebuild "
"the directive", rep.path);
diff --git a/testing/proofs/cbmc/upatch-name.c b/testing/proofs/cbmc/upatch-name.c
index b3429dfb..b79244a5 100644
--- a/testing/proofs/cbmc/upatch-name.c
+++ b/testing/proofs/cbmc/upatch-name.c
@@ -4,8 +4,10 @@
*
* 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 "..", and
- * upatch_select() exactly the archive names, something then ".upatch".
+ * not starting with a dot, 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 '_'.
*/
#include "proof.h"
@@ -56,8 +58,36 @@ static void archive_name(void)
"exactly a name before .upatch");
}
+static bool prog_char(char c)
+{
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') || c == '_' || c == '.';
+}
+
+static void prog_name(void)
+{
+ size_t n, len = 0, i;
+ struct upatch u;
+ char out[16];
+
+ __CPROVER_assume(n <= LEN_MAX && !u.hdr.name[n]);
+ while (u.hdr.name[len])
+ len++;
+ upatch_prog_name(&u, out, sizeof(out));
+ for (i = 0; i < sizeof(out); i++) {
+ char c = u.hdr.name[i];
+
+ if (i < len && i < sizeof(out) - 1)
+ __CPROVER_assert(out[i] == (prog_char(c) ? c : '_'),
+ "the header's byte, or '_'");
+ else
+ __CPROVER_assert(!out[i], "zero-padded");
+ }
+}
+
void harness(void)
{
+ prog_name();
name_valid();
archive_name();
}
diff --git a/testing/unit/upatch.c b/testing/unit/upatch.c
index c743426e..e9c16094 100644
--- a/testing/unit/upatch.c
+++ b/testing/unit/upatch.c
@@ -66,10 +66,39 @@ static void header_name_is_terminated(void)
EXPECT_EQ(strlen(out), UPATCH_NAME_MAX);
}
+static void prog_name_is_what_the_kernel_takes(void)
+{
+ char out[16];
+ struct upatch u = {};
+
+ memcpy(u.hdr.name, "fix-cve.2026", 12);
+ upatch_prog_name(&u, out, sizeof(out));
+ EXPECT_STREQ(out, "fix_cve.2026");
+ memset(u.hdr.name, 'a', sizeof(u.hdr.name));
+ upatch_prog_name(&u, out, sizeof(out));
+ EXPECT_EQ(strlen(out), 15);
+}
+
+static void name_signed_is_the_header_name(void)
+{
+ char signed_name[UPATCH_NAME_MAX] = "fix";
+ struct upatch u = {};
+
+ memcpy(u.hdr.name, "fix", 3);
+ u.map = (const __u8 *)signed_name;
+ EXPECT_FALSE(upatch_name_signed(&u));
+ u.signed_name = signed_name;
+ EXPECT_TRUE(upatch_name_signed(&u));
+ u.hdr.name[2] = 'y';
+ EXPECT_FALSE(upatch_name_signed(&u));
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(prog_specs_take_the_signed_counts),
UNIT_CASE(prog_specs_refuse_another_header_count),
UNIT_CASE(header_name_is_terminated),
+ UNIT_CASE(prog_name_is_what_the_kernel_takes),
+ UNIT_CASE(name_signed_is_the_header_name),
{}
};
diff --git a/util/upatch.c b/util/upatch.c
index d2f55d40..6694ec48 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -419,6 +419,31 @@ void upatch_header_name(const struct upatch *u, char out[UPATCH_NAME_MAX + 1])
out[UPATCH_NAME_MAX] = '\0';
}
+bool upatch_name_signed(const struct upatch *u)
+{
+ const char *name = upatch_signed_name(u);
+
+ return name && !memcmp(name, u->hdr.name, UPATCH_NAME_MAX);
+}
+
+static bool prog_name_char(char c)
+{
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') || c == '_' || c == '.';
+}
+
+void upatch_prog_name(const struct upatch *u, char *out, size_t size)
+{
+ size_t i, n = strnlen(u->hdr.name, sizeof(u->hdr.name));
+
+ if (!size)
+ return;
+ memset(out, 0, size);
+ n = min(n, size - 1);
+ for (i = 0; i < n; i++)
+ out[i] = prog_name_char(u->hdr.name[i]) ? u->hdr.name[i] : '_';
+}
+
const char *upatch_signed_name(const struct upatch *u)
{
return u->map ? u->signed_name : NULL;
diff --git a/util/upatch.h b/util/upatch.h
index d07d1932..cc70dcb7 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -110,6 +110,17 @@ const char *upatch_signed_name(const struct upatch *u);
/* The name in the header, which the file need not terminate, with a NUL. */
void upatch_header_name(const struct upatch *u, char out[UPATCH_NAME_MAX + 1]);
+/*
+ * Whether the header's name is the one the signed sections carry: the
+ * header's is what the pins, the archive and the eventlog go by.
+ */
+bool upatch_name_signed(const struct upatch *u);
+/*
+ * The name the loader program takes, zero-padded to size: at most
+ * size - 1 bytes of the header's, with every byte but those
+ * bpf_obj_name_cpy() takes, an alphanumeric, '_' or '.', made '_'.
+ */
+void upatch_prog_name(const struct upatch *u, char *out, size_t size);
const void *upatch_signed_attach(const struct upatch *u,
__u32 *len);
const __u8 *upatch_signed_unload_payload(const struct upatch *u);