Skip to content

eBPF-based security platform

upatch: parse kprobe multi targets in the signed attach metadata

Jiri Olsa67357862dfcf
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 4d083084..db6f30fb 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -554,8 +554,9 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
allocation allowed to fail
- A refusal leaves nothing allocated and ``*out`` NULL; an accepted
program has a known role and kind, a terminated target and map,
- uprobe fields only on a uprobe, and device classes, set and
- within the known mask, only on XDP
+ uprobe fields only on a uprobe, kprobe names only on a kprobe
+ multi program, and device classes, set and within the known
+ mask, only on XDP
* - ``upatch-prog-uprobes``
- What ``prog_specs_parse()`` makes of a uprobe target block
- One program and one record in 226 bytes, the table's program
@@ -574,6 +575,12 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
absolute path of its stated length without a NUL, one offset for a
single uprobe, a build ID of 1 to 64 bytes, offsets non-empty and
strictly rising, everything freed
+ * - ``upatch-prog-kprobes``
+ - The kprobe multi target records of ``prog_specs_parse()``
+ - One program and one record in 206 to 210 bytes, 2 to 6 bytes of
+ names
+ - At least one name, each terminated inside the record's buffer,
+ the names strictly rising, everything freed
* - ``upatch-loader``
- ``loader_lddw()``, ``sys_bpf_call()``, ``map_create_slot()``,
``map_update_slot()``, ``loader_store_source()``
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 36d2c0a8..dcb3948a 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -1001,7 +1001,8 @@ test makes the two differ, which takes runtime authority.
:Proofs: ``upatch-header``, ``upatch-signed-sections``,
``upatch-signed-name``, ``upatch-name``, ``upatch-prog-specs``,
``upatch-prog-uprobes``, ``upatch-uprobe-targets``,
- ``upatch-decoders``, ``upatch-policy-labels``
+ ``upatch-prog-kprobes``, ``upatch-decoders``,
+ ``upatch-policy-labels``
:Residual: R1, R16
.. _tm-t5:
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index d49d5e3f..c770178c 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -190,6 +190,10 @@ struct upatch_decoder_blob {
#define UPATCH_UPROBE_PATH_MAX 4096u
#define UPATCH_BUILD_ID_MAX 64u
#define UPATCH_UPROBE_OFFSETS_MAX (1u << 20)
+#define UPATCH_KPROBE_TARGET_MAGIC 0x534b5055u /* "UPKS" le */
+#define UPATCH_KPROBE_TARGET_VERSION 1u
+#define UPATCH_KPROBE_SYM_MAX 512u /* KSYM_NAME_LEN */
+#define UPATCH_KPROBE_SYMS_MAX (1u << 20) /* MAX_KPROBE_MULTI_CNT */
enum upatch_attach_kind {
UPATCH_ATTACH_RAW_TRACEPOINT = 0,
@@ -204,6 +208,8 @@ enum upatch_attach_kind {
UPATCH_ATTACH_URETPROBE_MULTI = 9,
UPATCH_ATTACH_UPROBE = 10,
UPATCH_ATTACH_URETPROBE = 11,
+ UPATCH_ATTACH_KPROBE_MULTI = 12,
+ UPATCH_ATTACH_KRETPROBE_MULTI = 13,
};
/* Bit 4 is not a class: loopback has no native XDP, so nothing selects it. */
@@ -268,5 +274,24 @@ struct upatch_uprobe_target {
__le32 nr_offsets;
};
+/*
+ * One UPKS block follows the UPUT block, or the fixed entries when there
+ * is none. It is required when any program has a kprobe multi attach kind
+ * and absent otherwise. Each record is followed by syms_len bytes holding
+ * nr_syms NUL-terminated names in strictly ascending strcmp() order.
+ */
+struct upatch_kprobe_targets {
+ __le32 magic; /* UPATCH_KPROBE_TARGET_MAGIC */
+ __le32 version; /* UPATCH_KPROBE_TARGET_VERSION */
+ __le32 nr_targets;
+ __le32 reserved;
+};
+
+struct upatch_kprobe_target {
+ __le32 prog_idx;
+ __le32 nr_syms;
+ __le32 syms_len;
+};
+
#endif /* SHIRUDO_UPATCH_H */
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index c45883c9..0706e431 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -16,9 +16,10 @@ upatch-policy-labels --unwind 3 LEN=0,19,20,4187,4188,4189,8356
upatch-name --unwind 33
upatch-signed-sections --refine-arithmetic --unwind 2 --unwindset parse_signed_sections.0:4,parse_decoders.0:0,parse_policy_labels.0:0 LEN=15,16,40,64
upatch-signed-name --refine-arithmetic --unwind 2 --unwindset parse_signed_sections.0:2,parse_decoders.0:2,parse_policy_labels.0:0 LEN=288
-upatch-prog-specs --refine-arithmetic --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memchr.0:129,memcmp.0:129,strchr.0:129,strnlen.0:1,strlen.0:1,parse_uprobe_targets.0:0,parse_uprobe_targets.1:0,uprobe_valid.0:1 LEN=3,15,16,176
+upatch-prog-specs --refine-arithmetic --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memchr.0:129,memcmp.0:129,strchr.0:129,strnlen.0:1,strlen.0:1,parse_uprobe_targets.0:0,parse_uprobe_targets.1:0,parse_kprobe_targets.0:0,parse_kprobe_syms.0:0,uprobe_valid.0:1,kprobe_multi_valid.0:1,strcmp.0:1 LEN=3,15,16,176
upatch-prog-uprobes --refine-arithmetic --max-field-sensitivity-array-size 226 --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 2 --unwindset memchr.0:129,memcmp.0:129,strchr.0:129,strnlen.0:3,strlen.0:3,parse_uprobe_targets.0:3 LEN=226
upatch-uprobe-targets --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 2 --unwindset parse_uprobe_targets.0:3,memchr.0:10,strnlen.0:11,strlen.0:11,memcmp.0:129 LEN=50
+upatch-prog-kprobes --refine-arithmetic --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memchr.0:129,memcmp.0:129,strchr.0:129,strnlen.0:12,strlen.0:12,parse_uprobe_targets.0:1,parse_uprobe_targets.1:0,uprobe_valid.0:1,parse_kprobe_targets.0:2,parse_kprobe_syms.0:4,kprobe_sym_valid.0:7,kprobe_multi_valid.0:4,strcmp.0:7 LEN=206-210
upatch-loader --unwind 50 LEN=4,5,25,49
upatch-rodata-btf --refine-arithmetic --object-bits 10 --unwind 16 LEN=4,10,15
upatch-btf --unwind 6 --unwindset btf_decl_printable.0:33,btf_name_printable.0:7,btf_unwrap.0:9
diff --git a/testing/proofs/cbmc/upatch-prog-kprobes.c b/testing/proofs/cbmc/upatch-prog-kprobes.c
new file mode 100644
index 00000000..6fe3711e
--- /dev/null
+++ b/testing/proofs/cbmc/upatch-prog-kprobes.c
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The attach metadata proof for metadata long enough to hold a kprobe
+ * multi target block, which proofs.list runs with the bounds its records
+ * need.
+ */
+
+#include "upatch-prog-specs.c"
diff --git a/testing/proofs/cbmc/upatch-prog-specs.c b/testing/proofs/cbmc/upatch-prog-specs.c
index d3ce22f7..26bb1d7f 100644
--- a/testing/proofs/cbmc/upatch-prog-specs.c
+++ b/testing/proofs/cbmc/upatch-prog-specs.c
@@ -6,8 +6,9 @@
* length proofs.list names, with as many programs as it has room for:
* prog_specs_parse() reads nothing outside it, gives every program a known
* role and kind with terminated names, every uprobe an absolute path and
- * rising offsets, every XDP program known device classes, and frees all it
- * took on refusal, as prog_specs_free() does.
+ * rising offsets, every kprobe multi program terminated, strictly rising
+ * names in its buffer, every XDP program known device classes, and frees
+ * all it took on refusal, as prog_specs_free() does.
*/
#include "proof.h"
@@ -56,14 +57,33 @@ static void uprobe_valid(const struct prog_spec *s)
s->uprobe_offsets[j], "rising offsets");
}
+static void kprobe_multi_valid(const struct prog_spec *s)
+{
+ __u32 i;
+
+ __CPROVER_assert(s->nr_kprobe_syms && s->kprobe_syms &&
+ s->nr_kprobe_syms <= UPATCH_KPROBE_SYMS_MAX, "names within the limit");
+ for (i = 0; i < s->nr_kprobe_syms; i++) {
+ size_t off = (size_t)(s->kprobe_syms[i] - s->kprobe_sym_buf);
+
+ __CPROVER_assert(off < s->kprobe_sym_buf_len &&
+ terminated(s->kprobe_syms[i], s->kprobe_sym_buf_len - off),
+ "a terminated name in the buffer");
+ __CPROVER_assert(!i || strcmp(s->kprobe_syms[i - 1], s->kprobe_syms[i]) < 0,
+ "rising names");
+ }
+}
+
static void attach_valid(const struct prog_spec *s)
{
__CPROVER_assert(!s->map[0], "no map");
- __CPROVER_assert(s->kind <= UPATCH_ATTACH_URETPROBE, "a known attach kind");
+ __CPROVER_assert(s->kind <= UPATCH_ATTACH_KRETPROBE_MULTI, "a known attach kind");
if (s->kind == UPATCH_ATTACH_XDP)
__CPROVER_assert(s->device_classes &&
!(s->device_classes & ~(__u32)UPATCH_XDP_ALL),
"known device classes");
+ if (kind_is_kprobe_multi(s->kind))
+ kprobe_multi_valid(s);
if (!needs_target(s->kind)) {
__CPROVER_assert(!s->target[0] && !s->offset,
"no target or offset");
@@ -88,6 +108,9 @@ static void spec_valid(const struct prog_spec *s)
if (s->kind != UPATCH_ATTACH_XDP)
__CPROVER_assert(!s->device_classes,
"device classes for XDP alone");
+ if (!kind_is_kprobe_multi(s->kind))
+ __CPROVER_assert(!s->kprobe_syms && !s->kprobe_sym_buf,
+ "kprobe names for kprobe multi alone");
switch (s->role) {
case UPATCH_PROG_ROLE_ATTACH:
attach_valid(s);
diff --git a/util/upatch.c b/util/upatch.c
index ee91956f..276f0bd5 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -585,6 +585,9 @@ void prog_specs_free(struct prog_spec *specs, __u32 nr_progs)
xfree(specs[i].uprobe_offsets,
(size_t)specs[i].nr_uprobe_offsets *
sizeof(*specs[i].uprobe_offsets));
+ xfree(specs[i].kprobe_sym_buf, specs[i].kprobe_sym_buf_len);
+ xfree(specs[i].kprobe_syms, (size_t)specs[i].nr_kprobe_syms *
+ sizeof(*specs[i].kprobe_syms));
}
xfree(specs, (size_t)nr_progs * sizeof(*specs));
}
@@ -599,6 +602,21 @@ __u32 prog_specs_xdp_classes(const struct prog_spec *specs, __u32 nr_progs)
return classes;
}
+bool kprobe_sym_valid(const char *sym, size_t len)
+{
+ size_t i;
+
+ if (!len || len >= UPATCH_KPROBE_SYM_MAX)
+ return false;
+ for (i = 0; i < len; i++) {
+ __u8 c = (__u8)sym[i];
+
+ if (c <= ' ' || c >= 0x7f || c == ',' || c == '*' || c == '?' || c == '|')
+ return false;
+ }
+ return true;
+}
+
static int parse_uprobe_targets(const __u8 **pos, const __u8 *end, struct prog_spec *specs,
__u32 nr_progs, __u32 nr_uprobes)
{
@@ -669,10 +687,88 @@ static int parse_uprobe_targets(const __u8 **pos, const __u8 *end, struct prog_s
return 0;
}
+/* The caller's prog_specs_free() releases what a failure leaves in spec. */
+static int parse_kprobe_syms(const __u8 *syms, __u32 syms_len, __u32 nr_syms,
+ struct prog_spec *spec)
+{
+ size_t off = 0;
+ __u32 i;
+
+ spec->kprobe_sym_buf = xmalloc(syms_len);
+ if (!spec->kprobe_sym_buf)
+ return -ENOMEM;
+ spec->kprobe_sym_buf_len = syms_len;
+ memcpy(spec->kprobe_sym_buf, syms, syms_len);
+ spec->kprobe_syms = xcalloc(nr_syms, sizeof(*spec->kprobe_syms));
+ if (!spec->kprobe_syms)
+ return -ENOMEM;
+ spec->nr_kprobe_syms = nr_syms;
+
+ for (i = 0; i < nr_syms; i++) {
+ const char *name = spec->kprobe_sym_buf + off;
+ const char *nul;
+
+ if (off >= syms_len)
+ return -EINVAL;
+ nul = memchr(name, '\0', syms_len - off);
+ if (!nul || !kprobe_sym_valid(name, (size_t)(nul - name)))
+ return -EINVAL;
+ if (i && strcmp(spec->kprobe_syms[i - 1], name) >= 0)
+ return -EINVAL;
+ spec->kprobe_syms[i] = name;
+ off += (size_t)(nul - name) + 1;
+ }
+ return off == syms_len ? 0 : -EINVAL;
+}
+
+static int parse_kprobe_targets(const __u8 **pos, const __u8 *end, struct prog_spec *specs,
+ __u32 nr_progs, __u32 nr_kprobe_multis)
+{
+ struct upatch_kprobe_targets targets;
+ const __u8 *cursor = *pos;
+ __u32 i;
+
+ if ((size_t)(end - cursor) < sizeof(targets))
+ return -EINVAL;
+ memcpy(&targets, cursor, sizeof(targets));
+ cursor += sizeof(targets);
+ if (le32_to_cpu(targets.magic) != UPATCH_KPROBE_TARGET_MAGIC ||
+ le32_to_cpu(targets.version) != UPATCH_KPROBE_TARGET_VERSION ||
+ le32_to_cpu(targets.reserved) != 0 ||
+ le32_to_cpu(targets.nr_targets) != nr_kprobe_multis)
+ return -EINVAL;
+
+ for (i = 0; i < nr_kprobe_multis; i++) {
+ struct upatch_kprobe_target record;
+ __u32 prog_idx, nr_syms, syms_len;
+ int ret;
+
+ if ((size_t)(end - cursor) < sizeof(record))
+ return -EINVAL;
+ memcpy(&record, cursor, sizeof(record));
+ cursor += sizeof(record);
+ prog_idx = le32_to_cpu(record.prog_idx);
+ nr_syms = le32_to_cpu(record.nr_syms);
+ syms_len = le32_to_cpu(record.syms_len);
+ if (prog_idx >= nr_progs || !kind_is_kprobe_multi(specs[prog_idx].kind) ||
+ specs[prog_idx].kprobe_syms || nr_syms == 0 ||
+ nr_syms > UPATCH_KPROBE_SYMS_MAX || syms_len / 2 < nr_syms ||
+ (size_t)(end - cursor) < syms_len)
+ return -EINVAL;
+ ret = parse_kprobe_syms(cursor, syms_len, nr_syms, &specs[prog_idx]);
+ if (ret)
+ return ret;
+ cursor += syms_len;
+ }
+ *pos = cursor;
+ return 0;
+}
+
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, xdp_classes = 0;
+ __u32 magic, version, count, i, nr_uprobes = 0, nr_kprobe_multis = 0;
+ __u32 xdp_classes = 0;
const struct upatch_prog_entry empty = {};
struct upatch_prog_metadata md;
struct prog_spec *specs = NULL;
@@ -784,6 +880,13 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,
xdp_classes |= spec->device_classes;
spec->kind = kind;
continue;
+ case UPATCH_ATTACH_KPROBE_MULTI:
+ case UPATCH_ATTACH_KRETPROBE_MULTI:
+ spec->kind = kind;
+ if (prog.target[0] || prog.offset)
+ goto out;
+ nr_kprobe_multis++;
+ continue;
default:
goto out;
}
@@ -809,6 +912,11 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,
if (ret)
goto out;
}
+ if (nr_kprobe_multis) {
+ ret = parse_kprobe_targets(&cursor, end, specs, nr_progs, nr_kprobe_multis);
+ if (ret)
+ goto out;
+ }
ret = -EINVAL;
if (cursor != end)
goto out;
diff --git a/util/upatch.h b/util/upatch.h
index a830d2a4..94bb673c 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -158,6 +158,10 @@ struct prog_spec {
__u32 build_id_len;
__u64 *uprobe_offsets;
__u32 nr_uprobe_offsets;
+ char *kprobe_sym_buf;
+ size_t kprobe_sym_buf_len;
+ const char **kprobe_syms;
+ __u32 nr_kprobe_syms;
int target_fd;
struct stat target_stat;
};
@@ -178,6 +182,18 @@ static inline bool kind_has_uprobe_target(__u32 kind)
return kind_is_uprobe_multi(kind) || kind_is_uprobe_single(kind);
}
+static inline bool kind_is_kprobe_multi(__u32 kind)
+{
+ return kind == UPATCH_ATTACH_KPROBE_MULTI || kind == UPATCH_ATTACH_KRETPROBE_MULTI;
+}
+
+/*
+ * Whether the len bytes at sym can be one name of a kprobe multi target:
+ * 1 to UPATCH_KPROBE_SYM_MAX - 1 printable ASCII bytes, none of them a
+ * space or one of the list and glob characters ",*?|".
+ */
+bool kprobe_sym_valid(const char *sym, size_t len);
+
/*
* The attach metadata of a load upatch, parsed strictly into one spec
* per program, target_fd -1. 0 or -errno; free with prog_specs_free().