Introduce composable bpf types
Subsystems (16)
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 express 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.
Changes since RFC v2:
- renamed BPF_BASE_TYPE to a more succinct name base_type and move its definition to bpf_verifier.h. Same for BPF_TYPE_FLAG. (Alexei)
- made checking MEM_RDONLY in check_reg_type() universal (Alexei)
- ran through majority of test_progs and fixed bugs in RFC v2:
- fixed incorrect BPF_BASE_TYPE_MASK. The high bit of GENMASK should be BITS - 1, rather than BITS. patch 1/9.
- fixed incorrect conditions when checking ARG_PTR_TO_MAP_VALUE in
check_func_arg(). See patch 2/9.
- fixed a bug where PTR_TO_BTF_ID may be combined with MEM_RDONLY, causing the check in check_mem_access() to fall through to the 'else' branch. See check_helper_call() in patch 7/9.
- fixed build failure on netronome driver. Entries in bpf_reg_type have been ordered. patch 4/9.
- fixed build warnings of using '%d' to print base_type. patch 4/9
- unify arg_type_may_be_null() and reg_type_may_be_null() into a single type_may_be_null().
Previous versions:
RFC v2:
https://lwn.net/Articles/877171/
RFC v1:
https://lore.kernel.org/bpf/[email protected]/T/
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
drivers/net/ethernet/netronome/nfp/bpf/fw.h | 4 +- include/linux/bpf.h | 99 +++- include/linux/bpf_verifier.h | 13 + kernel/bpf/btf.c | 12 +- 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 | 464 ++++++++---------- 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 ++ 16 files changed, 416 insertions(+), 335 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
Signature · 1 line
2.34.1.400.ga245620fadb-goog
PATCH 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 type" 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 following patches will rewrite arg_types, ret_types and reg_types respectively.
--- include/linux/bpf.h | 42 ++++++++++++++++++++++++++++++++++++ include/linux/bpf_verifier.h | 13 +++++++++++ 2 files changed, 55 insertions(+)
include/linux/bpf.h +42 -0
@@ -297,6 +297,29 @@ 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 , +}; + +/* 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)) +/* function argument constraints */ enum bpf_arg_type {ARG_DONTCARE =0 ,/* unused argument in helper function */
@@ -343,7 +366,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 +388,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 +497,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.
include/linux/bpf_verifier.h +13 -0
@@ -529,5 +529,18 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, struct bpf_attach_target_info *tgt_info);void bpf_free_kfunc_btf_tab (struct bpf_kfunc_btf_tab *tab); +#define BPF_BASE_TYPE_MASK GENMASK(BPF_BASE_TYPE_BITS - 1, 0) + +/* extract base type from bpf_{arg, return, reg}_type. */ +static inline u32 base_type (u32 type) +{ +return type &BPF_BASE_TYPE_MASK ; +} + +/* extract flags from an extended type. See bpf_type_flag in bpf.h. */ +static inline u32 type_flag (u32 type) +{ +return type & ~BPF_BASE_TYPE_MASK ; +}#endif /* _LINUX_BPF_VERIFIER_H */
Signature · 1 line
2.34.1.400.ga245620fadb-goog
PATCH 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 reserving high bits of bpf_arg to represent flags of a type.
One of the flags is PTR_MAYBE_NULL which indicates a pointer may be NULL. When applying this flag to an arg_type, it means the arg can take NULL pointer. 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 | 39 ++++++++++++++------------------------- 2 files changed, 23 insertions(+), 31 deletions(-)
include/linux/bpf.h +9 -6
@@ -331,13 +331,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.
@@ -347,26 +345,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 +14 -25
@@ -472,14 +472,9 @@ static bool arg_type_may_be_refcounted(enum bpf_arg_type type)return type ==ARG_PTR_TO_SOCK_COMMON ; } -static bool arg_type_may_be_null (enum bpf_arg_type type) +static bool type_may_be_null (u32 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 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 base_type (type) ==ARG_PTR_TO_MEM || +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[base_type (arg_type)];if (!compatible) {verbose (env,"verifier internal error: unsupported arg type %d\n" , arg_type);return -EFAULT ;
@@ -5190,15 +5179,14 @@ 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 (base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE || +base_type (arg_type) ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { err =resolve_map_arg_type (env, meta, &arg_type);if (err)return err; } -if (register_is_null (reg) &&arg_type_may_be_null (arg_type)) +if (register_is_null (reg) &&type_may_be_null (arg_type))/* A NULL register has a SCALAR_VALUE type, so skip * type checking. */
@@ -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 (base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE || +base_type (arg_type) ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { +if (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.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 2/9] bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
Quoted 177 lines · Hao Luo
We have introduced a new type to make
bpf_argcomposable, by reserving high bits ofbpf_argto represent flags of a type.One of the flags is
PTR_MAYBE_NULLwhich indicates a pointer may be NULL. When applying this flag to anarg_type, it means the arg can take NULL pointer. This patch switches the qualifiedarg_typesto use this flag. Thearg_typeschanged 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_NULLThis patch does not eliminate the use of these
arg_types, instead it makes them an alias to the 'ARG_XXX|PTR_MAYBE_NULL'.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 15 +++++++++------ kernel/bpf/verifier.c | 39 ++++++++++++++------------------------- 2 files changed, 23 insertions(+), 31 deletions(-)include/linux/bpf.h +9
-6@@ -331,13 +331,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. @@ -347,26 +345,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 +9
-21@@ -472,14 +472,9 @@ static bool arg_type_may_be_refcounted(enum bpf_arg_type type)return type ==ARG_PTR_TO_SOCK_COMMON ; } -static bool arg_type_may_be_null (enum bpf_arg_type type) +static bool type_may_be_null (u32 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 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 base_type (type) ==ARG_PTR_TO_MEM || +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[base_type (arg_type)];if (!compatible) {verbose (env,"verifier internal error: unsupported arg type %d\n" , arg_type);return -EFAULT ;@@ -5190,15 +5179,14 @@ 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 (base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE || +base_type (arg_type) ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { err =resolve_map_arg_type (env, meta, &arg_type);if (err)return err; } -if (register_is_null (reg) &&arg_type_may_be_null (arg_type)) +if (register_is_null (reg) &&type_may_be_null (arg_type))/* A NULL register has a SCALAR_VALUE type, so skip * type checking. */ @@ -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,
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 5262
Quoted 10 lines
kernel/bpf/verifier.c +5
-4NULL); - }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 (base_type (arg_type) ==ARG_PTR_TO_MAP_VALUE || +base_type (arg_type) ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { +if (type_may_be_null (arg_type) &®ister_is_null (reg)) +return err; +
small nit: return 0 would make it clear that we successfully checked everything (err is going to be zero here, but you need to scroll quite a lot up to check this, so it's a bit annoying).
Quoted 6 lines
kernel/bpf/verifier.c +0
-1/* bpf_map_xxx(..., map_ptr, ..., value) call: * check [value, value + map->value_size) validity */ --2.34.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 2/9] bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
Quoted 32 lines · Andrii Nakryiko
Quoted 30 lines · Hao Luo
We have introduced a new type to make
bpf_argcomposable, by reserving high bits ofbpf_argto represent flags of a type.One of the flags is
PTR_MAYBE_NULLwhich indicates a pointer may be NULL. When applying this flag to anarg_type, it means the arg can take NULL pointer. This patch switches the qualifiedarg_typesto use this flag. Thearg_typeschanged 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_NULLThis patch does not eliminate the use of these
arg_types, instead it makes them an alias to the 'ARG_XXX|PTR_MAYBE_NULL'.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 15 +++++++++------ kernel/bpf/verifier.c | 39 ++++++++++++++------------------------- 2 files changed, 23 insertions(+), 31 deletions(-)include/linux/bpf.h +0
-0
[...]
Quoted 9 lines
Quoted 9 lines
include/linux/bpf.h +1
-4@@ -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 (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
Quoted 9 lines
include/linux/bpf.h static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 5262
Quoted 4 lines
include/linux/bpf.h +4
-0+ base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { +if (type_may_be_null (arg_type) &®ister_is_null (reg)) +return err; +small nit: return 0 would make it clear that we successfully checked everything (err is going to be zero here, but you need to scroll quite a lot up to check this, so it's a bit annoying).
Yes, that makes sense. Will do.
Quoted 6 lines
Quoted 6 lines
include/linux/bpf.h +0
-1/* bpf_map_xxx(..., map_ptr, ..., value) call: * check [value, value + map->value_size) validity */ --2.34.1.400.ga245620fadb-goog
PATCH 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 reserving high bits to represent flags.
One of the flag is PTR_MAYBE_NULL, which indicates a pointer may be NULL. When applying this flag to ret_types, it means the returned value could be a NULL pointer. 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
This patch doesn't eliminate the use of these names, instead it makes them aliases to 'RET_PTR_TO_XXX | PTR_MAYBE_NULL'.
--- include/linux/bpf.h | 19 ++++++++++------ kernel/bpf/helpers.c | 2 +- kernel/bpf/verifier.c | 52 +++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 34 deletions(-)
include/linux/bpf.h +12 -7
@@ -382,17 +382,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 +26 -26
109 lines not on this page · raw message
Signature · 1 line
2.34.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 3/9] bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
Quoted 28 lines · Hao Luo
We have introduced a new type to make
bpf_retcomposable, by reserving high bits to represent flags.One of the flag is
PTR_MAYBE_NULL, which indicates a pointer may be NULL. When applying this flag toret_types, it means the returned value could be a NULL pointer. This patch switches the qualifiedarg_typesto use this flag. Theret_typeschanged 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_NULLThis patch doesn't eliminate the use of these names, instead it makes them aliases to '
RET_PTR_TO_XXX|PTR_MAYBE_NULL'.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 19 ++++++++++------ kernel/bpf/helpers.c | 2 +- kernel/bpf/verifier.c | 52 +++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 34 deletions(-)
[...]
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6574
Quoted 8 lines
kernel/bpf/verifier.c +2
-2@@ -6570,28 +6570,28 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insnreturn -EINVAL ; } regs[BPF_REG_0 ].type = - fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ? -PTR_TO_MEM :PTR_TO_MEM_OR_NULL ; + (ret_type &PTR_MAYBE_NULL ) ? +PTR_TO_MEM_OR_NULL :PTR_TO_MEM ;
nit: I expected something like (let's use the fact that those flags are the same across different enums):
regs[BPF_REG_0].type = PTR_TO_MEM | (ret_type & PTR_MAYBE_NULL);
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6579
Quoted 7 lines
kernel/bpf/verifier.c +2
-2regs[BPF_REG_0 ].mem_size = tsize; }else { regs[BPF_REG_0 ].type = - fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ? -PTR_TO_BTF_ID :PTR_TO_BTF_ID_OR_NULL ; + (ret_type &PTR_MAYBE_NULL ) ? +PTR_TO_BTF_ID_OR_NULL :PTR_TO_BTF_ID ;
same as above
Quoted 5 lines
kernel/bpf/verifier.c +0
-2regs[BPF_REG_0 ].btf = meta.ret_btf; regs[BPF_REG_0 ].btf_id = meta.ret_btf_id; } - }else if (fn->ret_type ==RET_PTR_TO_BTF_ID_OR_NULL || - fn->ret_type ==RET_PTR_TO_BTF_ID ) {
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6589
Quoted 10 lines
kernel/bpf/verifier.c +4
-3+ }else if (base_type (ret_type) ==RET_PTR_TO_BTF_ID ) {int ret_btf_id;mark_reg_known_zero (env, regs,BPF_REG_0 ); - regs[BPF_REG_0 ].type = fn->ret_type ==RET_PTR_TO_BTF_ID ? -PTR_TO_BTF_ID : -PTR_TO_BTF_ID_OR_NULL ; + regs[BPF_REG_0 ].type = (ret_type &PTR_MAYBE_NULL ) ? +PTR_TO_BTF_ID_OR_NULL : +PTR_TO_BTF_ID ;
and here
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6593
Quoted 6 lines
kernel/bpf/verifier.c +2
-2ret_btf_id = *fn->ret_btf_id;if (ret_btf_id ==0 ) { -verbose (env,"invalid return type %d of func %s#%d\n" , - fn->ret_type,func_id_name (func_id), func_id); + verbose(env, "invalid return type %lu of func %s#%d\n", + base_type(ret_type), func_id_name(func_id),
base type returns u32, shouldn't it be %u then?
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6604
Quoted 12 lines
kernel/bpf/verifier.c +3
-2+ func_id);return -EINVAL ; }/* current BPF helper definitions are only coming from @@ -6600,8 +6600,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn regs[BPF_REG_0 ].btf = btf_vmlinux; regs[BPF_REG_0 ].btf_id = ret_btf_id; }else { -verbose (env,"unknown return type %d of func %s#%d\n" , - fn->ret_type,func_id_name (func_id), func_id); +verbose (env,"unknown return type %lu of func %s#%d\n" , +base_type (ret_type),func_id_name (func_id), func_id);
same %u
Quoted 6 lines
kernel/bpf/verifier.c +0
-1return -EINVAL ; } --2.34.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 3/9] bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
Quoted 45 lines · Andrii Nakryiko
Quoted 28 lines · Hao Luo
We have introduced a new type to make
bpf_retcomposable, by reserving high bits to represent flags.One of the flag is
PTR_MAYBE_NULL, which indicates a pointer may be NULL. When applying this flag toret_types, it means the returned value could be a NULL pointer. This patch switches the qualifiedarg_typesto use this flag. Theret_typeschanged 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_NULLThis patch doesn't eliminate the use of these names, instead it makes them aliases to '
RET_PTR_TO_XXX|PTR_MAYBE_NULL'.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 19 ++++++++++------ kernel/bpf/helpers.c | 2 +- kernel/bpf/verifier.c | 52 +++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 34 deletions(-)[...]
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6574
Quoted 8 lines
kernel/bpf/verifier.c +2
-2@@ -6570,28 +6570,28 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insnreturn -EINVAL ; } regs[BPF_REG_0 ].type = - fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ? -PTR_TO_MEM :PTR_TO_MEM_OR_NULL ; + (ret_type &PTR_MAYBE_NULL ) ? +PTR_TO_MEM_OR_NULL :PTR_TO_MEM ;nit: I expected something like (let's use the fact that those flags are the same across different enums):
Quoted 2 lines
regs[BPF_REG_0].type =
PTR_TO_MEM| (ret_type&PTR_MAYBE_NULL);
We haven't taught reg_type to recognize PTR_MAYBE_NULL until the next patch. Patch 4/9 does have the suggested conversion:
regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
Quoted 30 lines
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6579
Quoted 8 lines
kernel/bpf/verifier.c +2
-2regs[BPF_REG_0 ].mem_size = tsize; }else { regs[BPF_REG_0 ].type = - fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ? -PTR_TO_BTF_ID :PTR_TO_BTF_ID_OR_NULL ; + (ret_type &PTR_MAYBE_NULL ) ? +PTR_TO_BTF_ID_OR_NULL :PTR_TO_BTF_ID ;same as above
Quoted 5 lines
kernel/bpf/verifier.c +0
-2regs[BPF_REG_0 ].btf = meta.ret_btf; regs[BPF_REG_0 ].btf_id = meta.ret_btf_id; } - }else if (fn->ret_type ==RET_PTR_TO_BTF_ID_OR_NULL || - fn->ret_type ==RET_PTR_TO_BTF_ID ) {kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6589
Quoted 10 lines
kernel/bpf/verifier.c +4
-3+ }else if (base_type (ret_type) ==RET_PTR_TO_BTF_ID ) {int ret_btf_id;mark_reg_known_zero (env, regs,BPF_REG_0 ); - regs[BPF_REG_0 ].type = fn->ret_type ==RET_PTR_TO_BTF_ID ? -PTR_TO_BTF_ID : -PTR_TO_BTF_ID_OR_NULL ; + regs[BPF_REG_0 ].type = (ret_type &PTR_MAYBE_NULL ) ? +PTR_TO_BTF_ID_OR_NULL : +PTR_TO_BTF_ID ;and here
Quoted 9 lines
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6593
Quoted 6 lines
kernel/bpf/verifier.c +2
-2ret_btf_id = *fn->ret_btf_id;if (ret_btf_id ==0 ) { -verbose (env,"invalid return type %d of func %s#%d\n" , - fn->ret_type,func_id_name (func_id), func_id); + verbose(env, "invalid return type %lu of func %s#%d\n", + base_type(ret_type), func_id_name(func_id),base type returns u32, shouldn't it be %u then?
Ack, you are right. When writing this, I know '%lu' will work but didn't give it much thought. Will use '%u' in v2.
Quoted 21 lines
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6604
Quoted 12 lines
kernel/bpf/verifier.c +3
-2+ func_id);return -EINVAL ; }/* current BPF helper definitions are only coming from @@ -6600,8 +6600,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn regs[BPF_REG_0 ].btf = btf_vmlinux; regs[BPF_REG_0 ].btf_id = ret_btf_id; }else { -verbose (env,"unknown return type %d of func %s#%d\n" , - fn->ret_type,func_id_name (func_id), func_id); +verbose (env,"unknown return type %lu of func %s#%d\n" , +base_type (ret_type),func_id_name (func_id), func_id);same %u
Quoted 6 lines
kernel/bpf/verifier.c +0
-1return -EINVAL ; } --2.34.1.400.ga245620fadb-goog
PATCH 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 allocating bits in the type to represent flags.
One of the flags 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
--- drivers/net/ethernet/netronome/nfp/bpf/fw.h | 4 +- include/linux/bpf.h | 16 +- kernel/bpf/btf.c | 7 +- kernel/bpf/map_iter.c | 4 +- kernel/bpf/verifier.c | 278 ++++++++------------ net/core/bpf_sk_storage.c | 2 +- net/core/sock_map.c | 2 +- 7 files changed, 126 insertions(+), 187 deletions(-)
drivers/net/ethernet/netronome/nfp/bpf/fw.h +2 -2
@@ -13,8 +13,8 @@*/ #define NFP_BPF_SCALAR_VALUE 1#define NFP_BPF_MAP_VALUE 4 -#define NFP_BPF_STACK 6 -#define NFP_BPF_PACKET_DATA 8 +#define NFP_BPF_STACK 5 +#define NFP_BPF_PACKET_DATA 7enum bpf_cap_tlv_type {NFP_BPF_CAP_TYPE_FUNC =1 ,
include/linux/bpf.h +8 -8
@@ -465,18 +465,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
@@ -494,18 +490,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 +5 -2
@@ -4941,10 +4941,13 @@ 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]; +u32 type, flag; + type =base_type (ctx_arg_info->reg_type); + flag =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 +107 -171
587 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.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 28 lines · Hao Luo
We have introduced a new type to make
bpf_regcomposable, by allocating bits in the type to represent flags.One of the flags is
PTR_MAYBE_NULLwhich indicates a pointer may be NULL. This patch switches the qualifiedreg_typesto use this flag. Thereg_typeschanged 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_NULLSigned-off-by Hao Luo <[email protected]>--- drivers/net/ethernet/netronome/nfp/bpf/fw.h | 4 +- include/linux/bpf.h | 16 +- kernel/bpf/btf.c | 7 +- kernel/bpf/map_iter.c | 4 +- kernel/bpf/verifier.c | 278 ++++++++------------ net/core/bpf_sk_storage.c | 2 +- net/core/sock_map.c | 2 +- 7 files changed, 126 insertions(+), 187 deletions(-)
[...]
Quoted 54 lines
kernel/bpf/verifier.c +18
-32@@ -535,38 +520,35 @@ static bool is_cmpxchg_insn(const struct bpf_insn *insn) }/* string representation of 'enum bpf_reg_type' */ -static const char *const reg_type_str[] = { - [NOT_INIT ] ="?" , - [SCALAR_VALUE ] ="inv" , - [PTR_TO_CTX ] ="ctx" , - [CONST_PTR_TO_MAP ] ="map_ptr" , - [PTR_TO_MAP_VALUE ] ="map_value" , - [PTR_TO_MAP_VALUE_OR_NULL ] ="map_value_or_null" , - [PTR_TO_STACK ] ="fp" , - [PTR_TO_PACKET ] ="pkt" , - [PTR_TO_PACKET_META ] ="pkt_meta" , - [PTR_TO_PACKET_END ] ="pkt_end" , - [PTR_TO_FLOW_KEYS ] ="flow_keys" , - [PTR_TO_SOCKET ] ="sock" , - [PTR_TO_SOCKET_OR_NULL ] ="sock_or_null" , - [PTR_TO_SOCK_COMMON ] ="sock_common" , - [PTR_TO_SOCK_COMMON_OR_NULL ] ="sock_common_or_null" , - [PTR_TO_TCP_SOCK ] ="tcp_sock" , - [PTR_TO_TCP_SOCK_OR_NULL ] ="tcp_sock_or_null" , - [PTR_TO_TP_BUFFER ] ="tp_buffer" , - [PTR_TO_XDP_SOCK ] ="xdp_sock" , - [PTR_TO_BTF_ID ] ="ptr_" , - [PTR_TO_BTF_ID_OR_NULL ] ="ptr_or_null_" , - [PTR_TO_PERCPU_BTF_ID ] ="percpu_ptr_" , - [PTR_TO_MEM ] ="mem" , - [PTR_TO_MEM_OR_NULL ] ="mem_or_null" , - [PTR_TO_RDONLY_BUF ] ="rdonly_buf" , - [PTR_TO_RDONLY_BUF_OR_NULL ] ="rdonly_buf_or_null" , - [PTR_TO_RDWR_BUF ] ="rdwr_buf" , - [PTR_TO_RDWR_BUF_OR_NULL ] ="rdwr_buf_or_null" , - [PTR_TO_FUNC ] ="func" , - [PTR_TO_MAP_KEY ] ="map_key" , -}; +static const char *reg_type_str (enum bpf_reg_type type) +{ +static const char *const str[] = { + [NOT_INIT ] ="?" , + [SCALAR_VALUE ] ="inv" , + [PTR_TO_CTX ] ="ctx" , + [CONST_PTR_TO_MAP ] ="map_ptr" , + [PTR_TO_MAP_VALUE ] ="map_value" , + [PTR_TO_STACK ] ="fp" , + [PTR_TO_PACKET ] ="pkt" , + [PTR_TO_PACKET_META ] ="pkt_meta" , + [PTR_TO_PACKET_END ] ="pkt_end" , + [PTR_TO_FLOW_KEYS ] ="flow_keys" , + [PTR_TO_SOCKET ] ="sock" , + [PTR_TO_SOCK_COMMON ] ="sock_common" , + [PTR_TO_TCP_SOCK ] ="tcp_sock" , + [PTR_TO_TP_BUFFER ] ="tp_buffer" , + [PTR_TO_XDP_SOCK ] ="xdp_sock" ,
kernel/bpf/verifier.c static bool is_cmpxchg_insn(const struct bpf_insn *insn) · line 550
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + +return str[base_type (type)];
well... now we lose the "_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?
What can we do about that?
Quoted 13 lines
kernel/bpf/verifier.c +2
-1+}static char slot_type_char[] = { [STACK_INVALID ] ='?' ,@@ -631,7 +613,7 @@ static void print_verifier_state(struct bpf_verifier_env *env,continue ;verbose (env," R%d" , i);print_liveness (env, reg->live); -verbose (env,"=%s" , reg_type_str[t]); +verbose (env,"=%s" ,reg_type_str (t));if (t ==SCALAR_VALUE && reg->precise)verbose (env,"P" ); if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
[...]
Quoted 8 lines
if (smin >=
BPF_MAX_VAR_OFF|| smin <= -BPF_MAX_VAR_OFF) {
verbose(env, "value %lld makes %s pointer be out of bounds\n",kernel/bpf/verifier.c +1
-1- smin, reg_type_str[type]); + smin, reg_type_str(type));return false; }
kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · line 7155
Quoted 7 lines
kernel/bpf/verifier.c +1
-2@@ -7209,11 +7151,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; } -switch (ptr_reg->type) { -case PTR_TO_MAP_VALUE_OR_NULL : +if (ptr_reg->type ==PTR_TO_MAP_VALUE_OR_NULL ) { verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
the same message can be output for other pointer types, so instead of special-casing, let's just combine them with more helpful message
Quoted 25 lines
kernel/bpf/verifier.c +5
-5- dst, reg_type_str[ptr_reg->type]); + dst, reg_type_str(ptr_reg->type));return -EACCES ; + } + +switch (base_type (ptr_reg->type)) {case CONST_PTR_TO_MAP :/* smin_val represents the known value */ if (known && smin_val ==0 && opcode ==BPF_ADD )@@ -7221,14 +7165,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, fallthrough;case PTR_TO_PACKET_END :case PTR_TO_SOCKET : -case PTR_TO_SOCKET_OR_NULL :case PTR_TO_SOCK_COMMON : -case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK : -case PTR_TO_TCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :verbose (env,"R%d pointer arithmetic on %s prohibited\n" , - dst, reg_type_str[ptr_reg->type]); + dst,reg_type_str (ptr_reg->type));return -EACCES ;default :break ;
[...]
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 101 lines · Andrii Nakryiko
Quoted 28 lines · Hao Luo
We have introduced a new type to make
bpf_regcomposable, by allocating bits in the type to represent flags.One of the flags is
PTR_MAYBE_NULLwhich indicates a pointer may be NULL. This patch switches the qualifiedreg_typesto use this flag. Thereg_typeschanged 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_NULLSigned-off-by Hao Luo <[email protected]>--- drivers/net/ethernet/netronome/nfp/bpf/fw.h | 4 +- include/linux/bpf.h | 16 +- kernel/bpf/btf.c | 7 +- kernel/bpf/map_iter.c | 4 +- kernel/bpf/verifier.c | 278 ++++++++------------ net/core/bpf_sk_storage.c | 2 +- net/core/sock_map.c | 2 +- 7 files changed, 126 insertions(+), 187 deletions(-)[...]
Quoted 54 lines
kernel/bpf/verifier.c +18
-32@@ -535,38 +520,35 @@ static bool is_cmpxchg_insn(const struct bpf_insn *insn) }/* string representation of 'enum bpf_reg_type' */ -static const char *const reg_type_str[] = { - [NOT_INIT ] ="?" , - [SCALAR_VALUE ] ="inv" , - [PTR_TO_CTX ] ="ctx" , - [CONST_PTR_TO_MAP ] ="map_ptr" , - [PTR_TO_MAP_VALUE ] ="map_value" , - [PTR_TO_MAP_VALUE_OR_NULL ] ="map_value_or_null" , - [PTR_TO_STACK ] ="fp" , - [PTR_TO_PACKET ] ="pkt" , - [PTR_TO_PACKET_META ] ="pkt_meta" , - [PTR_TO_PACKET_END ] ="pkt_end" , - [PTR_TO_FLOW_KEYS ] ="flow_keys" , - [PTR_TO_SOCKET ] ="sock" , - [PTR_TO_SOCKET_OR_NULL ] ="sock_or_null" , - [PTR_TO_SOCK_COMMON ] ="sock_common" , - [PTR_TO_SOCK_COMMON_OR_NULL ] ="sock_common_or_null" , - [PTR_TO_TCP_SOCK ] ="tcp_sock" , - [PTR_TO_TCP_SOCK_OR_NULL ] ="tcp_sock_or_null" , - [PTR_TO_TP_BUFFER ] ="tp_buffer" , - [PTR_TO_XDP_SOCK ] ="xdp_sock" , - [PTR_TO_BTF_ID ] ="ptr_" , - [PTR_TO_BTF_ID_OR_NULL ] ="ptr_or_null_" , - [PTR_TO_PERCPU_BTF_ID ] ="percpu_ptr_" , - [PTR_TO_MEM ] ="mem" , - [PTR_TO_MEM_OR_NULL ] ="mem_or_null" , - [PTR_TO_RDONLY_BUF ] ="rdonly_buf" , - [PTR_TO_RDONLY_BUF_OR_NULL ] ="rdonly_buf_or_null" , - [PTR_TO_RDWR_BUF ] ="rdwr_buf" , - [PTR_TO_RDWR_BUF_OR_NULL ] ="rdwr_buf_or_null" , - [PTR_TO_FUNC ] ="func" , - [PTR_TO_MAP_KEY ] ="map_key" , -}; +static const char *reg_type_str (enum bpf_reg_type type) +{ +static const char *const str[] = { + [NOT_INIT ] ="?" , + [SCALAR_VALUE ] ="inv" , + [PTR_TO_CTX ] ="ctx" , + [CONST_PTR_TO_MAP ] ="map_ptr" , + [PTR_TO_MAP_VALUE ] ="map_value" , + [PTR_TO_STACK ] ="fp" , + [PTR_TO_PACKET ] ="pkt" , + [PTR_TO_PACKET_META ] ="pkt_meta" , + [PTR_TO_PACKET_END ] ="pkt_end" , + [PTR_TO_FLOW_KEYS ] ="flow_keys" , + [PTR_TO_SOCKET ] ="sock" , + [PTR_TO_SOCK_COMMON ] ="sock_common" , + [PTR_TO_TCP_SOCK ] ="tcp_sock" , + [PTR_TO_TP_BUFFER ] ="tp_buffer" , + [PTR_TO_XDP_SOCK ] ="xdp_sock" ,kernel/bpf/verifier.c static bool is_cmpxchg_insn(const struct bpf_insn *insn) · line 550
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + +return str[base_type (type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?
Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
I assume there is already synchronization around the verifier to prevent race on the buffer. If not, we have to ask the caller of reg_type_str() to pass in a buffer.
Quoted 27 lines
Quoted 13 lines
kernel/bpf/verifier.c +2
-1+}static char slot_type_char[] = { [STACK_INVALID ] ='?' ,@@ -631,7 +613,7 @@ static void print_verifier_state(struct bpf_verifier_env *env,continue ;verbose (env," R%d" , i);print_liveness (env, reg->live); -verbose (env,"=%s" , reg_type_str[t]); +verbose (env,"=%s" ,reg_type_str (t));if (t ==SCALAR_VALUE && reg->precise)verbose (env,"P" ); if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&[...]
Quoted 11 lines
if (smin >=
BPF_MAX_VAR_OFF|| smin <= -BPF_MAX_VAR_OFF) {
verbose(env, "value %lld makes %s pointer be out of bounds\n",kernel/bpf/verifier.c +1
-1- smin, reg_type_str[type]); + smin, reg_type_str(type));return false; }@@ -7209,11 +7151,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; }
Quoted 8 lines
kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · line 7155
Quoted 4 lines
kernel/bpf/verifier.c +1
-2-switch (ptr_reg->type) { -case PTR_TO_MAP_VALUE_OR_NULL : +if (ptr_reg->type ==PTR_TO_MAP_VALUE_OR_NULL ) { verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",the same message can be output for other pointer types, so instead of special-casing, let's just combine them with more helpful message
Ok. Just want to confirm my understanding, do you mean checking for NULL? Like the following replacement:
- if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
+ if (ptr_reg->type & PTR_MAYBE_NULL) {
Quoted 27 lines
Quoted 25 lines
kernel/bpf/verifier.c +5
-5- dst, reg_type_str[ptr_reg->type]); + dst, reg_type_str(ptr_reg->type));return -EACCES ; + } + +switch (base_type (ptr_reg->type)) {case CONST_PTR_TO_MAP :/* smin_val represents the known value */ if (known && smin_val ==0 && opcode ==BPF_ADD )@@ -7221,14 +7165,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, fallthrough;case PTR_TO_PACKET_END :case PTR_TO_SOCKET : -case PTR_TO_SOCKET_OR_NULL :case PTR_TO_SOCK_COMMON : -case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK : -case PTR_TO_TCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :verbose (env,"R%d pointer arithmetic on %s prohibited\n" , - dst, reg_type_str[ptr_reg->type]); + dst,reg_type_str (ptr_reg->type));return -EACCES ;default :break ;[...]
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 104 lines · Hao Luo
Quoted 101 lines · Andrii Nakryiko
Quoted 28 lines · Hao Luo
We have introduced a new type to make
bpf_regcomposable, by allocating bits in the type to represent flags.One of the flags is
PTR_MAYBE_NULLwhich indicates a pointer may be NULL. This patch switches the qualifiedreg_typesto use this flag. Thereg_typeschanged 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_NULLSigned-off-by Hao Luo <[email protected]>--- drivers/net/ethernet/netronome/nfp/bpf/fw.h | 4 +- include/linux/bpf.h | 16 +- kernel/bpf/btf.c | 7 +- kernel/bpf/map_iter.c | 4 +- kernel/bpf/verifier.c | 278 ++++++++------------ net/core/bpf_sk_storage.c | 2 +- net/core/sock_map.c | 2 +- 7 files changed, 126 insertions(+), 187 deletions(-)[...]
Quoted 54 lines
kernel/bpf/verifier.c +18
-32@@ -535,38 +520,35 @@ static bool is_cmpxchg_insn(const struct bpf_insn *insn) }/* string representation of 'enum bpf_reg_type' */ -static const char *const reg_type_str[] = { - [NOT_INIT ] ="?" , - [SCALAR_VALUE ] ="inv" , - [PTR_TO_CTX ] ="ctx" , - [CONST_PTR_TO_MAP ] ="map_ptr" , - [PTR_TO_MAP_VALUE ] ="map_value" , - [PTR_TO_MAP_VALUE_OR_NULL ] ="map_value_or_null" , - [PTR_TO_STACK ] ="fp" , - [PTR_TO_PACKET ] ="pkt" , - [PTR_TO_PACKET_META ] ="pkt_meta" , - [PTR_TO_PACKET_END ] ="pkt_end" , - [PTR_TO_FLOW_KEYS ] ="flow_keys" , - [PTR_TO_SOCKET ] ="sock" , - [PTR_TO_SOCKET_OR_NULL ] ="sock_or_null" , - [PTR_TO_SOCK_COMMON ] ="sock_common" , - [PTR_TO_SOCK_COMMON_OR_NULL ] ="sock_common_or_null" , - [PTR_TO_TCP_SOCK ] ="tcp_sock" , - [PTR_TO_TCP_SOCK_OR_NULL ] ="tcp_sock_or_null" , - [PTR_TO_TP_BUFFER ] ="tp_buffer" , - [PTR_TO_XDP_SOCK ] ="xdp_sock" , - [PTR_TO_BTF_ID ] ="ptr_" , - [PTR_TO_BTF_ID_OR_NULL ] ="ptr_or_null_" , - [PTR_TO_PERCPU_BTF_ID ] ="percpu_ptr_" , - [PTR_TO_MEM ] ="mem" , - [PTR_TO_MEM_OR_NULL ] ="mem_or_null" , - [PTR_TO_RDONLY_BUF ] ="rdonly_buf" , - [PTR_TO_RDONLY_BUF_OR_NULL ] ="rdonly_buf_or_null" , - [PTR_TO_RDWR_BUF ] ="rdwr_buf" , - [PTR_TO_RDWR_BUF_OR_NULL ] ="rdwr_buf_or_null" , - [PTR_TO_FUNC ] ="func" , - [PTR_TO_MAP_KEY ] ="map_key" , -}; +static const char *reg_type_str (enum bpf_reg_type type) +{ +static const char *const str[] = { + [NOT_INIT ] ="?" , + [SCALAR_VALUE ] ="inv" , + [PTR_TO_CTX ] ="ctx" , + [CONST_PTR_TO_MAP ] ="map_ptr" , + [PTR_TO_MAP_VALUE ] ="map_value" , + [PTR_TO_STACK ] ="fp" , + [PTR_TO_PACKET ] ="pkt" , + [PTR_TO_PACKET_META ] ="pkt_meta" , + [PTR_TO_PACKET_END ] ="pkt_end" , + [PTR_TO_FLOW_KEYS ] ="flow_keys" , + [PTR_TO_SOCKET ] ="sock" , + [PTR_TO_SOCK_COMMON ] ="sock_common" , + [PTR_TO_TCP_SOCK ] ="tcp_sock" , + [PTR_TO_TP_BUFFER ] ="tp_buffer" , + [PTR_TO_XDP_SOCK ] ="xdp_sock" ,kernel/bpf/verifier.c static bool is_cmpxchg_insn(const struct bpf_insn *insn) · line 550
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + +return str[base_type (type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?
Quoted 5 lines
Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
Can't use global buffer, because multiple BPF verifications can proceed at the same time.
Quoted 4 lines
I assume there is already synchronization around the verifier to prevent race on the buffer. If not, we have to ask the caller of
reg_type_str()to pass in a buffer.
I think passing temp buffer is the best (even if annoying) solution.
Quoted 37 lines
Quoted 28 lines
Quoted 14 lines
kernel/bpf/verifier.c +2
-1+}static char slot_type_char[] = { [STACK_INVALID ] ='?' ,@@ -631,7 +613,7 @@ static void print_verifier_state(struct bpf_verifier_env *env,continue ;verbose (env," R%d" , i);print_liveness (env, reg->live); -verbose (env,"=%s" , reg_type_str[t]); +verbose (env,"=%s" ,reg_type_str (t));if (t ==SCALAR_VALUE && reg->precise)verbose (env,"P" ); if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&[...]
Quoted 11 lines
if (smin >=
BPF_MAX_VAR_OFF|| smin <= -BPF_MAX_VAR_OFF) {
verbose(env, "value %lld makes %s pointer be out of bounds\n",kernel/bpf/verifier.c +1
-1- smin, reg_type_str[type]); + smin, reg_type_str(type));return false; }@@ -7209,11 +7151,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; }Quoted 8 lines
kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · line 7155
Quoted 4 lines
kernel/bpf/verifier.c +1
-2-switch (ptr_reg->type) { -case PTR_TO_MAP_VALUE_OR_NULL : +if (ptr_reg->type ==PTR_TO_MAP_VALUE_OR_NULL ) { verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",the same message can be output for other pointer types, so instead of special-casing, let's just combine them with more helpful message
Quoted 5 lines
Ok. Just want to confirm my understanding, do you mean checking for NULL? Like the following replacement:
- if (
ptr_reg->type==PTR_TO_MAP_VALUE_OR_NULL) {
+ if (ptr_reg->type&PTR_MAYBE_NULL) {
no, I meant combine PTR_TO_MAP_VALUE_OR_NULL with all the other cases (PTR_TO_PACKET_END and others), but update their message to suggest "null-check it first".
Quoted 28 lines
Quoted 28 lines
Quoted 26 lines
kernel/bpf/verifier.c +5
-5- dst, reg_type_str[ptr_reg->type]); + dst, reg_type_str(ptr_reg->type));return -EACCES ; + } + +switch (base_type (ptr_reg->type)) {case CONST_PTR_TO_MAP :/* smin_val represents the known value */ if (known && smin_val ==0 && opcode ==BPF_ADD )@@ -7221,14 +7165,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, fallthrough;case PTR_TO_PACKET_END :case PTR_TO_SOCKET : -case PTR_TO_SOCKET_OR_NULL :case PTR_TO_SOCK_COMMON : -case PTR_TO_SOCK_COMMON_OR_NULL :case PTR_TO_TCP_SOCK : -case PTR_TO_TCP_SOCK_OR_NULL :case PTR_TO_XDP_SOCK :verbose (env,"R%d pointer arithmetic on %s prohibited\n" , - dst, reg_type_str[ptr_reg->type]); + dst,reg_type_str (ptr_reg->type));return -EACCES ;default :break ;[...]
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 33 lines · Andrii Nakryiko
Quoted 33 lines
Quoted 33 lines
Quoted 19 lines
kernel/bpf/verifier.c +18
-0+static const char *reg_type_str (enum bpf_reg_type type) +{ +static const char *const str[] = { + [NOT_INIT ] ="?" , + [SCALAR_VALUE ] ="inv" , + [PTR_TO_CTX ] ="ctx" , + [CONST_PTR_TO_MAP ] ="map_ptr" , + [PTR_TO_MAP_VALUE ] ="map_value" , + [PTR_TO_STACK ] ="fp" , + [PTR_TO_PACKET ] ="pkt" , + [PTR_TO_PACKET_META ] ="pkt_meta" , + [PTR_TO_PACKET_END ] ="pkt_end" , + [PTR_TO_FLOW_KEYS ] ="flow_keys" , + [PTR_TO_SOCKET ] ="sock" , + [PTR_TO_SOCK_COMMON ] ="sock_common" , + [PTR_TO_TCP_SOCK ] ="tcp_sock" , + [PTR_TO_TP_BUFFER ] ="tp_buffer" , + [PTR_TO_XDP_SOCK ] ="xdp_sock" ,kernel/bpf/verifier.c static bool is_cmpxchg_insn(const struct bpf_insn *insn) · line 550
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + +return str[base_type (type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
Can't use global buffer, because multiple BPF verifications can proceed at the same time.
I think reg_type_str() can still return a const char string with flags.
It's not going to be a direct str[base_type(type)]; of course. The str[] would be different.
If that doesn't work out we can try:
s/verbose(,"%s", reg_type_str[])
/verbose(, "%s%s", reg_type_str(), reg_type_flags_str())
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 42 lines · Alexei Starovoitov
Quoted 39 lines · Andrii Nakryiko
Quoted 38 lines
Quoted 33 lines
Quoted 19 lines
kernel/bpf/verifier.c +18
-0+static const char *reg_type_str (enum bpf_reg_type type) +{ +static const char *const str[] = { + [NOT_INIT ] ="?" , + [SCALAR_VALUE ] ="inv" , + [PTR_TO_CTX ] ="ctx" , + [CONST_PTR_TO_MAP ] ="map_ptr" , + [PTR_TO_MAP_VALUE ] ="map_value" , + [PTR_TO_STACK ] ="fp" , + [PTR_TO_PACKET ] ="pkt" , + [PTR_TO_PACKET_META ] ="pkt_meta" , + [PTR_TO_PACKET_END ] ="pkt_end" , + [PTR_TO_FLOW_KEYS ] ="flow_keys" , + [PTR_TO_SOCKET ] ="sock" , + [PTR_TO_SOCK_COMMON ] ="sock_common" , + [PTR_TO_TCP_SOCK ] ="tcp_sock" , + [PTR_TO_TP_BUFFER ] ="tp_buffer" , + [PTR_TO_XDP_SOCK ] ="xdp_sock" ,kernel/bpf/verifier.c static bool is_cmpxchg_insn(const struct bpf_insn *insn) · line 550
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + +return str[base_type (type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
Quoted 7 lines
Quoted 2 lines
Can't use global buffer, because multiple BPF verifications can proceed at the same time.
I think
reg_type_str()can still return a const char string with flags.
It's not going to be a direct str[base_type(type)]; of course. The str[] would be different.
Sorry I didn't fully grasp this comment. Following is my understanding and thoughts so far. Let me know if I missed anything.
Right, we can return a char string, but the string must be provided by the caller, which is the annoying part. We could do something as following (solution 1)
const char *reg_type_str(..., char *buf)
{
...
snprintf(buf, ...);
return buf;
}
OR, (solution 2) we may embed a buffer in bpf_verifier_env and pass env into reg_type_str(). reg_type_str() just returns the buffer in env. This doesn't require the caller to prepare the buffer.
const char *reg_type_str(..., env)
{
...
snprintf(env->buf, ...);
return env->buf;
}
However, there will be a caveat when there are more than one reg_type_str in one verbose statement. In solution 1, the input buf has to be different. In solution 2, we just can't do that, because the buffer is implicitly shared. What do you think about solution 2?
Quoted 4 lines
If that doesn't work out we can try:
s/verbose(,"%s", reg_type_str[])
/verbose(, "%s%s",reg_type_str(),reg_type_flags_str())
This is probably more cumbersome IMHO.
Thinking about the implementation of reg_type_flags_str(). Because flags can be combined, I think it would be better to format a dynamic buffer, then we are back to the same problem: caller needs to pass a buffer. Of course, with only two flags right now, we could enumerate all flag combinations and map them to const strings.
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 48 lines · Hao Luo
Quoted 45 lines · Alexei Starovoitov
Quoted 41 lines · Andrii Nakryiko
Quoted 33 lines
Quoted 33 lines
Quoted 19 lines
kernel/bpf/verifier.c +18
-0+static const char *reg_type_str (enum bpf_reg_type type) +{ +static const char *const str[] = { + [NOT_INIT ] ="?" , + [SCALAR_VALUE ] ="inv" , + [PTR_TO_CTX ] ="ctx" , + [CONST_PTR_TO_MAP ] ="map_ptr" , + [PTR_TO_MAP_VALUE ] ="map_value" , + [PTR_TO_STACK ] ="fp" , + [PTR_TO_PACKET ] ="pkt" , + [PTR_TO_PACKET_META ] ="pkt_meta" , + [PTR_TO_PACKET_END ] ="pkt_end" , + [PTR_TO_FLOW_KEYS ] ="flow_keys" , + [PTR_TO_SOCKET ] ="sock" , + [PTR_TO_SOCK_COMMON ] ="sock_common" , + [PTR_TO_TCP_SOCK ] ="tcp_sock" , + [PTR_TO_TP_BUFFER ] ="tp_buffer" , + [PTR_TO_XDP_SOCK ] ="xdp_sock" ,kernel/bpf/verifier.c static bool is_cmpxchg_insn(const struct bpf_insn *insn) · line 550
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + +return str[base_type (type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?Quoted 5 lines
Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
Can't use global buffer, because multiple BPF verifications can proceed at the same time.
Quoted 7 lines
Quoted 4 lines
I think
reg_type_str()can still return a const char string with flags.
It's not going to be a direct str[base_type(type)]; of course. The str[] would be different.Sorry I didn't fully grasp this comment. Following is my understanding and thoughts so far. Let me know if I missed anything.
I meant that for now we can do:
static const char * const str[] = {
[NOT_INIT] = "?",
[PTR_TO_RDONLY_BUF] = "rdonly_buf",
};
switch(type) {
case PTR_TO_RDONLY_BUF | MAYBE_NULL:return "rdonly_buf_or_null";
default: return str[base_type(type)];
}
Quoted 15 lines
Right, we can return a char string, but the string must be provided by the caller, which is the annoying part. We could do something as following (solution 1)
const char *reg_type_str(..., char *buf)
{
...
snprintf(buf, ...);
return buf;
}OR, (solution 2) we may embed a buffer in
bpf_verifier_envand pass env intoreg_type_str().reg_type_str()just returns the buffer in env. This doesn't require the caller to prepare the buffer.
Quoted 6 lines
const char *reg_type_str(..., env)
{
...
snprintf(env->buf, ...);
returnenv->buf;
}
If we go with this approach then passing 'env' is a bit better, since 'buf' doesn't need to be allocated on stack.
Quoted 4 lines
However, there will be a caveat when there are more than one
reg_type_strin one verbose statement. In solution 1, the input buf has to be different. In solution 2, we just can't do that, because the buffer is implicitly shared. What do you think about solution 2?
There is only one verbose() with two reg_type_str[] right now. It can easily be split into two verbose().
Quoted 5 lines
Quoted 4 lines
If that doesn't work out we can try:
s/verbose(,"%s", reg_type_str[])
/verbose(, "%s%s",reg_type_str(),reg_type_flags_str())
Quoted 7 lines
This is probably more cumbersome IMHO.
Thinking about the implementation of
reg_type_flags_str(). Because flags can be combined, I think it would be better to format a dynamic buffer, then we are back to the same problem: caller needs to pass a buffer. Of course, with only two flags right now, we could enumerate all flag combinations and map them to const strings.
Yeah. With 3 or more flags that can be combined the const char approach wouldn't scale. So let's try 'env' approach?
The only tweak is that 'log' would be better than 'env'. That temp buf can be in struct bpf_verifier_log *log.
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 115 lines · Alexei Starovoitov
Quoted 48 lines · Hao Luo
Quoted 45 lines · Alexei Starovoitov
Quoted 41 lines · Andrii Nakryiko
Quoted 33 lines
Quoted 33 lines
Quoted 19 lines
(quoted diff) +18
-0+static const char *reg_type_str(enum bpf_reg_type type) +{ + static const char * const str[] = { + [NOT_INIT] = "?", + [SCALAR_VALUE] = "inv", + [PTR_TO_CTX] = "ctx", + [CONST_PTR_TO_MAP] = "map_ptr", + [PTR_TO_MAP_VALUE] = "map_value", + [PTR_TO_STACK] = "fp", + [PTR_TO_PACKET] = "pkt", + [PTR_TO_PACKET_META] = "pkt_meta", + [PTR_TO_PACKET_END] = "pkt_end", + [PTR_TO_FLOW_KEYS] = "flow_keys", + [PTR_TO_SOCKET] = "sock", + [PTR_TO_SOCK_COMMON] = "sock_common", + [PTR_TO_TCP_SOCK] = "tcp_sock", + [PTR_TO_TP_BUFFER] = "tp_buffer", + [PTR_TO_XDP_SOCK] = "xdp_sock",Review
Quoted 10 lines
(quoted diff) +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + + return str[base_type(type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?Quoted 5 lines
Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
Can't use global buffer, because multiple BPF verifications can proceed at the same time.
Quoted 7 lines
Quoted 4 lines
I think
reg_type_str()can still return a const char string with flags.
It's not going to be a direct str[base_type(type)]; of course. The str[] would be different.Sorry I didn't fully grasp this comment. Following is my understanding and thoughts so far. Let me know if I missed anything.
I meant that for now we can do:
static const char * const str[] = { [NOT_INIT] = "?", [PTR_TO_RDONLY_BUF] = "rdonly_buf", };switch(type) {
casePTR_TO_RDONLY_BUF| MAYBE_NULL:return "rdonly_buf_or_null";
default: return str[base_type(type)];
}Quoted 15 lines
Right, we can return a char string, but the string must be provided by the caller, which is the annoying part. We could do something as following (solution 1)
const char *reg_type_str(..., char *buf)
{
...
snprintf(buf, ...);
return buf;
}OR, (solution 2) we may embed a buffer in
bpf_verifier_envand pass env intoreg_type_str().reg_type_str()just returns the buffer in env. This doesn't require the caller to prepare the buffer.Quoted 6 lines
const char *reg_type_str(..., env)
{
...
snprintf(env->buf, ...);
returnenv->buf;
}If we go with this approach then passing 'env' is a bit better, since 'buf' doesn't need to be allocated on stack.
Quoted 4 lines
However, there will be a caveat when there are more than one
reg_type_strin one verbose statement. In solution 1, the input buf has to be different. In solution 2, we just can't do that, because the buffer is implicitly shared. What do you think about solution 2?There is only one
verbose()with two reg_type_str[] right now. It can easily be split into twoverbose().Quoted 5 lines
Quoted 4 lines
If that doesn't work out we can try:
s/verbose(,"%s", reg_type_str[])
/verbose(, "%s%s",reg_type_str(),reg_type_flags_str())Quoted 7 lines
This is probably more cumbersome IMHO.
Thinking about the implementation of
reg_type_flags_str(). Because flags can be combined, I think it would be better to format a dynamic buffer, then we are back to the same problem: caller needs to pass a buffer. Of course, with only two flags right now, we could enumerate all flag combinations and map them to const strings.
Quoted 5 lines
Yeah. With 3 or more flags that can be combined the const char approach wouldn't scale. So let's try 'env' approach?
The only tweak is that 'log' would be better than 'env'. That temp buf can be in struct
bpf_verifier_log*log.
Another idea would be to add bpf specific modifiers in pointer() in lib/vsprintf.c for verifier's reg_state, reg_type, and other bpf structs.
It might make print_verifier_state() much cleaner and would allow easy printing of reg state from anywhere inside the verifier code.
Currently the error print has minimal info. Like:
verbose(env, "R%d invalid %s access off=%d size=%d\n",
regno, reg_type_str[reg->type], off, size);
With vsnprintf support we could do something like:
verbose(env, "%Brp invalid access off=%d size=%d\n",
reg, off, size);and pointer() will do full print of struct bpf_reg_state.
This is probably something to consider long term.
Re: [PATCH bpf-next v1 4/9] bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
Quoted 132 lines · Alexei Starovoitov
Quoted 115 lines · Alexei Starovoitov
Quoted 48 lines · Hao Luo
Quoted 45 lines · Alexei Starovoitov
Quoted 41 lines · Andrii Nakryiko
Quoted 33 lines
Quoted 33 lines
Quoted 19 lines
(quoted diff) +18
-0+static const char *reg_type_str(enum bpf_reg_type type) +{ + static const char * const str[] = { + [NOT_INIT] = "?", + [SCALAR_VALUE] = "inv", + [PTR_TO_CTX] = "ctx", + [CONST_PTR_TO_MAP] = "map_ptr", + [PTR_TO_MAP_VALUE] = "map_value", + [PTR_TO_STACK] = "fp", + [PTR_TO_PACKET] = "pkt", + [PTR_TO_PACKET_META] = "pkt_meta", + [PTR_TO_PACKET_END] = "pkt_end", + [PTR_TO_FLOW_KEYS] = "flow_keys", + [PTR_TO_SOCKET] = "sock", + [PTR_TO_SOCK_COMMON] = "sock_common", + [PTR_TO_TCP_SOCK] = "tcp_sock", + [PTR_TO_TP_BUFFER] = "tp_buffer", + [PTR_TO_XDP_SOCK] = "xdp_sock",Review
Quoted 10 lines
(quoted diff) +10
-0+ [PTR_TO_BTF_ID] = "ptr_", + [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", + [PTR_TO_MEM] = "mem", + [PTR_TO_RDONLY_BUF] = "rdonly_buf", + [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_FUNC] = "func", + [PTR_TO_MAP_KEY] = "map_key", + }; + + return str[base_type(type)];well... now we lose the "
_or_null" part, that can be pretty important. Same will happen with RDONLY flag, right?Quoted 5 lines
Quoted 2 lines
What can we do about that?
We can format the string into a global buffer and return the buffer to the caller. A little overhead on string formatting.
Can't use global buffer, because multiple BPF verifications can proceed at the same time.
Quoted 7 lines
Quoted 4 lines
I think
reg_type_str()can still return a const char string with flags.
It's not going to be a direct str[base_type(type)]; of course. The str[] would be different.Sorry I didn't fully grasp this comment. Following is my understanding and thoughts so far. Let me know if I missed anything.
I meant that for now we can do:
static const char * const str[] = { [NOT_INIT] = "?", [PTR_TO_RDONLY_BUF] = "rdonly_buf", };switch(type) {
casePTR_TO_RDONLY_BUF| MAYBE_NULL:return "rdonly_buf_or_null";
default: return str[base_type(type)];
}Quoted 15 lines
Right, we can return a char string, but the string must be provided by the caller, which is the annoying part. We could do something as following (solution 1)
const char *reg_type_str(..., char *buf)
{
...
snprintf(buf, ...);
return buf;
}OR, (solution 2) we may embed a buffer in
bpf_verifier_envand pass env intoreg_type_str().reg_type_str()just returns the buffer in env. This doesn't require the caller to prepare the buffer.Quoted 6 lines
const char *reg_type_str(..., env)
{
...
snprintf(env->buf, ...);
returnenv->buf;
}If we go with this approach then passing 'env' is a bit better, since 'buf' doesn't need to be allocated on stack.
Quoted 4 lines
However, there will be a caveat when there are more than one
reg_type_strin one verbose statement. In solution 1, the input buf has to be different. In solution 2, we just can't do that, because the buffer is implicitly shared. What do you think about solution 2?There is only one
verbose()with two reg_type_str[] right now. It can easily be split into twoverbose().Quoted 5 lines
Quoted 4 lines
If that doesn't work out we can try:
s/verbose(,"%s", reg_type_str[])
/verbose(, "%s%s",reg_type_str(),reg_type_flags_str())Quoted 7 lines
This is probably more cumbersome IMHO.
Thinking about the implementation of
reg_type_flags_str(). Because flags can be combined, I think it would be better to format a dynamic buffer, then we are back to the same problem: caller needs to pass a buffer. Of course, with only two flags right now, we could enumerate all flag combinations and map them to const strings.Quoted 5 lines
Yeah. With 3 or more flags that can be combined the const char approach wouldn't scale. So let's try 'env' approach?
The only tweak is that 'log' would be better than 'env'. That temp buf can be in struct
bpf_verifier_log*log.Another idea would be to add bpf specific modifiers in
pointer()inlib/vsprintf.cfor verifier'sreg_state,reg_type, and other bpf structs.
It might makeprint_verifier_state()much cleaner and would allow easy printing of reg state from anywhere inside the verifier code.
Currently the error print has minimal info. Like:
verbose(env, "R%d invalid %s access off=%d size=%d\n",
Quoted 7 lines
regno, reg_type_str[reg->type], off, size); With vsnprintf support we could do something like: verbose(env, "%Brp invalid access off=%d size=%d\n", reg, off, size);and
pointer()will do full print of structbpf_reg_state.This is probably something to consider long term.
ACK. Thanks for the comments. The 'env' proposal looks reasonably well. I sent out v2. Please take a look there.
PATCH 5/9 bpf: Introduce MEM_RDONLY flag
In reply to Hao LuoThis patch introduce a flag MEM_RDONLY to tag a reg value pointing to 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, };/* Max number of base types. */
@@ -491,8 +494,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
@@ -4945,8 +4945,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, type =base_type (ctx_arg_info->reg_type); flag =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.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 5/9] bpf: Introduce MEM_RDONLY flag
Quoted 17 lines · Hao Luo
This patch introduce a flag
MEM_RDONLYto tag a reg value pointing to 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_RDONLYSigned-off-by Hao Luo <[email protected]>--- 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(-)
[...]
kernel/bpf/verifier.c static const char *reg_type_str(enum bpf_reg_type type) · line 549
Quoted 10 lines
static bool
arg_type_may_be_refcounted(enum bpf_arg_type type){ return type == ARG_PTR_TO_SOCK_COMMON;kernel/bpf/verifier.c +1
-2@@ -541,8 +546,7 @@ static const char *reg_type_str(enum bpf_reg_type type) [PTR_TO_BTF_ID] = "ptr_", [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", [PTR_TO_MEM] = "mem", - [PTR_TO_RDONLY_BUF] = "rdonly_buf", - [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_BUF] = "rdwr_buf",
super misleading if it's actually read-only...
Quoted 3 lines
kernel/bpf/verifier.c +0
-0[PTR_TO_FUNC] = "func", [PTR_TO_MAP_KEY] = "map_key", };
[...]
Re: [PATCH bpf-next v1 5/9] bpf: Introduce MEM_RDONLY flag
Quoted 22 lines · Andrii Nakryiko
Quoted 17 lines · Hao Luo
This patch introduce a flag
MEM_RDONLYto tag a reg value pointing to 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_RDONLYSigned-off-by Hao Luo <[email protected]>--- 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(-)[...]
Quoted 13 lines
kernel/bpf/verifier.c static const char *reg_type_str(enum bpf_reg_type type) · line 549
Quoted 10 lines
static bool
arg_type_may_be_refcounted(enum bpf_arg_type type){ return type == ARG_PTR_TO_SOCK_COMMON;kernel/bpf/verifier.c +1
-2@@ -541,8 +546,7 @@ static const char *reg_type_str(enum bpf_reg_type type) [PTR_TO_BTF_ID] = "ptr_", [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", [PTR_TO_MEM] = "mem", - [PTR_TO_RDONLY_BUF] = "rdonly_buf", - [PTR_TO_RDWR_BUF] = "rdwr_buf", + [PTR_TO_BUF] = "rdwr_buf",super misleading if it's actually read-only...
True. :)
I will have it fixed in v2. See the reply on patch 4/9 for a potential solution.
Quoted 5 lines
Quoted 3 lines
kernel/bpf/verifier.c +0
-0[PTR_TO_FUNC] = "func", [PTR_TO_MAP_KEY] = "map_key", };[...]
PATCH 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
@@ -506,7 +506,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
@@ -5860,7 +5860,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
@@ -13303,7 +13303,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 (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.1.400.ga245620fadb-goog
PATCH 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. Previously these two helpers return PTR_OT_MEM for kernel objects of scalar type, which allows one to directly modify the memory. Now with RDONLY_MEM tagging, the verifier will reject programs that writes into RDONLY_MEM.
--- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 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 +28 -5
@@ -4296,16 +4296,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 (base_type (reg->type) ==PTR_TO_MEM ) { +bool rdonly_mem =type_is_rdonly_mem (reg->type); + +if (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 (!err && value_regno >=0 ) +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 ;
@@ -6534,6 +6550,13 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn regs[BPF_REG_0 ].type =PTR_TO_MEM | ret_flag; regs[BPF_REG_0 ].mem_size = tsize; }else { +/* MEM_RDONLY may be carried from ret_flag, but it +* doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise +* it will confuse the check of PTR_TO_BTF_ID in +* check_mem_access(). +*/ + ret_flag &= ~MEM_RDONLY ; + regs[BPF_REG_0 ].type =PTR_TO_BTF_ID | ret_flag; regs[BPF_REG_0 ].btf = meta.ret_btf; regs[BPF_REG_0 ].btf_id = meta.ret_btf_id;
@@ -9335,7 +9358,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 (base_type (dst_reg->type)) {case PTR_TO_MEM : dst_reg->mem_size = aux->btf_var.mem_size;break ;
@@ -11479,7 +11502,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.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 7/9] bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
Quoted 44 lines · Hao Luo
Tag 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. Previously these two helpers returnPTR_OT_MEMfor kernel objects of scalar type, which allows one to directly modify the memory. Now withRDONLY_MEMtagging, the verifier will reject programs that writes intoRDONLY_MEM.Fixes 63d9b80dcf2c ("bpf: Introduce bpf_this_cpu_ptr()")Fixes eaa6bcb71ef6 ("bpf: Introduce bpf_per_cpu_ptr()")Fixes 4976b718c355 ("bpf: Introduce pseudo_btf_id")Signed-off-by Hao Luo <[email protected]>--- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 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 +0
-0
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4304
Quoted 10 lines
kernel/bpf/verifier.c +6
-1@@ -4296,16 +4296,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 (base_type (reg->type) ==PTR_TO_MEM ) { +bool rdonly_mem =type_is_rdonly_mem (reg->type); + +if (type_may_be_null (reg->type)) { +verbose (env,"R%d invalid mem access '%s'\n" , regno, +reg_type_str (reg->type));
see, here you'll get "invalid mem access 'ptr_to_mem'" while it's actually ptr_to_mem_or_null. Like verifier logs are not hard enough to follow, now they will be also misleading.
Quoted 11 lines
kernel/bpf/verifier.c +9
-0+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)) {
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4323
Quoted 10 lines
kernel/bpf/verifier.c +3
-2verbose (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 (!err && value_regno >=0 ) +if (t ==BPF_READ || rdonly_mem)
why two nested ifs for one condition?
Quoted 4 lines
kernel/bpf/verifier.c +1
-0+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 ;
[...]
Re: [PATCH bpf-next v1 7/9] bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
Quoted 52 lines · Andrii Nakryiko
Quoted 50 lines · Hao Luo
Tag 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. Previously these two helpers returnPTR_OT_MEMfor kernel objects of scalar type, which allows one to directly modify the memory. Now withRDONLY_MEMtagging, the verifier will reject programs that writes intoRDONLY_MEM.Fixes 63d9b80dcf2c ("bpf: Introduce bpf_this_cpu_ptr()")Fixes eaa6bcb71ef6 ("bpf: Introduce bpf_per_cpu_ptr()")Fixes 4976b718c355 ("bpf: Introduce pseudo_btf_id")Signed-off-by Hao Luo <[email protected]>--- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 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 +2
-1@@ -4296,16 +4296,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 (base_type (reg->type) ==PTR_TO_MEM ) { +bool rdonly_mem =type_is_rdonly_mem (reg->type);
Quoted 9 lines
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4304
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+ +if (type_may_be_null (reg->type)) { +verbose (env,"R%d invalid mem access '%s'\n" , regno, +reg_type_str (reg->type));see, here you'll get "invalid mem access '
ptr_to_mem'" while it's actuallyptr_to_mem_or_null. Like verifier logs are not hard enough to follow, now they will be also misleading.
I think formatting string inside reg_type_str() can have this problem solved, preserving the previous behavior. I'll try that in v2.
Quoted 15 lines
Quoted 15 lines
kernel/bpf/verifier.c +10
-0+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 ; } +
Quoted 9 lines
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4323
Quoted 6 lines
kernel/bpf/verifier.c +2
-2err =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 (!err && value_regno >=0 ) +if (t ==BPF_READ || rdonly_mem)why two nested ifs for one condition?
No particular reason. I think it helped me understand the logic better. But I'm fine with combining them into one 'if'.
Quoted 6 lines
Quoted 4 lines
kernel/bpf/verifier.c +1
-0+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 ;[...]
Re: [PATCH bpf-next v1 7/9] bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
Quoted 6 lines · Hao Luo
Quoted 3 lines · Andrii Nakryiko
Quoted 1 line · Hao Luo
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Tag 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. Previously these two helpers returnPTR_OT_MEMfor kernel objects of scalar type, which allows one to directly modify the memory. Now withRDONLY_MEMtagging, the verifier will reject programs that writes intoRDONLY_MEM.Fixes 63d9b80dcf2c ("bpf: Introduce bpf_this_cpu_ptr()")
BTW, our tooling complained about this one because in reality the subject of the patch has a typo: "bpf: Introducte bpf_this_cpu_ptr()", please fix as well (that is, re-introduce the typo :) )
Quoted 79 lines
Quoted 41 lines
Quoted 41 lines
Fixes eaa6bcb71ef6 ("bpf: Introduce bpf_per_cpu_ptr()")Fixes 4976b718c355 ("bpf: Introduce pseudo_btf_id")Signed-off-by Hao Luo <[email protected]>--- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 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 +2
-1@@ -4296,16 +4296,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 (base_type (reg->type) ==PTR_TO_MEM ) { +bool rdonly_mem =type_is_rdonly_mem (reg->type);Quoted 9 lines
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4304
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+ +if (type_may_be_null (reg->type)) { +verbose (env,"R%d invalid mem access '%s'\n" , regno, +reg_type_str (reg->type));see, here you'll get "invalid mem access '
ptr_to_mem'" while it's actuallyptr_to_mem_or_null. Like verifier logs are not hard enough to follow, now they will be also misleading.I think formatting string inside
reg_type_str()can have this problem solved, preserving the previous behavior. I'll try that in v2.Quoted 15 lines
Quoted 15 lines
kernel/bpf/verifier.c +10
-0+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 ; } +Quoted 9 lines
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4323
Quoted 6 lines
kernel/bpf/verifier.c +2
-2err =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 (!err && value_regno >=0 ) +if (t ==BPF_READ || rdonly_mem)why two nested ifs for one condition?
Quoted 2 lines
No particular reason. I think it helped me understand the logic better. But I'm fine with combining them into one 'if'.
Personally two nested ifs are way harder to follow as it implies that there is some other sub-condition, while in reality it's one longer condition.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
kernel/bpf/verifier.c +1
-0+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 ;[...]
Re: [PATCH bpf-next v1 7/9] bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
Quoted 15 lines · Andrii Nakryiko
Quoted 13 lines · Hao Luo
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Hao Luo
Tag 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. Previously these two helpers returnPTR_OT_MEMfor kernel objects of scalar type, which allows one to directly modify the memory. Now withRDONLY_MEMtagging, the verifier will reject programs that writes intoRDONLY_MEM.
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Fixes 63d9b80dcf2c ("bpf: Introduce bpf_this_cpu_ptr()")BTW, our tooling complained about this one because in reality the subject of the patch has a typo: "bpf: Introducte
bpf_this_cpu_ptr()", please fix as well (that is, re-introduce the typo :) )
Ah, yes, thanks for the notice :). I do see that typo after sending out this version. I have it fixed in my local repo already.
Quoted 94 lines
Quoted 79 lines
Quoted 41 lines
Quoted 41 lines
Fixes eaa6bcb71ef6 ("bpf: Introduce bpf_per_cpu_ptr()")Fixes 4976b718c355 ("bpf: Introduce pseudo_btf_id")Signed-off-by Hao Luo <[email protected]>--- kernel/bpf/helpers.c | 4 ++-- kernel/bpf/verifier.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 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 +2
-1@@ -4296,16 +4296,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 (base_type (reg->type) ==PTR_TO_MEM ) { +bool rdonly_mem =type_is_rdonly_mem (reg->type);Quoted 9 lines
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4304
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+ +if (type_may_be_null (reg->type)) { +verbose (env,"R%d invalid mem access '%s'\n" , regno, +reg_type_str (reg->type));see, here you'll get "invalid mem access '
ptr_to_mem'" while it's actuallyptr_to_mem_or_null. Like verifier logs are not hard enough to follow, now they will be also misleading.I think formatting string inside
reg_type_str()can have this problem solved, preserving the previous behavior. I'll try that in v2.Quoted 15 lines
Quoted 15 lines
kernel/bpf/verifier.c +10
-0+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 ; } +Quoted 9 lines
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 4323
Quoted 6 lines
kernel/bpf/verifier.c +2
-2err =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 (!err && value_regno >=0 ) +if (t ==BPF_READ || rdonly_mem)why two nested ifs for one condition?
Quoted 2 lines
No particular reason. I think it helped me understand the logic better. But I'm fine with combining them into one 'if'.
Personally two nested ifs are way harder to follow as it implies that there is some other sub-condition, while in reality it's one longer condition.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
kernel/bpf/verifier.c +1
-0+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 ;[...]
PATCH 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 bpf_args compatible with immutable memory with MEM_RDONLY flag. The arguments that don't have this flag will be only compatible with mutable memory types, preventing the helper from modifying a read-only memory. The bpf_args that have MEM_RDONLY are compatible with both mutable memory and immutable memory.
--- include/linux/bpf.h | 4 ++- 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 | 14 +++++++-- kernel/trace/bpf_trace.c | 26 ++++++++-------- net/core/filter.c | 64 ++++++++++++++++++++-------------------- 9 files changed, 67 insertions(+), 57 deletions(-)
include/linux/bpf.h +3 -1
@@ -311,7 +311,9 @@ enum bpf_type_flag {/* PTR may be NULL. */ PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), -/* MEM is read-only. */ +/* MEM is read-only. When applied on bpf_arg, it indicates the arg is +* compatible with both mutable and immutable memory. +*/ MEM_RDONLY =BIT (1 +BPF_BASE_TYPE_BITS ), __BPF_TYPE_LAST_FLAG = MEM_RDONLY,
kernel/bpf/btf.c +1 -1
@@ -6350,7 +6350,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 +11 -3
@@ -4966,6 +4966,7 @@ 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;
@@ -5012,7 +5013,6 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, };
@@ -5074,6 +5074,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno];enum bpf_reg_type expected, type = reg->type;const struct bpf_reg_types *compatible; +u32 compatible_flags;int i, j; compatible = compatible_reg_types[base_type (arg_type)];
@@ -5082,6 +5083,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EFAULT ; } +/* If arg_type is tagged with MEM_RDONLY, it's compatible with both +* RDONLY and non-RDONLY reg values. Therefore fold this flag before +* comparison. PTR_MAYBE_NULL is similar. +*/ + compatible_flags = arg_type & (MEM_RDONLY |PTR_MAYBE_NULL ); + type &= ~compatible_flags; +for (i =0 ; i <ARRAY_SIZE (compatible->types); i++) { expected = compatible->types[i];if (expected ==NOT_INIT )
@@ -5091,14 +5099,14 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, goto found; } -verbose (env,"R%d type=%s expected=" , regno,reg_type_str (type)); +verbose (env,"R%d type=%s expected=" , regno,reg_type_str (reg->type));for (j =0 ; j +1 < i; j++)verbose (env,"%s, " ,reg_type_str (compatible->types[j]));verbose (env,"%s\n" ,reg_type_str (compatible->types[j]));return -EACCES ; found: -if (type ==PTR_TO_BTF_ID ) { +if (reg->type ==PTR_TO_BTF_ID ) {if (!arg_btf_id) {if (!compatible->btf_id) {verbose (env,"verifier internal error: missing arg compatible BTF ID\n" );
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.1.400.ga245620fadb-goog
Re: [PATCH bpf-next v1 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 140 lines · Hao Luo
Some helper functions may modify its arguments, for example,
bpf_d_path,bpf_get_stacketc. Previously, their argument types were marked asARG_PTR_TO_MEM, which is compatible with read-only mem types, such asPTR_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
bpf_argscompatible with immutable memory withMEM_RDONLYflag. The arguments that don't have this flag will be only compatible with mutable memory types, preventing the helper from modifying a read-only memory. Thebpf_argsthat haveMEM_RDONLYare compatible with both mutable memory and immutable memory.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 4 ++- 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 | 14 +++++++-- kernel/trace/bpf_trace.c | 26 ++++++++-------- net/core/filter.c | 64 ++++++++++++++++++++-------------------- 9 files changed, 67 insertions(+), 57 deletions(-)include/linux/bpf.h +3
-1@@ -311,7 +311,9 @@ enum bpf_type_flag {/* PTR may be NULL. */ PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), -/* MEM is read-only. */ +/* MEM is read-only. When applied on bpf_arg, it indicates the arg is +* compatible with both mutable and immutable memory. +*/ MEM_RDONLY =BIT (1 +BPF_BASE_TYPE_BITS ), __BPF_TYPE_LAST_FLAG = MEM_RDONLY,kernel/bpf/btf.c +1
-1@@ -6350,7 +6350,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 +0
-0
kernel/bpf/verifier.c static int resolve_map_arg_type(struct bpf_verifier_env *env, · line 4969
Quoted 4 lines
kernel/bpf/verifier.c +1
-0@@ -4966,6 +4966,7 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,return 0 ; } +
nit: unnecessary extra empty line?
Quoted 20 lines
kernel/bpf/verifier.c +1
-1struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id;@@ -5012,7 +5013,6 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, };@@ -5074,6 +5074,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno];enum bpf_reg_type expected, type = reg->type;const struct bpf_reg_types *compatible; +u32 compatible_flags;int i, j; compatible = compatible_reg_types[base_type (arg_type)];
kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5092
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -5082,6 +5083,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EFAULT ; } +/* If arg_type is tagged with MEM_RDONLY, it's compatible with both +* RDONLY and non-RDONLY reg values. Therefore fold this flag before +* comparison. PTR_MAYBE_NULL is similar. +*/ + compatible_flags = arg_type & (MEM_RDONLY |PTR_MAYBE_NULL ); + type &= ~compatible_flags; +
wouldn't
type &= ~MEM_RDONLY; /* clear read-only flag, if any */ type &= ~PTR_MAYBE_NULL; /* clear nullable flag, if any */
be cleaner and more straightforward?
Quoted 3 lines
kernel/bpf/verifier.c +0
-0for (i =0 ; i <ARRAY_SIZE (compatible->types); i++) { expected = compatible->types[i];if (expected ==NOT_INIT )
[...]
Re: [PATCH bpf-next v1 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 30 lines · Andrii Nakryiko
Quoted 28 lines · Hao Luo
Some helper functions may modify its arguments, for example,
bpf_d_path,bpf_get_stacketc. Previously, their argument types were marked asARG_PTR_TO_MEM, which is compatible with read-only mem types, such asPTR_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
bpf_argscompatible with immutable memory withMEM_RDONLYflag. The arguments that don't have this flag will be only compatible with mutable memory types, preventing the helper from modifying a read-only memory. Thebpf_argsthat haveMEM_RDONLYare compatible with both mutable memory and immutable memory.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 4 ++- 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 | 14 +++++++-- kernel/trace/bpf_trace.c | 26 ++++++++-------- net/core/filter.c | 64 ++++++++++++++++++++-------------------- 9 files changed, 67 insertions(+), 57 deletions(-)
[...]
Quoted 9 lines
Quoted 9 lines
kernel/bpf/verifier.c +0
-0@@ -4966,6 +4966,7 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,return 0 ; }
Quoted 4 lines
kernel/bpf/verifier.c static int resolve_map_arg_type(struct bpf_verifier_env *env, · line 4969
Quoted 1 line
kernel/bpf/verifier.c +1
-0+nit: unnecessary extra empty line?
Ack.
Quoted 33 lines
Quoted 20 lines
kernel/bpf/verifier.c +1
-1struct bpf_reg_types {const enum bpf_reg_type types[10 ];u32 *btf_id;@@ -5012,7 +5013,6 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_VALUE,PTR_TO_MEM , PTR_TO_BUF, - PTR_TO_BUF | MEM_RDONLY, }, };@@ -5074,6 +5074,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno];enum bpf_reg_type expected, type = reg->type;const struct bpf_reg_types *compatible; +u32 compatible_flags;int i, j; compatible = compatible_reg_types[base_type (arg_type)];kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5092
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -5082,6 +5083,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EFAULT ; } +/* If arg_type is tagged with MEM_RDONLY, it's compatible with both +* RDONLY and non-RDONLY reg values. Therefore fold this flag before +* comparison. PTR_MAYBE_NULL is similar. +*/ + compatible_flags = arg_type & (MEM_RDONLY |PTR_MAYBE_NULL ); + type &= ~compatible_flags; +wouldn't
Quoted 6 lines
type &= ~MEM_RDONLY; /* clear read-only flag, if any */ type &= ~PTR_MAYBE_NULL; /* clear nullable flag, if any */
be cleaner and more straightforward?
No problem. Sounds good to me.
Quoted 5 lines
Quoted 3 lines
kernel/bpf/verifier.c +0
-0for (i =0 ; i <ARRAY_SIZE (compatible->types); i++) { expected = compatible->types[i];if (expected ==NOT_INIT )[...]
Re: [PATCH bpf-next v1 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 33 lines · Hao Luo
Quoted 30 lines · Andrii Nakryiko
Quoted 28 lines · Hao Luo
Some helper functions may modify its arguments, for example,
bpf_d_path,bpf_get_stacketc. Previously, their argument types were marked asARG_PTR_TO_MEM, which is compatible with read-only mem types, such asPTR_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
bpf_argscompatible with immutable memory withMEM_RDONLYflag. The arguments that don't have this flag will be only compatible with mutable memory types, preventing the helper from modifying a read-only memory. Thebpf_argsthat haveMEM_RDONLYare compatible with both mutable memory and immutable memory.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 4 ++- 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 | 14 +++++++-- kernel/trace/bpf_trace.c | 26 ++++++++-------- net/core/filter.c | 64 ++++++++++++++++++++-------------------- 9 files changed, 67 insertions(+), 57 deletions(-)
[...]
Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
kernel/bpf/verifier.c +1
-0@@ -5074,6 +5074,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno];enum bpf_reg_type expected, type = reg->type;const struct bpf_reg_types *compatible; +u32 compatible_flags;int i, j; compatible = compatible_reg_types[base_type (arg_type)];kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5092
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -5082,6 +5083,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EFAULT ; } +/* If arg_type is tagged with MEM_RDONLY, it's compatible with both +* RDONLY and non-RDONLY reg values. Therefore fold this flag before +* comparison. PTR_MAYBE_NULL is similar. +*/ + compatible_flags = arg_type & (MEM_RDONLY |PTR_MAYBE_NULL ); + type &= ~compatible_flags; +wouldn't
Quoted 9 lines
Quoted 6 lines
type &= ~MEM_RDONLY; /* clear read-only flag, if any */ type &= ~PTR_MAYBE_NULL; /* clear nullable flag, if any */
be cleaner and more straightforward?
No problem. Sounds good to me.
I just realized the suggested transformation is wrong. Whether to fold the flag depends on whether arg_type has the flag. So it should instead be
if (arg_type & MEM_RDONLY)
type &= ~MEM_RDONLY;
or
type &= ~(arg_type & MEM_RDONLY);
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
kernel/bpf/verifier.c +0
-0for (i =0 ; i <ARRAY_SIZE (compatible->types); i++) { expected = compatible->types[i];if (expected ==NOT_INIT )[...]
Re: [PATCH bpf-next v1 8/9] bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
Quoted 72 lines · Hao Luo
Quoted 33 lines · Hao Luo
Quoted 30 lines · Andrii Nakryiko
Quoted 28 lines · Hao Luo
Some helper functions may modify its arguments, for example,
bpf_d_path,bpf_get_stacketc. Previously, their argument types were marked asARG_PTR_TO_MEM, which is compatible with read-only mem types, such asPTR_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
bpf_argscompatible with immutable memory withMEM_RDONLYflag. The arguments that don't have this flag will be only compatible with mutable memory types, preventing the helper from modifying a read-only memory. Thebpf_argsthat haveMEM_RDONLYare compatible with both mutable memory and immutable memory.Signed-off-by Hao Luo <[email protected]>--- include/linux/bpf.h | 4 ++- 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 | 14 +++++++-- kernel/trace/bpf_trace.c | 26 ++++++++-------- net/core/filter.c | 64 ++++++++++++++++++++-------------------- 9 files changed, 67 insertions(+), 57 deletions(-)[...]
Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
kernel/bpf/verifier.c +1
-0@@ -5074,6 +5074,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno];enum bpf_reg_type expected, type = reg->type;const struct bpf_reg_types *compatible; +u32 compatible_flags;int i, j; compatible = compatible_reg_types[base_type (arg_type)];kernel/bpf/verifier.c static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · line 5092
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -5082,6 +5083,13 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,return -EFAULT ; } +/* If arg_type is tagged with MEM_RDONLY, it's compatible with both +* RDONLY and non-RDONLY reg values. Therefore fold this flag before +* comparison. PTR_MAYBE_NULL is similar. +*/ + compatible_flags = arg_type & (MEM_RDONLY |PTR_MAYBE_NULL ); + type &= ~compatible_flags; +wouldn't
Quoted 9 lines
Quoted 6 lines
type &= ~MEM_RDONLY; /* clear read-only flag, if any */ type &= ~PTR_MAYBE_NULL; /* clear nullable flag, if any */
be cleaner and more straightforward?
No problem. Sounds good to me.
I just realized the suggested transformation is wrong. Whether to fold the flag depends on whether
arg_typehas the flag. So it should instead be
Quoted 6 lines
if (
arg_type&MEM_RDONLY)
type &= ~MEM_RDONLY;or
type &= ~(arg_type &
MEM_RDONLY);
You are totally right. I think this deserves a big verbose comment explaining that:
ARG_PTR_TO_MEM+RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM+RDONLY, but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM+RDONLY
Same for MAYBE_NULL:
ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM+MAYBE_NULL, butARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM+MAYBE_NULL
It might not be true for other future modifiers, so I'd do each of RDONLY and MAYBE_NULL with a separate if and comment.
Good catch, btw! (but hopefully selftests would have caught this? if not, we need better tests)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0for (i =0 ; i <ARRAY_SIZE (compatible->types); i++) { expected = compatible->types[i];if (expected ==NOT_INIT )[...]
PATCH 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.1.400.ga245620fadb-goog