Skip to content

Commit d6c9f52

Browse files
bpf: Fix out of bounds access for ringbuf helpers
jira VULN-140 cve CVE-2022-23222 commit-author Daniel Borkmann <daniel@iogearbox.net> commit 64620e0 Both bpf_ringbuf_submit() and bpf_ringbuf_discard() have ARG_PTR_TO_ALLOC_MEM in their bpf_func_proto definition as their first argument. They both expect the result from a prior bpf_ringbuf_reserve() call which has a return type of RET_PTR_TO_ALLOC_MEM_OR_NULL. Meaning, after a NULL check in the code, the verifier will promote the register type in the non-NULL branch to a PTR_TO_MEM and in the NULL branch to a known zero scalar. Generally, pointer arithmetic on PTR_TO_MEM is allowed, so the latter could have an offset. The ARG_PTR_TO_ALLOC_MEM expects a PTR_TO_MEM register type. However, the non- zero result from bpf_ringbuf_reserve() must be fed into either bpf_ringbuf_submit() or bpf_ringbuf_discard() but with the original offset given it will then read out the struct bpf_ringbuf_hdr mapping. The verifier missed to enforce a zero offset, so that out of bounds access can be triggered which could be used to escalate privileges if unprivileged BPF was enabled (disabled by default in kernel). Fixes: 457f443 ("bpf: Implement BPF ring buffer and verifier support for it") Reported-by: <tr3e.wang@gmail.com> (SecCoder Security Lab) 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 64620e0) Signed-off-by: Pratham Patel <ppatel@ciq.com>
1 parent 24faad9 commit d6c9f52

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4927,9 +4927,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49274927
case PTR_TO_BUF:
49284928
case PTR_TO_BUF | MEM_RDONLY:
49294929
case PTR_TO_STACK:
4930+
/* Some of the argument types nevertheless require a
4931+
* zero register offset.
4932+
*/
4933+
if (arg_type == ARG_PTR_TO_ALLOC_MEM)
4934+
goto force_off_check;
49304935
break;
49314936
/* All the rest must be rejected: */
49324937
default:
4938+
force_off_check:
49334939
err = __check_ptr_off_reg(env, reg, regno,
49344940
type == PTR_TO_BTF_ID);
49354941
if (err < 0)

0 commit comments

Comments
 (0)