[PATCH] perf: fix segmentation fault in perf_event__synthesize_one_bpf_prog

From: Yi Wang
Date: Wed Mar 15 2023 - 03:05:21 EST


From: "Liu.Xiaoyang" <liu.xiaoyang@xxxxxxxxxx>

Description of problem:
when /proc/sys/kernel/kptr_restrict set to 2 and there are bpf progs
loaded on system, ptr prog_lens and prog_addrs maybe Null.
then prog_addrs[i] and prog_lens[i] will case segmentation fault.

call trace:
perf: Segmentation fault
perf(sighandler_dump_stack+0x48)
/lib64/libc.so.6(+0x37400)
perf(perf_event__synthesize_bpf_events+0x23a)
perf(+0x235b73)
perf(cmd_record+0xc0d)
perf(+0x2a8c5d)
perf(main+0x69a)

Signed-off-by: Liu.Xiaoyang <liu.xiaoyang@xxxxxxxxxx>
Signed-off-by: Yi Wang <wang.yi59@xxxxxxxxxx>
---
tools/perf/util/bpf-event.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
index cc7c1f9..7a6ea6d 100644
--- a/tools/perf/util/bpf-event.c
+++ b/tools/perf/util/bpf-event.c
@@ -307,6 +307,11 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
int name_len;

+ if (!prog_lens || !prog_addrs) {
+ err = -1;
+ goto out;
+ }
+
*ksymbol_event = (struct perf_record_ksymbol) {
.header = {
.type = PERF_RECORD_KSYMBOL,
--
1.8.3.1