Skip to content

Commit ff27581

Browse files
laoarKernel Patches Daemon
authored andcommitted
selftests/bpf: Add selftests for BTF_TYPE_SAFE_TRUSTED_UNION
Add selftests for BTF_TYPE_SAFE_TRUSTED_UNION, the result as follows: #141/1 nested_trust/test_read_cpumask:OK #141/2 nested_trust/test_skb_field:OK <<<< #141/3 nested_trust/test_invalid_nested_user_cpus:OK #141/4 nested_trust/test_invalid_nested_offset:OK #141/5 nested_trust/test_invalid_skb_field:OK <<<< #141 nested_trust:OK Signed-off-by: Yafang Shao <[email protected]>
1 parent 8e0beb6 commit ff27581

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
char _license[] SEC("license") = "GPL";
1212

13+
struct {
14+
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
15+
__uint(map_flags, BPF_F_NO_PREALLOC);
16+
__type(key, int);
17+
__type(value, u64);
18+
} sk_storage_map SEC(".maps");
19+
1320
/* Prototype for all of the program trace events below:
1421
*
1522
* TRACE_EVENT(task_newtask,
@@ -31,3 +38,12 @@ int BPF_PROG(test_invalid_nested_offset, struct task_struct *task, u64 clone_fla
3138
bpf_cpumask_first_zero(&task->cpus_mask);
3239
return 0;
3340
}
41+
42+
/* Although R2 is of type sk_buff but sock_common is expected, we will hit untrusted ptr first. */
43+
SEC("tp_btf/tcp_probe")
44+
__failure __msg("R2 type=untrusted_ptr_ expected=ptr_, trusted_ptr_, rcu_ptr_")
45+
int BPF_PROG(test_invalid_skb_field, struct sock *sk, struct sk_buff *skb)
46+
{
47+
bpf_sk_storage_get(&sk_storage_map, skb->next, 0, 0);
48+
return 0;
49+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@
1010

1111
char _license[] SEC("license") = "GPL";
1212

13+
struct {
14+
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
15+
__uint(map_flags, BPF_F_NO_PREALLOC);
16+
__type(key, int);
17+
__type(value, u64);
18+
} sk_storage_map SEC(".maps");
19+
1320
SEC("tp_btf/task_newtask")
1421
__success
1522
int BPF_PROG(test_read_cpumask, struct task_struct *task, u64 clone_flags)
1623
{
1724
bpf_cpumask_test_cpu(0, task->cpus_ptr);
1825
return 0;
1926
}
27+
28+
SEC("tp_btf/tcp_probe")
29+
__success
30+
int BPF_PROG(test_skb_field, struct sock *sk, struct sk_buff *skb)
31+
{
32+
bpf_sk_storage_get(&sk_storage_map, skb->sk, 0, 0);
33+
return 0;
34+
}

0 commit comments

Comments
 (0)