Skip to content

eBPF-based security platform

upatch: resolve the location of one link in a function of its own

Jiri Olsa1345320f7b20
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 5fa088e9..f7fff5ef 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -218,94 +218,77 @@ static int uprobe_location(int link_fd, char *out, size_t cap)
path, linfo.perf_event.uprobe.offset) < 0 ? -E2BIG : 0;
}
-int upatch_resolve_location(struct btf *vbtf, const char *name,
- char *out, size_t cap)
+static int uprobe_multi_location(int link_fd, const struct bpf_link_info *linfo,
+ char *out, size_t cap)
+{
+ __u32 path_size = linfo->uprobe_multi.path_size;
+ struct bpf_link_info info = {};
+ __u32 len = sizeof(info);
+ char *path;
+ int ret;
+
+ if (!path_size || path_size > PATH_MAX)
+ return -EINVAL;
+ path = xmalloc(path_size);
+ if (!path)
+ return -ENOMEM;
+ info.uprobe_multi.path = ptr_to_u64(path);
+ info.uprobe_multi.path_size = path_size;
+ ret = bpf_link_get_info_by_fd(link_fd, &info, &len);
+ if (!ret && !memchr(path, '\0', path_size))
+ ret = -EINVAL;
+ if (!ret && strsprintf(out, cap, "%s/%s (%u funcs)",
+ linfo->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN ?
+ "uretprobe.multi" : "uprobe.multi", path,
+ linfo->uprobe_multi.count) < 0)
+ ret = -E2BIG;
+ xfree(path, path_size);
+ return ret;
+}
+
+static int prog_info_by_id(__u32 prog_id, struct bpf_prog_info *info)
+{
+ __u32 len = sizeof(*info);
+ int fd, ret;
+
+ fd = bpf_prog_get_fd_by_id(prog_id);
+ if (fd < 0)
+ return -errno;
+ ret = bpf_prog_get_info_by_fd(fd, info, &len);
+ close(fd);
+ return ret;
+}
+
+static int link_location(struct btf *vbtf, int link_fd, const struct bpf_link_info *linfo,
+ char *out, size_t cap)
{
- struct bpf_link_info linfo = {};
struct bpf_prog_info info = {};
const char *attach_name = NULL;
- int link_fd, prog_fd, ret;
__u32 attach_type = 0;
- struct upatch_pins pins;
const char *prefix;
- __u32 len;
+ int ret;
- out[0] = '\0';
- if (!upatch_pins_read(name, &pins) && pins.nr_xdp_progs)
- return xdp_location(name, out, cap);
- link_fd = upatch_open_first_link(name);
- if (link_fd < 0)
- return link_fd;
- len = sizeof(linfo);
- ret = bpf_link_get_info_by_fd(link_fd, &linfo, &len);
- if (ret != 0 || linfo.prog_id == 0) {
- close(link_fd);
- return ret ? ret : -EINVAL;
- }
- if (linfo.type == BPF_LINK_TYPE_KPROBE_MULTI) {
- close(link_fd);
+ if (linfo->type == BPF_LINK_TYPE_KPROBE_MULTI)
return strsprintf(out, cap, "%s (%u funcs)",
- linfo.kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN ?
+ 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;
- __u32 path_size = linfo.uprobe_multi.path_size;
- char *path;
-
- if (!path_size || path_size > PATH_MAX) {
- close(link_fd);
- return -EINVAL;
- }
- path = xmalloc(path_size);
- if (!path) {
- close(link_fd);
- return -ENOMEM;
- }
- detail.uprobe_multi.path = ptr_to_u64(path);
- detail.uprobe_multi.path_size = path_size;
- len = sizeof(detail);
- ret = bpf_link_get_info_by_fd(link_fd, &detail, &len);
- close(link_fd);
- if (ret != 0) {
- xfree(path, path_size);
- return ret;
- }
- if (!memchr(path, '\0', path_size)) {
- xfree(path, path_size);
- return -EINVAL;
- }
- ret = strsprintf(out, cap, "%s/%s (%u funcs)",
- linfo.uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN ?
- "uretprobe.multi" : "uprobe.multi", path, count) < 0 ? -E2BIG : 0;
- 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;
- if (linfo.type == BPF_LINK_TYPE_NETFILTER &&
- linfo.netfilter.pf == NFPROTO_IPV6 &&
- linfo.netfilter.hooknum == NF_INET_PRE_ROUTING &&
- linfo.netfilter.priority == NF_IP6_PRI_RAW_BEFORE_DEFRAG) {
+ linfo->kprobe_multi.count) < 0 ? -E2BIG : 0;
+ if (linfo->type == BPF_LINK_TYPE_UPROBE_MULTI)
+ return uprobe_multi_location(link_fd, linfo, out, cap);
+ if (link_is_uprobe(linfo))
+ return uprobe_location(link_fd, out, cap);
+ if (linfo->type == BPF_LINK_TYPE_TRACING)
+ attach_type = linfo->tracing.attach_type;
+ if (linfo->type == BPF_LINK_TYPE_NETFILTER &&
+ linfo->netfilter.pf == NFPROTO_IPV6 &&
+ linfo->netfilter.hooknum == NF_INET_PRE_ROUTING &&
+ linfo->netfilter.priority == NF_IP6_PRI_RAW_BEFORE_DEFRAG) {
strscpy(out, "netfilter/ipv6-pre-routing", cap);
return 0;
}
- prog_fd = bpf_prog_get_fd_by_id(linfo.prog_id);
- if (prog_fd < 0)
- return -errno;
- len = sizeof(info);
- ret = bpf_prog_get_info_by_fd(prog_fd, &info, &len);
- close(prog_fd);
- if (ret != 0)
+ ret = prog_info_by_id(linfo->prog_id, &info);
+ if (ret)
return ret;
if (vbtf && info.attach_btf_id &&
@@ -334,6 +317,29 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,
return 0;
}
+int upatch_resolve_location(struct btf *vbtf, const char *name,
+ char *out, size_t cap)
+{
+ struct bpf_link_info linfo = {};
+ __u32 len = sizeof(linfo);
+ struct upatch_pins pins;
+ int link_fd, ret;
+
+ out[0] = '\0';
+ if (!upatch_pins_read(name, &pins) && pins.nr_xdp_progs)
+ return xdp_location(name, out, cap);
+ link_fd = upatch_open_first_link(name);
+ if (link_fd < 0)
+ return link_fd;
+ ret = bpf_link_get_info_by_fd(link_fd, &linfo, &len);
+ if (!ret && !linfo.prog_id)
+ ret = -EINVAL;
+ if (!ret)
+ ret = link_location(vbtf, link_fd, &linfo, out, cap);
+ close(link_fd);
+ return ret;
+}
+
void upatch_loaded_at(const char *name, char *out, size_t cap)
{
struct upatch_pins pins;