Fix partial copy of non-linear skb test_run output
Subsystems (5)
cover Fix partial copy of non-linear skb test_run output
This series fixes BPF_PROG_TEST_RUN copy-out handling for non-linear skbs when userspace provides a short data_out buffer.
Patch 1 fixes bpf_test_finish() to compute the skb linear head copy length from the skb layout instead of deriving it from the clamped copy size.
Patch 2 adds a selftest covering a non-linear skb with a short data_out buffer. The test checks that test_run returns -ENOSPC, reports the full packet length through data_size_out, and copies the packet prefix into data_out.
Tested with:
./test_progs -t skb_load_bytes
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
./test_progs -t skb_load_bytes -v test_nonlinear_data_out_partial:PASS:nonlinear_partial_err test_nonlinear_data_out_partial:PASS:nonlinear_partial_data_size_out test_nonlinear_data_out_partial:PASS:nonlinear_partial_data_out
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
./test_progs -t skb
Summary: 14/92 PASSED, 0 SKIPPED, 0 FAILED
Sun Jian (2):
bpf: Fix partial copy of non-linear skb test_run output selftests/bpf: Cover partial copy of non-linear skb test_run output
net/bpf/test_run.c | 11 +++--- .../selftests/bpf/prog_tests/skb_load_bytes.c | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 bpf: Fix partial copy of non-linear skb test_run output
For non-linear skbs, bpf_test_finish() derives the linear head copy length from copy_size - frag_size. This only matches the skb head length when copy_size is the full packet size.
When userspace provides a short data_out buffer, copy_size is clamped to that buffer size. If copy_size is smaller than frag_size, the computed length becomes negative and bpf_test_finish() returns -ENOSPC before copying the packet prefix or updating data_size_out.
Compute the linear head length from the skb layout instead, and clamp the head copy length to copy_size. This preserves the expected partial-copy
semantics: return -ENOSPC, copy the packet prefix that fits in data_out, and report the full packet length through data_size_out.
--- net/bpf/test_run.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
net/bpf/test_run.c +4 -7
@@ -453,19 +453,16 @@ static int bpf_test_finish(const union bpf_attr *kattr, }if (data_out) { -int len = sinfo ? copy_size - frag_size : copy_size; - -if (len <0 ) { - err = -ENOSPC ; - goto out; - } +u32 head_len = size - frag_size; +u32 len =min (copy_size, head_len);if (copy_to_user (data_out, data, len)) goto out;if (sinfo) { -int i, offset = len; +u32 offset = len;u32 data_len; +int i;for (i =0 ; i < sinfo->nr_frags; i++) {skb_frag_t *frag = &sinfo->frags[i];
Signature · 1 line
2.43.0
Re: [PATCH bpf 1/2] bpf: Fix partial copy of non-linear skb test_run output
Quoted 8 lines · Sun Jian
For non-linear skbs,
bpf_test_finish()derives the linear head copy length fromcopy_size-frag_size. This only matches the skb head length whencopy_sizeis the full packet size.When userspace provides a short
data_outbuffer,copy_sizeis clamped to that buffer size. Ifcopy_sizeis smaller thanfrag_size, the computed length becomes negative andbpf_test_finish()returns-ENOSPCbefore copying the packet prefix or updatingdata_size_out.
Thanks for fixing this!
Quoted 7 lines
Compute the linear head length from the skb layout instead, and clamp the head copy length to
copy_size. This preserves the expected partial-copy
semantics: return-ENOSPC, copy the packet prefix that fits indata_out, and report the full packet length throughdata_size_out.Fixes 838baa351cee ("bpf: Craft non-linear skbs in BPF_PROG_TEST_RUN")
Wouldn't this bug actually go back to 7855e0db150ad ("bpf: test_run:
add xdp_shared_info pointer in bpf_test_finish signature") and also affect the XDP bpf_prog_test_run_xdp()? If so, could you also add a selftest that covers it for XDP?
Quoted 36 lines
Signed-off-by Sun Jian <[email protected]>--- net/bpf/test_run.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)net/bpf/test_run.c +4
-7@@ -453,19 +453,16 @@ static int bpf_test_finish(const union bpf_attr *kattr, }if (data_out) { -int len = sinfo ? copy_size - frag_size : copy_size; - -if (len <0 ) { - err = -ENOSPC ; - goto out; - } +u32 head_len = size - frag_size; +u32 len =min (copy_size, head_len);if (copy_to_user (data_out, data, len)) goto out;if (sinfo) { -int i, offset = len; +u32 offset = len;u32 data_len; +int i;for (i =0 ; i < sinfo->nr_frags; i++) {skb_frag_t *frag = &sinfo->frags[i];Signature · 1 line
2.43.0
Re: [PATCH bpf 1/2] bpf: Fix partial copy of non-linear skb test_run output
Quoted 57 lines · Paul Chaignon
Quoted 8 lines · Sun Jian
For non-linear skbs,
bpf_test_finish()derives the linear head copy length fromcopy_size-frag_size. This only matches the skb head length whencopy_sizeis the full packet size.When userspace provides a short
data_outbuffer,copy_sizeis clamped to that buffer size. Ifcopy_sizeis smaller thanfrag_size, the computed length becomes negative andbpf_test_finish()returns-ENOSPCbefore copying the packet prefix or updatingdata_size_out.Thanks for fixing this!
Quoted 7 lines
Compute the linear head length from the skb layout instead, and clamp the head copy length to
copy_size. This preserves the expected partial-copy
semantics: return-ENOSPC, copy the packet prefix that fits indata_out, and report the full packet length throughdata_size_out.Fixes 838baa351cee ("bpf: Craft non-linear skbs in BPF_PROG_TEST_RUN")Wouldn't this bug actually go back to
7855e0db150ad("bpf:test_run:
addxdp_shared_infopointer inbpf_test_finishsignature") and also affect the XDPbpf_prog_test_run_xdp()? If so, could you also add a selftest that covers it for XDP?Quoted 31 lines
Signed-off-by Sun Jian <[email protected]>--- net/bpf/test_run.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)net/bpf/test_run.c +4
-7@@ -453,19 +453,16 @@ static int bpf_test_finish(const union bpf_attr *kattr, }if (data_out) { -int len = sinfo ? copy_size - frag_size : copy_size; - -if (len <0 ) { - err = -ENOSPC ; - goto out; - } +u32 head_len = size - frag_size; +u32 len =min (copy_size, head_len);if (copy_to_user (data_out, data, len)) goto out;if (sinfo) { -int i, offset = len; +u32 offset = len;u32 data_len; +int i;
Quoted 5 lines
Quoted 5 lines
net/bpf/test_run.c +0
-1for (i =0 ; i < sinfo->nr_frags; i++) {skb_frag_t *frag = &sinfo->frags[i]; --2.43.0
Hi Paul,
Thanks for taking a look.
Yes, that makes sense. I'll re-check the Fixes tag against 7855e0db150ad and add XDP coverage in v2, since the issue is in the shared bpf_test_finish() path.
Thanks,
Sun Jian
PATCH 2/2 selftests/bpf: Cover partial copy of non-linear skb test_run output
In reply to Sun JianAdd a test case for BPF_PROG_TEST_RUN with a non-linear skb and a short data_out buffer.
The test verifies that test_run returns -ENOSPC, reports the full packet length through data_size_out, and copies the packet prefix into data_out. The test uses a 100-byte data_out buffer with a 64-byte linear head, so the expected output spans both the skb head and the first fragment.
--- .../selftests/bpf/prog_tests/skb_load_bytes.c | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +35 -0
@@ -3,6 +3,39 @@#include <network_helpers.h> #include "skb_load_bytes.skel.h" +#define NONLINEAR_PKT_LEN 9000 +#define NONLINEAR_HEAD_LEN 64 +#define SHORT_OUT_LEN 100 + +static void test_nonlinear_data_out_partial (int prog_fd) +{ +LIBBPF_OPTS (bpf_test_run_opts, tattr); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct __sk_buff skb = {}; +int err, i; + +for (i =0 ; i <sizeof (pkt); i++) + pkt[i] = i &0xff ; + +memset (out,0xa5 ,sizeof (out)); + + skb.data_end =NONLINEAR_HEAD_LEN ; + + tattr.data_in = pkt; + tattr.data_size_in =sizeof (pkt); + tattr.data_out = out; + tattr.data_size_out =sizeof (out); + tattr.ctx_in = &skb; + tattr.ctx_size_in =sizeof (skb); + + err =bpf_prog_test_run_opts (prog_fd, &tattr); + +ASSERT_EQ (err, -ENOSPC ,"nonlinear_partial_err" ); +ASSERT_EQ (tattr.data_size_out,sizeof (pkt),"nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"nonlinear_partial_data_out" ); +} +void test_skb_load_bytes (void ) {struct skb_load_bytes *skel;
@@ -40,6 +73,8 @@ void test_skb_load_bytes(void)if (!ASSERT_EQ (test_result,0 ,"offset 10" )) goto out; +test_nonlinear_data_out_partial (prog_fd); + out:skb_load_bytes__destroy (skel); }
Signature · 1 line
2.43.0
Re: [PATCH bpf 2/2] selftests/bpf: Cover partial copy of non-linear skb test_run output
Quoted 58 lines · Sun Jian
Add a test case for
BPF_PROG_TEST_RUNwith a non-linear skb and a shortdata_outbuffer.The test verifies that
test_runreturns-ENOSPC, reports the full packet length throughdata_size_out, and copies the packet prefix intodata_out. The test uses a 100-bytedata_outbuffer with a 64-byte linear head, so the expected output spans both the skb head and the first fragment.Signed-off-by Sun Jian <[email protected]>--- .../selftests/bpf/prog_tests/skb_load_bytes.c | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+)tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +33
-0@@ -3,6 +3,39 @@#include <network_helpers.h> #include "skb_load_bytes.skel.h" +#define NONLINEAR_PKT_LEN 9000 +#define NONLINEAR_HEAD_LEN 64 +#define SHORT_OUT_LEN 100 + +static void test_nonlinear_data_out_partial (int prog_fd) +{ +LIBBPF_OPTS (bpf_test_run_opts, tattr); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct __sk_buff skb = {}; +int err, i; + +for (i =0 ; i <sizeof (pkt); i++) + pkt[i] = i &0xff ; + +memset (out,0xa5 ,sizeof (out)); + + skb.data_end =NONLINEAR_HEAD_LEN ; + + tattr.data_in = pkt; + tattr.data_size_in =sizeof (pkt); + tattr.data_out = out; + tattr.data_size_out =sizeof (out); + tattr.ctx_in = &skb; + tattr.ctx_size_in =sizeof (skb); + + err =bpf_prog_test_run_opts (prog_fd, &tattr); + +ASSERT_EQ (err, -ENOSPC ,"nonlinear_partial_err" ); +ASSERT_EQ (tattr.data_size_out,sizeof (pkt),"nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"nonlinear_partial_data_out" ); +} +void test_skb_load_bytes (void ) {struct skb_load_bytes *skel;
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c void test_skb_load_bytes(void) · line 77
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +2
-0@@ -40,6 +73,8 @@ void test_skb_load_bytes(void)if (!ASSERT_EQ (test_result,0 ,"offset 10" )) goto out; +test_nonlinear_data_out_partial (prog_fd); +
Maybe prog_tests/prog_run_opts.c would be a better place to cover this? test_skb_load_bytes() is meant to cover the bpf_skb_load_bytes helper.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +0
-0out:skb_load_bytes__destroy (skel); }Signature · 1 line
2.43.0
Re: [PATCH bpf 2/2] selftests/bpf: Cover partial copy of non-linear skb test_run output
Quoted 63 lines · Paul Chaignon
Quoted 61 lines · Sun Jian
Add a test case for
BPF_PROG_TEST_RUNwith a non-linear skb and a shortdata_outbuffer.The test verifies that
test_runreturns-ENOSPC, reports the full packet length throughdata_size_out, and copies the packet prefix intodata_out. The test uses a 100-bytedata_outbuffer with a 64-byte linear head, so the expected output spans both the skb head and the first fragment.Signed-off-by Sun Jian <[email protected]>--- .../selftests/bpf/prog_tests/skb_load_bytes.c | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+)tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +33
-0@@ -3,6 +3,39 @@#include <network_helpers.h> #include "skb_load_bytes.skel.h" +#define NONLINEAR_PKT_LEN 9000 +#define NONLINEAR_HEAD_LEN 64 +#define SHORT_OUT_LEN 100 + +static void test_nonlinear_data_out_partial (int prog_fd) +{ +LIBBPF_OPTS (bpf_test_run_opts, tattr); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct __sk_buff skb = {}; +int err, i; + +for (i =0 ; i <sizeof (pkt); i++) + pkt[i] = i &0xff ; + +memset (out,0xa5 ,sizeof (out)); + + skb.data_end =NONLINEAR_HEAD_LEN ; + + tattr.data_in = pkt; + tattr.data_size_in =sizeof (pkt); + tattr.data_out = out; + tattr.data_size_out =sizeof (out); + tattr.ctx_in = &skb; + tattr.ctx_size_in =sizeof (skb); + + err =bpf_prog_test_run_opts (prog_fd, &tattr); + +ASSERT_EQ (err, -ENOSPC ,"nonlinear_partial_err" ); +ASSERT_EQ (tattr.data_size_out,sizeof (pkt),"nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"nonlinear_partial_data_out" ); +} +void test_skb_load_bytes (void ) {struct skb_load_bytes *skel;@@ -40,6 +73,8 @@ void test_skb_load_bytes(void)if (!ASSERT_EQ (test_result,0 ,"offset 10" )) goto out;
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c void test_skb_load_bytes(void) · line 77
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +2
-0+test_nonlinear_data_out_partial (prog_fd); +Maybe
prog_tests/prog_run_opts.cwould be a better place to cover this?test_skb_load_bytes()is meant to cover thebpf_skb_load_byteshelper.Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c +0
-1out:skb_load_bytes__destroy (skel); } --2.43.0
Hi Paul,
Thanks, agreed. The test is really about BPF_PROG_TEST_RUN copy-out semantics, not the bpf_skb_load_bytes() helper.
I'll move it to prog_run_opts.c in v2.
Thanks,
Sun Jian