Skip to content

eBPF-based security platform

upatch: attach standard uprobe and uretprobe programs

Jiri Olsae99a2351d803
diff --git a/.agents/skills/bpf-concurrency/SKILL.md b/.agents/skills/bpf-concurrency/SKILL.md
index 5b9b5941..71dac27e 100644
--- a/.agents/skills/bpf-concurrency/SKILL.md
+++ b/.agents/skills/bpf-concurrency/SKILL.md
@@ -56,8 +56,8 @@ reasoning; leave the plain form where only one guarded program, or only
The kernel skips `kprobe` and `kretprobe` runs while another runs on the
CPU or the CPU is in a `bpf()` map call, nested `fentry`, `fexit`,
`fmod_ret` and `tp_btf` runs, `kretprobe` returns beyond its
-instances and `uretprobe.multi` returns beyond the task's 64, so a
-detector can miss what it looks for. Refuse an
+instances and `uretprobe` and `uretprobe.multi` returns beyond the
+task's 64, so a detector can miss what it looks for. Refuse an
operation in an `lsm` program, never in `fmod_ret`: a skipped
`fmod_ret` run lets the call through, and a task can arrange the skip.
An `lsm` program does not see what an LSM before it refused, and its
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 37191a24..778bd660 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -182,8 +182,9 @@ locate_checkpatch() {
# form checkpatch reports in, and in userspace one that names unsigned
# char, short, int or long. Those stay where an interface fixes them: BPF
# programs spell hook arguments as the kernel's prototypes do,
-# util/elf_target.c hands libbpf the unsigned long ** it takes, and
-# util/sign.c is vendored. A call to strtoul() and its kin or atoi() is
+# util/elf_target.c passes a type required by libbpf, declared in
+# util/libbpf_internal.h; util/sign.c is vendored.
+# A call to strtoul() and its kin or atoi() is
# reported too, outside the tests: a number is parsed with parse_u64() and
# its kin from <shirudo/string.h>, which take a length, read nothing past
# it and refuse a sign, blank or base prefix. So is a string copied with
@@ -197,7 +198,8 @@ check_spellings() {
awk '
function c_names_kept(f) {
return f ~ /^(bpf|upatches)\// || f ~ /\.bpf\.c$/ ||
- f == "util/elf_target.c" || f == "util/sign.c"
+ f == "util/elf_target.c" ||
+ f == "util/libbpf_internal.h" || f == "util/sign.c"
}
function parsers_kept(f) {
return f ~ /^(bpf|upatches|testing)\// || f ~ /\.bpf\.c$/
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 7f9c0a59..200b4a5a 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -297,9 +297,10 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- The uprobe target records of ``prog_specs_parse()``
- One program and one record in 226 bytes, path and build ID up to
9 bytes, up to 2 offsets
- - An absolute path of its stated length without a NUL, a target
- without ``,`` or ``|``, a build ID of 1 to 64 bytes, offsets
- non-empty and strictly rising, everything freed
+ - An absolute path of its stated length without a NUL, a glob
+ without ``,`` or ``|``, one offset for a single uprobe, a build ID
+ of 1 to 64 bytes, offsets non-empty and strictly rising, everything
+ freed
* - ``upatch-loader``
- ``loader_lddw()``, ``sys_bpf_call()``, ``map_create_slot()``,
``map_update_slot()``, ``loader_store_source()``
diff --git a/Documentation/upatches/write/memory-consistency/hazards.rst b/Documentation/upatches/write/memory-consistency/hazards.rst
index 8af7d4d2..a4b283ab 100644
--- a/Documentation/upatches/write/memory-consistency/hazards.rst
+++ b/Documentation/upatches/write/memory-consistency/hazards.rst
@@ -49,7 +49,8 @@ program types against a second run and not others:
- Can be preempted with preemptible RCU. The kernel skips a run of a
program that is already running on the CPU, so a program does not
nest in itself, but other programs can run in between.
- * - ``lsm``, ``uprobe.multi``, ``uretprobe.multi``
+ * - ``lsm``, ``uprobe``, ``uretprobe``, ``uprobe.multi``,
+ ``uretprobe.multi``
- Can be preempted with preemptible RCU and are not guarded: a
second run of the same program can start on the CPU before the
first one ends, and an ``lsm`` hook a softirq calls can run on top
@@ -57,8 +58,8 @@ program types against a second run and not others:
* - ``syscall``
- Run with only migration disabled on any kernel and are not
guarded: another task can preempt a run.
- * - ``lsm.s``, ``fentry.s``, ``fexit.s``, ``fmod_ret.s``,
- ``uprobe.multi.s``, ``uretprobe.multi.s``
+ * - ``lsm.s``, ``fentry.s``, ``fexit.s``, ``fmod_ret.s``, ``uprobe.s``,
+ ``uretprobe.s``, ``uprobe.multi.s``, ``uretprobe.multi.s``
- Can be preempted and sleep on any kernel. Another run can take the
CPU at any sleeping call.
* - ``cgroup_skb``, ``netfilter``
diff --git a/Documentation/upatches/write/pitfalls.rst b/Documentation/upatches/write/pitfalls.rst
index 8a7fc5a9..e90a0ff9 100644
--- a/Documentation/upatches/write/pitfalls.rst
+++ b/Documentation/upatches/write/pitfalls.rst
@@ -33,11 +33,11 @@ program's ``recursion_misses``, which ``struct bpf_prog_info`` reports:
- The return of a function a ``kretprobe`` watches, when more calls of
it are in flight than the probe has instances for: the larger of 10
and twice the number of CPUs.
-- The return of a function a ``uretprobe.multi`` or ``uretprobe.multi.s``
- watches, when the task already waits on the returns of 64 calls that
- return probes watch, and the return of a function that leaves by
- ``longjmp()`` or ends the thread. The kernel logs the first case,
- rate-limited, and counts neither.
+- The return of a function a ``uretprobe``, a ``uretprobe.multi`` or
+ their ``.s`` forms watch, when the task already waits on the returns of
+ 64 calls that return probes watch, and the return of a function that
+ leaves by ``longjmp()`` or ends the thread. The kernel logs the first
+ case, rate-limited, and counts neither.
A detector built on these kinds can therefore miss the event it looks
for, and a count it keeps is a lower bound. ``lsm`` and ``xdp`` programs
diff --git a/builtin-upatch.c b/builtin-upatch.c
index d960b229..8f4c5741 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1069,6 +1069,10 @@ static const char *attach_kind_name(__u32 kind)
return "xdp";
case UPATCH_ATTACH_URETPROBE_MULTI:
return "uretprobe.multi";
+ case UPATCH_ATTACH_UPROBE:
+ return "uprobe";
+ case UPATCH_ATTACH_URETPROBE:
+ return "uretprobe";
default:
return "unknown";
}
@@ -1087,7 +1091,7 @@ static int prepare_uprobe_targets(struct prog_spec *specs,
struct stat opened_stat;
int path_fd, fd, ret;
- if (!kind_is_uprobe_multi(spec->kind))
+ if (!kind_has_uprobe_target(spec->kind))
continue;
path_fd = open(spec->uprobe_path, O_PATH | O_CLOEXEC);
if (path_fd < 0)
@@ -1164,6 +1168,23 @@ static int prog_attach_uprobe_multi(int prog_fd,
return fd >= 0 ? (int)fd : -errno;
}
+/*
+ * The kernel resolves the binary from a path, so name the descriptor
+ * prepare_uprobe_targets() checked: the probe lands in that inode, not in
+ * whatever spec->uprobe_path names by now.
+ */
+static int perf_event_open_uprobe(const struct prog_spec *spec)
+{
+ char path[64];
+
+ if (spec->target_fd < 0 || !spec->uprobe_offsets || spec->nr_uprobe_offsets != 1)
+ return -EINVAL;
+ if (strsprintf(path, sizeof(path), "/proc/self/fd/%d", spec->target_fd) < 0)
+ return -E2BIG;
+ return perf_event_open_probe("uprobe", path, spec->uprobe_offsets[0],
+ spec->kind == UPATCH_ATTACH_URETPROBE);
+}
+
static int prog_attach_for_spec(int prog_fd,
const struct prog_spec *spec)
{
@@ -1189,14 +1210,19 @@ static int prog_attach_for_spec(int prog_fd,
case UPATCH_ATTACH_KRETPROBE:
perf_fd = perf_event_open_probe("kprobe", spec->target, spec->offset,
spec->kind == UPATCH_ATTACH_KRETPROBE);
- if (perf_fd < 0)
- return perf_fd;
- link_fd = prog_attach_perf_event(prog_fd, perf_fd);
- close(perf_fd);
- return link_fd;
+ break;
+ case UPATCH_ATTACH_UPROBE:
+ case UPATCH_ATTACH_URETPROBE:
+ perf_fd = perf_event_open_uprobe(spec);
+ break;
default:
return -EINVAL;
}
+ if (perf_fd < 0)
+ return perf_fd;
+ link_fd = prog_attach_perf_event(prog_fd, perf_fd);
+ close(perf_fd);
+ return link_fd;
}
/* The root of the unified cgroup hierarchy. */
@@ -1211,7 +1237,9 @@ static int prog_attach_for_spec(int prog_fd,
* its own guards nothing, a kprobe opened through a sysfs that names
* another PMU lands elsewhere, and each is measured as loaded. What the
* kernel reports about the link is compared with the spec before the
- * link is pinned; -EXDEV when they differ.
+ * link is pinned; -EXDEV when they differ. A uprobe's file name is not:
+ * the descriptor it was opened through fixes the inode, and the name the
+ * kernel reports can differ from the signed path by a symlink.
*/
static int link_matches_spec(int link_fd,
const struct prog_spec *spec, __u32 ifindex)
@@ -1259,6 +1287,17 @@ static int link_matches_spec(int link_fd,
strcmp(func, spec->target))
return -EXDEV;
return 0;
+ case UPATCH_ATTACH_UPROBE:
+ case UPATCH_ATTACH_URETPROBE:
+ ret = spec->kind == UPATCH_ATTACH_URETPROBE;
+ if (info.type != BPF_LINK_TYPE_PERF_EVENT ||
+ info.perf_event.type != (ret ? BPF_PERF_EVENT_URETPROBE :
+ BPF_PERF_EVENT_UPROBE) ||
+ spec->nr_uprobe_offsets != 1 ||
+ info.perf_event.uprobe.offset != spec->uprobe_offsets[0] ||
+ info.perf_event.uprobe.ref_ctr_offset)
+ return -EXDEV;
+ return 0;
case UPATCH_ATTACH_UPROBE_MULTI:
case UPATCH_ATTACH_URETPROBE_MULTI:
ret = spec->kind == UPATCH_ATTACH_URETPROBE_MULTI;
@@ -1579,7 +1618,7 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,
link_fd = prog_attach_for_spec(inner_fd, spec);
if (link_fd < 0) {
- if (kind_is_uprobe_multi(spec->kind))
+ if (kind_has_uprobe_target(spec->kind))
pr_err("attach prog %u (%s %s:%s): %s", i,
attach_kind_name(spec->kind),
spec->uprobe_path, spec->target,
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index 83881ab0..d49d5e3f 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -202,6 +202,8 @@ enum upatch_attach_kind {
UPATCH_ATTACH_UPROBE_MULTI = 7,
UPATCH_ATTACH_XDP = 8,
UPATCH_ATTACH_URETPROBE_MULTI = 9,
+ UPATCH_ATTACH_UPROBE = 10,
+ UPATCH_ATTACH_URETPROBE = 11,
};
/* Bit 4 is not a class: loopback has no native XDP, so nothing selects it. */
@@ -223,13 +225,13 @@ struct upatch_prog_entry {
__le32 role; /* enum upatch_prog_role */
__le32 kind; /* enum upatch_attach_kind */
union {
- __le64 offset; /* kprobe offset */
+ __le64 offset; /* kprobe or uprobe offset in the function */
struct {
__le32 device_classes;
__le32 reserved;
} xdp;
};
- char target[UPATCH_ATTACH_TARGET_MAX]; /* kprobe symbol */
+ char target[UPATCH_ATTACH_TARGET_MAX]; /* probe symbol or glob */
char map[UPATCH_PROG_MAP_MAX];
};
@@ -247,8 +249,8 @@ struct upatch_prog_metadata {
/*
* UPGM v1 optionally appends one UPUT block after the fixed program
- * entries. It is required when any program has a uprobe multi attach kind
- * and absent otherwise. Each variable-length record is followed by
+ * entries. It is required when any program has a uprobe attach kind and
+ * absent otherwise; a single uprobe has one offset. Each record is followed by
* path_len path bytes (without NUL), build_id_len build-ID bytes, and
* nr_offsets little-endian __u64 file offsets. Records have no padding.
*/
diff --git a/testing/proofs/cbmc/upatch-prog-specs.c b/testing/proofs/cbmc/upatch-prog-specs.c
index 4350257f..44be2782 100644
--- a/testing/proofs/cbmc/upatch-prog-specs.c
+++ b/testing/proofs/cbmc/upatch-prog-specs.c
@@ -27,7 +27,7 @@ static bool needs_target(__u32 kind)
{
return kind == UPATCH_ATTACH_KPROBE ||
kind == UPATCH_ATTACH_KRETPROBE ||
- kind_is_uprobe_multi(kind);
+ kind_has_uprobe_target(kind);
}
static void uprobe_valid(const struct prog_spec *s)
@@ -37,8 +37,11 @@ static void uprobe_valid(const struct prog_spec *s)
__CPROVER_assert(s->uprobe_path && s->uprobe_path[0] == '/' &&
strnlen(s->uprobe_path, s->uprobe_path_len + 1) ==
s->uprobe_path_len, "an absolute path of its length");
- __CPROVER_assert(!strchr(s->target, ',') && !strchr(s->target, '|'),
- "a target without separators");
+ if (kind_is_uprobe_multi(s->kind))
+ __CPROVER_assert(!strchr(s->target, ',') && !strchr(s->target, '|'),
+ "a glob without separators");
+ else
+ __CPROVER_assert(s->nr_uprobe_offsets == 1, "one offset for a uprobe");
__CPROVER_assert(s->build_id_len && s->build_id_len <=
UPATCH_BUILD_ID_MAX, "a build ID");
__CPROVER_assert(s->nr_uprobe_offsets && s->uprobe_offsets,
@@ -51,7 +54,7 @@ static void uprobe_valid(const struct prog_spec *s)
static void attach_valid(const struct prog_spec *s)
{
__CPROVER_assert(!s->map[0], "no map");
- __CPROVER_assert(s->kind <= UPATCH_ATTACH_URETPROBE_MULTI, "a known attach kind");
+ __CPROVER_assert(s->kind <= UPATCH_ATTACH_URETPROBE, "a known attach kind");
if (s->kind == UPATCH_ATTACH_XDP)
__CPROVER_assert(s->device_classes &&
!(s->device_classes & ~(__u32)UPATCH_XDP_ALL),
@@ -62,9 +65,9 @@ static void attach_valid(const struct prog_spec *s)
return;
}
__CPROVER_assert(s->target[0], "a target");
- __CPROVER_assert(s->kind == UPATCH_ATTACH_KPROBE || !s->offset,
- "an offset for a kprobe alone");
- if (kind_is_uprobe_multi(s->kind))
+ __CPROVER_assert(s->kind == UPATCH_ATTACH_KPROBE || s->kind == UPATCH_ATTACH_UPROBE ||
+ !s->offset, "an offset for a kprobe or uprobe alone");
+ if (kind_has_uprobe_target(s->kind))
uprobe_valid(s);
}
@@ -74,7 +77,7 @@ static void spec_valid(const struct prog_spec *s)
__CPROVER_assert(terminated(s->target, sizeof(s->target)) &&
terminated(s->map, sizeof(s->map)),
"terminated target and map");
- if (!kind_is_uprobe_multi(s->kind))
+ if (!kind_has_uprobe_target(s->kind))
__CPROVER_assert(!s->uprobe_path && !s->uprobe_offsets,
"uprobe targets for uprobes alone");
if (s->kind != UPATCH_ATTACH_XDP)
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index ca970995..ef5d942b 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -548,12 +548,16 @@ static bool unsupported_kprobe_section(const char *sec)
* The sleepable forms take the same attach kinds: BPF_F_SLEEPABLE is a
* load flag, which the signed loader carries, not part of the link.
*/
-static const char *uprobe_multi_section_target(const char *sec, __u32 *kind)
+static const char *uprobe_section_target(const char *sec, __u32 *kind)
{
static const struct {
const char *prefix;
__u32 kind;
} forms[] = {
+ { "uprobe/", UPATCH_ATTACH_UPROBE },
+ { "uprobe.s/", UPATCH_ATTACH_UPROBE },
+ { "uretprobe/", UPATCH_ATTACH_URETPROBE },
+ { "uretprobe.s/", UPATCH_ATTACH_URETPROBE },
{ "uprobe.multi/", UPATCH_ATTACH_UPROBE_MULTI },
{ "uprobe.multi.s/", UPATCH_ATTACH_UPROBE_MULTI },
{ "uretprobe.multi/", UPATCH_ATTACH_URETPROBE_MULTI },
@@ -579,7 +583,7 @@ static bool unsupported_uprobe_section(const char *sec)
sec++;
if (!sec)
return false;
- if (uprobe_multi_section_target(sec, &kind))
+ if (uprobe_section_target(sec, &kind))
return false;
return strcmp(sec, "uprobe") == 0 ||
strncmp(sec, "uprobe/", 7) == 0 ||
@@ -676,7 +680,7 @@ static __u32 attach_kind_from_section(const char *sec,
*target = sec + 10;
return UPATCH_ATTACH_KRETPROBE;
}
- *target = uprobe_multi_section_target(sec, &kind);
+ *target = uprobe_section_target(sec, &kind);
if (*target)
return kind;
if (strcmp(sec, "cgroup_skb/ingress") == 0)
@@ -765,6 +769,61 @@ static int split_probe_target(const char *src, char *target,
return 0;
}
+/*
+ * <path>:<function>[+<offset>] or <path>:0x<file offset> of a uprobe or
+ * uretprobe section into prog's target and offset and one file offset
+ * in elf.
+ */
+static int resolve_single_uprobe(const char *sec, __u32 kind, const char *src,
+ struct upatch_prog_entry *prog, char **path,
+ __u32 *path_len, struct elf_target *elf)
+{
+ char loc[UPATCH_ATTACH_TARGET_MAX];
+ const char *func = NULL;
+ __u64 offset = 0;
+ int err;
+
+ err = split_uprobe_target(src, path, path_len, loc, sizeof(loc));
+ if (err)
+ goto invalid;
+ if (loc[0] == '0' && (loc[1] == 'x' || loc[1] == 'X')) {
+ err = parse_x64(loc + 2, strlen(loc + 2), UINT64_MAX, &offset);
+ if (err || strscpy(prog->target, loc, sizeof(prog->target)) < 0)
+ goto invalid;
+ } else {
+ err = split_probe_target(loc, prog->target, sizeof(prog->target), &offset);
+ if (err)
+ goto invalid;
+ if (kind == UPATCH_ATTACH_URETPROBE && offset) {
+ pr_err("uretprobe offsets are unsupported in section '%s'", sec);
+ return -EINVAL;
+ }
+ func = prog->target;
+ prog->offset = cpu_to_le64(offset);
+ }
+ err = elf_target_resolve_func(*path, func, offset, elf);
+ if (err == -ENOENT)
+ pr_err("uprobe function '%s' not found in %s (or the ELF has no GNU build ID)",
+ prog->target, *path);
+ else if (err == -ERANGE)
+ pr_err("uprobe offset in section '%s' lies outside %s", sec, *path);
+ else if (err)
+ pr_err("cannot resolve uprobe target %s:%s: %s", *path, prog->target,
+ strerror(-err));
+ if (err)
+ return err;
+ /* The kernel reports a uprobe link's offset in 32 bits. */
+ if (elf->offsets[0] > UINT32_MAX) {
+ pr_err("uprobe offset in section '%s' is beyond 4 GiB of %s", sec, *path);
+ return -ERANGE;
+ }
+ return 0;
+invalid:
+ pr_err("invalid uprobe section '%s'; expected uprobe[.s]//absolute/path:function[+offset], uretprobe[.s]//absolute/path:function or a 0x file offset in place of the function",
+ sec);
+ return -EINVAL;
+}
+
static bool is_syscall_section(const char *sec)
{
return sec && strcmp(sec, "syscall") == 0;
@@ -1312,7 +1371,7 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,
goto err_free;
}
if (unsupported_uprobe_section(sec)) {
- pr_err("unsupported uprobe section '%s'; use uprobe.multi[.s]//absolute/path:function_glob or uretprobe.multi[.s]//absolute/path:function_glob",
+ pr_err("unsupported uprobe section '%s'; use uprobe[.s]//absolute/path:function[+offset], uretprobe[.s]//absolute/path:function, uprobe.multi[.s]//absolute/path:function_glob or uretprobe.multi[.s]//absolute/path:function_glob",
sec);
err = -EOPNOTSUPP;
goto err_free;
@@ -1344,7 +1403,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.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, uprobe[.s], uretprobe[.s], uprobe.multi[.s], uretprobe.multi[.s], cgroup_skb, netfilter and xdp/<mask>",
sec);
err = -EOPNOTSUPP;
goto err_free;
@@ -1374,6 +1433,17 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,
md->progs[i].xdp.device_classes = cpu_to_le32((__u32)mask);
i++;
continue;
+ } else if (kind_is_uprobe_single(kind)) {
+ struct build_uprobe_target *ut =
+ &uprobe_targets[nr_uprobe_targets++];
+
+ ut->prog_idx = i;
+ err = resolve_single_uprobe(sec, kind, target, &md->progs[i], &ut->path,
+ &ut->path_len, &ut->elf);
+ if (err)
+ goto err_free;
+ i++;
+ continue;
} else if (kind_is_uprobe_multi(kind)) {
struct build_uprobe_target *ut =
&uprobe_targets[nr_uprobe_targets];
diff --git a/util/elf_target.c b/util/elf_target.c
index 0b3773cc..b70fc11d 100644
--- a/util/elf_target.c
+++ b/util/elf_target.c
@@ -2,7 +2,7 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * ELF symbol and GNU build-ID handling for uprobe.multi upatches.
+ * ELF symbol and GNU build-ID handling for uprobe upatches.
*/
#include <elf.h>
@@ -16,23 +16,15 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sys/stat.h>
#include <shirudo/misc.h>
+#include <shirudo/overflow.h>
#include "elf_target.h"
+#include "libbpf_internal.h"
#include "malloc.h"
-/*
- * libbpf uses this resolver for its uprobe.multi attach path. It is not yet
- * part of libbpf's installed public headers, so keep the narrow declaration
- * local instead of including libbpf_internal.h (which requires kernel-only
- * headers that are not part of shirudo's userspace build).
- */
-extern int elf_resolve_pattern_offsets(const char *binary_path,
- const char *pattern,
- unsigned long **poffsets,
- size_t *pcnt);
-
static int u64_cmp(const void *a, const void *b)
{
__u64 av = *(const __u64 *)a;
@@ -217,6 +209,46 @@ out:
return err;
}
+int elf_target_resolve_func(const char *path, const char *func, __u64 offset,
+ struct elf_target *out)
+{
+ struct stat st;
+ __u64 base = 0;
+ long func_off;
+ int fd, err;
+
+ if (!path || path[0] != '/' || (func && !func[0]) || !out)
+ return -EINVAL;
+ memset(out, 0, sizeof(*out));
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ if (fstat(fd, &st) != 0) {
+ err = -errno;
+ close(fd);
+ return err;
+ }
+ err = elf_build_id_fd(fd, out->build_id, &out->build_id_len);
+ close(fd);
+ if (err)
+ return err;
+ if (func) {
+ func_off = elf_find_func_offset_from_file(path, func);
+ if (func_off < 0)
+ return (int)func_off;
+ base = (__u64)func_off;
+ }
+ if (check_add_overflow(base, offset, &base) || base >= (__u64)st.st_size)
+ return -ERANGE;
+ out->offsets = xcalloc(1, sizeof(*out->offsets));
+ if (!out->offsets)
+ return -ENOMEM;
+ out->offsets_cap = 1;
+ out->offsets[0] = base;
+ out->nr_offsets = 1;
+ return 0;
+}
+
void elf_target_free(struct elf_target *target)
{
if (!target)
diff --git a/util/elf_target.h b/util/elf_target.h
index 4cc350e4..ae584a56 100644
--- a/util/elf_target.h
+++ b/util/elf_target.h
@@ -20,6 +20,13 @@ struct elf_target {
int elf_target_resolve(const char *path, const char *pattern,
struct elf_target *out);
+/*
+ * One file offset: func's plus offset, or offset itself when func is NULL,
+ * inside the file at path. 0 or -errno; free with elf_target_free().
+ */
+int elf_target_resolve_func(const char *path, const char *func, __u64 offset,
+ struct elf_target *out);
+
int elf_build_id_fd(int fd,
__u8 build_id[UPATCH_BUILD_ID_MAX],
__u32 *build_id_len);
diff --git a/util/libbpf_internal.h b/util/libbpf_internal.h
new file mode 100644
index 00000000..3180bce5
--- /dev/null
+++ b/util/libbpf_internal.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Internal libbpf declarations used by shirudo.
+ */
+
+#ifndef SHIRUDO_LIBBPF_INTERNAL_H
+#define SHIRUDO_LIBBPF_INTERNAL_H
+
+#include <stddef.h>
+
+/* Shirudo's local version of libbpf's libbpf_internal.h. */
+int elf_resolve_pattern_offsets(const char *binary_path,
+ const char *pattern,
+ unsigned long **poffsets,
+ size_t *pcnt);
+long elf_find_func_offset_from_file(const char *binary_path,
+ const char *name);
+
+#endif /* SHIRUDO_LIBBPF_INTERNAL_H */
diff --git a/util/upatch.c b/util/upatch.c
index 88c888a2..aa524667 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -631,11 +631,12 @@ static int parse_uprobe_targets(const __u8 **pos, const __u8 *end, struct prog_s
path_len = le32_to_cpu(record.path_len);
build_id_len = le32_to_cpu(record.build_id_len);
nr_offsets = le32_to_cpu(record.nr_offsets);
- if (prog_idx >= nr_progs || !kind_is_uprobe_multi(specs[prog_idx].kind) ||
+ if (prog_idx >= nr_progs || !kind_has_uprobe_target(specs[prog_idx].kind) ||
specs[prog_idx].uprobe_path || path_len == 0 ||
path_len >= UPATCH_UPROBE_PATH_MAX || build_id_len == 0 ||
build_id_len > UPATCH_BUILD_ID_MAX || nr_offsets == 0 ||
- nr_offsets > UPATCH_UPROBE_OFFSETS_MAX)
+ nr_offsets > UPATCH_UPROBE_OFFSETS_MAX ||
+ (kind_is_uprobe_single(specs[prog_idx].kind) && nr_offsets != 1))
return -EINVAL;
payload_len = (size_t)path_len + build_id_len + (size_t)nr_offsets * sizeof(__u64);
if ((size_t)(end - cursor) < payload_len ||
@@ -662,7 +663,7 @@ static int parse_uprobe_targets(const __u8 **pos, const __u8 *end, struct prog_s
}
}
for (i = 0; i < nr_progs; i++) {
- if (kind_is_uprobe_multi(specs[i].kind) && !specs[i].uprobe_path)
+ if (kind_has_uprobe_target(specs[i].kind) && !specs[i].uprobe_path)
return -EINVAL;
}
*pos = cursor;
@@ -767,6 +768,10 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,
goto out;
nr_uprobes++;
break;
+ case UPATCH_ATTACH_UPROBE:
+ case UPATCH_ATTACH_URETPROBE:
+ nr_uprobes++;
+ break;
case UPATCH_ATTACH_XDP:
spec->device_classes = le32_to_cpu(prog.xdp.device_classes);
/* A device runs one native XDP program at a time. */
@@ -790,7 +795,8 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,
spec->offset = le64_to_cpu(prog.offset);
memcpy(spec->target, prog.target, sizeof(spec->target));
spec->target[sizeof(spec->target) - 1] = '\0';
- if (kind == UPATCH_ATTACH_KRETPROBE && spec->offset)
+ if ((kind == UPATCH_ATTACH_KRETPROBE || kind == UPATCH_ATTACH_URETPROBE) &&
+ spec->offset)
goto out;
if (kind_is_uprobe_multi(kind) &&
(strchr(spec->target, ',') || strchr(spec->target, '|')))
diff --git a/util/upatch.h b/util/upatch.h
index b30d0a5b..a830d2a4 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -167,6 +167,17 @@ static inline bool kind_is_uprobe_multi(__u32 kind)
return kind == UPATCH_ATTACH_UPROBE_MULTI || kind == UPATCH_ATTACH_URETPROBE_MULTI;
}
+static inline bool kind_is_uprobe_single(__u32 kind)
+{
+ return kind == UPATCH_ATTACH_UPROBE || kind == UPATCH_ATTACH_URETPROBE;
+}
+
+/* The kinds that attach to a userspace binary named in the UPUT block. */
+static inline bool kind_has_uprobe_target(__u32 kind)
+{
+ return kind_is_uprobe_multi(kind) || kind_is_uprobe_single(kind);
+}
+
/*
* 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().
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index f994c397..af592872 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -194,6 +194,30 @@ static int xdp_location(const char *name, char *out, size_t cap)
return n < 0 ? -E2BIG : 0;
}
+static bool link_is_uprobe(const struct bpf_link_info *linfo)
+{
+ return linfo->type == BPF_LINK_TYPE_PERF_EVENT &&
+ (linfo->perf_event.type == BPF_PERF_EVENT_UPROBE ||
+ linfo->perf_event.type == BPF_PERF_EVENT_URETPROBE);
+}
+
+static int uprobe_location(int link_fd, char *out, size_t cap)
+{
+ struct bpf_link_info linfo = {};
+ char path[PATH_MAX] = {};
+ __u32 len = sizeof(linfo);
+
+ linfo.perf_event.uprobe.file_name = ptr_to_u64(path);
+ linfo.perf_event.uprobe.name_len = sizeof(path);
+ if (bpf_link_get_info_by_fd(link_fd, &linfo, &len))
+ return -errno;
+ if (!link_is_uprobe(&linfo) || !memchr(path, '\0', sizeof(path)))
+ return -EINVAL;
+ return strsprintf(out, cap, "%s/%s+0x%x",
+ linfo.perf_event.type == BPF_PERF_EVENT_URETPROBE ? "uretprobe" : "uprobe",
+ path, linfo.perf_event.uprobe.offset) < 0 ? -E2BIG : 0;
+}
+
int upatch_resolve_location(struct btf *vbtf, const char *name,
char *out, size_t cap)
{
@@ -252,6 +276,11 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,
xfree(path, path_size);
return ret;
}
+ if (link_is_uprobe(&linfo)) {
+ ret = uprobe_location(link_fd, out, cap);
+ close(link_fd);
+ return ret;
+ }
close(link_fd);
if (linfo.type == BPF_LINK_TYPE_TRACING)
attach_type = linfo.tracing.attach_type;