Enable writing xattr from BPF programs
Subsystems (6)
Song Liu
reviewerv4 cover Enable writing xattr from BPF programs
Add support to set and remove xattr from BPF program. Also add security.bpf. xattr name prefix.
kfuncs are added to set and remove xattrs with security.bpf. name prefix. Update kfuncs bpf_get_[file|dentry]_xattr to read xattrs with security.bpf. name prefix. Note that BPF programs can read user. xattrs, but not write and remove them.
Cover letter of v1 and v2:
Follow up discussion in LPC 2024 [1], that we need security.bpf xattr prefix. This set adds "security.bpf." xattr name prefix, and allows bpf kfuncs bpf_get_[file|dentry]_xattr() to read these xattrs.
[1] https://lpc.events/event/18/contributions/1940/
Changes v3 => v4
1. Do write permission check with inode locked. (Jan Kara)
2. Fix some source_inline warnings.
v3: https://lore.kernel.org/bpf/[email protected]/
Changes v2 => v3
1. Add kfuncs to set and remove xattr from BPF programs.
v2: https://lore.kernel.org/bpf/[email protected]/
Changes v1 => v2
1. Update comment of bpf_get_[file|dentry]_xattr. (Jiri Olsa)
2. Fix comment for return value of bpf_get_[file|dentry]_xattr.
v1: https://lore.kernel.org/bpf/[email protected]/
Song Liu (6):
fs/xattr: bpf: Introduce security.bpf. xattr name prefix selftests/bpf: Extend test fs_kfuncs to cover security.bpf. xattr
names
bpf: lsm: Add two more sleepable hooks
bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs selftests/bpf: Test kfuncs that set and remove xattr from BPF programs selftests/bpf: Add __failure tests for set/remove xattr kfuncs
fs/bpf_fs_kfuncs.c | 262 +++++++++++++++++- include/uapi/linux/xattr.h | 4 + kernel/bpf/bpf_lsm.c | 2 + tools/testing/selftests/bpf/bpf_kfuncs.h | 10 + .../selftests/bpf/prog_tests/fs_kfuncs.c | 165 ++++++++++- .../selftests/bpf/progs/test_get_xattr.c | 28 +- .../bpf/progs/test_set_remove_xattr.c | 129 +++++++++ .../bpf/progs/test_set_remove_xattr_failure.c | 56 ++++ 8 files changed, 636 insertions(+), 20 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_set_remove_xattr.c create mode 100644 tools/testing/selftests/bpf/progs/test_set_remove_xattr_failure.c
--
2.43.5
Song Liu
reviewerPATCH v4 1/6 fs/xattr: bpf: Introduce security.bpf. xattr name prefix
Introduct new xattr name prefix security.bpf., and enable reading these xattrs from bpf kfuncs bpf_get_[file|dentry]_xattr().
As we are on it, correct the comments for return value of bpf_get_[file|dentry]_xattr(), i.e. return length the xattr value on success.
--- fs/bpf_fs_kfuncs.c | 19 ++++++++++++++----- include/uapi/linux/xattr.h | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-)
fs/bpf_fs_kfuncs.c +14 -5
@@ -93,6 +93,11 @@ __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)return len; } +static bool match_security_bpf_prefix (const char *name__str) +{ +return !strncmp (name__str,XATTR_NAME_BPF_LSM ,XATTR_NAME_BPF_LSM_LEN ); +} +/** * bpf_get_dentry_xattr - get xattr of a dentry * @dentry: dentry to get xattr from
@@ -101,9 +106,10 @@ __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)* * Get xattr *name__str* of *dentry* and store the output in *value_ptr*. * -* For security reasons, only *name__str* with prefix "user." is allowed. +* For security reasons, only *name__str* with prefix "user." or +* "security.bpf." is allowed. * -* Return: 0 on success, a negative value on error. +* Return: length of the xattr value on success, a negative value on error. */ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,struct bpf_dynptr *value_p)
@@ -117,7 +123,9 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__stif (WARN_ON (!inode))return -EINVAL ; -if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +/* Allow reading xattr with user. and security.bpf. prefix */ +if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN ) && + !match_security_bpf_prefix (name__str))return -EPERM ; value_len =__bpf_dynptr_size (value_ptr);
@@ -139,9 +147,10 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st* * Get xattr *name__str* of *file* and store the output in *value_ptr*. * -* For security reasons, only *name__str* with prefix "user." is allowed. +* For security reasons, only *name__str* with prefix "user." or +* "security.bpf." is allowed. * -* Return: 0 on success, a negative value on error. +* Return: length of the xattr value on success, a negative value on error. */ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,struct bpf_dynptr *value_p)
include/uapi/linux/xattr.h +4 -0
@@ -83,6 +83,10 @@ struct xattr_args {#define XATTR_CAPS_SUFFIX "capability"#define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX +#define XATTR_BPF_LSM_SUFFIX "bpf." +#define XATTR_NAME_BPF_LSM (XATTR_SECURITY_PREFIX XATTR_BPF_LSM_SUFFIX) +#define XATTR_NAME_BPF_LSM_LEN (sizeof(XATTR_NAME_BPF_LSM) - 1) +#define XATTR_POSIX_ACL_ACCESS "posix_acl_access"#define XATTR_NAME_POSIX_ACL_ACCESS XATTR_SYSTEM_PREFIX XATTR_POSIX_ACL_ACCESS#define XATTR_POSIX_ACL_DEFAULT "posix_acl_default"
Signature · 1 line
2.43.5
Re: [PATCH v4 bpf-next 1/6] fs/xattr: bpf: Introduce security.bpf. xattr name prefix
Quoted 9 lines · Song Liu
Introduct new xattr name prefix security.bpf., and enable reading these xattrs from bpf kfuncs bpf_get_[file|dentry]_xattr().
As we are on it, correct the comments for return value of bpf_get_[file|dentry]_xattr(), i.e. return length the xattr value on success.
Signed-off-by Song Liu <[email protected]>
Looks good to me. Feel free to add:
Honza
Quoted 76 lines
--- fs/bpf_fs_kfuncs.c | 19 ++++++++++++++----- include/uapi/linux/xattr.h | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-)fs/bpf_fs_kfuncs.c +14
-5@@ -93,6 +93,11 @@ __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)return len; } +static bool match_security_bpf_prefix (const char *name__str) +{ +return !strncmp (name__str,XATTR_NAME_BPF_LSM ,XATTR_NAME_BPF_LSM_LEN ); +} +/** * bpf_get_dentry_xattr - get xattr of a dentry * @dentry: dentry to get xattr from @@ -101,9 +106,10 @@ __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)* * Get xattr *name__str* of *dentry* and store the output in *value_ptr*. * -* For security reasons, only *name__str* with prefix "user." is allowed. +* For security reasons, only *name__str* with prefix "user." or +* "security.bpf." is allowed. * -* Return: 0 on success, a negative value on error. +* Return: length of the xattr value on success, a negative value on error. */ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,struct bpf_dynptr *value_p)@@ -117,7 +123,9 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__stif (WARN_ON (!inode))return -EINVAL ; -if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +/* Allow reading xattr with user. and security.bpf. prefix */ +if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN ) && + !match_security_bpf_prefix (name__str))return -EPERM ; value_len =__bpf_dynptr_size (value_ptr);@@ -139,9 +147,10 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st* * Get xattr *name__str* of *file* and store the output in *value_ptr*. * -* For security reasons, only *name__str* with prefix "user." is allowed. +* For security reasons, only *name__str* with prefix "user." or +* "security.bpf." is allowed. * -* Return: 0 on success, a negative value on error. +* Return: length of the xattr value on success, a negative value on error. */ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,struct bpf_dynptr *value_p)include/uapi/linux/xattr.h +4
-0@@ -83,6 +83,10 @@ struct xattr_args {#define XATTR_CAPS_SUFFIX "capability"#define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX +#define XATTR_BPF_LSM_SUFFIX "bpf." +#define XATTR_NAME_BPF_LSM (XATTR_SECURITY_PREFIX XATTR_BPF_LSM_SUFFIX) +#define XATTR_NAME_BPF_LSM_LEN (sizeof(XATTR_NAME_BPF_LSM) - 1) +#define XATTR_POSIX_ACL_ACCESS "posix_acl_access"#define XATTR_NAME_POSIX_ACL_ACCESS XATTR_SYSTEM_PREFIX XATTR_POSIX_ACL_ACCESS#define XATTR_POSIX_ACL_DEFAULT "posix_acl_default"Signature · 1 line
2.43.5
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Song Liu
reviewerPATCH v4 2/6 selftests/bpf: Extend test fs_kfuncs to cover security.bpf. xattr names
In reply to Song LiuExtend test_progs fs_kfuncs to cover different xattr names. Specifically:
xattr name "user.kfuncs" and "security.bpf.xxx" can be read from BPF program with kfuncs bpf_get_[file|dentry]_xattr(); while "security.bpf" and "security.selinux" cannot be read.
--- .../selftests/bpf/prog_tests/fs_kfuncs.c | 37 ++++++++++++++----- .../selftests/bpf/progs/test_get_xattr.c | 28 ++++++++++++-- 2 files changed, 51 insertions(+), 14 deletions(-)
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +27 -10
@@ -12,7 +12,7 @@static const char testfile[] ="/tmp/test_progs_fs_kfuncs" ; -static void test_xattr (void ) +static void test_get_xattr (const char *name,const char *value,bool allow_access) {struct test_get_xattr *skel =NULL ;int fd =-1 , err;
@@ -25,7 +25,7 @@ static void test_xattr(void)close (fd); fd =-1 ; - err =setxattr (testfile,"user.kfuncs" ,"hello" ,sizeof ("hello" ),0 ); + err =setxattr (testfile, name, value,strlen (value) +1 ,0 );if (err && errno ==EOPNOTSUPP ) { printf("%s:SKIP:local fs doesn't support xattr (%d)\n" "To run this test, make sure /tmp filesystem supports xattr.\n",
@@ -48,16 +48,23 @@ static void test_xattr(void) goto out; fd =open (testfile,O_RDONLY ,0644 ); +if (!ASSERT_GE (fd,0 ,"open_file" )) goto out; -ASSERT_EQ (skel->bss->found_xattr_from_file,1 ,"found_xattr_from_file" ); -/* Trigger security_inode_getxattr */ - err =getxattr (testfile,"user.kfuncs" , v,sizeof (v)); -ASSERT_EQ (err,-1 ,"getxattr_return" ); -ASSERT_EQ (errno,EINVAL ,"getxattr_errno" ); -ASSERT_EQ (skel->bss->found_xattr_from_dentry,1 ,"found_xattr_from_dentry" ); + err =getxattr (testfile, name, v,sizeof (v)); + +if (allow_access) { +ASSERT_EQ (err,-1 ,"getxattr_return" ); +ASSERT_EQ (errno,EINVAL ,"getxattr_errno" ); +ASSERT_EQ (skel->bss->found_xattr_from_file,1 ,"found_xattr_from_file" ); +ASSERT_EQ (skel->bss->found_xattr_from_dentry,1 ,"found_xattr_from_dentry" ); + }else { +ASSERT_EQ (err,strlen (value) +1 ,"getxattr_return" ); +ASSERT_EQ (skel->bss->found_xattr_from_file,0 ,"found_xattr_from_file" ); +ASSERT_EQ (skel->bss->found_xattr_from_dentry,0 ,"found_xattr_from_dentry" ); + } out:close (fd);
@@ -141,8 +148,18 @@ static void test_fsverity(void)void test_fs_kfuncs (void ) { -if (test__start_subtest ("xattr" )) -test_xattr (); +/* Matches xattr_names in progs/test_get_xattr.c */ +if (test__start_subtest ("user_xattr" )) +test_get_xattr ("user.kfuncs" ,"hello" , true); + +if (test__start_subtest ("security_bpf_xattr" )) +test_get_xattr ("security.bpf.xxx" ,"hello" , true); + +if (test__start_subtest ("security_bpf_xattr_error" )) +test_get_xattr ("security.bpf" ,"hello" , false); + +if (test__start_subtest ("security_selinux_xattr_error" )) +test_get_xattr ("security.selinux" ,"hello" , false);if (test__start_subtest ("fsverity" ))test_fsverity ();
tools/testing/selftests/bpf/progs/test_get_xattr.c +24 -4
@@ -6,6 +6,7 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> #include "bpf_kfuncs.h" +#include "bpf_misc.h" char _license[] SEC("license") = "GPL";
@@ -17,12 +18,23 @@ static const char expected_value[] = "hello";char value1[32 ];char value2[32 ]; +/* Matches caller of test_get_xattr() in prog_tests/fs_kfuncs.c */ +static const char *const xattr_names[] = { +/* The following work. */ +"user.kfuncs" , +"security.bpf.xxx" , + +/* The following do not work. */ +"security.bpf" , +"security.selinux" +}; + SEC("lsm.s/file_open")int BPF_PROG (test_file_open ,struct file *f) {struct bpf_dynptr value_ptr;__u32 pid; -int ret; +int ret, i; pid =bpf_get_current_pid_tgid () >>32 ;if (pid != monitored_pid)
@@ -30,7 +42,11 @@ int BPF_PROG(test_file_open, struct file *f)bpf_dynptr_from_mem (value1,sizeof (value1),0 , &value_ptr); - ret =bpf_get_file_xattr (f,"user.kfuncs" , &value_ptr); +for (i =0 ; i <ARRAY_SIZE (xattr_names); i++) { + ret =bpf_get_file_xattr (f, xattr_names[i], &value_ptr); +if (ret ==sizeof (expected_value)) +break ; + }if (ret !=sizeof (expected_value))return 0 ;if (bpf_strncmp (value1, ret, expected_value))
@@ -44,7 +60,7 @@ int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name) {struct bpf_dynptr value_ptr;__u32 pid; -int ret; +int ret, i; pid =bpf_get_current_pid_tgid () >>32 ;if (pid != monitored_pid)
@@ -52,7 +68,11 @@ int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name)bpf_dynptr_from_mem (value2,sizeof (value2),0 , &value_ptr); - ret =bpf_get_dentry_xattr (dentry,"user.kfuncs" , &value_ptr); +for (i =0 ; i <ARRAY_SIZE (xattr_names); i++) { + ret =bpf_get_dentry_xattr (dentry, xattr_names[i], &value_ptr); +if (ret ==sizeof (expected_value)) +break ; + }if (ret !=sizeof (expected_value))return 0 ;if (bpf_strncmp (value2, ret, expected_value))
Signature · 1 line
2.43.5
Song Liu
reviewerPATCH v4 3/6 bpf: lsm: Add two more sleepable hooks
In reply to Song LiuAdd bpf_lsm_inode_removexattr and bpf_lsm_inode_post_removexattr to list sleepable_lsm_hooks. These two hooks are always called from sleepable context.
--- kernel/bpf/bpf_lsm.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/bpf/bpf_lsm.c +2 -0
@@ -316,7 +316,9 @@ BTF_ID(func, bpf_lsm_inode_getxattr) BTF_ID(func, bpf_lsm_inode_mknod) BTF_ID(func, bpf_lsm_inode_need_killpriv) BTF_ID(func, bpf_lsm_inode_post_setxattr) +BTF_ID(func, bpf_lsm_inode_post_removexattr) BTF_ID(func, bpf_lsm_inode_readlink) +BTF_ID(func, bpf_lsm_inode_removexattr) BTF_ID(func, bpf_lsm_inode_rename) BTF_ID(func, bpf_lsm_inode_rmdir)BTF_ID (func, bpf_lsm_inode_setattr)
Signature · 1 line
2.43.5
Song Liu
reviewerPATCH v4 4/6 bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs
In reply to Song LiuAdd the following kfuncs to set and remove xattrs from BPF programs:
bpf_set_dentry_xattr
bpf_remove_dentry_xattr
bpf_set_dentry_xattr_locked
bpf_remove_dentry_xattr_locked
The _locked version of these kfuncs are called from hooks where dentry->d_inode is already locked.
--- fs/bpf_fs_kfuncs.c | 243 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 242 insertions(+), 1 deletion(-)
fs/bpf_fs_kfuncs.c +242 -1
@@ -6,6 +6,7 @@#include <linux/btf_ids.h> #include <linux/dcache.h> #include <linux/fs.h> +#include <linux/fsnotify.h> #include <linux/file.h> #include <linux/mm.h> #include <linux/xattr.h>
@@ -161,6 +162,164 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,return bpf_get_dentry_xattr (dentry, name__str, value_p); } +static int bpf_xattr_write_permission (const char *name,struct inode *inode) +{ +if (WARN_ON (!inode)) +return -EINVAL ; + +/* Only allow setting and removing security.bpf. xattrs */ +if (!match_security_bpf_prefix (name)) +return -EPERM ; + +return inode_permission (&nop_mnt_idmap, inode,MAY_WRITE ); +} + +static int __bpf_set_dentry_xattr (struct dentry *dentry,const char *name, +const struct bpf_dynptr *value_p,int flags,bool lock_inode) +{ +struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p; +struct inode *inode =d_inode (dentry); +const void *value; +u32 value_len; +int ret; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data (value_ptr, value_len); +if (!value) +return -EINVAL ; + +if (lock_inode) +inode_lock (inode); + + ret =bpf_xattr_write_permission (name, inode); +if (ret) + goto out; + + ret =__vfs_setxattr (&nop_mnt_idmap, dentry, inode, name, + value, value_len, flags); +if (!ret) { +fsnotify_xattr (dentry); + +/* This xattr is set by BPF LSM, so we do not call +* security_inode_post_setxattr. This is the same as +* security_inode_setsecurity(). +*/ + } +out: +if (lock_inode) +inode_unlock (inode); +return ret; +} + +/** +* bpf_set_dentry_xattr - set a xattr of a dentry +* @dentry: dentry to get xattr from +* @name__str: name of the xattr +* @value_p: xattr value +* @flags: flags to pass into filesystem operations +* +* Set xattr *name__str* of *dentry* to the value in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "security.bpf." +* is allowed. +* +* The caller has not locked dentry->d_inode. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str, +const struct bpf_dynptr *value_p,int flags) +{ +return __bpf_set_dentry_xattr (dentry, name__str, value_p, flags, true); +} + +/** +* bpf_set_dentry_xattr_locked - set a xattr of a dentry +* @dentry: dentry to get xattr from +* @name__str: name of the xattr +* @value_p: xattr value +* @flags: flags to pass into filesystem operations +* +* Set xattr *name__str* of *dentry* to the value in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "security.bpf." +* is allowed. +* +* The caller already locked dentry->d_inode. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_set_dentry_xattr_locked(struct dentry *dentry, const char *name__str, +const struct bpf_dynptr *value_p,int flags) +{ +return __bpf_set_dentry_xattr (dentry, name__str, value_p, flags, false); +} + +static int __bpf_remove_dentry_xattr (struct dentry *dentry,const char *name__str, +bool lock_inode) +{ +struct inode *inode =d_inode (dentry); +int ret; + +if (lock_inode) +inode_lock (inode); + + ret =bpf_xattr_write_permission (name__str, inode); +if (ret) + goto out; + + ret =__vfs_removexattr (&nop_mnt_idmap, dentry, name__str); +if (!ret) { +fsnotify_xattr (dentry); + +/* This xattr is removed by BPF LSM, so we do not call +* security_inode_post_removexattr. +*/ + } +out: +if (lock_inode) +inode_unlock (inode); +return ret; +} + +/** +* bpf_remove_dentry_xattr - remove a xattr of a dentry +* @dentry: dentry to get xattr from +* @name__str: name of the xattr +* +* Rmove xattr *name__str* of *dentry*. +* +* For security reasons, only *name__str* with prefix "security.bpf." +* is allowed. +* +* The caller has not locked dentry->d_inode. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) +{ +return __bpf_remove_dentry_xattr (dentry, name__str, true); +} + +/** +* bpf_remove_dentry_xattr_locked - remove a xattr of a dentry +* @dentry: dentry to get xattr from +* @name__str: name of the xattr +* +* Rmove xattr *name__str* of *dentry*. +* +* For security reasons, only *name__str* with prefix "security.bpf." +* is allowed. +* +* The caller already locked dentry->d_inode. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_remove_dentry_xattr_locked(struct dentry *dentry, const char *name__str) +{ +return __bpf_remove_dentry_xattr (dentry, name__str, false); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)
@@ -186,9 +345,91 @@ static const struct btf_kfunc_id_set bpf_fs_kfunc_set = { .filter = bpf_fs_kfuncs_filter, }; +/* bpf_[set|remove]_dentry_xattr.* hooks have KF_TRUSTED_ARGS and +* KF_SLEEPABLE, so they are only available to sleepable hooks with +* dentry arguments. +* +* Setting and removing xattr requires exclusive lock on dentry->d_inode. +* Some hooks already locked d_inode, while some hooks have not locked +* d_inode. Therefore, we need different kfuncs for different hooks. +* Specifically, hooks in the following list (d_inode_locked_hooks) +* should call bpf_[set|remove]_dentry_xattr_locked; while other hooks +* should call bpf_[set|remove]_dentry_xattr. +*/ +BTF_SET_START (d_inode_locked_hooks) +BTF_ID (func ,bpf_lsm_inode_post_removexattr ) +BTF_ID (func, bpf_lsm_inode_post_setattr) +BTF_ID (func, bpf_lsm_inode_post_setxattr) +BTF_ID (func, bpf_lsm_inode_removexattr) +BTF_ID (func, bpf_lsm_inode_rmdir) +BTF_ID (func, bpf_lsm_inode_setattr) +BTF_ID (func, bpf_lsm_inode_setxattr) +BTF_ID (func, bpf_lsm_inode_unlink) +#ifdef CONFIG_SECURITY_PATH +BTF_ID (func, bpf_lsm_path_unlink) +BTF_ID (func, bpf_lsm_path_rmdir) +#endif /* CONFIG_SECURITY_PATH */ +BTF_SET_END (d_inode_locked_hooks) + +static bool bpf_lsm_has_d_inode_locked(const struct bpf_prog *prog) +{ +return btf_id_set_contains (&d_inode_locked_hooks, prog->aux->attach_btf_id); +} + +BTF_KFUNCS_START(bpf_write_xattr_set_ids) +BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_KFUNCS_END(bpf_write_xattr_set_ids) + +static int bpf_write_xattr_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ + if (!btf_id_set8_contains(&bpf_write_xattr_set_ids, kfunc_id)) + return 0; + if (prog->type != BPF_PROG_TYPE_LSM) + return -EACCES; + + if (bpf_lsm_has_d_inode_locked(prog)) + return -EINVAL; +return 0 ; +} + +static const struct btf_kfunc_id_set bpf_write_xattr_set = { + .owner =THIS_MODULE , + .set = &bpf_write_xattr_set_ids, + .filter = bpf_write_xattr_filter, +}; + +BTF_KFUNCS_START (bpf_write_xattr_locked_set_ids) +BTF_ID_FLAGS(func, bpf_set_dentry_xattr_locked, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_remove_dentry_xattr_locked, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_KFUNCS_END (bpf_write_xattr_locked_set_ids ) + +static int bpf_write_xattr_locked_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ + if (!btf_id_set8_contains(&bpf_write_xattr_locked_set_ids, kfunc_id)) + return 0; + if (prog->type != BPF_PROG_TYPE_LSM) + return -EACCES; + + if (!bpf_lsm_has_d_inode_locked(prog)) + return -EINVAL; + return 0; +} + +static const struct btf_kfunc_id_set bpf_write_xattr_locked_set = { + .owner =THIS_MODULE , + .set = &bpf_write_xattr_locked_set_ids, + .filter = bpf_write_xattr_locked_filter, +}; + static int __init bpf_fs_kfuncs_init(void) { -return register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_fs_kfunc_set); +int ret; + + ret =register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_fs_kfunc_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_write_xattr_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_write_xattr_locked_set); +return ret; }late_initcall (bpf_fs_kfuncs_init);
Signature · 1 line
2.43.5
Re: [PATCH v4 bpf-next 4/6] bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs
Quoted 10 lines · Song Liu
Add the following kfuncs to set and remove xattrs from BPF programs:
bpf_set_dentry_xattr
bpf_remove_dentry_xattr
bpf_set_dentry_xattr_locked
bpf_remove_dentry_xattr_lockedThe
_lockedversion of these kfuncs are called from hooks wheredentry->d_inodeis already locked.
...
Quoted 8 lines
+ *
+ * Setting and removing xattr requires exclusive lock ondentry->d_inode.
+ * Some hooks already lockedd_inode, while some hooks have not locked
+ *d_inode. Therefore, we need different kfuncs for different hooks.
+ * Specifically, hooks in the following list (d_inode_locked_hooks)
+ * should call bpf_[set|remove]_dentry_xattr_locked; while other hooks
+ * should call bpf_[set|remove]_dentry_xattr.
+ */
the inode locking rules might change, so let's hide this implementation detail from the bpf progs by making kfunc polymorphic.
To struct bpf_prog_aux add:
bool use_locked_kfunc:1;
and set it in bpf_check_attach_target() if it's attaching to one of d_inode_locked_hooks
Then in fixup_kfunc_call() call some helper that if (prog->aux->use_locked_kfunc &&
insn->imm == special_kfunc_list[KF_bpf_remove_dentry_xattr])
insn->imm = special_kfunc_list[KF_bpf_remove_dentry_xattr_locked];The progs will be simpler and will suffer less churn when the kernel side changes.
Re: [PATCH v4 bpf-next 4/6] bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs
Hi Alexei,
Thanks for the review!
Quoted 33 lines · Alexei Starovoitov
Quoted 10 lines · Song Liu
Add the following kfuncs to set and remove xattrs from BPF programs:
bpf_set_dentry_xattr
bpf_remove_dentry_xattr
bpf_set_dentry_xattr_locked
bpf_remove_dentry_xattr_lockedThe
_lockedversion of these kfuncs are called from hooks wheredentry->d_inodeis already locked....
Quoted 8 lines
+ *
+ * Setting and removing xattr requires exclusive lock ondentry->d_inode.
+ * Some hooks already lockedd_inode, while some hooks have not locked
+ *d_inode. Therefore, we need different kfuncs for different hooks.
+ * Specifically, hooks in the following list (d_inode_locked_hooks)
+ * should call bpf_[set|remove]_dentry_xattr_locked; while other hooks
+ * should call bpf_[set|remove]_dentry_xattr.
+ */the inode locking rules might change, so let's hide this implementation detail from the bpf progs by making kfunc polymorphic.
To struct
bpf_prog_auxadd:
bool use_locked_kfunc:1;
and set it inbpf_check_attach_target()if it's attaching to one ofd_inode_locked_hooks
Quoted 7 lines
Then in
fixup_kfunc_call()call some helper that
if (prog->aux->use_locked_kfunc&&
insn->imm== special_kfunc_list[KF_bpf_remove_dentry_xattr])insn->imm = special_kfunc_list[KF_bpf_remove_dentry_xattr_locked];The progs will be simpler and will suffer less churn when the kernel side changes.
I was thinking about something in similar direction.
If we do this, shall we somehow hide the _locked version of the kfuncs, so that the user cannot use it? If so, what's the best way to do it?
Thanks,
Song
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH v4 bpf-next 4/6] bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs
Quoted 43 lines · Song Liu
Hi Alexei,
Thanks for the review!
Quoted 38 lines · Alexei Starovoitov
Quoted 10 lines · Song Liu
Add the following kfuncs to set and remove xattrs from BPF programs:
bpf_set_dentry_xattr
bpf_remove_dentry_xattr
bpf_set_dentry_xattr_locked
bpf_remove_dentry_xattr_lockedThe
_lockedversion of these kfuncs are called from hooks wheredentry->d_inodeis already locked....
Quoted 8 lines
+ *
+ * Setting and removing xattr requires exclusive lock ondentry->d_inode.
+ * Some hooks already lockedd_inode, while some hooks have not locked
+ *d_inode. Therefore, we need different kfuncs for different hooks.
+ * Specifically, hooks in the following list (d_inode_locked_hooks)
+ * should call bpf_[set|remove]_dentry_xattr_locked; while other hooks
+ * should call bpf_[set|remove]_dentry_xattr.
+ */the inode locking rules might change, so let's hide this implementation detail from the bpf progs by making kfunc polymorphic.
To struct
bpf_prog_auxadd:
bool use_locked_kfunc:1;
and set it inbpf_check_attach_target()if it's attaching to one ofd_inode_locked_hooksThen in
fixup_kfunc_call()call some helper that
if (prog->aux->use_locked_kfunc&&
insn->imm== special_kfunc_list[KF_bpf_remove_dentry_xattr])insn->imm = special_kfunc_list[KF_bpf_remove_dentry_xattr_locked];
Quoted 8 lines
Quoted 2 lines
The progs will be simpler and will suffer less churn when the kernel side changes.
I was thinking about something in similar direction.
If we do this, shall we somehow hide the
_lockedversion of the kfuncs, so that the user cannot use it? If so, what's the best way to do it?
Just don't add BTF_ID_FLAGS entries for them.
You'd also need to make an extra call to add_kfunc_call to add its details before you can do the fixup.
That allows find_kfunc_desc to work.
I did something similar in earlier versions of resilient locks. In add_kfunc_call's end (instead of directly returning):func_id = get_shadow_kfunc_id(func_id, offset);
if (!func_id)
return err;
return add_kfunc_call(env, func_id, offset);
Then check in fixup_kfunc_call to find shadow kfunc id and substitute imm. Can use some other naming instead of "shadow".
Probably need to take a prog pointer to make a decision to find the underlying kfunc id in your case.
Quoted 4 lines
Thanks,
Song
Re: [PATCH v4 bpf-next 4/6] bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs
Quoted 68 lines · Kumar Kartikeya Dwivedi
Quoted 43 lines · Song Liu
Hi Alexei,
Thanks for the review!
Quoted 38 lines · Alexei Starovoitov
Quoted 10 lines · Song Liu
Add the following kfuncs to set and remove xattrs from BPF programs:
bpf_set_dentry_xattrbpf_remove_dentry_xattrbpf_set_dentry_xattr_lockedbpf_remove_dentry_xattr_lockedThe
_lockedversion of these kfuncs are called from hooks wheredentry->d_inodeis already locked....
Quoted 8 lines
+ *
+ * Setting and removing xattr requires exclusive lock ondentry->d_inode.
+ * Some hooks already lockedd_inode, while some hooks have not locked
+ *d_inode. Therefore, we need different kfuncs for different hooks.
+ * Specifically, hooks in the following list (d_inode_locked_hooks)
+ * should call bpf_[set|remove]_dentry_xattr_locked; while other hooks
+ * should call bpf_[set|remove]_dentry_xattr.
+ */the inode locking rules might change, so let's hide this implementation detail from the bpf progs by making kfunc polymorphic.
To struct
bpf_prog_auxadd:
bool use_locked_kfunc:1;
and set it inbpf_check_attach_target()if it's attaching to one ofd_inode_locked_hooksThen in
fixup_kfunc_call()call some helper that
if (prog->aux->use_locked_kfunc&&
insn->imm== special_kfunc_list[KF_bpf_remove_dentry_xattr])
insn->imm= special_kfunc_list[KF_bpf_remove_dentry_xattr_locked];Quoted 8 lines
Quoted 2 lines
The progs will be simpler and will suffer less churn when the kernel side changes.
I was thinking about something in similar direction.
If we do this, shall we somehow hide the
_lockedversion of the kfuncs, so that the user cannot use it? If so, what's the best way to do it?Just don't add
BTF_ID_FLAGSentries for them.
You'd also need to make an extra call toadd_kfunc_callto add its details before you can do the fixup.
That allowsfind_kfunc_descto work.
I did something similar in earlier versions of resilient locks. In add_kfunc_call's end (instead of directly returning):func_id=get_shadow_kfunc_id(func_id, offset);
if (!func_id)
return err;
returnadd_kfunc_call(env, func_id, offset);
Quoted 4 lines
Then check in
fixup_kfunc_callto find shadow kfunc id and substitute imm. Can use some other naming instead of "shadow".
Probably need to take a prog pointer to make a decision to find the underlying kfunc id in your case.
Thanks for the hints! They helped a lot.
I ended up doing this with a slightly different logic, which I think is cleaner. I will send v5 shortly.
Song
Song Liu
reviewerPATCH v4 5/6 selftests/bpf: Test kfuncs that set and remove xattr from BPF programs
In reply to Song LiuTwo sets of tests are added to exercise the not _locked and _locked version of the kfuncs. For both tests, user space accesses xattr security.bpf.foo on a testfile. The BPF program is triggered by user space access (on LSM hook inode_[set|get]_xattr) and sets or removes xattr security.bpf.bar. Then user space then validates that xattr security.bpf.bar is set or removed as expected.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 10 ++ .../selftests/bpf/prog_tests/fs_kfuncs.c | 125 +++++++++++++++++ .../bpf/progs/test_set_remove_xattr.c | 129 ++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_set_remove_xattr.c
tools/testing/selftests/bpf/bpf_kfuncs.h +10 -0
@@ -87,4 +87,14 @@ struct dentry;*/ extern int bpf_get_dentry_xattr(struct dentry *dentry, const char *name, struct bpf_dynptr *value_ptr) __ksym __weak; + +extern int bpf_set_dentry_xattr_locked(struct dentry *dentry, const char *name__str, + const struct bpf_dynptr *value_p, int flags) __ksym __weak; +extern int bpf_remove_dentry_xattr_locked(struct dentry *dentry, + const char *name__str) __ksym __weak; + +extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str, + const struct bpf_dynptr *value_p, int flags) __ksym __weak; +extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak; +#endif
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +125 -0
143 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_set_remove_xattr.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.43.5
Song Liu
reviewerPATCH v4 6/6 selftests/bpf: Add __failure tests for set/remove xattr kfuncs
In reply to Song LiuDifferent LSM hooks should call different versions of set/remove xattr kfuncs (with _locked or not). Add __failure tests to make sure the verifier can detect when the user uses the wrong kfuncs.
--- .../selftests/bpf/prog_tests/fs_kfuncs.c | 3 + .../bpf/progs/test_set_remove_xattr_failure.c | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_set_remove_xattr_failure.c
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +3 -0
@@ -9,6 +9,7 @@#include <test_progs.h> #include "test_get_xattr.skel.h" #include "test_set_remove_xattr.skel.h" +#include "test_set_remove_xattr_failure.skel.h" #include "test_fsverity.skel.h" static const char testfile[] ="/tmp/test_progs_fs_kfuncs" ;
@@ -286,6 +287,8 @@ void test_fs_kfuncs(void)if (test__start_subtest ("set_remove_xattr" ))test_set_remove_xattr (); +RUN_TESTS (test_set_remove_xattr_failure); +if (test__start_subtest ("fsverity" ))test_fsverity (); }
tools/testing/selftests/bpf/progs/test_set_remove_xattr_failure.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.43.5