Introduce composable bpf types
Subsystems (13)
v2 cover Introduce composable bpf types
This patch set consists of two changes:
- a cleanup of arg_type, ret_type and reg_type which try to make those types composable. (patch 1/9 - patch 6/9)
- a bug fix that prevents bpf programs from writing kernel memory. (patch 7/9 - patch 9/9)
The purpose of the cleanup is to find a scalable way to expressing type nullness and read-onliness. This patchset introduces two flags that can be applied on all three types: PTR_MAYBE_NULL and MEM_RDONLY. Previous types such as ARG_XXX_OR_NULL can now be written as
ARG_XXX | PTR_MAYBE_NULL
Similarly, PTR_TO_RDONLY_BUF is now "PTR_TO_BUF | MEM_RDONLY".
Flags can be composed, as ARGs can be both MEM_RDONLY and MAYBE_NULL.
ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY
Based on this new composable types, patch 7/9 applies MEM_RDONLY on PTR_TO_MEM, in order to tag the returned memory from per_cpu_ptr as read-only. Therefore fixing a previous bug that one can leverage per_cpu_ptr to modify kernel memory within BPF programs.
Patch 8/9 generalizes the use of MEM_RDONLY further by tagging a set of helper arguments ARG_PTR_TO_MEM with MEM_RDONLY. Some helper functions may override their arguments, such as bpf_d_path, bpf_snprintf. In this patch, we narrow the ARG_PTR_TO_MEM to be compatible with only a subset of memory types. This prevents these helpers from writing read-only memories. For the helpers that do not write its arguments, we add tag MEM_RDONLY to allow taking a RDONLY memory as argument.
Previous versions of this patchset:
[1] https://lore.kernel.org/bpf/[email protected]/T/
[2] https://lore.kernel.org/bpf/[email protected]/T/
Hao Luo (9):
bpf: Introduce composable reg, ret and arg types.
bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
bpf: Introduce MEM_RDONLY flag
bpf: Convert PTR_TO_MEM_OR_NULL to composable types.
bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem. bpf/selftests: Test PTR_TO_RDONLY_MEM
include/linux/bpf.h | 105 +++- kernel/bpf/btf.c | 13 +- kernel/bpf/cgroup.c | 2 +- kernel/bpf/helpers.c | 12 +- kernel/bpf/map_iter.c | 4 +- kernel/bpf/ringbuf.c | 2 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 455 +++++++++--------- kernel/trace/bpf_trace.c | 26 +- net/core/bpf_sk_storage.c | 2 +- net/core/filter.c | 64 +-- net/core/sock_map.c | 2 +- .../selftests/bpf/prog_tests/ksyms_btf.c | 14 + .../bpf/progs/test_ksyms_btf_write_check.c | 29 ++ 14 files changed, 414 insertions(+), 318 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
Signature · 1 line
2.34.0.384.gca35af8252-goog
RFC PATCH v2 1/9 bpf: Introduce composable reg, ret and arg types.
There are some common properties shared between bpf reg, ret and arg values. For instance, a value may be a NULL pointer, or a pointer to a read-only memory. Previously, to express these properties, enumeration was used. For example, in order to test whether a reg value can be NULL, reg_type_may_be_null() simply enumerates all types that are possibly NULL. The problem of this approach is that it's not scalable and causes a lot of duplication. These properties can be combined, for example, a type could be either MAYBE_NULL or RDONLY, or both.
This patch series rewrites the layout of reg_type, arg_type and ret_type, so that common properties can be extracted and represented as composable flag. For example, one can write
ARG_PTR_TO_MEM | PTR_MAYBE_NULL
which is equivalent to the previous
ARG_PTR_TO_MEM_OR_NULL
The type ARG_PTR_TO_MEM are called "base types" in this patch. Base types can be extended with flags. A flag occupies the higher bits while base types sits in the lower bits.
This patch in particular sets up a set of macro for this purpose. The followed patches rewrites arg_types, ret_types and reg_types respectively.
--- include/linux/bpf.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)
include/linux/bpf.h +50 -0
@@ -297,6 +297,37 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,extern const struct bpf_map_ops bpf_map_offload_ops; +/* bpf_type_flag contains a set of flags that are applicable to the values of +* arg_type, ret_type and reg_type. For example, a pointer value may be null, +* or a memory is read-only. We classify types into two categories: base types +* and extended types. Extended types are base types combined with a type flag. +* +* Currently there are no more than 32 base types in arg_type, ret_type and +* reg_types. +*/ +#define BPF_BASE_TYPE_BITS 8 + +enum bpf_type_flag { +/* PTR may be NULL. */ +PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), + + __BPF_TYPE_LAST_FLAG =PTR_MAYBE_NULL , +}; + +#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS, 0) + +/* Max number of base types. */ +#define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS) + +/* Max number of all types. */ +#define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1)) + +/* extract base type. */ +#define BPF_BASE_TYPE (x) ((x) & BPF_BASE_TYPE_MASK) + +/* extract flags from an extended type. */ +#define BPF_TYPE_FLAG (x) ((enum bpf_type_flag)((x) & ~BPF_BASE_TYPE_MASK)) +/* function argument constraints */ enum bpf_arg_type {ARG_DONTCARE =0 ,/* unused argument in helper function */
@@ -343,7 +374,13 @@ enum bpf_arg_type {ARG_PTR_TO_CONST_STR ,/* pointer to a null terminated read-only string */ ARG_PTR_TO_TIMER ,/* pointer to bpf_timer */ __BPF_ARG_TYPE_MAX, + +/* This must be the last entry. Its purpose is to ensure the enum is +* wide enough to hold the higher bits reserved for bpf_type_flag. +*/ + __BPF_ARG_TYPE_LIMIT =BPF_TYPE_LIMIT , }; +static_assert (__BPF_ARG_TYPE_MAX <=BPF_BASE_TYPE_LIMIT );/* type of values returned from helper functions */ enum bpf_return_type {
@@ -359,7 +396,14 @@ enum bpf_return_type {RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ,/* returns a pointer to a valid memory or a btf_id or NULL */ RET_PTR_TO_MEM_OR_BTF_ID ,/* returns a pointer to a valid memory or a btf_id */ RET_PTR_TO_BTF_ID ,/* returns a pointer to a btf_id */ + __BPF_RET_TYPE_MAX, + +/* This must be the last entry. Its purpose is to ensure the enum is +* wide enough to hold the higher bits reserved for bpf_type_flag. +*/ + __BPF_RET_TYPE_LIMIT =BPF_TYPE_LIMIT , }; +static_assert (__BPF_RET_TYPE_MAX <=BPF_BASE_TYPE_LIMIT );/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
@@ -461,7 +505,13 @@ enum bpf_reg_type {PTR_TO_FUNC ,/* reg points to a bpf program function */ PTR_TO_MAP_KEY ,/* reg points to a map element key */ __BPF_REG_TYPE_MAX, + +/* This must be the last entry. Its purpose is to ensure the enum is +* wide enough to hold the higher bits reserved for bpf_type_flag. +*/ + __BPF_REG_TYPE_LIMIT =BPF_TYPE_LIMIT , }; +static_assert (__BPF_REG_TYPE_MAX <=BPF_BASE_TYPE_LIMIT );/* The information passed from prog-specific *_is_valid_access * back to the verifier.
Signature · 1 line
2.34.0.384.gca35af8252-goog
Re: [RFC PATCH bpf-next v2 1/9] bpf: Introduce composable reg, ret and arg types.
Quoted 60 lines · Hao Luo
There are some common properties shared between bpf reg, ret and arg values. For instance, a value may be a NULL pointer, or a pointer to a read-only memory. Previously, to express these properties, enumeration was used. For example, in order to test whether a reg value can be NULL,
reg_type_may_be_null()simply enumerates all types that are possibly NULL. The problem of this approach is that it's not scalable and causes a lot of duplication. These properties can be combined, for example, a type could be eitherMAYBE_NULLor RDONLY, or both.This patch series rewrites the layout of
reg_type,arg_typeandret_type, so that common properties can be extracted and represented as composable flag. For example, one can write
ARG_PTR_TO_MEM|PTR_MAYBE_NULLwhich is equivalent to the previous
ARG_PTR_TO_MEM_OR_NULLThe type
ARG_PTR_TO_MEMare called "base types" in this patch. Base types can be extended with flags. A flag occupies the higher bits while base types sits in the lower bits.This patch in particular sets up a set of macro for this purpose. The followed patches rewrites
arg_types,ret_typesandreg_typesrespectively.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)include/linux/bpf.h +20
-0@@ -297,6 +297,37 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,extern const struct bpf_map_ops bpf_map_offload_ops; +/* bpf_type_flag contains a set of flags that are applicable to the values of +* arg_type, ret_type and reg_type. For example, a pointer value may be null, +* or a memory is read-only. We classify types into two categories: base types +* and extended types. Extended types are base types combined with a type flag. +* +* Currently there are no more than 32 base types in arg_type, ret_type and +* reg_types. +*/ +#define BPF_BASE_TYPE_BITS 8 + +enum bpf_type_flag { +/* PTR may be NULL. */ +PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), + + __BPF_TYPE_LAST_FLAG =PTR_MAYBE_NULL , +}; + +#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS, 0) + +/* Max number of base types. */
include/linux/bpf.h bool bpf_map_meta_equal(const struct bpf_map *meta0, · line 329
Quoted 10 lines
include/linux/bpf.h +10
-0+#define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS) + +/* Max number of all types. */ +#define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1)) + +/* extract base type. */ +#define BPF_BASE_TYPE (x) ((x) & BPF_BASE_TYPE_MASK) + +/* extract flags from an extended type. */ +#define BPF_TYPE_FLAG (x) ((enum bpf_type_flag)((x) & ~BPF_BASE_TYPE_MASK))
Overall I think it's really great.
The only suggestion is to use:
static inline u32 base_type(u32 x)
{
return x & BPF_BASE_TYPE_MASK;
}
and
static inline u32 type_flag(u32 x) ..
The capital letter macros are too loud.
wdyt?
Re: [RFC PATCH bpf-next v2 1/9] bpf: Introduce composable reg, ret and arg types.
Quoted 82 lines · Alexei Starovoitov
Quoted 60 lines · Hao Luo
There are some common properties shared between bpf reg, ret and arg values. For instance, a value may be a NULL pointer, or a pointer to a read-only memory. Previously, to express these properties, enumeration was used. For example, in order to test whether a reg value can be NULL,
reg_type_may_be_null()simply enumerates all types that are possibly NULL. The problem of this approach is that it's not scalable and causes a lot of duplication. These properties can be combined, for example, a type could be eitherMAYBE_NULLor RDONLY, or both.This patch series rewrites the layout of
reg_type,arg_typeandret_type, so that common properties can be extracted and represented as composable flag. For example, one can write
ARG_PTR_TO_MEM|PTR_MAYBE_NULLwhich is equivalent to the previous
ARG_PTR_TO_MEM_OR_NULLThe type
ARG_PTR_TO_MEMare called "base types" in this patch. Base types can be extended with flags. A flag occupies the higher bits while base types sits in the lower bits.This patch in particular sets up a set of macro for this purpose. The followed patches rewrites
arg_types,ret_typesandreg_typesrespectively.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)include/linux/bpf.h +20
-0@@ -297,6 +297,37 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,extern const struct bpf_map_ops bpf_map_offload_ops; +/* bpf_type_flag contains a set of flags that are applicable to the values of +* arg_type, ret_type and reg_type. For example, a pointer value may be null, +* or a memory is read-only. We classify types into two categories: base types +* and extended types. Extended types are base types combined with a type flag. +* +* Currently there are no more than 32 base types in arg_type, ret_type and +* reg_types. +*/ +#define BPF_BASE_TYPE_BITS 8 + +enum bpf_type_flag { +/* PTR may be NULL. */ +PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), + + __BPF_TYPE_LAST_FLAG =PTR_MAYBE_NULL , +}; + +#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS, 0) + +/* Max number of base types. */ include/linux/bpf.h bool bpf_map_meta_equal(const struct bpf_map *meta0, · line 329
Quoted 10 lines
include/linux/bpf.h +10
-0+#define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS) + +/* Max number of all types. */ +#define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1)) + +/* extract base type. */ +#define BPF_BASE_TYPE (x) ((x) & BPF_BASE_TYPE_MASK) + +/* extract flags from an extended type. */ +#define BPF_TYPE_FLAG (x) ((enum bpf_type_flag)((x) & ~BPF_BASE_TYPE_MASK))Overall I think it's really great.
The only suggestion is to use:
static inline u32base_type(u32 x)
{
return x &BPF_BASE_TYPE_MASK;
}
and
static inline u32type_flag(u32 x)..
Quoted 4 lines
The capital letter macros are too loud.
wdyt?
Sounds good to me. Will do in the next iteration.
RFC PATCH v2 2/9 bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
In reply to Hao LuoWe have introduced a new type to make bpf_arg composable, by introducing flags to tag a particular type.
One of the flag is PTR_MAYBE_NULL, which indicates a pointer may be NULL. This patch switches the qualified arg_types to use this flag. The arg_types changed in this patch include:
1. ARG_PTR_TO_MAP_VALUE_OR_NULL
2. ARG_PTR_TO_MEM_OR_NULL
3. ARG_PTR_TO_CTX_OR_NULL
4. ARG_PTR_TO_SOCKET_OR_NULL
5. ARG_PTR_TO_ALLOC_MEM_OR_NULL
6. ARG_PTR_TO_STACK_OR_NULL
This patch does not eliminate the use of these arg_types, instead, it makes them an alias to the 'ARG_XXX | PTR_MAYBE_NULL'.
--- include/linux/bpf.h | 15 +++++++++------ kernel/bpf/verifier.c | 35 ++++++++++++----------------------- 2 files changed, 21 insertions(+), 29 deletions(-)
include/linux/bpf.h +9 -6
@@ -339,13 +339,11 @@ enum bpf_arg_type {ARG_PTR_TO_MAP_KEY ,/* pointer to stack used as map key */ ARG_PTR_TO_MAP_VALUE ,/* pointer to stack used as map value */ ARG_PTR_TO_UNINIT_MAP_VALUE ,/* pointer to valid memory used to store a map value */ -ARG_PTR_TO_MAP_VALUE_OR_NULL ,/* pointer to stack used as map value or NULL */ /* the following constraints used to prototype bpf_memcmp() and other * functions that access data on eBPF program stack */ ARG_PTR_TO_MEM ,/* pointer to valid memory (stack, packet, map value) */ -ARG_PTR_TO_MEM_OR_NULL ,/* pointer to valid memory or NULL */ ARG_PTR_TO_UNINIT_MEM ,/* pointer to memory does not need to be initialized, * helper function must fill all bytes or clear * them in error case.
@@ -355,26 +353,31 @@ enum bpf_arg_type {ARG_CONST_SIZE_OR_ZERO ,/* number of bytes accessed from memory or 0 */ ARG_PTR_TO_CTX ,/* pointer to context */ -ARG_PTR_TO_CTX_OR_NULL ,/* pointer to context or NULL */ ARG_ANYTHING ,/* any (initialized) argument is ok */ ARG_PTR_TO_SPIN_LOCK ,/* pointer to bpf_spin_lock */ ARG_PTR_TO_SOCK_COMMON ,/* pointer to sock_common */ ARG_PTR_TO_INT ,/* pointer to int */ ARG_PTR_TO_LONG ,/* pointer to long */ ARG_PTR_TO_SOCKET ,/* pointer to bpf_sock (fullsock) */ -ARG_PTR_TO_SOCKET_OR_NULL ,/* pointer to bpf_sock (fullsock) or NULL */ ARG_PTR_TO_BTF_ID ,/* pointer to in-kernel struct */ ARG_PTR_TO_ALLOC_MEM ,/* pointer to dynamically allocated memory */ -ARG_PTR_TO_ALLOC_MEM_OR_NULL ,/* pointer to dynamically allocated memory or NULL */ ARG_CONST_ALLOC_SIZE_OR_ZERO ,/* number of allocated bytes requested */ ARG_PTR_TO_BTF_ID_SOCK_COMMON ,/* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */ ARG_PTR_TO_PERCPU_BTF_ID ,/* pointer to in-kernel percpu type */ ARG_PTR_TO_FUNC ,/* pointer to a bpf program function */ -ARG_PTR_TO_STACK_OR_NULL ,/* pointer to stack or NULL */ +ARG_PTR_TO_STACK ,/* pointer to stack */ ARG_PTR_TO_CONST_STR ,/* pointer to a null terminated read-only string */ ARG_PTR_TO_TIMER ,/* pointer to bpf_timer */ __BPF_ARG_TYPE_MAX, +/* Extended arg_types. */ +ARG_PTR_TO_MAP_VALUE_OR_NULL =PTR_MAYBE_NULL |ARG_PTR_TO_MAP_VALUE , +ARG_PTR_TO_MEM_OR_NULL =PTR_MAYBE_NULL |ARG_PTR_TO_MEM , +ARG_PTR_TO_CTX_OR_NULL =PTR_MAYBE_NULL |ARG_PTR_TO_CTX , +ARG_PTR_TO_SOCKET_OR_NULL =PTR_MAYBE_NULL |ARG_PTR_TO_SOCKET , +ARG_PTR_TO_ALLOC_MEM_OR_NULL =PTR_MAYBE_NULL |ARG_PTR_TO_ALLOC_MEM , +ARG_PTR_TO_STACK_OR_NULL =PTR_MAYBE_NULL |ARG_PTR_TO_STACK , +/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag. */
kernel/bpf/verifier.c +12 -23
@@ -474,12 +474,7 @@ static bool arg_type_may_be_refcounted(enum bpf_arg_type type)static bool arg_type_may_be_null (enum bpf_arg_type type) { -return type ==ARG_PTR_TO_MAP_VALUE_OR_NULL || - type ==ARG_PTR_TO_MEM_OR_NULL || - type ==ARG_PTR_TO_CTX_OR_NULL || - type ==ARG_PTR_TO_SOCKET_OR_NULL || - type ==ARG_PTR_TO_ALLOC_MEM_OR_NULL || - type ==ARG_PTR_TO_STACK_OR_NULL ; +return BPF_TYPE_FLAG (type) &PTR_MAYBE_NULL ; }/* Determine whether the function releases some resources allocated by another
@@ -4932,9 +4927,8 @@ static int process_timer_func(struct bpf_verifier_env *env, int regno,static bool arg_type_is_mem_ptr (enum bpf_arg_type type) { -return type ==ARG_PTR_TO_MEM || - type ==ARG_PTR_TO_MEM_OR_NULL || - type ==ARG_PTR_TO_UNINIT_MEM ; +return BPF_BASE_TYPE (type) ==ARG_PTR_TO_MEM || +BPF_BASE_TYPE (type) ==ARG_PTR_TO_UNINIT_MEM ; }static bool arg_type_is_mem_size (enum bpf_arg_type type)
@@ -5071,31 +5065,26 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, - [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, [ARG_CONST_SIZE] = &scalar_types, [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, [ARG_CONST_MAP_PTR] = &const_map_ptr_types, [ARG_PTR_TO_CTX] = &context_types, - [ARG_PTR_TO_CTX_OR_NULL] = &context_types, [ARG_PTR_TO_SOCK_COMMON ] = &sock_types, #ifdef CONFIG_NET [ARG_PTR_TO_BTF_ID_SOCK_COMMON ] = &btf_id_sock_common_types, #endif [ARG_PTR_TO_SOCKET] = &fullsock_types, - [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, [ARG_PTR_TO_MEM] = &mem_types, - [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, [ARG_PTR_TO_UNINIT_MEM] = &mem_types, [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, - [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, [ARG_PTR_TO_INT] = &int_ptr_types, [ARG_PTR_TO_LONG] = &int_ptr_types, [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, [ARG_PTR_TO_FUNC] = &func_ptr_types, - [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types, + [ARG_PTR_TO_STACK] = &stack_ptr_types, [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, [ARG_PTR_TO_TIMER] = &timer_types, };
@@ -5109,7 +5098,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,const struct bpf_reg_types *compatible;int i, j; - compatible = compatible_reg_types[arg_type]; + compatible = compatible_reg_types[BPF_BASE_TYPE (arg_type)];if (!compatible) {verbose (env,"verifier internal error: unsupported arg type %d\n" , arg_type);return -EFAULT ;
@@ -5190,9 +5179,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return -EACCES ; } -if (arg_type ==ARG_PTR_TO_MAP_VALUE || - arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE || - arg_type ==ARG_PTR_TO_MAP_VALUE_OR_NULL ) { +if (BPF_BASE_TYPE (arg_type) ==ARG_PTR_TO_MAP_VALUE || +BPF_BASE_TYPE (arg_type) ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { err =resolve_map_arg_type (env, meta, &arg_type);if (err)return err;
@@ -5267,10 +5255,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_helper_mem_access (env, regno, meta->map_ptr->key_size, false,NULL ); - }else if (arg_type ==ARG_PTR_TO_MAP_VALUE || - (arg_type ==ARG_PTR_TO_MAP_VALUE_OR_NULL && - !register_is_null (reg)) || - arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { + }else if (BPF_BASE_TYPE (arg_type) ==ARG_PTR_TO_MAP_VALUE || +BPF_BASE_TYPE (arg_type) ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { +if (!arg_type_may_be_null (arg_type) &®ister_is_null (reg)) +return err; +/* bpf_map_xxx(..., map_ptr, ..., value) call: * check [value, value + map->value_size) validity */
Signature · 1 line
2.34.0.384.gca35af8252-goog
RFC PATCH v2 3/9 bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
In reply to Hao LuoWe have introduced a new type to make bpf_ret composable, by introducing flags to tag a particular type.
One of the flag is PTR_MAYBE_NULL, which indicates a pointer may be NULL. This patch switches the qualified arg_types to use this flag. The ret_types changed in this patch include:
1. RET_PTR_TO_MAP_VALUE_OR_NULL
2. RET_PTR_TO_SOCKET_OR_NULL
3. RET_PTR_TO_TCP_SOCK_OR_NULL
4. RET_PTR_TO_SOCK_COMMON_OR_NULL
5. RET_PTR_TO_ALLOC_MEM_OR_NULL
6. RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL
7. RET_PTR_TO_BTF_ID_OR_NULL
--- include/linux/bpf.h | 19 ++++++++++------ kernel/bpf/helpers.c | 2 +- kernel/bpf/verifier.c | 53 +++++++++++++++++++++++-------------------- 3 files changed, 42 insertions(+), 32 deletions(-)
include/linux/bpf.h +12 -7
@@ -390,17 +390,22 @@ enum bpf_return_type {RET_INTEGER ,/* function returns integer */ RET_VOID ,/* function doesn't return anything */ RET_PTR_TO_MAP_VALUE ,/* returns a pointer to map elem value */ -RET_PTR_TO_MAP_VALUE_OR_NULL ,/* returns a pointer to map elem value or NULL */ -RET_PTR_TO_SOCKET_OR_NULL ,/* returns a pointer to a socket or NULL */ -RET_PTR_TO_TCP_SOCK_OR_NULL ,/* returns a pointer to a tcp_sock or NULL */ -RET_PTR_TO_SOCK_COMMON_OR_NULL ,/* returns a pointer to a sock_common or NULL */ -RET_PTR_TO_ALLOC_MEM_OR_NULL ,/* returns a pointer to dynamically allocated memory or NULL */ -RET_PTR_TO_BTF_ID_OR_NULL ,/* returns a pointer to a btf_id or NULL */ -RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ,/* returns a pointer to a valid memory or a btf_id or NULL */ +RET_PTR_TO_SOCKET ,/* returns a pointer to a socket */ +RET_PTR_TO_TCP_SOCK ,/* returns a pointer to a tcp_sock */ +RET_PTR_TO_SOCK_COMMON ,/* returns a pointer to a sock_common */ +RET_PTR_TO_ALLOC_MEM ,/* returns a pointer to dynamically allocated memory */ RET_PTR_TO_MEM_OR_BTF_ID ,/* returns a pointer to a valid memory or a btf_id */ RET_PTR_TO_BTF_ID ,/* returns a pointer to a btf_id */ __BPF_RET_TYPE_MAX, +/* Extended ret_types. */ +RET_PTR_TO_MAP_VALUE_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_MAP_VALUE , +RET_PTR_TO_SOCKET_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_SOCKET , +RET_PTR_TO_TCP_SOCK_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_TCP_SOCK , +RET_PTR_TO_SOCK_COMMON_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_SOCK_COMMON , +RET_PTR_TO_ALLOC_MEM_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_ALLOC_MEM , +RET_PTR_TO_BTF_ID_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_BTF_ID , +/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag. */
kernel/bpf/helpers.c +1 -1
@@ -667,7 +667,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)const struct bpf_func_proto bpf_per_cpu_ptr_proto = { .func = bpf_per_cpu_ptr, .gpl_only = false, - .ret_type =RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL , + .ret_type =RET_PTR_TO_MEM_OR_BTF_ID |PTR_MAYBE_NULL , .arg1_type =ARG_PTR_TO_PERCPU_BTF_ID , .arg2_type =ARG_ANYTHING , };
kernel/bpf/verifier.c +29 -24
117 lines not on this page · raw message
Signature · 1 line
2.34.0.384.gca35af8252-goog
kernel test robot
build botRe: [RFC PATCH bpf-next v2 3/9] bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/5af019e76ba5485e0b56b5b4607c9d2e30ca6138 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags…
Hi Hao,
[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/Introduce-composable-bpf-types/20211130-093143 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20211130/202111301041.Xvx6affN-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/5af019e76ba5485e0b56b5b4607c9d2e30ca6138
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Introduce-composable-bpf-types/20211130-093143 git checkout 5af019e76ba5485e0b56b5b4607c9d2e30ca6138
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash kernel/bpf/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
kernel/bpf/verifier.c: In function 'check_helper_call':
>> kernel/bpf/verifier.c:6597:39: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
6597 | verbose(env, "invalid return type %d of func %s#%d\n",
| ~^
| |
| int
| %ld
kernel/bpf/verifier.c:6608:38: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
6608 | verbose(env, "unknown return type %d of func %s#%d\n",
| ~^
| |
| int
| %ldvim +6597 kernel/bpf/verifier.c
9b99edcae5c80c Jiri Olsa 2021-07-14 6373
69c087ba6225b5 Yonghong Song 2021-02-26 6374 static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
69c087ba6225b5 Yonghong Song 2021-02-26 6375 int *insn_idx_p)
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6376 {
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6377 const struct bpf_func_proto *fn = NULL;
5af019e76ba548 Hao Luo 2021-11-29 6378 enum bpf_return_type ret_type;
638f5b90d46016 Alexei Starovoitov 2017-10-31 6379 struct bpf_reg_state *regs;
33ff9823c569f3 Daniel Borkmann 2016-04-13 6380 struct bpf_call_arg_meta meta;
69c087ba6225b5 Yonghong Song 2021-02-26 6381 int insn_idx = *insn_idx_p;
969bf05eb3cedd Alexei Starovoitov 2016-05-05 6382 bool changes_data;
69c087ba6225b5 Yonghong Song 2021-02-26 6383 int i, err, func_id;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6384
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6385 /* find function prototype */
69c087ba6225b5 Yonghong Song 2021-02-26 6386 func_id = insn->imm;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6387 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
61bd5218eef349 Jakub Kicinski 2017-10-09 6388 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
61bd5218eef349 Jakub Kicinski 2017-10-09 6389 func_id);
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6390 return -EINVAL;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6391 }
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6392
00176a34d9e27a Jakub Kicinski 2017-10-16 6393 if (env->ops->get_func_proto)
5e43f899b03a34 Andrey Ignatov 2018-03-30 6394 fn = env->ops->get_func_proto(func_id, env->prog);
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6395 if (!fn) {
61bd5218eef349 Jakub Kicinski 2017-10-09 6396 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
61bd5218eef349 Jakub Kicinski 2017-10-09 6397 func_id);
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6398 return -EINVAL;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6399 }
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6400
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6401 /* eBPF programs must be GPL compatible to use GPL-ed functions */
24701ecea76b0b Daniel Borkmann 2015-03-01 6402 if (!env->prog->gpl_compatible && fn->gpl_only) {
3fe2867cdf088f Daniel Borkmann 2018-06-02 6403 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6404 return -EINVAL;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6405 }
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6406
eae2e83e62633a Jiri Olsa 2020-08-25 6407 if (fn->allowed && !fn->allowed(env->prog)) {
eae2e83e62633a Jiri Olsa 2020-08-25 6408 verbose(env, "helper call is not allowed in probe\n");
eae2e83e62633a Jiri Olsa 2020-08-25 6409 return -EINVAL;
eae2e83e62633a Jiri Olsa 2020-08-25 6410 }
eae2e83e62633a Jiri Olsa 2020-08-25 6411
04514d13222f2c Daniel Borkmann 2017-12-14 6412 /* With LD_ABS/IND some JITs save/restore skb from r1. */
17bedab2723145 Martin KaFai Lau 2016-12-07 6413 changes_data = bpf_helper_changes_pkt_data(fn->func);
04514d13222f2c Daniel Borkmann 2017-12-14 6414 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
04514d13222f2c Daniel Borkmann 2017-12-14 6415 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
04514d13222f2c Daniel Borkmann 2017-12-14 6416 func_id_name(func_id), func_id);
04514d13222f2c Daniel Borkmann 2017-12-14 6417 return -EINVAL;
04514d13222f2c Daniel Borkmann 2017-12-14 6418 }
969bf05eb3cedd Alexei Starovoitov 2016-05-05 6419
33ff9823c569f3 Daniel Borkmann 2016-04-13 6420 memset(&meta, 0, sizeof(meta));
36bbef52c7eb64 Daniel Borkmann 2016-09-20 6421 meta.pkt_access = fn->pkt_access;
33ff9823c569f3 Daniel Borkmann 2016-04-13 6422
1b986589680a2a Martin KaFai Lau 2019-03-12 6423 err = check_func_proto(fn, func_id);
435faee1aae9c1 Daniel Borkmann 2016-04-13 6424 if (err) {
61bd5218eef349 Jakub Kicinski 2017-10-09 6425 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
ebb676daa1a340 Thomas Graf 2016-10-27 6426 func_id_name(func_id), func_id);
435faee1aae9c1 Daniel Borkmann 2016-04-13 6427 return err;
435faee1aae9c1 Daniel Borkmann 2016-04-13 6428 }
435faee1aae9c1 Daniel Borkmann 2016-04-13 6429
d83525ca62cf8e Alexei Starovoitov 2019-01-31 6430 meta.func_id = func_id;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6431 /* check args */
523a4cf491b3c9 Dmitrii Banshchikov 2021-02-26 6432 for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
af7ec13833619e Yonghong Song 2020-06-23 6433 err = check_func_arg(env, i, &meta, fn);
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6434 if (err)
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6435 return err;
a7658e1a4164ce Alexei Starovoitov 2019-10-15 6436 }
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6437
c93552c443ebc6 Daniel Borkmann 2018-05-24 6438 err = record_func_map(env, &meta, func_id, insn_idx);
c93552c443ebc6 Daniel Borkmann 2018-05-24 6439 if (err)
c93552c443ebc6 Daniel Borkmann 2018-05-24 6440 return err;
c93552c443ebc6 Daniel Borkmann 2018-05-24 6441
d2e4c1e6c29472 Daniel Borkmann 2019-11-22 6442 err = record_func_key(env, &meta, func_id, insn_idx);
d2e4c1e6c29472 Daniel Borkmann 2019-11-22 6443 if (err)
d2e4c1e6c29472 Daniel Borkmann 2019-11-22 6444 return err;
d2e4c1e6c29472 Daniel Borkmann 2019-11-22 6445
435faee1aae9c1 Daniel Borkmann 2016-04-13 6446 /* Mark slots with STACK_MISC in case of raw mode, stack offset
435faee1aae9c1 Daniel Borkmann 2016-04-13 6447 * is inferred from register state.
435faee1aae9c1 Daniel Borkmann 2016-04-13 6448 */
435faee1aae9c1 Daniel Borkmann 2016-04-13 6449 for (i = 0; i < meta.access_size; i++) {
ca36960211eb22 Daniel Borkmann 2018-02-23 6450 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
ca36960211eb22 Daniel Borkmann 2018-02-23 6451 BPF_WRITE, -1, false);
435faee1aae9c1 Daniel Borkmann 2016-04-13 6452 if (err)
435faee1aae9c1 Daniel Borkmann 2016-04-13 6453 return err;
435faee1aae9c1 Daniel Borkmann 2016-04-13 6454 }
435faee1aae9c1 Daniel Borkmann 2016-04-13 6455
fd978bf7fd3125 Joe Stringer 2018-10-02 6456 if (func_id == BPF_FUNC_tail_call) {
fd978bf7fd3125 Joe Stringer 2018-10-02 6457 err = check_reference_leak(env);
fd978bf7fd3125 Joe Stringer 2018-10-02 6458 if (err) {
fd978bf7fd3125 Joe Stringer 2018-10-02 6459 verbose(env, "tail_call would lead to reference leak\n");
fd978bf7fd3125 Joe Stringer 2018-10-02 6460 return err;
fd978bf7fd3125 Joe Stringer 2018-10-02 6461 }
fd978bf7fd3125 Joe Stringer 2018-10-02 6462 } else if (is_release_function(func_id)) {
1b986589680a2a Martin KaFai Lau 2019-03-12 6463 err = release_reference(env, meta.ref_obj_id);
46f8bc92758c62 Martin KaFai Lau 2019-02-09 6464 if (err) {
46f8bc92758c62 Martin KaFai Lau 2019-02-09 6465 verbose(env, "func %s#%d reference has not been acquired before\n",
46f8bc92758c62 Martin KaFai Lau 2019-02-09 6466 func_id_name(func_id), func_id);
fd978bf7fd3125 Joe Stringer 2018-10-02 6467 return err;
fd978bf7fd3125 Joe Stringer 2018-10-02 6468 }
46f8bc92758c62 Martin KaFai Lau 2019-02-09 6469 }
fd978bf7fd3125 Joe Stringer 2018-10-02 6470
638f5b90d46016 Alexei Starovoitov 2017-10-31 6471 regs = cur_regs(env);
cd339431765383 Roman Gushchin 2018-08-02 6472
cd339431765383 Roman Gushchin 2018-08-02 6473 /* check that flags argument in get_local_storage(map, flags) is 0,
cd339431765383 Roman Gushchin 2018-08-02 6474 * this is required because get_local_storage() can't return an error.
cd339431765383 Roman Gushchin 2018-08-02 6475 */
cd339431765383 Roman Gushchin 2018-08-02 6476 if (func_id == BPF_FUNC_get_local_storage &&
cd339431765383 Roman Gushchin 2018-08-02 6477 !register_is_null(®s[BPF_REG_2])) {
cd339431765383 Roman Gushchin 2018-08-02 6478 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
cd339431765383 Roman Gushchin 2018-08-02 6479 return -EINVAL;
cd339431765383 Roman Gushchin 2018-08-02 6480 }
cd339431765383 Roman Gushchin 2018-08-02 6481
69c087ba6225b5 Yonghong Song 2021-02-26 6482 if (func_id == BPF_FUNC_for_each_map_elem) {
69c087ba6225b5 Yonghong Song 2021-02-26 6483 err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
69c087ba6225b5 Yonghong Song 2021-02-26 6484 set_map_elem_callback_state);
69c087ba6225b5 Yonghong Song 2021-02-26 6485 if (err < 0)
69c087ba6225b5 Yonghong Song 2021-02-26 6486 return -EINVAL;
69c087ba6225b5 Yonghong Song 2021-02-26 6487 }
69c087ba6225b5 Yonghong Song 2021-02-26 6488
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6489 if (func_id == BPF_FUNC_timer_set_callback) {
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6490 err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6491 set_timer_callback_state);
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6492 if (err < 0)
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6493 return -EINVAL;
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6494 }
b00628b1c7d595 Alexei Starovoitov 2021-07-14 6495
7c7e3d31e7856a Song Liu 2021-11-05 6496 if (func_id == BPF_FUNC_find_vma) {
7c7e3d31e7856a Song Liu 2021-11-05 6497 err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
7c7e3d31e7856a Song Liu 2021-11-05 6498 set_find_vma_callback_state);
7c7e3d31e7856a Song Liu 2021-11-05 6499 if (err < 0)
7c7e3d31e7856a Song Liu 2021-11-05 6500 return -EINVAL;
7c7e3d31e7856a Song Liu 2021-11-05 6501 }
7c7e3d31e7856a Song Liu 2021-11-05 6502
7b15523a989b63 Florent Revest 2021-04-19 6503 if (func_id == BPF_FUNC_snprintf) {
7b15523a989b63 Florent Revest 2021-04-19 6504 err = check_bpf_snprintf_call(env, regs);
7b15523a989b63 Florent Revest 2021-04-19 6505 if (err < 0)
7b15523a989b63 Florent Revest 2021-04-19 6506 return err;
7b15523a989b63 Florent Revest 2021-04-19 6507 }
7b15523a989b63 Florent Revest 2021-04-19 6508
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6509 /* reset caller saved regs */
dc503a8ad98474 Edward Cree 2017-08-15 6510 for (i = 0; i < CALLER_SAVED_REGS; i++) {
61bd5218eef349 Jakub Kicinski 2017-10-09 6511 mark_reg_not_init(env, regs, caller_saved[i]);
dc503a8ad98474 Edward Cree 2017-08-15 6512 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
dc503a8ad98474 Edward Cree 2017-08-15 6513 }
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6514
5327ed3d44b754 Jiong Wang 2019-05-24 6515 /* helper call returns 64-bit value. */
5327ed3d44b754 Jiong Wang 2019-05-24 6516 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
5327ed3d44b754 Jiong Wang 2019-05-24 6517
dc503a8ad98474 Edward Cree 2017-08-15 6518 /* update return register (already marked as written above) */
5af019e76ba548 Hao Luo 2021-11-29 6519 ret_type = fn->ret_type;
5af019e76ba548 Hao Luo 2021-11-29 6520 if (ret_type == RET_INTEGER) {
f1174f77b50c94 Edward Cree 2017-08-07 6521 /* sets type to SCALAR_VALUE */
61bd5218eef349 Jakub Kicinski 2017-10-09 6522 mark_reg_unknown(env, regs, BPF_REG_0);
5af019e76ba548 Hao Luo 2021-11-29 6523 } else if (ret_type == RET_VOID) {
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6524 regs[BPF_REG_0].type = NOT_INIT;
5af019e76ba548 Hao Luo 2021-11-29 6525 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_MAP_VALUE) {
f1174f77b50c94 Edward Cree 2017-08-07 6526 /* There is no offset yet applied, variable or fixed */
61bd5218eef349 Jakub Kicinski 2017-10-09 6527 mark_reg_known_zero(env, regs, BPF_REG_0);
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6528 /* remember map_ptr, so that check_map_access()
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6529 * can check 'value_size' boundary of memory access
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6530 * to map element returned from bpf_map_lookup_elem()
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6531 */
33ff9823c569f3 Daniel Borkmann 2016-04-13 6532 if (meta.map_ptr == NULL) {
61bd5218eef349 Jakub Kicinski 2017-10-09 6533 verbose(env,
61bd5218eef349 Jakub Kicinski 2017-10-09 6534 "kernel subsystem misconfigured verifier\n");
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6535 return -EINVAL;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6536 }
33ff9823c569f3 Daniel Borkmann 2016-04-13 6537 regs[BPF_REG_0].map_ptr = meta.map_ptr;
3e8ce29850f183 Alexei Starovoitov 2021-07-14 6538 regs[BPF_REG_0].map_uid = meta.map_uid;
5af019e76ba548 Hao Luo 2021-11-29 6539 if (ret_type_may_be_null(fn->ret_type)) {
5af019e76ba548 Hao Luo 2021-11-29 6540 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
5af019e76ba548 Hao Luo 2021-11-29 6541 } else {
4d31f30148cea6 Daniel Borkmann 2018-11-01 6542 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
e16d2f1ab96849 Alexei Starovoitov 2019-01-31 6543 if (map_value_has_spin_lock(meta.map_ptr))
e16d2f1ab96849 Alexei Starovoitov 2019-01-31 6544 regs[BPF_REG_0].id = ++env->id_gen;
4d31f30148cea6 Daniel Borkmann 2018-11-01 6545 }
5af019e76ba548 Hao Luo 2021-11-29 6546 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_SOCKET) {
46f8bc92758c62 Martin KaFai Lau 2019-02-09 6547 mark_reg_known_zero(env, regs, BPF_REG_0);
46f8bc92758c62 Martin KaFai Lau 2019-02-09 6548 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
5af019e76ba548 Hao Luo 2021-11-29 6549 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_SOCK_COMMON) {
85a51f8c28b981 Lorenz Bauer 2019-03-22 6550 mark_reg_known_zero(env, regs, BPF_REG_0);
85a51f8c28b981 Lorenz Bauer 2019-03-22 6551 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
5af019e76ba548 Hao Luo 2021-11-29 6552 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_TCP_SOCK) {
655a51e536c09d Martin KaFai Lau 2019-02-09 6553 mark_reg_known_zero(env, regs, BPF_REG_0);
655a51e536c09d Martin KaFai Lau 2019-02-09 6554 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
5af019e76ba548 Hao Luo 2021-11-29 6555 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_ALLOC_MEM) {
457f44363a8894 Andrii Nakryiko 2020-05-29 6556 mark_reg_known_zero(env, regs, BPF_REG_0);
457f44363a8894 Andrii Nakryiko 2020-05-29 6557 regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
457f44363a8894 Andrii Nakryiko 2020-05-29 6558 regs[BPF_REG_0].mem_size = meta.mem_size;
5af019e76ba548 Hao Luo 2021-11-29 6559 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) {
eaa6bcb71ef6ed Hao Luo 2020-09-29 6560 const struct btf_type *t;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6561
eaa6bcb71ef6ed Hao Luo 2020-09-29 6562 mark_reg_known_zero(env, regs, BPF_REG_0);
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6563 t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL);
eaa6bcb71ef6ed Hao Luo 2020-09-29 6564 if (!btf_type_is_struct(t)) {
eaa6bcb71ef6ed Hao Luo 2020-09-29 6565 u32 tsize;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6566 const struct btf_type *ret;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6567 const char *tname;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6568
eaa6bcb71ef6ed Hao Luo 2020-09-29 6569 /* resolve the type size of ksym. */
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6570 ret = btf_resolve_size(meta.ret_btf, t, &tsize);
eaa6bcb71ef6ed Hao Luo 2020-09-29 6571 if (IS_ERR(ret)) {
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6572 tname = btf_name_by_offset(meta.ret_btf, t->name_off);
eaa6bcb71ef6ed Hao Luo 2020-09-29 6573 verbose(env, "unable to resolve the size of type '%s': %ld\n",
eaa6bcb71ef6ed Hao Luo 2020-09-29 6574 tname, PTR_ERR(ret));
eaa6bcb71ef6ed Hao Luo 2020-09-29 6575 return -EINVAL;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6576 }
63d9b80dcf2c67 Hao Luo 2020-09-29 6577 regs[BPF_REG_0].type =
5af019e76ba548 Hao Luo 2021-11-29 6578 (ret_type & PTR_MAYBE_NULL) ?
5af019e76ba548 Hao Luo 2021-11-29 6579 PTR_TO_MEM_OR_NULL : PTR_TO_MEM;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6580 regs[BPF_REG_0].mem_size = tsize;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6581 } else {
63d9b80dcf2c67 Hao Luo 2020-09-29 6582 regs[BPF_REG_0].type =
5af019e76ba548 Hao Luo 2021-11-29 6583 (ret_type & PTR_MAYBE_NULL) ?
5af019e76ba548 Hao Luo 2021-11-29 6584 PTR_TO_BTF_ID_OR_NULL : PTR_TO_BTF_ID;
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6585 regs[BPF_REG_0].btf = meta.ret_btf;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6586 regs[BPF_REG_0].btf_id = meta.ret_btf_id;
eaa6bcb71ef6ed Hao Luo 2020-09-29 6587 }
5af019e76ba548 Hao Luo 2021-11-29 6588 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_BTF_ID) {
af7ec13833619e Yonghong Song 2020-06-23 6589 int ret_btf_id;
af7ec13833619e Yonghong Song 2020-06-23 6590
af7ec13833619e Yonghong Song 2020-06-23 6591 mark_reg_known_zero(env, regs, BPF_REG_0);
5af019e76ba548 Hao Luo 2021-11-29 6592 regs[BPF_REG_0].type = (ret_type & PTR_MAYBE_NULL) ?
5af019e76ba548 Hao Luo 2021-11-29 6593 PTR_TO_BTF_ID_OR_NULL :
5af019e76ba548 Hao Luo 2021-11-29 6594 PTR_TO_BTF_ID;
af7ec13833619e Yonghong Song 2020-06-23 6595 ret_btf_id = *fn->ret_btf_id;
af7ec13833619e Yonghong Song 2020-06-23 6596 if (ret_btf_id == 0) {
af7ec13833619e Yonghong Song 2020-06-23 @6597 verbose(env, "invalid return type %d of func %s#%d\n",
5af019e76ba548 Hao Luo 2021-11-29 6598 BPF_BASE_TYPE(ret_type), func_id_name(func_id),
5af019e76ba548 Hao Luo 2021-11-29 6599 func_id);
af7ec13833619e Yonghong Song 2020-06-23 6600 return -EINVAL;
af7ec13833619e Yonghong Song 2020-06-23 6601 }
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6602 /* current BPF helper definitions are only coming from
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6603 * built-in code with type IDs from vmlinux BTF
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6604 */
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 6605 regs[BPF_REG_0].btf = btf_vmlinux;
af7ec13833619e Yonghong Song 2020-06-23 6606 regs[BPF_REG_0].btf_id = ret_btf_id;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6607 } else {
61bd5218eef349 Jakub Kicinski 2017-10-09 6608 verbose(env, "unknown return type %d of func %s#%d\n",
5af019e76ba548 Hao Luo 2021-11-29 6609 BPF_BASE_TYPE(ret_type), func_id_name(func_id), func_id);
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6610 return -EINVAL;
17a5267067f3c3 Alexei Starovoitov 2014-09-26 6611 }
04fd61ab36ec06 Alexei Starovoitov 2015-05-19 6612
93c230e3f5bd6e Martin KaFai Lau 2020-10-19 6613 if (reg_type_may_be_null(regs[BPF_REG_0].type))
93c230e3f5bd6e Martin KaFai Lau 2020-10-19 6614 regs[BPF_REG_0].id = ++env->id_gen;
93c230e3f5bd6e Martin KaFai Lau 2020-10-19 6615
0f3adc288df8ba Lorenz Bauer 2019-03-22 6616 if (is_ptr_cast_function(func_id)) {
1b986589680a2a Martin KaFai Lau 2019-03-12 6617 /* For release_reference() */
1b986589680a2a Martin KaFai Lau 2019-03-12 6618 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
64d85290d79c06 Jakub Sitnicki 2020-04-29 6619 } else if (is_acquire_function(func_id, meta.map_ptr)) {
0f3adc288df8ba Lorenz Bauer 2019-03-22 6620 int id = acquire_reference_state(env, insn_idx);
0f3adc288df8ba Lorenz Bauer 2019-03-22 6621
0f3adc288df8ba Lorenz Bauer 2019-03-22 6622 if (id < 0)
0f3adc288df8ba Lorenz Bauer 2019-03-22 6623 return id;
0f3adc288df8ba Lorenz Bauer 2019-03-22 6624 /* For mark_ptr_or_null_reg() */
0f3adc288df8ba Lorenz Bauer 2019-03-22 6625 regs[BPF_REG_0].id = id;
0f3adc288df8ba Lorenz Bauer 2019-03-22 6626 /* For release_reference() */
0f3adc288df8ba Lorenz Bauer 2019-03-22 6627 regs[BPF_REG_0].ref_obj_id = id;
0f3adc288df8ba Lorenz Bauer 2019-03-22 6628 }
1b986589680a2a Martin KaFai Lau 2019-03-12 6629
849fa50662fbc8 Yonghong Song 2018-04-28 6630 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
849fa50662fbc8 Yonghong Song 2018-04-28 6631
61bd5218eef349 Jakub Kicinski 2017-10-09 6632 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
35578d79840030 Kaixu Xia 2015-08-06 6633 if (err)
35578d79840030 Kaixu Xia 2015-08-06 6634 return err;
04fd61ab36ec06 Alexei Starovoitov 2015-05-19 6635
fa28dcb82a38f8 Song Liu 2020-06-29 6636 if ((func_id == BPF_FUNC_get_stack ||
fa28dcb82a38f8 Song Liu 2020-06-29 6637 func_id == BPF_FUNC_get_task_stack) &&
fa28dcb82a38f8 Song Liu 2020-06-29 6638 !env->prog->has_callchain_buf) {
c195651e565ae7 Yonghong Song 2018-04-28 6639 const char *err_str;
c195651e565ae7 Yonghong Song 2018-04-28 6640 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [RFC PATCH bpf-next v2 3/9] bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
In reply to Hao Luoreproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install arm cross compiling tool for…
Hi Hao,
[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/Introduce-composable-bpf-types/20211130-093143 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: arm-buildonly-randconfig-r005-20211128 (https://download.01.org/0day-ci/archive/20211130/202111301101.rEYY4B1t-lkp(a)intel.com/config) compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 25eb7fa01d7ebbe67648ea03841cda55b4239ab2)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/5af019e76ba5485e0b56b5b4607c9d2e30ca6138
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Introduce-composable-bpf-types/20211130-093143
git checkout 5af019e76ba5485e0b56b5b4607c9d2e30ca6138
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash kernel/bpf/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/bpf/verifier.c:6598:5: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
BPF_BASE_TYPE(ret_type), func_id_name(func_id),
^~~~~~~~~~~~~~~~~~~~~~~
include/linux/bpf.h:326:26: note: expanded from macro 'BPF_BASE_TYPE'
#define BPF_BASE_TYPE(x) ((x) & BPF_BASE_TYPE_MASK)
^~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/verifier.c:6609:4: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
BPF_BASE_TYPE(ret_type), func_id_name(func_id), func_id);
^~~~~~~~~~~~~~~~~~~~~~~
include/linux/bpf.h:326:26: note: expanded from macro 'BPF_BASE_TYPE'
#define BPF_BASE_TYPE(x) ((x) & BPF_BASE_TYPE_MASK)
^~~~~~~~~~~~~~~~~~~~~~~~~~2 warnings generated.
vim +6598 kernel/bpf/verifier.c
6373
6374 static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
6375 int *insn_idx_p)
6376 {
6377 const struct bpf_func_proto *fn = NULL;
6378 enum bpf_return_type ret_type;
6379 struct bpf_reg_state *regs;
6380 struct bpf_call_arg_meta meta;
6381 int insn_idx = *insn_idx_p;
6382 bool changes_data;
6383 int i, err, func_id;
6384
6385 /* find function prototype */
6386 func_id = insn->imm;
6387 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
6388 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
6389 func_id);
6390 return -EINVAL;
6391 }
6392
6393 if (env->ops->get_func_proto)
6394 fn = env->ops->get_func_proto(func_id, env->prog);
6395 if (!fn) {
6396 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
6397 func_id);
6398 return -EINVAL;
6399 }
6400
6401 /* eBPF programs must be GPL compatible to use GPL-ed functions */
6402 if (!env->prog->gpl_compatible && fn->gpl_only) {
6403 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
6404 return -EINVAL;
6405 }
6406
6407 if (fn->allowed && !fn->allowed(env->prog)) {
6408 verbose(env, "helper call is not allowed in probe\n");
6409 return -EINVAL;
6410 }
6411
6412 /* With LD_ABS/IND some JITs save/restore skb from r1. */
6413 changes_data = bpf_helper_changes_pkt_data(fn->func);
6414 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
6415 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
6416 func_id_name(func_id), func_id);
6417 return -EINVAL;
6418 }
6419
6420 memset(&meta, 0, sizeof(meta));
6421 meta.pkt_access = fn->pkt_access;
6422
6423 err = check_func_proto(fn, func_id);
6424 if (err) {
6425 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
6426 func_id_name(func_id), func_id);
6427 return err;
6428 }
6429
6430 meta.func_id = func_id;
6431 /* check args */
6432 for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
6433 err = check_func_arg(env, i, &meta, fn);
6434 if (err)
6435 return err;
6436 }
6437
6438 err = record_func_map(env, &meta, func_id, insn_idx);
6439 if (err)
6440 return err;
6441
6442 err = record_func_key(env, &meta, func_id, insn_idx);
6443 if (err)
6444 return err;
6445
6446 /* Mark slots with STACK_MISC in case of raw mode, stack offset
6447 * is inferred from register state.
6448 */
6449 for (i = 0; i < meta.access_size; i++) {
6450 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
6451 BPF_WRITE, -1, false);
6452 if (err)
6453 return err;
6454 }
6455
6456 if (func_id == BPF_FUNC_tail_call) {
6457 err = check_reference_leak(env);
6458 if (err) {
6459 verbose(env, "tail_call would lead to reference leak\n");
6460 return err;
6461 }
6462 } else if (is_release_function(func_id)) {
6463 err = release_reference(env, meta.ref_obj_id);
6464 if (err) {
6465 verbose(env, "func %s#%d reference has not been acquired before\n",
6466 func_id_name(func_id), func_id);
6467 return err;
6468 }
6469 }
6470
6471 regs = cur_regs(env);
6472
6473 /* check that flags argument in get_local_storage(map, flags) is 0,
6474 * this is required because get_local_storage() can't return an error.
6475 */
6476 if (func_id == BPF_FUNC_get_local_storage &&
6477 !register_is_null(®s[BPF_REG_2])) {
6478 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
6479 return -EINVAL;
6480 }
6481
6482 if (func_id == BPF_FUNC_for_each_map_elem) {
6483 err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
6484 set_map_elem_callback_state);
6485 if (err < 0)
6486 return -EINVAL;
6487 }
6488
6489 if (func_id == BPF_FUNC_timer_set_callback) {
6490 err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
6491 set_timer_callback_state);
6492 if (err < 0)
6493 return -EINVAL;
6494 }
6495
6496 if (func_id == BPF_FUNC_find_vma) {
6497 err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
6498 set_find_vma_callback_state);
6499 if (err < 0)
6500 return -EINVAL;
6501 }
6502
6503 if (func_id == BPF_FUNC_snprintf) {
6504 err = check_bpf_snprintf_call(env, regs);
6505 if (err < 0)
6506 return err;
6507 }
6508
6509 /* reset caller saved regs */
6510 for (i = 0; i < CALLER_SAVED_REGS; i++) {
6511 mark_reg_not_init(env, regs, caller_saved[i]);
6512 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
6513 }
6514
6515 /* helper call returns 64-bit value. */
6516 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
6517
6518 /* update return register (already marked as written above) */
6519 ret_type = fn->ret_type;
6520 if (ret_type == RET_INTEGER) {
6521 /* sets type to SCALAR_VALUE */
6522 mark_reg_unknown(env, regs, BPF_REG_0);
6523 } else if (ret_type == RET_VOID) {
6524 regs[BPF_REG_0].type = NOT_INIT;
6525 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_MAP_VALUE) {
6526 /* There is no offset yet applied, variable or fixed */
6527 mark_reg_known_zero(env, regs, BPF_REG_0);
6528 /* remember map_ptr, so that check_map_access()
6529 * can check 'value_size' boundary of memory access
6530 * to map element returned from bpf_map_lookup_elem()
6531 */
6532 if (meta.map_ptr == NULL) {
6533 verbose(env,
6534 "kernel subsystem misconfigured verifier\n");
6535 return -EINVAL;
6536 }
6537 regs[BPF_REG_0].map_ptr = meta.map_ptr;
6538 regs[BPF_REG_0].map_uid = meta.map_uid;
6539 if (ret_type_may_be_null(fn->ret_type)) {
6540 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
6541 } else {
6542 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
6543 if (map_value_has_spin_lock(meta.map_ptr))
6544 regs[BPF_REG_0].id = ++env->id_gen;
6545 }
6546 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_SOCKET) {
6547 mark_reg_known_zero(env, regs, BPF_REG_0);
6548 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
6549 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_SOCK_COMMON) {
6550 mark_reg_known_zero(env, regs, BPF_REG_0);
6551 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
6552 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_TCP_SOCK) {
6553 mark_reg_known_zero(env, regs, BPF_REG_0);
6554 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
6555 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_ALLOC_MEM) {
6556 mark_reg_known_zero(env, regs, BPF_REG_0);
6557 regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
6558 regs[BPF_REG_0].mem_size = meta.mem_size;
6559 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) {
6560 const struct btf_type *t;
6561
6562 mark_reg_known_zero(env, regs, BPF_REG_0);
6563 t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL);
6564 if (!btf_type_is_struct(t)) {
6565 u32 tsize;
6566 const struct btf_type *ret;
6567 const char *tname;
6568
6569 /* resolve the type size of ksym. */
6570 ret = btf_resolve_size(meta.ret_btf, t, &tsize);
6571 if (IS_ERR(ret)) {
6572 tname = btf_name_by_offset(meta.ret_btf, t->name_off);
6573 verbose(env, "unable to resolve the size of type '%s': %ld\n",
6574 tname, PTR_ERR(ret));
6575 return -EINVAL;
6576 }
6577 regs[BPF_REG_0].type =
6578 (ret_type & PTR_MAYBE_NULL) ?
6579 PTR_TO_MEM_OR_NULL : PTR_TO_MEM;
6580 regs[BPF_REG_0].mem_size = tsize;
6581 } else {
6582 regs[BPF_REG_0].type =
6583 (ret_type & PTR_MAYBE_NULL) ?
6584 PTR_TO_BTF_ID_OR_NULL : PTR_TO_BTF_ID;
6585 regs[BPF_REG_0].btf = meta.ret_btf;
6586 regs[BPF_REG_0].btf_id = meta.ret_btf_id;
6587 }
6588 } else if (BPF_BASE_TYPE(ret_type) == RET_PTR_TO_BTF_ID) {
6589 int ret_btf_id;
6590
6591 mark_reg_known_zero(env, regs, BPF_REG_0);
6592 regs[BPF_REG_0].type = (ret_type & PTR_MAYBE_NULL) ?
6593 PTR_TO_BTF_ID_OR_NULL :
6594 PTR_TO_BTF_ID;
6595 ret_btf_id = *fn->ret_btf_id;
6596 if (ret_btf_id == 0) {
6597 verbose(env, "invalid return type %d of func %s#%d\n",
> 6598 BPF_BASE_TYPE(ret_type), func_id_name(func_id),
6599 func_id);
6600 return -EINVAL;
6601 }
6602 /* current BPF helper definitions are only coming from
6603 * built-in code with type IDs from vmlinux BTF
6604 */
6605 regs[BPF_REG_0].btf = btf_vmlinux;
6606 regs[BPF_REG_0].btf_id = ret_btf_id;
6607 } else {
6608 verbose(env, "unknown return type %d of func %s#%d\n",
6609 BPF_BASE_TYPE(ret_type), func_id_name(func_id), func_id);
6610 return -EINVAL;
6611 }
6612
6613 if (reg_type_may_be_null(regs[BPF_REG_0].type))
6614 regs[BPF_REG_0].id = ++env->id_gen;
6615
6616 if (is_ptr_cast_function(func_id)) {
6617 /* For release_reference() */
6618 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
6619 } else if (is_acquire_function(func_id, meta.map_ptr)) {
6620 int id = acquire_reference_state(env, insn_idx);
6621
6622 if (id < 0)
6623 return id;
6624 /* For mark_ptr_or_null_reg() */
6625 regs[BPF_REG_0].id = id;
6626 /* For release_reference() */
6627 regs[BPF_REG_0].ref_obj_id = id;
6628 }
6629
6630 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
6631
6632 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
6633 if (err)
6634 return err;
6635
6636 if ((func_id == BPF_FUNC_get_stack ||
6637 func_id == BPF_FUNC_get_task_stack) &&
6638 !env->prog->has_callchain_buf) {
6639 const char *err_str;
6640
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [RFC PATCH bpf-next v2 3/9] bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
In reply to Hao LuoQuoted 16 lines · Hao Luo
kernel/bpf/verifier.c +4
-4/* update return register (already marked as written above) */ -if (fn->ret_type ==RET_INTEGER ) { + ret_type = fn->ret_type; +if (ret_type ==RET_INTEGER ) {/* sets type to SCALAR_VALUE */ mark_reg_unknown (env, regs,BPF_REG_0 ); - }else if (fn->ret_type ==RET_VOID ) { + }else if (ret_type ==RET_VOID ) { regs[BPF_REG_0 ].type =NOT_INIT ; - }else if (fn->ret_type ==RET_PTR_TO_MAP_VALUE_OR_NULL || - fn->ret_type ==RET_PTR_TO_MAP_VALUE ) { + }else if (BPF_BASE_TYPE (ret_type) ==RET_PTR_TO_MAP_VALUE ) {/* There is no offset yet applied, variable or fixed */ mark_reg_known_zero (env, regs,BPF_REG_0 );/* remember map_ptr, so that check_map_access()
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6539
Quoted 5 lines
kernel/bpf/verifier.c +1
-1@@ -6530,28 +6536,27 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn } regs[BPF_REG_0 ].map_ptr = meta.map_ptr; regs[BPF_REG_0 ].map_uid = meta.map_uid; -if (fn->ret_type ==RET_PTR_TO_MAP_VALUE ) { +if (ret_type_may_be_null (fn->ret_type)) {
it should have been ret_type here?
Quoted 3 lines
kernel/bpf/verifier.c +2
-0+ regs[BPF_REG_0 ].type =PTR_TO_MAP_VALUE_OR_NULL ; + }else { regs[BPF_REG_0 ].type =PTR_TO_MAP_VALUE ;
Re: [RFC PATCH bpf-next v2 3/9] bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Hao Luo
/* update return register (already marked as written above) */kernel/bpf/verifier.c +4
-4-if (fn->ret_type ==RET_INTEGER ) { + ret_type = fn->ret_type; +if (ret_type ==RET_INTEGER ) {/* sets type to SCALAR_VALUE */ mark_reg_unknown (env, regs,BPF_REG_0 ); - }else if (fn->ret_type ==RET_VOID ) { + }else if (ret_type ==RET_VOID ) { regs[BPF_REG_0 ].type =NOT_INIT ; - }else if (fn->ret_type ==RET_PTR_TO_MAP_VALUE_OR_NULL || - fn->ret_type ==RET_PTR_TO_MAP_VALUE ) { + }else if (BPF_BASE_TYPE (ret_type) ==RET_PTR_TO_MAP_VALUE ) {/* There is no offset yet applied, variable or fixed */ mark_reg_known_zero (env, regs,BPF_REG_0 );/* remember map_ptr, so that check_map_access()
Quoted 9 lines
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6539
Quoted 6 lines
kernel/bpf/verifier.c +1
-1@@ -6530,28 +6536,27 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn } regs[BPF_REG_0 ].map_ptr = meta.map_ptr; regs[BPF_REG_0 ].map_uid = meta.map_uid; -if (fn->ret_type ==RET_PTR_TO_MAP_VALUE ) { +if (ret_type_may_be_null (fn->ret_type)) {it should have been
ret_typehere?
Yes. I think fn->ret_type and ret_type are the same here. I can replace it with 'ret_type' in the next version.
Quoted 3 lines
Quoted 3 lines
kernel/bpf/verifier.c +2
-0+ regs[BPF_REG_0 ].type =PTR_TO_MAP_VALUE_OR_NULL ; + }else { regs[BPF_REG_0 ].type =PTR_TO_MAP_VALUE ;
RFC PATCH v2 4/9 bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
In reply to Hao LuoWe have introduced a new type to make bpf_reg composable, by introducing flags to tag a particular type.
One of the flag is PTR_MAYBE_NULL, which indicates a pointer may be NULL. This patch switches the qualified reg_types to use this flag. The reg_types changed in this patch include:
1. PTR_TO_MAP_VALUE_OR_NULL
2. PTR_TO_SOCKET_OR_NULL
3. PTR_TO_SOCK_COMMON_OR_NULL
4. PTR_TO_TCP_SOCK_OR_NULL
5. PTR_TO_BTF_ID_OR_NULL
6. PTR_TO_MEM_OR_NULL
7. PTR_TO_RDONLY_BUF_OR_NULL
8. PTR_TO_RDWR_BUF_OR_NULL
--- include/linux/bpf.h | 16 +-- kernel/bpf/btf.c | 8 +- kernel/bpf/map_iter.c | 4 +- kernel/bpf/verifier.c | 265 +++++++++++++++----------------------- net/core/bpf_sk_storage.c | 2 +- net/core/sock_map.c | 2 +- 6 files changed, 121 insertions(+), 176 deletions(-)
include/linux/bpf.h +8 -8
@@ -473,18 +473,14 @@ enum bpf_reg_type {PTR_TO_CTX ,/* reg points to bpf_context */ CONST_PTR_TO_MAP ,/* reg points to struct bpf_map */ PTR_TO_MAP_VALUE ,/* reg points to map element value */ -PTR_TO_MAP_VALUE_OR_NULL ,/* points to map elem value or NULL */ PTR_TO_STACK ,/* reg == frame_pointer + offset */ PTR_TO_PACKET_META ,/* skb->data - meta_len */ PTR_TO_PACKET ,/* reg points to skb->data */ PTR_TO_PACKET_END ,/* skb->data + headlen */ PTR_TO_FLOW_KEYS ,/* reg points to bpf_flow_keys */ PTR_TO_SOCKET ,/* reg points to struct bpf_sock */ -PTR_TO_SOCKET_OR_NULL ,/* reg points to struct bpf_sock or NULL */ PTR_TO_SOCK_COMMON ,/* reg points to sock_common */ -PTR_TO_SOCK_COMMON_OR_NULL ,/* reg points to sock_common or NULL */ PTR_TO_TCP_SOCK ,/* reg points to struct tcp_sock */ -PTR_TO_TCP_SOCK_OR_NULL ,/* reg points to struct tcp_sock or NULL */ PTR_TO_TP_BUFFER ,/* reg points to a writable raw tp's buffer */ PTR_TO_XDP_SOCK ,/* reg points to struct xdp_sock */ /* PTR_TO_BTF_ID points to a kernel struct that does not need
@@ -502,18 +498,22 @@ enum bpf_reg_type {* been checked for null. Used primarily to inform the verifier * an explicit null check is required for this struct. */ -PTR_TO_BTF_ID_OR_NULL ,PTR_TO_MEM ,/* reg points to valid memory region */ -PTR_TO_MEM_OR_NULL ,/* reg points to valid memory region or NULL */ PTR_TO_RDONLY_BUF ,/* reg points to a readonly buffer */ -PTR_TO_RDONLY_BUF_OR_NULL ,/* reg points to a readonly buffer or NULL */ PTR_TO_RDWR_BUF ,/* reg points to a read/write buffer */ -PTR_TO_RDWR_BUF_OR_NULL ,/* reg points to a read/write buffer or NULL */ PTR_TO_PERCPU_BTF_ID ,/* reg points to a percpu kernel variable */ PTR_TO_FUNC ,/* reg points to a bpf program function */ PTR_TO_MAP_KEY ,/* reg points to a map element key */ __BPF_REG_TYPE_MAX, +/* Extended reg_types. */ +PTR_TO_MAP_VALUE_OR_NULL =PTR_MAYBE_NULL |PTR_TO_MAP_VALUE , +PTR_TO_SOCKET_OR_NULL =PTR_MAYBE_NULL |PTR_TO_SOCKET , +PTR_TO_SOCK_COMMON_OR_NULL =PTR_MAYBE_NULL |PTR_TO_SOCK_COMMON , +PTR_TO_TCP_SOCK_OR_NULL =PTR_MAYBE_NULL |PTR_TO_TCP_SOCK , +PTR_TO_BTF_ID_OR_NULL =PTR_MAYBE_NULL |PTR_TO_BTF_ID , +PTR_TO_MEM_OR_NULL =PTR_MAYBE_NULL |PTR_TO_MEM , +/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag. */
kernel/bpf/btf.c +6 -2
@@ -4941,10 +4941,14 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */ for (i =0 ; i < prog->aux->ctx_arg_info_size; i++) {const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; +enum bpf_reg_type type; +enum bpf_type_flag flag; + type =BPF_BASE_TYPE (ctx_arg_info->reg_type); + flag =BPF_TYPE_FLAG (ctx_arg_info->reg_type);if (ctx_arg_info->offset == off && - (ctx_arg_info->reg_type ==PTR_TO_RDONLY_BUF_OR_NULL || - ctx_arg_info->reg_type ==PTR_TO_RDWR_BUF_OR_NULL )) { + (type ==PTR_TO_RDWR_BUF || type ==PTR_TO_RDONLY_BUF ) && + (flag &PTR_MAYBE_NULL )) { info->reg_type = ctx_arg_info->reg_type;return true; }
kernel/bpf/map_iter.c +2 -2
@@ -174,9 +174,9 @@ static const struct bpf_iter_reg bpf_map_elem_reg_info = { .ctx_arg_info_size = 2, .ctx_arg_info = { { offsetof(struct bpf_iter__bpf_map_elem, key), - PTR_TO_RDONLY_BUF_OR_NULL }, + PTR_TO_RDONLY_BUF | PTR_MAYBE_NULL }, { offsetof(struct bpf_iter__bpf_map_elem, value), - PTR_TO_RDWR_BUF_OR_NULL }, + PTR_TO_RDWR_BUF | PTR_MAYBE_NULL }, }, };
kernel/bpf/verifier.c +103 -162
544 lines not on this page · raw message
net/core/bpf_sk_storage.c +1 -1
@@ -929,7 +929,7 @@ static struct bpf_iter_reg bpf_sk_storage_map_reg_info = { { offsetof(struct bpf_iter__bpf_sk_storage_map , sk), PTR_TO_BTF_ID_OR_NULL }, { offsetof(struct bpf_iter__bpf_sk_storage_map, value), - PTR_TO_RDWR_BUF_OR_NULL }, + PTR_TO_RDWR_BUF | PTR_MAYBE_NULL }, }, .seq_info = &iter_seq_info, };
net/core/sock_map.c +1 -1
@@ -1559,7 +1559,7 @@ static struct bpf_iter_reg sock_map_iter_reg = { .ctx_arg_info_size = 2, .ctx_arg_info = { { offsetof(struct bpf_iter__sockmap, key), - PTR_TO_RDONLY_BUF_OR_NULL }, + PTR_TO_RDONLY_BUF | PTR_MAYBE_NULL }, { offsetof(struct bpf_iter__sockmap, sk), PTR_TO_BTF_ID_OR_NULL }, },
Signature · 1 line
2.34.0.384.gca35af8252-goog
kernel test robot
build botRe: [RFC PATCH bpf-next v2 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/9e92c0a723fc173ac102b3bb27df479a01f32896 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags…
Hi Hao,
[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/Introduce-composable-bpf-types/20211130-093143 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20211130/202111301155.7gf5aKBg-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/9e92c0a723fc173ac102b3bb27df479a01f32896
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Introduce-composable-bpf-types/20211130-093143 git checkout 9e92c0a723fc173ac102b3bb27df479a01f32896
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash kernel/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/bpf/verifier.c:533:8: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
533 | static const char * const reg_type_str(enum bpf_reg_type type)
| ^~~~~
kernel/bpf/verifier.c: In function 'check_helper_call':
kernel/bpf/verifier.c:6544:39: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
6544 | verbose(env, "invalid return type %d of func %s#%d\n",
| ~^
| |
| int
| %ld
kernel/bpf/verifier.c:6555:38: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
6555 | verbose(env, "unknown return type %d of func %s#%d\n",
| ~^
| |
| int
| %ldvim +533 kernel/bpf/verifier.c
531
532 /* string representation of 'enum bpf_reg_type' */
> 533 static const char * const reg_type_str(enum bpf_reg_type type)
534 {
535 static const char * const str[] = {
536 [NOT_INIT] = "?",
537 [SCALAR_VALUE] = "inv",
538 [PTR_TO_CTX] = "ctx",
539 [CONST_PTR_TO_MAP] = "map_ptr",
540 [PTR_TO_MAP_VALUE] = "map_value",
541 [PTR_TO_STACK] = "fp",
542 [PTR_TO_PACKET] = "pkt",
543 [PTR_TO_PACKET_META] = "pkt_meta",
544 [PTR_TO_PACKET_END] = "pkt_end",
545 [PTR_TO_FLOW_KEYS] = "flow_keys",
546 [PTR_TO_SOCKET] = "sock",
547 [PTR_TO_SOCK_COMMON] = "sock_common",
548 [PTR_TO_TCP_SOCK] = "tcp_sock",
549 [PTR_TO_TP_BUFFER] = "tp_buffer",
550 [PTR_TO_XDP_SOCK] = "xdp_sock",
551 [PTR_TO_BTF_ID] = "ptr_",
552 [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_",
553 [PTR_TO_MEM] = "mem",
554 [PTR_TO_RDONLY_BUF] = "rdonly_buf",
555 [PTR_TO_RDWR_BUF] = "rdwr_buf",
556 [PTR_TO_FUNC] = "func",
557 [PTR_TO_MAP_KEY] = "map_key",
558 };
559
560 return str[BPF_BASE_TYPE(type)];
561 }
562
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [RFC PATCH bpf-next v2 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
In reply to Hao Luoreproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install arm cross compiling tool for…
Hi Hao,
[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/Introduce-composable-bpf-types/20211130-093143 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: arm-buildonly-randconfig-r005-20211128 (https://download.01.org/0day-ci/archive/20211130/202111301214.JYWypTyw-lkp(a)intel.com/config) compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 25eb7fa01d7ebbe67648ea03841cda55b4239ab2)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/9e92c0a723fc173ac102b3bb27df479a01f32896
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Introduce-composable-bpf-types/20211130-093143
git checkout 9e92c0a723fc173ac102b3bb27df479a01f32896
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash kernel/bpf/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/bpf/verifier.c:533:21: warning: 'const' type qualifier on return type has no effect [-Wignored-qualifiers]
static const char * const reg_type_str(enum bpf_reg_type type)
^~~~~~
kernel/bpf/verifier.c:6545:5: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
BPF_BASE_TYPE(ret_type), func_id_name(func_id),
^~~~~~~~~~~~~~~~~~~~~~~
include/linux/bpf.h:326:26: note: expanded from macro 'BPF_BASE_TYPE'
#define BPF_BASE_TYPE(x) ((x) & BPF_BASE_TYPE_MASK)
^~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/verifier.c:6556:4: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
BPF_BASE_TYPE(ret_type), func_id_name(func_id), func_id);
^~~~~~~~~~~~~~~~~~~~~~~
include/linux/bpf.h:326:26: note: expanded from macro 'BPF_BASE_TYPE'
#define BPF_BASE_TYPE(x) ((x) & BPF_BASE_TYPE_MASK)
^~~~~~~~~~~~~~~~~~~~~~~~~~3 warnings generated.
vim +/const +533 kernel/bpf/verifier.c
531
532 /* string representation of 'enum bpf_reg_type' */
> 533 static const char * const reg_type_str(enum bpf_reg_type type)
534 {
535 static const char * const str[] = {
536 [NOT_INIT] = "?",
537 [SCALAR_VALUE] = "inv",
538 [PTR_TO_CTX] = "ctx",
539 [CONST_PTR_TO_MAP] = "map_ptr",
540 [PTR_TO_MAP_VALUE] = "map_value",
541 [PTR_TO_STACK] = "fp",
542 [PTR_TO_PACKET] = "pkt",
543 [PTR_TO_PACKET_META] = "pkt_meta",
544 [PTR_TO_PACKET_END] = "pkt_end",
545 [PTR_TO_FLOW_KEYS] = "flow_keys",
546 [PTR_TO_SOCKET] = "sock",
547 [PTR_TO_SOCK_COMMON] = "sock_common",
548 [PTR_TO_TCP_SOCK] = "tcp_sock",
549 [PTR_TO_TP_BUFFER] = "tp_buffer",
550 [PTR_TO_XDP_SOCK] = "xdp_sock",
551 [PTR_TO_BTF_ID] = "ptr_",
552 [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_",
553 [PTR_TO_MEM] = "mem",
554 [PTR_TO_RDONLY_BUF] = "rdonly_buf",
555 [PTR_TO_RDWR_BUF] = "rdwr_buf",
556 [PTR_TO_FUNC] = "func",
557 [PTR_TO_MAP_KEY] = "map_key",
558 };
559
560 return str[BPF_BASE_TYPE(type)];
561 }
562
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [RFC PATCH bpf-next v2 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
In reply to Hao Luoreproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…
Hi Hao,
[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/Introduce-composable-bpf-types/20211130-093143 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20211130/202111301226.380sF7wF-lkp(a)intel.com/config) compiler: powerpc-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/9e92c0a723fc173ac102b3bb27df479a01f32896
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/Introduce-composable-bpf-types/20211130-093143
git checkout 9e92c0a723fc173ac102b3bb27df479a01f32896
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=powerpc SHELL=/bin/bash drivers/net/ethernet/netronome/nfp/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from <command-line>:
drivers/net/ethernet/netronome/nfp/bpf/verifier.c: In function 'nfp_bpf_check_helper_call':
>> include/linux/compiler_types.h:335:45: error: call to '__compiletime_assert_631' declared with attribute error: BUILD_BUG_ON failed: NFP_BPF_SCALAR_VALUE != SCALAR_VALUE || NFP_BPF_MAP_VALUE != PTR_TO_MAP_VALUE || NFP_BPF_STACK != PTR_TO_STACK || NFP_BPF_PACKET_DATA != PTR_TO_PACKET
335 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler_types.h:316:25: note: in definition of macro '__compiletime_assert'
316 | prefix ## suffix(); \
| ^~~~~~
include/linux/compiler_types.h:335:9: note: in expansion of macro '_compiletime_assert'
335 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert'
39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
| ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
| ^~~~~~~~~~~~~~~~
drivers/net/ethernet/netronome/nfp/bpf/verifier.c:234:17: note: in expansion of macro 'BUILD_BUG_ON'
234 | BUILD_BUG_ON(NFP_BPF_SCALAR_VALUE != SCALAR_VALUE ||
| ^~~~~~~~~~~~vim +/__compiletime_assert_631 +335 include/linux/compiler_types.h
eb5c2d4b45e3d2 Will Deacon 2020-07-21 321 eb5c2d4b45e3d2 Will Deacon 2020-07-21 322 #define _compiletime_assert(condition, msg, prefix, suffix) \ eb5c2d4b45e3d2 Will Deacon 2020-07-21 323 __compiletime_assert(condition, msg, prefix, suffix) eb5c2d4b45e3d2 Will Deacon 2020-07-21 324 eb5c2d4b45e3d2 Will Deacon 2020-07-21 325 /** eb5c2d4b45e3d2 Will Deacon 2020-07-21 326 * compiletime_assert - break build and emit msg if condition is false eb5c2d4b45e3d2 Will Deacon 2020-07-21 327 * @condition: a compile-time constant condition to check eb5c2d4b45e3d2 Will Deacon 2020-07-21 328 * @msg: a message to emit if condition is false eb5c2d4b45e3d2 Will Deacon 2020-07-21 329 * eb5c2d4b45e3d2 Will Deacon 2020-07-21 330 * In tradition of POSIX assert, this macro will break the build if the eb5c2d4b45e3d2 Will Deacon 2020-07-21 331 * supplied condition is *false*, emitting the supplied error message if the eb5c2d4b45e3d2 Will Deacon 2020-07-21 332 * compiler has support to do so. eb5c2d4b45e3d2 Will Deacon 2020-07-21 333 */ eb5c2d4b45e3d2 Will Deacon 2020-07-21 334 #define compiletime_assert(condition, msg) \
eb5c2d4b45e3d2 Will Deacon 2020-07-21 @335 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)eb5c2d4b45e3d2 Will Deacon 2020-07-21 336
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC PATCH v2 5/9 bpf: Introduce MEM_RDONLY flag
In reply to Hao LuoThis patch introduce another flag MEM_RDONLY to tag a reg value is pointing to a read-only memory. It makes the following changes:
1. PTR_TO_RDWR_BUF -> PTR_TO_BUF
2. PTR_TO_RDONLY_BUF -> PTR_TO_BUF | MEM_RDONLY
--- include/linux/bpf.h | 8 +++-- kernel/bpf/btf.c | 3 +- kernel/bpf/map_iter.c | 4 +-- kernel/bpf/verifier.c | 76 +++++++++++++++++++++++---------------- net/core/bpf_sk_storage.c | 2 +- net/core/sock_map.c | 2 +- 6 files changed, 55 insertions(+), 40 deletions(-)
include/linux/bpf.h +5 -3
@@ -311,7 +311,10 @@ enum bpf_type_flag {/* PTR may be NULL. */ PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), - __BPF_TYPE_LAST_FLAG = PTR_MAYBE_NULL, +/* MEM is read-only. */ +MEM_RDONLY =BIT (1 +BPF_BASE_TYPE_BITS ), + + __BPF_TYPE_LAST_FLAG = MEM_RDONLY, };#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS, 0)
@@ -499,8 +502,7 @@ enum bpf_reg_type {* an explicit null check is required for this struct. */ PTR_TO_MEM ,/* reg points to valid memory region */ -PTR_TO_RDONLY_BUF ,/* reg points to a readonly buffer */ -PTR_TO_RDWR_BUF ,/* reg points to a read/write buffer */ +PTR_TO_BUF ,/* reg points to a read/write buffer */ PTR_TO_PERCPU_BTF_ID ,/* reg points to a percpu kernel variable */ PTR_TO_FUNC ,/* reg points to a bpf program function */ PTR_TO_MAP_KEY, /* reg points to a map element key */
kernel/bpf/btf.c +1 -2
@@ -4946,8 +4946,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, type =BPF_BASE_TYPE (ctx_arg_info->reg_type); flag =BPF_TYPE_FLAG (ctx_arg_info->reg_type); -if (ctx_arg_info->offset == off && - (type ==PTR_TO_RDWR_BUF || type ==PTR_TO_RDONLY_BUF ) && +if (ctx_arg_info->offset == off && type ==PTR_TO_BUF && (flag &PTR_MAYBE_NULL )) { info->reg_type = ctx_arg_info->reg_type;return true;
kernel/bpf/map_iter.c +2 -2
@@ -174,9 +174,9 @@ static const struct bpf_iter_reg bpf_map_elem_reg_info = { .ctx_arg_info_size = 2, .ctx_arg_info = { { offsetof(struct bpf_iter__bpf_map_elem, key), - PTR_TO_RDONLY_BUF | PTR_MAYBE_NULL }, + PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY }, { offsetof(struct bpf_iter__bpf_map_elem, value), - PTR_TO_RDWR_BUF | PTR_MAYBE_NULL }, + PTR_TO_BUF | PTR_MAYBE_NULL }, }, };
kernel/bpf/verifier.c +45 -31
124 lines not on this page · raw message
net/core/bpf_sk_storage.c +1 -1
@@ -929,7 +929,7 @@ static struct bpf_iter_reg bpf_sk_storage_map_reg_info = { { offsetof(struct bpf_iter__bpf_sk_storage_map , sk), PTR_TO_BTF_ID_OR_NULL }, { offsetof(struct bpf_iter__bpf_sk_storage_map, value), - PTR_TO_RDWR_BUF | PTR_MAYBE_NULL }, + PTR_TO_BUF | PTR_MAYBE_NULL }, }, .seq_info = &iter_seq_info, };
net/core/sock_map.c +1 -1
@@ -1559,7 +1559,7 @@ static struct bpf_iter_reg sock_map_iter_reg = { .ctx_arg_info_size = 2, .ctx_arg_info = { { offsetof(struct bpf_iter__sockmap, key), - PTR_TO_RDONLY_BUF | PTR_MAYBE_NULL }, + PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY }, { offsetof(struct bpf_iter__sockmap, sk), PTR_TO_BTF_ID_OR_NULL }, },
Signature · 1 line
2.34.0.384.gca35af8252-goog
RFC PATCH v2 6/9 bpf: Convert PTR_TO_MEM_OR_NULL to composable types.
In reply to Hao LuoRemove PTR_TO_MEM_OR_NULL and replace it with PTR_TO_MEM combined with flag PTR_MAYBE_NULL.
--- include/linux/bpf.h | 1 - kernel/bpf/btf.c | 2 +- kernel/bpf/verifier.c | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-)
include/linux/bpf.h +0 -1
@@ -514,7 +514,6 @@ enum bpf_reg_type {PTR_TO_SOCK_COMMON_OR_NULL =PTR_MAYBE_NULL |PTR_TO_SOCK_COMMON ,PTR_TO_TCP_SOCK_OR_NULL =PTR_MAYBE_NULL |PTR_TO_TCP_SOCK , PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | PTR_TO_BTF_ID, -PTR_TO_MEM_OR_NULL =PTR_MAYBE_NULL |PTR_TO_MEM ,/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag.
kernel/bpf/btf.c +1 -1
@@ -5861,7 +5861,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,return -EINVAL ; } - reg->type =PTR_TO_MEM_OR_NULL ; + reg->type =PTR_TO_MEM |PTR_MAYBE_NULL ; reg->id = ++env->id_gen;continue ;
kernel/bpf/verifier.c +1 -1
@@ -13313,7 +13313,7 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)mark_reg_known_zero (env, regs, i); else if (regs[i].type == SCALAR_VALUE) mark_reg_unknown(env, regs, i); - else if (regs[i].type == PTR_TO_MEM_OR_NULL) { + else if (BPF_BASE_TYPE(regs[i].type) == PTR_TO_MEM) {const u32 mem_size = regs[i].mem_size;mark_reg_known_zero (env, regs, i);
Signature · 1 line
2.34.0.384.gca35af8252-goog
RFC PATCH v2 7/9 bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
In reply to Hao LuoTag the return type of {per, this}_cpu_ptr with RDONLY_MEM. The returned value of this pair of helpers is kernel object, which can not be updated by bpf programs. In addition to tagging these two helpers, the verifier is now able to differentiate RDONLY MEM from RDWR MEM.
--- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-)
kernel/bpf/helpers.c +2 -2
@@ -667,7 +667,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)const struct bpf_func_proto bpf_per_cpu_ptr_proto = { .func = bpf_per_cpu_ptr, .gpl_only = false, - .ret_type =RET_PTR_TO_MEM_OR_BTF_ID |PTR_MAYBE_NULL , + .ret_type =RET_PTR_TO_MEM_OR_BTF_ID |PTR_MAYBE_NULL |MEM_RDONLY , .arg1_type =ARG_PTR_TO_PERCPU_BTF_ID , .arg2_type =ARG_ANYTHING , };
@@ -680,7 +680,7 @@ BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr)const struct bpf_func_proto bpf_this_cpu_ptr_proto = { .func = bpf_this_cpu_ptr, .gpl_only = false, - .ret_type =RET_PTR_TO_MEM_OR_BTF_ID , + .ret_type =RET_PTR_TO_MEM_OR_BTF_ID |MEM_RDONLY , .arg1_type =ARG_PTR_TO_PERCPU_BTF_ID , };
kernel/bpf/verifier.c +20 -4
@@ -4306,16 +4306,32 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnmark_reg_unknown (env, regs, value_regno); } } - }else if (reg->type ==PTR_TO_MEM ) { + }else if (BPF_BASE_TYPE (reg->type) ==PTR_TO_MEM ) { +bool rdonly_mem =reg_type_is_rdonly_mem (reg->type); + +if (reg_type_may_be_null (reg->type)) { +verbose (env,"R%d invalid mem access '%s'\n" , regno, +reg_type_str (reg->type)); +return -EACCES ; + } + +if (t ==BPF_WRITE && rdonly_mem) { +verbose (env,"R%d cannot write into rdonly %s\n" , + regno,reg_type_str (reg->type)); +return -EACCES ; + } +if (t ==BPF_WRITE && value_regno >=0 &&is_pointer_value (env, value_regno)) {verbose (env,"R%d leaks addr into mem\n" , value_regno);return -EACCES ; } + err =check_mem_region_access (env, regno, off, size, reg->mem_size, false);if (!err && t ==BPF_READ && value_regno >=0 ) -mark_reg_unknown (env, regs, value_regno); +if (t ==BPF_READ || rdonly_mem) +mark_reg_unknown (env, regs, value_regno); }else if (reg->type ==PTR_TO_CTX ) {enum bpf_reg_type reg_type =SCALAR_VALUE ;struct btf *btf =NULL ;
@@ -9345,7 +9361,7 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)mark_reg_known_zero (env, regs, insn->dst_reg); dst_reg->type = aux->btf_var.reg_type; -switch (dst_reg->type) { +switch (BPF_BASE_TYPE (dst_reg->type)) {case PTR_TO_MEM : dst_reg->mem_size = aux->btf_var.mem_size;break ;
@@ -11489,7 +11505,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, err = -EINVAL ; goto err_put; } - aux->btf_var.reg_type =PTR_TO_MEM ; + aux->btf_var.reg_type =PTR_TO_MEM |MEM_RDONLY ; aux->btf_var.mem_size = tsize; }else { aux->btf_var.reg_type =PTR_TO_BTF_ID ;
Signature · 1 line
2.34.0.384.gca35af8252-goog
RFC PATCH v2 8/9 bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
In reply to Hao LuoSome helper functions may modify its arguments, for example, bpf_d_path, bpf_get_stack etc. Previously, their argument types were marked as ARG_PTR_TO_MEM, which is compatible with read-only mem types, such as PTR_TO_RDONLY_BUF. Therefore it's legitimate to modify a read-only memory by passing it into one of such helper functions.
This patch tags the args that point to immutable memory with MEM_RDONLY flag. The arguments that don't have this flag will be only compatible with mutable memory types. The arguments that have MEM_RDONLY flag are compatible with both mutable memory and immutable memory.
--- kernel/bpf/btf.c | 2 +- kernel/bpf/cgroup.c | 2 +- kernel/bpf/helpers.c | 8 ++--- kernel/bpf/ringbuf.c | 2 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 28 +++++++++++++++++- kernel/trace/bpf_trace.c | 26 ++++++++-------- net/core/filter.c | 64 ++++++++++++++++++++-------------------- 8 files changed, 80 insertions(+), 54 deletions(-)
kernel/bpf/btf.c +1 -1
@@ -6351,7 +6351,7 @@ const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { .func = bpf_btf_find_by_name_kind, .gpl_only = false, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_ANYTHING,
kernel/bpf/cgroup.c +1 -1
@@ -1789,7 +1789,7 @@ static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE, };
kernel/bpf/helpers.c +4 -4
@@ -530,7 +530,7 @@ const struct bpf_func_proto bpf_strtol_proto = { .func = bpf_strtol, .gpl_only = false, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_LONG,
@@ -558,7 +558,7 @@ const struct bpf_func_proto bpf_strtoul_proto = { .func = bpf_strtoul, .gpl_only = false, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_LONG,
@@ -630,7 +630,7 @@ const struct bpf_func_proto bpf_event_output_data_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_MEM, + .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg5_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -1011,7 +1011,7 @@ const struct bpf_func_proto bpf_snprintf_proto = { .arg1_type = ARG_PTR_TO_MEM_OR_NULL, .arg2_type = ARG_CONST_SIZE_OR_ZERO, .arg3_type = ARG_PTR_TO_CONST_STR, - .arg4_type = ARG_PTR_TO_MEM_OR_NULL, + .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, .arg5_type = ARG_CONST_SIZE_OR_ZERO, };
kernel/bpf/ringbuf.c +1 -1
@@ -444,7 +444,7 @@ const struct bpf_func_proto bpf_ringbuf_output_proto = { .func = bpf_ringbuf_output, .ret_type = RET_INTEGER, .arg1_type = ARG_CONST_MAP_PTR, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
kernel/bpf/syscall.c +1 -1
@@ -4757,7 +4757,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_ANYTHING, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE, };
kernel/bpf/verifier.c +27 -1
@@ -4976,9 +4976,15 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,return 0 ; } +struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {
@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,
@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,PTR_TO_MEM ,PTR_TO_BUF , - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match, };static const struct bpf_reg_types int_ptr_types = {
@@ -5097,6 +5116,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,if (expected ==NOT_INIT )break ; +if (compatible->type_match_fn) { +if (compatible->type_match_fn (arg_type, type, expected)) + goto found; + +continue ; + } +if (type == expected) goto found; }
kernel/trace/bpf_trace.c +13 -13
@@ -345,7 +345,7 @@ static const struct bpf_func_proto bpf_probe_write_user_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_ANYTHING, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE, };
@@ -394,7 +394,7 @@ static const struct bpf_func_proto bpf_trace_printk_proto = { .func = bpf_trace_printk, .gpl_only = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, };
@@ -450,9 +450,9 @@ static const struct bpf_func_proto bpf_trace_vprintk_proto = { .func = bpf_trace_vprintk, .gpl_only = true, .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_MEM, + .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg2_type = ARG_CONST_SIZE, - .arg3_type = ARG_PTR_TO_MEM_OR_NULL, + .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, .arg4_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -492,9 +492,9 @@ static const struct bpf_func_proto bpf_seq_printf_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &btf_seq_file_ids[0], - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE, - .arg4_type = ARG_PTR_TO_MEM_OR_NULL, + .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, .arg5_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -509,7 +509,7 @@ static const struct bpf_func_proto bpf_seq_write_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &btf_seq_file_ids[0], - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -533,7 +533,7 @@ static const struct bpf_func_proto bpf_seq_printf_btf_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &btf_seq_file_ids[0], - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
@@ -694,7 +694,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_MEM, + .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg5_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -1004,7 +1004,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM, .arg2_type = ARG_CONST_SIZE, - .arg3_type = ARG_PTR_TO_MEM, + .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg4_type = ARG_CONST_SIZE, .arg5_type = ARG_ANYTHING, };
@@ -1289,7 +1289,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_MEM, + .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg5_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -1515,7 +1515,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, - .arg4_type = ARG_PTR_TO_MEM, + .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg5_type = ARG_CONST_SIZE_OR_ZERO, };
@@ -1569,7 +1569,7 @@ static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, - .arg2_type = ARG_PTR_TO_MEM, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
net/core/filter.c +32 -32
241 lines not on this page · raw message
Signature · 1 line
2.34.0.384.gca35af8252-goog
Re: [RFC PATCH bpf-next v2 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 34 lines · Hao Luo
kernel/bpf/verifier.c +19
-0+struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,
kernel/bpf/verifier.c static const struct bpf_reg_types mem_types = { · line 5045
Quoted 6 lines
kernel/bpf/verifier.c +1
-1@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = {PTR_TO_MAP_VALUE ,PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match,
why add a callback for this logic?
Isn't it a universal rule for MEM_RDONLY?
Re: [RFC PATCH bpf-next v2 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Hao Luo
kernel/bpf/verifier.c +19
-0+struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,
Quoted 8 lines
kernel/bpf/verifier.c static const struct bpf_reg_types mem_types = { · line 5045
Quoted 5 lines
kernel/bpf/verifier.c +1
-1PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match,why add a callback for this logic?
Isn't it a universal rule forMEM_RDONLY?
Ah, good point, I didn't realize that. Maybe, not only MEM_RDONLY, but all flags can be checked in the same way? Like the following
static const struct bpf_reg_types int_ptr_types = {
@@ -5097,6 +5116,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
if (expected == NOT_INIT)
break;+ flag = bpf_type_flag(arg_type);
- if (type == expected)
+ if ((type & ~flag) == expected)
goto found;
}Re: [RFC PATCH bpf-next v2 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 54 lines · Hao Luo
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Hao Luo
kernel/bpf/verifier.c +19
-0+struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,Quoted 8 lines
kernel/bpf/verifier.c static const struct bpf_reg_types mem_types = { · line 5045
Quoted 5 lines
kernel/bpf/verifier.c +1
-1PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match,why add a callback for this logic?
Isn't it a universal rule forMEM_RDONLY?Ah, good point, I didn't realize that. Maybe, not only
MEM_RDONLY, but all flags can be checked in the same way? Like the followingstatic const struct
bpf_reg_typesint_ptr_types= {
kernel/bpf/verifier.c static int check_reg_type(struct · line 5128
Quoted 10 lines
kernel/bpf/verifier.c +2
-1@@ -5097,6 +5116,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, if (expected == NOT_INIT) break; + flag = bpf_type_flag(arg_type); -if (type == expected) +if ((type & ~flag) == expected) goto found; }
I think for MAYBE_NULL and MEM_RDONLY that would be correct, but not necessarily apply to future flags.
I would open code it for these specific flags.
Also what do you think about dropping bpf_ prefix from type_flag()? It won't conflict with anything and less verbose.
Re: [RFC PATCH bpf-next v2 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 62 lines · Alexei Starovoitov
Quoted 60 lines · Hao Luo
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Hao Luo
kernel/bpf/verifier.c +19
-0+struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,Quoted 8 lines
kernel/bpf/verifier.c static const struct bpf_reg_types mem_types = { · line 5045
Quoted 5 lines
kernel/bpf/verifier.c +1
-1PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match,why add a callback for this logic?
Isn't it a universal rule forMEM_RDONLY?Ah, good point, I didn't realize that. Maybe, not only
MEM_RDONLY, but all flags can be checked in the same way? Like the followingstatic const struct
bpf_reg_typesint_ptr_types= {kernel/bpf/verifier.c +1
-0@@ -5097,6 +5116,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, if (expected == NOT_INIT) break; + flag = bpf_type_flag(arg_type);
Quoted 8 lines
kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5128
Quoted 4 lines
kernel/bpf/verifier.c +1
-1-if (type == expected) +if ((type & ~flag) == expected) goto found; }I think for
MAYBE_NULLandMEM_RDONLYthat would be correct, but not necessarily apply to future flags.
I would open code it for these specific flags.
ack.
Quoted 2 lines
Also what do you think about dropping
bpf_prefix fromtype_flag()? It won't conflict with anything and less verbose.
Sounds good as long as it won't conflict. IMO it would be good to have an internal header in kernel/bpf. It appears to me that we put everything in linux/bpf.h now. In sched, there is a kernel/sched/sched.h used internally in kernel/sched and a linux/sched.h that is public to other subsystems.
Re: [RFC PATCH bpf-next v2 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 76 lines · Hao Luo
Quoted 62 lines · Alexei Starovoitov
Quoted 60 lines · Hao Luo
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Hao Luo
kernel/bpf/verifier.c +19
-0+struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,Quoted 8 lines
kernel/bpf/verifier.c static const struct bpf_reg_types mem_types = { · line 5045
Quoted 5 lines
kernel/bpf/verifier.c +1
-1PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match,why add a callback for this logic?
Isn't it a universal rule forMEM_RDONLY?Ah, good point, I didn't realize that. Maybe, not only
MEM_RDONLY, but all flags can be checked in the same way? Like the followingstatic const struct
bpf_reg_typesint_ptr_types= {kernel/bpf/verifier.c +1
-0@@ -5097,6 +5116,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, if (expected == NOT_INIT) break; + flag = bpf_type_flag(arg_type);Quoted 8 lines
kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5128
Quoted 4 lines
kernel/bpf/verifier.c +1
-1-if (type == expected) +if ((type & ~flag) == expected) goto found; }I think for
MAYBE_NULLandMEM_RDONLYthat would be correct, but not necessarily apply to future flags.
I would open code it for these specific flags.ack.
Quoted 8 lines
Quoted 2 lines
Also what do you think about dropping
bpf_prefix fromtype_flag()? It won't conflict with anything and less verbose.Sounds good as long as it won't conflict. IMO it would be good to have an internal header in kernel/bpf. It appears to me that we put everything in
linux/bpf.hnow. In sched, there is akernel/sched/sched.hused internally in kernel/sched and alinux/sched.hthat is public to other subsystems.
yeah. That's long overdue. We have include/linux/bpf_verifier.h that might work for this case too.
Or even directly in verifier.c (if possible).
Re: [RFC PATCH bpf-next v2 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 87 lines · Alexei Starovoitov
Quoted 76 lines · Hao Luo
Quoted 62 lines · Alexei Starovoitov
Quoted 60 lines · Hao Luo
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Hao Luo
kernel/bpf/verifier.c +19
-0+struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id; + +/* Certain types require customized type matching function. */ +bool (*type_match_fn)(enum bpf_arg_type arg_type, +enum bpf_reg_type type, +enum bpf_reg_type expected); };static const struct bpf_reg_types map_key_value_types = {@@ -5013,6 +5019,19 @@ static const struct bpf_reg_types btf_id_sock_common_types = { };#endif +static bool mem_type_match (enum bpf_arg_type arg_type, +enum bpf_reg_type type,enum bpf_reg_type expected) +{ +/* If arg_type is tagged with MEM_RDONLY, type is compatible with both +* RDONLY and RDWR mem, fold the MEM_RDONLY flag in 'type' before +* comparison. +*/ +if ((arg_type &MEM_RDONLY ) !=0 ) + type &= ~MEM_RDONLY ; + +return type == expected; +} +static const struct bpf_reg_types mem_types = { .types = {PTR_TO_STACK ,@@ -5022,8 +5041,8 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,Quoted 8 lines
kernel/bpf/verifier.c static const struct bpf_reg_types mem_types = { · line 5045
Quoted 5 lines
kernel/bpf/verifier.c +1
-1PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, + .type_match_fn = mem_type_match,why add a callback for this logic?
Isn't it a universal rule forMEM_RDONLY?Ah, good point, I didn't realize that. Maybe, not only
MEM_RDONLY, but all flags can be checked in the same way? Like the followingstatic const struct
bpf_reg_typesint_ptr_types= {kernel/bpf/verifier.c +1
-0@@ -5097,6 +5116,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, if (expected == NOT_INIT) break; + flag = bpf_type_flag(arg_type);Quoted 8 lines
kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5128
Quoted 4 lines
kernel/bpf/verifier.c +1
-1-if (type == expected) +if ((type & ~flag) == expected) goto found; }I think for
MAYBE_NULLandMEM_RDONLYthat would be correct, but not necessarily apply to future flags.
I would open code it for these specific flags.ack.
Quoted 8 lines
Quoted 2 lines
Also what do you think about dropping
bpf_prefix fromtype_flag()? It won't conflict with anything and less verbose.Sounds good as long as it won't conflict. IMO it would be good to have an internal header in kernel/bpf. It appears to me that we put everything in
linux/bpf.hnow. In sched, there is akernel/sched/sched.hused internally in kernel/sched and alinux/sched.hthat is public to other subsystems.
Quoted 3 lines
yeah. That's long overdue. We have
include/linux/bpf_verifier.hthat might work for this case too.
Or even directly inverifier.c(if possible).
Ack. Directly in verifier.c would be best, but bpf_verifier.h works. There is only one place outside verifier.c (i.e. btf_ctx_access() in btf.c) that needs base_type, the rest are all in verifier.c. Anyway, I am going to put these functions in bpf_verifer.h for now.
RFC PATCH v2 9/9 bpf/selftests: Test PTR_TO_RDONLY_MEM
In reply to Hao LuoThis test verifies that a ksym of non-struct can not be directly updated.
--- .../selftests/bpf/prog_tests/ksyms_btf.c | 14 +++++++++ .../bpf/progs/test_ksyms_btf_write_check.c | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c +14 -0
@@ -8,6 +8,7 @@#include "test_ksyms_btf_null_check.skel.h" #include "test_ksyms_weak.skel.h" #include "test_ksyms_weak.lskel.h" +#include "test_ksyms_btf_write_check.skel.h" static int duration;
@@ -137,6 +138,16 @@ static void test_weak_syms_lskel(void)test_ksyms_weak_lskel__destroy (skel); } +static void test_write_check (void ) +{ +struct test_ksyms_btf_write_check *skel; + + skel =test_ksyms_btf_write_check__open_and_load (); +ASSERT_ERR_PTR (skel,"unexpected load of a prog writing to ksym memory\n" ); + +test_ksyms_btf_write_check__destroy (skel); +} +void test_ksyms_btf (void ) {int percpu_datasec;
@@ -167,4 +178,7 @@ void test_ksyms_btf(void)if (test__start_subtest ("weak_ksyms_lskel" ))test_weak_syms_lskel (); + +if (test__start_subtest ("write_check" )) +test_write_check (); }
tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ + +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> + +extern const int bpf_prog_active __ksym; /* int type global var. */ + +SEC("raw_tp/sys_enter") +int handler (const void *ctx) +{ +int *active; +__u32 cpu; + + cpu =bpf_get_smp_processor_id (); + active = (int *)bpf_per_cpu_ptr (&bpf_prog_active, cpu); +if (active) { +/* Kernel memory obtained from bpf_{per,this}_cpu_ptr +* is read-only, should _not_ pass verification. +*/ +/* WRITE_ONCE */ + *(volatile int *)active =-1 ; + } + +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.34.0.384.gca35af8252-goog