Skip to content

Commit ac65c71

Browse files
KalliopeMaineddyz87
authored andcommitted
bpf: Reject writes through untrusted BTF pointers
check_ptr_to_btf_access() lets program-type btf_struct_access callbacks validate writes before the default BTF access path rejects non-read accesses. That bypasses the read-only policy for untrusted BTF pointers created by helpers such as bpf_rdonly_cast(). Reject non-read accesses through PTR_UNTRUSTED BTF pointers at the common entry point, before the callback branch to handle all cases. Fixes: 282de14 ("bpf: Introduce allocated objects support") Signed-off-by: Nicholas Dudar <main.kalliope@gmail.com> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Reviewed-by: Amery Hung <ameryhung@gmail.com> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
1 parent ee1bcf8 commit ac65c71

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎kernel/bpf/verifier.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5790,6 +5790,11 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
57905790
return -EACCES;
57915791
}
57925792

5793+
if (atype != BPF_READ && (type_flag(reg->type) & PTR_UNTRUSTED)) {
5794+
verbose(env, "only read is supported\n");
5795+
return -EACCES;
5796+
}
5797+
57935798
if (env->ops->btf_struct_access && !type_is_alloc(reg->type) && atype == BPF_WRITE) {
57945799
if (!btf_is_kernel(reg->btf)) {
57955800
verifier_bug(env, "reg->btf must be kernel btf");
@@ -5802,8 +5807,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
58025807
reg_arg_name(env, argno), tname, off, size);
58035808
} else {
58045809
/* Writes are permitted with default btf_struct_access for
5805-
* program allocated objects (which always have id > 0),
5806-
* but not for untrusted PTR_TO_BTF_ID | MEM_ALLOC.
5810+
* program allocated objects (which always have id > 0).
58075811
*/
58085812
if (atype != BPF_READ && !type_is_ptr_alloc_obj(reg->type)) {
58095813
verbose(env, "only read is supported\n");

0 commit comments

Comments
 (0)