Skip to content

Commit dd6c760

Browse files
committed
bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
jira VULN-136 cve-pre CVE-2022-0500 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: Brett Mastbergen <[email protected]>
1 parent 753521e commit dd6c760

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
@@ -313,13 +313,11 @@ enum bpf_arg_type {
313313
ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
314314
ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
315315
ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
316-
ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
317316

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

331329
ARG_PTR_TO_CTX, /* pointer to context */
332-
ARG_PTR_TO_CTX_OR_NULL, /* pointer to context or NULL */
333330
ARG_ANYTHING, /* any (initialized) argument is ok */
334331
ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
335332
ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
336333
ARG_PTR_TO_INT, /* pointer to int */
337334
ARG_PTR_TO_LONG, /* pointer to long */
338335
ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
339-
ARG_PTR_TO_SOCKET_OR_NULL, /* pointer to bpf_sock (fullsock) or NULL */
340336
ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
341337
ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */
342-
ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */
343338
ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
344339
ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
345340
ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
346341
ARG_PTR_TO_FUNC, /* pointer to a bpf program function */
347-
ARG_PTR_TO_STACK_OR_NULL, /* pointer to stack or NULL */
342+
ARG_PTR_TO_STACK, /* pointer to stack */
348343
ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
349344
__BPF_ARG_TYPE_MAX,
350345

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

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
@@ -4647,9 +4642,8 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno,
46474642

46484643
static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
46494644
{
4650-
return type == ARG_PTR_TO_MEM ||
4651-
type == ARG_PTR_TO_MEM_OR_NULL ||
4652-
type == ARG_PTR_TO_UNINIT_MEM;
4645+
return base_type(type) == ARG_PTR_TO_MEM ||
4646+
base_type(type) == ARG_PTR_TO_UNINIT_MEM;
46534647
}
46544648

46554649
static bool arg_type_is_mem_size(enum bpf_arg_type type)
@@ -4782,31 +4776,26 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
47824776
[ARG_PTR_TO_MAP_KEY] = &map_key_value_types,
47834777
[ARG_PTR_TO_MAP_VALUE] = &map_key_value_types,
47844778
[ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types,
4785-
[ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types,
47864779
[ARG_CONST_SIZE] = &scalar_types,
47874780
[ARG_CONST_SIZE_OR_ZERO] = &scalar_types,
47884781
[ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types,
47894782
[ARG_CONST_MAP_PTR] = &const_map_ptr_types,
47904783
[ARG_PTR_TO_CTX] = &context_types,
4791-
[ARG_PTR_TO_CTX_OR_NULL] = &context_types,
47924784
[ARG_PTR_TO_SOCK_COMMON] = &sock_types,
47934785
#ifdef CONFIG_NET
47944786
[ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types,
47954787
#endif
47964788
[ARG_PTR_TO_SOCKET] = &fullsock_types,
4797-
[ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types,
47984789
[ARG_PTR_TO_BTF_ID] = &btf_ptr_types,
47994790
[ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types,
48004791
[ARG_PTR_TO_MEM] = &mem_types,
4801-
[ARG_PTR_TO_MEM_OR_NULL] = &mem_types,
48024792
[ARG_PTR_TO_UNINIT_MEM] = &mem_types,
48034793
[ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types,
4804-
[ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types,
48054794
[ARG_PTR_TO_INT] = &int_ptr_types,
48064795
[ARG_PTR_TO_LONG] = &int_ptr_types,
48074796
[ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types,
48084797
[ARG_PTR_TO_FUNC] = &func_ptr_types,
4809-
[ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types,
4798+
[ARG_PTR_TO_STACK] = &stack_ptr_types,
48104799
[ARG_PTR_TO_CONST_STR] = &const_str_ptr_types,
48114800
};
48124801

@@ -4819,7 +4808,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
48194808
const struct bpf_reg_types *compatible;
48204809
int i, j;
48214810

4822-
compatible = compatible_reg_types[arg_type];
4811+
compatible = compatible_reg_types[base_type(arg_type)];
48234812
if (!compatible) {
48244813
verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type);
48254814
return -EFAULT;
@@ -4900,15 +4889,14 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49004889
return -EACCES;
49014890
}
49024891

4903-
if (arg_type == ARG_PTR_TO_MAP_VALUE ||
4904-
arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
4905-
arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
4892+
if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
4893+
base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) {
49064894
err = resolve_map_arg_type(env, meta, &arg_type);
49074895
if (err)
49084896
return err;
49094897
}
49104898

4911-
if (register_is_null(reg) && arg_type_may_be_null(arg_type))
4899+
if (register_is_null(reg) && type_may_be_null(arg_type))
49124900
/* A NULL register has a SCALAR_VALUE type, so skip
49134901
* type checking.
49144902
*/
@@ -4955,10 +4943,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49554943
err = check_helper_mem_access(env, regno,
49564944
meta->map_ptr->key_size, false,
49574945
NULL);
4958-
} else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
4959-
(arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
4960-
!register_is_null(reg)) ||
4961-
arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
4946+
} else if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
4947+
base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) {
4948+
if (type_may_be_null(arg_type) && register_is_null(reg))
4949+
return 0;
4950+
49624951
/* bpf_map_xxx(..., map_ptr, ..., value) call:
49634952
* check [value, value + map->value_size) validity
49644953
*/

0 commit comments

Comments
 (0)