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 2/3] libbpf: require size hint in bpf_prog_test_run
Date: Fri, 16 Nov 2018 12:53:28 +0000 [thread overview]
Message-ID: <20181116125329.3974-3-lmb@cloudflare.com> (raw)
In-Reply-To: <20181116125329.3974-1-lmb@cloudflare.com>
Require size_out to be non-NULL if data_out is given. This prevents
accidental overwriting of process memory after the output buffer.
Adjust callers of bpf_prog_test_run to this behaviour.
Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
---
tools/lib/bpf/bpf.c | 4 +++-
tools/testing/selftests/bpf/test_progs.c | 10 ++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 03f9bcc4ef50..127a9aa6170e 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -403,10 +403,12 @@ int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
attr.test.data_in = ptr_to_u64(data);
attr.test.data_out = ptr_to_u64(data_out);
attr.test.data_size_in = size;
+ if (data_out)
+ attr.test.data_size_out = *size_out;
attr.test.repeat = repeat;
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
- if (size_out)
+ if (data_out)
*size_out = attr.test.data_size_out;
if (retval)
*retval = attr.test.retval;
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 2d3c04f45530..560d7527b86b 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -150,6 +150,7 @@ static void test_xdp(void)
bpf_map_update_elem(map_fd, &key4, &value4, 0);
bpf_map_update_elem(map_fd, &key6, &value6, 0);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
@@ -158,6 +159,7 @@ static void test_xdp(void)
"err %d errno %d retval %d size %d\n",
err, errno, retval, size);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration);
CHECK(err || retval != XDP_TX || size != 114 ||
@@ -182,6 +184,7 @@ static void test_xdp_adjust_tail(void)
return;
}
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
@@ -189,6 +192,7 @@ static void test_xdp_adjust_tail(void)
"ipv4", "err %d errno %d retval %d size %d\n",
err, errno, retval, size);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration);
CHECK(err || retval != XDP_TX || size != 54,
@@ -252,6 +256,7 @@ static void test_l4lb(const char *file)
goto out;
bpf_map_update_elem(map_fd, &real_num, &real_def, 0);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
CHECK(err || retval != 7/*TC_ACT_REDIRECT*/ || size != 54 ||
@@ -259,6 +264,7 @@ static void test_l4lb(const char *file)
"err %d errno %d retval %d size %d magic %x\n",
err, errno, retval, size, *magic);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration);
CHECK(err || retval != 7/*TC_ACT_REDIRECT*/ || size != 74 ||
@@ -341,6 +347,7 @@ static void test_xdp_noinline(void)
goto out;
bpf_map_update_elem(map_fd, &real_num, &real_def, 0);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
CHECK(err || retval != 1 || size != 54 ||
@@ -348,6 +355,7 @@ static void test_xdp_noinline(void)
"err %d errno %d retval %d size %d magic %x\n",
err, errno, retval, size, *magic);
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration);
CHECK(err || retval != 1 || size != 74 ||
@@ -1795,6 +1803,7 @@ static void test_queue_stack_map(int type)
pkt_v4.iph.saddr = vals[MAP_SIZE - 1 - i] * 5;
}
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
if (err || retval || size != sizeof(pkt_v4) ||
@@ -1808,6 +1817,7 @@ static void test_queue_stack_map(int type)
err, errno, retval, size, iph->daddr);
/* Queue is empty, program should return TC_ACT_SHOT */
+ size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
CHECK(err || retval != 2 /* TC_ACT_SHOT */|| size != sizeof(pkt_v4),
--
2.17.1
next prev parent reply other threads:[~2018-11-16 12:53 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 ` Lorenz Bauer [this message]
2018-11-18 5:53 ` [PATCH 2/3] libbpf: require size hint in bpf_prog_test_run 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 ` [PATCH v5 1/4] bpf: respect size hint to BPF_PROG_TEST_RUN if present Lorenz Bauer
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=20181116125329.3974-3-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).