[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250125-bpf_dynptr_probe-v2-7-c42c87f97afe@outlook.com>
Date: Sat, 25 Jan 2025 16:29:54 +0800
From: Levi Zim via B4 Relay <devnull+rsworktech.outlook.com@...nel.org>
To: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>, Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>,
John Fastabend <john.fastabend@...il.com>, KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>, Matt Bobrowski <mattbobrowski@...gle.com>,
Steven Rostedt <rostedt@...dmis.org>,
Masami Hiramatsu <mhiramat@...nel.org>,
Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
Mykola Lysenko <mykolal@...com>, Shuah Khan <shuah@...nel.org>
Cc: bpf@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-trace-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org,
Andrii Nakryiko <andrii.nakryiko@...il.com>,
Levi Zim <rsworktech@...look.com>
Subject: [PATCH bpf-next v2 7/7] selftests/bpf: copy_from_user_dynptr test
From: Levi Zim <rsworktech@...look.com>
This patch adds a test for copy_from_user_dynptr helper,
which tests reading variable length of user memory into a ringbuf
backed dynptr.
Signed-off-by: Levi Zim <rsworktech@...look.com>
---
tools/testing/selftests/bpf/prog_tests/dynptr.c | 3 ++
tools/testing/selftests/bpf/progs/dynptr_success.c | 34 ++++++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c
index a5a61a4c570d803bfca2af3f1a3b7d5eb0b8197f..a0095204609c710d49a6105ebf8be44723fb53d2 100644
--- a/tools/testing/selftests/bpf/prog_tests/dynptr.c
+++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c
@@ -33,6 +33,7 @@ static struct {
{"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP},
{"test_probe_read_kernel_dynptr", SETUP_RINGBUF},
{"test_probe_read_user_dynptr", SETUP_RINGBUF},
+ {"test_copy_from_user_dynptr", SETUP_RINGBUF},
};
static int ringbuf_cb(void *ctx, void *data, size_t len)
@@ -43,6 +44,8 @@ static int ringbuf_cb(void *ctx, void *data, size_t len)
if (!ASSERT_EQ(len, skel->data->test_buf.length, "length"))
return -E2BIG;
+ ASSERT_EQ(skel->bss->err, 0, "err");
+
if (!ASSERT_MEMEQ(buf, skel->data->test_buf.buf, len, "ringbuf_cb"))
return -EINVAL;
diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c
index 5317860290dccb0862b6a0b94bb6f738c8d92835..10dda58fed66a05ad1b08c8684784fddabe3f4ad 100644
--- a/tools/testing/selftests/bpf/progs/dynptr_success.c
+++ b/tools/testing/selftests/bpf/progs/dynptr_success.c
@@ -639,3 +639,37 @@ int test_probe_read_user_dynptr(void *ctx)
bpf_ringbuf_submit_dynptr(&ptr, 0);
return 0;
}
+
+SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
+int test_copy_from_user_dynptr(void *ctx)
+{
+ struct bpf_dynptr ptr;
+ int copy_len;
+
+ if (bpf_get_current_pid_tgid() >> 32 != pid)
+ return 0;
+
+ err = bpf_probe_read_user(©_len, sizeof(copy_len), &user_ptr->length);
+ if (err < 0 || copy_len < 0) {
+ err = 1;
+ return 0;
+ }
+
+ if (copy_len > MAX_BUFFER_LEN)
+ copy_len = MAX_BUFFER_LEN;
+
+ bpf_ringbuf_reserve_dynptr(&ringbuf, copy_len, 0, &ptr);
+
+ if (-E2BIG != bpf_copy_from_user_dynptr(&ptr, 0, MAX_BUFFER_LEN + 1,
+ &user_ptr->buf, 0)) {
+ err = 2;
+ goto cleanup;
+ }
+
+ err = bpf_copy_from_user_dynptr(&ptr, 0, copy_len,
+ &user_ptr->buf, 0);
+
+cleanup:
+ bpf_ringbuf_submit_dynptr(&ptr, 0);
+ return 0;
+}
--
2.48.1
Powered by blists - more mailing lists