Skip to content

Commit 1697b91

Browse files
committed
bpf: Generalize check_ctx_reg for reuse with other types
jira VULN-72 cve CVE-2021-4204 commit-author Daniel Borkmann <[email protected]> commit be80a1d 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 <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> (cherry picked from commit be80a1d) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent 495c18b commit 1697b91

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

include/linux/bpf_verifier.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
487487
void
488488
bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
489489

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

kernel/bpf/btf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5494,7 +5494,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
54945494
i, btf_type_str(t));
54955495
return -EINVAL;
54965496
}
5497-
if (check_ctx_reg(env, reg, regno))
5497+
if (check_ptr_off_reg(env, reg, regno))
54985498
return -EINVAL;
54995499
} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {
55005500
const struct btf_type *reg_ref_t;

kernel/bpf/verifier.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -3661,24 +3661,25 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
36613661
}
36623662
#endif
36633663

3664-
int check_ctx_reg(struct bpf_verifier_env *env,
3665-
const struct bpf_reg_state *reg, int regno)
3664+
int check_ptr_off_reg(struct bpf_verifier_env *env,
3665+
const struct bpf_reg_state *reg, int regno)
36663666
{
3667-
/* Access to ctx or passing it to a helper is only allowed in
3668-
* its original, unmodified form.
3667+
/* Access to this pointer-typed register or passing it to a helper
3668+
* is only allowed in its original, unmodified form.
36693669
*/
36703670

36713671
if (reg->off) {
3672-
verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
3673-
regno, reg->off);
3672+
verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n",
3673+
reg_type_str(env, reg->type), regno, reg->off);
36743674
return -EACCES;
36753675
}
36763676

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

36803680
tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3681-
verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
3681+
verbose(env, "variable %s access var_off=%s disallowed\n",
3682+
reg_type_str(env, reg->type), tn_buf);
36823683
return -EACCES;
36833684
}
36843685

@@ -4114,7 +4115,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
41144115
return -EACCES;
41154116
}
41164117

4117-
err = check_ctx_reg(env, reg, regno);
4118+
err = check_ptr_off_reg(env, reg, regno);
41184119
if (err < 0)
41194120
return err;
41204121

@@ -4926,7 +4927,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
49264927
return err;
49274928

49284929
if (type == PTR_TO_CTX) {
4929-
err = check_ctx_reg(env, reg, regno);
4930+
err = check_ptr_off_reg(env, reg, regno);
49304931
if (err < 0)
49314932
return err;
49324933
}
@@ -9069,7 +9070,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
90699070
return err;
90709071
}
90719072

9072-
err = check_ctx_reg(env, &regs[ctx_reg], ctx_reg);
9073+
err = check_ptr_off_reg(env, &regs[ctx_reg], ctx_reg);
90739074
if (err < 0)
90749075
return err;
90759076

0 commit comments

Comments
 (0)