Skip to content

Commit a6541f4

Browse files
Byte-LabAlexei Starovoitov
authored andcommitted
selftests/bpf: Add nested trust selftests suite
Now that defining trusted fields in a struct is supported, we should add selftests to verify the behavior. This patch adds a few such testcases. Signed-off-by: David Vernet <void@manifault.com> Link: https://lore.kernel.org/r/20230125143816.721952-4-void@manifault.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 516f4d3 commit a6541f4

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

tools/testing/selftests/bpf/DENYLIST.s390x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ map_kptr # failed to open_and_load program: -524
4444
modify_return # modify_return attach failed: -524 (trampoline)
4545
module_attach # skel_attach skeleton attach failed: -524 (trampoline)
4646
mptcp
47+
nested_trust # JIT does not support calling kernel function
4748
netcnt # failed to load BPF skeleton 'netcnt_prog': -7 (?)
4849
probe_user # check_kprobe_res wrong kprobe res from probe read (?)
4950
rcu_read_lock # failed to find kernel BTF type ID of '__x64_sys_getpgid': -3 (?)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include "nested_trust_failure.skel.h"
6+
#include "nested_trust_success.skel.h"
7+
8+
void test_nested_trust(void)
9+
{
10+
RUN_TESTS(nested_trust_success);
11+
RUN_TESTS(nested_trust_failure);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#ifndef _NESTED_TRUST_COMMON_H
5+
#define _NESTED_TRUST_COMMON_H
6+
7+
#include <stdbool.h>
8+
9+
bool bpf_cpumask_test_cpu(unsigned int cpu, const struct cpumask *cpumask) __ksym;
10+
bool bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym;
11+
12+
#endif /* _NESTED_TRUST_COMMON_H */
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include <bpf/bpf_helpers.h>
7+
#include "bpf_misc.h"
8+
9+
#include "nested_trust_common.h"
10+
11+
char _license[] SEC("license") = "GPL";
12+
13+
/* Prototype for all of the program trace events below:
14+
*
15+
* TRACE_EVENT(task_newtask,
16+
* TP_PROTO(struct task_struct *p, u64 clone_flags)
17+
*/
18+
19+
SEC("tp_btf/task_newtask")
20+
__failure __msg("R2 must be referenced or trusted")
21+
int BPF_PROG(test_invalid_nested_user_cpus, struct task_struct *task, u64 clone_flags)
22+
{
23+
bpf_cpumask_test_cpu(0, task->user_cpus_ptr);
24+
return 0;
25+
}
26+
27+
SEC("tp_btf/task_newtask")
28+
__failure __msg("R1 must have zero offset when passed to release func or trusted arg to kfunc")
29+
int BPF_PROG(test_invalid_nested_offset, struct task_struct *task, u64 clone_flags)
30+
{
31+
bpf_cpumask_first_zero(&task->cpus_mask);
32+
return 0;
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include <bpf/bpf_helpers.h>
7+
#include "bpf_misc.h"
8+
9+
#include "nested_trust_common.h"
10+
11+
char _license[] SEC("license") = "GPL";
12+
13+
SEC("tp_btf/task_newtask")
14+
__success
15+
int BPF_PROG(test_read_cpumask, struct task_struct *task, u64 clone_flags)
16+
{
17+
bpf_cpumask_test_cpu(0, task->cpus_ptr);
18+
return 0;
19+
}

0 commit comments

Comments
 (0)