Skip to content

Commit be80a1d

Browse files
committed
bpf: Generalize check_ctx_reg for reuse with other types
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]>
1 parent 343e537 commit be80a1d

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
@@ -519,8 +519,8 @@ bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
519519
void
520520
bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
521521

522-
int check_ctx_reg(struct bpf_verifier_env *env,
523-
const struct bpf_reg_state *reg, int regno);
522+
int check_ptr_off_reg(struct bpf_verifier_env *env,
523+
const struct bpf_reg_state *reg, int regno);
524524
int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
525525
u32 regno, u32 mem_size);
526526

kernel/bpf/btf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5686,7 +5686,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
56865686
i, btf_type_str(t));
56875687
return -EINVAL;
56885688
}
5689-
if (check_ctx_reg(env, reg, regno))
5689+
if (check_ptr_off_reg(env, reg, regno))
56905690
return -EINVAL;
56915691
} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {
56925692
const struct btf_type *reg_ref_t;

kernel/bpf/verifier.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3969,24 +3969,25 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
39693969
}
39703970
#endif
39713971

3972-
int check_ctx_reg(struct bpf_verifier_env *env,
3973-
const struct bpf_reg_state *reg, int regno)
3972+
int check_ptr_off_reg(struct bpf_verifier_env *env,
3973+
const struct bpf_reg_state *reg, int regno)
39743974
{
3975-
/* Access to ctx or passing it to a helper is only allowed in
3976-
* its original, unmodified form.
3975+
/* Access to this pointer-typed register or passing it to a helper
3976+
* is only allowed in its original, unmodified form.
39773977
*/
39783978

39793979
if (reg->off) {
3980-
verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
3981-
regno, reg->off);
3980+
verbose(env, "dereference of modified %s ptr R%d off=%d disallowed\n",
3981+
reg_type_str(env, reg->type), regno, reg->off);
39823982
return -EACCES;
39833983
}
39843984

39853985
if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
39863986
char tn_buf[48];
39873987

39883988
tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3989-
verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
3989+
verbose(env, "variable %s access var_off=%s disallowed\n",
3990+
reg_type_str(env, reg->type), tn_buf);
39903991
return -EACCES;
39913992
}
39923993

@@ -4437,7 +4438,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
44374438
return -EACCES;
44384439
}
44394440

4440-
err = check_ctx_reg(env, reg, regno);
4441+
err = check_ptr_off_reg(env, reg, regno);
44414442
if (err < 0)
44424443
return err;
44434444

@@ -5305,7 +5306,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
53055306
return err;
53065307

53075308
if (type == PTR_TO_CTX) {
5308-
err = check_ctx_reg(env, reg, regno);
5309+
err = check_ptr_off_reg(env, reg, regno);
53095310
if (err < 0)
53105311
return err;
53115312
}
@@ -9651,7 +9652,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
96519652
return err;
96529653
}
96539654

9654-
err = check_ctx_reg(env, &regs[ctx_reg], ctx_reg);
9655+
err = check_ptr_off_reg(env, &regs[ctx_reg], ctx_reg);
96559656
if (err < 0)
96569657
return err;
96579658

0 commit comments

Comments
 (0)