From: Maxim Skokov <skokovmaksimevg@gmail.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
yonghong.song@linux.dev, jolsa@kernel.org, memxor@gmail.com,
emil@etsalapatis.com, ihor.solodrai@linux.dev,
davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, shuah@kernel.org,
vadim.fedorenko@linux.dev, netdev@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Maxim Skokov <skokovmaksimevg@gmail.com>
Subject: [PATCH bpf 2/2] selftests/bpf: Cover test_run csum validation after bpf_skb_change_tail()
Date: Wed, 7 Oct 2026 21:12:42 +0300 [thread overview]
Message-ID: <20261007181249.352831-3-skokovmaksimevg@gmail.com> (raw)
In-Reply-To: <20261007181249.352831-1-skokovmaksimevg@gmail.com>
Add 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.
Assisted-by: LLM
Signed-off-by: Maxim Skokov <skokovmaksimevg@gmail.com>
---
.../bpf/prog_tests/skb_csum_complete.c | 39 ++++++++++++++++
.../selftests/bpf/progs/skb_csum_complete.c | 44 +++++++++++++++++++
2 files changed, 83 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
diff --git a/tools/testing/selftests/bpf/prog_tests/skb_csum_complete.c b/tools/testing/selftests/bpf/prog_tests/skb_csum_complete.c
new file mode 100644
index 0000000000000..919f24dbacd3d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/skb_csum_complete.c
@@ -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);
+}
diff --git a/tools/testing/selftests/bpf/progs/skb_csum_complete.c b/tools/testing/selftests/bpf/progs/skb_csum_complete.c
new file mode 100644
index 0000000000000..8e8d53530899a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/skb_csum_complete.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("tc")
+int change_tail_trim(struct __sk_buff *skb)
+{
+ /* Drops CHECKSUM_COMPLETE. skb->csum still covers the trimmed bytes,
+ * which hold the non-zero tcp.urg_ptr of pkt_v4, so it no longer
+ * matches the packet.
+ */
+ if (bpf_skb_change_tail(skb, skb->len - 4, 0))
+ return 1;
+ return 0;
+}
+
+SEC("tc")
+int change_tail_grow_write(struct __sk_buff *skb)
+{
+ __u8 val = 0xab;
+
+ /* Drops CHECKSUM_COMPLETE. The new bytes are zero and do not change
+ * the sum, but the write below can no longer update skb->csum.
+ */
+ if (bpf_skb_change_tail(skb, skb->len + 4, 0))
+ return 1;
+ if (bpf_skb_store_bytes(skb, skb->len - 1, &val, sizeof(val),
+ BPF_F_RECOMPUTE_CSUM))
+ return 2;
+ return 0;
+}
+
+SEC("tc")
+int store_no_recompute(struct __sk_buff *skb)
+{
+ __u8 val = 0xab;
+
+ /* skb stays CHECKSUM_COMPLETE, but skb->csum is not updated. */
+ if (bpf_skb_store_bytes(skb, skb->len - 1, &val, sizeof(val), 0))
+ return 1;
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.47.3
prev parent reply other threads:[~2026-10-07 18:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-07 18:12 [PATCH bpf 0/2] bpf: test_run: Fix false -EBADMSG with BPF_F_TEST_SKB_CHECKSUM_COMPLETE Maxim Skokov
2026-10-07 18:12 ` [PATCH bpf 1/2] bpf: test_run: Fix false -EBADMSG after skb leaves CHECKSUM_COMPLETE Maxim Skokov
2026-10-07 18:45 ` bot+bpf-ci
2026-10-08 17:17 ` Maxim Skokov
2026-10-07 18:12 ` Maxim Skokov [this message]
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=20261007181249.352831-3-skokovmaksimevg@gmail.com \
--to=skokovmaksimevg@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=vadim.fedorenko@linux.dev \
--cc=yonghong.song@linux.dev \
/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