bpf: test_run: Fix false -EBADMSG with BPF_F_TEST_SKB_CHECKSUM_COMPLETE
Subsystems (5)
v2 cover bpf: test_run: Fix false -EBADMSG with BPF_F_TEST_SKB_CHECKSUM_COMPLETE
BPF_PROG_TEST_RUN with BPF_F_TEST_SKB_CHECKSUM_COMPLETE returns -EBADMSG for correct tc programs that call bpf_skb_change_tail(). The helper moves the skb to CHECKSUM_NONE and leaves skb->csum stale, but the post-run check added by commit a3cfe84cca28 ("bpf: Add CHECKSUM_COMPLETE to bpf test progs") still compares against it.
Patch 1 only does the check while the skb is still CHECKSUM_COMPLETE. Patch 2 adds selftests for a trim, a grow followed by a write, and a negative case that keeps the skb CHECKSUM_COMPLETE with a stale skb->csum.
Tested in a VM on bpf, with two kernels that differ only by patch 1 (subtest results):
without patch 1 with patch 1 change_tail_trim FAIL (-EBADMSG) OK change_tail_grow_write FAIL (-EBADMSG) OK store_no_recompute OK OK test_skb_pkt_end OK OK
Changes in v2:
- Put the opening /* of multi-line comments on its own line (bpf-ci, Sashiko).
v1: https://lore.kernel.org/bpf/[email protected]/
Maxim Skokov (2):
bpf: test_run: Fix false -EBADMSG after skb leaves CHECKSUM_COMPLETE selftests/bpf: Cover test_run csum validation after
bpf_skb_change_tail()
net/bpf/test_run.c | 8 +++- .../bpf/prog_tests/skb_csum_complete.c | 39 ++++++++++++++++ .../selftests/bpf/progs/skb_csum_complete.c | 46 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_csum_complete.c create mode 100644 tools/testing/selftests/bpf/progs/skb_csum_complete.c
base-commit: ff47652a4b66c067c765a7ad464d930b5a9367cc
Signature · 1 line
2.47.3
PATCH v2 1/2 bpf: test_run: Fix false -EBADMSG after skb leaves CHECKSUM_COMPLETE
With BPF_F_TEST_SKB_CHECKSUM_COMPLETE, bpf_prog_test_run_skb() computes skb->csum over the packet from the network header to the end and marks the skb CHECKSUM_COMPLETE. After the program has run, it recomputes the checksum and returns -EBADMSG if it does not match skb->csum.
bpf_skb_change_tail() resizes the skb with __skb_trim_rcsum() and __skb_grow_rcsum(), which move a CHECKSUM_COMPLETE skb to CHECKSUM_NONE and leave skb->csum as it is. The post-run check still compares against that stale value, so a correct program fails with -EBADMSG. After a trim, skb->csum still covers the removed bytes. After a grow, later writes cannot update skb->csum, even with BPF_F_RECOMPUTE_CSUM, because skb->csum is only adjusted for CHECKSUM_COMPLETE skbs.
skb->csum only holds the packet checksum while ip_summed is CHECKSUM_COMPLETE. With CHECKSUM_NONE the stack does not rely on it and validates the packet in software, so there is nothing to compare. Only do the check if the skb is still CHECKSUM_COMPLETE after the run. A program that keeps the skb CHECKSUM_COMPLETE but leaves skb->csum stale still gets -EBADMSG.
Dropping the checksum offload is documented behaviour of bpf_skb_change_tail() ("implicitly linearizes, unclones and drops offloads"), and the stack does the same in other places, for example skb_forward_csum(). The test harness has to accept CHECKSUM_NONE either way, whether or not the helper is later taught to keep CHECKSUM_COMPLETE.
--- net/bpf/test_run.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
net/bpf/test_run.c +7 -1
@@ -1237,7 +1237,13 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,memset (__skb_push (skb, hh_len),0 , hh_len); } -if (kattr->test.flags &BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) { +/* +* A helper such as bpf_skb_change_tail() may have downgraded the skb +* from CHECKSUM_COMPLETE. skb->csum is then unused by the stack and +* there is nothing to validate. +*/ +if ((kattr->test.flags &BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) && + skb->ip_summed ==CHECKSUM_COMPLETE ) {const int off =skb_network_offset (skb);int len = skb->len - off;__wsum csum;
Signature · 1 line
2.47.3
PATCH v2 2/2 selftests/bpf: Cover test_run csum validation after bpf_skb_change_tail()
In reply to Maxim SkokovAdd BPF_F_TEST_SKB_CHECKSUM_COMPLETE tests for tc programs that move the skb out of CHECKSUM_COMPLETE with bpf_skb_change_tail():
- change_tail_trim trims the last 4 bytes of pkt_v4. They hold the non-zero tcp.urg_ptr, so the stale skb->csum no longer matches the packet.
- change_tail_grow_write appends 4 zero bytes, which on their own do not change the sum, and then writes a byte with BPF_F_RECOMPUTE_CSUM. That write can no longer update skb->csum.
Both return -EBADMSG without the preceding test_run fix and pass with it.
Also add store_no_recompute as a negative test. It writes a byte without BPF_F_RECOMPUTE_CSUM, so the skb stays CHECKSUM_COMPLETE with a stale skb->csum, and test_run must keep returning -EBADMSG. This checks that the validation still runs for CHECKSUM_COMPLETE skbs. The passing case, a CHECKSUM_COMPLETE skb whose checksum is updated correctly, is already covered by test_skb_pkt_end.
--- .../bpf/prog_tests/skb_csum_complete.c | 39 ++++++++++++++++ .../selftests/bpf/progs/skb_csum_complete.c | 46 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_csum_complete.c create mode 100644 tools/testing/selftests/bpf/progs/skb_csum_complete.c
tools/testing/selftests/bpf/prog_tests/skb_csum_complete.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <network_helpers.h> +#include "skb_csum_complete.skel.h" + +static void run (struct bpf_program *prog,int expected_err) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + .flags = BPF_F_TEST_SKB_CHECKSUM_COMPLETE, + ); +int err; + + err =bpf_prog_test_run_opts (bpf_program__fd (prog), &topts); +if (!ASSERT_EQ (err, expected_err,"test_run" )) +return ; +if (!expected_err) +ASSERT_EQ (topts.retval,0 ,"retval" ); +} + +void test_skb_csum_complete (void ) +{ +struct skb_csum_complete *skel; + + skel =skb_csum_complete__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) +return ; + +if (test__start_subtest ("change_tail_trim" )) +run (skel->progs.change_tail_trim,0 ); +if (test__start_subtest ("change_tail_grow_write" )) +run (skel->progs.change_tail_grow_write,0 ); +if (test__start_subtest ("store_no_recompute" )) +run (skel->progs.store_no_recompute, -EBADMSG ); + +skb_csum_complete__destroy (skel); +}
tools/testing/selftests/bpf/progs/skb_csum_complete.c new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.47.3
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 6 lines · On Fri, 9 Oct 2026 20:47:46 +0300 you wrote:
BPF_PROG_TEST_RUNwithBPF_F_TEST_SKB_CHECKSUM_COMPLETEreturns-EBADMSGfor correct tc programs that callbpf_skb_change_tail(). The helper moves the skb toCHECKSUM_NONEand leavesskb->csumstale, but the post-run check added by commita3cfe84cca28("bpf: AddCHECKSUM_COMPLETEto bpf test progs") still compares against it.
Quoted 6 lines
Patch 1 only does the check while the skb is still
CHECKSUM_COMPLETE. Patch 2 adds selftests for a trim, a grow followed by a write, and a negative case that keeps the skbCHECKSUM_COMPLETEwith a staleskb->csum.[...]
Here is the summary with links:
- [bpf,v2,1/2] bpf: test_run: Fix false -EBADMSG after skb leaves CHECKSUM_COMPLETE
https://git.kernel.org/bpf/bpf-next/c/36edb1c7738d - [bpf,v2,2/2] selftests/bpf: Cover test_run csum validation after bpf_skb_change_tail() https://git.kernel.org/bpf/bpf-next/c/2e6be13e8ca4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html