bpf: File verification with LSM and fsverity
Subsystems (10)
Song Liu
maintainerv13 cover bpf: File verification with LSM and fsverity
Changes v12 => v13:
1. Only keep 4/9 through 9/9 of v12, as the first 3 patches already applied;
2. Use new macro __bpf_kfunc_[start|end]_defs().
Changes v11 => v12:
1. Fix typo (data_ptr => sig_ptr) in bpf_get_file_xattr().
Changes v10 => v11:
1. Let __bpf_dynptr_data() return const void *. (Andrii)
2. Optimize code to reuse output from __bpf_dynptr_size(). (Andrii)
3. Add __diag_ignore_all("-Wmissing-declarations") for kfunc definition.
4. Fix an off indentation. (Andrii)
Changes v9 => v10:
1. Remove WARN_ON_ONCE() from check_reg_const_str. (Alexei)
Changes v8 => v9:
1. Fix test_progs kfunc_dynptr_param/dynptr_data_null.
Changes v7 => v8:
1. Do not use bpf_dynptr_slice* in the kernel. Add __bpf_dynptr_data* and use them in ther kernel. (Andrii)
Changes v6 => v7:
1. Change "__const_str" annotation to "__str". (Alexei, Andrii)
2. Add KF_TRUSTED_ARGS flag for both new kfuncs. (KP)
3. Only allow bpf_get_file_xattr() to read xattr with "user." prefix.
4. Add Acked-by from Eric Biggers.
Changes v5 => v6:
1. Let fsverity_init_bpf() return void. (Eric Biggers)
2. Sort things in alphabetic orders. (Eric Biggers)
Changes v4 => v5:
1. Revise commit logs. (Alexei)
Changes v3 => v4:
1. Fix error reported by CI.
2. Update comments of bpf_dynptr_slice* that they may return error pointer.
Changes v2 => v3:
1. Rebase and resolve conflicts.
Changes v1 => v2:
1. Let bpf_get_file_xattr() use const string for arg "name". (Alexei)
2. Add recursion prevention with allowlist. (Alexei)
3. Let bpf_get_file_xattr() use __vfs_getxattr() to avoid recursion, as vfs_getxattr() calls into other LSM hooks.
4. Do not use dynptr->data directly, use helper insteadd. (Andrii)
5. Fixes with bpf_get_fsverity_digest. (Eric Biggers)
6. Add documentation. (Eric Biggers)
7. Fix some compile warnings. (kernel test robot)
This set enables file verification with BPF LSM and fsverity.
In this solution, fsverity is used to provide reliable and efficient hash of files; and BPF LSM is used to implement signature verification (against asymmetric keys), and to enforce access control.
This solution can be used to implement access control in complicated cases. For example: only signed python binary and signed python script and access special files/devices/ports.
Thanks,
Song
Song Liu (6):
bpf: Add kfunc bpf_get_file_xattr
bpf, fsverity: Add kfunc bpf_get_fsverity_digest Documentation/bpf: Add documentation for filesystem kfuncs selftests/bpf: Sort config in alphabetic order selftests/bpf: Add tests for filesystem kfuncs selftests/bpf: Add test that uses fsverity and xattr to sign a file
Documentation/bpf/fs_kfuncs.rst | 21 +++ Documentation/bpf/index.rst | 1 + fs/verity/fsverity_private.h | 10 ++ fs/verity/init.c | 1 + fs/verity/measure.c | 84 +++++++++ kernel/trace/bpf_trace.c | 63 +++++++ tools/testing/selftests/bpf/bpf_kfuncs.h | 10 ++ tools/testing/selftests/bpf/config | 3 +- .../selftests/bpf/prog_tests/fs_kfuncs.c | 132 ++++++++++++++ .../bpf/prog_tests/verify_pkcs7_sig.c | 163 +++++++++++++++++- .../selftests/bpf/progs/test_fsverity.c | 46 +++++ .../selftests/bpf/progs/test_get_xattr.c | 37 ++++ .../selftests/bpf/progs/test_sig_in_xattr.c | 82 +++++++++ .../bpf/progs/test_verify_pkcs7_sig.c | 8 +- .../testing/selftests/bpf/verify_sig_setup.sh | 25 +++ 15 files changed, 677 insertions(+), 9 deletions(-) create mode 100644 Documentation/bpf/fs_kfuncs.rst create mode 100644 tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/test_fsverity.c create mode 100644 tools/testing/selftests/bpf/progs/test_get_xattr.c create mode 100644 tools/testing/selftests/bpf/progs/test_sig_in_xattr.c
--
2.34.1
Song Liu
maintainerPATCH v13 1/6 bpf: Add kfunc bpf_get_file_xattr
It is common practice for security solutions to store tags/labels in xattrs. To implement similar functionalities in BPF LSM, add new kfunc bpf_get_file_xattr().
The first use case of bpf_get_file_xattr() is to implement file verifications with asymmetric keys. Specificially, security applications could use fsverity for file hashes and use xattr to store file signatures. (kfunc for fsverity hash will be added in a separate commit.)
Currently, only xattrs with "user." prefix can be read with kfunc bpf_get_file_xattr(). As use cases evolve, we may add a dedicated prefix for bpf_get_file_xattr().
To avoid recursion, bpf_get_file_xattr can be only called from LSM hooks.
--- kernel/trace/bpf_trace.c | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
kernel/trace/bpf_trace.c +63 -0
@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/verification.h> #include <linux/namei.h> +#include <linux/fileattr.h> #include <net/bpf_sk_storage.h>
@@ -1431,6 +1432,68 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__bpf_kfunc_start_defs (); + +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__str* of *file* and store the output in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "user." is allowed. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +u32 value_len; +void *value; + +if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data_rw (value_ptr, value_len); +if (!value) +return -EINVAL ; + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__str, value, value_len); +} + +__bpf_kfunc_end_defs (); + +BTF_SET8_START (fs_kfunc_set_ids) +BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_SET8_END (fs_kfunc_set_ids) + +static int bpf_get_file_xattr_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ + if (!btf_id_set8_contains(&fs_kfunc_set_ids, kfunc_id)) + return 0; + + /* Only allow to attach from LSM hooks, to avoid recursion */ + return prog->type != BPF_PROG_TYPE_LSM ? -EACCES : 0; +} + +const struct btf_kfunc_id_set bpf_fs_kfunc_set = { + .owner =THIS_MODULE , + .set = &fs_kfunc_set_ids, + .filter = bpf_get_file_xattr_filter, +}; + +static int __init bpf_fs_kfuncs_init(void) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_fs_kfunc_set); +} + +late_initcall (bpf_fs_kfuncs_init); +static const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {
Signature · 1 line
2.34.1
Song Liu
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Quoted 77 lines · Song Liu
It is common practice for security solutions to store tags/labels in xattrs. To implement similar functionalities in BPF LSM, add new kfunc
bpf_get_file_xattr().The first use case of
bpf_get_file_xattr()is to implement file verifications with asymmetric keys. Specificially, security applications could use fsverity for file hashes and use xattr to store file signatures. (kfunc for fsverity hash will be added in a separate commit.)Currently, only xattrs with "user." prefix can be read with kfunc
bpf_get_file_xattr(). As use cases evolve, we may add a dedicated prefix forbpf_get_file_xattr().To avoid recursion,
bpf_get_file_xattrcan be only called from LSM hooks.Signed-off-by Song Liu <[email protected]>--- kernel/trace/bpf_trace.c | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)kernel/trace/bpf_trace.c +41
-0@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/verification.h> #include <linux/namei.h> +#include <linux/fileattr.h> #include <net/bpf_sk_storage.h> @@ -1431,6 +1432,68 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__bpf_kfunc_start_defs (); + +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__str* of *file* and store the output in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "user." is allowed. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +u32 value_len; +void *value; + +if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data_rw (value_ptr, value_len); +if (!value) +return -EINVAL ; + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__str, value, value_len); +} + +__bpf_kfunc_end_defs (); + +BTF_SET8_START(fs_kfunc_set_ids) +BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_SET8_END(fs_kfunc_set_ids) +
kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1484
Quoted 10 lines
kernel/trace/bpf_trace.c +10
-0+static int bpf_get_file_xattr_filter (const struct bpf_prog *prog,u32 kfunc_id) +{ +if (!btf_id_set8_contains (&fs_kfunc_set_ids, kfunc_id)) +return 0 ; + +/* Only allow to attach from LSM hooks, to avoid recursion */ +return prog->type !=BPF_PROG_TYPE_LSM ? -EACCES :0 ; +} + +const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
Missed static here. If there will be v14, I will fix it here.
Thanks,
Song
Quoted 18 lines
kernel/trace/bpf_trace.c +12
-1+ .owner = THIS_MODULE, + .set = &fs_kfunc_set_ids, + .filter = bpf_get_file_xattr_filter, +}; + +static int __init bpf_fs_kfuncs_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_fs_kfunc_set); +} + +late_initcall (bpf_fs_kfuncs_init); +static const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) { --2.34.1
Christian Brauner
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
In reply to Song LiuQuoted 9 lines · Song Liu
It is common practice for security solutions to store tags/labels in xattrs. To implement similar functionalities in BPF LSM, add new kfunc
bpf_get_file_xattr().The first use case of
bpf_get_file_xattr()is to implement file verifications with asymmetric keys. Specificially, security applications could use fsverity for file hashes and use xattr to store file signatures. (kfunc for fsverity hash will be added in a separate commit.)
Quoted 8 lines
Currently, only xattrs with "user." prefix can be read with kfunc
bpf_get_file_xattr(). As use cases evolve, we may add a dedicated prefix forbpf_get_file_xattr().To avoid recursion,
bpf_get_file_xattrcan be only called from LSM hooks.Signed-off-by Song Liu <[email protected]>---
Looks ok to me. But see below for a question.
If you ever allow the retrieval of additional extended attributes through bfs_get_file_xattr() or other bpf interfaces we would like to be Cced, please. The xattr stuff is (/me looks for suitable words)...
Over the last months we've moved POSIX_ACL retrieval out of these low-level functions. They now have a dedicated api. The same is going to happen for fscaps as well.
But even with these out of the way we would want the bpf helpers to always maintain an allowlist of retrievable attributes.
Quoted 41 lines
kernel/trace/bpf_trace.c | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)kernel/trace/bpf_trace.c +23
-0@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/verification.h> #include <linux/namei.h> +#include <linux/fileattr.h> #include <net/bpf_sk_storage.h> @@ -1431,6 +1432,68 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__bpf_kfunc_start_defs (); + +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__str* of *file* and store the output in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "user." is allowed. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +u32 value_len; +void *value; +
kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1466
Quoted 10 lines
kernel/trace/bpf_trace.c +10
-0+if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data_rw (value_ptr, value_len); +if (!value) +return -EINVAL ; + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__str, value, value_len);
By calling __vfs_getxattr() from bpf_get_file_xattr() you're skipping at least inode_permission() from xattr_permission(). I'm probably just missing or forgot the context. But why is that ok?
Quoted 36 lines
kernel/trace/bpf_trace.c +30
-0+} + +__bpf_kfunc_end_defs (); + +BTF_SET8_START (fs_kfunc_set_ids) +BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_SET8_END (fs_kfunc_set_ids) + +static int bpf_get_file_xattr_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ + if (!btf_id_set8_contains(&fs_kfunc_set_ids, kfunc_id)) + return 0; + + /* Only allow to attach from LSM hooks, to avoid recursion */ + return prog->type != BPF_PROG_TYPE_LSM ? -EACCES : 0; +} + +const struct btf_kfunc_id_set bpf_fs_kfunc_set = { + .owner =THIS_MODULE , + .set = &fs_kfunc_set_ids, + .filter = bpf_get_file_xattr_filter, +}; + +static int __init bpf_fs_kfuncs_init(void) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_LSM , &bpf_fs_kfunc_set); +} + +late_initcall (bpf_fs_kfuncs_init); +static const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {Signature · 1 line
2.34.1
Song Liu
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Quoted 26 lines · Christian Brauner
Quoted 9 lines · Song Liu
It is common practice for security solutions to store tags/labels in xattrs. To implement similar functionalities in BPF LSM, add new kfunc
bpf_get_file_xattr().The first use case of
bpf_get_file_xattr()is to implement file verifications with asymmetric keys. Specificially, security applications could use fsverity for file hashes and use xattr to store file signatures. (kfunc for fsverity hash will be added in a separate commit.)Quoted 8 lines
Currently, only xattrs with "user." prefix can be read with kfunc
bpf_get_file_xattr(). As use cases evolve, we may add a dedicated prefix forbpf_get_file_xattr().To avoid recursion,
bpf_get_file_xattrcan be only called from LSM hooks.Signed-off-by Song Liu <[email protected]>---
Looks ok to me. But see below for a question.
If you ever allow the retrieval of additional extended attributes through
bfs_get_file_xattr()or other bpf interfaces we would like to be Cced, please. The xattr stuff is (/me looks for suitable words)...
Quoted 6 lines
Over the last months we've moved
POSIX_ACLretrieval out of these low-level functions. They now have a dedicated api. The same is going to happen for fscaps as well.But even with these out of the way we would want the bpf helpers to always maintain an allowlist of retrievable attributes.
Agreed. We will be very specific which attributes are available to bpf helpers/kfuncs.
Quoted 48 lines
Quoted 48 lines
kernel/trace/bpf_trace.c | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)kernel/trace/bpf_trace.c +29
-0@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/verification.h> #include <linux/namei.h> +#include <linux/fileattr.h> #include <net/bpf_sk_storage.h> @@ -1431,6 +1432,68 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__bpf_kfunc_start_defs (); + +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__str* of *file* and store the output in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "user." is allowed. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +u32 value_len; +void *value; + +if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data_rw (value_ptr, value_len); +if (!value)
Quoted 8 lines
kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1466
Quoted 4 lines
kernel/trace/bpf_trace.c +4
-0+return -EINVAL ; + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__str, value, value_len);By calling
__vfs_getxattr()frombpf_get_file_xattr()you're skipping at leastinode_permission()fromxattr_permission(). I'm probably just missing or forgot the context. But why is that ok?
AFAICT, the XATTR_USER_PREFIX above is equivalent to the prefix check in xattr_permission().
For inode_permission(), I think it is not required because we already have the "struct file" of the target file. Did I misunderstand something here?
Thanks,
Song
Quoted 2 lines
Quoted 2 lines
kernel/trace/bpf_trace.c +2
-0+} +
[...]
Christian Brauner
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Quoted 94 lines · Song Liu
Quoted 26 lines · Christian Brauner
Quoted 9 lines · Song Liu
It is common practice for security solutions to store tags/labels in xattrs. To implement similar functionalities in BPF LSM, add new kfunc
bpf_get_file_xattr().The first use case of
bpf_get_file_xattr()is to implement file verifications with asymmetric keys. Specificially, security applications could use fsverity for file hashes and use xattr to store file signatures. (kfunc for fsverity hash will be added in a separate commit.)Quoted 8 lines
Currently, only xattrs with "user." prefix can be read with kfunc
bpf_get_file_xattr(). As use cases evolve, we may add a dedicated prefix forbpf_get_file_xattr().To avoid recursion,
bpf_get_file_xattrcan be only called from LSM hooks.Signed-off-by Song Liu <[email protected]>---
Looks ok to me. But see below for a question.
If you ever allow the retrieval of additional extended attributes through
bfs_get_file_xattr()or other bpf interfaces we would like to be Cced, please. The xattr stuff is (/me looks for suitable words)...Quoted 6 lines
Over the last months we've moved
POSIX_ACLretrieval out of these low-level functions. They now have a dedicated api. The same is going to happen for fscaps as well.But even with these out of the way we would want the bpf helpers to always maintain an allowlist of retrievable attributes.
Agreed. We will be very specific which attributes are available to bpf helpers/kfuncs.
Quoted 48 lines
Quoted 48 lines
kernel/trace/bpf_trace.c | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)kernel/trace/bpf_trace.c +29
-0@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/verification.h> #include <linux/namei.h> +#include <linux/fileattr.h> #include <net/bpf_sk_storage.h> @@ -1431,6 +1432,68 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__bpf_kfunc_start_defs (); + +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__str* of *file* and store the output in *value_ptr*. +* +* For security reasons, only *name__str* with prefix "user." is allowed. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +u32 value_len; +void *value; + +if (strncmp (name__str,XATTR_USER_PREFIX ,XATTR_USER_PREFIX_LEN )) +return -EPERM ; + + value_len =__bpf_dynptr_size (value_ptr); + value =__bpf_dynptr_data_rw (value_ptr, value_len); +if (!value)Quoted 8 lines
kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1466
Quoted 4 lines
kernel/trace/bpf_trace.c +4
-0+return -EINVAL ; + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__str, value, value_len);By calling
__vfs_getxattr()frombpf_get_file_xattr()you're skipping at leastinode_permission()fromxattr_permission(). I'm probably just missing or forgot the context. But why is that ok?
Quoted 6 lines
AFAICT, the
XATTR_USER_PREFIXabove is equivalent to the prefix check inxattr_permission().For
inode_permission(), I think it is not required because we already have the "struct file" of the target file. Did I misunderstand something here?
I had overlooked that you don't allow writing xattrs. But there's still some issues:
So if you look at the system call interface:
fgetxattr(fd)
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_permission()
-> __vfs_getxattr()and io_uring:
do_getxattr()
-> vfs_getxattr()
-> xattr_permission()
-> __vfs_getxattr()
you can see that xattr_permission() is a _read/write-time check_, not an open check. That's because the read/write permissions may depend on what xattr is read/written. Since you don't know what xattr will be read/written at open-time.
So there needs to be a good reason for bpf_get_file_xattr() to deviate from the system call and io_uring interface. And I'd like to hear it, please. :)
I think I might see the argument because you document the helper as "may only be called from BPF LSM function" in which case you're trying to say that bpf_get_file_xattr() is equivalent to a call to __vfs_getxattr() from an LSM to get at it's own security xattr.
But if that's the case you really should have a way to verify that these helpers are only callable from a specific BPF context. Because you otherwise omit read/write-time permission checking when retrieving xattrs which is a potentialy security issue and may be abused by a BPF program to skip permission checks that are otherwise enforced.
Is there a way for BPF to enforce/verify that a function is only called from a specific BPF program? It should be able to recognize that, no? And then refuse to load that BPF program if a helper is called outside it's intended context.
Song Liu
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Hi Christian,
Thanks again for your comments.
Quoted 1 line · Christian Brauner
[...]
Quoted 41 lines
Quoted 7 lines
AFAICT, the
XATTR_USER_PREFIXabove is equivalent to the prefix check inxattr_permission().For
inode_permission(), I think it is not required because we already have the "struct file" of the target file. Did I misunderstand something here?I had overlooked that you don't allow writing xattrs. But there's still some issues:
So if you look at the system call interface:
fgetxattr(fd)
->getxattr()
->do_getxattr()-> vfs_getxattr() -> xattr_permission() -> __vfs_getxattr()and
io_uring:
do_getxattr()
->vfs_getxattr()
->xattr_permission()
->__vfs_getxattr()you can see that
xattr_permission()is a _read/write-timecheck_, not an open check. That's because the read/write permissions may depend on what xattr is read/written. Since you don't know what xattr will be read/written at open-time.So there needs to be a good reason for
bpf_get_file_xattr()to deviate from the system call andio_uringinterface. And I'd like to hear it, please. :)I think I might see the argument because you document the helper as "may only be called from BPF LSM function" in which case you're trying to say that
bpf_get_file_xattr()is equivalent to a call to__vfs_getxattr()from an LSM to get at it's own security xattr.
Quoted 5 lines
But if that's the case you really should have a way to verify that these helpers are only callable from a specific BPF context. Because you otherwise omit read/write-time permission checking when retrieving xattrs which is a potentialy security issue and may be abused by a BPF program to skip permission checks that are otherwise enforced.
What do you mean by "a specific BPF context"? Current implementation makes sure the helper only works on LSM hooks with "struct file *" in the argument list. Specifically, we can only use them from the following hooks:
security_binder_transfer_file
security_bprm_creds_from_file
security_file_permission
security_file_alloc_security
security_file_free_security
security_file_ioctl
security_mmap_file
security_file_lock
security_file_fcntl
security_file_set_fowner
security_file_receive
security_file_open
security_file_truncate
security_kernel_read_file
security_kernel_post_read_file
Note that, we disallow pointer-walking with the kfunc, so the kfunc is not allowed from hooks with indirect access to "struct file". For example, we cannot use it with security_bprm_creds_for_exec(struct linux_binprm *bprm) as this hook only has bprm, and calling bpf_get_file_xattr(bprm->file) is not allowed.
Quoted 4 lines
Is there a way for BPF to enforce/verify that a function is only called from a specific BPF program? It should be able to recognize that, no? And then refuse to load that BPF program if a helper is called outside it's intended context.
Similarly, I am not quite sure what you mean by "a specific BPF program". My answer to this is probably the same as above.
Going back to xattr_permission itself. AFAICT, it does 3 checks:
1. MAY_WRITE check;
2. prefix check;
3. inode_permission().
We don't need MAY_WRITE check as bpf_get_file_xattr is read only. We have the prefix check embedded in bpf_get_file_xattr():
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
return -EPERM;
inode_permission() is a little trickier here, which checks against idmap. However, I don't think the check makes sense in the context of LSM. In this case, we have two processes: one security daemon, which owns the BPF LSM program, and a process being monitored. idmap here, from file_mnt_idmap(file), is the idmap from the being monitored process. However, whether the BPF LSM program have the permission to read the xattr should be determined by the security daemon.
Overall, we can technically add xattr_permission() check here. But I don't think that's the right check for the LSM use case.
Does this make sense? Did I miss or misunderstand something?
Thanks,
Song
Song Liu
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Quoted 7 lines · Song Liu
Hi Christian,
Thanks again for your comments.
Quoted 1 line · Christian Brauner
[...]
Quoted 12 lines
Going back to
xattr_permissionitself. AFAICT, it does 3 checks:1.
MAY_WRITEcheck;
2. prefix check;
3.inode_permission().We don't need
MAY_WRITEcheck asbpf_get_file_xattris read only. We have the prefix check embedded inbpf_get_file_xattr():if (
strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
return-EPERM;
Quoted 8 lines
inode_permission()is a little trickier here, which checks against idmap. However, I don't think the check makes sense in the context of LSM. In this case, we have two processes: one security daemon, which owns the BPF LSM program, and a process being monitored. idmap here, fromfile_mnt_idmap(file), is the idmap from the being monitored process. However, whether the BPF LSM program have the permission to read the xattr should be determined by the security daemon.
Maybe we should check against nop_mnt_idmap? Would something like the following make more sense?
Thanks,
Song
kernel/trace/bpf_trace.c +0 -0
@@ -1453,6 +1453,7 @@ __bpf_kfunc int bpf_get_file_xattr(struct file
*file, const char *name__str,
struct dentry *dentry;
u32 value_len;
void *value;+ int ret;
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
return -EPERM;
@@ -1463,6 +1464,9 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,
return -EINVAL;
dentry = file_dentry(file);
+ ret = inode_permission(&nop_mnt_idmap, dentry->d_inode, MAY_READ);
+ if (ret)
+ return ret;
return __vfs_getxattr(dentry, dentry->d_inode, name__str,value, value_len);
}
Christian Brauner
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
In reply to Song LiuQuoted 58 lines · Song Liu
Hi Christian,
Thanks again for your comments.
Quoted 1 line · Christian Brauner
[...]
Quoted 41 lines
Quoted 7 lines
AFAICT, the
XATTR_USER_PREFIXabove is equivalent to the prefix check inxattr_permission().For
inode_permission(), I think it is not required because we already have the "struct file" of the target file. Did I misunderstand something here?I had overlooked that you don't allow writing xattrs. But there's still some issues:
So if you look at the system call interface:
fgetxattr(fd)
->getxattr()
->do_getxattr()-> vfs_getxattr() -> xattr_permission() -> __vfs_getxattr()and
io_uring:
do_getxattr()
->vfs_getxattr()
->xattr_permission()
->__vfs_getxattr()you can see that
xattr_permission()is a _read/write-timecheck_, not an open check. That's because the read/write permissions may depend on what xattr is read/written. Since you don't know what xattr will be read/written at open-time.So there needs to be a good reason for
bpf_get_file_xattr()to deviate from the system call andio_uringinterface. And I'd like to hear it, please. :)I think I might see the argument because you document the helper as "may only be called from BPF LSM function" in which case you're trying to say that
bpf_get_file_xattr()is equivalent to a call to__vfs_getxattr()from an LSM to get at it's own security xattr.Quoted 5 lines
But if that's the case you really should have a way to verify that these helpers are only callable from a specific BPF context. Because you otherwise omit read/write-time permission checking when retrieving xattrs which is a potentialy security issue and may be abused by a BPF program to skip permission checks that are otherwise enforced.
What do you mean by "a specific BPF context"? Current implementation makes sure the helper only works on LSM hooks with "struct file *" in the argument list. Specifically, we can only use them from the following hooks:
Quoted 15 lines
security_binder_transfer_file
security_bprm_creds_from_file
security_file_permission
security_file_alloc_security
security_file_free_security
security_file_ioctl
security_mmap_file
security_file_lock
security_file_fcntl
security_file_set_fowner
security_file_receive
security_file_open
security_file_truncate
security_kernel_read_file
security_kernel_post_read_file
Ok, good!
Quoted 5 lines
Note that, we disallow pointer-walking with the kfunc, so the kfunc is not allowed from hooks with indirect access to "struct file". For example, we cannot use it with
security_bprm_creds_for_exec(struct linux_binprm *bprm)as this hook only has bprm, and callingbpf_get_file_xattr(bprm->file)is not allowed.
Great.
Quoted 8 lines
Quoted 5 lines
Is there a way for BPF to enforce/verify that a function is only called from a specific BPF program? It should be able to recognize that, no? And then refuse to load that BPF program if a helper is called outside it's intended context.
Similarly, I am not quite sure what you mean by "a specific BPF program". My answer to this is probably the same as above.
Yes, this is exactly what I meant.
Quoted 22 lines
Going back to
xattr_permissionitself. AFAICT, it does 3 checks:1.
MAY_WRITEcheck;
2. prefix check;
3.inode_permission().We don't need
MAY_WRITEcheck asbpf_get_file_xattris read only. We have the prefix check embedded inbpf_get_file_xattr():if (
strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
return-EPERM;
inode_permission()is a little trickier here, which checks against idmap. However, I don't think the check makes sense in the context of LSM. In this case, we have two processes: one security daemon, which owns the BPF LSM program, and a process being monitored. idmap here, fromfile_mnt_idmap(file), is the idmap from the being monitored process. However, whether the BPF LSM program have the permission to read the xattr should be determined by the security daemon.
Quoted 4 lines
Overall, we can technically add
xattr_permission()check here. But I don't think that's the right check for the LSM use case.Does this make sense? Did I miss or misunderstand something?
If the helper is only callable from an LSM context then this should be fine.
Song Liu
maintainerRe: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Hi Christian,
Quoted 2 lines · Christian Brauner
On Mon, Nov 27, 2023 at 10:05:23AM -0800, Song Liu wrote:
[...]
Quoted 8 lines
Quoted 5 lines
Overall, we can technically add
xattr_permission()check here. But I don't think that's the right check for the LSM use case.Does this make sense? Did I miss or misunderstand something?
If the helper is only callable from an LSM context then this should be fine.
If everything looks good, would you please give an official Acked-by or Reviewed-by?
Thanks,
Song
Re: [PATCH v13 bpf-next 1/6] bpf: Add kfunc bpf_get_file_xattr
Quoted 10 lines · Song Liu
Hi Christian,
Quoted 2 lines · Christian Brauner
On Mon, Nov 27, 2023 at 10:05:23AM -0800, Song Liu wrote:
[...]
Quoted 4 lines
Quoted 4 lines
Overall, we can technically add
xattr_permission()check here. But I don't think that's the right check for the LSM use case.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Does this make sense? Did I miss or misunderstand something?
If the helper is only callable from an LSM context then this should be fine.
If everything looks good, would you please give an official Acked-by or Reviewed-by?
Yeah looks ok to me,
Song Liu
maintainerPATCH v13 2/6 bpf, fsverity: Add kfunc bpf_get_fsverity_digest
In reply to Song Liufsverity provides fast and reliable hash of files, namely fsverity_digest. The digest can be used by security solutions to verify file contents.
Add new kfunc bpf_get_fsverity_digest() so that we can access fsverity from BPF LSM programs. This kfunc is added to fs/verity/measure.c because some data structure used in the function is private to fsverity (fs/verity/fsverity_private.h).
To avoid recursion, bpf_get_fsverity_digest is only allowed in BPF LSM programs.
--- fs/verity/fsverity_private.h | 10 +++++ fs/verity/init.c | 1 + fs/verity/measure.c | 84 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+)
fs/verity/fsverity_private.h +10 -0
@@ -100,6 +100,16 @@ fsverity_msg(const struct inode *inode, const char *level,#define fsverity_err (inode, fmt, ...) \ fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) +/* measure.c */ + +#ifdef CONFIG_BPF_SYSCALL +void __init fsverity_init_bpf (void ); +#else +static inline void fsverity_init_bpf (void ) +{ +} +#endif +/* open.c */ int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
fs/verity/init.c +1 -0
@@ -69,6 +69,7 @@ static int __init fsverity_init(void)fsverity_init_workqueue ();fsverity_init_sysctl ();fsverity_init_signature (); +fsverity_init_bpf ();return 0 ; }late_initcall (fsverity_init)
fs/verity/measure.c +84 -0
93 lines not on this page · raw message
Signature · 1 line
2.34.1
Song Liu
maintainerPATCH v13 3/6 Documentation/bpf: Add documentation for filesystem kfuncs
In reply to Song LiuAdd a brief introduction for file system kfuncs:
bpf_get_file_xattr()
bpf_get_fsverity_digest()
The documentation highlights the strategy to avoid recursions of these kfuncs.
--- Documentation/bpf/fs_kfuncs.rst | 21 +++++++++++++++++++++ Documentation/bpf/index.rst | 1 + 2 files changed, 22 insertions(+) create mode 100644 Documentation/bpf/fs_kfuncs.rst
Documentation/bpf/fs_kfuncs.rst new file +21 -0
@@ -0,0 +1,21 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _fs_kfuncs-header-label: + +===================== +BPF filesystem kfuncs +===================== + +BPF LSM programs need to access filesystem data from LSM hooks. The following +BPF kfuncs can be used to get these data. + + * ``bpf_get_file_xattr()`` + + * ``bpf_get_fsverity_digest()`` + +To avoid recursions, these kfuncs follow the following rules: + +1. These kfuncs are only permitted from BPF LSM function. +2. These kfuncs should not call into other LSM hooks, i.e. security_*(). For + example, ``bpf_get_file_xattr()`` does not use ``vfs_getxattr()``, because + the latter calls LSM hook ``security_inode_getxattr``.
Documentation/bpf/index.rst +1 -0
@@ -21,6 +21,7 @@ that goes into great technical depth about the BPF Architecture. helpers kfuncs cpumasks + fs_kfuncs programs maps bpf_prog_run
Signature · 1 line
2.34.1
Song Liu
maintainerPATCH v13 4/6 selftests/bpf: Sort config in alphabetic order
In reply to Song LiuMove CONFIG_VSOCKETS up, so the CONFIGs are in alphabetic order.
--- tools/testing/selftests/bpf/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/config +1 -1
@@ -82,7 +82,7 @@ CONFIG_SECURITY=y CONFIG_SECURITYFS=y CONFIG_TEST_BPF=m CONFIG_USERFAULTFD=y +CONFIG_VSOCKETS=y CONFIG_VXLAN=y CONFIG_XDP_SOCKETS=y CONFIG_XFRM_INTERFACE=y -CONFIG_VSOCKETS=y
Signature · 1 line
2.34.1
Song Liu
maintainerPATCH v13 5/6 selftests/bpf: Add tests for filesystem kfuncs
In reply to Song LiuAdd selftests for two new filesystem kfuncs:
1. bpf_get_file_xattr
2. bpf_get_fsverity_digest
These tests simply make sure the two kfuncs work. Another selftest will be added to demonstrate how to use these kfuncs to verify file signature.
CONFIG_FS_VERITY is added to selftests config. However, this is not sufficient to guarantee bpf_get_fsverity_digest works. This is because fsverity need to be enabled at file system level (for example, with tune2fs on ext4). If local file system doesn't have this feature enabled, just skip the test.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 3 + tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/fs_kfuncs.c | 132 ++++++++++++++++++ .../selftests/bpf/progs/test_fsverity.c | 46 ++++++ .../selftests/bpf/progs/test_get_xattr.c | 37 +++++ 5 files changed, 219 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/test_fsverity.c create mode 100644 tools/testing/selftests/bpf/progs/test_get_xattr.c
tools/testing/selftests/bpf/bpf_kfuncs.h +3 -0
@@ -55,4 +55,7 @@ void *bpf_cast_to_kern_ctx(void *) __ksym;void *bpf_rdonly_cast (void *obj,__u32 btf_id) __ksym; +extern int bpf_get_file_xattr(struct file *file, const char *name, + struct bpf_dynptr *value_ptr) __ksym; +extern int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr *digest_ptr) __ksym;#endif
tools/testing/selftests/bpf/config +1 -0
@@ -23,6 +23,7 @@ CONFIG_FPROBE=y CONFIG_FTRACE_SYSCALLS=y CONFIG_FUNCTION_ERROR_INJECTION=y CONFIG_FUNCTION_TRACER=y +CONFIG_FS_VERITY=y CONFIG_GENEVE=y CONFIG_IKCONFIG=y CONFIG_IKCONFIG_PROC=y
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c new file +132 -0
132 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_fsverity.c new file +46 -0
46 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_get_xattr.c new file +37 -0
@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "bpf_kfuncs.h" + +char _license[] SEC("license") = "GPL"; + +__u32 monitored_pid; +__u32 found_xattr; + +static const char expected_value[] ="hello" ; +char value[32 ]; + +SEC("lsm.s/file_open") +int BPF_PROG (test_file_open ,struct file *f) +{ +struct bpf_dynptr value_ptr; +__u32 pid; +int ret; + + pid =bpf_get_current_pid_tgid () >>32 ; +if (pid != monitored_pid) +return 0 ; + +bpf_dynptr_from_mem (value,sizeof (value),0 , &value_ptr); + + ret =bpf_get_file_xattr (f,"user.kfuncs" , &value_ptr); +if (ret !=sizeof (expected_value)) +return 0 ; +if (bpf_strncmp (value, ret, expected_value)) +return 0 ; + found_xattr =1 ; +return 0 ; +}
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs
kernel test robot noticed the following build errors:
Hi Song,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Song-Liu/bpf-Add-kfunc-bpf_get_file_xattr/20231124-074239 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231123233936.3079687-6-song%40kernel.org patch subject: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231124/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> progs/test_fsverity.c:16:22: error: invalid application of 'sizeof' to an incomplete type 'struct fsverity_digest'
16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~
progs/test_fsverity.c:16:36: note: forward declaration of 'struct fsverity_digest'
16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
| ^
progs/test_fsverity.c:17:13: error: invalid application of 'sizeof' to an incomplete type 'struct fsverity_digest'
17 | char digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~
progs/test_fsverity.c:16:36: note: forward declaration of 'struct fsverity_digest'
16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
| ^2 errors generated.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Song Liu
maintainerRe: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs
Quoted 33 lines · kernel test robot
Hi Song,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Song-Liu/bpf-Add-kfunc-bpf_get_file_xattr/20231124-074239 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231123233936.3079687-6-song%40kernel.org patch subject: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231124/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
progs/test_fsverity.c:16:22: error: invalid application of 'sizeof' to an incomplete type 'structfsverity_digest'16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^ ~~~~~~~~~~~~~~~~~~~~~~~~ progs/test_fsverity.c:16:36: note: forward declaration of 'struct fsverity_digest' 16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^ progs/test_fsverity.c:17:13: error: invalid application of 'sizeof' to an incomplete type 'struct fsverity_digest' 17 | char digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^ ~~~~~~~~~~~~~~~~~~~~~~~~ progs/test_fsverity.c:16:36: note: forward declaration of 'struct fsverity_digest' 16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^
Quoted 1 line
2 errors generated.
In the patch, I added CONFIG_FS_VERITY to the config (tools/testing/selftests/bpf/config). I guess the test robot didn't update the config accordingly?
Thanks,
Song
Re: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs
Quoted 36 lines · Song Liu
Quoted 33 lines · kernel test robot
Hi Song,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Song-Liu/bpf-Add-kfunc-bpf_get_file_xattr/20231124-074239 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231123233936.3079687-6-song%40kernel.org patch subject: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231124/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
progs/test_fsverity.c:16:22: error: invalid application of 'sizeof' to an incomplete type 'structfsverity_digest'16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^ ~~~~~~~~~~~~~~~~~~~~~~~~ progs/test_fsverity.c:16:36: note: forward declaration of 'struct fsverity_digest' 16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^ progs/test_fsverity.c:17:13: error: invalid application of 'sizeof' to an incomplete type 'struct fsverity_digest' 17 | char digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^ ~~~~~~~~~~~~~~~~~~~~~~~~ progs/test_fsverity.c:16:36: note: forward declaration of 'struct fsverity_digest' 16 | char expected_digest[sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE]; | ^Quoted 1 line
2 errors generated.
Quoted 3 lines
In the patch, I added
CONFIG_FS_VERITYto the config (tools/testing/selftests/bpf/config). I guess the test robot didn't update the config accordingly?
Thanks for this information. The bot does use a static config to build selftests. Dynamic config updating is not supported yet. We will fix this soon.
Best Regards,
Yujie
Re: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs
In reply to Song LiuQuoted 11 lines · Song Liu
tools/testing/selftests/bpf/progs/test_get_xattr.c new file +10
-0+static const char expected_value[] ="hello" ; +char value[32 ]; + +SEC("lsm.s/file_open") +int BPF_PROG (test_file_open ,struct file *f) +{ +struct bpf_dynptr value_ptr; +__u32 pid; +int ret; +
tools/testing/selftests/bpf/progs/test_get_xattr.c line 33
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_get_xattr.c new file +10
-0+ pid =bpf_get_current_pid_tgid () >>32 ; +if (pid != monitored_pid) +return 0 ; + +bpf_dynptr_from_mem (value,sizeof (value),0 , &value_ptr); + + ret =bpf_get_file_xattr (f,"user.kfuncs" , &value_ptr); +if (ret !=sizeof (expected_value)) +return 0 ; +if (bpf_strncmp (value, ret, expected_value))
Hmm. It doesn't work like:
if (bpf_strncmp(value, ret, "hello"))
?
Song Liu
maintainerRe: [PATCH v13 bpf-next 5/6] selftests/bpf: Add tests for filesystem kfuncs
Quoted 18 lines · Alexei Starovoitov
Quoted 16 lines · Song Liu
tools/testing/selftests/bpf/progs/test_get_xattr.c new file +15
-0+static const char expected_value[] ="hello" ; +char value[32 ]; + +SEC("lsm.s/file_open") +int BPF_PROG (test_file_open ,struct file *f) +{ +struct bpf_dynptr value_ptr; +__u32 pid; +int ret; + + pid =bpf_get_current_pid_tgid () >>32 ; +if (pid != monitored_pid) +return 0 ; + +bpf_dynptr_from_mem (value,sizeof (value),0 , &value_ptr);
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_get_xattr.c line 33
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_get_xattr.c new file +5
-0+ + ret =bpf_get_file_xattr (f,"user.kfuncs" , &value_ptr); +if (ret !=sizeof (expected_value)) +return 0 ; +if (bpf_strncmp (value, ret, expected_value))Hmm. It doesn't work like:
if (bpf_strncmp(value, ret, "hello"))
This also works. I used expected_value because there is a size check above. We can also make do something like
if (ret != sizeof("hello"))
return 0;
if (bpf_strncmp(value, ret, "hello"))
return 0;Both of the two work.
Thanks,
Song
Song Liu
maintainerPATCH v13 6/6 selftests/bpf: Add test that uses fsverity and xattr to sign a file
In reply to Song LiuThis selftests shows a proof of concept method to use BPF LSM to enforce file signature. This test is added to verify_pkcs7_sig, so that some existing logic can be reused.
This file signature method uses fsverity, which provides reliable and efficient hash (known as digest) of the file. The file digest is signed with asymmetic key, and the signature is stored in xattr. At the run time, BPF LSM reads file digest and the signature, and then checks them against the public key.
Note that this solution does NOT require FS_VERITY_BUILTIN_SIGNATURES. fsverity is only used to provide file digest. The signature verification and access control is all implemented in BPF LSM.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 7 + .../bpf/prog_tests/verify_pkcs7_sig.c | 163 +++++++++++++++++- .../selftests/bpf/progs/test_sig_in_xattr.c | 82 +++++++++ .../bpf/progs/test_verify_pkcs7_sig.c | 8 +- .../testing/selftests/bpf/verify_sig_setup.sh | 25 +++ 5 files changed, 277 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_sig_in_xattr.c
tools/testing/selftests/bpf/bpf_kfuncs.h +7 -0
@@ -58,4 +58,11 @@ void *bpf_rdonly_cast(void *obj, __u32 btf_id) __ksym; extern int bpf_get_file_xattr(struct file *file, const char *name, struct bpf_dynptr *value_ptr) __ksym; extern int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr *digest_ptr) __ksym; + +extern struct bpf_key *bpf_lookup_user_key (__u32 serial,__u64 flags) __ksym; +extern struct bpf_key *bpf_lookup_system_key (__u64 id) __ksym; +extern void bpf_key_put(struct bpf_key *key) __ksym; +extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr, + struct bpf_dynptr *sig_ptr, + struct bpf_key *trusted_keyring) __ksym;#endif
tools/testing/selftests/bpf/prog_tests/verify_pkcs7_sig.c +162 -1
187 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c new file +82 -0
82 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_verify_pkcs7_sig.c +1 -7
@@ -10,17 +10,11 @@#include <errno.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> +#include "bpf_kfuncs.h" #define MAX_DATA_SIZE (1024 * 1024)#define MAX_SIG_SIZE 1024 -extern struct bpf_key *bpf_lookup_user_key (__u32 serial,__u64 flags) __ksym; -extern struct bpf_key *bpf_lookup_system_key (__u64 id) __ksym; -extern void bpf_key_put(struct bpf_key *key) __ksym; -extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr, - struct bpf_dynptr *sig_ptr, - struct bpf_key *trusted_keyring) __ksym; -__u32 monitored_pid;__u32 user_keyring_serial;__u64 system_keyring_id;
tools/testing/selftests/bpf/verify_sig_setup.sh +25 -0
@@ -60,6 +60,27 @@ cleanup() { rm -rf ${tmp_dir} } +fsverity_create_sign_file() { + local tmp_dir="$1" + + data_file=${tmp_dir}/data-file + sig_file=${tmp_dir}/sig-file + dd if=/dev/urandom of=$data_file bs=1 count=12345 2> /dev/null + fsverity sign --key ${tmp_dir}/signing_key.pem $data_file $sig_file + + # We do not want to enable fsverity on $data_file yet. Try whether + # the file system support fsverity on a different file. + touch ${tmp_dir}/tmp-file + fsverity enable ${tmp_dir}/tmp-file +} + +fsverity_enable_file() { + local tmp_dir="$1" + + data_file=${tmp_dir}/data-file + fsverity enable $data_file +} + catch() { local exit_code="$1"
@@ -86,6 +107,10 @@ main() setup "${tmp_dir}" elif [[ "${action}" == "cleanup" ]]; then cleanup "${tmp_dir}" + elif [[ "${action}" == "fsverity-create-sign" ]]; then + fsverity_create_sign_file "${tmp_dir}" + elif [[ "${action}" == "fsverity-enable" ]]; then + fsverity_enable_file "${tmp_dir}" else echo "Unknown action: ${action}" exit 1
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v13 bpf-next 6/6] selftests/bpf: Add test that uses fsverity and xattr to sign a file
kernel test robot noticed the following build errors:
Hi Song,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Song-Liu/bpf-Add-kfunc-bpf_get_file_xattr/20231124-074239 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231123233936.3079687-7-song%40kernel.org patch subject: [PATCH v13 bpf-next 6/6] selftests/bpf: Add test that uses fsverity and xattr to sign a file compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231125/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> progs/test_sig_in_xattr.c:36:26: error: invalid application of 'sizeof' to an incomplete type 'struct fsverity_digest'
36 | char digest[MAGIC_SIZE + sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~
progs/test_sig_in_xattr.c:36:40: note: forward declaration of 'struct fsverity_digest'
36 | char digest[MAGIC_SIZE + sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
| ^1 error generated.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki