public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bpf: reject blacklisted symbols in kprobe_multi to avoid recursive trap
@ 2023-05-10 12:20 Ze Gao
  2023-05-10 14:13 ` Yonghong Song
  0 siblings, 1 reply; 20+ messages in thread
From: Ze Gao @ 2023-05-10 12:20 UTC (permalink / raw)
  To: Song Liu, Jiri Olsa, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Steven Rostedt,
	Masami Hiramatsu
  Cc: Ze Gao, bpf, linux-kernel, linux-trace-kernel

BPF_LINK_TYPE_KPROBE_MULTI attaches kprobe programs through fprobe,
however it does not takes those kprobe blacklisted into consideration,
which likely introduce recursive traps and blows up stacks.

this patch adds simple check and remove those are in kprobe_blacklist
from one fprobe during bpf_kprobe_multi_link_attach. And also
check_kprobe_address_safe is open for more future checks.

note that ftrace provides recursion detection mechanism, but for kprobe
only, we can directly reject those cases early without turning to ftrace.

Signed-off-by: Ze Gao <zegao@tencent.com>
---
 kernel/trace/bpf_trace.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 9a050e36dc6c..44c68bc06bbd 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2764,6 +2764,37 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3
 	return arr.mods_cnt;
 }
 
+static inline int check_kprobe_address_safe(unsigned long addr)
+{
+	if (within_kprobe_blacklist(addr))
+		return -EINVAL;
+	else
+		return 0;
+}
+
+static int check_bpf_kprobe_addrs_safe(unsigned long *addrs, int num)
+{
+	int i, cnt;
+	char symname[KSYM_NAME_LEN];
+
+	for (i = 0; i < num; ++i) {
+		if (check_kprobe_address_safe((unsigned long)addrs[i])) {
+			lookup_symbol_name(addrs[i], symname);
+			pr_warn("bpf_kprobe: %s at %lx is blacklisted\n", symname, addrs[i]);
+			/* mark blacklisted symbol for remove */
+			addrs[i] = 0;
+		}
+	}
+
+	/* remove blacklisted symbol from addrs */
+	for (i = 0, cnt = 0; i < num; ++i) {
+		if (addrs[i])
+			addrs[cnt++]  = addrs[i];
+	}
+
+	return cnt;
+}
+
 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 {
 	struct bpf_kprobe_multi_link *link = NULL;
@@ -2859,6 +2890,12 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 	else
 		link->fp.entry_handler = kprobe_multi_link_handler;
 
+	cnt = check_bpf_kprobe_addrs_safe(addrs, cnt);
+	if (!cnt) {
+		err = -EINVAL;
+		goto error;
+	}
+
 	link->addrs = addrs;
 	link->cookies = cookies;
 	link->cnt = cnt;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2023-05-16 15:17 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-10 12:20 [PATCH] bpf: reject blacklisted symbols in kprobe_multi to avoid recursive trap Ze Gao
2023-05-10 14:13 ` Yonghong Song
2023-05-10 17:27   ` Jiri Olsa
2023-05-10 20:20     ` Yonghong Song
2023-05-10 23:54       ` Yonghong Song
2023-05-11  1:24         ` Ze Gao
2023-05-11  2:06           ` Ze Gao
2023-05-16  4:57           ` Masami Hiramatsu
2023-05-12  5:53     ` Ze Gao
2023-05-12 14:29       ` Yonghong Song
2023-05-12 22:33         ` Jiri Olsa
2023-05-13  4:17         ` Steven Rostedt
2023-05-13  9:19           ` Ze Gao
2023-05-14 17:11           ` Yonghong Song
2023-05-16  4:31           ` Masami Hiramatsu
2023-05-16  5:10             ` Masami Hiramatsu
2023-05-16  5:49         ` Masami Hiramatsu
2023-05-16 15:16           ` Yonghong Song
2023-05-11  1:06   ` Ze Gao
2023-05-15  5:59     ` Ze Gao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox