bpf: File verification with LSM and fsverity
Subsystems (9)
Song Liu
maintainerv6 cover bpf: File verification with LSM and fsverity
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 (9):
bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
bpf: Factor out helper check_reg_const_str()
bpf: Introduce KF_ARG_PTR_TO_CONST_STR
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 + Documentation/bpf/kfuncs.rst | 24 +++ fs/verity/fsverity_private.h | 10 ++ fs/verity/init.c | 1 + fs/verity/measure.c | 85 +++++++++ include/linux/bpf.h | 4 + kernel/bpf/helpers.c | 16 +- kernel/bpf/verifier.c | 104 +++++++---- kernel/trace/bpf_trace.c | 74 +++++++- 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 +++ 19 files changed, 789 insertions(+), 57 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 v6 1/9 bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
kfuncs bpf_dynptr_slice and bpf_dynptr_slice_rdwr are used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example, bpf_verify_pkcs7_signature.
Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr to bpf.h so that kernel functions can use them instead of accessing dynptr->data directly.
Update bpf_verify_pkcs7_signature to use bpf_dynptr_slice instead of dynptr->data.
Also, update the comments for bpf_dynptr_slice and bpf_dynptr_slice_rdwr that they may return error pointers for BPF_DYNPTR_TYPE_XDP.
--- include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)
include/linux/bpf.h +4 -0
@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);
kernel/bpf/helpers.c +8 -8
@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only -* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer +* to a read-only data slice (can be either direct pointer to the data or a +* pointer to the user provided buffer, with its contents containing the data, +* if unable to obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)
@@ -2354,10 +2354,10 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset* bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a -* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer +* to a data slice (can be either direct pointer to the data or a pointer to the +* user provided buffer, with its contents containing the data, if unable to +* obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)
kernel/trace/bpf_trace.c +11 -4
@@ -1378,6 +1378,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,struct bpf_dynptr_kern *sig_ptr, struct bpf_key *trusted_keyring) { + void *data, *sig;int ret;if (trusted_keyring->has_ref) {
@@ -1394,10 +1395,16 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,return ret; } -return verify_pkcs7_signature (data_ptr->data, -__bpf_dynptr_size (data_ptr), - sig_ptr->data, -__bpf_dynptr_size (sig_ptr), + data =bpf_dynptr_slice (data_ptr,0 ,NULL ,0 ); +if (IS_ERR (data)) +return PTR_ERR (data); + + sig =bpf_dynptr_slice (sig_ptr,0 ,NULL ,0 ); +if (IS_ERR (sig)) +return PTR_ERR (sig); + +return verify_pkcs7_signature (data,__bpf_dynptr_size (data_ptr), + sig,__bpf_dynptr_size (sig_ptr), trusted_keyring->key,VERIFYING_UNSPECIFIED_SIGNATURE ,NULL ,NULL );
Signature · 1 line
2.34.1
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
Quoted 41 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>--- include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-0
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 8 lines
kernel/bpf/helpers.c +1
-4@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only -* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer
Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Quoted 58 lines
kernel/bpf/helpers.c +7
-4+* to a read-only data slice (can be either direct pointer to the data or a +* pointer to the user provided buffer, with its contents containing the data, +* if unable to obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)@@ -2354,10 +2354,10 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset* bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a -* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer +* to a data slice (can be either direct pointer to the data or a pointer to the +* user provided buffer, with its contents containing the data, if unable to +* obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)kernel/trace/bpf_trace.c +11
-5@@ -1378,6 +1378,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,struct bpf_dynptr_kern *sig_ptr, struct bpf_key *trusted_keyring) { + void *data, *sig;int ret;if (trusted_keyring->has_ref) {@@ -1394,10 +1395,16 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,return ret; } -return verify_pkcs7_signature (data_ptr->data, -__bpf_dynptr_size (data_ptr), - sig_ptr->data, -__bpf_dynptr_size (sig_ptr), + data =bpf_dynptr_slice (data_ptr,0 ,NULL ,0 ); +if (IS_ERR (data)) +return PTR_ERR (data); + + sig =bpf_dynptr_slice (sig_ptr,0 ,NULL ,0 ); +if (IS_ERR (sig)) +return PTR_ERR (sig); + +return verify_pkcs7_signature (data,__bpf_dynptr_size (data_ptr), + sig,__bpf_dynptr_size (sig_ptr), trusted_keyring->key,VERIFYING_UNSPECIFIED_SIGNATURE ,NULL ,NULL ); --2.34.1
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>--- include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-1@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only
Quoted 8 lines
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 4 lines
kernel/bpf/helpers.c +1
-3-* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
So I just checked the code (should have done it before replying, sorry). It is a bug that slipped through when adding bpf_xdp_pointer() usage. We should always return NULL from this kfunc on error conditions. Let's fix it separately, but please don't change the comments.
Quoted 6 lines
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Reading bpf_dynptr_slice_rdwr code, it does look exactly like what you want, despite the confusingly-looking 0, NULL, 0 arguments. So I guess I'm fine exposing it directly, but it still feels like it will bite us at some point later.
Quoted 58 lines
Quoted 58 lines
kernel/bpf/helpers.c +7
-4+* to a read-only data slice (can be either direct pointer to the data or a +* pointer to the user provided buffer, with its contents containing the data, +* if unable to obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)@@ -2354,10 +2354,10 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset* bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a -* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer +* to a data slice (can be either direct pointer to the data or a pointer to the +* user provided buffer, with its contents containing the data, if unable to +* obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)kernel/trace/bpf_trace.c +11
-5@@ -1378,6 +1378,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,struct bpf_dynptr_kern *sig_ptr, struct bpf_key *trusted_keyring) { + void *data, *sig;int ret;if (trusted_keyring->has_ref) {@@ -1394,10 +1395,16 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,return ret; } -return verify_pkcs7_signature (data_ptr->data, -__bpf_dynptr_size (data_ptr), - sig_ptr->data, -__bpf_dynptr_size (sig_ptr), + data =bpf_dynptr_slice (data_ptr,0 ,NULL ,0 ); +if (IS_ERR (data)) +return PTR_ERR (data); + + sig =bpf_dynptr_slice (sig_ptr,0 ,NULL ,0 ); +if (IS_ERR (sig)) +return PTR_ERR (sig); + +return verify_pkcs7_signature (data,__bpf_dynptr_size (data_ptr), + sig,__bpf_dynptr_size (sig_ptr), trusted_keyring->key,VERIFYING_UNSPECIFIED_SIGNATURE ,NULL ,NULL ); --2.34.1
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
Quoted 72 lines · Andrii Nakryiko
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>--- include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-1@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only Quoted 8 lines
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 4 lines
kernel/bpf/helpers.c +1
-3-* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
So I just checked the code (should have done it before replying, sorry). It is a bug that slipped through when adding
bpf_xdp_pointer()usage. We should always return NULL from this kfunc on error conditions. Let's fix it separately, but please don't change the comments.Quoted 6 lines
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Quoted 4 lines
Reading
bpf_dynptr_slice_rdwrcode, it does look exactly like what you want, despite the confusingly-looking 0, NULL, 0 arguments. So I guess I'm fine exposing it directly, but it still feels like it will bite us at some point later.
Ok, now I'm at patch #5. Think about what you are doing here. You are asking bpf_dynptr_slice_rdrw() if you can get a directly writable pointer to a data area of length *zero*. So if it's SKB, for example, then yeah, you'll be granted a pointer. But then you are proceeding to write up to sizeof(struct fsverity_digest) bytes, and that can cross into non-contiguous memory.
So I'll take it back, let's not expose this kfunc directly to kernel code. Let's have a separate internal helper that will return either valid pointer or NULL for a given dynptr, but will require valid non-zero max size. Something with the signature like below
void * __bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);
If ptr can provide a direct pointer to memory of length *len*, great. If not, return NULL. This will be an appropriate internal API for all the use cases you are adding where we will be writing back into dynptr from other kernel APIs with the assumption of contiguous memory region.
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
kernel/bpf/helpers.c +7
-4+* to a read-only data slice (can be either direct pointer to the data or a +* pointer to the user provided buffer, with its contents containing the data, +* if unable to obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)@@ -2354,10 +2354,10 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset* bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a -* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer +* to a data slice (can be either direct pointer to the data or a pointer to the +* user provided buffer, with its contents containing the data, if unable to +* obtain direct pointer) */ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,void *buffer__opt,u32 buffer__szk)kernel/trace/bpf_trace.c +11
-5@@ -1378,6 +1378,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,struct bpf_dynptr_kern *sig_ptr, struct bpf_key *trusted_keyring) { + void *data, *sig;int ret;if (trusted_keyring->has_ref) {@@ -1394,10 +1395,16 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,return ret; } -return verify_pkcs7_signature (data_ptr->data, -__bpf_dynptr_size (data_ptr), - sig_ptr->data, -__bpf_dynptr_size (sig_ptr), + data =bpf_dynptr_slice (data_ptr,0 ,NULL ,0 ); +if (IS_ERR (data)) +return PTR_ERR (data); + + sig =bpf_dynptr_slice (sig_ptr,0 ,NULL ,0 ); +if (IS_ERR (sig)) +return PTR_ERR (sig); + +return verify_pkcs7_signature (data,__bpf_dynptr_size (data_ptr), + sig,__bpf_dynptr_size (sig_ptr), trusted_keyring->key,VERIFYING_UNSPECIFIED_SIGNATURE ,NULL ,NULL ); --2.34.1
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
Quoted 56 lines · Andrii Nakryiko
Quoted 50 lines · Andrii Nakryiko
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>---
include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-1@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 4 lines
kernel/bpf/helpers.c +1
-3-* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
Right. kfunc should not return error pointer.
Quoted 31 lines
Quoted 14 lines
So I just checked the code (should have done it before replying, sorry). It is a bug that slipped through when adding
bpf_xdp_pointer()usage. We should always return NULL from this kfunc on error conditions. Let's fix it separately, but please don't change the comments.Quoted 6 lines
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Quoted 4 lines
Reading
bpf_dynptr_slice_rdwrcode, it does look exactly like what you want, despite the confusingly-looking 0, NULL, 0 arguments. So I guess I'm fine exposing it directly, but it still feels like it will bite us at some point later.Ok, now I'm at patch #5. Think about what you are doing here. You are asking
bpf_dynptr_slice_rdrw()if you can get a directly writable pointer to a data area of length *zero*. So if it's SKB, for example, then yeah, you'll be granted a pointer. But then you are proceeding to write up tosizeof(struct fsverity_digest)bytes, and that can cross into non-contiguous memory.So I'll take it back, let's not expose this kfunc directly to kernel code. Let's have a separate internal helper that will return either valid pointer or NULL for a given dynptr, but will require valid non-zero max size. Something with the signature like below
Quoted 7 lines
void *
__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);If ptr can provide a direct pointer to memory of length *len*, great. If not, return NULL. This will be an appropriate internal API for all the use cases you are adding where we will be writing back into dynptr from other kernel APIs with the assumption of contiguous memory region.
Sounds good. Fixing this in the next version.
Thanks,
Song
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
Quoted 66 lines · Song Liu
Quoted 57 lines · Andrii Nakryiko
Quoted 50 lines · Andrii Nakryiko
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>---
include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-1@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 4 lines
kernel/bpf/helpers.c +1
-3-* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
Quoted 1 line
Right. kfunc should not return error pointer.
Turns out it doesn't, see discussion in [0]. Maybe you can sneak in that comment in your next revision as a separate lightweight patch :)
[0] https://lore.kernel.org/bpf/CAEf4Bzb4VbH56S2D_5Sc3u9V=OXOy20JTr4wsObBOiUA32Md2Q@mail.gmail.com/
Quoted 45 lines
Quoted 32 lines
Quoted 15 lines
So I just checked the code (should have done it before replying, sorry). It is a bug that slipped through when adding
bpf_xdp_pointer()usage. We should always return NULL from this kfunc on error conditions. Let's fix it separately, but please don't change the comments.Quoted 6 lines
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Quoted 4 lines
Reading
bpf_dynptr_slice_rdwrcode, it does look exactly like what you want, despite the confusingly-looking 0, NULL, 0 arguments. So I guess I'm fine exposing it directly, but it still feels like it will bite us at some point later.Ok, now I'm at patch #5. Think about what you are doing here. You are asking
bpf_dynptr_slice_rdrw()if you can get a directly writable pointer to a data area of length *zero*. So if it's SKB, for example, then yeah, you'll be granted a pointer. But then you are proceeding to write up tosizeof(struct fsverity_digest)bytes, and that can cross into non-contiguous memory.So I'll take it back, let's not expose this kfunc directly to kernel code. Let's have a separate internal helper that will return either valid pointer or NULL for a given dynptr, but will require valid non-zero max size. Something with the signature like below
Quoted 7 lines
void *
__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);If ptr can provide a direct pointer to memory of length *len*, great. If not, return NULL. This will be an appropriate internal API for all the use cases you are adding where we will be writing back into dynptr from other kernel APIs with the assumption of contiguous memory region.
Sounds good. Fixing this in the next version.
Thanks,
Song
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
In reply to Song LiuQuoted 69 lines · Song Liu
Quoted 57 lines · Andrii Nakryiko
Quoted 50 lines · Andrii Nakryiko
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>---
include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-1@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 4 lines
kernel/bpf/helpers.c +1
-3-* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
Quoted 27 lines
Right. kfunc should not return error pointer.
Quoted 25 lines
Quoted 14 lines
So I just checked the code (should have done it before replying, sorry). It is a bug that slipped through when adding
bpf_xdp_pointer()usage. We should always return NULL from this kfunc on error conditions. Let's fix it separately, but please don't change the comments.Quoted 6 lines
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Quoted 4 lines
Reading
bpf_dynptr_slice_rdwrcode, it does look exactly like what you want, despite the confusingly-looking 0, NULL, 0 arguments. So I guess I'm fine exposing it directly, but it still feels like it will bite us at some point later.Ok, now I'm at patch #5. Think about what you are doing here. You are asking
bpf_dynptr_slice_rdrw()if you can get a directly writable pointer to a data area of length *zero*. So if it's SKB, for example, then yeah, you'll be granted a pointer. But then you are proceeding to write up tosizeof(struct fsverity_digest)bytes, and that can cross into non-contiguous memory.
By the way, the syntax and comment of bpf_dynptr_slice() is confusing:
* @buffer__opt: User-provided buffer to copy contents into. May be NULL
* @buffer__szk: Size (in bytes) of the buffer if present. This is the
* length of the requested slice. This must be a constant.
It reads (to me) as, "if buffer__opt is NULL, buffer__szk should be 0".
Is this just my confusion, or is there a real issue?
Thanks,
Song
Quoted 14 lines
Quoted 13 lines
So I'll take it back, let's not expose this kfunc directly to kernel code. Let's have a separate internal helper that will return either valid pointer or NULL for a given dynptr, but will require valid non-zero max size. Something with the signature like below
void *
__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);If ptr can provide a direct pointer to memory of length *len*, great. If not, return NULL. This will be an appropriate internal API for all the use cases you are adding where we will be writing back into dynptr from other kernel APIs with the assumption of contiguous memory region.
Re: [PATCH v6 bpf-next 1/9] bpf: Expose bpf_dynptr_slice* kfuncs for in kernel use
Quoted 100 lines · Song Liu
Quoted 70 lines · Song Liu
Quoted 57 lines · Andrii Nakryiko
Quoted 50 lines · Andrii Nakryiko
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Song Liu
kfuncs
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrare used by BPF programs to access the dynptr data. They are also useful for in kernel functions that access dynptr data, for example,bpf_verify_pkcs7_signature.Add
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrtobpf.hso that kernel functions can use them instead of accessingdynptr->datadirectly.Update
bpf_verify_pkcs7_signatureto usebpf_dynptr_sliceinstead ofdynptr->data.Also, update the comments for
bpf_dynptr_sliceandbpf_dynptr_slice_rdwrthat they may return error pointers forBPF_DYNPTR_TYPE_XDP.Signed-off-by Song Liu <[email protected]>---
include/linux/bpf.h | 4 ++++ kernel/bpf/helpers.c | 16 ++++++++-------- kernel/trace/bpf_trace.c | 15 +++++++++++---- 3 files changed, 23 insertions(+), 12 deletions(-)include/linux/bpf.h +4
-0@@ -1222,6 +1222,10 @@ enum bpf_dynptr_type {int bpf_dynptr_check_size (u32 size);u32 __bpf_dynptr_size (const struct bpf_dynptr_kern *ptr); +void *bpf_dynptr_slice (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr_kern *ptr,u32 offset, +void *buffer__opt,u32 buffer__szk);#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);kernel/bpf/helpers.c +0
-1@@ -2270,10 +2270,10 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)* bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in * the bpf program. * -* Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2273
Quoted 4 lines
kernel/bpf/helpers.c +1
-3-* data slice (can be either direct pointer to the data or a pointer to the user -* provided buffer, with its contents containing the data, if unable to obtain -* direct pointer) +* Return: NULL or error pointer if the call failed (eg invalid dynptr), pointer Hold on, nope, this one shouldn't return error pointer because it's used from BPF program side and BPF program is checking for NULL only. Does it actually return error pointer, though?
Quoted 27 lines
Right. kfunc should not return error pointer.
Quoted 25 lines
Quoted 14 lines
So I just checked the code (should have done it before replying, sorry). It is a bug that slipped through when adding
bpf_xdp_pointer()usage. We should always return NULL from this kfunc on error conditions. Let's fix it separately, but please don't change the comments.Quoted 6 lines
I'm generally skeptical of allowing to call kfuncs directly from internal kernel code, tbh, and concerns like this are one reason why. BPF verifier sets up various conditions that kfuncs have to follow, and it seems error-prone to mix this up with internal kernel usage.
Quoted 4 lines
Reading
bpf_dynptr_slice_rdwrcode, it does look exactly like what you want, despite the confusingly-looking 0, NULL, 0 arguments. So I guess I'm fine exposing it directly, but it still feels like it will bite us at some point later.Ok, now I'm at patch #5. Think about what you are doing here. You are asking
bpf_dynptr_slice_rdrw()if you can get a directly writable pointer to a data area of length *zero*. So if it's SKB, for example, then yeah, you'll be granted a pointer. But then you are proceeding to write up tosizeof(struct fsverity_digest)bytes, and that can cross into non-contiguous memory.By the way, the syntax and comment of
bpf_dynptr_slice()is confusing:
Quoted 7 lines
* @buffer__opt: User-provided buffer to copy contents into. May be NULL
* @buffer__szk: Size (in bytes) of the buffer if present. This is the
* length of the requested slice. This must be a constant.It reads (to me) as, "if
buffer__optis NULL,buffer__szkshould be 0".Is this just my confusion, or is there a real issue?
It's a bit confusing, but no, that size needs to be a "how much data do I want to read/write", so even if buffer is NULL, size has to be specified.
"This is the length of the requested slice." is the most important part in that comment.
Quoted 20 lines
Thanks,
SongQuoted 14 lines
Quoted 13 lines
So I'll take it back, let's not expose this kfunc directly to kernel code. Let's have a separate internal helper that will return either valid pointer or NULL for a given dynptr, but will require valid non-zero max size. Something with the signature like below
void *
__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);If ptr can provide a direct pointer to memory of length *len*, great. If not, return NULL. This will be an appropriate internal API for all the use cases you are adding where we will be writing back into dynptr from other kernel APIs with the assumption of contiguous memory region.
Song Liu
maintainerPATCH v6 2/9 bpf: Factor out helper check_reg_const_str()
In reply to Song LiuARG_PTR_TO_CONST_STR is used to specify constant string args for BPF helpers. The logic that verifies a reg is ARG_PTR_TO_CONST_STR is implemented in check_func_arg().
As we introduce kfuncs with constant string args, it is necessary to do the same check for kfuncs (in check_kfunc_args). Factor out the logic for ARG_PTR_TO_CONST_STR to a new check_reg_const_str() so that it can be reused.
check_func_arg() ensures check_reg_const_str() is only called with reg of type PTR_TO_MAP_VALUE. Add a redundent type check in check_reg_const_str() to avoid misuse in the future. Other than this redundent check, there is no change in behavior.
--- kernel/bpf/verifier.c | 85 +++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 36 deletions(-)
kernel/bpf/verifier.c +49 -36
@@ -8571,6 +8571,54 @@ static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,return state->stack[spi].spilled_ptr.dynptr.type; } +static int check_reg_const_str (struct bpf_verifier_env *env, +struct bpf_reg_state *reg,u32 regno) +{ +struct bpf_map *map = reg->map_ptr; +int err; +int map_off; +u64 map_addr; +char *str_ptr; + +if (WARN_ON_ONCE (reg->type !=PTR_TO_MAP_VALUE )) +return -EINVAL ; + +if (!bpf_map_is_rdonly (map)) { +verbose (env,"R%d does not point to a readonly map'\n" , regno); +return -EACCES ; + } + +if (!tnum_is_const (reg->var_off)) { +verbose (env,"R%d is not a constant address'\n" , regno); +return -EACCES ; + } + +if (!map->ops->map_direct_value_addr) { +verbose (env,"no direct value access support for this map type\n" ); +return -EACCES ; + } + + err =check_map_access (env, regno, reg->off, + map->value_size - reg->off, false, +ACCESS_HELPER ); +if (err) +return err; + + map_off = reg->off + reg->var_off.value; + err = map->ops->map_direct_value_addr (map, &map_addr, map_off); +if (err) { +verbose (env,"direct value access on string failed\n" ); +return err; + } + + str_ptr = (char *)(long )(map_addr); +if (!strnchr (str_ptr + map_off, map->value_size - map_off,0 )) { +verbose (env,"string is not zero-terminated\n" ); +return -EINVAL ; + } +return 0 ; +} + static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, const struct bpf_func_proto *fn,
@@ -8815,44 +8863,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, }case ARG_PTR_TO_CONST_STR : { -struct bpf_map *map = reg->map_ptr; -int map_off; -u64 map_addr; -char *str_ptr; - -if (!bpf_map_is_rdonly (map)) { -verbose (env,"R%d does not point to a readonly map'\n" , regno); -return -EACCES ; - } - -if (!tnum_is_const (reg->var_off)) { -verbose (env,"R%d is not a constant address'\n" , regno); -return -EACCES ; - } - -if (!map->ops->map_direct_value_addr) { -verbose (env,"no direct value access support for this map type\n" ); -return -EACCES ; - } - - err =check_map_access (env, regno, reg->off, - map->value_size - reg->off, false, -ACCESS_HELPER ); + err =check_reg_const_str (env, reg, regno);if (err)return err; - - map_off = reg->off + reg->var_off.value; - err = map->ops->map_direct_value_addr (map, &map_addr, map_off); -if (err) { -verbose (env,"direct value access on string failed\n" ); -return err; - } - - str_ptr = (char *)(long )(map_addr); -if (!strnchr (str_ptr + map_off, map->value_size - map_off,0 )) { -verbose (env,"string is not zero-terminated\n" ); -return -EINVAL ; - }break ; }case ARG_PTR_TO_KPTR :
Signature · 1 line
2.34.1
Re: [PATCH v6 bpf-next 2/9] bpf: Factor out helper check_reg_const_str()
Quoted 15 lines · Song Liu
ARG_PTR_TO_CONST_STRis used to specify constant string args for BPF helpers. The logic that verifies a reg isARG_PTR_TO_CONST_STRis implemented incheck_func_arg().As we introduce kfuncs with constant string args, it is necessary to do the same check for kfuncs (in
check_kfunc_args). Factor out the logic forARG_PTR_TO_CONST_STRto a newcheck_reg_const_str()so that it can be reused.
check_func_arg()ensurescheck_reg_const_str()is only called with reg of typePTR_TO_MAP_VALUE. Add a redundent type check incheck_reg_const_str()to avoid misuse in the future. Other than this redundent check, there is no change in behavior.
Quoted 5 lines
Signed-off-by Song Liu <[email protected]>--- kernel/bpf/verifier.c | 85 +++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 36 deletions(-)
LGTM, I don't feel too strongly about that added WARN_ON_ONCE.
Quoted 8 lines
kernel/bpf/verifier.c +0
-0@@ -8571,6 +8571,54 @@ static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,return state->stack[spi].spilled_ptr.dynptr.type; }
[...]
Song Liu
maintainerPATCH v6 3/9 bpf: Introduce KF_ARG_PTR_TO_CONST_STR
In reply to Song LiuSimilar to ARG_PTR_TO_CONST_STR for BPF helpers, KF_ARG_PTR_TO_CONST_STR specifies kfunc args that point to const strings. Annotation "__const_str" is used to specify kfunc arg of type KF_ARG_PTR_TO_CONST_STR. Also, add documentation for the "__const_str" annotation.
bpf_get_file_xattr() will be the first kfunc that uses this type.
--- Documentation/bpf/kfuncs.rst | 24 ++++++++++++++++++++++++ kernel/bpf/verifier.c | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+)
Documentation/bpf/kfuncs.rst +24 -0
@@ -137,6 +137,30 @@ Either way, the returned buffer is either NULL, or of size buffer_szk. Without t annotation, the verifier will reject the program if a null pointer is passed in with a nonzero size. +2.2.5 __const_str Annotation +---------------------------- +This annotation is used to indicate that the argument is a constant string. + +An example is given below:: + + __bpf_kfunc bpf_get_file_xattr(..., const char *name__const_str, ...) + { + ... + } + +In this case, ``bpf_get_file_xattr()`` can be called as:: + + bpf_get_file_xattr(..., "xattr_name", ...); + +Or:: + + const char name[] = "xattr_name"; /* This need to be global */ + int BPF_PROG(...) + { + ... + bpf_get_file_xattr(..., name, ...); + ... + } .. _BPF_kfunc_nodef:
kernel/bpf/verifier.c +19 -0
@@ -10656,6 +10656,11 @@ static bool is_kfunc_arg_nullable(const struct btf *btf, const struct btf_paramreturn __kfunc_param_match_suffix (btf, arg,"__nullable" ); } +static bool is_kfunc_arg_const_str (const struct btf *btf,const struct btf_param *arg) +{ +return __kfunc_param_match_suffix (btf, arg,"__const_str" ); +} +static bool is_kfunc_arg_scalar_with_name (const struct btf *btf,const struct btf_param *arg,const char *name)
@@ -10799,6 +10804,7 @@ enum kfunc_ptr_arg_type {KF_ARG_PTR_TO_RB_ROOT ,KF_ARG_PTR_TO_RB_NODE ,KF_ARG_PTR_TO_NULL , + KF_ARG_PTR_TO_CONST_STR, }; enum special_kfunc_type {
@@ -10943,6 +10949,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (is_kfunc_arg_rbtree_node (meta->btf, &args[argno]))return KF_ARG_PTR_TO_RB_NODE ; +if (is_kfunc_arg_const_str (meta->btf, &args[argno])) +return KF_ARG_PTR_TO_CONST_STR ; +if ((base_type (reg->type) ==PTR_TO_BTF_ID || reg2btf_ids[base_type (reg->type)])) {if (!btf_type_is_struct (ref_t)) { verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n",
@@ -11566,6 +11575,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_case KF_ARG_PTR_TO_MEM_SIZE :case KF_ARG_PTR_TO_CALLBACK :case KF_ARG_PTR_TO_REFCOUNTED_KPTR : +case KF_ARG_PTR_TO_CONST_STR :/* Trusted by default */ break ;default :
@@ -11837,6 +11847,15 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ meta->arg_btf = reg->btf; meta->arg_btf_id = reg->btf_id;break ; +case KF_ARG_PTR_TO_CONST_STR : +if (reg->type !=PTR_TO_MAP_VALUE ) { +verbose (env,"arg#%d doesn't point to a const string\n" , i); +return -EINVAL ; + } + ret =check_reg_const_str (env, reg, regno); +if (ret) +return ret; +break ; } }
Signature · 1 line
2.34.1
Re: [PATCH v6 bpf-next 3/9] bpf: Introduce KF_ARG_PTR_TO_CONST_STR
Documentation/bpf/kfuncs.rst Either way, the returned buffer is either NULL, or of size buffer_szk. Without t · line 146
Quoted 9 lines · Song Liu
+2.2.5
__const_strAnnotation
+----------------------------Documentation/bpf/kfuncs.rst +5
-0+This annotation is used to indicate that the argument is a constant string. + +An example is given below:: + + __bpf_kfunc bpf_get_file_xattr(..., const char *name__const_str, ...)
After sleeping on it this still looks too verbose to me. 'const' is repeated back to back.
Let's use __str suffix?
Quoted 1 line
+ ret =
check_reg_const_str(env, reg, regno);
I don't mind that helper has a more verbose name.
Song Liu
maintainerRe: [PATCH v6 bpf-next 3/9] bpf: Introduce KF_ARG_PTR_TO_CONST_STR
Quoted 4 lines · Alexei Starovoitov
Quoted 2 lines · Song Liu
Quoted 11 lines
Documentation/bpf/kfuncs.rst Either way, the returned buffer is either NULL, or of size buffer_szk. Without t · line 146
Quoted 7 lines
+2.2.5
__const_strAnnotation
+----------------------------Documentation/bpf/kfuncs.rst +5
-0+This annotation is used to indicate that the argument is a constant string. + +An example is given below:: + + __bpf_kfunc bpf_get_file_xattr(..., const char *name__const_str, ...)After sleeping on it this still looks too verbose to me. 'const' is repeated back to back.
Let's use__strsuffix?
Yeah, the first const should be enough. Let's use __str.
Thanks,
Song
Quoted 4 lines
Quoted 2 lines
+ ret =
check_reg_const_str(env, reg, regno);I don't mind that helper has a more verbose name.
Song Liu
maintainerPATCH v6 4/9 bpf: Add kfunc bpf_get_file_xattr
In reply to Song LiuIt 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().
To avoid recursion, bpf_get_file_xattr can be only called from LSM hooks.
--- kernel/trace/bpf_trace.c | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
kernel/trace/bpf_trace.c +59 -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>
@@ -1436,6 +1437,64 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__diag_push (); +__diag_ignore_all ("-Wmissing-prototypes" , +"kfuncs which will be used in BPF programs" ); + +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__const_str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__const_str* of *file* and store the output in *value_ptr*. +* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__const_str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +void *value; + + value =bpf_dynptr_slice_rdwr (value_ptr,0 ,NULL ,0 ); +if (IS_ERR_OR_NULL (value)) +return PTR_ERR (value); + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__const_str, + value,__bpf_dynptr_size (value_ptr)); +} + +__diag_pop (); + +BTF_SET8_START (fs_kfunc_set_ids) +BTF_ID_FLAGS (func ,bpf_get_file_xattr ,KF_SLEEPABLE ) +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
Re: [PATCH v6 bpf-next 4/9] bpf: Add kfunc bpf_get_file_xattr
Quoted 25 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().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 | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)kernel/trace/bpf_trace.c +1
-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>
kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1443
Quoted 7 lines
kernel/trace/bpf_trace.c +4
-0@@ -1436,6 +1437,64 @@ static int __init bpf_key_sig_kfuncs_init(void)late_initcall (bpf_key_sig_kfuncs_init);#endif /* CONFIG_KEYS */ +/* filesystem kfuncs */ +__diag_push (); +__diag_ignore_all ("-Wmissing-prototypes" , +"kfuncs which will be used in BPF programs" );
Dave just added convenient macros for kfunc definition regions, please use them for new code.
Quoted 8 lines
kernel/trace/bpf_trace.c +8
-0+ +/** +* bpf_get_file_xattr - get xattr of a file +* @file: file to get xattr from +* @name__const_str: name of the xattr +* @value_ptr: output buffer of the xattr value +* +* Get xattr *name__const_str* of *file* and store the output in *value_ptr*.
kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1461
Quoted 10 lines
kernel/trace/bpf_trace.c +10
-0+* +* Return: 0 on success, a negative value on error. +*/ +__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__const_str, +struct bpf_dynptr_kern *value_ptr) +{ +struct dentry *dentry; +void *value; + + value =bpf_dynptr_slice_rdwr (value_ptr,0 ,NULL ,0 );
This is very confusing and looks wrong. You are passing requested size 0, which is certainly not the intent of this API.
As I mentioned in previous patch, let's add internal-only dynptr helper that will return pointer and separately we already have a helper to get dynptr size, and do that explicitly.
Quoted 42 lines
kernel/trace/bpf_trace.c +36
-1+if (IS_ERR_OR_NULL (value)) +return PTR_ERR (value); + + dentry =file_dentry (file); +return __vfs_getxattr (dentry, dentry->d_inode, name__const_str, + value,__bpf_dynptr_size (value_ptr)); +} + +__diag_pop (); + +BTF_SET8_START (fs_kfunc_set_ids) +BTF_ID_FLAGS (func ,bpf_get_file_xattr ,KF_SLEEPABLE ) +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) { --2.34.1
Song Liu
maintainerPATCH v6 5/9 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 | 85 ++++++++++++++++++++++++++++++++++++ 3 files changed, 96 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 +85 -0
94 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v6 bpf-next 5/9] bpf, fsverity: Add kfunc bpf_get_fsverity_digest
Quoted 8 lines · Song Liu
fsverity 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 tofs/verity/measure.cbecause some data structure used in the function is private to fsverity (fs/verity/fsverity_private.h).
Quoted 4 lines
To avoid recursion,
bpf_get_fsverity_digestis only allowed in BPF LSM programs.Signed-off-by Song Liu <[email protected]>
- Eric
Song Liu
maintainerPATCH v6 6/9 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 v6 7/9 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
@@ -81,7 +81,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 v6 8/9 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
Song Liu
maintainerPATCH v6 9/9 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