Skip to content

Commit 97e6d7d

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Check PTR_TO_MEM | MEM_RDONLY in check_helper_mem_access
The commit being fixed was aiming to disallow users from incorrectly obtaining writable pointer to memory that is only meant to be read. This is enforced now using a MEM_RDONLY flag. For instance, in case of global percpu variables, when the BTF type is not struct (e.g. bpf_prog_active), the verifier marks register type as PTR_TO_MEM | MEM_RDONLY from bpf_this_cpu_ptr or bpf_per_cpu_ptr helpers. However, when passing such pointer to kfunc, global funcs, or BPF helpers, in check_helper_mem_access, there is no expectation MEM_RDONLY flag will be set, hence it is checked as pointer to writable memory. Later, verifier sets up argument type of global func as PTR_TO_MEM | PTR_MAYBE_NULL, so user can use a global func to get around the limitations imposed by this flag. This check will also cover global non-percpu variables that may be introduced in kernel BTF in future. Also, we update the log message for PTR_TO_BUF case to be similar to PTR_TO_MEM case, so that the reason for error is clear to user. Fixes: 34d3a78 ("bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.") Reviewed-by: Hao Luo <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent be77354 commit 97e6d7d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

kernel/bpf/verifier.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4871,13 +4871,23 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
48714871
return check_map_access(env, regno, reg->off, access_size,
48724872
zero_size_allowed);
48734873
case PTR_TO_MEM:
4874+
if (type_is_rdonly_mem(reg->type)) {
4875+
if (meta && meta->raw_mode) {
4876+
verbose(env, "R%d cannot write into %s\n", regno,
4877+
reg_type_str(env, reg->type));
4878+
return -EACCES;
4879+
}
4880+
}
48744881
return check_mem_region_access(env, regno, reg->off,
48754882
access_size, reg->mem_size,
48764883
zero_size_allowed);
48774884
case PTR_TO_BUF:
48784885
if (type_is_rdonly_mem(reg->type)) {
4879-
if (meta && meta->raw_mode)
4886+
if (meta && meta->raw_mode) {
4887+
verbose(env, "R%d cannot write into %s\n", regno,
4888+
reg_type_str(env, reg->type));
48804889
return -EACCES;
4890+
}
48814891

48824892
max_access = &env->prog->aux->max_rdonly_access;
48834893
} else {

0 commit comments

Comments
 (0)