Re: [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF

From: Alexei Starovoitov

Date: Mon Nov 30 2026 - 23:21:57 EST


On Mon, Nov 30, 2026 at 01:44 PM Siddharth Nayyar <sidnayyar@xxxxxxxxxx> wrote:
> - BPF_MAP_UPDATE_ELEM: When local_type is set on the map, accept a value
> with the local struct's size and layout, validate member types, sizes,
> and zeroed padding holes, skip zeroed members absent from the running
> kernel, and copy each member into the kernel struct_ops value buffer.

> + const void *src = data + __btf_member_bit_offset(t, member) / 8;
> +
> + if (__btf_member_bitfield_size(t, member) ||
> + IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, member->type), &size)))
> + goto err;

My diff had this bug and the next one, sorry.
The size is compared with the size of the kernel member only.
Nothing checks that the member fits into the local struct.

btf_int_check_member() bounds an int by nr_bits, not by its size.
BPF_BTF_LOAD accepts this:

INT 'u64' size=8 nr_bits=8
STRUCT 'sched_ext_ops' size=192
'name' char[128] bits_offset=0
'hotplug_seq' u64 bits_offset=1528

Then CAP_BPF reads the kernel heap:

map_update_elem() kvmemdup_bpfptr(uvalue, 192)
local_to_kern_value() src = data + 191, size = 8, kern_size = 8
memcpy() copies 7 bytes past the end of data
BPF_MAP_LOOKUP_ELEM returns them in hotplug_seq

When the kernel doesn't have the member the size can be anything.
memchr_inv(src, 0, size) runs off the buffer as well.
Reject size > t->size - offset before src is read.

> + value_id = bpf_find_btf_id(name, BTF_KIND_STRUCT, kern_btf);
> + if (value_id < 0)
> + return ERR_PTR(value_id);
> + st_ops_desc = bpf_struct_ops_find_value(*kern_btf, value_id);

This races with module init.
bpf_find_btf_id() sees the BTF of a module since MODULE_STATE_COMING.
struct_ops_tab starts with 4 slots. bpf_testmod registers 5 struct_ops.

bpf_struct_ops_resolve_attach() bpf_testmod_init()
st_ops_desc = &tab->ops[0]
btf_add_struct_ops()
krealloc(tab)
st_ops_desc->type <- use after free

check_struct_ops_btf_id() and bpf_struct_ops_map_alloc() call
btf_try_get_module() first.
Do it in find_desc_by_local_type() before the lookup.

> + if (btf_type_is_ptr(mtype) &&
> + !!btf_type_resolve_func_ptr(btf, member->type, NULL) !=
> + !!btf_type_resolve_func_ptr(kern_btf, kern_member->type, NULL))
> + goto err;

> + err = check_zero_holes(btf, t, data);

No need for these two.
The holes of the local struct are not copied.
__bpf_struct_ops_map_update_elem() checks pointers by the kernel type.
A NULL 'void *' callback loads with a regular skeleton and fails here.

pw-bot: cr