Skip to content

Commit a672b2e

Browse files
committed
bpf: Fix ringbuf memory type confusion when passing to helpers
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]>
1 parent 64620e0 commit a672b2e

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
@@ -316,7 +316,12 @@ enum bpf_type_flag {
316316
*/
317317
MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS),
318318

319-
__BPF_TYPE_LAST_FLAG = MEM_RDONLY,
319+
/* MEM was "allocated" from a different helper, and cannot be mixed
320+
* with regular non-MEM_ALLOC'ed MEM types.
321+
*/
322+
MEM_ALLOC = BIT(2 + BPF_BASE_TYPE_BITS),
323+
324+
__BPF_TYPE_LAST_FLAG = MEM_ALLOC,
320325
};
321326

322327
/* Max number of base types. */
@@ -400,7 +405,7 @@ enum bpf_return_type {
400405
RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
401406
RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
402407
RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
403-
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
408+
RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
404409
RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
405410

406411
/* 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
@@ -570,6 +570,8 @@ static const char *reg_type_str(struct bpf_verifier_env *env,
570570

571571
if (type & MEM_RDONLY)
572572
strncpy(prefix, "rdonly_", 16);
573+
if (type & MEM_ALLOC)
574+
strncpy(prefix, "alloc_", 16);
573575

574576
snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s",
575577
prefix, str[base_type(type)], postfix);
@@ -5135,6 +5137,7 @@ static const struct bpf_reg_types mem_types = {
51355137
PTR_TO_MAP_KEY,
51365138
PTR_TO_MAP_VALUE,
51375139
PTR_TO_MEM,
5140+
PTR_TO_MEM | MEM_ALLOC,
51385141
PTR_TO_BUF,
51395142
},
51405143
};
@@ -5152,7 +5155,7 @@ static const struct bpf_reg_types int_ptr_types = {
51525155
static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } };
51535156
static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } };
51545157
static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } };
5155-
static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } };
5158+
static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM | MEM_ALLOC } };
51565159
static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } };
51575160
static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } };
51585161
static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } };
@@ -5315,6 +5318,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
53155318
case PTR_TO_MAP_VALUE:
53165319
case PTR_TO_MEM:
53175320
case PTR_TO_MEM | MEM_RDONLY:
5321+
case PTR_TO_MEM | MEM_ALLOC:
53185322
case PTR_TO_BUF:
53195323
case PTR_TO_BUF | MEM_RDONLY:
53205324
case PTR_TO_STACK:

0 commit comments

Comments
 (0)