Skip to content

Commit e6385a9

Browse files
shunghsiyuNobody
authored and
Nobody
committed
bpf: determine buf_info inside check_buffer_access()
Instead of determining buf_info string in the caller of check_buffer_access(), we can determine whether the register type is read-only through type_is_rdonly_mem() helper inside check_buffer_access() and construct buf_info, making the code slightly cleaner. Signed-off-by: Shung-Hsi Yu <[email protected]> Acked-by: Yonghong Song <[email protected]>
1 parent 206e807 commit e6385a9

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

kernel/bpf/verifier.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,9 +4062,9 @@ static int check_buffer_access(struct bpf_verifier_env *env,
40624062
const struct bpf_reg_state *reg,
40634063
int regno, int off, int size,
40644064
bool zero_size_allowed,
4065-
const char *buf_info,
40664065
u32 *max_access)
40674066
{
4067+
const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr";
40684068
int err;
40694069

40704070
err = __check_buffer_access(env, buf_info, reg, regno, off, size);
@@ -4576,7 +4576,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
45764576
value_regno);
45774577
} else if (base_type(reg->type) == PTR_TO_BUF) {
45784578
bool rdonly_mem = type_is_rdonly_mem(reg->type);
4579-
const char *buf_info;
45804579
u32 *max_access;
45814580

45824581
if (rdonly_mem) {
@@ -4585,15 +4584,13 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
45854584
regno, reg_type_str(env, reg->type));
45864585
return -EACCES;
45874586
}
4588-
buf_info = "rdonly";
45894587
max_access = &env->prog->aux->max_rdonly_access;
45904588
} else {
4591-
buf_info = "rdwr";
45924589
max_access = &env->prog->aux->max_rdwr_access;
45934590
}
45944591

45954592
err = check_buffer_access(env, reg, regno, off, size, false,
4596-
buf_info, max_access);
4593+
max_access);
45974594

45984595
if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ))
45994596
mark_reg_unknown(env, regs, value_regno);
@@ -4856,7 +4853,6 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
48564853
struct bpf_call_arg_meta *meta)
48574854
{
48584855
struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
4859-
const char *buf_info;
48604856
u32 *max_access;
48614857

48624858
switch (base_type(reg->type)) {
@@ -4883,15 +4879,13 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
48834879
if (meta && meta->raw_mode)
48844880
return -EACCES;
48854881

4886-
buf_info = "rdonly";
48874882
max_access = &env->prog->aux->max_rdonly_access;
48884883
} else {
4889-
buf_info = "rdwr";
48904884
max_access = &env->prog->aux->max_rdwr_access;
48914885
}
48924886
return check_buffer_access(env, reg, regno, reg->off,
48934887
access_size, zero_size_allowed,
4894-
buf_info, max_access);
4888+
max_access);
48954889
case PTR_TO_STACK:
48964890
return check_stack_range_initialized(
48974891
env,

0 commit comments

Comments
 (0)