Skip to content

Commit d5f56c6

Browse files
bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
jira VULN-140 pre-cve CVE-2022-23222 commit-author Hao Luo <[email protected]> commit 3c48073 upstream-diff A merge confict arised because 3e8ce29 ("bpf: Prevent pointer mismatch in bpf_timer_init.") does not exist in our tree. We 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'. 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 3c48073) Signed-off-by: Pratham Patel <[email protected]>
1 parent 1a5faae commit d5f56c6

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

include/linux/bpf.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,22 @@ enum bpf_return_type {
361361
RET_INTEGER, /* function returns integer */
362362
RET_VOID, /* function doesn't return anything */
363363
RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
364-
RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
365-
RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
366-
RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
367-
RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
368-
RET_PTR_TO_ALLOC_MEM_OR_NULL, /* returns a pointer to dynamically allocated memory or NULL */
369-
RET_PTR_TO_BTF_ID_OR_NULL, /* returns a pointer to a btf_id or NULL */
370-
RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
364+
RET_PTR_TO_SOCKET, /* returns a pointer to a socket */
365+
RET_PTR_TO_TCP_SOCK, /* returns a pointer to a tcp_sock */
366+
RET_PTR_TO_SOCK_COMMON, /* returns a pointer to a sock_common */
367+
RET_PTR_TO_ALLOC_MEM, /* returns a pointer to dynamically allocated memory */
371368
RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
372369
RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
373370
__BPF_RET_TYPE_MAX,
374371

372+
/* Extended ret_types. */
373+
RET_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
374+
RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
375+
RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
376+
RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
377+
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
378+
RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
379+
375380
/* This must be the last entry. Its purpose is to ensure the enum is
376381
* wide enough to hold the higher bits reserved for bpf_type_flag.
377382
*/

kernel/bpf/helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
660660
const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
661661
.func = bpf_per_cpu_ptr,
662662
.gpl_only = false,
663-
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL,
663+
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL,
664664
.arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
665665
.arg2_type = ARG_ANYTHING,
666666
};

kernel/bpf/verifier.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5925,6 +5925,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
59255925
int *insn_idx_p)
59265926
{
59275927
const struct bpf_func_proto *fn = NULL;
5928+
enum bpf_return_type ret_type;
59285929
struct bpf_reg_state *regs;
59295930
struct bpf_call_arg_meta meta;
59305931
int insn_idx = *insn_idx_p;
@@ -6051,13 +6052,13 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
60516052
regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
60526053

60536054
/* update return register (already marked as written above) */
6054-
if (fn->ret_type == RET_INTEGER) {
6055+
ret_type = fn->ret_type;
6056+
if (ret_type == RET_INTEGER) {
60556057
/* sets type to SCALAR_VALUE */
60566058
mark_reg_unknown(env, regs, BPF_REG_0);
6057-
} else if (fn->ret_type == RET_VOID) {
6059+
} else if (ret_type == RET_VOID) {
60586060
regs[BPF_REG_0].type = NOT_INIT;
6059-
} else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
6060-
fn->ret_type == RET_PTR_TO_MAP_VALUE) {
6061+
} else if (base_type(ret_type) == RET_PTR_TO_MAP_VALUE) {
60616062
/* There is no offset yet applied, variable or fixed */
60626063
mark_reg_known_zero(env, regs, BPF_REG_0);
60636064
/* remember map_ptr, so that check_map_access()
@@ -6070,28 +6071,27 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
60706071
return -EINVAL;
60716072
}
60726073
regs[BPF_REG_0].map_ptr = meta.map_ptr;
6073-
if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
6074+
if (type_may_be_null(ret_type)) {
6075+
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
6076+
} else {
60746077
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
60756078
if (map_value_has_spin_lock(meta.map_ptr))
60766079
regs[BPF_REG_0].id = ++env->id_gen;
6077-
} else {
6078-
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
60796080
}
6080-
} else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
6081+
} else if (base_type(ret_type) == RET_PTR_TO_SOCKET) {
60816082
mark_reg_known_zero(env, regs, BPF_REG_0);
60826083
regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
6083-
} else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
6084+
} else if (base_type(ret_type) == RET_PTR_TO_SOCK_COMMON) {
60846085
mark_reg_known_zero(env, regs, BPF_REG_0);
60856086
regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
6086-
} else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
6087+
} else if (base_type(ret_type) == RET_PTR_TO_TCP_SOCK) {
60876088
mark_reg_known_zero(env, regs, BPF_REG_0);
60886089
regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
6089-
} else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) {
6090+
} else if (base_type(ret_type) == RET_PTR_TO_ALLOC_MEM) {
60906091
mark_reg_known_zero(env, regs, BPF_REG_0);
60916092
regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
60926093
regs[BPF_REG_0].mem_size = meta.mem_size;
6093-
} else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ||
6094-
fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) {
6094+
} else if (base_type(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) {
60956095
const struct btf_type *t;
60966096

60976097
mark_reg_known_zero(env, regs, BPF_REG_0);
@@ -6110,28 +6110,28 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
61106110
return -EINVAL;
61116111
}
61126112
regs[BPF_REG_0].type =
6113-
fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ?
6114-
PTR_TO_MEM : PTR_TO_MEM_OR_NULL;
6113+
(ret_type & PTR_MAYBE_NULL) ?
6114+
PTR_TO_MEM_OR_NULL : PTR_TO_MEM;
61156115
regs[BPF_REG_0].mem_size = tsize;
61166116
} else {
61176117
regs[BPF_REG_0].type =
6118-
fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ?
6119-
PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL;
6118+
(ret_type & PTR_MAYBE_NULL) ?
6119+
PTR_TO_BTF_ID_OR_NULL : PTR_TO_BTF_ID;
61206120
regs[BPF_REG_0].btf = meta.ret_btf;
61216121
regs[BPF_REG_0].btf_id = meta.ret_btf_id;
61226122
}
6123-
} else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL ||
6124-
fn->ret_type == RET_PTR_TO_BTF_ID) {
6123+
} else if (base_type(ret_type) == RET_PTR_TO_BTF_ID) {
61256124
int ret_btf_id;
61266125

61276126
mark_reg_known_zero(env, regs, BPF_REG_0);
6128-
regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ?
6129-
PTR_TO_BTF_ID :
6130-
PTR_TO_BTF_ID_OR_NULL;
6127+
regs[BPF_REG_0].type = (ret_type & PTR_MAYBE_NULL) ?
6128+
PTR_TO_BTF_ID_OR_NULL :
6129+
PTR_TO_BTF_ID;
61316130
ret_btf_id = *fn->ret_btf_id;
61326131
if (ret_btf_id == 0) {
6133-
verbose(env, "invalid return type %d of func %s#%d\n",
6134-
fn->ret_type, func_id_name(func_id), func_id);
6132+
verbose(env, "invalid return type %u of func %s#%d\n",
6133+
base_type(ret_type), func_id_name(func_id),
6134+
func_id);
61356135
return -EINVAL;
61366136
}
61376137
/* current BPF helper definitions are only coming from
@@ -6140,8 +6140,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
61406140
regs[BPF_REG_0].btf = btf_vmlinux;
61416141
regs[BPF_REG_0].btf_id = ret_btf_id;
61426142
} else {
6143-
verbose(env, "unknown return type %d of func %s#%d\n",
6144-
fn->ret_type, func_id_name(func_id), func_id);
6143+
verbose(env, "unknown return type %u of func %s#%d\n",
6144+
base_type(ret_type), func_id_name(func_id), func_id);
61456145
return -EINVAL;
61466146
}
61476147

0 commit comments

Comments
 (0)