bpf: Generalize check_ctx_reg for reuse with other types

ANBZ: #7670

commit be80a1d3f9 upstream.

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>
Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
Reviewed-by: Yuanhe Shu <xiangzao@linux.alibaba.com>
Link: https://gitee.com/anolis/cloud-kernel/pulls/2478
This commit is contained in:
Daniel Borkmann 2022-01-10 14:05:49 +00:00 committed by 小龙
parent 2919e6c540
commit f1e2001452
3 changed files with 14 additions and 13 deletions

View File

@ -490,8 +490,8 @@ bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
void
bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
int check_ctx_reg(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg, int regno);
int check_ptr_off_reg(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg, int regno);
/* this lives here instead of in bpf.h because it needs to dereference tgt_prog */
static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,

View File

@ -5206,7 +5206,7 @@ int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
i, btf_kind_str[BTF_INFO_KIND(t->info)]);
goto out;
}
if (check_ctx_reg(env, &reg[i + 1], i + 1))
if (check_ptr_off_reg(env, &reg[i + 1], i + 1))
goto out;
continue;
}

View File

@ -3403,16 +3403,16 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
}
#endif
int check_ctx_reg(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg, int regno)
int check_ptr_off_reg(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg, int regno)
{
/* Access to ctx or passing it to a helper is only allowed in
* its original, unmodified form.
/* Access to this pointer-typed register or passing it to a helper
* is only allowed in its original, unmodified form.
*/
if (reg->off) {
verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
regno, reg->off);
verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n",
reg_type_str(env, reg->type), regno, reg->off);
return -EACCES;
}
@ -3420,7 +3420,8 @@ int check_ctx_reg(struct bpf_verifier_env *env,
char tn_buf[48];
tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
verbose(env, "variable %s access var_off=%s disallowed\n",
reg_type_str(env, reg->type), tn_buf);
return -EACCES;
}
@ -3870,7 +3871,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
return -EACCES;
}
err = check_ctx_reg(env, reg, regno);
err = check_ptr_off_reg(env, reg, regno);
if (err < 0)
return err;
@ -4610,7 +4611,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
return err;
if (type == PTR_TO_CTX) {
err = check_ctx_reg(env, reg, regno);
err = check_ptr_off_reg(env, reg, regno);
if (err < 0)
return err;
}
@ -8506,7 +8507,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
return err;
}
err = check_ctx_reg(env, &regs[ctx_reg], ctx_reg);
err = check_ptr_off_reg(env, &regs[ctx_reg], ctx_reg);
if (err < 0)
return err;