Enable writing xattr from BPF programs
Subsystems (9)
Song Liu
reviewerv11 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.
To pick the right version of kfunc to use, a remap logic is added to btf_kfunc_id_set. This helps move some kfunc specific logic off the verifier core code. Also use this remap logic to select bpf_dynptr_from_skb or bpf_dynptr_from_skb_rdonly.
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 v10 => v11:
1. Add Acked-by from Christian Brauner.
2. Fix selftests build error like this one:
https://github.com/kernel-patches/bpf/actions/runs/13022268618/job/36325472992
3. Rename some variables in the selftests.
v10: https://lore.kernel.org/bpf/[email protected]/
Changes v9 => v10:
1. Refactor bpf_[set|remove]_dentry_xattr[_locked]. (Christian Brauner).
v9: https://lore.kernel.org/bpf/[email protected]/
Changes v8 => v9
1. Fix build for CONFIG_DEBUG_INFO_BTF=n case. (kernel test robot)
v8: https://lore.kernel.org/bpf/[email protected]/
Changes v7 => v8
1. Rebase and resolve conflicts.
v7: https://lore.kernel.org/bpf/[email protected]/
Changes v6 => v7
1. Move btf_kfunc_id_remap() to the right place. (Bug reported by CI)
v6: https://lore.kernel.org/bpf/[email protected]/
Changes v5 => v6
1. Hide _locked version of the kfuncs from vmlinux.h (Alexei)
2. Add remap logic to btf_kfunc_id_set and use that to pick the correct version of kfuncs to use.
3. Also use the remap logic for bpf_dynptr_from_skb[|_rdonly].
v5: https://lore.kernel.org/bpf/[email protected]/
Changes v4 => v5
1. Let verifier pick proper kfunc (_locked or not _locked) based on the calling context. (Alexei)
2. Remove the __failure test (6/6 of v4).
v4: https://lore.kernel.org/bpf/[email protected]/
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 (7):
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: Extend btf_kfunc_id_set to handle kfunc polymorphism
bpf: Use btf_kfunc_id_set.remap logic for bpf_dynptr_from_skb
bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs selftests/bpf: Test kfuncs that set and remove xattr from BPF programs
fs/bpf_fs_kfuncs.c | 238 +++++++++++++++++- include/linux/bpf_lsm.h | 2 + include/linux/btf.h | 20 ++ include/linux/btf_ids.h | 4 + include/uapi/linux/xattr.h | 4 + kernel/bpf/bpf_lsm.c | 2 + kernel/bpf/btf.c | 117 +++++++-- kernel/bpf/verifier.c | 31 +-- net/core/filter.c | 49 +++- tools/testing/selftests/bpf/bpf_kfuncs.h | 5 + .../selftests/bpf/prog_tests/fs_kfuncs.c | 162 +++++++++++- .../selftests/bpf/progs/test_get_xattr.c | 28 ++- .../bpf/progs/test_set_remove_xattr.c | 133 ++++++++++ 13 files changed, 732 insertions(+), 63 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_set_remove_xattr.c
--
2.43.5
Song Liu
reviewerPATCH v11 1/7 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 v11 bpf-next 1/7] fs/xattr: bpf: Introduce security.bpf. xattr name prefix
Quoted 6 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.
Quoted 13 lines
Signed-off-by Song Liu <[email protected]>Reviewed-by Jan Kara <[email protected]>--- fs/bpf_fs_kfuncs.c | 19 ++++++++++++++----- include/uapi/linux/xattr.h | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-)fs/bpf_fs_kfuncs.c +0
-0
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 99
Quoted 7 lines
fs/bpf_fs_kfuncs.c +4
-0@@ -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 ); +}
I think this can also just be match_xattr_prefix(const char *name__str, const char *prefix, size_t len) such that we can do the same checks for aribitrary xattr prefixes i.e. XATTR_USER_PREFIX, XATTR_NAME_BPF_LSM.
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 109
Quoted 9 lines
fs/bpf_fs_kfuncs.c +1
-1/** * 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
^ prefixes
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 110
Quoted 1 line
fs/bpf_fs_kfuncs.c +1
-0+* "security.bpf." is allowed.
^ are
Out of curiosity, what is the security reasoning here? This isn't obvious to me, and I'd like to understand this better. Is it simply frowned upon to read arbitrary xattr values from the context of a BPF LSM program, or has it got something to do with the backing xattr handler that ends up being called once we step into __vfs_getxattr() and such? Also, just so that it's clear, I don't have anything against this allow listing approach either, I just genuinely don't understand the security implications.
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st · line 128
Quoted 13 lines
fs/bpf_fs_kfuncs.c +4
-2-* 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))
I think it would be cleaner to have single function i.e. is_allowed_xattr_prefix(const char *name__str) which simply checks all the allowed xattr prefixes that can be read by this BPF kfunc.
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st · line 150
Quoted 9 lines
fs/bpf_fs_kfuncs.c +1
-1return -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
^ prefixes
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st · line 151
Quoted 1 line
fs/bpf_fs_kfuncs.c +1
-0+* "security.bpf." is allowed.
^ are
Quoted 23 lines
fs/bpf_fs_kfuncs.c +1
-1-* 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
Christian Brauner
maintainerRe: [PATCH v11 bpf-next 1/7] fs/xattr: bpf: Introduce security.bpf. xattr name prefix
Quoted 50 lines · Matt Bobrowski
Quoted 6 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.
Reviewed-by Matt Bobrowski <[email protected]>Quoted 13 lines
Signed-off-by Song Liu <[email protected]>Reviewed-by Jan Kara <[email protected]>--- fs/bpf_fs_kfuncs.c | 19 ++++++++++++++----- include/uapi/linux/xattr.h | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-)fs/bpf_fs_kfuncs.c +0
-0fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 99
Quoted 7 lines
fs/bpf_fs_kfuncs.c +4
-0@@ -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 ); +}I think this can also just be
match_xattr_prefix(const char *name__str, const char *prefix, size_t len)such that we can do the same checks for aribitrary xattr prefixes i.e.XATTR_USER_PREFIX,XATTR_NAME_BPF_LSM.fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 109
Quoted 9 lines
fs/bpf_fs_kfuncs.c +1
-1/** * 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 ^ prefixes
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 110
Quoted 1 line
fs/bpf_fs_kfuncs.c +1
-0+* "security.bpf." is allowed. ^ are
Quoted 8 lines
Out of curiosity, what is the security reasoning here? This isn't obvious to me, and I'd like to understand this better. Is it simply frowned upon to read arbitrary xattr values from the context of a BPF LSM program, or has it got something to do with the backing xattr handler that ends up being called once we step into
__vfs_getxattr()and such? Also, just so that it's clear, I don't have anything against this allow listing approach either, I just genuinely don't understand the security implications.
I've explained this at lenghts in multiple threads. The gist is various xattrs require you to have access to properties that are carried by objects you don't have access to (e.g., the mount) or can't guarantee that you're in the correct context and interpreting those xattrs without this information is either meaningless or actively wrong.
Re: [PATCH v11 bpf-next 1/7] fs/xattr: bpf: Introduce security.bpf. xattr name prefix
Quoted 60 lines · Christian Brauner
Quoted 50 lines · Matt Bobrowski
Quoted 6 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.
Reviewed-by Matt Bobrowski <[email protected]>Quoted 13 lines
Signed-off-by Song Liu <[email protected]>Reviewed-by Jan Kara <[email protected]>--- fs/bpf_fs_kfuncs.c | 19 ++++++++++++++----- include/uapi/linux/xattr.h | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-)fs/bpf_fs_kfuncs.c +0
-0fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 99
Quoted 7 lines
fs/bpf_fs_kfuncs.c +4
-0@@ -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 ); +}I think this can also just be
match_xattr_prefix(const char *name__str, const char *prefix, size_t len)such that we can do the same checks for aribitrary xattr prefixes i.e.XATTR_USER_PREFIX,XATTR_NAME_BPF_LSM.fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 109
Quoted 9 lines
fs/bpf_fs_kfuncs.c +1
-1/** * 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 ^ prefixes
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) · line 110
Quoted 1 line
fs/bpf_fs_kfuncs.c +1
-0+* "security.bpf." is allowed. ^ are
Quoted 8 lines
Out of curiosity, what is the security reasoning here? This isn't obvious to me, and I'd like to understand this better. Is it simply frowned upon to read arbitrary xattr values from the context of a BPF LSM program, or has it got something to do with the backing xattr handler that ends up being called once we step into
__vfs_getxattr()and such? Also, just so that it's clear, I don't have anything against this allow listing approach either, I just genuinely don't understand the security implications.
Quoted 5 lines
I've explained this at lenghts in multiple threads. The gist is various xattrs require you to have access to properties that are carried by objects you don't have access to (e.g., the mount) or can't guarantee that you're in the correct context and interpreting those xattrs without this information is either meaningless or actively wrong.
Oh, right, I see. Thank you Christian!
Re: [PATCH v11 bpf-next 1/7] fs/xattr: bpf: Introduce security.bpf. xattr name prefix
In reply to Matt BobrowskiHi Matt,
Quoted 11 lines · Matt Bobrowski
Quoted 6 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.
Reviewed-by Matt Bobrowski <[email protected]>
Thanks for the review!
[...]
Quoted 10 lines
Quoted 10 lines
- * Return: 0 on success, a negative value on error.
+ * Return: length of the xattr value on success, a negative value on error. */__bpf_kfuncint bpf_get_dentry_xattr(struct dentry *dentry, const char*name__str,
structbpf_dynptr*value_p)fs/bpf_fs_kfuncs.c +0
-0@@ -117,7 +123,9 @@ __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__stif (WARN_ON (!inode))return -EINVAL ;
Quoted 9 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st · line 128
Quoted 4 lines
fs/bpf_fs_kfuncs.c +3
-1-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))I think it would be cleaner to have single function i.e.
is_allowed_xattr_prefix(const char *name__str)which simply checks all the allowed xattr prefixes that can be read by this BPF kfunc.
Sure, we can add bpf_xattr_read_permission() which pairs with bpf_xattr_write_permission().
Thanks,
Song
Quoted 38 lines
fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st · line 150
Quoted 10 lines
fs/bpf_fs_kfuncs.c +1
-1return -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 ^ prefixes
Review __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__st · line 151
Quoted 1 line
(quoted diff) +1
-0+ * "security.bpf." is allowed.^ are
Quoted 23 lines
fs/bpf_fs_kfuncs.c +1
-1-* 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
Song Liu
reviewerPATCH v11 2/7 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 xattr_names[][64 ] = { +/* 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 v11 3/7 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 v11 4/7 bpf: Extend btf_kfunc_id_set to handle kfunc polymorphism
In reply to Song LiuPolymorphism exists in kernel functions, BPF helpers, as well as kfuncs. When called from different contexts, it is necessary to pick the right version of a kfunc. One of such example is bpf_dynptr_from_skb vs. bpf_dynptr_from_skb_rdonly.
To avoid the burden on the users, the verifier can inspect the calling context and select the right version of kfunc. However, with more kfuncs being added to the kernel, it is not scalable to push all these logic to the verifiler.
Extend btf_kfunc_id_set to handle kfunc polymorphism. Specifically, a list of kfuncs, "hidden_set", and a new method "remap" is added to btf_kfunc_id_set. kfuncs in hidden_set do not have BTF_SET8_KFUNCS flag, and are not exposed in vmlinux.h. The remap method is used to inspect the calling context, and when necessary, remap the user visible kfuncs (for example, bpf_dynptr_from_skb), to its hidden version (for example, bpf_dynptr_from_skb_rdonly).
The verifier calls in these remap logic via the new btf_kfunc_id_remap() API, and picks the right kfuncs for the context.
--- include/linux/btf.h | 20 +++++++ include/linux/btf_ids.h | 4 ++ kernel/bpf/btf.c | 117 ++++++++++++++++++++++++++++++++++------ kernel/bpf/verifier.c | 6 ++- 4 files changed, 128 insertions(+), 19 deletions(-)
include/linux/btf.h +20 -0
@@ -114,11 +114,23 @@ struct btf_id_set;struct bpf_prog ;typedef int (*btf_kfunc_filter_t )(const struct bpf_prog *prog,u32 kfunc_id); +typedef u32 (*btf_kfunc_remap_t )(const struct bpf_prog *prog,u32 kfunc_id);struct btf_kfunc_id_set {struct module *owner;struct btf_id_set8 *set; + +/* *hidden_set* contains kfuncs that are not exposed as kfunc in +* vmlinux.h. These kfuncs are usually a variation of a kfunc +* in *set*. +*/ +struct btf_id_set8 *hidden_set;btf_kfunc_filter_t filter; + +/* *remap* method remaps kfuncs in *set* to proper version in +* *hidden_set*. +*/ +btf_kfunc_remap_t remap; };struct btf_id_dtor_kfunc {
@@ -575,6 +587,8 @@ u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,int register_btf_kfunc_id_set (enum bpf_prog_type prog_type,const struct btf_kfunc_id_set *s);int register_btf_fmodret_id_set (const struct btf_kfunc_id_set *kset); +u32 btf_kfunc_id_remap (const struct btf *btf,u32 kfunc_btf_id, +const struct bpf_prog *prog);s32 btf_find_dtor_kfunc (struct btf *btf,u32 btf_id);int register_btf_id_dtor_kfuncs (const struct btf_id_dtor_kfunc *dtors,u32 add_cnt,struct module *owner);
@@ -637,6 +651,12 @@ static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, {return NULL ; } +static inline u32 btf_kfunc_id_remap (const struct btf *btf,u32 kfunc_btf_id, +const struct bpf_prog *prog) +{ +return kfunc_btf_id; +} +static inline int register_btf_kfunc_id_set (enum bpf_prog_type prog_type,const struct btf_kfunc_id_set *s) {
include/linux/btf_ids.h +4 -0
@@ -212,6 +212,9 @@ extern struct btf_id_set8 name;#define BTF_KFUNCS_START (name) \ __BTF_SET8_START(name, local, BTF_SET8_KFUNCS) +#define BTF_HIDDEN_KFUNCS_START (name) \ +__BTF_SET8_START(name, local, 0) +#define BTF_KFUNCS_END (name) \ BTF_SET8_END(name)
@@ -230,6 +233,7 @@ BTF_SET8_END(name)#define BTF_SET8_START (name) static struct btf_id_set8 __maybe_unused name = { 0 };#define BTF_SET8_END (name)#define BTF_KFUNCS_START (name) static struct btf_id_set8 __maybe_unused name = { .flags = BTF_SET8_KFUNCS }; +#define BTF_HIDDEN_KFUNCS_START (name) static struct btf_id_set8 __maybe_unused name = { .flags = BTF_SET8_KFUNCS };#define BTF_KFUNCS_END (name)#endif /* CONFIG_DEBUG_INFO_BTF */
kernel/bpf/btf.c +100 -17
202 lines not on this page · raw message
kernel/bpf/verifier.c +4 -2
@@ -3030,13 +3030,14 @@ static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, s16 offset)return btf_vmlinux ?:ERR_PTR (-ENOENT ); } -static int add_kfunc_call (struct bpf_verifier_env *env,u32 func_id,s16 offset) +static int add_kfunc_call (struct bpf_verifier_env *env,struct bpf_insn *insn,s16 offset) {const struct btf_type *func, *func_proto;struct bpf_kfunc_btf_tab *btf_tab;struct bpf_kfunc_desc_tab *tab;struct bpf_prog_aux *prog_aux;struct bpf_kfunc_desc *desc; +u32 func_id = insn->imm;const char *func_name;struct btf *desc_btf;unsigned long call_imm;
@@ -3095,6 +3096,7 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)return PTR_ERR (desc_btf); } + func_id = insn->imm =btf_kfunc_id_remap (desc_btf, insn->imm, env->prog);if (find_kfunc_desc (env->prog, func_id, offset))return 0 ;
@@ -3228,7 +3230,7 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env)if (bpf_pseudo_func (insn) ||bpf_pseudo_call (insn)) ret =add_subprog (env, i + insn->imm +1 );else - ret =add_kfunc_call (env, insn->imm, insn->off); + ret =add_kfunc_call (env, insn, insn->off);if (ret <0 )return ret;
Signature · 1 line
2.43.5
Song Liu
reviewerPATCH v11 5/7 bpf: Use btf_kfunc_id_set.remap logic for bpf_dynptr_from_skb
In reply to Song Liubtf_kfunc_id_set.remap can pick proper version of a kfunc for the calling context. Use this logic to select bpf_dynptr_from_skb or bpf_dynptr_from_skb_rdonly. This will make the verifier simpler.
Unfortunately, btf_kfunc_id_set.remap cannot cover the DYNPTR_TYPE_SKB logic in check_kfunc_args(). This can be addressed later.
--- kernel/bpf/verifier.c | 25 ++++++---------------- net/core/filter.c | 49 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 23 deletions(-)
kernel/bpf/verifier.c +6 -19
@@ -11750,6 +11750,7 @@ enum special_kfunc_type { KF_bpf_rbtree_add_impl, KF_bpf_rbtree_first, KF_bpf_dynptr_from_skb, + KF_bpf_dynptr_from_skb_rdonly, KF_bpf_dynptr_from_xdp, KF_bpf_dynptr_slice, KF_bpf_dynptr_slice_rdwr,
@@ -11785,6 +11786,7 @@ BTF_ID(func, bpf_rbtree_add_impl) BTF_ID(func, bpf_rbtree_first)#ifdef CONFIG_NET BTF_ID(func, bpf_dynptr_from_skb) +BTF_ID(func, bpf_dynptr_from_skb_rdonly) BTF_ID(func, bpf_dynptr_from_xdp)#endif BTF_ID (func, bpf_dynptr_slice)
@@ -11816,10 +11818,12 @@ BTF_ID(func, bpf_rbtree_add_impl) BTF_ID(func, bpf_rbtree_first)#ifdef CONFIG_NET BTF_ID(func, bpf_dynptr_from_skb) +BTF_ID(func, bpf_dynptr_from_skb_rdonly) BTF_ID(func, bpf_dynptr_from_xdp)#else BTF_ID_UNUSED BTF_ID_UNUSED +BTF_ID_UNUSED #endif BTF_ID(func, bpf_dynptr_slice) BTF_ID(func, bpf_dynptr_slice_rdwr)
@@ -12741,7 +12745,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_if (is_kfunc_arg_uninit (btf, &args[i])) dynptr_arg_type |=MEM_UNINIT ; -if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { +if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb] || + meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb_rdonly]) { dynptr_arg_type |=DYNPTR_TYPE_SKB ; }else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) { dynptr_arg_type |=DYNPTR_TYPE_XDP ;
@@ -20898,9 +20903,7 @@ static void specialize_kfunc(struct bpf_verifier_env *env, u32 func_id, u16 offset, unsigned long *addr) { struct bpf_prog *prog = env->prog; -bool seen_direct_write;void *xdp_kfunc; -bool is_rdonly;if (bpf_dev_bound_kfunc_id (func_id)) { xdp_kfunc =bpf_dev_bound_resolve_kfunc (prog, func_id);
@@ -20910,22 +20913,6 @@ static void specialize_kfunc(struct bpf_verifier_env *env, }/* fallback to default kfunc when not supported by netdev */ } - -if (offset) -return ; - -if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { - seen_direct_write = env->seen_direct_write; - is_rdonly = !may_access_direct_pkt_data (env,NULL ,BPF_WRITE ); - -if (is_rdonly) - *addr = (unsigned long )bpf_dynptr_from_skb_rdonly; - -/* restore env->seen_direct_write to its original value, since -* may_access_direct_pkt_data mutates it -*/ - env->seen_direct_write = seen_direct_write; - } } static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,
net/core/filter.c +45 -4
74 lines not on this page · raw message
Signature · 1 line
2.43.5
Re: [PATCH v11 bpf-next 5/7] bpf: Use btf_kfunc_id_set.remap logic for bpf_dynptr_from_skb
Quoted 135 lines · Song Liu
btf_kfunc_id_set.remap can pick proper version of a kfunc for the calling context. Use this logic to select
bpf_dynptr_from_skborbpf_dynptr_from_skb_rdonly. This will make the verifier simpler.Unfortunately, btf_kfunc_id_set.remap cannot cover the
DYNPTR_TYPE_SKBlogic incheck_kfunc_args(). This can be addressed later.Signed-off-by Song Liu <[email protected]>--- kernel/bpf/verifier.c | 25 ++++++---------------- net/core/filter.c | 49 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 23 deletions(-)kernel/bpf/verifier.c +6
-19@@ -11750,6 +11750,7 @@ enum special_kfunc_type { KF_bpf_rbtree_add_impl, KF_bpf_rbtree_first, KF_bpf_dynptr_from_skb, + KF_bpf_dynptr_from_skb_rdonly, KF_bpf_dynptr_from_xdp, KF_bpf_dynptr_slice, KF_bpf_dynptr_slice_rdwr,@@ -11785,6 +11786,7 @@ BTF_ID(func, bpf_rbtree_add_impl) BTF_ID(func, bpf_rbtree_first)#ifdef CONFIG_NET BTF_ID(func, bpf_dynptr_from_skb) +BTF_ID(func, bpf_dynptr_from_skb_rdonly) BTF_ID(func, bpf_dynptr_from_xdp)#endif BTF_ID (func, bpf_dynptr_slice)@@ -11816,10 +11818,12 @@ BTF_ID(func, bpf_rbtree_add_impl) BTF_ID(func, bpf_rbtree_first)#ifdef CONFIG_NET BTF_ID(func, bpf_dynptr_from_skb) +BTF_ID(func, bpf_dynptr_from_skb_rdonly) BTF_ID(func, bpf_dynptr_from_xdp)#else BTF_ID_UNUSED BTF_ID_UNUSED +BTF_ID_UNUSED #endif BTF_ID(func, bpf_dynptr_slice) BTF_ID(func, bpf_dynptr_slice_rdwr)@@ -12741,7 +12745,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_if (is_kfunc_arg_uninit (btf, &args[i])) dynptr_arg_type |=MEM_UNINIT ; -if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { +if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb] || + meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb_rdonly]) { dynptr_arg_type |=DYNPTR_TYPE_SKB ; }else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) { dynptr_arg_type |=DYNPTR_TYPE_XDP ;@@ -20898,9 +20903,7 @@ static void specialize_kfunc(struct bpf_verifier_env *env, u32 func_id, u16 offset, unsigned long *addr) { struct bpf_prog *prog = env->prog; -bool seen_direct_write;void *xdp_kfunc; -bool is_rdonly;if (bpf_dev_bound_kfunc_id (func_id)) { xdp_kfunc =bpf_dev_bound_resolve_kfunc (prog, func_id);@@ -20910,22 +20913,6 @@ static void specialize_kfunc(struct bpf_verifier_env *env, }/* fallback to default kfunc when not supported by netdev */ } - -if (offset) -return ; - -if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) { - seen_direct_write = env->seen_direct_write; - is_rdonly = !may_access_direct_pkt_data (env,NULL ,BPF_WRITE ); - -if (is_rdonly) - *addr = (unsigned long )bpf_dynptr_from_skb_rdonly; - -/* restore env->seen_direct_write to its original value, since -* may_access_direct_pkt_data mutates it -*/ - env->seen_direct_write = seen_direct_write; - } } static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,net/core/filter.c +15
-4@@ -12062,10 +12062,8 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,#endif } -__bpf_kfunc_end_defs (); - -int bpf_dynptr_from_skb_rdonly (struct __sk_buff *skb,u64 flags, -struct bpf_dynptr *ptr__uninit) +__bpf_kfunc int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags, +struct bpf_dynptr *ptr__uninit) {struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;int err;@@ -12079,10 +12077,16 @@ int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,return 0 ; } +__bpf_kfunc_end_defs (); + BTF_KFUNCS_START(bpf_kfunc_check_set_skb) BTF_ID_FLAGS(func, bpf_dynptr_from_skb, KF_TRUSTED_ARGS) BTF_KFUNCS_END(bpf_kfunc_check_set_skb) +BTF_HIDDEN_KFUNCS_START(bpf_kfunc_check_hidden_set_skb) +BTF_ID_FLAGS(func, bpf_dynptr_from_skb_rdonly, KF_TRUSTED_ARGS) +BTF_KFUNCS_END(bpf_kfunc_check_hidden_set_skb) + BTF_KFUNCS_START(bpf_kfunc_check_set_xdp) BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) BTF_KFUNCS_END(bpf_kfunc_check_set_xdp)@@ -12095,9 +12099,46 @@ BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) BTF_ID_FLAGS(func, bpf_sk_assign_tcp_reqsk, KF_TRUSTED_ARGS)BTF_KFUNCS_END (bpf_kfunc_check_set_tcp_reqsk) +BTF_ID_LIST (bpf_dynptr_from_skb_list ) +BTF_ID (func, bpf_dynptr_from_skb) +BTF_ID (func, bpf_dynptr_from_skb_rdonly) + +static u32 bpf_kfunc_set_skb_remap(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (kfunc_id != bpf_dynptr_from_skb_list[0 ])
net/core/filter.c BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) · line 12118
Quoted 10 lines
net/core/filter.c +10
-0+return 0 ; + +switch (resolve_prog_type (prog)) { +/* Program types only with direct read access go here! */ +case BPF_PROG_TYPE_LWT_IN : +case BPF_PROG_TYPE_LWT_OUT : +case BPF_PROG_TYPE_LWT_SEG6LOCAL : +case BPF_PROG_TYPE_SK_REUSEPORT : +case BPF_PROG_TYPE_FLOW_DISSECTOR : +case BPF_PROG_TYPE_CGROUP_SKB :
This copy pastes the logic from may_access_direct_pkt_data(), so any future change to that helper would need to update this one as well.
net/core/filter.c BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) · line 12119
Quoted 1 line
net/core/filter.c +1
-0+return bpf_dynptr_from_skb_list[1 ];
The [0] and [1] stuff is quite error prone.
Quoted 11 lines
net/core/filter.c +11
-0+ +/* Program types with direct read + write access go here! */ +case BPF_PROG_TYPE_SCHED_CLS : +case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_XDP : +case BPF_PROG_TYPE_LWT_XMIT : +case BPF_PROG_TYPE_SK_SKB : +case BPF_PROG_TYPE_SK_MSG : +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +return kfunc_id; +
net/core/filter.c BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) · line 12140
Quoted 10 lines
net/core/filter.c +7
-0+default : +break ; + } +return bpf_dynptr_from_skb_list[1 ]; +} +static const struct btf_kfunc_id_set bpf_kfunc_set_skb = { .owner =THIS_MODULE , .set = &bpf_kfunc_check_set_skb, + .hidden_set = &bpf_kfunc_check_hidden_set_skb,
If I'm reading it correctly the hidden_set serves no additional purpose. It splits the set into two, but patch 4 just adds them together.
net/core/filter.c BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) · line 12141
Quoted 1 line
net/core/filter.c +1
-0+ .remap = &bpf_kfunc_set_skb_remap,
I'm not a fan of callbacks in general.
The makes everything harder to follow.
For all these reasons I don't like this approach. This "generality" doesn't make it cleaner or easier to extend. For the patch 6... just repeat what specialize_kfunc() currently does for dynptr ?
pw-bot: cr
Re: [PATCH v11 bpf-next 5/7] bpf: Use btf_kfunc_id_set.remap logic for bpf_dynptr_from_skb
Hi Alexei,
Thanks for the review!
Quoted 1 line · Alexei Starovoitov
[...]
Quoted 14 lines
Quoted 14 lines
net/core/filter.c +13
-0+BTF_ID_LIST (bpf_dynptr_from_skb_list) +BTF_ID (func ,bpf_dynptr_from_skb ) +BTF_ID (func, bpf_dynptr_from_skb_rdonly) + +static u32 bpf_kfunc_set_skb_remap(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (kfunc_id != bpf_dynptr_from_skb_list[0 ]) +return 0 ; + +switch (resolve_prog_type (prog)) { +/* Program types only with direct read access go here! */ +case BPF_PROG_TYPE_LWT_IN : +case BPF_PROG_TYPE_LWT_OUT :
Quoted 8 lines
net/core/filter.c BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) · line 12118
Quoted 4 lines
net/core/filter.c +4
-0+case BPF_PROG_TYPE_LWT_SEG6LOCAL : +case BPF_PROG_TYPE_SK_REUSEPORT : +case BPF_PROG_TYPE_FLOW_DISSECTOR : +case BPF_PROG_TYPE_CGROUP_SKB :This copy pastes the logic from
may_access_direct_pkt_data(), so any future change to that helper would need to update this one as well.
We can probably improve this with some helpers/macros.
Quoted 21 lines
Quoted 2 lines
+ return bpf_dynptr_from_skb_list[1];
The [0] and [1] stuff is quite error prone.
Quoted 16 lines
net/core/filter.c +16
-0+ +/* Program types with direct read + write access go here! */ +case BPF_PROG_TYPE_SCHED_CLS : +case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_XDP : +case BPF_PROG_TYPE_LWT_XMIT : +case BPF_PROG_TYPE_SK_SKB : +case BPF_PROG_TYPE_SK_MSG : +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +return kfunc_id; + +default : +break ; + } +return bpf_dynptr_from_skb_list[1 ]; +}
Quoted 8 lines
Quoted 5 lines
+
static const structbtf_kfunc_id_setbpf_kfunc_set_skb= {
.owner =THIS_MODULE,
.set =&bpf_kfunc_check_set_skb,
+ .hidden_set =&bpf_kfunc_check_hidden_set_skb,If I'm reading it correctly the
hidden_setserves no additional purpose. It splits the set into two, but patch 4 just adds them together.
hidden_set does not have BTF_SET8_KFUNCS, so pahole will not export these kfuncs to vmlinux.h.
Quoted 5 lines
Quoted 2 lines
+ .remap =
&bpf_kfunc_set_skb_remap,I'm not a fan of callbacks in general.
The makes everything harder to follow.
This motivation here is to move polymorphism logic from verifier core to kfuncs owners. I guess we will need some callback to achieve this goal. Of course, we don't have to do it in this set.
Quoted 4 lines
For all these reasons I don't like this approach. This "generality" doesn't make it cleaner or easier to extend. For the patch 6... just repeat what
specialize_kfunc()currently does for dynptr ?
Yes, specialize_kfunc() can handle this. But we will need to use d_inode_locked_hooks from 6/7 in specialize_kfunc(). It works, but it is not clean (to me).
I will revise this set so that the polymorphism logic in handled in specialize_kfunc(). For longer term, maybe we should discuss "move some logic from verifier core to kfuncs" in the upcoming LSF/MM/BPF?
Thanks,
Song
Re: [PATCH v11 bpf-next 5/7] bpf: Use btf_kfunc_id_set.remap logic for bpf_dynptr_from_skb
Quoted 78 lines · Song Liu
Hi Alexei,
Thanks for the review!
Quoted 1 line · Alexei Starovoitov
[...]
Quoted 14 lines
Quoted 14 lines
net/core/filter.c +13
-0+BTF_ID_LIST (bpf_dynptr_from_skb_list) +BTF_ID (func ,bpf_dynptr_from_skb ) +BTF_ID (func, bpf_dynptr_from_skb_rdonly) + +static u32 bpf_kfunc_set_skb_remap(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (kfunc_id != bpf_dynptr_from_skb_list[0 ]) +return 0 ; + +switch (resolve_prog_type (prog)) { +/* Program types only with direct read access go here! */ +case BPF_PROG_TYPE_LWT_IN : +case BPF_PROG_TYPE_LWT_OUT :Quoted 8 lines
net/core/filter.c BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk) · line 12118
Quoted 4 lines
net/core/filter.c +4
-0+case BPF_PROG_TYPE_LWT_SEG6LOCAL : +case BPF_PROG_TYPE_SK_REUSEPORT : +case BPF_PROG_TYPE_FLOW_DISSECTOR : +case BPF_PROG_TYPE_CGROUP_SKB :This copy pastes the logic from
may_access_direct_pkt_data(), so any future change to that helper would need to update this one as well.We can probably improve this with some helpers/macros.
Quoted 21 lines
Quoted 2 lines
+ return bpf_dynptr_from_skb_list[1];
The [0] and [1] stuff is quite error prone.
Quoted 16 lines
net/core/filter.c +16
-0+ +/* Program types with direct read + write access go here! */ +case BPF_PROG_TYPE_SCHED_CLS : +case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_XDP : +case BPF_PROG_TYPE_LWT_XMIT : +case BPF_PROG_TYPE_SK_SKB : +case BPF_PROG_TYPE_SK_MSG : +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +return kfunc_id; + +default : +break ; + } +return bpf_dynptr_from_skb_list[1 ]; +}Quoted 8 lines
Quoted 5 lines
+
static const structbtf_kfunc_id_setbpf_kfunc_set_skb= {
.owner =THIS_MODULE,
.set =&bpf_kfunc_check_set_skb,
+ .hidden_set =&bpf_kfunc_check_hidden_set_skb,If I'm reading it correctly the
hidden_setserves no additional purpose. It splits the set into two, but patch 4 just adds them together.
hidden_setdoes not haveBTF_SET8_KFUNCS, so pahole will not export these kfuncs tovmlinux.h.Quoted 5 lines
Quoted 2 lines
+ .remap =
&bpf_kfunc_set_skb_remap,I'm not a fan of callbacks in general.
The makes everything harder to follow.This motivation here is to move polymorphism logic from verifier core to kfuncs owners. I guess we will need some callback to achieve this goal. Of course, we don't have to do it in this set.
Quoted 8 lines
Quoted 4 lines
For all these reasons I don't like this approach. This "generality" doesn't make it cleaner or easier to extend. For the patch 6... just repeat what
specialize_kfunc()currently does for dynptr ?Yes,
specialize_kfunc()can handle this. But we will need to used_inode_locked_hooksfrom 6/7 inspecialize_kfunc(). It works, but it is not clean (to me).
I'm missing why that would be necessary to cross the layers so much. I guess the code will tell.
Pls send an rfc to illustrate the unclean part.
Quoted 4 lines
I will revise this set so that the polymorphism logic in handled in
specialize_kfunc(). For longer term, maybe we should discuss "move some logic from verifier core to kfuncs" in the upcoming LSF/MM/BPF?
imo such topic is too narrow and detail oriented. There is not much to gain from discussing it at lsfmm. email works well for such discussions.
Re: [PATCH v11 bpf-next 5/7] bpf: Use btf_kfunc_id_set.remap logic for bpf_dynptr_from_skb
Quoted 2 lines · Alexei Starovoitov
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
For all these reasons I don't like this approach. This "generality" doesn't make it cleaner or easier to extend. For the patch 6... just repeat what
specialize_kfunc()currently does for dynptr ?
Quoted 7 lines
Quoted 3 lines
Yes,
specialize_kfunc()can handle this. But we will need to used_inode_locked_hooksfrom 6/7 inspecialize_kfunc(). It works, but it is not clean (to me).I'm missing why that would be necessary to cross the layers so much. I guess the code will tell.
Pls send an rfc to illustrate the unclean part.
The actual code is actually a lot cleaner than I thought. We just need to use the bpf_lsm_has_d_inode_locked() helper in verifier.c.
Thanks,
Song
Quoted 9 lines
Quoted 5 lines
I will revise this set so that the polymorphism logic in handled in
specialize_kfunc(). For longer term, maybe we should discuss "move some logic from verifier core to kfuncs" in the upcoming LSF/MM/BPF?imo such topic is too narrow and detail oriented. There is not much to gain from discussing it at lsfmm. email works well for such discussions.
Song Liu
reviewerPATCH v11 6/7 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. Instead of requiring the user to know which version of the kfuncs to use, the verifier will pick the proper kfunc based on the calling hook.
--- fs/bpf_fs_kfuncs.c | 219 +++++++++++++++++++++++++++++++++++++++- include/linux/bpf_lsm.h | 2 + 2 files changed, 219 insertions(+), 2 deletions(-)
fs/bpf_fs_kfuncs.c +217 -2
@@ -2,10 +2,12 @@/* Copyright (c) 2024 Google LLC. */ #include <linux/bpf.h> +#include <linux/bpf_lsm.h> #include <linux/btf.h> #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 +163,156 @@ __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 ); +} + +/** +* 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) +{ + +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 ; + + ret =bpf_xattr_write_permission (name__str, inode); +if (ret) +return ret; + + ret =__vfs_setxattr (&nop_mnt_idmap, dentry, inode, name__str, + 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. Otherwise, we would +* risk deadlocks by calling back to the same kfunc. +* +* This is the same as security_inode_setsecurity(). +*/ + } +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) +{ +struct inode *inode =d_inode (dentry); +int ret; + +inode_lock (inode); + ret =bpf_set_dentry_xattr_locked (dentry, name__str, value_p, flags); +inode_unlock (inode); +return ret; +} + +/** +* 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) +{ +struct inode *inode =d_inode (dentry); +int ret; + + ret =bpf_xattr_write_permission (name__str, inode); +if (ret) +return ret; + + 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. Otherwise, we would +* risk deadlocks by calling back to the same kfunc. +*/ + } +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) +{ +struct inode *inode =d_inode (dentry); +int ret; + +inode_lock (inode); + ret =bpf_remove_dentry_xattr_locked (dentry, name__str); +inode_unlock (inode); +return ret; +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (bpf_fs_kfunc_set_ids)
@@ -170,20 +322,83 @@ 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_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) +BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) +BTF_HIDDEN_KFUNCS_START (bpf_fs_kfunc_hidden_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_fs_kfunc_hidden_set_ids) + static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) { -if (!btf_id_set8_contains (&bpf_fs_kfunc_set_ids, kfunc_id) || - prog->type ==BPF_PROG_TYPE_LSM ) +if (!btf_id_set8_contains (&bpf_fs_kfunc_set_ids, kfunc_id) && + !btf_id_set8_contains (&bpf_fs_kfunc_hidden_set_ids, kfunc_id)) +return 0 ; +if (prog->type ==BPF_PROG_TYPE_LSM )return 0 ;return -EACCES ; } +/* 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_ID_LIST (not_locked_fs_kfuncs) +BTF_ID (func ,bpf_set_dentry_xattr ) +BTF_ID (func, bpf_remove_dentry_xattr) + +BTF_ID_LIST (locked_fs_kfuncs) +BTF_ID (func, bpf_set_dentry_xattr_locked) +BTF_ID (func, bpf_remove_dentry_xattr_locked) + +static u32 bpf_fs_kfunc_remap(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (!bpf_lsm_has_d_inode_locked (prog)) +return 0 ; + +if (kfunc_id == not_locked_fs_kfuncs[0 ]) +return locked_fs_kfuncs[0 ]; +if (kfunc_id == not_locked_fs_kfuncs[1 ]) +return locked_fs_kfuncs[1 ]; + +return 0 ; +} +static const struct btf_kfunc_id_set bpf_fs_kfunc_set = { .owner =THIS_MODULE , .set = &bpf_fs_kfunc_set_ids, + .hidden_set = &bpf_fs_kfunc_hidden_set_ids, .filter = bpf_fs_kfuncs_filter, + .remap = bpf_fs_kfunc_remap, };static int __init bpf_fs_kfuncs_init (void )
include/linux/bpf_lsm.h +2 -0
@@ -48,6 +48,7 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, bpf_func_t *bpf_func)int bpf_lsm_get_retval_range (const struct bpf_prog *prog,struct bpf_retval_range *range); +#else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook (u32 btf_id)
@@ -86,6 +87,7 @@ static inline int bpf_lsm_get_retval_range(const struct bpf_prog *prog, {return -EOPNOTSUPP ; } +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
Signature · 1 line
2.43.5
Song Liu
reviewerPATCH v11 7/7 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.
Note that, in both tests, the BPF programs use the not _locked kfuncs. The verifier picks the proper kfuncs based on the calling context.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 5 + .../selftests/bpf/prog_tests/fs_kfuncs.c | 125 ++++++++++++++++ .../bpf/progs/test_set_remove_xattr.c | 133 ++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_set_remove_xattr.c
tools/testing/selftests/bpf/bpf_kfuncs.h +5 -0
@@ -87,4 +87,9 @@ 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(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 +133 -0
133 lines not on this page · raw message
Signature · 1 line
2.43.5