btf_struct_access() rejects direct access to special fields (kptr, spin_lock, list/rbtree nodes, ...) of program allocated objects, but for variable offset access the check is done at min_off only, while the whole [min_off, max_off] range can be reached at runtime. A read leaks the pointer; a write corrupts the refcounted pointer and crashes the release path (bpf_obj_free_fields -> bpf_mem_free_rcu). Check the whole [min_off, max_off + size) range against the record, like check_map_access() already does for map values. Fixes: 89e72f81a455 ("bpf: Varying offset access support for PTR_TO_BTF_ID pointers") Signed-off-by: Shihuang Liu --- kernel/bpf/btf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 8bf39098ba97a..47a64a57b370d 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8094,7 +8094,8 @@ int btf_struct_access(struct bpf_verifier_log *log, for (i = 0; i < rec->cnt; i++) { struct btf_field *field = &rec->fields[i]; u32 offset = field->offset; - if (off < offset + field->size && offset < off + size) { + if (min_off < (s64)offset + field->size && + (s64)offset < max_off + size) { bpf_log(log, "direct access to %s is disallowed\n", btf_field_type_name(field->type)); -- 2.43.0