Introduce bpf_kern_path and bpf_path_put
Subsystems (6)
Song Liu
reviewercover Introduce bpf_kern_path and bpf_path_put
Security solutions use LSM hook security_sb_mount to monitor mount operations. security_sb_mount takes dev_name as a string. To get a struct path from dev_name, in-tree LSMs use kern_path. Introduce kfuncs bpf_kern_path so that bpf LSM can do similar operations. bpf_kern_path takes a reference on the return value path. Also add kfunc bpf_path_put to release path returned by bpf_kern_path. Note that, bpf_kern_path only holds reference on the path during the duration of this bpf program. The verifier enforces the bpf program release this reference.
Patch 1/3 prepares bpf verifier to handle const char * passed in as hook argument. Before this change, bpf helpers and kfuncs only consider value from read only map as const string.
Patch 2/3 adds the two kfuncs.
Patch 3/3 add tests for the new kfuncs.
Song Liu (3):
bpf: Allow const char * from LSM hooks as kfunc const string arguments
bpf: Add bpf_kern_path and bpf_path_put kfuncs selftests/bpf: Add tests for bpf_kern_path kfunc
fs/bpf_fs_kfuncs.c | 58 +++++++++++ include/linux/btf.h | 1 + kernel/bpf/btf.c | 33 +++++++ kernel/bpf/verifier.c | 51 +++++++--- .../testing/selftests/bpf/bpf_experimental.h | 4 + .../selftests/bpf/prog_tests/kern_path.c | 82 ++++++++++++++++ .../selftests/bpf/progs/test_kern_path.c | 56 +++++++++++ .../selftests/bpf/progs/verifier_kern_path.c | 52 ++++++++++ .../bpf/progs/verifier_kern_path_fail.c | 97 +++++++++++++++++++ 9 files changed, 422 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/test_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path_fail.c
--
2.47.3
Song Liu
reviewerPATCH 1/3 bpf: Allow const char * from LSM hooks as kfunc const string arguments
Let the BPF verifier to recognize const char * arguments from LSM hooks (and other BPF program types) as valid const string pointers that can be passed to kfuncs expecting KF_ARG_PTR_TO_CONST_STR.
Previously, kfuncs with KF_ARG_PTR_TO_CONST_STR only accepted PTR_TO_MAP_VALUE from readonly maps. This was limiting for LSM programs that receive const char * arguments from hooks like sb_mount's dev_name.
--- include/linux/btf.h | 1 + kernel/bpf/btf.c | 33 ++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 51 +++++++++++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 12 deletions(-)
include/linux/btf.h +1 -0
@@ -224,6 +224,7 @@ struct btf *btf_base_btf(const struct btf *btf);bool btf_type_is_i32 (const struct btf_type *t);bool btf_type_is_i64 (const struct btf_type *t);bool btf_type_is_primitive (const struct btf_type *t); +bool btf_type_is_const_char_ptr (const struct btf *btf,const struct btf_type *t);bool btf_member_is_reg_int (const struct btf *btf,const struct btf_type *s,const struct btf_member *m,u32 expected_offset,u32 expected_size);
kernel/bpf/btf.c +33 -0
@@ -897,6 +897,25 @@ bool btf_type_is_primitive(const struct btf_type *t)btf_is_any_enum (t); } +bool btf_type_is_const_char_ptr (const struct btf *btf,const struct btf_type *t) +{ +const char *tname; + +/* The type chain has to be PTR->CONST->CHAR */ +if (BTF_INFO_KIND (t->info) !=BTF_KIND_PTR ) +return false; + + t =btf_type_by_id (btf, t->type); +if (BTF_INFO_KIND (t->info) !=BTF_KIND_CONST ) +return false; + + t =btf_type_by_id (btf, t->type); + tname =btf_name_by_offset (btf, t->name_off); +if (tname &&strcmp (tname,"char" ) ==0 ) +return true; +return false; +} +/* * Check that given struct member is a regular int with expected * offset and size.
@@ -6746,6 +6765,20 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* Default prog with MAX_BPF_FUNC_REG_ARGS args */ return true; t =btf_type_by_id (btf, args[arg].type); + +/* +* For const string, we need to match "const char *" +* exactly. Therefore, do the check before the skipping +* modifiers. +*/ +if (btf_type_is_const_char_ptr (btf, t)) { + info->reg_type =PTR_TO_BTF_ID ; +if (prog_args_trusted (prog)) + info->reg_type |=PTR_TRUSTED ; + info->btf = btf; + info->btf_id = args[arg].type; +return true; + } }/* skip modifiers */
kernel/bpf/verifier.c +39 -12
82 lines not on this page · raw message
Signature · 1 line
2.47.3
Song Liu
reviewerPATCH 2/3 bpf: Add bpf_kern_path and bpf_path_put kfuncs
In reply to Song LiuAdd 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
}
--- fs/bpf_fs_kfuncs.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
fs/bpf_fs_kfuncs.c +58 -0
@@ -11,6 +11,7 @@#include <linux/file.h> #include <linux/kernfs.h> #include <linux/mm.h> +#include <linux/namei.h> #include <linux/xattr.h> __bpf_kfunc_start_defs ();
@@ -96,6 +97,61 @@ __bpf_kfunc int bpf_path_d_path(const struct path *path, char *buf, size_t buf__return len; } +/** +* bpf_kern_path - resolve a pathname to a struct path +* @pathname__str: pathname to resolve +* @flags: lookup flags (e.g., LOOKUP_FOLLOW) +* +* Resolve the pathname for the supplied *pathname__str* and return a pointer +* to a struct path. This is a wrapper around kern_path() that allocates and +* returns a struct path pointer on success. +* +* The returned struct path pointer must be released using bpf_path_put(). +* Failing to call bpf_path_put() on the returned struct path pointer will +* result in the BPF program being rejected by the BPF verifier. +* +* This BPF kfunc may only be called from BPF LSM programs. +* +* Return: A pointer to an allocated struct path on success, NULL on error. +*/ +__bpf_kfunc struct path *bpf_kern_path(const char *pathname__str, unsigned int flags) +{ +struct path *path; +int ret; + + path =kmalloc (sizeof (*path),GFP_KERNEL ); +if (!path) +return NULL ; + + ret =kern_path (pathname__str, flags, path); +if (ret) { +kfree (path); +return NULL ; + } + +return path; +} + +/** +* bpf_path_put - release a struct path reference +* @path: struct path pointer to release +* +* Release the struct path pointer that was acquired by bpf_kern_path(). +* This BPF kfunc calls path_put() on the supplied *path* and then frees +* the allocated memory. +* +* Only struct path pointers acquired by bpf_kern_path() may be passed to +* this BPF kfunc. Attempting to pass any other pointer will result in the +* BPF program being rejected by the BPF verifier. +* +* This BPF kfunc may only be called from BPF LSM programs. +*/ +__bpf_kfunc void bpf_path_put(struct path *path) +{ +path_put (path); +kfree (path); +} +static bool match_security_bpf_prefix (const char *name__str) {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, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_kern_path, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_SLEEPABLE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_path_put, KF_RELEASE) BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)BTF_ID_FLAGS (func, bpf_set_dentry_xattr,KF_SLEEPABLE |KF_TRUSTED_ARGS )
Signature · 1 line
2.47.3
Song Liu
reviewerPATCH 3/3 selftests/bpf: Add tests for bpf_kern_path kfunc
In reply to Song LiuAdd comprehensive selftests for the new bpf_kern_path and bpf_path_put kfuncs:
1. Functional tests (prog_tests/kern_path.c, progs/test_kern_path.c):
- test_kern_path_basic: Tests successful path resolution using /proc/self/exe and validates the resolved path with bpf_path_d_path
- test_kern_path_sb_mount: Tests bpf_kern_path with dynamic input from LSM hook parameter (dev_name from sb_mount), demonstrating real-world usage where BPF programs resolve paths from hook args
2. Verifier success tests (progs/verifier_kern_path.c):
- kern_path_success: Proper acquire -> use -> release pattern
- kern_path_multiple_paths: Multiple concurrent path acquisitions
3. Verifier failure tests (progs/verifier_kern_path_fail.c):
- kern_path_unreleased: Resource leak detection
- path_put_unacquired: Releasing unacquired path
- path_use_after_put: Use-after-free detection
- double_path_put: Double-free detection
- kern_path_non_lsm: Program type restrictions (LSM only)
- kern_path_non_const_str: reject none const string
These tests verify both the functionality of the kfuncs and that the verifier properly enforces acquire/release semantics to prevent resource leaks.
--- .../testing/selftests/bpf/bpf_experimental.h | 4 + .../selftests/bpf/prog_tests/kern_path.c | 82 ++++++++++++++++ .../selftests/bpf/progs/test_kern_path.c | 56 +++++++++++ .../selftests/bpf/progs/verifier_kern_path.c | 52 ++++++++++ .../bpf/progs/verifier_kern_path_fail.c | 97 +++++++++++++++++++ 5 files changed, 291 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/test_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path_fail.c
tools/testing/selftests/bpf/bpf_experimental.h +4 -0
@@ -221,6 +221,10 @@ extern void bpf_put_file(struct file *file) __ksym;*/ extern int bpf_path_d_path(const struct path *path, char *buf, size_t buf__sz) __ksym; +extern struct path *bpf_kern_path (const char *pathname,unsigned int flags) __ksym; +extern void bpf_path_put(struct path *path) __ksym; +extern int bpf_path_d_path(const struct path *path, char *buf, size_t buf__sz) __ksym; +/* This macro must be used to mark the exception callback corresponding to the * main program. For example: *
tools/testing/selftests/bpf/prog_tests/kern_path.c new file +82 -0
82 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_kern_path.c new file +56 -0
56 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_kern_path.c new file +52 -0
52 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_kern_path_fail.c new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.47.3
Song Liu
reviewercover Introduce bpf_kern_path and bpf_path_put
In reply to Song LiuSecurity solutions use LSM hook security_sb_mount to monitor mount operations. security_sb_mount takes dev_name as a string. To get a struct path from dev_name, in-tree LSMs use kern_path. Introduce kfuncs bpf_kern_path so that bpf LSM can do similar operations. bpf_kern_path takes a reference on the return value path. Also add kfunc bpf_path_put to release path returned by bpf_kern_path. Note that, bpf_kern_path only holds reference on the path during the duration of this bpf program. The verifier enforces the bpf program release this reference.
Patch 1/3 prepares bpf verifier to handle const char * passed in as hook argument. Before this change, bpf helpers and kfuncs only consider value from read only map as const string.
Patch 2/3 adds the two kfuncs.
Patch 3/3 add tests for the new kfuncs.
Song Liu (3):
bpf: Allow const char * from LSM hooks as kfunc const string arguments
bpf: Add bpf_kern_path and bpf_path_put kfuncs selftests/bpf: Add tests for bpf_kern_path kfunc
fs/bpf_fs_kfuncs.c | 58 +++++++++++ include/linux/btf.h | 1 + kernel/bpf/btf.c | 33 +++++++ kernel/bpf/verifier.c | 51 +++++++--- .../testing/selftests/bpf/bpf_experimental.h | 4 + .../selftests/bpf/prog_tests/kern_path.c | 82 ++++++++++++++++ .../selftests/bpf/progs/test_kern_path.c | 56 +++++++++++ .../selftests/bpf/progs/verifier_kern_path.c | 52 ++++++++++ .../bpf/progs/verifier_kern_path_fail.c | 97 +++++++++++++++++++ 9 files changed, 422 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/test_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path_fail.c
--
2.47.3
Song Liu
reviewerPATCH 1/3 bpf: Allow const char * from LSM hooks as kfunc const string arguments
In reply to Song LiuLet the BPF verifier to recognize const char * arguments from LSM hooks (and other BPF program types) as valid const string pointers that can be passed to kfuncs expecting KF_ARG_PTR_TO_CONST_STR.
Previously, kfuncs with KF_ARG_PTR_TO_CONST_STR only accepted PTR_TO_MAP_VALUE from readonly maps. This was limiting for LSM programs that receive const char * arguments from hooks like sb_mount's dev_name.
--- include/linux/btf.h | 1 + kernel/bpf/btf.c | 33 ++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 51 +++++++++++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 12 deletions(-)
include/linux/btf.h +1 -0
@@ -224,6 +224,7 @@ struct btf *btf_base_btf(const struct btf *btf);bool btf_type_is_i32 (const struct btf_type *t);bool btf_type_is_i64 (const struct btf_type *t);bool btf_type_is_primitive (const struct btf_type *t); +bool btf_type_is_const_char_ptr (const struct btf *btf,const struct btf_type *t);bool btf_member_is_reg_int (const struct btf *btf,const struct btf_type *s,const struct btf_member *m,u32 expected_offset,u32 expected_size);
kernel/bpf/btf.c +33 -0
@@ -897,6 +897,25 @@ bool btf_type_is_primitive(const struct btf_type *t)btf_is_any_enum (t); } +bool btf_type_is_const_char_ptr (const struct btf *btf,const struct btf_type *t) +{ +const char *tname; + +/* The type chain has to be PTR->CONST->CHAR */ +if (BTF_INFO_KIND (t->info) !=BTF_KIND_PTR ) +return false; + + t =btf_type_by_id (btf, t->type); +if (BTF_INFO_KIND (t->info) !=BTF_KIND_CONST ) +return false; + + t =btf_type_by_id (btf, t->type); + tname =btf_name_by_offset (btf, t->name_off); +if (tname &&strcmp (tname,"char" ) ==0 ) +return true; +return false; +} +/* * Check that given struct member is a regular int with expected * offset and size.
@@ -6746,6 +6765,20 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* Default prog with MAX_BPF_FUNC_REG_ARGS args */ return true; t =btf_type_by_id (btf, args[arg].type); + +/* +* For const string, we need to match "const char *" +* exactly. Therefore, do the check before the skipping +* modifiers. +*/ +if (btf_type_is_const_char_ptr (btf, t)) { + info->reg_type =PTR_TO_BTF_ID ; +if (prog_args_trusted (prog)) + info->reg_type |=PTR_TRUSTED ; + info->btf = btf; + info->btf_id = args[arg].type; +return true; + } }/* skip modifiers */
kernel/bpf/verifier.c +39 -12
82 lines not on this page · raw message
Signature · 1 line
2.47.3
Song Liu
reviewerPATCH 2/3 bpf: Add bpf_kern_path and bpf_path_put kfuncs
In reply to Song LiuAdd 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
}
--- fs/bpf_fs_kfuncs.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
fs/bpf_fs_kfuncs.c +58 -0
@@ -11,6 +11,7 @@#include <linux/file.h> #include <linux/kernfs.h> #include <linux/mm.h> +#include <linux/namei.h> #include <linux/xattr.h> __bpf_kfunc_start_defs ();
@@ -96,6 +97,61 @@ __bpf_kfunc int bpf_path_d_path(const struct path *path, char *buf, size_t buf__return len; } +/** +* bpf_kern_path - resolve a pathname to a struct path +* @pathname__str: pathname to resolve +* @flags: lookup flags (e.g., LOOKUP_FOLLOW) +* +* Resolve the pathname for the supplied *pathname__str* and return a pointer +* to a struct path. This is a wrapper around kern_path() that allocates and +* returns a struct path pointer on success. +* +* The returned struct path pointer must be released using bpf_path_put(). +* Failing to call bpf_path_put() on the returned struct path pointer will +* result in the BPF program being rejected by the BPF verifier. +* +* This BPF kfunc may only be called from BPF LSM programs. +* +* Return: A pointer to an allocated struct path on success, NULL on error. +*/ +__bpf_kfunc struct path *bpf_kern_path(const char *pathname__str, unsigned int flags) +{ +struct path *path; +int ret; + + path =kmalloc (sizeof (*path),GFP_KERNEL ); +if (!path) +return NULL ; + + ret =kern_path (pathname__str, flags, path); +if (ret) { +kfree (path); +return NULL ; + } + +return path; +} + +/** +* bpf_path_put - release a struct path reference +* @path: struct path pointer to release +* +* Release the struct path pointer that was acquired by bpf_kern_path(). +* This BPF kfunc calls path_put() on the supplied *path* and then frees +* the allocated memory. +* +* Only struct path pointers acquired by bpf_kern_path() may be passed to +* this BPF kfunc. Attempting to pass any other pointer will result in the +* BPF program being rejected by the BPF verifier. +* +* This BPF kfunc may only be called from BPF LSM programs. +*/ +__bpf_kfunc void bpf_path_put(struct path *path) +{ +path_put (path); +kfree (path); +} +static bool match_security_bpf_prefix (const char *name__str) {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, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_kern_path, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_SLEEPABLE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_path_put, KF_RELEASE) BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)BTF_ID_FLAGS (func, bpf_set_dentry_xattr,KF_SLEEPABLE |KF_TRUSTED_ARGS )
Signature · 1 line
2.47.3
Song Liu
reviewerPATCH 3/3 selftests/bpf: Add tests for bpf_kern_path kfunc
In reply to Song LiuAdd comprehensive selftests for the new bpf_kern_path and bpf_path_put kfuncs:
1. Functional tests (prog_tests/kern_path.c, progs/test_kern_path.c):
- test_kern_path_basic: Tests successful path resolution using /proc/self/exe and validates the resolved path with bpf_path_d_path
- test_kern_path_sb_mount: Tests bpf_kern_path with dynamic input from LSM hook parameter (dev_name from sb_mount), demonstrating real-world usage where BPF programs resolve paths from hook args
2. Verifier success tests (progs/verifier_kern_path.c):
- kern_path_success: Proper acquire -> use -> release pattern
- kern_path_multiple_paths: Multiple concurrent path acquisitions
3. Verifier failure tests (progs/verifier_kern_path_fail.c):
- kern_path_unreleased: Resource leak detection
- path_put_unacquired: Releasing unacquired path
- path_use_after_put: Use-after-free detection
- double_path_put: Double-free detection
- kern_path_non_lsm: Program type restrictions (LSM only)
- kern_path_non_const_str: reject none const string
These tests verify both the functionality of the kfuncs and that the verifier properly enforces acquire/release semantics to prevent resource leaks.
--- .../testing/selftests/bpf/bpf_experimental.h | 4 + .../selftests/bpf/prog_tests/kern_path.c | 82 ++++++++++++++++ .../selftests/bpf/progs/test_kern_path.c | 56 +++++++++++ .../selftests/bpf/progs/verifier_kern_path.c | 52 ++++++++++ .../bpf/progs/verifier_kern_path_fail.c | 97 +++++++++++++++++++ 5 files changed, 291 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/test_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kern_path_fail.c
tools/testing/selftests/bpf/bpf_experimental.h +4 -0
@@ -221,6 +221,10 @@ extern void bpf_put_file(struct file *file) __ksym;*/ extern int bpf_path_d_path(const struct path *path, char *buf, size_t buf__sz) __ksym; +extern struct path *bpf_kern_path (const char *pathname,unsigned int flags) __ksym; +extern void bpf_path_put(struct path *path) __ksym; +extern int bpf_path_d_path(const struct path *path, char *buf, size_t buf__sz) __ksym; +/* This macro must be used to mark the exception callback corresponding to the * main program. For example: *
tools/testing/selftests/bpf/prog_tests/kern_path.c new file +82 -0
82 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_kern_path.c new file +56 -0
56 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_kern_path.c new file +52 -0
52 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_kern_path_fail.c new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.47.3
kernel test robot
build botRe: [PATCH bpf-next 1/3] bpf: Allow const char * from LSM hooks as kfunc const string arguments
In reply to Song Liukernel test robot noticed the following build warnings:
Hi Song,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Song-Liu/bpf-Allow-const-char-from-LSM-hooks-as-kfunc-const-string-arguments/20251127-125352 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20251127005011.1872209-6-song%40kernel.org patch subject: [PATCH bpf-next 1/3] bpf: Allow const char * from LSM hooks as kfunc const string arguments config: loongarch-randconfig-001-20251127 (https://download.01.org/0day-ci/archive/20251128/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project b3428bb966f1de8aa48375ffee0eba04ede133b7) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251128/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/bpf/verifier.c:9655:14: warning: unused variable 'tname' [-Wunused-variable]
9655 | const char *tname;
| ^~~~~1 warning generated.
vim +/tname +9655 kernel/bpf/verifier.c
9649
9650 /* Check for const string passed in as input to the bpf program. */
9651 static int check_reg_const_str_arg(struct bpf_reg_state *reg)
9652 {
9653 const struct btf *btf;
9654 const struct btf_type *t;
> 9655 const char *tname;
9656
9657 if (base_type(reg->type) != PTR_TO_BTF_ID)
9658 return -EINVAL;
9659
9660 btf = reg->btf;
9661 t = btf_type_by_id(btf, reg->btf_id);
9662 if (!t)
9663 return -EINVAL;
9664
9665 if (btf_type_is_const_char_ptr(btf, t))
9666 return 0;
9667 return -EINVAL;
9668 }
9669
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Al Viro
maintainerRe: [PATCH bpf-next 2/3] bpf: Add bpf_kern_path and bpf_path_put kfuncs
In reply to Song LiuQuoted 10 lines · Song Liu
Add two new kfuncs to
fs/bpf_fs_kfuncs.cthat wrapkern_path()for use by BPF LSM programs:
bpf_kern_path():
- Resolves a pathname string to a struct pathThese kfuncs enable BPF LSM programs to resolve pathnames provided by hook arguments (e.g.,
dev_namefromsb_mount) and validate or inspect the resolved paths. The verifier enforces proper resource management through acquire/release tracking.
Oh, *brilliant*. Thank you for giving a wonderful example of the reasons why this is fundamentally worthless.
OK, your "BPF LSM" has been called and it got that dev_name. You decide that you want to know what it resolves to (which, BTW, requries a really non-trivial amount of parsing other arguments - just to figure out whether it *is* a pathname of some sort). Thanks to your shiny new kfuncs you can do that! You are a proud holder of mount/dentry pair. You stare at those and decide whether it's OK to go on. Then you... drop that pair and let mount(2) proceed towards the point where it will (if you parsed the arguments correctly) repeat that pathname resolution and get a mount/dentry pair of its own, that may very well be different from what you've got the first time around.
Your primitive is a walking TOCTOU bug - it's impossible to use safely.
Song Liu
reviewerRe: [PATCH bpf-next 2/3] bpf: Add bpf_kern_path and bpf_path_put kfuncs
Quoted 27 lines · Al Viro
Quoted 10 lines · Song Liu
Add two new kfuncs to
fs/bpf_fs_kfuncs.cthat wrapkern_path()for use by BPF LSM programs:
bpf_kern_path():
- Resolves a pathname string to a struct pathThese kfuncs enable BPF LSM programs to resolve pathnames provided by hook arguments (e.g.,
dev_namefromsb_mount) and validate or inspect the resolved paths. The verifier enforces proper resource management through acquire/release tracking.Oh, *brilliant*. Thank you for giving a wonderful example of the reasons why this is fundamentally worthless.
OK, your "BPF LSM" has been called and it got that
dev_name. You decide that you want to know what it resolves to (which, BTW, requries a really non-trivial amount of parsing other arguments - just to figure out whether it *is* a pathname of some sort). Thanks to your shiny new kfuncs you can do that! You are a proud holder of mount/dentry pair. You stare at those and decide whether it's OK to go on. Then you... drop that pair and let mount(2) proceed towards the point where it will (if you parsed the arguments correctly) repeat that pathname resolution and get a mount/dentry pair of its own, that may very well be different from what you've got the first time around.
Quoted 1 line
Your primitive is a walking TOCTOU bug - it's impossible to use safely.
Good point. AFAICT, the sample TOCTOU bug applies to other LSMs that care about dev_name in sb_mount, namely, aa_bind_mount() for apparmor and tomoyo_mount_acl() for tomoyo.
What would you recommend to do this properly? How about we add a new LSM hook that works on the actual mount/dentry pair? Something like:
fs/namespace.c +0 -0
@@ -2984,6 +2984,10 @@ static int do_loopback(const struct path *path,
const char *old_name,
if (err)
return err;+ err = security_mount_loopback(old_path, path, recurse);
+ if (err)
+ return err;
+
if (mnt_ns_loop(old_path.dentry))
return -EINVAL;(Or s/security_mount_loopback/some_other_name).
In other words, do you think we should go [1] by Shervin Oloumi? CCing Shervin here.
We will also need something similar to cover move mount operation via path_mount()=>do_move_mount_old()=>do_move_mount().
Thanks,
Song
[1] https://lore.kernel.org/linux-security-module/[email protected]/
Al Viro
maintainerRe: [PATCH bpf-next 2/3] bpf: Add bpf_kern_path and bpf_path_put kfuncs
Quoted 5 lines · Song Liu
Quoted 1 line
Your primitive is a walking TOCTOU bug - it's impossible to use safely.
Good point. AFAICT, the sample TOCTOU bug applies to other LSMs that care about
dev_nameinsb_mount, namely,aa_bind_mount()for apparmor andtomoyo_mount_acl()for tomoyo.
sb_mount needs to be taken out of its misery; it makes very little sense and it's certainly rife with TOCTOU issues.
What to replace it with is an interesting question, especially considering how easy it is to bypass the damn thing with fsopen(), open_tree() and friends.
It certainly won't be a single hook; multiplexing thing aside, if you look at e.g. loopback you'll see that there are two separate operations involved - one is cloning a tree (that's where dev_name is parsed in old API; the corresponding spot in the new one is open_tree() with OPEN_TREE_CLONE in flags) and another - attaching that tree to destination (move_mount(2) in the new API).
The former is "what", the latter - "where". And in open_tree()/move_mount() it literally could be done by different processes - there's no problem with open_tree() in one process, passing the resulting descriptor to another process that will attach it.
Any checks you do sb_mount (or in your mount_loopback) would have to have equivalent counterparts in those, or you get an easy way to bypass them.
That's a very unpleasant can of worms; if you want to open it, be my guest, but I would seriously suggest doing that after the end of merge window - and going over the existing LSMs to see what they are trying to do in that area before starting that thread. And yes, that's an example of the reasons why I'm very sceptical about out-of-tree modules in that area - with API in that state, we have no realistic way to promise any kind of stability, with obvious consequences for everyone we can't even see.
Song Liu
reviewerRe: [PATCH bpf-next 2/3] bpf: Add bpf_kern_path and bpf_path_put kfuncs
Quoted 15 lines · Al Viro
Quoted 5 lines · Song Liu
Quoted 1 line
Your primitive is a walking TOCTOU bug - it's impossible to use safely.
Good point. AFAICT, the sample TOCTOU bug applies to other LSMs that care about
dev_nameinsb_mount, namely,aa_bind_mount()for apparmor andtomoyo_mount_acl()for tomoyo.
sb_mountneeds to be taken out of its misery; it makes very little sense and it's certainly rife with TOCTOU issues.What to replace it with is an interesting question, especially considering how easy it is to bypass the damn thing with
fsopen(),open_tree()and friends.
Quoted 6 lines
It certainly won't be a single hook; multiplexing thing aside, if you look at e.g. loopback you'll see that there are two separate operations involved - one is cloning a tree (that's where
dev_nameis parsed in old API; the corresponding spot in the new one isopen_tree()withOPEN_TREE_CLONEin flags) and another - attaching that tree to destination (move_mount(2)in the new API).
We currently have security_move_mount(), security_sb_remount(), and security_sb_kern_mount(), so most things are somewhat covered.
Quoted 4 lines
The former is "what", the latter - "where". And in open_tree()/move_mount() it literally could be done by different processes - there's no problem with
open_tree()in one process, passing the resulting descriptor to another process that will attach it.
For open_tree, security_file_open() can cover the "what" part of it.
Quoted 9 lines
Any checks you do
sb_mount(or in yourmount_loopback) would have to have equivalent counterparts in those, or you get an easy way to bypass them.That's a very unpleasant can of worms; if you want to open it, be my guest, but I would seriously suggest doing that after the end of merge window - and going over the existing LSMs to see what they are trying to do in that area before starting that thread.
I very much support fixing it properly, and I don't plan to rush into a half broken workaround. I am not very optimistic about whether we can bring everyone to the same page, but I think it is worth a try.
Quoted 5 lines
And yes, that's an example
of the reasons why I'm very sceptical about out-of-tree modules in that area - with API in that state, we have no realistic way to promise any kind of stability, with obvious consequences for everyone we can't even see.
I am not sure what you mean by "with API in that state". Which API sets are you talking about: the LSM hooks, the BPF kfuncs in bpf_fs_kfuncs.c, or all the exported symbols that are available to in-tree and out-of-tree LSMs? And how would you suggest we make these APIs into a better state?
Thanks,
Song