Fix partial copy of non-linear test_run output
Subsystems (5)
v2 cover Fix partial copy of non-linear test_run output
When BPF_PROG_TEST_RUN returns non-linear output and userspace provides a short data_out buffer, bpf_test_finish() can return -ENOSPC before copying the packet prefix or updating data_size_out.
Fix this by deriving the linear copy length from the packet layout rather than from the already-clamped copy_size. Add selftest coverage for both non-linear skb and XDP frags paths.
---
Changes in v2:
* Fix the Fixes tag to point to the commit that introduced the shared non-linear copy-out logic.
* Drop skb-specific wording from the fix commit.
* Move the selftest from skb_load_bytes.c to prog_run_opts.c.
* Add XDP frags coverage in addition to non-linear skb coverage.
v1:
https://lore.kernel.org/bpf/[email protected]/
Tested with:
./test_progs -t prog_run_opts -v
./test_progs -t skb_load_bytes -v
./test_progs -t xdp_pull_data -v
Sun Jian (2):
bpf: Fix partial copy of non-linear test_run output selftests/bpf: Cover partial copy of non-linear test_run output
net/bpf/test_run.c | 11 ++- .../selftests/bpf/prog_tests/prog_run_opts.c | 72 +++++++++++++++++++ .../selftests/bpf/progs/test_pkt_access.c | 12 ++++ 3 files changed, 88 insertions(+), 7 deletions(-)
Range-diff:
1: 3691b07aa440 ! 1: e5a0c426d4cb bpf: Fix partial copy of non-linear skb test_run output
@@ Metadata
Author: Sun Jian <[email protected]>
## Commit message ##
- bpf: Fix partial copy of non-linear skb test_run output
+ bpf: Fix partial copy of non-linear 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.
+ For non-linear test_run output, bpf_test_finish() derives the linear
+ data copy length from copy_size - frag_size. This only matches the
+ linear data 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.
+ Compute the linear data length from the packet layout instead, and clamp
+ the linear 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.
- Fixes: 838baa351cee ("bpf: Craft non-linear skbs in BPF_PROG_TEST_RUN")
+ Fixes: 7855e0db150ad ("bpf: test_run: add xdp_shared_info pointer in bpf_test_finish signature")
## net/bpf/test_run.c ##
2: 663847520f0b < -: ------------ selftests/bpf: Cover partial copy of non-linear skb test_run output -: ------------ > 2: 680506532d97 selftests/bpf: Cover partial copy of non-linear test_run output
Signature · 1 line
2.43.0
PATCH v2 1/2 bpf: Fix partial copy of non-linear test_run output
For non-linear test_run output, bpf_test_finish() derives the linear data copy length from copy_size - frag_size. This only matches the linear data 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 data length from the packet layout instead, and clamp the linear 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 v2 1/2] bpf: Fix partial copy of non-linear test_run output
Quoted 35 lines · Sun Jian
For non-linear
test_runoutput,bpf_test_finish()derives the linear data copy length fromcopy_size-frag_size. This only matches the linear data 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.Compute the linear data length from the packet layout instead, and clamp the linear 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 7855e0db150ad ("bpf: test_run: add xdp_shared_info pointer in bpf_test_finish signature")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 +1
-6@@ -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;
net/bpf/test_run.c static int bpf_test_finish(const union bpf_attr *kattr, · line 465
Quoted 10 lines
net/bpf/test_run.c +3
-1+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;
That doesn't look needed.
Quoted 6 lines
net/bpf/test_run.c +0
-0for (i =0 ; i < sinfo->nr_frags; i++) {skb_frag_t *frag = &sinfo->frags[i];Signature · 1 line
2.43.0
Re: [PATCH bpf v2 1/2] bpf: Fix partial copy of non-linear test_run output
Quoted 42 lines · Paul Chaignon
Quoted 40 lines · Sun Jian
For non-linear
test_runoutput,bpf_test_finish()derives the linear data copy length fromcopy_size-frag_size. This only matches the linear data 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.Compute the linear data length from the packet layout instead, and clamp the linear 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 7855e0db150ad ("bpf: test_run: add xdp_shared_info pointer in bpf_test_finish signature")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 +2
-6@@ -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;
Quoted 14 lines
net/bpf/test_run.c static int bpf_test_finish(const union bpf_attr *kattr, · line 465
Quoted 5 lines
net/bpf/test_run.c +2
-1if (sinfo) { -int i, offset = len; +u32 offset = len;u32 data_len; +int i;That doesn't look needed.
Quoted 6 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 another look.
Agreed, I'll keep the fix patch minimal and leave offset as-is.
For the selftest patch, I'll try to reuse pkt_v4 and the existing TC program where possible, and keep only the minimal XDP frags program for the XDP case.
Thanks,
Sun Jian
PATCH v2 2/2 selftests/bpf: Cover partial copy of non-linear test_run output
In reply to Sun Jianprog_run_opts already verifies that BPF_PROG_TEST_RUN returns -ENOSPC for a short data_out buffer while still reporting the full output size through data_size_out.
Add the same coverage for non-linear test_run output. Use pass-through TC and XDP programs with a 9000-byte packet, a 64-byte linear data area, and a 100-byte data_out buffer. The expected output spans both the linear data and the first fragment.
Verify that test_run returns -ENOSPC, reports the full packet length through data_size_out, and copies the packet prefix into data_out for both non-linear skb and XDP frags paths.
--- .../selftests/bpf/prog_tests/prog_run_opts.c | 72 +++++++++++++++++++ .../selftests/bpf/progs/test_pkt_access.c | 12 ++++ 2 files changed, 84 insertions(+)
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +72 -0
@@ -4,6 +4,10 @@#include "test_pkt_access.skel.h" +#define NONLINEAR_PKT_LEN 9000 +#define NONLINEAR_LINEAR_DATA_LEN 64 +#define SHORT_OUT_LEN 100 +static const __u32 duration;static void check_run_cnt (int prog_fd,__u64 run_cnt)
@@ -20,6 +24,71 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt) "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt); } +static void init_pkt (__u8 *pkt,size_t len) +{ +size_t i; + +for (i =0 ; i < len; i++) + pkt[i] = i &0xff ; +} + +static void test_skb_nonlinear_data_out_partial (struct test_pkt_access *skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct __sk_buff skb = {}; +int prog_fd, err; + +init_pkt (pkt,sizeof (pkt)); +memset (out,0xa5 ,sizeof (out)); + + skb.data_end =NONLINEAR_LINEAR_DATA_LEN ; + + topts.data_in = pkt; + topts.data_size_in =sizeof (pkt); + topts.data_out = out; + topts.data_size_out =sizeof (out); + topts.ctx_in = &skb; + topts.ctx_size_in =sizeof (skb); + + prog_fd =bpf_program__fd (skel->progs.tc_pass_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); + +ASSERT_EQ (err, -ENOSPC ,"skb_nonlinear_partial_err" ); +ASSERT_EQ (topts.data_size_out,sizeof (pkt),"skb_nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"skb_nonlinear_partial_data_out" ); +} + +static void test_xdp_nonlinear_data_out_partial (struct test_pkt_access *skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct xdp_md ctx = {}; +int prog_fd, err; + +init_pkt (pkt,sizeof (pkt)); +memset (out,0xa5 ,sizeof (out)); + + ctx.data =0 ; + ctx.data_end =NONLINEAR_LINEAR_DATA_LEN ; + + topts.data_in = pkt; + topts.data_size_in =sizeof (pkt); + topts.data_out = out; + topts.data_size_out =sizeof (out); + topts.ctx_in = &ctx; + topts.ctx_size_in =sizeof (ctx); + + prog_fd =bpf_program__fd (skel->progs.xdp_frags_pass_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); + +ASSERT_EQ (err, -ENOSPC ,"xdp_nonlinear_partial_err" ); +ASSERT_EQ (topts.data_size_out,sizeof (pkt),"xdp_nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"xdp_nonlinear_partial_data_out" ); +} +void test_prog_run_opts (void ) {struct test_pkt_access *skel;
@@ -69,6 +138,9 @@ void test_prog_run_opts(void) run_cnt += topts.repeat;check_run_cnt (prog_fd, run_cnt); +test_skb_nonlinear_data_out_partial (skel); +test_xdp_nonlinear_data_out_partial (skel); + cleanup:if (skel)test_pkt_access__destroy (skel);
tools/testing/selftests/bpf/progs/test_pkt_access.c +12 -0
@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)return TC_ACT_UNSPEC ; } + +SEC("tc") +int tc_pass_prog (struct __sk_buff *skb) +{ +return TC_ACT_OK ; +} + +SEC("xdp.frags") +int xdp_frags_pass_prog (struct xdp_md *ctx) +{ +return XDP_PASS ; +}
Signature · 1 line
2.43.0
Re: [PATCH bpf v2 2/2] selftests/bpf: Cover partial copy of non-linear test_run output
Quoted 45 lines · Sun Jian
prog_run_optsalready verifies thatBPF_PROG_TEST_RUNreturns-ENOSPCfor a shortdata_outbuffer while still reporting the full output size throughdata_size_out.Add the same coverage for non-linear
test_runoutput. Use pass-through TC and XDP programs with a 9000-byte packet, a 64-byte linear data area, and a 100-bytedata_outbuffer. The expected output spans both the linear data and the first fragment.Verify that
test_runreturns-ENOSPC, reports the full packet length throughdata_size_out, and copies the packet prefix intodata_outfor both non-linear skb and XDP frags paths.Signed-off-by Sun Jian <[email protected]>--- .../selftests/bpf/prog_tests/prog_run_opts.c | 72 +++++++++++++++++++ .../selftests/bpf/progs/test_pkt_access.c | 12 ++++ 2 files changed, 84 insertions(+)tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +11
-0@@ -4,6 +4,10 @@#include "test_pkt_access.skel.h" +#define NONLINEAR_PKT_LEN 9000 +#define NONLINEAR_LINEAR_DATA_LEN 64 +#define SHORT_OUT_LEN 100 +static const __u32 duration;static void check_run_cnt (int prog_fd,__u64 run_cnt)@@ -20,6 +24,71 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt) "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt); } +static void init_pkt (__u8 *pkt,size_t len) +{ +size_t i; + +for (i =0 ; i < len; i++) + pkt[i] = i &0xff ; +}
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 43
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +10
-0+ +static void test_skb_nonlinear_data_out_partial (struct test_pkt_access *skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct __sk_buff skb = {}; +int prog_fd, err; + +init_pkt (pkt,sizeof (pkt));
Can't we reuse pkt_v4 by reducing the linear area to ETH_HLEN?
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 44
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +1
-0+memset (out,0xa5 ,sizeof (out));
Why is this needed?
Quoted 65 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +50
-0+ + skb.data_end =NONLINEAR_LINEAR_DATA_LEN ; + + topts.data_in = pkt; + topts.data_size_in =sizeof (pkt); + topts.data_out = out; + topts.data_size_out =sizeof (out); + topts.ctx_in = &skb; + topts.ctx_size_in =sizeof (skb); + + prog_fd =bpf_program__fd (skel->progs.tc_pass_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); + +ASSERT_EQ (err, -ENOSPC ,"skb_nonlinear_partial_err" ); +ASSERT_EQ (topts.data_size_out,sizeof (pkt),"skb_nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"skb_nonlinear_partial_data_out" ); +} + +static void test_xdp_nonlinear_data_out_partial (struct test_pkt_access *skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +__u8 pkt[NONLINEAR_PKT_LEN ]; +__u8 out[SHORT_OUT_LEN ]; +struct xdp_md ctx = {}; +int prog_fd, err; + +init_pkt (pkt,sizeof (pkt)); +memset (out,0xa5 ,sizeof (out)); + + ctx.data =0 ; + ctx.data_end =NONLINEAR_LINEAR_DATA_LEN ; + + topts.data_in = pkt; + topts.data_size_in =sizeof (pkt); + topts.data_out = out; + topts.data_size_out =sizeof (out); + topts.ctx_in = &ctx; + topts.ctx_size_in =sizeof (ctx); + + prog_fd =bpf_program__fd (skel->progs.xdp_frags_pass_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); + +ASSERT_EQ (err, -ENOSPC ,"xdp_nonlinear_partial_err" ); +ASSERT_EQ (topts.data_size_out,sizeof (pkt),"xdp_nonlinear_partial_data_size_out" ); +ASSERT_OK (memcmp (out, pkt,sizeof (out)),"xdp_nonlinear_partial_data_out" ); +} +void test_prog_run_opts (void ) {struct test_pkt_access *skel;@@ -69,6 +138,9 @@ void test_prog_run_opts(void) run_cnt += topts.repeat;check_run_cnt (prog_fd, run_cnt); +test_skb_nonlinear_data_out_partial (skel); +test_xdp_nonlinear_data_out_partial (skel); + cleanup:if (skel)test_pkt_access__destroy (skel);tools/testing/selftests/bpf/progs/test_pkt_access.c +0
-0
tools/testing/selftests/bpf/progs/test_pkt_access.c int test_pkt_access(struct __sk_buff *skb) · line 158
Quoted 9 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +6
-0@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)return TC_ACT_UNSPEC ; } + +SEC("tc") +int tc_pass_prog (struct __sk_buff *skb) +{ +return TC_ACT_OK ; +}
Once we're reusing pkt_v4, maybe we can also reuse the existing BPF program?
Quoted 9 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +6
-0+ +SEC("xdp.frags") +int xdp_frags_pass_prog (struct xdp_md *ctx) +{ +return XDP_PASS ; +}Signature · 1 line
2.43.0