Skip to content

Commit 37d7f65

Browse files
bpf: Fix ringbuf memory type confusion when passing to helpers
jira VULN-68 cve CVE-2021-4204 commit-author Daniel Borkmann <daniel@iogearbox.net> 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 <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> (cherry picked from commit a672b2e) Signed-off-by: Pratham Patel <ppatel@ciq.com>
1 parent b9fae43 commit 37d7f65

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

include/linux/bpf.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,12 @@ enum bpf_type_flag {
296296
*/
297297
MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS),
298298

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

302307
/* Max number of base types. */
@@ -379,7 +384,7 @@ enum bpf_return_type {
379384
RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
380385
RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
381386
RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
382-
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
387+
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
383388
RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
384389

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

kernel/bpf/verifier.c

Lines changed: 5 additions & 1 deletion
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);
@@ -4746,6 +4748,7 @@ static const struct bpf_reg_types mem_types = {
47464748
PTR_TO_MAP_KEY,
47474749
PTR_TO_MAP_VALUE,
47484750
PTR_TO_MEM,
4751+
PTR_TO_MEM | MEM_ALLOC,
47494752
PTR_TO_BUF,
47504753
},
47514754
};
@@ -4763,7 +4766,7 @@ static const struct bpf_reg_types int_ptr_types = {
47634766
static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } };
47644767
static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } };
47654768
static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } };
4766-
static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } };
4769+
static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM | MEM_ALLOC } };
47674770
static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } };
47684771
static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } };
47694772
static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } };
@@ -4924,6 +4927,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49244927
case PTR_TO_MAP_VALUE:
49254928
case PTR_TO_MEM:
49264929
case PTR_TO_MEM | MEM_RDONLY:
4930+
case PTR_TO_MEM | MEM_ALLOC:
49274931
case PTR_TO_BUF:
49284932
case PTR_TO_BUF | MEM_RDONLY:
49294933
case PTR_TO_STACK:

0 commit comments

Comments
 (0)