Skip to content

Commit 2b399b9

Browse files
mattbobrowskiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: add positive tests for new VFS based BPF kfuncs
Add a bunch of positive selftests which extensively cover the various contexts and parameters in which the new VFS based BPF kfuncs may be used from. Again, the following VFS based BPF kfuncs are thoroughly tested within this new selftest: * struct file *bpf_get_task_exe_file(struct task_struct *); * void bpf_put_file(struct file *); * int bpf_path_d_path(struct path *, char *, size_t); Acked-by: Christian Brauner <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Matt Bobrowski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ff358ad commit 2b399b9

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

tools/testing/selftests/bpf/prog_tests/verifier.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include "verifier_value_or_null.skel.h"
8686
#include "verifier_value_ptr_arith.skel.h"
8787
#include "verifier_var_off.skel.h"
88+
#include "verifier_vfs_accept.skel.h"
8889
#include "verifier_vfs_reject.skel.h"
8990
#include "verifier_xadd.skel.h"
9091
#include "verifier_xdp.skel.h"
@@ -206,6 +207,7 @@ void test_verifier_value(void) { RUN(verifier_value); }
206207
void test_verifier_value_illegal_alu(void) { RUN(verifier_value_illegal_alu); }
207208
void test_verifier_value_or_null(void) { RUN(verifier_value_or_null); }
208209
void test_verifier_var_off(void) { RUN(verifier_var_off); }
210+
void test_verifier_vfs_accept(void) { RUN(verifier_vfs_accept); }
209211
void test_verifier_vfs_reject(void) { RUN(verifier_vfs_reject); }
210212
void test_verifier_xadd(void) { RUN(verifier_xadd); }
211213
void test_verifier_xdp(void) { RUN(verifier_xdp); }
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Google LLC. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
8+
#include "bpf_misc.h"
9+
#include "bpf_experimental.h"
10+
11+
static char buf[64];
12+
13+
SEC("lsm.s/file_open")
14+
__success
15+
int BPF_PROG(get_task_exe_file_and_put_kfunc_from_current_sleepable)
16+
{
17+
struct file *acquired;
18+
19+
acquired = bpf_get_task_exe_file(bpf_get_current_task_btf());
20+
if (!acquired)
21+
return 0;
22+
23+
bpf_put_file(acquired);
24+
return 0;
25+
}
26+
27+
SEC("lsm/file_open")
28+
__success
29+
int BPF_PROG(get_task_exe_file_and_put_kfunc_from_current_non_sleepable, struct file *file)
30+
{
31+
struct file *acquired;
32+
33+
acquired = bpf_get_task_exe_file(bpf_get_current_task_btf());
34+
if (!acquired)
35+
return 0;
36+
37+
bpf_put_file(acquired);
38+
return 0;
39+
}
40+
41+
SEC("lsm.s/task_alloc")
42+
__success
43+
int BPF_PROG(get_task_exe_file_and_put_kfunc_from_argument,
44+
struct task_struct *task)
45+
{
46+
struct file *acquired;
47+
48+
acquired = bpf_get_task_exe_file(task);
49+
if (!acquired)
50+
return 0;
51+
52+
bpf_put_file(acquired);
53+
return 0;
54+
}
55+
56+
SEC("lsm.s/inode_getattr")
57+
__success
58+
int BPF_PROG(path_d_path_from_path_argument, struct path *path)
59+
{
60+
int ret;
61+
62+
ret = bpf_path_d_path(path, buf, sizeof(buf));
63+
__sink(ret);
64+
return 0;
65+
}
66+
67+
SEC("lsm.s/file_open")
68+
__success
69+
int BPF_PROG(path_d_path_from_file_argument, struct file *file)
70+
{
71+
int ret;
72+
struct path *path;
73+
74+
/* The f_path member is a path which is embedded directly within a
75+
* file. Therefore, a pointer to such embedded members are still
76+
* recognized by the BPF verifier as being PTR_TRUSTED as it's
77+
* essentially PTR_TRUSTED w/ a non-zero fixed offset.
78+
*/
79+
path = &file->f_path;
80+
ret = bpf_path_d_path(path, buf, sizeof(buf));
81+
__sink(ret);
82+
return 0;
83+
}
84+
85+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)