Skip to content

Commit 9b5c111

Browse files
ameryhungMartin KaFai Lau
authored andcommitted
selftests/bpf: Test changing packet data from global functions with a kfunc
The verifier should invalidate all packet pointers after a packet data changing kfunc is called. So, similar to commit 3f23ee5 ("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 <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent bc8712f commit 9b5c111

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

tools/testing/selftests/bpf/progs/verifier_sock.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Converted from tools/testing/selftests/bpf/verifier/sock.c */
33

4-
#include <linux/bpf.h>
4+
#include "vmlinux.h"
55
#include <bpf/bpf_helpers.h>
66
#include "bpf_misc.h"
77

@@ -1068,6 +1068,34 @@ int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk)
10681068
return TCX_PASS;
10691069
}
10701070

1071+
__noinline
1072+
long xdp_pull_data2(struct xdp_md *x, __u32 len)
1073+
{
1074+
return bpf_xdp_pull_data(x, len);
1075+
}
1076+
1077+
__noinline
1078+
long xdp_pull_data1(struct xdp_md *x, __u32 len)
1079+
{
1080+
return xdp_pull_data2(x, len);
1081+
}
1082+
1083+
/* global function calls bpf_xdp_pull_data(), which invalidates packet
1084+
* pointers established before global function call.
1085+
*/
1086+
SEC("xdp")
1087+
__failure __msg("invalid mem access")
1088+
int invalidate_xdp_pkt_pointers_from_global_func(struct xdp_md *x)
1089+
{
1090+
int *p = (void *)(long)x->data;
1091+
1092+
if ((void *)(p + 1) > (void *)(long)x->data_end)
1093+
return TCX_DROP;
1094+
xdp_pull_data1(x, 0);
1095+
*p = 42; /* this is unsafe */
1096+
return TCX_PASS;
1097+
}
1098+
10711099
__noinline
10721100
int tail_call(struct __sk_buff *sk)
10731101
{

0 commit comments

Comments
 (0)