Skip to content

Commit 6e083ab

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-introduce-new-vfs-based-bpf-kfuncs'
Matt Bobrowski says: ==================== bpf: introduce new VFS based BPF kfuncs G'day! A respin based off v3, which can be found here [0]. Original motivations for introducing this suite of BPF kfuncs can be found here [1]. The primary difference in this version of the patch series is that the suite of VFS related BPF kfuncs added can be used from both sleepable and non-sleepable BPF LSM program types. IOW, the KF_SLEEPABLE annotation has been removed from all of them. Changes sinve v3: * KF_SLEEPABLE annotation has been dropped from all newly introduced VFS related BPF kfuncs. This includes bpf_get_task_exe_file(), bpf_put_file(), and bpf_path_d_path(). Both negative and positive selftests backing these new BPF kfuncs have also been updated accordingly. * buf__sz conditional in bpf_path_d_path() has been updated from buf__sz <= 0, to !buf__sz. * Syntax issues as reported so here [2] have been corrected. [0] https://lore.kernel.org/bpf/[email protected]/ [1] https://lore.kernel.org/bpf/[email protected]/#t [2] https://netdev.bots.linux.dev/static/nipa/874023/13742510/checkpatch/stdout ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 3d650ab + 2b399b9 commit 6e083ab

File tree

6 files changed

+400
-0
lines changed

6 files changed

+400
-0
lines changed

fs/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/
129129
obj-$(CONFIG_EROFS_FS) += erofs/
130130
obj-$(CONFIG_VBOXSF_FS) += vboxsf/
131131
obj-$(CONFIG_ZONEFS_FS) += zonefs/
132+
obj-$(CONFIG_BPF_LSM) += bpf_fs_kfuncs.o

fs/bpf_fs_kfuncs.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Google LLC. */
3+
4+
#include <linux/bpf.h>
5+
#include <linux/btf.h>
6+
#include <linux/btf_ids.h>
7+
#include <linux/dcache.h>
8+
#include <linux/fs.h>
9+
#include <linux/file.h>
10+
#include <linux/mm.h>
11+
12+
__bpf_kfunc_start_defs();
13+
14+
/**
15+
* bpf_get_task_exe_file - get a reference on the exe_file struct file member of
16+
* the mm_struct that is nested within the supplied
17+
* task_struct
18+
* @task: task_struct of which the nested mm_struct exe_file member to get a
19+
* reference on
20+
*
21+
* Get a reference on the exe_file struct file member field of the mm_struct
22+
* nested within the supplied *task*. The referenced file pointer acquired by
23+
* this BPF kfunc must be released using bpf_put_file(). Failing to call
24+
* bpf_put_file() on the returned referenced struct file pointer that has been
25+
* acquired by this BPF kfunc will result in the BPF program being rejected by
26+
* the BPF verifier.
27+
*
28+
* This BPF kfunc may only be called from BPF LSM programs.
29+
*
30+
* Internally, this BPF kfunc leans on get_task_exe_file(), such that calling
31+
* bpf_get_task_exe_file() would be analogous to calling get_task_exe_file()
32+
* directly in kernel context.
33+
*
34+
* Return: A referenced struct file pointer to the exe_file member of the
35+
* mm_struct that is nested within the supplied *task*. On error, NULL is
36+
* returned.
37+
*/
38+
__bpf_kfunc struct file *bpf_get_task_exe_file(struct task_struct *task)
39+
{
40+
return get_task_exe_file(task);
41+
}
42+
43+
/**
44+
* bpf_put_file - put a reference on the supplied file
45+
* @file: file to put a reference on
46+
*
47+
* Put a reference on the supplied *file*. Only referenced file pointers may be
48+
* passed to this BPF kfunc. Attempting to pass an unreferenced file pointer, or
49+
* any other arbitrary pointer for that matter, will result in the BPF program
50+
* being rejected by the BPF verifier.
51+
*
52+
* This BPF kfunc may only be called from BPF LSM programs.
53+
*/
54+
__bpf_kfunc void bpf_put_file(struct file *file)
55+
{
56+
fput(file);
57+
}
58+
59+
/**
60+
* bpf_path_d_path - resolve the pathname for the supplied path
61+
* @path: path to resolve the pathname for
62+
* @buf: buffer to return the resolved pathname in
63+
* @buf__sz: length of the supplied buffer
64+
*
65+
* Resolve the pathname for the supplied *path* and store it in *buf*. This BPF
66+
* kfunc is the safer variant of the legacy bpf_d_path() helper and should be
67+
* used in place of bpf_d_path() whenever possible. It enforces KF_TRUSTED_ARGS
68+
* semantics, meaning that the supplied *path* must itself hold a valid
69+
* reference, or else the BPF program will be outright rejected by the BPF
70+
* verifier.
71+
*
72+
* This BPF kfunc may only be called from BPF LSM programs.
73+
*
74+
* Return: A positive integer corresponding to the length of the resolved
75+
* pathname in *buf*, including the NUL termination character. On error, a
76+
* negative integer is returned.
77+
*/
78+
__bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)
79+
{
80+
int len;
81+
char *ret;
82+
83+
if (!buf__sz)
84+
return -EINVAL;
85+
86+
ret = d_path(path, buf, buf__sz);
87+
if (IS_ERR(ret))
88+
return PTR_ERR(ret);
89+
90+
len = buf + buf__sz - ret;
91+
memmove(buf, ret, len);
92+
return len;
93+
}
94+
95+
__bpf_kfunc_end_defs();
96+
97+
BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
98+
BTF_ID_FLAGS(func, bpf_get_task_exe_file,
99+
KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
100+
BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE)
101+
BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS)
102+
BTF_KFUNCS_END(bpf_fs_kfunc_set_ids)
103+
104+
static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
105+
{
106+
if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id) ||
107+
prog->type == BPF_PROG_TYPE_LSM)
108+
return 0;
109+
return -EACCES;
110+
}
111+
112+
static const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
113+
.owner = THIS_MODULE,
114+
.set = &bpf_fs_kfunc_set_ids,
115+
.filter = bpf_fs_kfuncs_filter,
116+
};
117+
118+
static int __init bpf_fs_kfuncs_init(void)
119+
{
120+
return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set);
121+
}
122+
123+
late_initcall(bpf_fs_kfuncs_init);

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,32 @@ extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym;
195195
*/
196196
extern void bpf_throw(u64 cookie) __ksym;
197197

198+
/* Description
199+
* Acquire a reference on the exe_file member field belonging to the
200+
* mm_struct that is nested within the supplied task_struct. The supplied
201+
* task_struct must be trusted/referenced.
202+
* Returns
203+
* A referenced file pointer pointing to the exe_file member field of the
204+
* mm_struct nested in the supplied task_struct, or NULL.
205+
*/
206+
extern struct file *bpf_get_task_exe_file(struct task_struct *task) __ksym;
207+
208+
/* Description
209+
* Release a reference on the supplied file. The supplied file must be
210+
* acquired.
211+
*/
212+
extern void bpf_put_file(struct file *file) __ksym;
213+
214+
/* Description
215+
* Resolve a pathname for the supplied path and store it in the supplied
216+
* buffer. The supplied path must be trusted/referenced.
217+
* Returns
218+
* A positive integer corresponding to the length of the resolved pathname,
219+
* including the NULL termination character, stored in the supplied
220+
* buffer. On error, a negative integer is returned.
221+
*/
222+
extern int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) __ksym;
223+
198224
/* This macro must be used to mark the exception callback corresponding to the
199225
* main program. For example:
200226
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
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"
89+
#include "verifier_vfs_reject.skel.h"
8890
#include "verifier_xadd.skel.h"
8991
#include "verifier_xdp.skel.h"
9092
#include "verifier_xdp_direct_packet_access.skel.h"
@@ -205,6 +207,8 @@ void test_verifier_value(void) { RUN(verifier_value); }
205207
void test_verifier_value_illegal_alu(void) { RUN(verifier_value_illegal_alu); }
206208
void test_verifier_value_or_null(void) { RUN(verifier_value_or_null); }
207209
void test_verifier_var_off(void) { RUN(verifier_var_off); }
210+
void test_verifier_vfs_accept(void) { RUN(verifier_vfs_accept); }
211+
void test_verifier_vfs_reject(void) { RUN(verifier_vfs_reject); }
208212
void test_verifier_xadd(void) { RUN(verifier_xadd); }
209213
void test_verifier_xdp(void) { RUN(verifier_xdp); }
210214
void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); }
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)