Skip to content

Commit 1a7b74f

Browse files
bpf: Generalize check_ctx_reg for reuse with other types
jira VULN-140 pre-cve CVE-2022-23222 commit-author Daniel Borkmann <daniel@iogearbox.net> commit be80a1d upstream-diff A merge confict arised because 3363bd0 ("bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support") does not exist in our tree Generalize the check_ctx_reg() helper function into a more generic named one so that it can be reused for other register types as well to check whether their offset is non-zero. No functional change. 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 be80a1d) Signed-off-by: Pratham Patel <ppatel@ciq.com>
1 parent d613c41 commit 1a7b74f

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

include/linux/bpf_verifier.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
486486
void
487487
bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
488488

489-
int check_ctx_reg(struct bpf_verifier_env *env,
490-
const struct bpf_reg_state *reg, int regno);
489+
int check_ptr_off_reg(struct bpf_verifier_env *env,
490+
const struct bpf_reg_state *reg, int regno);
491491
int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
492492
u32 regno, u32 mem_size);
493493

kernel/bpf/btf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5492,7 +5492,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
54925492
i, btf_type_str(t));
54935493
return -EINVAL;
54945494
}
5495-
if (check_ctx_reg(env, reg, regno))
5495+
if (check_ptr_off_reg(env, reg, regno))
54965496
return -EINVAL;
54975497
} else if (ptr_to_mem_ok) {
54985498
const struct btf_type *resolve_ret;

kernel/bpf/verifier.c

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

3662-
int check_ctx_reg(struct bpf_verifier_env *env,
3663-
const struct bpf_reg_state *reg, int regno)
3662+
int check_ptr_off_reg(struct bpf_verifier_env *env,
3663+
const struct bpf_reg_state *reg, int regno)
36643664
{
3665-
/* Access to ctx or passing it to a helper is only allowed in
3666-
* its original, unmodified form.
3665+
/* Access to this pointer-typed register or passing it to a helper
3666+
* is only allowed in its original, unmodified form.
36673667
*/
36683668

36693669
if (reg->off) {
3670-
verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
3671-
regno, reg->off);
3670+
verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n",
3671+
reg_type_str(env, reg->type), regno, reg->off);
36723672
return -EACCES;
36733673
}
36743674

36753675
if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
36763676
char tn_buf[48];
36773677

36783678
tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3679-
verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
3679+
verbose(env, "variable %s access var_off=%s disallowed\n",
3680+
reg_type_str(env, reg->type), tn_buf);
36803681
return -EACCES;
36813682
}
36823683

@@ -4112,7 +4113,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
41124113
return -EACCES;
41134114
}
41144115

4115-
err = check_ctx_reg(env, reg, regno);
4116+
err = check_ptr_off_reg(env, reg, regno);
41164117
if (err < 0)
41174118
return err;
41184119

@@ -4914,7 +4915,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49144915
return err;
49154916

49164917
if (type == PTR_TO_CTX) {
4917-
err = check_ctx_reg(env, reg, regno);
4918+
err = check_ptr_off_reg(env, reg, regno);
49184919
if (err < 0)
49194920
return err;
49204921
}
@@ -9051,7 +9052,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
90519052
return err;
90529053
}
90539054

9054-
err = check_ctx_reg(env, &regs[ctx_reg], ctx_reg);
9055+
err = check_ptr_off_reg(env, &regs[ctx_reg], ctx_reg);
90559056
if (err < 0)
90569057
return err;
90579058

0 commit comments

Comments
 (0)