Skip to content

Commit 1a5faae

Browse files
bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
jira VULN-140 pre-cve CVE-2022-23222 commit-author Hao Luo <[email protected]> commit 48946bd We 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'. Signed-off-by: Hao Luo <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] (cherry picked from commit 48946bd) Signed-off-by: Pratham Patel <[email protected]>
1 parent 558ddf8 commit 1a5faae

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

include/linux/bpf.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,11 @@ enum bpf_arg_type {
311311
ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
312312
ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
313313
ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
314-
ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
315314

316315
/* the following constraints used to prototype bpf_memcmp() and other
317316
* functions that access data on eBPF program stack
318317
*/
319318
ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
320-
ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
321319
ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
322320
* helper function must fill all bytes or clear
323321
* them in error case.
@@ -327,25 +325,30 @@ enum bpf_arg_type {
327325
ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
328326

329327
ARG_PTR_TO_CTX, /* pointer to context */
330-
ARG_PTR_TO_CTX_OR_NULL, /* pointer to context or NULL */
331328
ARG_ANYTHING, /* any (initialized) argument is ok */
332329
ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
333330
ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
334331
ARG_PTR_TO_INT, /* pointer to int */
335332
ARG_PTR_TO_LONG, /* pointer to long */
336333
ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
337-
ARG_PTR_TO_SOCKET_OR_NULL, /* pointer to bpf_sock (fullsock) or NULL */
338334
ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
339335
ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */
340-
ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */
341336
ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
342337
ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
343338
ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
344339
ARG_PTR_TO_FUNC, /* pointer to a bpf program function */
345-
ARG_PTR_TO_STACK_OR_NULL, /* pointer to stack or NULL */
340+
ARG_PTR_TO_STACK, /* pointer to stack */
346341
ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
347342
__BPF_ARG_TYPE_MAX,
348343

344+
/* Extended arg_types. */
345+
ARG_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
346+
ARG_PTR_TO_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
347+
ARG_PTR_TO_CTX_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
348+
ARG_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
349+
ARG_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_ALLOC_MEM,
350+
ARG_PTR_TO_STACK_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
351+
349352
/* This must be the last entry. Its purpose is to ensure the enum is
350353
* wide enough to hold the higher bits reserved for bpf_type_flag.
351354
*/

kernel/bpf/verifier.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,9 @@ static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
479479
return type == ARG_PTR_TO_SOCK_COMMON;
480480
}
481481

482-
static bool arg_type_may_be_null(enum bpf_arg_type type)
482+
static bool type_may_be_null(u32 type)
483483
{
484-
return type == ARG_PTR_TO_MAP_VALUE_OR_NULL ||
485-
type == ARG_PTR_TO_MEM_OR_NULL ||
486-
type == ARG_PTR_TO_CTX_OR_NULL ||
487-
type == ARG_PTR_TO_SOCKET_OR_NULL ||
488-
type == ARG_PTR_TO_ALLOC_MEM_OR_NULL ||
489-
type == ARG_PTR_TO_STACK_OR_NULL;
484+
return type & PTR_MAYBE_NULL;
490485
}
491486

492487
/* Determine whether the function releases some resources allocated by another
@@ -4645,9 +4640,8 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno,
46454640

46464641
static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
46474642
{
4648-
return type == ARG_PTR_TO_MEM ||
4649-
type == ARG_PTR_TO_MEM_OR_NULL ||
4650-
type == ARG_PTR_TO_UNINIT_MEM;
4643+
return base_type(type) == ARG_PTR_TO_MEM ||
4644+
base_type(type) == ARG_PTR_TO_UNINIT_MEM;
46514645
}
46524646

46534647
static bool arg_type_is_mem_size(enum bpf_arg_type type)
@@ -4780,31 +4774,26 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
47804774
[ARG_PTR_TO_MAP_KEY] = &map_key_value_types,
47814775
[ARG_PTR_TO_MAP_VALUE] = &map_key_value_types,
47824776
[ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types,
4783-
[ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types,
47844777
[ARG_CONST_SIZE] = &scalar_types,
47854778
[ARG_CONST_SIZE_OR_ZERO] = &scalar_types,
47864779
[ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types,
47874780
[ARG_CONST_MAP_PTR] = &const_map_ptr_types,
47884781
[ARG_PTR_TO_CTX] = &context_types,
4789-
[ARG_PTR_TO_CTX_OR_NULL] = &context_types,
47904782
[ARG_PTR_TO_SOCK_COMMON] = &sock_types,
47914783
#ifdef CONFIG_NET
47924784
[ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types,
47934785
#endif
47944786
[ARG_PTR_TO_SOCKET] = &fullsock_types,
4795-
[ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types,
47964787
[ARG_PTR_TO_BTF_ID] = &btf_ptr_types,
47974788
[ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types,
47984789
[ARG_PTR_TO_MEM] = &mem_types,
4799-
[ARG_PTR_TO_MEM_OR_NULL] = &mem_types,
48004790
[ARG_PTR_TO_UNINIT_MEM] = &mem_types,
48014791
[ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types,
4802-
[ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types,
48034792
[ARG_PTR_TO_INT] = &int_ptr_types,
48044793
[ARG_PTR_TO_LONG] = &int_ptr_types,
48054794
[ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types,
48064795
[ARG_PTR_TO_FUNC] = &func_ptr_types,
4807-
[ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types,
4796+
[ARG_PTR_TO_STACK] = &stack_ptr_types,
48084797
[ARG_PTR_TO_CONST_STR] = &const_str_ptr_types,
48094798
};
48104799

@@ -4817,7 +4806,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
48174806
const struct bpf_reg_types *compatible;
48184807
int i, j;
48194808

4820-
compatible = compatible_reg_types[arg_type];
4809+
compatible = compatible_reg_types[base_type(arg_type)];
48214810
if (!compatible) {
48224811
verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type);
48234812
return -EFAULT;
@@ -4898,15 +4887,14 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
48984887
return -EACCES;
48994888
}
49004889

4901-
if (arg_type == ARG_PTR_TO_MAP_VALUE ||
4902-
arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
4903-
arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
4890+
if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
4891+
base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) {
49044892
err = resolve_map_arg_type(env, meta, &arg_type);
49054893
if (err)
49064894
return err;
49074895
}
49084896

4909-
if (register_is_null(reg) && arg_type_may_be_null(arg_type))
4897+
if (register_is_null(reg) && type_may_be_null(arg_type))
49104898
/* A NULL register has a SCALAR_VALUE type, so skip
49114899
* type checking.
49124900
*/
@@ -4953,10 +4941,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49534941
err = check_helper_mem_access(env, regno,
49544942
meta->map_ptr->key_size, false,
49554943
NULL);
4956-
} else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
4957-
(arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
4958-
!register_is_null(reg)) ||
4959-
arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
4944+
} else if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
4945+
base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) {
4946+
if (type_may_be_null(arg_type) && register_is_null(reg))
4947+
return 0;
4948+
49604949
/* bpf_map_xxx(..., map_ptr, ..., value) call:
49614950
* check [value, value + map->value_size) validity
49624951
*/

0 commit comments

Comments
 (0)