eBPF-based security platform
upatch: attach kprobe.multi and kretprobe.multi programs
Jiri Olsaf708037557b4
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex db6f30fb..064f4e79 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -655,13 +655,17 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.reads back each role, kind, target, offset and map; a refusalleaves nothing written and nothing allocated* - ``upatch-writer-kprobes``- - The kprobe entries of ``build_prog_metadata()``- - 16 objects from a table: a symbol of 1 to 127 bytes, alone or with- a decimal or hexadecimal offset of up to 64 bits, and the sections- the writer must refuse, a symbol of 128 bytes and an offset behind- ``kretprobe`` among them- - The symbol and offset read back are the section's; what the reader- refuses, or would cut short, is refused+ - The kprobe entries and the kprobe multi target block of+ ``build_prog_metadata()``+ - 23 objects from a table: a symbol of 1 to 127 bytes, alone or with+ a decimal or hexadecimal offset of up to 64 bits; a kprobe multi+ list of one name or two out of order, alone, beside another list+ or behind a uprobe; and the sections the writer must refuse, a+ symbol of 128 bytes, an offset behind ``kretprobe``, a glob, a name+ given twice and an empty name among them+ - The symbol and offset read back are the section's, and a list's+ names its own, sorted; a uprobe and a list in one object both read+ back; what the reader refuses, or would cut short, is refused* - ``upatch-writer-xdp``- The XDP entries of ``build_prog_metadata()``- ``xdp/`` with a first digit of 0 to 4, 8 or 9 and any 3 bytesdiff --git a/Documentation/upatches/write/memory-consistency/hazards.rst b/Documentation/upatches/write/memory-consistency/hazards.rstindex b7e89f20..7d7821b0 100644--- a/Documentation/upatches/write/memory-consistency/hazards.rst+++ b/Documentation/upatches/write/memory-consistency/hazards.rst@@ -56,16 +56,17 @@ riscv64 offer differ in one thing that matters here, preemptible RCU,- Kernels before 7.0 only, where no task preempted a runningprogram. The kernels shirudo needs have neither.-``kprobe``, ``kretprobe`` and ``xdp`` programs run with preemption or-bottom halves disabled on every model, ``syscall`` programs with only-migration disabled on any kernel, and a sleepable program can sleep. On-every kernel an interrupt can run a softirq on top of a run in a-process, and an interrupt or an NMI can run other programs on top of any-run. Besides, the kernel skips a second run of some program types on-the CPU while one runs. The table gives, for each attach kind ``shirudo-build`` accepts (:doc:`/upatches/write/attach-kinds` lists them),-whether the kernel skips such a nested run and whether a run of another-task can come in between on the CPU under each model:+``kprobe``, ``kretprobe``, ``kprobe.multi``, ``kretprobe.multi`` and+``xdp`` programs run with preemption or bottom halves disabled on every+model, ``syscall`` programs with only migration disabled on any kernel,+and a sleepable program can sleep. On every kernel an interrupt can run+a softirq on top of a run in a process, and an interrupt or an NMI can+run other programs on top of any run. Besides, the kernel skips a second+run of some program types on the CPU while one runs. The table gives,+for each attach kind ``shirudo build`` accepts+(:doc:`/upatches/write/attach-kinds` lists them), whether the kernel+skips such a nested run and whether a run of another task can come in+between on the CPU under each model:.. list-table:::header-rows: 1@@ -76,9 +77,9 @@ task can come in between on the CPU under each model:- Another task between, without preemptible RCU- With preemptible RCU- On ``PREEMPT_RT``- * - ``kprobe``, ``kretprobe``- - Yes, while any ``kprobe`` or ``kretprobe`` program runs on the- CPU+ * - ``kprobe``, ``kretprobe``, ``kprobe.multi``,+ ``kretprobe.multi``+ - Yes, while any program of these four kinds runs on the CPU- No- No- Nodiff --git a/Documentation/upatches/write/pitfalls.rst b/Documentation/upatches/write/pitfalls.rstindex f30bcb0a..8113867a 100644--- a/Documentation/upatches/write/pitfalls.rst+++ b/Documentation/upatches/write/pitfalls.rst@@ -21,12 +21,19 @@ Runs the kernel skipsThe kernel skips some runs of a program and counts them only in theprogram's ``recursion_misses``, which ``struct bpf_prog_info`` reports:-- A ``kprobe`` or ``kretprobe`` run, while another ``kprobe`` or- ``kretprobe`` program runs on the CPU, and while the CPU is in a- ``bpf()`` call that looks up, updates or deletes a map element from- userspace, as shirudo makes when it reads a upatch's maps. A- ``kprobe`` hit inside the handler of another ``kprobe`` is missed by- the kprobe itself.+- A ``kprobe``, ``kretprobe``, ``kprobe.multi`` or ``kretprobe.multi``+ run, while another program of these four kinds runs on the CPU, and+ while the CPU is in a ``bpf()`` call that looks up, updates or deletes+ a map element from userspace, as shirudo makes when it reads a+ upatch's maps. A ``kprobe`` hit inside the handler of another+ ``kprobe`` is missed by the kprobe itself.+- A ``kprobe.multi`` or ``kretprobe.multi`` hit while ftrace already+ traces a function in the same context on the CPU, such as a watched+ function the program calls itself, and the return of a function a+ ``kretprobe.multi`` watches when the task's return stack is full. The+ kernel counts neither. A return it has no room to keep the probe's+ data for is counted in the link's ``kprobe_multi.missed``, which+ ``struct bpf_link_info`` reports, not in ``recursion_misses``.- An ``fentry``, ``fexit``, ``fmod_ret`` or ``tp_btf`` run of a programthat is already running on the CPU: one a preempted task left therewith preemptible RCU, or one an NMI interrupted.diff --git a/builtin-upatch.c b/builtin-upatch.cindex 53aadcbe..e8eaf0ed 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -167,6 +167,7 @@ static const char *cat_from_attach_err(int err)return "attach_scope_mismatch";case -ENOENT:case -ENOTDIR:+ case -ESRCH:return "attach_target_missing";case -ESTALE:return "binary_mismatch";@@ -1217,6 +1218,10 @@ static const char *attach_kind_name(__u32 kind)return "uprobe.multi";case UPATCH_ATTACH_XDP:return "xdp";+ case UPATCH_ATTACH_KPROBE_MULTI:+ return "kprobe.multi";+ case UPATCH_ATTACH_KRETPROBE_MULTI:+ return "kretprobe.multi";case UPATCH_ATTACH_URETPROBE_MULTI:return "uretprobe.multi";case UPATCH_ATTACH_UPROBE:@@ -1337,6 +1342,23 @@ static int perf_event_open_uprobe(const struct prog_spec *spec)spec->kind == UPATCH_ATTACH_URETPROBE);}+static int prog_attach_kprobe_multi(int prog_fd, const struct prog_spec *spec)+{+ union bpf_attr attr = {};+ long fd;++ if (!spec->kprobe_syms || !spec->nr_kprobe_syms)+ return -EINVAL;+ attr.link_create.prog_fd = (__u32)prog_fd;+ attr.link_create.attach_type = BPF_TRACE_KPROBE_MULTI;+ attr.link_create.kprobe_multi.syms = ptr_to_u64(spec->kprobe_syms);+ attr.link_create.kprobe_multi.cnt = spec->nr_kprobe_syms;+ if (spec->kind == UPATCH_ATTACH_KRETPROBE_MULTI)+ attr.link_create.kprobe_multi.flags = BPF_F_KPROBE_MULTI_RETURN;+ fd = syscall(__NR_bpf, BPF_LINK_CREATE, &attr, sizeof(attr));+ return fd >= 0 ? (int)fd : -errno;+}+static int prog_attach_for_spec(int prog_fd,const struct prog_spec *spec){@@ -1358,6 +1380,9 @@ static int prog_attach_for_spec(int prog_fd,case UPATCH_ATTACH_UPROBE_MULTI:case UPATCH_ATTACH_URETPROBE_MULTI:return prog_attach_uprobe_multi(prog_fd, spec);+ case UPATCH_ATTACH_KPROBE_MULTI:+ case UPATCH_ATTACH_KRETPROBE_MULTI:+ return prog_attach_kprobe_multi(prog_fd, spec);case UPATCH_ATTACH_KPROBE:case UPATCH_ATTACH_KRETPROBE:perf_fd = perf_event_open_probe("kprobe", spec->target, spec->offset,@@ -1458,6 +1483,14 @@ static int link_matches_spec(int link_fd,!!(info.uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN) != ret)return -EXDEV;return 0;+ case UPATCH_ATTACH_KPROBE_MULTI:+ case UPATCH_ATTACH_KRETPROBE_MULTI:+ ret = spec->kind == UPATCH_ATTACH_KRETPROBE_MULTI;+ if (info.type != BPF_LINK_TYPE_KPROBE_MULTI ||+ info.kprobe_multi.count != spec->nr_kprobe_syms ||+ !!(info.kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN) != ret)+ return -EXDEV;+ return 0;case UPATCH_ATTACH_FMOD_RET:if (info.type != BPF_LINK_TYPE_TRACING ||info.tracing.attach_type != BPF_MODIFY_RETURN)@@ -1775,6 +1808,10 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,attach_kind_name(spec->kind),spec->uprobe_path, spec->target,strerror(-link_fd));+ else if (kind_is_kprobe_multi(spec->kind))+ pr_err("attach prog %u (%s, %u symbols): %s", i,+ attach_kind_name(spec->kind), spec->nr_kprobe_syms,+ strerror(-link_fd));else if (spec->target[0])pr_err("attach prog %u (%s %s): %s",i, attach_kind_name(spec->kind),@@ -1789,10 +1826,15 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,/* Unpinned, a closed link detaches. */ret = link_matches_spec(link_fd, spec, 0);if (ret) {- pr_err("attach prog %u (%s %s): the link is not at "- "the site the metadata names: %s", i,- attach_kind_name(spec->kind), spec->target,- strerror(-ret));+ if (kind_is_kprobe_multi(spec->kind))+ pr_err("attach prog %u (%s, %u symbols): the link "+ "is not at the site the metadata names: %s",+ i, attach_kind_name(spec->kind),+ spec->nr_kprobe_syms, strerror(-ret));+ else+ pr_err("attach prog %u (%s %s): the link is not at "+ "the site the metadata names: %s", i,+ attach_kind_name(spec->kind), spec->target, strerror(-ret));close(link_fd);goto out;}diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 0706e431..ba4371bd 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -109,7 +109,7 @@ upatch-writer-sections --memory-leak-check --max-field-sensitivity-array-size 51upatch-writer-load --max-field-sensitivity-array-size 8192 --unwind 2 --unwindset compose_signed_data.0:5,compose_signed_data.1:5,parse_signed_sections.0:5,parse_decoders.0:2,parse_policy_labels.0:2 LEN=1upatch-writer-decoders --memory-leak-check --max-field-sensitivity-array-size 512 --unwind 3 --unwindset strnlen.0:65,memchr.0:65,copy_bytes.0:65,viewed.0:4 LEN=0-2upatch-writer-prog-specs --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-21-upatch-writer-kprobes --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-15+upatch-writer-kprobes --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65,strpbrk.0:5 LEN=0-22upatch-writer-xdp --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-8upatch-writer-uprobes --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-21vouch-payload --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memcmp.0:33,harness.0:5 LEN=0-2diff --git a/testing/proofs/cbmc/upatch-writer-kprobes.c b/testing/proofs/cbmc/upatch-writer-kprobes.cindex ded59a14..86c3fef2 100644--- a/testing/proofs/cbmc/upatch-writer-kprobes.c+++ b/testing/proofs/cbmc/upatch-writer-kprobes.c@@ -2,9 +2,10 @@/** Copyright (c) 2026 Authors of shirudo*- * The attach metadata proof for kprobe and kretprobe sections: a symbol of- * up to 127 bytes and an offset of up to 64 bits, decimal or hexadecimal,- * read back as written, and the sections the writer must refuse.+ * The attach metadata proof for kprobe sections: a symbol of up to 127+ * bytes and an offset of up to 64 bits read back as written, kprobe multi+ * name lists read back sorted, beside a uprobe or another list, and the+ * sections the writer must refuse.*/#define KPROBE_OBJECTSdiff --git a/testing/proofs/cbmc/upatch-writer-prog-specs.c b/testing/proofs/cbmc/upatch-writer-prog-specs.cindex a941cc12..69c52e45 100644--- a/testing/proofs/cbmc/upatch-writer-prog-specs.c+++ b/testing/proofs/cbmc/upatch-writer-prog-specs.c@@ -38,7 +38,8 @@ enum { TAKEN, EITHER, REFUSED };* A program: its section and name, any TAIL bytes behind the one or the* other where tail says so, whether the writer takes it, and what the* reader then gives. A uprobe's binary has a build ID of build_id_len- * bytes, and the resolver finds it unless no_binary is set.+ * bytes, and the resolver finds it unless no_binary is set. A kprobe multi+ * program reads back syms, its names sorted and joined by ','.*/struct shape {const char *sec;@@ -51,6 +52,7 @@ struct shape {__u64 offset;__u32 build_id_len;bool no_binary;+ const char *syms;};#define PROG(sec, verdict, kind, ...) \@@ -80,6 +82,8 @@ struct shape {PROG(sec, TAKEN, kind, target, 0, build_id_len)#define NO_BINARY(sec, kind) \PROG(sec, REFUSED, kind, "f", 0, 20, true)+#define KMULTI(sec, kind, syms) \+ PROG(sec, TAKEN, kind, "", 0, 0, false, syms)static const struct {__u32 nr_progs;@@ -100,7 +104,16 @@ static const struct {{ 1, { REFUSE("kprobe/+1") } },{ 1, { REFUSE("kprobe/f+") } },{ 1, { REFUSE("kprobe/f+07") } },- { 1, { REFUSE("kprobe.multi/f") } },+ { 1, { KMULTI("kprobe.multi/f", KPROBE_MULTI, "f") } },+ { 1, { KMULTI("kretprobe.multi/g,f", KRETPROBE_MULTI, "f,g") } },+ { 2, { KMULTI("kprobe.multi/f", KPROBE_MULTI, "f"),+ KMULTI("kretprobe.multi/g", KRETPROBE_MULTI, "g") } },+ { 2, { UPROBE(AT("uprobe", "f"), UPROBE, "f", 0, 20),+ KMULTI("kprobe.multi/g", KPROBE_MULTI, "g") } },+ { 1, { REFUSE("kprobe.multi/f*") } },+ { 1, { REFUSE("kprobe.multi/f,f") } },+ { 1, { REFUSE("kprobe.multi/f,") } },+ { 1, { REFUSE("kprobe.multi/") } },{ 1, { REFUSE("kprobe") } },{ 1, { REFUSE("kprobe/") } },{ 1, { REFUSE("kretprobe") } },@@ -256,6 +269,31 @@ void pr_err(const char *fmt, ...){}+/* A body for strpbrk(), which proof.h does not give, as it does strnlen(). */+char *strpbrk(const char *s, const char *accept)+{+ for (; *s; s++)+ if (strchr(accept, *s))+ return (char *)s;+ return NULL;+}++/* CBMC models no qsort(); the writer sorts at most two names here. */+void qsort(void *base, size_t nmemb, size_t size,+ int (*compare)(const void *, const void *))+{+ char **names = base;+ char *first;++ __CPROVER_assert(nmemb <= 2 && size == sizeof(*names),+ "at most two names to sort");+ if (nmemb == 2 && compare(&names[0], &names[1]) > 0) {+ first = names[0];+ names[0] = names[1];+ names[1] = first;+ }+}+char *strerror(int err){return "error";@@ -344,21 +382,29 @@ static void text(char *dst, const char *prefix, bool tail)dst[n] = '\0';}-/* The table of the format: a header, the entries, the uprobe targets. */+/*+ * The table of the format: a header, the entries, the uprobe targets and+ * the kprobe multi targets.+ */static __u32 table_len(void){- __u32 len = 16 + 160 * NR_PROGS, nr_uprobes = 0, i;+ __u32 len = 16 + 160 * NR_PROGS, nr_uprobes = 0, nr_kprobes = 0, i;for (i = 0; i < NR_PROGS; i++) {const struct shape *shape = &objects[LEN].progs[i];+ if (kind_is_kprobe_multi(shape->kind)) {+ len += 12 + strlen(shape->syms) + 1;+ nr_kprobes++;+ continue;+ }if (!kind_has_uprobe_target(shape->kind))continue;len += 16 + strlen(UPROBE_PATH) + shape->build_id_len +8 * (kind_is_uprobe_multi(shape->kind) ? NR_OFFSETS : 1);nr_uprobes++;}- return len + (nr_uprobes ? 16 : 0);+ return len + (nr_uprobes ? 16 : 0) + (nr_kprobes ? 16 : 0);}static void uprobe_target(const struct prog_spec *s,@@ -384,6 +430,23 @@ static void uprobe_target(const struct prog_spec *s,"a function below 4 GiB");}+/* The names of syms, in its order, each NUL-terminated in the record. */+static void kprobe_syms(const struct prog_spec *s, const char *syms)+{+ const char *want = syms;+ __u32 i;++ for (i = 0; i < s->nr_kprobe_syms; i++) {+ size_t len = strlen(s->kprobe_syms[i]);++ __CPROVER_assert(*want && !strncmp(s->kprobe_syms[i], want, len) &&+ (want[len] == ',' || !want[len]),+ "the names the section lists, sorted");+ want += len + (want[len] == ',');+ }+ __CPROVER_assert(s->nr_kprobe_syms && !*want, "every name the section lists");+}+static __u32 decimal(const char *digits){__u32 value = 0;@@ -413,6 +476,8 @@ static void read_back(const struct prog_spec *s, const char *sec,"the target and offset the section names");if (kind_has_uprobe_target(s->kind))uprobe_target(s, shape, nth_uprobe);+ if (kind_is_kprobe_multi(s->kind))+ kprobe_syms(s, shape->syms);}void harness(void)diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 758a772b..a41b78ef 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -582,10 +582,31 @@ int build_upatch_cli(int argc, const char **argv)return ret ? 1 : 0;}+static const char *kprobe_multi_section_target(const char *sec, __u32 *kind)+{+ size_t n;++ n = str_has_prefix(sec, "kprobe.multi/");+ if (n && sec[n]) {+ *kind = UPATCH_ATTACH_KPROBE_MULTI;+ return sec + n;+ }+ n = str_has_prefix(sec, "kretprobe.multi/");+ if (n && sec[n]) {+ *kind = UPATCH_ATTACH_KRETPROBE_MULTI;+ return sec + n;+ }+ return NULL;+}+static bool unsupported_kprobe_section(const char *sec){+ __u32 kind;+if (sec && sec[0] == '?')sec++;+ if (sec && kprobe_multi_section_target(sec, &kind))+ return false;return sec &&(strcmp(sec, "kprobe") == 0 ||strcmp(sec, "kretprobe") == 0 ||@@ -735,6 +756,9 @@ static __u32 attach_kind_from_section(const char *sec,return UPATCH_ATTACH_KRETPROBE;}*target = uprobe_section_target(sec, &kind);+ if (*target)+ return kind;+ *target = kprobe_multi_section_target(sec, &kind);if (*target)return kind;if (strcmp(sec, "cgroup_skb/ingress") == 0)@@ -958,6 +982,137 @@ static __u8 *write_uprobe_targets(__u8 *cursor, const struct build_uprobe_targetreturn cursor;}+struct build_kprobe_target {+ __u32 prog_idx;+ char *buf;+ size_t buf_len;+ char **syms;+ __u32 nr_syms;+};++static void free_kprobe_targets(struct build_kprobe_target *targets,+ __u32 nr_targets, __u32 nr_progs)+{+ __u32 i;++ if (!targets)+ return;+ for (i = 0; i < nr_targets; i++) {+ xfree(targets[i].buf, targets[i].buf_len);+ xfree(targets[i].syms, (size_t)targets[i].nr_syms * sizeof(*targets[i].syms));+ }+ xfree(targets, (size_t)nr_progs * sizeof(*targets));+}++static int sym_cmp(const void *a, const void *b)+{+ return strcmp(*(char * const *)a, *(char * const *)b);+}++/*+ * Splits "<name>[,<name>...]" into sorted names. The buffer keeps them in+ * the order given; it is as long as the signed record, which+ * write_kprobe_targets() fills in sorted order. On failure kt owns+ * nothing: -EOPNOTSUPP for a glob, -EEXIST for a name given twice, -E2BIG+ * for too many names, -EINVAL for an invalid one.+ */+static int split_kprobe_multi_target(const char *list, struct build_kprobe_target *kt)+{+ size_t list_len = strlen(list), buf_len;+ __u32 nr_syms = 1, i;+ const char *comma;+ char **syms;+ char *buf, *name;+ int ret = -EINVAL;++ if (strpbrk(list, "*?"))+ return -EOPNOTSUPP;+ for (comma = strchr(list, ','); comma; comma = strchr(comma + 1, ','))+ if (++nr_syms > UPATCH_KPROBE_SYMS_MAX)+ return -E2BIG;+ if (check_add_overflow(list_len, 1, &buf_len) || buf_len > UINT32_MAX)+ return -E2BIG;+ buf = xstrndup(list, list_len);+ if (!buf)+ return -ENOMEM;+ syms = xcalloc(nr_syms, sizeof(*syms));+ if (!syms) {+ ret = -ENOMEM;+ goto err_buf;+ }+ name = buf;+ for (i = 0; i < nr_syms; i++) {+ char *end = strchr(name, ',');+ size_t len = end ? (size_t)(end - name) : strlen(name);++ if (!kprobe_sym_valid(name, len))+ goto err_syms;+ name[len] = '\0';+ syms[i] = name;+ name += len + 1;+ }+ qsort(syms, nr_syms, sizeof(*syms), sym_cmp);+ for (i = 1; i < nr_syms; i++) {+ if (!strcmp(syms[i - 1], syms[i])) {+ ret = -EEXIST;+ goto err_syms;+ }+ }+ kt->buf = buf;+ kt->buf_len = buf_len;+ kt->syms = syms;+ kt->nr_syms = nr_syms;+ return 0;+err_syms:+ xfree(syms, (size_t)nr_syms * sizeof(*syms));+err_buf:+ xfree(buf, buf_len);+ return ret;+}++static size_t kprobe_targets_block_len(const struct build_kprobe_target *targets, __u32 nr_targets)+{+ size_t len = sizeof(struct upatch_kprobe_targets);+ __u32 i;++ for (i = 0; i < nr_targets; i++)+ len = size_add(len, size_add(sizeof(struct upatch_kprobe_target),+ targets[i].buf_len));+ return len;+}++static __u8 *write_kprobe_targets(__u8 *cursor, const struct build_kprobe_target *targets,+ __u32 nr_targets)+{+ struct upatch_kprobe_targets header = {+ .magic = cpu_to_le32(UPATCH_KPROBE_TARGET_MAGIC),+ .version = cpu_to_le32(UPATCH_KPROBE_TARGET_VERSION),+ .nr_targets = cpu_to_le32(nr_targets),+ };+ __u32 i, j;++ memcpy(cursor, &header, sizeof(header));+ cursor += sizeof(header);+ for (i = 0; i < nr_targets; i++) {+ const struct build_kprobe_target *kt = &targets[i];+ struct upatch_kprobe_target record = {+ .prog_idx = cpu_to_le32(kt->prog_idx),+ .nr_syms = cpu_to_le32(kt->nr_syms),+ .syms_len = cpu_to_le32((__u32)kt->buf_len),+ };++ memcpy(cursor, &record, sizeof(record));+ cursor += sizeof(record);+ for (j = 0; j < kt->nr_syms; j++) {+ size_t sym_size = strlen(kt->syms[j]) + 1;++ memcpy(cursor, kt->syms[j], sym_size);+ cursor += sym_size;+ }+ }+ return cursor;+}+static bool is_syscall_section(const char *sec){return sec && strcmp(sec, "syscall") == 0;@@ -1372,11 +1527,13 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,__u32 nr_progs, void **out, __u32 *out_len){struct build_uprobe_target *uprobe_targets = NULL;+ struct build_kprobe_target *kprobe_targets = NULL;char (*seen_maps)[UPATCH_PROG_MAP_MAX] = NULL;struct upatch_prog_metadata *md;struct bpf_program *p;+ __u32 i = 0, n_seen = 0, nr_uprobe_targets = 0, nr_kprobe_targets = 0;+ __u32 xdp_classes = 0;size_t len, base_len;- __u32 i = 0, n_seen = 0, nr_uprobe_targets = 0, xdp_classes = 0;bool has_serializer = false, has_test_run = false;int err;@@ -1414,6 +1571,11 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,err = -ENOMEM;goto err_free;}+ kprobe_targets = xcalloc(nr_progs, sizeof(*kprobe_targets));+ if (!kprobe_targets) {+ err = -ENOMEM;+ goto err_free;+ }bpf_object__for_each_program(p, obj) {const char *sec = bpf_program__section_name(p);@@ -1491,7 +1653,7 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,continue;}if (unsupported_kprobe_section(sec)) {- pr_err("unsupported kprobe section '%s'; use kprobe/<symbol> or kretprobe/<symbol>",+ pr_err("unsupported kprobe section '%s'; use kprobe/<symbol>, kretprobe/<symbol>, kprobe.multi/<symbol>[,<symbol>...] or kretprobe.multi/<symbol>[,<symbol>...]",sec);err = -EOPNOTSUPP;goto err_free;@@ -1529,7 +1691,7 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,kind = attach_kind_from_section(sec, &target);if (kind == UPATCH_ATTACH_RAW_TRACEPOINT &&!default_attach_section(sec)) {- pr_err("unsupported section '%s'; supported are fentry, fexit, fmod_ret, lsm, tp_btf, kprobe, kretprobe, uprobe[.s], uretprobe[.s], uprobe.multi[.s], uretprobe.multi[.s], cgroup_skb, netfilter and xdp/<mask>",+ pr_err("unsupported section '%s'; supported are fentry, fexit, fmod_ret, lsm, tp_btf, kprobe, kretprobe, kprobe.multi, kretprobe.multi, uprobe[.s], uretprobe[.s], uprobe.multi[.s], uretprobe.multi[.s], cgroup_skb, netfilter and xdp/<mask>",sec);err = -EOPNOTSUPP;goto err_free;@@ -1599,6 +1761,28 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,strerror(-err));goto err_free;}+ } else if (kind_is_kprobe_multi(kind)) {+ struct build_kprobe_target *kt = &kprobe_targets[nr_kprobe_targets];++ err = split_kprobe_multi_target(target, kt);+ if (err) {+ if (err == -ENOMEM)+ pr_err("kprobe multi section '%s': %s", sec,+ strerror(-err));+ else if (err == -EOPNOTSUPP)+ pr_err("invalid kprobe multi section '%s': globs are unsupported; name each function",+ sec);+ else if (err == -EEXIST)+ pr_err("invalid kprobe multi section '%s': a function is named twice",+ sec);+ else+ pr_err("invalid kprobe multi section '%s'; expected a comma-separated list of 1 to %u function names of at most %u bytes",+ sec, UPATCH_KPROBE_SYMS_MAX,+ UPATCH_KPROBE_SYM_MAX - 1);+ goto err_free;+ }+ kt->prog_idx = i;+ nr_kprobe_targets++;} else if (target) {err = split_probe_target(target, md->progs[i].target,sizeof(md->progs[i].target),@@ -1620,33 +1804,51 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,}len = base_len;- if (nr_uprobe_targets) {+ if (nr_uprobe_targets &&+ check_add_overflow(len, uprobe_targets_block_len(uprobe_targets, nr_uprobe_targets),+ &len)) {+ err = -EOVERFLOW;+ goto err_free;+ }+ if (nr_kprobe_targets &&+ check_add_overflow(len, kprobe_targets_block_len(kprobe_targets, nr_kprobe_targets),+ &len)) {+ err = -EOVERFLOW;+ goto err_free;+ }+ if (len > UINT32_MAX) {+ err = -EOVERFLOW;+ goto err_free;+ }+ if (len != base_len) {+ __u8 *cursor;void *grown;- if (check_add_overflow(len,- uprobe_targets_block_len(uprobe_targets, nr_uprobe_targets),- &len) || len > UINT32_MAX) {- err = -EOVERFLOW;- goto err_free;- }grown = xreallocarray(md, base_len, len, 1);if (!grown) {err = -ENOMEM;goto err_free;}md = grown;- write_uprobe_targets((__u8 *)md + base_len, uprobe_targets, nr_uprobe_targets);+ cursor = (__u8 *)md + base_len;+ /* prog_specs_parse() reads the uprobe block first. */+ if (nr_uprobe_targets)+ cursor = write_uprobe_targets(cursor, uprobe_targets, nr_uprobe_targets);+ if (nr_kprobe_targets)+ write_kprobe_targets(cursor, kprobe_targets, nr_kprobe_targets);}*out = md;*out_len = (__u32)len;xfree(seen_maps, (size_t)nr_progs * sizeof(*seen_maps));free_uprobe_targets(uprobe_targets, nr_uprobe_targets, nr_progs);+ free_kprobe_targets(kprobe_targets, nr_kprobe_targets, nr_progs);return 0;err_free:xfree(seen_maps, (size_t)nr_progs * sizeof(*seen_maps));free_uprobe_targets(uprobe_targets, nr_uprobe_targets, nr_progs);+ free_kprobe_targets(kprobe_targets, nr_kprobe_targets, nr_progs);xfree(md, base_len);return err;}diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex d72d98de..5fa088e9 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -242,6 +242,13 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,close(link_fd);return ret ? ret : -EINVAL;}+ if (linfo.type == BPF_LINK_TYPE_KPROBE_MULTI) {+ close(link_fd);+ return strsprintf(out, cap, "%s (%u funcs)",+ linfo.kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN ?+ "kretprobe.multi" : "kprobe.multi",+ linfo.kprobe_multi.count) < 0 ? -E2BIG : 0;+ }if (linfo.type == BPF_LINK_TYPE_UPROBE_MULTI) {struct bpf_link_info detail = {};__u32 count = linfo.uprobe_multi.count;