Skip to contentSkip to conversation

bpf: file verification with LSM and fsverity

PATCH Superseded Song Liu 7 participants 24 messages Oct 13, 2023 – Nov 2, 2023

Subsystems (8)
Kselftest BPF Tracing BPF selftests +4 more subsystems

v1 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Song Liu

author maintainer
#0 lore raw

cover bpf: file verification with LSM and fsverity

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 (5):
bpf: Add kfunc bpf_get_file_xattr
bpf, fsverity: Add kfunc bpf_get_fsverity_digest selftests/bpf: Sort config in alphabetic order selftests/bpf: Add tests for filesystem kfuncs selftests/bpf: Add test that use fsverity and xattr to sign a file

 fs/verity/measure.c                           |  66 +++++++
 include/linux/bpf.h                           |  12 ++
 kernel/trace/bpf_trace.c                      |  44 +++++
 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      |  39 +++++
 .../selftests/bpf/progs/test_sig_in_xattr.c   |  84 +++++++++
 .../bpf/progs/test_verify_pkcs7_sig.c         |   8 +-
 .../testing/selftests/bpf/verify_sig_setup.sh |  25 +++
 12 files changed, 623 insertions(+), 9 deletions(-)
 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

author maintainer
#1 lore raw

PATCH 1/5 bpf: Add kfunc bpf_get_file_xattr

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
 include/linux/bpf.h      | 12 +++++++++++
 kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
include/linux/bpf.h +12 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
 	return !!current->bpf_ctx;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+	char *str = ptr->data;
+
+	return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
 	return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+	return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +44 -0

56 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 18 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
 include/linux/bpf.h      | 12 +++++++++++
 kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
        return !!current->bpf_ctx;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

Quoted 46 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
        return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +10 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+ *
+ * Get xattr *name_ptr* 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, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

Quoted 30 lines
kernel/trace/bpf_trace.c +24 -1
+
+       return vfs_getxattr(mnt_idmap(file->f_path.mnt), file_dentry(file), name_ptr->data,
+                           value_ptr->data, __bpf_dynptr_size(value_ptr));
+}
+
+__diag_pop();
+
+BTF_SET8_START(fs_kfunc_set)
+BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE)
+BTF_SET8_END(fs_kfunc_set)
+
+const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set = &fs_kfunc_set,
+};
+
+static int __init bpf_fs_kfuncs_init(void)
+{
+       return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+                                        &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

#3 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 26 lines · Andrii Nakryiko
Quoted 21 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
include/linux/bpf.h | 12 +++++++++++
kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++

2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
       return !!current->bpf_ctx;
 }
 
Quoted 4 lines

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 1 line
include/linux/bpf.h +1 -0
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

is_zero_terminated sounds better.

Quoted 52 lines
Quoted 52 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
       return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +15 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value
+ *
+ * Get xattr *name_ptr* of *file* and store the output in *value_ptr*.
+ *
+ * Return: 0 on success, a negative value on error.
+ */
Quoted 8 lines

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 5 lines
kernel/trace/bpf_trace.c +5 -0
+__bpf_kfunc int bpf_get_file_xattr(struct file *file, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

We can add a NULL check (or size check) here.

Quoted 3 lines

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

We can require the dynptr to be BPF_DYNPTR_TYPE_LOCAL. I don't think we need this for dynptr of skb or xdp. Would this be sufficient?

Thanks,
Song

Quoted 5 lines
Quoted 5 lines
kernel/trace/bpf_trace.c +4 -0
+
+       return vfs_getxattr(mnt_idmap(file->f_path.mnt), file_dentry(file), name_ptr->data,
+                           value_ptr->data, __bpf_dynptr_size(value_ptr));
+}
#4 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 95 lines · Song Liu
Quoted 27 lines · Andrii Nakryiko
Quoted 21 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
include/linux/bpf.h | 12 +++++++++++
kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++

2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
       return !!current->bpf_ctx;
 }
 
Quoted 4 lines

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 1 line
include/linux/bpf.h +1 -0
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

is_zero_terminated sounds better.

Quoted 52 lines
Quoted 52 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
       return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +15 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value
+ *
+ * Get xattr *name_ptr* of *file* and store the output in *value_ptr*.
+ *
+ * Return: 0 on success, a negative value on error.
+ */
Quoted 8 lines

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 5 lines
kernel/trace/bpf_trace.c +5 -0
+__bpf_kfunc int bpf_get_file_xattr(struct file *file, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

Quoted 1 line

We can add a NULL check (or size check) here.

there must be some helper to check if dynptr is valid, let's use that instead of NULL checks

Quoted 7 lines
Quoted 4 lines

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

We can require the dynptr to be BPF_DYNPTR_TYPE_LOCAL. I don't think we need this for dynptr of skb or xdp. Would this be sufficient?

well, to keep thing simple we can have a simple internal helper API that will tell if it's safe to assume that dynptr memory is contiguous and it's ok to use dynptr memory. But still, you shouldn't access data pointer directly, there must be some helper for that. Please check. It has to take into account offset and stuff like that.

Also, and separately from that, we should think about providing a bpf_dynptr_slice()-like helper that will accept a fixed-sized temporary buffer and return pointer to either actual memory or copy non-contiguous memory into that buffer. That will make sure you can use any dynptr as a source of data, and only pay the price of memory copy in rare cases where it's necessary

Quoted 10 lines

Thanks,
Song

Quoted 5 lines
Quoted 5 lines
kernel/trace/bpf_trace.c +4 -0
+
+       return vfs_getxattr(mnt_idmap(file->f_path.mnt), file_dentry(file), name_ptr->data,
+                           value_ptr->data, __bpf_dynptr_size(value_ptr));
+}
#5 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 100 lines · Andrii Nakryiko
Quoted 95 lines · Song Liu
Quoted 27 lines · Andrii Nakryiko
Quoted 21 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
include/linux/bpf.h | 12 +++++++++++
kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++

2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
      return !!current->bpf_ctx;
 }
 
Quoted 4 lines

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 1 line
include/linux/bpf.h +1 -0
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

is_zero_terminated sounds better.

Quoted 60 lines
Quoted 47 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
      return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +10 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+ *
+ * Get xattr *name_ptr* 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, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

Quoted 4 lines
Quoted 1 line

We can add a NULL check (or size check) here.

there must be some helper to check if dynptr is valid, let's use that instead of NULL checks

Yeah, we can use bpf_dynptr_is_null().

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

Quoted 8 lines
Quoted 2 lines

We can require the dynptr to be BPF_DYNPTR_TYPE_LOCAL. I don't think we need this for dynptr of skb or xdp. Would this be sufficient?

well, to keep thing simple we can have a simple internal helper API that will tell if it's safe to assume that dynptr memory is contiguous and it's ok to use dynptr memory. But still, you shouldn't access data pointer directly, there must be some helper for that. Please check. It has to take into account offset and stuff like that.

Yeah, we can use bpf_dynptr_write(), which is a helper (not kfunc).

Quoted 7 lines

Also, and separately from that, we should think about providing a bpf_dynptr_slice()-like helper that will accept a fixed-sized temporary buffer and return pointer to either actual memory or copy non-contiguous memory into that buffer. That will make sure you can use any dynptr as a source of data, and only pay the price of memory copy in rare cases where it's necessary

I don't quite follow here. Currently, we have

bpf_dynptr_data()
bpf_dynptr_slice()
bpf_dynptr_slice_rdwr()
bpf_dynptr_write()

AFAICT, they are sufficient to cover existing use cases (and the new use case we are adding in this set). What's the new kfunc are you thinking about?

Thanks,
Song

#6 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 135 lines · Song Liu
Quoted 101 lines · Andrii Nakryiko
Quoted 95 lines · Song Liu
Quoted 27 lines · Andrii Nakryiko
Quoted 21 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
include/linux/bpf.h | 12 +++++++++++
kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++

2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
      return !!current->bpf_ctx;
 }
 
Quoted 4 lines

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 1 line
include/linux/bpf.h +1 -0
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

is_zero_terminated sounds better.

Quoted 60 lines
Quoted 47 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
      return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +10 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+ *
+ * Get xattr *name_ptr* 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, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

Quoted 4 lines
Quoted 1 line

We can add a NULL check (or size check) here.

there must be some helper to check if dynptr is valid, let's use that instead of NULL checks

Yeah, we can use bpf_dynptr_is_null().

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

Quoted 8 lines
Quoted 2 lines

We can require the dynptr to be BPF_DYNPTR_TYPE_LOCAL. I don't think we need this for dynptr of skb or xdp. Would this be sufficient?

well, to keep thing simple we can have a simple internal helper API that will tell if it's safe to assume that dynptr memory is contiguous and it's ok to use dynptr memory. But still, you shouldn't access data pointer directly, there must be some helper for that. Please check. It has to take into account offset and stuff like that.

Yeah, we can use bpf_dynptr_write(), which is a helper (not kfunc).

Quoted 7 lines

Also, and separately from that, we should think about providing a bpf_dynptr_slice()-like helper that will accept a fixed-sized temporary buffer and return pointer to either actual memory or copy non-contiguous memory into that buffer. That will make sure you can use any dynptr as a source of data, and only pay the price of memory copy in rare cases where it's necessary

I don't quite follow here. Currently, we have

Quoted 8 lines

bpf_dynptr_data()
bpf_dynptr_slice()
bpf_dynptr_slice_rdwr()
bpf_dynptr_write()

AFAICT, they are sufficient to cover existing use cases (and the new use case we are adding in this set). What's the new kfunc are you thinking about?

I wasn't talking about kfuncs, but rather just internal helpers to be used by other kfuncs when working with dynptrs as input arguments.

Quoted 5 lines

Thanks,
Song

#7 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 145 lines · Andrii Nakryiko
Quoted 140 lines · Song Liu
Quoted 101 lines · Andrii Nakryiko
Quoted 95 lines · Song Liu
Quoted 27 lines · Andrii Nakryiko
Quoted 21 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
include/linux/bpf.h | 12 +++++++++++
kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++

2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
     return !!current->bpf_ctx;
 }
 
Quoted 4 lines

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 1 line
include/linux/bpf.h +1 -0
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

is_zero_terminated sounds better.

Quoted 60 lines
Quoted 47 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
     return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +10 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+ *
+ * Get xattr *name_ptr* 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, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

Quoted 4 lines
Quoted 1 line

We can add a NULL check (or size check) here.

there must be some helper to check if dynptr is valid, let's use that instead of NULL checks

Yeah, we can use bpf_dynptr_is_null().

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

Quoted 8 lines
Quoted 2 lines

We can require the dynptr to be BPF_DYNPTR_TYPE_LOCAL. I don't think we need this for dynptr of skb or xdp. Would this be sufficient?

well, to keep thing simple we can have a simple internal helper API that will tell if it's safe to assume that dynptr memory is contiguous and it's ok to use dynptr memory. But still, you shouldn't access data pointer directly, there must be some helper for that. Please check. It has to take into account offset and stuff like that.

Yeah, we can use bpf_dynptr_write(), which is a helper (not kfunc).

Quoted 7 lines

Also, and separately from that, we should think about providing a bpf_dynptr_slice()-like helper that will accept a fixed-sized temporary buffer and return pointer to either actual memory or copy non-contiguous memory into that buffer. That will make sure you can use any dynptr as a source of data, and only pay the price of memory copy in rare cases where it's necessary

I don't quite follow here. Currently, we have

bpf_dynptr_data()
bpf_dynptr_slice()
bpf_dynptr_slice_rdwr()
bpf_dynptr_write()

Quoted 6 lines
Quoted 3 lines

AFAICT, they are sufficient to cover existing use cases (and the new use case we are adding in this set). What's the new kfunc are you thinking about?

I wasn't talking about kfuncs, but rather just internal helpers to be used by other kfuncs when working with dynptrs as input arguments.

AFAICT, kfuncs can call other kfuncs, for example bpf_dynptr_slice_rdwr() calls bpf_dynptr_slice(). This is limited to the same file at the moment, since we do not expose kfuncs in headers.

Thanks,
Song

#8 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

In reply to Song Liu

Hi,

Quoted 87 lines · Song Liu
Quoted 24 lines · Andrii Nakryiko
Quoted 20 lines · Song Liu

This kfunc can be used to read xattr of a file.

Since vfs_getxattr() requires null-terminated string as input "name", a new helper bpf_dynptr_is_string() is added to check the input before calling vfs_getxattr().

---
include/linux/bpf.h | 12 +++++++++++
kernel/trace/bpf_trace.c | 44 ++++++++++++++++++++++++++++++++++++++++

2 files changed, 56 insertions(+)
include/linux/bpf.h +0 -0
index 61bde4520f5c..f14fae45e13d 100644
@@ -2472,6 +2472,13 @@ static inline bool has_current_bpf_ctx(void)
       return !!current->bpf_ctx;
 }
 
Quoted 3 lines

include/linux/bpf.h static inline bool has_current_bpf_ctx(void) · line 2475

Quoted 1 line
include/linux/bpf.h +1 -0
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)

is_zero_terminated would be more accurate? though there is nothing really dynptr-specific here...

is_zero_terminated sounds better.

Quoted 50 lines
Quoted 50 lines
include/linux/bpf.h +11 -0
+{
+       char *str = ptr->data;
+
+       return str[__bpf_dynptr_size(ptr) - 1] == '\0';
+}
+
 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
 
 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
@@ -2708,6 +2715,11 @@ static inline bool has_current_bpf_ctx(void)
       return false;
 }
 
+static inline bool bpf_dynptr_is_string(struct bpf_dynptr_kern *ptr)
+{
+       return false;
+}
+
 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
 {
 }
kernel/trace/bpf_trace.c +14 -0
index df697c74d519..946268574e05 100644
@@ -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>
 
@@ -1429,6 +1430,49 @@ 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
+ * @name_ptr: name of the xattr
+ * @value_ptr: output buffer of the xattr value
+ *
+ * Get xattr *name_ptr* of *file* and store the output in *value_ptr*.
+ *
+ * Return: 0 on success, a negative value on error.
Quoted 8 lines

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1451

Quoted 6 lines
kernel/trace/bpf_trace.c +6 -0
+ */
+__bpf_kfunc int bpf_get_file_xattr(struct file *file, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))
+               return -EINVAL;

so dynptr can be invalid and name_ptr->data will be NULL, you should account for that

Quoted 6 lines

We can add a NULL check (or size check) here.

Quoted 2 lines

and there could also be special dynptrs that don't have contiguous memory region, so somehow you'd need to take care of that as well

We can require the dynptr to be BPF_DYNPTR_TYPE_LOCAL. I don't think we need this for dynptr of skb or xdp. Would this be sufficient?

I think bpf_dynptr_is_rdonly() is also needed. Because the content of dynptr may be modified by other bpf program and the zero-terminated condition will not true. As suggested by Alexei, add string support in verifier is a better choice.

Quoted 8 lines

Thanks,
Song

Quoted 4 lines
Quoted 4 lines
kernel/trace/bpf_trace.c +4 -0
+
+       return vfs_getxattr(mnt_idmap(file->f_path.mnt), file_dentry(file), name_ptr->data,
+                           value_ptr->data, __bpf_dynptr_size(value_ptr));
+}
#9 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

In reply to Song Liu
Quoted 4 lines · Song Liu
kernel/trace/bpf_trace.c +4 -0
+__bpf_kfunc int bpf_get_file_xattr(struct file *file, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1460

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+               return -EINVAL;
+
+       return vfs_getxattr(mnt_idmap(file->f_path.mnt), file_dentry(file), name_ptr->data,
+                           value_ptr->data, __bpf_dynptr_size(value_ptr));
+}
+
+__diag_pop();
+
+BTF_SET8_START(fs_kfunc_set)
+BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE)

I suspect it needs to be allowlisted too.
Sleepable might not be enough.

KP proposed such kfunc in the past and there were recursion issues.

KP,
do you remember the details?

maintainer
#10 lore raw

Re: [PATCH bpf-next 1/5] bpf: Add kfunc bpf_get_file_xattr

Quoted 20 lines · Alexei Starovoitov
Quoted 4 lines · Song Liu
kernel/trace/bpf_trace.c +4 -0
+__bpf_kfunc int bpf_get_file_xattr(struct file *file, struct bpf_dynptr_kern *name_ptr,
+                                  struct bpf_dynptr_kern *value_ptr)
+{
+       if (!bpf_dynptr_is_string(name_ptr))

kernel/trace/bpf_trace.c static int __init bpf_key_sig_kfuncs_init(void) · line 1460

Quoted 10 lines
kernel/trace/bpf_trace.c +10 -0
+               return -EINVAL;
+
+       return vfs_getxattr(mnt_idmap(file->f_path.mnt), file_dentry(file), name_ptr->data,
+                           value_ptr->data, __bpf_dynptr_size(value_ptr));
+}
+
+__diag_pop();
+
+BTF_SET8_START(fs_kfunc_set)
+BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE)

I suspect it needs to be allowlisted too.
Sleepable might not be enough.

Quoted 4 lines

KP proposed such kfunc in the past and there were recursion issues.

KP,
do you remember the details?

yeah, have a look at Al's reply:

https://lore.kernel.org/bpf/Yrs4+ThR4ACb5eD%2F@ZenIV/

it can create deadlocks and potentially UAFs (similar to the situation Jann mentioned). This will need to be allowlisted.

author maintainer
#11 lore raw

PATCH 2/5 bpf, fsverity: Add kfunc bpf_get_fsverity_digest

In reply to Song Liu

The kfunc can be used to read fsverity_digest, so that we can verify signature in BPF LSM.

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).

---
 fs/verity/measure.c | 66 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
fs/verity/measure.c +66 -0
index eec5956141da..2d4b2e6f5a5d 100644
@@ -8,6 +8,8 @@
 #include "fsverity_private.h"
 
 #include <linux/uaccess.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
 
 /**
  * fsverity_ioctl_measure() - get a verity file's digest
@@ -100,3 +102,67 @@ int fsverity_get_digest(struct inode *inode,
 	return hash_alg->digest_size;
 }
 EXPORT_SYMBOL_GPL(fsverity_get_digest);
+
+/* bpf kfuncs */
+__diag_push();
+__diag_ignore_all("-Wmissing-prototypes",
+		  "kfuncs which will be used in BPF programs");
+
+/**
+ * bpf_get_fsverity_digest: read fsverity digest of file
+ * @file: file to get digest from
+ * @digest_ptr: (out) dynptr for struct fsverity_digest
+ *
+ * Read fsverity_digest of *file* into *digest_ptr*.
+ *
+ * Return: 0 on success, a negative value on error.
+ */
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+	const struct inode *inode = file_inode(file);
+	struct fsverity_digest *arg = digest_ptr->data;
+	const struct fsverity_info *vi;
+	const struct fsverity_hash_alg *hash_alg;
+	int out_digest_sz;
+
+	if (__bpf_dynptr_size(digest_ptr) < sizeof(struct fsverity_digest))
+		return -EINVAL;
+
+	vi = fsverity_get_info(inode);
+	if (!vi)
+		return -ENODATA; /* not a verity file */
+
+	hash_alg = vi->tree_params.hash_alg;
+
+	arg->digest_algorithm = hash_alg - fsverity_hash_algs;
+	arg->digest_size = hash_alg->digest_size;
+
+	out_digest_sz = __bpf_dynptr_size(digest_ptr) - sizeof(struct fsverity_digest);
+
+	/* copy digest */
+	memcpy(arg->digest, vi->file_digest,  min_t(int, hash_alg->digest_size, out_digest_sz));
+
+	/* fill the extra buffer with zeros */
+	memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size);
+
+	return 0;
+}
+
+__diag_pop();
+
+BTF_SET8_START(fsverity_set)
+BTF_ID_FLAGS(func, bpf_get_fsverity_digest, KF_SLEEPABLE)
+BTF_SET8_END(fsverity_set)
+
+const struct btf_kfunc_id_set bpf_fsverity_set = {
+	.owner = THIS_MODULE,
+	.set = &fsverity_set,
+};
+
+static int __init bpf_fsverity_init(void)
+{
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+					 &bpf_fsverity_set);
+}
+
+late_initcall(bpf_fsverity_init);
Signature · 1 line
2.34.1
maintainer
#12 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

Quoted 37 lines · Song Liu

The kfunc can be used to read fsverity_digest, so that we can verify signature in BPF LSM.

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).

---
 fs/verity/measure.c | 66 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
fs/verity/measure.c +11 -0
index eec5956141da..2d4b2e6f5a5d 100644
@@ -8,6 +8,8 @@
 #include "fsverity_private.h"
 
 #include <linux/uaccess.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
 
 /**
  * fsverity_ioctl_measure() - get a verity file's digest
@@ -100,3 +102,67 @@ int fsverity_get_digest(struct inode *inode,
 	return hash_alg->digest_size;
 }
 EXPORT_SYMBOL_GPL(fsverity_get_digest);
+
+/* bpf kfuncs */
+__diag_push();
+__diag_ignore_all("-Wmissing-prototypes",
+		  "kfuncs which will be used in BPF programs");
+
+/**
+ * bpf_get_fsverity_digest: read fsverity digest of file
+ * @file: file to get digest from

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 10 lines
fs/verity/measure.c +10 -0
+ * @digest_ptr: (out) dynptr for struct fsverity_digest
+ *
+ * Read fsverity_digest of *file* into *digest_ptr*.
+ *
+ * Return: 0 on success, a negative value on error.
+ */
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+	const struct inode *inode = file_inode(file);
+	struct fsverity_digest *arg = digest_ptr->data;

What alignment is guaranteed here?

Quoted 13 lines
fs/verity/measure.c +13 -0
+	const struct fsverity_info *vi;
+	const struct fsverity_hash_alg *hash_alg;
+	int out_digest_sz;
+
+	if (__bpf_dynptr_size(digest_ptr) < sizeof(struct fsverity_digest))
+		return -EINVAL;
+
+	vi = fsverity_get_info(inode);
+	if (!vi)
+		return -ENODATA; /* not a verity file */
+
+	hash_alg = vi->tree_params.hash_alg;
+

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 146

Quoted 10 lines
fs/verity/measure.c +10 -0
+	arg->digest_algorithm = hash_alg - fsverity_hash_algs;
+	arg->digest_size = hash_alg->digest_size;
+
+	out_digest_sz = __bpf_dynptr_size(digest_ptr) - sizeof(struct fsverity_digest);
+
+	/* copy digest */
+	memcpy(arg->digest, vi->file_digest,  min_t(int, hash_alg->digest_size, out_digest_sz));
+
+	/* fill the extra buffer with zeros */
+	memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size);

Can't 'out_digest_sz - hash_alg->digest_size' underflow?

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 154

Quoted 8 lines
fs/verity/measure.c +8 -0
+
+	return 0;
+}
+
+__diag_pop();
+
+BTF_SET8_START(fsverity_set)
+BTF_ID_FLAGS(func, bpf_get_fsverity_digest, KF_SLEEPABLE)

Should it be sleepable? Nothing in it sleeps, as far as I can tell.

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 160

Quoted 6 lines
fs/verity/measure.c +6 -0
+BTF_SET8_END(fsverity_set)
+
+const struct btf_kfunc_id_set bpf_fsverity_set = {
+	.owner = THIS_MODULE,
+	.set = &fsverity_set,
+};

static const?

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 168

Quoted 8 lines
fs/verity/measure.c +8 -0
+
+static int __init bpf_fsverity_init(void)
+{
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+					 &bpf_fsverity_set);
+}
+
+late_initcall(bpf_fsverity_init);

Maybe this should be called by the existing fsverity_init() initcall instead of having a brand new initcall just for this.

Also, doesn't this all need to be guarded by a kconfig such as CONFIG_BPF?

Also, it looks like I'm being signed up to maintain this. This isn't a stable UAPI, right? No need to document this in Documentation/?

- Eric

author maintainer
#13 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

Quoted 1 line · Eric Biggers

[...]

Quoted 7 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 5 lines

+ */

fs/verity/measure.c +4 -0
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+     const struct inode *inode = file_inode(file);
+     struct fsverity_digest *arg = digest_ptr->data;

What alignment is guaranteed here?

drnptr doesn't not provide alignment guarantee for digest_ptr->data. If we need alignment guarantee, we need to add it here.

Quoted 18 lines
Quoted 18 lines
fs/verity/measure.c +17 -0
+     const struct fsverity_info *vi;
+     const struct fsverity_hash_alg *hash_alg;
+     int out_digest_sz;
+
+     if (__bpf_dynptr_size(digest_ptr) < sizeof(struct fsverity_digest))
+             return -EINVAL;
+
+     vi = fsverity_get_info(inode);
+     if (!vi)
+             return -ENODATA; /* not a verity file */
+
+     hash_alg = vi->tree_params.hash_alg;
+
+     arg->digest_algorithm = hash_alg - fsverity_hash_algs;
+     arg->digest_size = hash_alg->digest_size;
+
+     out_digest_sz = __bpf_dynptr_size(digest_ptr) - sizeof(struct fsverity_digest);
Quoted 8 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 146

Quoted 6 lines
fs/verity/measure.c +6 -0
+
+     /* copy digest */
+     memcpy(arg->digest, vi->file_digest,  min_t(int, hash_alg->digest_size, out_digest_sz));
+
+     /* fill the extra buffer with zeros */
+     memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size);

Can't 'out_digest_sz - hash_alg->digest_size' underflow?

Will fix this in the next version.

Quoted 11 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 154

Quoted 9 lines
fs/verity/measure.c +8 -0
+
+     return 0;
+}
+
+__diag_pop();
+
+BTF_SET8_START(fsverity_set)
+BTF_ID_FLAGS(func, bpf_get_fsverity_digest, KF_SLEEPABLE)

Should it be sleepable? Nothing in it sleeps, as far as I can tell.

Indeed, we can remove sleepable requirement here.

Quoted 9 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 160

Quoted 7 lines
fs/verity/measure.c +6 -0
+BTF_SET8_END(fsverity_set)
+
+const struct btf_kfunc_id_set bpf_fsverity_set = {
+     .owner = THIS_MODULE,
+     .set = &fsverity_set,
+};

static const?

Will fix in v2.

Quoted 12 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 168

Quoted 9 lines
fs/verity/measure.c +8 -0
+
+static int __init bpf_fsverity_init(void)
+{
+     return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+                                      &bpf_fsverity_set);
+}
+
+late_initcall(bpf_fsverity_init);

Maybe this should be called by the existing fsverity_init() initcall instead of having a brand new initcall just for this.

Yeah, that would also work.

Quoted 2 lines

Also, doesn't this all need to be guarded by a kconfig such as CONFIG_BPF?

Will add this in v2.

Quoted 3 lines

Also, it looks like I'm being signed up to maintain this. This isn't a stable UAPI, right? No need to document this in Documentation/?

BPF kfuncs are not UAPI. They are as stable as exported symbols. We do have some documents for BPF kfuncs, for example in Documentation/bpf/cpumasks.rst.

Do you have a recommendation or preference on where we should document this? AFAICT, we can either add it to fsverity.rst or somewhere in Documentation/bpf/.

Thanks,
Song

maintainer
#14 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

Quoted 11 lines · Song Liu
Quoted 1 line · Eric Biggers

[...]

Quoted 7 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 5 lines

+ */

fs/verity/measure.c +4 -0
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+     const struct inode *inode = file_inode(file);
+     struct fsverity_digest *arg = digest_ptr->data;

What alignment is guaranteed here?

Quoted 2 lines

drnptr doesn't not provide alignment guarantee for digest_ptr->data. If we need alignment guarantee, we need to add it here.

So technically it's wrong to cast it to struct fsverity_digest, then.

Quoted 11 lines
Quoted 3 lines

Also, it looks like I'm being signed up to maintain this. This isn't a stable UAPI, right? No need to document this in Documentation/?

BPF kfuncs are not UAPI. They are as stable as exported symbols. We do have some documents for BPF kfuncs, for example in Documentation/bpf/cpumasks.rst.

Do you have a recommendation or preference on where we should document this? AFAICT, we can either add it to fsverity.rst or somewhere in Documentation/bpf/.

The BPF documentation seems like the right place.

- Eric

author maintainer
#15 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

Quoted 13 lines · Eric Biggers
Quoted 11 lines · Song Liu
Quoted 1 line · Eric Biggers

[...]

Quoted 7 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 5 lines

+ */

fs/verity/measure.c +4 -0
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+     const struct inode *inode = file_inode(file);
+     struct fsverity_digest *arg = digest_ptr->data;

What alignment is guaranteed here?

Quoted 4 lines
Quoted 2 lines

drnptr doesn't not provide alignment guarantee for digest_ptr->data. If we need alignment guarantee, we need to add it here.

So technically it's wrong to cast it to struct fsverity_digest, then.

We can enforce alignment here. Would __aligned(2) be sufficient?

Thanks,
Song

maintainer
#16 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

Quoted 14 lines · Song Liu
Quoted 13 lines · Eric Biggers
Quoted 11 lines · Song Liu
Quoted 1 line · Eric Biggers

[...]

Quoted 7 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 5 lines

+ */

fs/verity/measure.c +4 -0
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+     const struct inode *inode = file_inode(file);
+     struct fsverity_digest *arg = digest_ptr->data;

What alignment is guaranteed here?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

drnptr doesn't not provide alignment guarantee for digest_ptr->data. If we need alignment guarantee, we need to add it here.

So technically it's wrong to cast it to struct fsverity_digest, then.

We can enforce alignment here. Would __aligned(2) be sufficient?

Do you mean something like the following:

	if (!IS_ALIGNED((uintptr_t)digest_ptr->data, __alignof__(*arg)))
		return -EINVAL;
#17 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

Quoted 24 lines · Eric Biggers
Quoted 19 lines · Song Liu
Quoted 17 lines · Eric Biggers
Quoted 11 lines · Song Liu
Quoted 1 line · Eric Biggers

[...]

Quoted 7 lines

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 5 lines

+ */

fs/verity/measure.c +4 -0
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+     const struct inode *inode = file_inode(file);
+     struct fsverity_digest *arg = digest_ptr->data;

What alignment is guaranteed here?

Quoted 2 lines

drnptr doesn't not provide alignment guarantee for digest_ptr->data. If we need alignment guarantee, we need to add it here.

So technically it's wrong to cast it to struct fsverity_digest, then.

Quoted 7 lines
Quoted 2 lines

We can enforce alignment here. Would __aligned(2) be sufficient?

Do you mean something like the following:

if (!IS_ALIGNED((uintptr_t)digest_ptr->data, __alignof__(*arg))) return -EINVAL;

I was thinking about hard-coding the alignment requirement. __alignof__ is much better. Thanks for the suggestion!

Song

#18 lore raw

Re: [PATCH bpf-next 2/5] bpf, fsverity: Add kfunc bpf_get_fsverity_digest

In reply to Song Liu
Quoted 38 lines · Song Liu

The kfunc can be used to read fsverity_digest, so that we can verify signature in BPF LSM.

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).

---
 fs/verity/measure.c | 66 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
fs/verity/measure.c +11 -0
index eec5956141da..2d4b2e6f5a5d 100644
@@ -8,6 +8,8 @@
 #include "fsverity_private.h"
 
 #include <linux/uaccess.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
 
 /**
  * fsverity_ioctl_measure() - get a verity file's digest
@@ -100,3 +102,67 @@ int fsverity_get_digest(struct inode *inode,
        return hash_alg->digest_size;
 }
 EXPORT_SYMBOL_GPL(fsverity_get_digest);
+
+/* bpf kfuncs */
+__diag_push();
+__diag_ignore_all("-Wmissing-prototypes",
+                 "kfuncs which will be used in BPF programs");
+
+/**
+ * bpf_get_fsverity_digest: read fsverity digest of file
+ * @file: file to get digest from

fs/verity/measure.c int fsverity_get_digest(struct inode *inode, · line 123

Quoted 10 lines
fs/verity/measure.c +10 -0
+ * @digest_ptr: (out) dynptr for struct fsverity_digest
+ *
+ * Read fsverity_digest of *file* into *digest_ptr*.
+ *
+ * Return: 0 on success, a negative value on error.
+ */
+__bpf_kfunc int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr_kern *digest_ptr)
+{
+       const struct inode *inode = file_inode(file);
+       struct fsverity_digest *arg = digest_ptr->data;

this can be null

I think we need some internal helpers that are similar to bpf_dynptr_slice() that would handle invalid dynptr cases, as well as abstract away potentially non-contiguous memory dynptr points to. WDYT?

Quoted 48 lines
fs/verity/measure.c +45 -1
+       const struct fsverity_info *vi;
+       const struct fsverity_hash_alg *hash_alg;
+       int out_digest_sz;
+
+       if (__bpf_dynptr_size(digest_ptr) < sizeof(struct fsverity_digest))
+               return -EINVAL;
+
+       vi = fsverity_get_info(inode);
+       if (!vi)
+               return -ENODATA; /* not a verity file */
+
+       hash_alg = vi->tree_params.hash_alg;
+
+       arg->digest_algorithm = hash_alg - fsverity_hash_algs;
+       arg->digest_size = hash_alg->digest_size;
+
+       out_digest_sz = __bpf_dynptr_size(digest_ptr) - sizeof(struct fsverity_digest);
+
+       /* copy digest */
+       memcpy(arg->digest, vi->file_digest,  min_t(int, hash_alg->digest_size, out_digest_sz));
+
+       /* fill the extra buffer with zeros */
+       memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size);
+
+       return 0;
+}
+
+__diag_pop();
+
+BTF_SET8_START(fsverity_set)
+BTF_ID_FLAGS(func, bpf_get_fsverity_digest, KF_SLEEPABLE)
+BTF_SET8_END(fsverity_set)
+
+const struct btf_kfunc_id_set bpf_fsverity_set = {
+       .owner = THIS_MODULE,
+       .set = &fsverity_set,
+};
+
+static int __init bpf_fsverity_init(void)
+{
+       return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+                                        &bpf_fsverity_set);
+}
+
+late_initcall(bpf_fsverity_init);
--

2.34.1

author maintainer
#19 lore raw

PATCH 3/5 selftests/bpf: Sort config in alphabetic order

In reply to Song Liu

Move CONFIG_VSOCKETS up, so the CONFIGs are in sorted order.

---
 tools/testing/selftests/bpf/config | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/config +1 -1
index 02dd4409200e..09da30be8728 100644
@@ -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
author maintainer
#20 lore raw

PATCH 4/5 selftests/bpf: Add tests for filesystem kfuncs

In reply to Song Liu

Add 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.

CONFIG_FS_VERITY is added to selftests config. However, this is not sufficient to garantee 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      |  39 ++++++
 5 files changed, 221 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
index 5ca68ff0b59f..ce27b0f3ffc0 100644
@@ -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, struct bpf_dynptr *name_ptr,
+			      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
index 09da30be8728..4534a913e46c 100644
@@ -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 +39 -0
new file mode 100644
index 000000000000..dc018877526f
@@ -0,0 +1,39 @@
+// 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;
+
+char key[] = "user.kfuncs";
+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 key_ptr, value_ptr;
+	__u32 pid;
+	int ret;
+
+	pid = bpf_get_current_pid_tgid() >> 32;
+	if (pid != monitored_pid)
+		return 0;
+
+	bpf_dynptr_from_mem(key, sizeof(key), 0, &key_ptr);
+	bpf_dynptr_from_mem(value, sizeof(value), 0, &value_ptr);
+
+	ret = bpf_get_file_xattr(f, &key_ptr, &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
author maintainer
#21 lore raw

PATCH 5/5 selftests/bpf: Add test that use fsverity and xattr to sign a file

In reply to Song Liu

This 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   |  84 +++++++++
 .../bpf/progs/test_verify_pkcs7_sig.c         |   8 +-
 .../testing/selftests/bpf/verify_sig_setup.sh |  25 +++
 5 files changed, 279 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
index ce27b0f3ffc0..fd17f1da42eb 100644
@@ -58,4 +58,11 @@ void *bpf_rdonly_cast(void *obj, __u32 btf_id) __ksym;
 extern int bpf_get_file_xattr(struct file *file, struct bpf_dynptr *name_ptr,
 			      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 +84 -0

84 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_verify_pkcs7_sig.c +1 -7
index 7748cc23de8a..f42e9f3831a1 100644
@@ -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
index ba08922b4a27..7e6caa134e1a 100755
@@ -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
#22 lore raw

Re: [PATCH bpf-next 5/5] selftests/bpf: Add test that use fsverity and xattr to sign a file

Quoted 41 lines · Song Liu
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c new file +40 -0
+#define MAGIC_SIZE 8
+char digest[MAGIC_SIZE + sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
+
+__u32 monitored_pid;
+char xattr_name[] = "user.sig";
+char sig[MAX_SIG_SIZE];
+__u32 sig_size;
+__u32 user_keyring_serial;
+
+SEC("lsm.s/file_open")
+int BPF_PROG(test_file_open, struct file *f)
+{
+       struct bpf_dynptr digest_ptr, sig_ptr, name_ptr;
+       struct bpf_key *trusted_keyring;
+       __u32 pid;
+       int ret;
+
+       pid = bpf_get_current_pid_tgid() >> 32;
+       if (pid != monitored_pid)
+               return 0;
+
+       /* digest_ptr points to fsverity_digest */
+       bpf_dynptr_from_mem(digest + MAGIC_SIZE, sizeof(digest) - MAGIC_SIZE, 0, &digest_ptr);
+
+       ret = bpf_get_fsverity_digest(f, &digest_ptr);
+       /* No verity, allow access */
+       if (ret < 0)
+               return 0;
+
+       /* Move digest_ptr to fsverity_formatted_digest */
+       bpf_dynptr_from_mem(digest, sizeof(digest), 0, &digest_ptr);
+
+       /* Read signature from xattr */
+       bpf_dynptr_from_mem(sig, sizeof(sig), 0, &sig_ptr);
+       bpf_dynptr_from_mem(xattr_name, sizeof(xattr_name), 0, &name_ptr);
+       ret = bpf_get_file_xattr(f, &name_ptr, &sig_ptr);
+       /* No signature, reject access */
+       if (ret < 0)
+               return -EPERM;
+

tools/testing/selftests/bpf/progs/test_sig_in_xattr.c line 84

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c new file +10 -0
+       trusted_keyring = bpf_lookup_user_key(user_keyring_serial, 0);
+       if (!trusted_keyring)
+               return -ENOENT;
+
+       /* Verify signature */
+       ret = bpf_verify_pkcs7_signature(&digest_ptr, &sig_ptr, trusted_keyring);
+
+       bpf_key_put(trusted_keyring);
+       return ret;
+}

I think the UX is cumbersome.
Putting a NULL terminated string into dynptr not only a source code ugliness, but it adds run-time overhead too.
We better add proper C style string support in the verifier, so that bpf_get_file_xattr() can look like normal C.

#23 lore raw

Re: [PATCH bpf-next 5/5] selftests/bpf: Add test that use fsverity and xattr to sign a file

Quoted 54 lines · Alexei Starovoitov
Quoted 49 lines · Song Liu
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c new file +48 -0
+#define MAGIC_SIZE 8
+char digest[MAGIC_SIZE + sizeof(struct fsverity_digest) + SHA256_DIGEST_SIZE];
+
+__u32 monitored_pid;
+char xattr_name[] = "user.sig";
+char sig[MAX_SIG_SIZE];
+__u32 sig_size;
+__u32 user_keyring_serial;
+
+SEC("lsm.s/file_open")
+int BPF_PROG(test_file_open, struct file *f)
+{
+       struct bpf_dynptr digest_ptr, sig_ptr, name_ptr;
+       struct bpf_key *trusted_keyring;
+       __u32 pid;
+       int ret;
+
+       pid = bpf_get_current_pid_tgid() >> 32;
+       if (pid != monitored_pid)
+               return 0;
+
+       /* digest_ptr points to fsverity_digest */
+       bpf_dynptr_from_mem(digest + MAGIC_SIZE, sizeof(digest) - MAGIC_SIZE, 0, &digest_ptr);
+
+       ret = bpf_get_fsverity_digest(f, &digest_ptr);
+       /* No verity, allow access */
+       if (ret < 0)
+               return 0;
+
+       /* Move digest_ptr to fsverity_formatted_digest */
+       bpf_dynptr_from_mem(digest, sizeof(digest), 0, &digest_ptr);
+
+       /* Read signature from xattr */
+       bpf_dynptr_from_mem(sig, sizeof(sig), 0, &sig_ptr);
+       bpf_dynptr_from_mem(xattr_name, sizeof(xattr_name), 0, &name_ptr);
+       ret = bpf_get_file_xattr(f, &name_ptr, &sig_ptr);
+       /* No signature, reject access */
+       if (ret < 0)
+               return -EPERM;
+
+       trusted_keyring = bpf_lookup_user_key(user_keyring_serial, 0);
+       if (!trusted_keyring)
+               return -ENOENT;
+
+       /* Verify signature */
+       ret = bpf_verify_pkcs7_signature(&digest_ptr, &sig_ptr, trusted_keyring);
+
+       bpf_key_put(trusted_keyring);
Quoted 8 lines

tools/testing/selftests/bpf/progs/test_sig_in_xattr.c line 84

Quoted 2 lines
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c new file +2 -0
+       return ret;
+}

I think the UX is cumbersome.
Putting a NULL terminated string into dynptr not only a source code ugliness, but it adds run-time overhead too.
We better add proper C style string support in the verifier, so that bpf_get_file_xattr() can look like normal C.

That will indeed look much better. Let me check what do we need make this happen.

Thanks,
Song