Skip to content

Commit a85fa58

Browse files
committed
bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
jira VULN-136 cve-pre CVE-2022-0500 commit-author Hao Luo <[email protected]> commit 3c48073 upstream-diff This kernel doesn't have map_uid in bpf_reg_state so a minor conflict was resolved while cherry picking 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: Brett Mastbergen <[email protected]>
1 parent dd6c760 commit a85fa58

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
@@ -363,17 +363,22 @@ enum bpf_return_type {
363363
RET_INTEGER, /* function returns integer */
364364
RET_VOID, /* function doesn't return anything */
365365
RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
366-
RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
367-
RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
368-
RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
369-
RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
370-
RET_PTR_TO_ALLOC_MEM_OR_NULL, /* returns a pointer to dynamically allocated memory or NULL */
371-
RET_PTR_TO_BTF_ID_OR_NULL, /* returns a pointer to a btf_id or NULL */
372-
RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
366+
RET_PTR_TO_SOCKET, /* returns a pointer to a socket */
367+
RET_PTR_TO_TCP_SOCK, /* returns a pointer to a tcp_sock */
368+
RET_PTR_TO_SOCK_COMMON, /* returns a pointer to a sock_common */
369+
RET_PTR_TO_ALLOC_MEM, /* returns a pointer to dynamically allocated memory */
373370
RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
374371
RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
375372
__BPF_RET_TYPE_MAX,
376373

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

kernel/bpf/helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
670670
const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
671671
.func = bpf_per_cpu_ptr,
672672
.gpl_only = false,
673-
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL,
673+
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL,
674674
.arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
675675
.arg2_type = ARG_ANYTHING,
676676
};

kernel/bpf/verifier.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5931,6 +5931,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
59315931
int *insn_idx_p)
59325932
{
59335933
const struct bpf_func_proto *fn = NULL;
5934+
enum bpf_return_type ret_type;
59345935
struct bpf_reg_state *regs;
59355936
struct bpf_call_arg_meta meta;
59365937
int insn_idx = *insn_idx_p;
@@ -6057,13 +6058,13 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
60576058
regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
60586059

60596060
/* update return register (already marked as written above) */
6060-
if (fn->ret_type == RET_INTEGER) {
6061+
ret_type = fn->ret_type;
6062+
if (ret_type == RET_INTEGER) {
60616063
/* sets type to SCALAR_VALUE */
60626064
mark_reg_unknown(env, regs, BPF_REG_0);
6063-
} else if (fn->ret_type == RET_VOID) {
6065+
} else if (ret_type == RET_VOID) {
60646066
regs[BPF_REG_0].type = NOT_INIT;
6065-
} else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
6066-
fn->ret_type == RET_PTR_TO_MAP_VALUE) {
6067+
} else if (base_type(ret_type) == RET_PTR_TO_MAP_VALUE) {
60676068
/* There is no offset yet applied, variable or fixed */
60686069
mark_reg_known_zero(env, regs, BPF_REG_0);
60696070
/* remember map_ptr, so that check_map_access()
@@ -6076,28 +6077,27 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
60766077
return -EINVAL;
60776078
}
60786079
regs[BPF_REG_0].map_ptr = meta.map_ptr;
6079-
if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
6080+
if (type_may_be_null(ret_type)) {
6081+
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
6082+
} else {
60806083
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
60816084
if (map_value_has_spin_lock(meta.map_ptr))
60826085
regs[BPF_REG_0].id = ++env->id_gen;
6083-
} else {
6084-
regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
60856086
}
6086-
} else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
6087+
} else if (base_type(ret_type) == RET_PTR_TO_SOCKET) {
60876088
mark_reg_known_zero(env, regs, BPF_REG_0);
60886089
regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
6089-
} else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
6090+
} else if (base_type(ret_type) == RET_PTR_TO_SOCK_COMMON) {
60906091
mark_reg_known_zero(env, regs, BPF_REG_0);
60916092
regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
6092-
} else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
6093+
} else if (base_type(ret_type) == RET_PTR_TO_TCP_SOCK) {
60936094
mark_reg_known_zero(env, regs, BPF_REG_0);
60946095
regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
6095-
} else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) {
6096+
} else if (base_type(ret_type) == RET_PTR_TO_ALLOC_MEM) {
60966097
mark_reg_known_zero(env, regs, BPF_REG_0);
60976098
regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
60986099
regs[BPF_REG_0].mem_size = meta.mem_size;
6099-
} else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ||
6100-
fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) {
6100+
} else if (base_type(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) {
61016101
const struct btf_type *t;
61026102

61036103
mark_reg_known_zero(env, regs, BPF_REG_0);
@@ -6116,28 +6116,28 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
61166116
return -EINVAL;
61176117
}
61186118
regs[BPF_REG_0].type =
6119-
fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ?
6120-
PTR_TO_MEM : PTR_TO_MEM_OR_NULL;
6119+
(ret_type & PTR_MAYBE_NULL) ?
6120+
PTR_TO_MEM_OR_NULL : PTR_TO_MEM;
61216121
regs[BPF_REG_0].mem_size = tsize;
61226122
} else {
61236123
regs[BPF_REG_0].type =
6124-
fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ?
6125-
PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL;
6124+
(ret_type & PTR_MAYBE_NULL) ?
6125+
PTR_TO_BTF_ID_OR_NULL : PTR_TO_BTF_ID;
61266126
regs[BPF_REG_0].btf = meta.ret_btf;
61276127
regs[BPF_REG_0].btf_id = meta.ret_btf_id;
61286128
}
6129-
} else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL ||
6130-
fn->ret_type == RET_PTR_TO_BTF_ID) {
6129+
} else if (base_type(ret_type) == RET_PTR_TO_BTF_ID) {
61316130
int ret_btf_id;
61326131

61336132
mark_reg_known_zero(env, regs, BPF_REG_0);
6134-
regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ?
6135-
PTR_TO_BTF_ID :
6136-
PTR_TO_BTF_ID_OR_NULL;
6133+
regs[BPF_REG_0].type = (ret_type & PTR_MAYBE_NULL) ?
6134+
PTR_TO_BTF_ID_OR_NULL :
6135+
PTR_TO_BTF_ID;
61376136
ret_btf_id = *fn->ret_btf_id;
61386137
if (ret_btf_id == 0) {
6139-
verbose(env, "invalid return type %d of func %s#%d\n",
6140-
fn->ret_type, func_id_name(func_id), func_id);
6138+
verbose(env, "invalid return type %u of func %s#%d\n",
6139+
base_type(ret_type), func_id_name(func_id),
6140+
func_id);
61416141
return -EINVAL;
61426142
}
61436143
/* current BPF helper definitions are only coming from
@@ -6146,8 +6146,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
61466146
regs[BPF_REG_0].btf = btf_vmlinux;
61476147
regs[BPF_REG_0].btf_id = ret_btf_id;
61486148
} else {
6149-
verbose(env, "unknown return type %d of func %s#%d\n",
6150-
fn->ret_type, func_id_name(func_id), func_id);
6149+
verbose(env, "unknown return type %u of func %s#%d\n",
6150+
base_type(ret_type), func_id_name(func_id), func_id);
61516151
return -EINVAL;
61526152
}
61536153

0 commit comments

Comments
 (0)