From: Lorenz Bauer <lmb@cloudflare.com>
To: ast@kernel.org, daniel@iogearbox.net
Cc: netdev@vger.kernel.org, linux-api@vger.kernel.org,
Lorenz Bauer <lmb@cloudflare.com>
Subject: [PATCH v5 1/4] bpf: respect size hint to BPF_PROG_TEST_RUN if present
Date: Mon, 3 Dec 2018 11:31:23 +0000 [thread overview]
Message-ID: <20181203113126.2591-2-lmb@cloudflare.com> (raw)
In-Reply-To: <20181203113126.2591-1-lmb@cloudflare.com>
Use data_size_out as a size hint when copying test output to user space.
ENOSPC is returned if the output buffer is too small.
Callers which so far did not set data_size_out are not affected.
Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
---
include/uapi/linux/bpf.h | 7 +++++--
net/bpf/test_run.c | 15 +++++++++++++--
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 23e2031a43d4..01b0e5f485c4 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -360,8 +360,11 @@ union bpf_attr {
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
__u32 prog_fd;
__u32 retval;
- __u32 data_size_in;
- __u32 data_size_out;
+ __u32 data_size_in; /* input: len of data_in */
+ __u32 data_size_out; /* input/output: len of data_out
+ * returns ENOSPC if data_out
+ * is too small.
+ */
__aligned_u64 data_in;
__aligned_u64 data_out;
__u32 repeat;
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index c89c22c49015..7663e6a57280 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -74,8 +74,18 @@ static int bpf_test_finish(const union bpf_attr *kattr,
{
void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
int err = -EFAULT;
+ u32 copy_size = size;
- if (data_out && copy_to_user(data_out, data, size))
+ /* Clamp copy if the user has provided a size hint, but copy the full
+ * buffer if not to retain old behaviour.
+ */
+ if (kattr->test.data_size_out &&
+ copy_size > kattr->test.data_size_out) {
+ copy_size = kattr->test.data_size_out;
+ err = -ENOSPC;
+ }
+
+ if (data_out && copy_to_user(data_out, data, copy_size))
goto out;
if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
goto out;
@@ -83,7 +93,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,
goto out;
if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
goto out;
- err = 0;
+ if (err != -ENOSPC)
+ err = 0;
out:
return err;
}
--
2.19.1
next prev parent reply other threads:[~2018-12-03 11:31 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-16 12:53 [PATCH 0/3] Fix unsafe BPF_PROG_TEST_RUN interface Lorenz Bauer
2018-11-16 12:53 ` [PATCH 1/3] bpf: respect size hint to BPF_PROG_TEST_RUN if present Lorenz Bauer
2018-11-18 5:47 ` Y Song
2018-11-16 12:53 ` [PATCH 2/3] libbpf: require size hint in bpf_prog_test_run Lorenz Bauer
2018-11-18 5:53 ` Y Song
2018-11-16 12:53 ` [PATCH 3/3] selftests: add a test for bpf_prog_test_run output size Lorenz Bauer
2018-11-18 5:59 ` Y Song
2018-11-20 11:35 ` Lorenz Bauer
2018-11-20 16:58 ` Y Song
2018-11-18 6:13 ` [PATCH 0/3] Fix unsafe BPF_PROG_TEST_RUN interface Y Song
2018-11-19 14:30 ` Lorenz Bauer
2018-11-20 0:34 ` Daniel Borkmann
2018-11-20 15:43 ` [PATCH v2 0/4] " Lorenz Bauer
2018-11-20 15:43 ` [PATCH v2 1/4] bpf: respect size hint to BPF_PROG_TEST_RUN if present Lorenz Bauer
2018-11-20 15:43 ` [PATCH v2 2/4] tools: sync uapi/linux/bpf.h Lorenz Bauer
2018-11-20 15:43 ` [PATCH v2 3/4] libbpf: require size hint in bpf_prog_test_run Lorenz Bauer
2018-11-20 19:18 ` Alexei Starovoitov
2018-11-20 19:43 ` Lorenz Bauer
2018-11-20 22:51 ` Alexei Starovoitov
2018-11-20 15:43 ` [PATCH v2 4/4] selftests: add a test for bpf_prog_test_run output size Lorenz Bauer
2018-11-20 17:18 ` [PATCH v2 0/4] Fix unsafe BPF_PROG_TEST_RUN interface Y Song
2018-11-22 14:09 ` [PATCH v3 " Lorenz Bauer
2018-11-22 14:09 ` [PATCH v3 1/4] bpf: respect size hint to BPF_PROG_TEST_RUN if present Lorenz Bauer
2018-11-22 14:09 ` [PATCH v3 2/4] tools: sync uapi/linux/bpf.h Lorenz Bauer
2018-11-22 14:09 ` [PATCH v3 3/4] libbpf: add bpf_prog_test_run_xattr Lorenz Bauer
2018-11-23 22:25 ` Daniel Borkmann
2018-11-24 22:20 ` Alexei Starovoitov
2018-11-26 12:45 ` Lorenz Bauer
2018-11-26 13:39 ` Daniel Borkmann
2018-11-28 5:05 ` Alexei Starovoitov
2018-11-28 16:52 ` Lorenz Bauer
2018-11-22 14:09 ` [PATCH v3 4/4] selftests: add a test for bpf_prog_test_run_xattr Lorenz Bauer
2018-11-28 16:53 ` [PATCH v4 0/4] Fix unsafe BPF_PROG_TEST_RUN interface Lorenz Bauer
2018-11-28 16:53 ` [PATCH v4 1/4] bpf: respect size hint to BPF_PROG_TEST_RUN if present Lorenz Bauer
2018-11-28 16:53 ` [PATCH v4 2/4] tools: sync uapi/linux/bpf.h Lorenz Bauer
2018-11-28 16:53 ` [PATCH v4 3/4] libbpf: add bpf_prog_test_run_xattr Lorenz Bauer
2018-11-30 21:21 ` Alexei Starovoitov
2018-11-28 16:53 ` [PATCH v4 4/4] selftests: add a test for bpf_prog_test_run_xattr Lorenz Bauer
2018-12-03 11:31 ` [PATCH v5 0/4] Fix unsafe BPF_PROG_TEST_RUN interface Lorenz Bauer
2018-12-03 11:31 ` Lorenz Bauer [this message]
2018-12-03 11:31 ` [PATCH v5 2/4] tools: sync uapi/linux/bpf.h Lorenz Bauer
2018-12-03 11:31 ` [PATCH v5 3/4] libbpf: add bpf_prog_test_run_xattr Lorenz Bauer
2018-12-03 11:31 ` [PATCH v5 4/4] selftests: add a test for bpf_prog_test_run_xattr Lorenz Bauer
2018-12-04 16:22 ` [PATCH v5 0/4] Fix unsafe BPF_PROG_TEST_RUN interface Alexei Starovoitov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181203113126.2591-2-lmb@cloudflare.com \
--to=lmb@cloudflare.com \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-api@vger.kernel.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).