bpf: Introduce union trusted
Subsystems (4)
cover bpf: Introduce union trusted
When we are verifying a field in a union, we may verify another field which has the same offset. So we should annotate that field as untrusted. In some cases we have already known that some fields are safe and then we can add them into the union trusted allow list.
Patch #3 fixes an issue found in our dev server.
Changes:
- bpf: Fix errors in verifying a union
https://lore.kernel.org/bpf/[email protected]/
Yafang Shao (3):
bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION selftests/bpf: Add selftests for BTF_TYPE_SAFE_TRUSTED_UNION
bpf: Fix an error in verifying a field in a union
kernel/bpf/btf.c | 22 +++++++++---------- kernel/bpf/verifier.c | 21 ++++++++++++++++++ .../bpf/progs/nested_trust_failure.c | 16 ++++++++++++++ .../bpf/progs/nested_trust_success.c | 15 +++++++++++++ 4 files changed, 62 insertions(+), 12 deletions(-)
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH 1/3 bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION
When we are verifying a field in a union, we may unexpectedly verify another field which has the same offset in this union. So in such case, we should annotate that field as PTR_UNTRUSTED. However, in some cases we are sure some fields in a union is safe and then we can add them into BTF_TYPE_SAFE_TRUSTED_UNION allow list.
--- kernel/bpf/btf.c | 20 +++++++++----------- kernel/bpf/verifier.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-)
kernel/bpf/btf.c +9 -11
@@ -6133,7 +6133,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,const char *tname, *mname, *tag_value;u32 vlen, elem_id, mid; - *flag =0 ; again:if (btf_type_is_modifier (t)) t =btf_type_skip_modifiers (btf, t->type,NULL );
@@ -6144,6 +6143,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, } vlen =btf_type_vlen (t); +if (BTF_INFO_KIND (t->info) ==BTF_KIND_UNION && vlen !=1 && !(*flag &PTR_UNTRUSTED )) +/* +* walking unions yields untrusted pointers +* with exception of __bpf_md_ptr and other +* unions with a single member +*/ + *flag |=PTR_UNTRUSTED ; +if (off + size > t->size) {/* If the last element is a variable size array, we may * need to relax the rule.
@@ -6304,15 +6311,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* of this field or inside of this struct */ if (btf_type_is_struct (mtype)) { -if (BTF_INFO_KIND (mtype->info) ==BTF_KIND_UNION && -btf_type_vlen (mtype) !=1 ) -/* -* walking unions yields untrusted pointers -* with exception of __bpf_md_ptr and other -* unions with a single member -*/ - *flag |=PTR_UNTRUSTED ; -/* our field must be inside that union or struct */ t = mtype;
@@ -6478,7 +6476,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, bool strict) {const struct btf_type *type; -enum bpf_type_flag flag; +enum bpf_type_flag flag =0 ;int err;/* Are we already done? */
kernel/bpf/verifier.c +21 -0
@@ -5847,6 +5847,7 @@ static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)#define BTF_TYPE_SAFE_RCU (__type) __PASTE(__type, __safe_rcu)#define BTF_TYPE_SAFE_RCU_OR_NULL (__type) __PASTE(__type, __safe_rcu_or_null)#define BTF_TYPE_SAFE_TRUSTED (__type) __PASTE(__type, __safe_trusted) +#define BTF_TYPE_SAFE_TRUSTED_UNION (__type) __PASTE(__type, __safe_trusted_union)/* * Allow list few fields as RCU trusted or full trusted.
@@ -5914,6 +5915,11 @@ BTF_TYPE_SAFE_TRUSTED(struct socket) {struct sock *sk; }; +/* union trusted: these fields are trusted even in a uion */ +BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff) { +struct sock *sk; +}; +static bool type_is_rcu (struct bpf_verifier_env *env,struct bpf_reg_state *reg,const char *field_name,u32 btf_id)
@@ -5950,6 +5956,17 @@ static bool type_is_trusted(struct bpf_verifier_env *env,return btf_nested_type_is_trusted (&env->log, reg, field_name, btf_id,"__safe_trusted" ); } + +static bool type_is_trusted_union (struct bpf_verifier_env *env, +struct bpf_reg_state *reg, +const char *field_name,u32 btf_id) +{ +BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff )); + +return btf_nested_type_is_trusted (&env->log, reg, field_name, btf_id, +"__safe_trusted_union" ); +} + static int check_ptr_to_btf_access(struct bpf_verifier_env *env, struct bpf_reg_state *regs, int regno, int off, int size,
@@ -6087,6 +6104,10 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,clear_trusted_flags (&flag); } +/* Clear the PTR_UNTRUSTED for the fields which are in the allow list */ +if (type_is_trusted_union (env, reg, field_name, btf_id)) + flag &= ~PTR_UNTRUSTED ; +if (atype ==BPF_READ && value_regno >=0 )mark_btf_ld_reg (env, regs, value_regno, ret, reg->btf, btf_id, flag);
Signature · 1 line
2.30.1 (Apple Git-130)
Re: [PATCH bpf-next 1/3] bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION
Quoted 80 lines · Yafang Shao
When we are verifying a field in a union, we may unexpectedly verify another field which has the same offset in this union. So in such case, we should annotate that field as
PTR_UNTRUSTED. However, in some cases we are sure some fields in a union is safe and then we can add them intoBTF_TYPE_SAFE_TRUSTED_UNIONallow list.Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/btf.c | 20 +++++++++----------- kernel/bpf/verifier.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-)kernel/bpf/btf.c +9
-11@@ -6133,7 +6133,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,const char *tname, *mname, *tag_value;u32 vlen, elem_id, mid; - *flag =0 ; again:if (btf_type_is_modifier (t)) t =btf_type_skip_modifiers (btf, t->type,NULL );@@ -6144,6 +6143,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, } vlen =btf_type_vlen (t); +if (BTF_INFO_KIND (t->info) ==BTF_KIND_UNION && vlen !=1 && !(*flag &PTR_UNTRUSTED )) +/* +* walking unions yields untrusted pointers +* with exception of __bpf_md_ptr and other +* unions with a single member +*/ + *flag |=PTR_UNTRUSTED ; +if (off + size > t->size) {/* If the last element is a variable size array, we may * need to relax the rule. @@ -6304,15 +6311,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* of this field or inside of this struct */ if (btf_type_is_struct (mtype)) { -if (BTF_INFO_KIND (mtype->info) ==BTF_KIND_UNION && -btf_type_vlen (mtype) !=1 ) -/* -* walking unions yields untrusted pointers -* with exception of __bpf_md_ptr and other -* unions with a single member -*/ - *flag |=PTR_UNTRUSTED ; -/* our field must be inside that union or struct */ t = mtype;@@ -6478,7 +6476,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, bool strict) {const struct btf_type *type; -enum bpf_type_flag flag; +enum bpf_type_flag flag =0 ;int err;/* Are we already done? */ kernel/bpf/verifier.c +1
-0@@ -5847,6 +5847,7 @@ static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)#define BTF_TYPE_SAFE_RCU (__type) __PASTE(__type, __safe_rcu)#define BTF_TYPE_SAFE_RCU_OR_NULL (__type) __PASTE(__type, __safe_rcu_or_null)#define BTF_TYPE_SAFE_TRUSTED (__type) __PASTE(__type, __safe_trusted) +#define BTF_TYPE_SAFE_TRUSTED_UNION (__type) __PASTE(__type, __safe_trusted_union)/* * Allow list few fields as RCU trusted or full trusted. @@ -5914,6 +5915,11 @@ BTF_TYPE_SAFE_TRUSTED(struct socket) {struct sock *sk; };
[..]
kernel/bpf/verifier.c BTF_TYPE_SAFE_TRUSTED(struct socket) { · line 5921
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+/* union trusted: these fields are trusted even in a uion */ +BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff) { +struct sock *sk; +};
Does it say that sk member of sk_buff is always dereferencable? Why is it universally safe?
In general, I don't really understand why it's safe to statically mark the members this way. Shouldn't it depend on the context?
Re: [PATCH bpf-next 1/3] bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION
Quoted 86 lines · Stanislav Fomichev
Quoted 80 lines · Yafang Shao
When we are verifying a field in a union, we may unexpectedly verify another field which has the same offset in this union. So in such case, we should annotate that field as
PTR_UNTRUSTED. However, in some cases we are sure some fields in a union is safe and then we can add them intoBTF_TYPE_SAFE_TRUSTED_UNIONallow list.Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/btf.c | 20 +++++++++----------- kernel/bpf/verifier.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-)kernel/bpf/btf.c +9
-11@@ -6133,7 +6133,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,const char *tname, *mname, *tag_value;u32 vlen, elem_id, mid; - *flag =0 ; again:if (btf_type_is_modifier (t)) t =btf_type_skip_modifiers (btf, t->type,NULL );@@ -6144,6 +6143,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, } vlen =btf_type_vlen (t); +if (BTF_INFO_KIND (t->info) ==BTF_KIND_UNION && vlen !=1 && !(*flag &PTR_UNTRUSTED )) +/* +* walking unions yields untrusted pointers +* with exception of __bpf_md_ptr and other +* unions with a single member +*/ + *flag |=PTR_UNTRUSTED ; +if (off + size > t->size) {/* If the last element is a variable size array, we may * need to relax the rule. @@ -6304,15 +6311,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* of this field or inside of this struct */ if (btf_type_is_struct (mtype)) { -if (BTF_INFO_KIND (mtype->info) ==BTF_KIND_UNION && -btf_type_vlen (mtype) !=1 ) -/* -* walking unions yields untrusted pointers -* with exception of __bpf_md_ptr and other -* unions with a single member -*/ - *flag |=PTR_UNTRUSTED ; -/* our field must be inside that union or struct */ t = mtype;@@ -6478,7 +6476,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, bool strict) {const struct btf_type *type; -enum bpf_type_flag flag; +enum bpf_type_flag flag =0 ;int err;/* Are we already done? */ kernel/bpf/verifier.c +1
-0@@ -5847,6 +5847,7 @@ static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)#define BTF_TYPE_SAFE_RCU (__type) __PASTE(__type, __safe_rcu)#define BTF_TYPE_SAFE_RCU_OR_NULL (__type) __PASTE(__type, __safe_rcu_or_null)#define BTF_TYPE_SAFE_TRUSTED (__type) __PASTE(__type, __safe_trusted) +#define BTF_TYPE_SAFE_TRUSTED_UNION (__type) __PASTE(__type, __safe_trusted_union)/* * Allow list few fields as RCU trusted or full trusted. @@ -5914,6 +5915,11 @@ BTF_TYPE_SAFE_TRUSTED(struct socket) {struct sock *sk; };[..]
Quoted 9 lines
kernel/bpf/verifier.c BTF_TYPE_SAFE_TRUSTED(struct socket) { · line 5921
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+/* union trusted: these fields are trusted even in a uion */ +BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff) { +struct sock *sk; +};Does it say that sk member of
sk_buffis always dereferencable? Why is it universally safe?
In general, I don't really understand why it's safe to statically mark the members this way. Shouldn't it depend on the context?
Right. It should depend on the context. Will change it.
Signature · 2 lines
Regards Yafang
Re: [PATCH bpf-next 1/3] bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION
In reply to Yafang ShaoQuoted 78 lines · Yafang Shao
When we are verifying a field in a union, we may unexpectedly verify another field which has the same offset in this union. So in such case, we should annotate that field as
PTR_UNTRUSTED. However, in some cases we are sure some fields in a union is safe and then we can add them intoBTF_TYPE_SAFE_TRUSTED_UNIONallow list.Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/btf.c | 20 +++++++++----------- kernel/bpf/verifier.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-)kernel/bpf/btf.c +9
-11@@ -6133,7 +6133,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,const char *tname, *mname, *tag_value;u32 vlen, elem_id, mid; - *flag =0 ; again:if (btf_type_is_modifier (t)) t =btf_type_skip_modifiers (btf, t->type,NULL );@@ -6144,6 +6143,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, } vlen =btf_type_vlen (t); +if (BTF_INFO_KIND (t->info) ==BTF_KIND_UNION && vlen !=1 && !(*flag &PTR_UNTRUSTED )) +/* +* walking unions yields untrusted pointers +* with exception of __bpf_md_ptr and other +* unions with a single member +*/ + *flag |=PTR_UNTRUSTED ; +if (off + size > t->size) {/* If the last element is a variable size array, we may * need to relax the rule. @@ -6304,15 +6311,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* of this field or inside of this struct */ if (btf_type_is_struct (mtype)) { -if (BTF_INFO_KIND (mtype->info) ==BTF_KIND_UNION && -btf_type_vlen (mtype) !=1 ) -/* -* walking unions yields untrusted pointers -* with exception of __bpf_md_ptr and other -* unions with a single member -*/ - *flag |=PTR_UNTRUSTED ; -/* our field must be inside that union or struct */ t = mtype;@@ -6478,7 +6476,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, bool strict) {const struct btf_type *type; -enum bpf_type_flag flag; +enum bpf_type_flag flag =0 ;int err;/* Are we already done? */ kernel/bpf/verifier.c +1
-0@@ -5847,6 +5847,7 @@ static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)#define BTF_TYPE_SAFE_RCU (__type) __PASTE(__type, __safe_rcu)#define BTF_TYPE_SAFE_RCU_OR_NULL (__type) __PASTE(__type, __safe_rcu_or_null)#define BTF_TYPE_SAFE_TRUSTED (__type) __PASTE(__type, __safe_trusted) +#define BTF_TYPE_SAFE_TRUSTED_UNION (__type) __PASTE(__type, __safe_trusted_union)/* * Allow list few fields as RCU trusted or full trusted.
kernel/bpf/verifier.c BTF_TYPE_SAFE_TRUSTED(struct socket) { · line 5921
Quoted 7 lines
kernel/bpf/verifier.c +4
-0@@ -5914,6 +5915,11 @@ BTF_TYPE_SAFE_TRUSTED(struct socket) {struct sock *sk; }; +/* union trusted: these fields are trusted even in a uion */ +BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff) { +struct sock *sk; +};
Why is this needed?
We already have:
BTF_TYPE_SAFE_RCU_OR_NULL(struct sk_buff) {
struct sock *sk;
};kernel/bpf/verifier.c static int check_ptr_to_btf_access(struct bpf_verifier_env *env, · line 6109
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+/* Clear the PTR_UNTRUSTED for the fields which are in the allow list */ +if (type_is_trusted_union (env, reg, field_name, btf_id)) + flag &= ~PTR_UNTRUSTED ;
we cannot do this unconditionally.
The type_is_rcu_or_null() check applies only after
in_rcu_cs(env) && !type_may_be_null(reg->type)).
Re: [PATCH bpf-next 1/3] bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION
Quoted 83 lines · Alexei Starovoitov
Quoted 81 lines · Yafang Shao
When we are verifying a field in a union, we may unexpectedly verify another field which has the same offset in this union. So in such case, we should annotate that field as
PTR_UNTRUSTED. However, in some cases we are sure some fields in a union is safe and then we can add them intoBTF_TYPE_SAFE_TRUSTED_UNIONallow list.Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/btf.c | 20 +++++++++----------- kernel/bpf/verifier.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-)kernel/bpf/btf.c +9
-11@@ -6133,7 +6133,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,const char *tname, *mname, *tag_value;u32 vlen, elem_id, mid; - *flag =0 ; again:if (btf_type_is_modifier (t)) t =btf_type_skip_modifiers (btf, t->type,NULL );@@ -6144,6 +6143,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, } vlen =btf_type_vlen (t); +if (BTF_INFO_KIND (t->info) ==BTF_KIND_UNION && vlen !=1 && !(*flag &PTR_UNTRUSTED )) +/* +* walking unions yields untrusted pointers +* with exception of __bpf_md_ptr and other +* unions with a single member +*/ + *flag |=PTR_UNTRUSTED ; +if (off + size > t->size) {/* If the last element is a variable size array, we may * need to relax the rule. @@ -6304,15 +6311,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* of this field or inside of this struct */ if (btf_type_is_struct (mtype)) { -if (BTF_INFO_KIND (mtype->info) ==BTF_KIND_UNION && -btf_type_vlen (mtype) !=1 ) -/* -* walking unions yields untrusted pointers -* with exception of __bpf_md_ptr and other -* unions with a single member -*/ - *flag |=PTR_UNTRUSTED ; -/* our field must be inside that union or struct */ t = mtype;@@ -6478,7 +6476,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, bool strict) {const struct btf_type *type; -enum bpf_type_flag flag; +enum bpf_type_flag flag =0 ;int err;/* Are we already done? */ kernel/bpf/verifier.c +1
-0@@ -5847,6 +5847,7 @@ static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)#define BTF_TYPE_SAFE_RCU (__type) __PASTE(__type, __safe_rcu)#define BTF_TYPE_SAFE_RCU_OR_NULL (__type) __PASTE(__type, __safe_rcu_or_null)#define BTF_TYPE_SAFE_TRUSTED (__type) __PASTE(__type, __safe_trusted) +#define BTF_TYPE_SAFE_TRUSTED_UNION (__type) __PASTE(__type, __safe_trusted_union)/* * Allow list few fields as RCU trusted or full trusted. @@ -5914,6 +5915,11 @@ BTF_TYPE_SAFE_TRUSTED(struct socket) {struct sock *sk; };
Quoted 6 lines
kernel/bpf/verifier.c BTF_TYPE_SAFE_TRUSTED(struct socket) { · line 5921
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+/* union trusted: these fields are trusted even in a uion */ +BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff) { +struct sock *sk; +};Why is this needed?
Will discard it.
Quoted 5 lines
We already have:
BTF_TYPE_SAFE_RCU_OR_NULL(struct sk_buff) { struct sock *sk; };
Quoted 7 lines
kernel/bpf/verifier.c static int check_ptr_to_btf_access(struct bpf_verifier_env *env, · line 6109
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+/* Clear the PTR_UNTRUSTED for the fields which are in the allow list */ +if (type_is_trusted_union (env, reg, field_name, btf_id)) + flag &= ~PTR_UNTRUSTED ;we cannot do this unconditionally.
Thetype_is_rcu_or_null()check applies only after
in_rcu_cs(env)&& !type_may_be_null(reg->type)).
Thanks for the explanation. Will change it.
Signature · 2 lines
Regards Yafang
PATCH 2/3 selftests/bpf: Add selftests for BTF_TYPE_SAFE_TRUSTED_UNION
In reply to Yafang ShaoAdd selftests for BTF_TYPE_SAFE_TRUSTED_UNION, the result as follows:
#141/1 nested_trust/test_read_cpumask:OK #141/2 nested_trust/test_skb_field:OK <<<< #141/3 nested_trust/test_invalid_nested_user_cpus:OK #141/4 nested_trust/test_invalid_nested_offset:OK #141/5 nested_trust/test_invalid_skb_field:OK <<<< #141 nested_trust:OK
--- .../selftests/bpf/progs/nested_trust_failure.c | 16 ++++++++++++++++ .../selftests/bpf/progs/nested_trust_success.c | 15 +++++++++++++++ 2 files changed, 31 insertions(+)
tools/testing/selftests/bpf/progs/nested_trust_failure.c +16 -0
@@ -10,6 +10,13 @@ char _license[] SEC("license") = "GPL"; +struct { + __uint(type, BPF_MAP_TYPE_SK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, u64); +} sk_storage_map SEC(".maps"); +/* Prototype for all of the program trace events below: * * TRACE_EVENT(task_newtask,
@@ -31,3 +38,12 @@ int BPF_PROG(test_invalid_nested_offset, struct task_struct *task, u64 clone_flabpf_cpumask_first_zero (&task->cpus_mask);return 0 ; } + +/* Although R2 is of type sk_buff but sock_common is expected, we will hit untrusted ptr first. */ +SEC("tp_btf/tcp_probe") +__failure __msg("R2 type=untrusted_ptr_ expected=ptr_, trusted_ptr_, rcu_ptr_") +int BPF_PROG(test_invalid_skb_field, struct sock *sk, struct sk_buff *skb) +{ +bpf_sk_storage_get (&sk_storage_map, skb->next,0 ,0 ); +return 0 ; +}
tools/testing/selftests/bpf/progs/nested_trust_success.c +15 -0
@@ -10,6 +10,13 @@ char _license[] SEC("license") = "GPL"; +struct { + __uint(type, BPF_MAP_TYPE_SK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, u64); +} sk_storage_map SEC(".maps"); + SEC("tp_btf/task_newtask") __successint BPF_PROG (test_read_cpumask ,struct task_struct *task,u64 clone_flags)
@@ -17,3 +24,11 @@ int BPF_PROG(test_read_cpumask, struct task_struct *task, u64 clone_flags)bpf_cpumask_test_cpu (0 , task->cpus_ptr);return 0 ; } + +SEC("tp_btf/tcp_probe") +__success +int BPF_PROG (test_skb_field ,struct sock *sk,struct sk_buff *skb) +{ +bpf_sk_storage_get (&sk_storage_map, skb->sk,0 ,0 ); +return 0 ; +}
Signature · 1 line
2.30.1 (Apple Git-130)
PATCH 3/3 bpf: Fix an error in verifying a field in a union
In reply to Yafang ShaoWe are utilizing BPF LSM to monitor BPF operations within our container environment. When we add support for raw_tracepoint, it hits below error.
; (const void *)attr->raw_tracepoint.name);
27: (79) r3 = *(u64 *)(r2 +0)
access beyond the end of member map_type (mend:4) in struct (anon) with off 0 size 8
It can be reproduced with below BPF prog.
SEC("lsm/bpf")
int BPF_PROG(bpf_audit, int cmd, union bpf_attr *attr, unsigned int size)
{
switch (cmd) {
case BPF_RAW_TRACEPOINT_OPEN:
bpf_printk("raw_tracepoint is %s", attr->raw_tracepoint.name);
break;
default:
break;
}
return 0;
}The reason is that when accessing a field in a union, such as bpf_attr, if the field is located within a nested struct that is not the first member of the union, it can result in incorrect field verification.
union bpf_attr {
struct {
__u32 map_type; <<<< Actually it will find that field.
__u32 key_size;
__u32 value_size;
...
};
...
struct {
__u64 name; <<<< We want to verify this field.
__u32 prog_fd;
} raw_tracepoint;
};Considering the potential deep nesting levels, finding a perfect solution to address this issue has proven challenging. Therefore, I propose a solution where we simply skip the verification process if the field in question is located within a union.
--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
@@ -6368,7 +6368,7 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* that also allows using an array of int as a scratch * space. e.g. skb->cb[]. */ -if (off + size > mtrue_end) { +if (off + size > mtrue_end && !(*flag &PTR_UNTRUSTED )) {bpf_log (log,"access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n" , mname, mtrue_end, tname, off, size);
Signature · 1 line
2.30.1 (Apple Git-130)
Re: [PATCH bpf-next 3/3] bpf: Fix an error in verifying a field in a union
Quoted 58 lines · Yafang Shao
We are utilizing BPF LSM to monitor BPF operations within our container environment. When we add support for
raw_tracepoint, it hits below error.; (const void *)attr->raw_tracepoint.name);
27: (79) r3 = *(u64 *)(r2 +0)
access beyond the end of membermap_type(mend:4) in struct (anon) with off 0 size 8It can be reproduced with below BPF prog.
SEC("lsm/bpf")
intBPF_PROG(bpf_audit, int cmd, union bpf_attr *attr, unsigned int size){ switch (cmd) { case BPF_RAW_TRACEPOINT_OPEN: bpf_printk("raw_tracepoint is %s", attr->raw_tracepoint.name); break; default: break; } return 0; }The reason is that when accessing a field in a union, such as
bpf_attr, if the field is located within a nested struct that is not the first member of the union, it can result in incorrect field verification.union bpf_attr { struct { __u32 map_type; <<<< Actually it will find that field. __u32 key_size; __u32 value_size; ... }; ... struct { __u64 name; <<<< We want to verify this field. __u32 prog_fd; } raw_tracepoint; };Considering the potential deep nesting levels, finding a perfect solution to address this issue has proven challenging. Therefore, I propose a solution where we simply skip the verification process if the field in question is located within a union.
Fixes 7e3617a72df3 ("bpf: Add array support to btf_struct_access")Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/btf.c +0
-0
kernel/bpf/btf.c static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, · line 6371
Quoted 5 lines
kernel/bpf/btf.c +1
-1@@ -6368,7 +6368,7 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* that also allows using an array of int as a scratch * space. e.g. skb->cb[]. */ -if (off + size > mtrue_end) { +if (off + size > mtrue_end && !(*flag &PTR_UNTRUSTED )) {
The selftest for this condition is missing.
Re: [PATCH bpf-next 3/3] bpf: Fix an error in verifying a field in a union
Quoted 55 lines · Alexei Starovoitov
Quoted 53 lines · Yafang Shao
We are utilizing BPF LSM to monitor BPF operations within our container environment. When we add support for
raw_tracepoint, it hits below error.; (const void *)attr->raw_tracepoint.name);
27: (79) r3 = *(u64 *)(r2 +0)
access beyond the end of membermap_type(mend:4) in struct (anon) with off 0 size 8It can be reproduced with below BPF prog.
SEC("lsm/bpf")
intBPF_PROG(bpf_audit, int cmd, union bpf_attr *attr, unsigned int size){ switch (cmd) { case BPF_RAW_TRACEPOINT_OPEN: bpf_printk("raw_tracepoint is %s", attr->raw_tracepoint.name); break; default: break; } return 0; }The reason is that when accessing a field in a union, such as
bpf_attr, if the field is located within a nested struct that is not the first member of the union, it can result in incorrect field verification.union bpf_attr { struct { __u32 map_type; <<<< Actually it will find that field. __u32 key_size; __u32 value_size; ... }; ... struct { __u64 name; <<<< We want to verify this field. __u32 prog_fd; } raw_tracepoint; };Considering the potential deep nesting levels, finding a perfect solution to address this issue has proven challenging. Therefore, I propose a solution where we simply skip the verification process if the field in question is located within a union.
Fixes 7e3617a72df3 ("bpf: Add array support to btf_struct_access")Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
kernel/bpf/btf.c static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, · line 6371
Quoted 10 lines
kernel/bpf/btf.c +1
-1@@ -6368,7 +6368,7 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,* that also allows using an array of int as a scratch * space. e.g. skb->cb[]. */ -if (off + size > mtrue_end) { +if (off + size > mtrue_end && !(*flag &PTR_UNTRUSTED )) {The selftest for this condition is missing.
Will add it.
Signature · 2 lines
Regards Yafang