Skip to content

Commit 4a0aa8b

Browse files
eddyz87jfvogel
authored andcommitted
selftests/bpf: validate that tail call invalidates packet pointers
commit d9706b5 upstream. Add a test case with a tail call done from a global sub-program. Such tails calls should be considered as invalidating packet pointers. Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Shung-Hsi Yu <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 1062b7612cbd19ea25ccac9a29b25353b67141cc) Signed-off-by: Jack Vogel <[email protected]>
1 parent e5bb5bd commit 4a0aa8b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ struct {
5050
__uint(map_flags, BPF_F_NO_PREALLOC);
5151
} sk_storage_map SEC(".maps");
5252

53+
struct {
54+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
55+
__uint(max_entries, 1);
56+
__uint(key_size, sizeof(__u32));
57+
__uint(value_size, sizeof(__u32));
58+
} jmp_table SEC(".maps");
59+
5360
SEC("cgroup/skb")
5461
__description("skb->sk: no NULL check")
5562
__failure __msg("invalid mem access 'sock_common_or_null'")
@@ -1005,4 +1012,25 @@ int invalidate_pkt_pointers_from_global_func(struct __sk_buff *sk)
10051012
return TCX_PASS;
10061013
}
10071014

1015+
__noinline
1016+
int tail_call(struct __sk_buff *sk)
1017+
{
1018+
bpf_tail_call_static(sk, &jmp_table, 0);
1019+
return 0;
1020+
}
1021+
1022+
/* Tail calls invalidate packet pointers. */
1023+
SEC("tc")
1024+
__failure __msg("invalid mem access")
1025+
int invalidate_pkt_pointers_by_tail_call(struct __sk_buff *sk)
1026+
{
1027+
int *p = (void *)(long)sk->data;
1028+
1029+
if ((void *)(p + 1) > (void *)(long)sk->data_end)
1030+
return TCX_DROP;
1031+
tail_call(sk);
1032+
*p = 42; /* this is unsafe */
1033+
return TCX_PASS;
1034+
}
1035+
10081036
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)