[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20190506183116.33014-2-joel@joelfernandes.org>
Date: Mon, 6 May 2019 14:31:14 -0400
From: "Joel Fernandes (Google)" <joel@...lfernandes.org>
To: linux-kernel@...r.kernel.org
Cc: "Joel Fernandes (Google)" <joel@...lfernandes.org>,
Adrian Ratiu <adrian.ratiu@...labora.com>,
Alexei Starovoitov <ast@...nel.org>,
Andrii Nakryiko <andrii.nakryiko@...il.com>,
bpf@...r.kernel.org, Brendan Gregg <brendan.d.gregg@...il.com>,
Daniel Borkmann <daniel@...earbox.net>,
duyuchao <yuchao.du@...soc.com>, Ingo Molnar <mingo@...hat.com>,
Karim Yaghmour <karim.yaghmour@...rsys.com>,
Kees Cook <keescook@...omium.org>, kernel-team@...roid.com,
Manjo Raja Rao <linux@...ojrajarao.com>,
Martin KaFai Lau <kafai@...com>,
Masami Hiramatsu <mhiramat@...nel.org>,
Michal Gregorczyk <michalgr@...e.com>,
Mohammad Husain <russoue@...il.com>, netdev@...r.kernel.org,
Peter Ziljstra <peterz@...radead.org>,
Qais Yousef <qais.yousef@....com>,
Song Liu <songliubraving@...com>,
Srinivas Ramana <sramana@...eaurora.org>,
Steven Rostedt <rostedt@...dmis.org>,
Tamir Carmeli <carmeli.tamir@...il.com>,
Yonghong Song <yhs@...com>
Subject: [PATCH v2 2/4] bpf: Add support for reading kernel pointers
The bpf_probe_read function is ambiguous in whether the pointer being
read is a kernel or user one. Add a specific function for kernel pointer
in this patch. Previous patches add it for userspace pointers.
Signed-off-by: Joel Fernandes (Google) <joel@...lfernandes.org>
---
include/uapi/linux/bpf.h | 9 ++++++++-
kernel/trace/bpf_trace.c | 22 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 8146784b9fe3..05af4e1151d3 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -2437,6 +2437,12 @@ union bpf_attr {
* Read a userspace pointer safely.
* Return
* 0 on success or negative error
+ *
+ * int bpf_probe_read_kernel(void *dst, int size, void *src)
+ * Description
+ * Read a kernel pointer safely.
+ * Return
+ * 0 on success or negative error
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
@@ -2538,7 +2544,8 @@ union bpf_attr {
FN(tcp_sock), \
FN(skb_ecn_set_ce), \
FN(get_listener_sock), \
- FN(probe_read_user),
+ FN(probe_read_user), \
+ FN(probe_read_kernel),
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 7485deb0777f..99dc354fd62b 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -153,6 +153,26 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
.arg3_type = ARG_ANYTHING,
};
+BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size, const void *, unsafe_ptr)
+{
+ int ret;
+
+ ret = probe_kernel_read(dst, unsafe_ptr, size);
+ if (unlikely(ret < 0))
+ memset(dst, 0, size);
+
+ return ret;
+}
+
+static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
+ .func = bpf_probe_read_kernel,
+ .gpl_only = true,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_UNINIT_MEM,
+ .arg2_type = ARG_CONST_SIZE_OR_ZERO,
+ .arg3_type = ARG_ANYTHING,
+};
+
BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, const void *, unsafe_ptr)
{
int ret;
@@ -593,6 +613,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_probe_read_proto;
case BPF_FUNC_probe_read_user:
return &bpf_probe_read_user_proto;
+ case BPF_FUNC_probe_read_kernel:
+ return &bpf_probe_read_kernel_proto;
case BPF_FUNC_ktime_get_ns:
return &bpf_ktime_get_ns_proto;
case BPF_FUNC_tail_call:
--
2.21.0.1020.gf2820cf01a-goog
Powered by blists - more mailing lists