Skip to content

Commit 7964fac

Browse files
committed
bpf: Fix ringbuf memory type confusion when passing to helpers
jira VULN-72 cve CVE-2021-4204 commit-author Daniel Borkmann <[email protected]> commit a672b2e The bpf_ringbuf_submit() and bpf_ringbuf_discard() have ARG_PTR_TO_ALLOC_MEM in their bpf_func_proto definition as their first argument, and thus both expect the result from a prior bpf_ringbuf_reserve() call which has a return type of RET_PTR_TO_ALLOC_MEM_OR_NULL. While the non-NULL memory from bpf_ringbuf_reserve() can be passed to other helpers, the two sinks (bpf_ringbuf_submit(), bpf_ringbuf_discard()) right now only enforce a register type of PTR_TO_MEM. This can lead to potential type confusion since it would allow other PTR_TO_MEM memory to be passed into the two sinks which did not come from bpf_ringbuf_reserve(). Add a new MEM_ALLOC composable type attribute for PTR_TO_MEM, and enforce that: - bpf_ringbuf_reserve() returns NULL or PTR_TO_MEM | MEM_ALLOC - bpf_ringbuf_submit() and bpf_ringbuf_discard() only take PTR_TO_MEM | MEM_ALLOC but not plain PTR_TO_MEM arguments via ARG_PTR_TO_ALLOC_MEM - however, other helpers might treat PTR_TO_MEM | MEM_ALLOC as plain PTR_TO_MEM to populate the memory area when they use ARG_PTR_TO_{UNINIT_,}MEM in their func proto description Fixes: 457f443 ("bpf: Implement BPF ring buffer and verifier support for it") Reported-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> (cherry picked from commit a672b2e) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent 3a067b2 commit 7964fac

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

include/linux/bpf.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,12 @@ enum bpf_type_flag {
298298
*/
299299
MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS),
300300

301-
__BPF_TYPE_LAST_FLAG = MEM_RDONLY,
301+
/* MEM was "allocated" from a different helper, and cannot be mixed
302+
* with regular non-MEM_ALLOC'ed MEM types.
303+
*/
304+
MEM_ALLOC = BIT(2 + BPF_BASE_TYPE_BITS),
305+
306+
__BPF_TYPE_LAST_FLAG = MEM_ALLOC,
302307
};
303308

304309
/* Max number of base types. */
@@ -381,7 +386,7 @@ enum bpf_return_type {
381386
RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
382387
RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
383388
RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
384-
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
389+
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
385390
RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
386391

387392
/* This must be the last entry. Its purpose is to ensure the enum is

kernel/bpf/verifier.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ static const char *reg_type_str(struct bpf_verifier_env *env,
574574

575575
if (type & MEM_RDONLY)
576576
strncpy(prefix, "rdonly_", 16);
577+
if (type & MEM_ALLOC)
578+
strncpy(prefix, "alloc_", 16);
577579

578580
snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s",
579581
prefix, str[base_type(type)], postfix);
@@ -4758,6 +4760,7 @@ static const struct bpf_reg_types mem_types = {
47584760
PTR_TO_MAP_KEY,
47594761
PTR_TO_MAP_VALUE,
47604762
PTR_TO_MEM,
4763+
PTR_TO_MEM | MEM_ALLOC,
47614764
PTR_TO_BUF,
47624765
},
47634766
};
@@ -4775,7 +4778,7 @@ static const struct bpf_reg_types int_ptr_types = {
47754778
static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } };
47764779
static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } };
47774780
static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } };
4778-
static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } };
4781+
static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM | MEM_ALLOC } };
47794782
static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } };
47804783
static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } };
47814784
static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } };
@@ -4936,6 +4939,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49364939
case PTR_TO_MAP_VALUE:
49374940
case PTR_TO_MEM:
49384941
case PTR_TO_MEM | MEM_RDONLY:
4942+
case PTR_TO_MEM | MEM_ALLOC:
49394943
case PTR_TO_BUF:
49404944
case PTR_TO_BUF | MEM_RDONLY:
49414945
case PTR_TO_STACK:

0 commit comments

Comments
 (0)