Skip to content

Commit 559e06d

Browse files
bpf: Generally fix helper register offset check
jira VULN-140 pre-cve CVE-2022-23222 commit-author Daniel Borkmann <daniel@iogearbox.net> commit 6788ab2 Right now the assertion on check_ptr_off_reg() is only enforced for register types PTR_TO_CTX (and open coded also for PTR_TO_BTF_ID), however, this is insufficient since many other PTR_TO_* register types such as PTR_TO_FUNC do not handle/expect register offsets when passed to helper functions. Given this can slip-through easily when adding new types, make this an explicit allow-list and reject all other current and future types by default if this is encountered. Also, extend check_ptr_off_reg() to handle PTR_TO_BTF_ID as well instead of duplicating it. For PTR_TO_BTF_ID, reg->off is used for BTF to match expected BTF ids if struct offset is used. This part still needs to be allowed, but the dynamic off from the tnum must be rejected. Fixes: 69c087b ("bpf: Add bpf_for_each_map_elem() helper") Fixes: eaa6bcb ("bpf: Introduce bpf_per_cpu_ptr()") 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 6788ab2) Signed-off-by: Pratham Patel <ppatel@ciq.com>
1 parent 1a7b74f commit 559e06d

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

kernel/bpf/verifier.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,14 +3659,15 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
36593659
}
36603660
#endif
36613661

3662-
int check_ptr_off_reg(struct bpf_verifier_env *env,
3663-
const struct bpf_reg_state *reg, int regno)
3662+
static int __check_ptr_off_reg(struct bpf_verifier_env *env,
3663+
const struct bpf_reg_state *reg, int regno,
3664+
bool fixed_off_ok)
36643665
{
36653666
/* Access to this pointer-typed register or passing it to a helper
36663667
* is only allowed in its original, unmodified form.
36673668
*/
36683669

3669-
if (reg->off) {
3670+
if (!fixed_off_ok && reg->off) {
36703671
verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n",
36713672
reg_type_str(env, reg->type), regno, reg->off);
36723673
return -EACCES;
@@ -3684,6 +3685,12 @@ int check_ptr_off_reg(struct bpf_verifier_env *env,
36843685
return 0;
36853686
}
36863687

3688+
int check_ptr_off_reg(struct bpf_verifier_env *env,
3689+
const struct bpf_reg_state *reg, int regno)
3690+
{
3691+
return __check_ptr_off_reg(env, reg, regno, false);
3692+
}
3693+
36873694
static int __check_buffer_access(struct bpf_verifier_env *env,
36883695
const char *buf_info,
36893696
const struct bpf_reg_state *reg,
@@ -4854,12 +4861,6 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
48544861
kernel_type_name(btf_vmlinux, *arg_btf_id));
48554862
return -EACCES;
48564863
}
4857-
4858-
if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
4859-
verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n",
4860-
regno);
4861-
return -EACCES;
4862-
}
48634864
}
48644865

48654866
return 0;
@@ -4914,10 +4915,26 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49144915
if (err)
49154916
return err;
49164917

4917-
if (type == PTR_TO_CTX) {
4918-
err = check_ptr_off_reg(env, reg, regno);
4918+
switch ((u32)type) {
4919+
case SCALAR_VALUE:
4920+
/* Pointer types where reg offset is explicitly allowed: */
4921+
case PTR_TO_PACKET:
4922+
case PTR_TO_PACKET_META:
4923+
case PTR_TO_MAP_KEY:
4924+
case PTR_TO_MAP_VALUE:
4925+
case PTR_TO_MEM:
4926+
case PTR_TO_MEM | MEM_RDONLY:
4927+
case PTR_TO_BUF:
4928+
case PTR_TO_BUF | MEM_RDONLY:
4929+
case PTR_TO_STACK:
4930+
break;
4931+
/* All the rest must be rejected: */
4932+
default:
4933+
err = __check_ptr_off_reg(env, reg, regno,
4934+
type == PTR_TO_BTF_ID);
49194935
if (err < 0)
49204936
return err;
4937+
break;
49214938
}
49224939

49234940
skip_type_check:

0 commit comments

Comments
 (0)