Skip to content

Commit 3aecda0

Browse files
liu-song-6Kernel Patches Daemon
authored andcommitted
bpf: Add bpf_kern_path and bpf_path_put kfuncs
Add two new kfuncs to fs/bpf_fs_kfuncs.c that wrap kern_path() for use by BPF LSM programs: bpf_kern_path(): - Resolves a pathname string to a struct path - Allocates memory for the path structure - Returns NULL on error or if the path doesn't exist - Marked with KF_ACQUIRE | KF_SLEEPABLE | KF_RET_NULL bpf_path_put(): - Releases the path reference and frees the allocated memory - Marked with KF_RELEASE to enforce acquire/release semantics These kfuncs enable BPF LSM programs to resolve pathnames provided by hook arguments (e.g., dev_name from sb_mount) and validate or inspect the resolved paths. The verifier enforces proper resource management through acquire/release tracking. Example usage: struct path *p = bpf_kern_path("/etc/passwd", LOOKUP_FOLLOW); if (p) { // Use the path... bpf_path_put(p); // Must release } Signed-off-by: Song Liu <[email protected]>
1 parent 9d76009 commit 3aecda0

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

fs/bpf_fs_kfuncs.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/file.h>
1212
#include <linux/kernfs.h>
1313
#include <linux/mm.h>
14+
#include <linux/namei.h>
1415
#include <linux/xattr.h>
1516

1617
__bpf_kfunc_start_defs();
@@ -96,6 +97,61 @@ __bpf_kfunc int bpf_path_d_path(const struct path *path, char *buf, size_t buf__
9697
return len;
9798
}
9899

100+
/**
101+
* bpf_kern_path - resolve a pathname to a struct path
102+
* @pathname__str: pathname to resolve
103+
* @flags: lookup flags (e.g., LOOKUP_FOLLOW)
104+
*
105+
* Resolve the pathname for the supplied *pathname__str* and return a pointer
106+
* to a struct path. This is a wrapper around kern_path() that allocates and
107+
* returns a struct path pointer on success.
108+
*
109+
* The returned struct path pointer must be released using bpf_path_put().
110+
* Failing to call bpf_path_put() on the returned struct path pointer will
111+
* result in the BPF program being rejected by the BPF verifier.
112+
*
113+
* This BPF kfunc may only be called from BPF LSM programs.
114+
*
115+
* Return: A pointer to an allocated struct path on success, NULL on error.
116+
*/
117+
__bpf_kfunc struct path *bpf_kern_path(const char *pathname__str, unsigned int flags)
118+
{
119+
struct path *path;
120+
int ret;
121+
122+
path = kmalloc(sizeof(*path), GFP_KERNEL);
123+
if (!path)
124+
return NULL;
125+
126+
ret = kern_path(pathname__str, flags, path);
127+
if (ret) {
128+
kfree(path);
129+
return NULL;
130+
}
131+
132+
return path;
133+
}
134+
135+
/**
136+
* bpf_path_put - release a struct path reference
137+
* @path: struct path pointer to release
138+
*
139+
* Release the struct path pointer that was acquired by bpf_kern_path().
140+
* This BPF kfunc calls path_put() on the supplied *path* and then frees
141+
* the allocated memory.
142+
*
143+
* Only struct path pointers acquired by bpf_kern_path() may be passed to
144+
* this BPF kfunc. Attempting to pass any other pointer will result in the
145+
* BPF program being rejected by the BPF verifier.
146+
*
147+
* This BPF kfunc may only be called from BPF LSM programs.
148+
*/
149+
__bpf_kfunc void bpf_path_put(struct path *path)
150+
{
151+
path_put(path);
152+
kfree(path);
153+
}
154+
99155
static bool match_security_bpf_prefix(const char *name__str)
100156
{
101157
return !strncmp(name__str, XATTR_NAME_BPF_LSM, XATTR_NAME_BPF_LSM_LEN);
@@ -363,6 +419,8 @@ BTF_ID_FLAGS(func, bpf_get_task_exe_file,
363419
KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
364420
BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE)
365421
BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS)
422+
BTF_ID_FLAGS(func, bpf_kern_path, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_SLEEPABLE | KF_RET_NULL)
423+
BTF_ID_FLAGS(func, bpf_path_put, KF_RELEASE)
366424
BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
367425
BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
368426
BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)

0 commit comments

Comments
 (0)