[PATCH bpf-next] libbpf: Fix event name too long error
From: Feng Yang
Date: Thu Apr 10 2025 - 01:28:31 EST
From: Feng Yang <yangfeng@xxxxxxxxxx>
If the event name is too long, it will cause an EINVAL error.
The kernel error path is
probes_write
trace_parse_run_command
create_or_delete_trace_uprobe
trace_uprobe_create
trace_probe_create
__trace_uprobe_create
traceprobe_parse_event_name
else if (len >= MAX_EVENT_NAME_LEN)
Requires less than 64 bytes.
Signed-off-by: Feng Yang <yangfeng@xxxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index b2591f5cab65..8e48ba99f06c 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -12227,6 +12227,16 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
return libbpf_err_ptr(err);
}
+static const char *get_last_part(const char *path)
+{
+ const char *last_slash = strrchr(path, '/');
+
+ if (last_slash != NULL)
+ return last_slash + 1;
+ else
+ return path;
+}
+
LIBBPF_API struct bpf_link *
bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
const char *binary_path, size_t func_offset,
@@ -12241,7 +12251,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
size_t ref_ctr_off;
int pfd, err;
bool retprobe, legacy;
- const char *func_name;
+ const char *func_name, *binary_name;
if (!OPTS_VALID(opts, bpf_uprobe_opts))
return libbpf_err_ptr(-EINVAL);
@@ -12254,6 +12264,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
if (!binary_path)
return libbpf_err_ptr(-EINVAL);
+ binary_name = get_last_part(binary_path);
/* Check if "binary_path" refers to an archive. */
archive_sep = strstr(binary_path, "!/");
if (archive_sep) {
@@ -12318,7 +12329,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
return libbpf_err_ptr(-EINVAL);
gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
- binary_path, func_offset);
+ binary_name, func_offset);
legacy_probe = strdup(probe_name);
if (!legacy_probe)
--
2.43.0