Skip to content

Commit 2207355

Browse files
kknjhgregkh
authored andcommitted
libbpf: Fix event name too long error
[ Upstream commit 4dde20b ] When the binary path is excessively long, the generated probe_name in libbpf exceeds the kernel's MAX_EVENT_NAME_LEN limit (64 bytes). This causes legacy uprobe event attachment to fail with error code -22. The fix reorders the fields to place the unique ID before the name. This ensures that even if truncation occurs via snprintf, the unique ID remains intact, preserving event name uniqueness. Additionally, explicit checks with MAX_EVENT_NAME_LEN are added to enforce length constraints. Before Fix: ./test_progs -t attach_probe/kprobe-long_name ...... libbpf: failed to add legacy kprobe event for 'bpf_testmod_looooooooooooooooooooooooooooooong_name+0x0': -EINVAL libbpf: prog 'handle_kprobe': failed to create kprobe 'bpf_testmod_looooooooooooooooooooooooooooooong_name+0x0' perf event: -EINVAL test_attach_kprobe_long_event_name:FAIL:attach_kprobe_long_event_name unexpected error: -22 test_attach_probe:PASS:uprobe_ref_ctr_cleanup 0 nsec #13/11 attach_probe/kprobe-long_name:FAIL #13 attach_probe:FAIL ./test_progs -t attach_probe/uprobe-long_name ...... libbpf: failed to add legacy uprobe event for /root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9: -EINVAL libbpf: prog 'handle_uprobe': failed to create uprobe '/root/linux-bpf/bpf-next/tools/testing/selftests/bpf/test_progs:0x13efd9' perf event: -EINVAL test_attach_uprobe_long_event_name:FAIL:attach_uprobe_long_event_name unexpected error: -22 #13/10 attach_probe/uprobe-long_name:FAIL #13 attach_probe:FAIL After Fix: ./test_progs -t attach_probe/uprobe-long_name #13/10 attach_probe/uprobe-long_name:OK #13 attach_probe:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED ./test_progs -t attach_probe/kprobe-long_name #13/11 attach_probe/kprobe-long_name:OK #13 attach_probe:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED Fixes: 46ed5fc ("libbpf: Refactor and simplify legacy kprobe code") Fixes: cc10623 ("libbpf: Add legacy uprobe attaching support") Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> Signed-off-by: Feng Yang <yangfeng@kylinos.cn> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250417014848.59321-2-yangfeng59949@163.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 124df6e commit 2207355

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
#define BPF_FS_MAGIC 0xcafe4a11
6161
#endif
6262

63+
#define MAX_EVENT_NAME_LEN 64
64+
6365
#define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
6466

6567
#define BPF_INSN_SZ (sizeof(struct bpf_insn))
@@ -11112,16 +11114,16 @@ static const char *tracefs_available_filter_functions_addrs(void)
1111211114
: TRACEFS"/available_filter_functions_addrs";
1111311115
}
1111411116

11115-
static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
11116-
const char *kfunc_name, size_t offset)
11117+
static void gen_probe_legacy_event_name(char *buf, size_t buf_sz,
11118+
const char *name, size_t offset)
1111711119
{
1111811120
static int index = 0;
1111911121
int i;
1112011122

11121-
snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
11122-
__sync_fetch_and_add(&index, 1));
11123+
snprintf(buf, buf_sz, "libbpf_%u_%d_%s_0x%zx", getpid(),
11124+
__sync_fetch_and_add(&index, 1), name, offset);
1112311125

11124-
/* sanitize binary_path in the probe name */
11126+
/* sanitize name in the probe name */
1112511127
for (i = 0; buf[i]; i++) {
1112611128
if (!isalnum(buf[i]))
1112711129
buf[i] = '_';
@@ -11246,9 +11248,9 @@ int probe_kern_syscall_wrapper(int token_fd)
1124611248

1124711249
return pfd >= 0 ? 1 : 0;
1124811250
} else { /* legacy mode */
11249-
char probe_name[128];
11251+
char probe_name[MAX_EVENT_NAME_LEN];
1125011252

11251-
gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
11253+
gen_probe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
1125211254
if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
1125311255
return 0;
1125411256

@@ -11304,10 +11306,10 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
1130411306
func_name, offset,
1130511307
-1 /* pid */, 0 /* ref_ctr_off */);
1130611308
} else {
11307-
char probe_name[256];
11309+
char probe_name[MAX_EVENT_NAME_LEN];
1130811310

11309-
gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
11310-
func_name, offset);
11311+
gen_probe_legacy_event_name(probe_name, sizeof(probe_name),
11312+
func_name, offset);
1131111313

1131211314
legacy_probe = strdup(probe_name);
1131311315
if (!legacy_probe)
@@ -11851,20 +11853,6 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
1185111853
return ret;
1185211854
}
1185311855

11854-
static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
11855-
const char *binary_path, uint64_t offset)
11856-
{
11857-
int i;
11858-
11859-
snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
11860-
11861-
/* sanitize binary_path in the probe name */
11862-
for (i = 0; buf[i]; i++) {
11863-
if (!isalnum(buf[i]))
11864-
buf[i] = '_';
11865-
}
11866-
}
11867-
1186811856
static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
1186911857
const char *binary_path, size_t offset)
1187011858
{
@@ -12288,13 +12276,14 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
1228812276
pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
1228912277
func_offset, pid, ref_ctr_off);
1229012278
} else {
12291-
char probe_name[PATH_MAX + 64];
12279+
char probe_name[MAX_EVENT_NAME_LEN];
1229212280

1229312281
if (ref_ctr_off)
1229412282
return libbpf_err_ptr(-EINVAL);
1229512283

12296-
gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
12297-
binary_path, func_offset);
12284+
gen_probe_legacy_event_name(probe_name, sizeof(probe_name),
12285+
strrchr(binary_path, '/') ? : binary_path,
12286+
func_offset);
1229812287

1229912288
legacy_probe = strdup(probe_name);
1230012289
if (!legacy_probe)

0 commit comments

Comments
 (0)