]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/bpf: Test changing packet data from global functions with a kfunc
authorAmery Hung <ameryhung@gmail.com>
Thu, 25 Sep 2025 17:00:13 +0000 (10:00 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Thu, 25 Sep 2025 21:52:09 +0000 (14:52 -0700)
The verifier should invalidate all packet pointers after a packet data
changing kfunc is called. So, similar to commit 3f23ee5590d9
("selftests/bpf: test for changing packet data from global functions"),
test changing packet data from global functions to make sure packet
pointers are indeed invalidated.

Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250925170013.1752561-2-ameryhung@gmail.com
tools/testing/selftests/bpf/progs/verifier_sock.c

index bf88c644eb303885a4f00551c3a96e8b0dea8a4c..b4d31f10a9768723be8c46c3408c88a69b86aadd 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Converted from tools/testing/selftests/bpf/verifier/sock.c */
 
-#include <linux/bpf.h>
+#include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
 
@@ -1068,6 +1068,34 @@ int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk)
        return TCX_PASS;
 }
 
+__noinline
+long xdp_pull_data2(struct xdp_md *x, __u32 len)
+{
+       return bpf_xdp_pull_data(x, len);
+}
+
+__noinline
+long xdp_pull_data1(struct xdp_md *x, __u32 len)
+{
+       return xdp_pull_data2(x, len);
+}
+
+/* global function calls bpf_xdp_pull_data(), which invalidates packet
+ * pointers established before global function call.
+ */
+SEC("xdp")
+__failure __msg("invalid mem access")
+int invalidate_xdp_pkt_pointers_from_global_func(struct xdp_md *x)
+{
+       int *p = (void *)(long)x->data;
+
+       if ((void *)(p + 1) > (void *)(long)x->data_end)
+               return XDP_DROP;
+       xdp_pull_data1(x, 0);
+       *p = 42; /* this is unsafe */
+       return XDP_PASS;
+}
+
 __noinline
 int tail_call(struct __sk_buff *sk)
 {