All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Siddharth Nayyar" <sidnayyar@google.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF
Date: Mon, 30 Nov 2026 22:05:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-242072@kernel.org> (raw)
In-Reply-To: <20261130-lskel-struct-ops-v2-1-8b76e65073ab@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: out-of-bounds heap read in local_to_kern_value() on
  crafted local BTF
- [Medium] bpf: unlocked struct_ops_tab access in
  find_desc_by_local_type()
--

commit 358556cdd1778df7a46e8f700b565dd624b3828f
Author: Siddharth Nayyar <sidnayyar@google.com>

bpf: Support struct_ops with object BTF

This commit allows BPF_MAP_TYPE_STRUCT_OPS maps and BPF_PROG_TYPE_STRUCT_OPS
programs to specify their target struct and members using the BPF object's
own BTF, resolving the corresponding kernel struct_ops type and members by
name.

> diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
> index 3d334cd526c03..96eca8493892a 100644
> --- a/kernel/bpf/bpf_struct_ops.c
> +++ b/kernel/bpf/bpf_struct_ops.c
[ ... ]
> @@ -729,8 +736,152 @@ static void bpf_struct_ops_map_free_ksyms(struct bpf_struct_ops_map *st_map)
[ ... ]
> +/*
> + * 'id' is a local struct, e.g. tcp_congestion_ops or tcp_congestion_ops___v2.
> + * Find bpf_struct_ops_tcp_congestion_ops in the BTF of vmlinux or of a module.
> + * A module can have it for a struct of vmlinux. The caller does btf_put(*kern_btf).
> + */
> +static const struct bpf_struct_ops_desc *
> +find_desc_by_local_type(const struct btf *btf, u32 id, struct btf **kern_btf)
> +{
> +	const struct btf_type *t = btf_type_by_id(btf, id);
> +	const struct bpf_struct_ops_desc *st_ops_desc;
> +	char name[KSYM_NAME_LEN];
> +	const char *tname;
> +	s32 value_id;
> +
> +	if (!t || !__btf_type_is_struct(t))
> +		return ERR_PTR(-EINVAL);
> +	tname = btf_name_by_offset(btf, t->name_off);
> +	if (snprintf(name, sizeof(name), VALUE_PREFIX "%.*s",
> +		     (int)bpf_core_essential_name_len(tname), tname) >= sizeof(name))
> +		return ERR_PTR(-EINVAL);
> +	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);
> +	if (!st_ops_desc) {
> +		btf_put(*kern_btf);
> +		return ERR_PTR(-EOPNOTSUPP);
> +	}
> +	return st_ops_desc;
> +}
> +
> +/*
> + * attach_btf_id is a local struct and expected_attach_type is the index of its
> + * member. Replace them, and the BTF, with what the kernel has under the same names.
> + */
> +int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr)
> +{
> +	const struct bpf_struct_ops_desc *st_ops_desc;
> +	const struct btf_member *member;
> +	const struct btf_type *t;
> +	struct btf *kern_btf;
> +
> +	st_ops_desc = find_desc_by_local_type(*btf, attr->attach_btf_id, &kern_btf);
> +	if (IS_ERR(st_ops_desc))
> +		return PTR_ERR(st_ops_desc);
> +	t = btf_type_by_id(*btf, attr->attach_btf_id);
> +	if (attr->expected_attach_type >= btf_type_vlen(t)) {
> +		btf_put(kern_btf);
> +		return -EINVAL;
> +	}
> +	member = btf_type_member(t) + attr->expected_attach_type;
> +	member = find_member(kern_btf, st_ops_desc->type,
> +			     btf_name_by_offset(*btf, member->name_off));

[Severity: Medium]
Can find_desc_by_local_type() race with btf_add_struct_ops() and trigger a
use-after-free on (*kern_btf)->struct_ops_tab?

When a module loads, btf_module_notify() publishes the module BTF in
btf_idr on MODULE_STATE_COMING before the module's init function runs and
before BTF_MODULE_F_LIVE is set on MODULE_STATE_LIVE.  While init runs,
register_bpf_struct_ops() calls btf_add_struct_ops(), which reallocates
btf->struct_ops_tab via krealloc() once tab->cnt reaches tab->capacity:

CPU0                                  CPU1
module_init()
  register_bpf_struct_ops()
    btf_add_struct_ops()
                                      bpf_struct_ops_resolve_attach()
                                        find_desc_by_local_type()
                                          bpf_find_btf_id()
                                          bpf_struct_ops_find_value()
      krealloc(btf->struct_ops_tab)
                                        find_member(..., st_ops_desc->type)
                                          <-- use-after-free

Existing struct_ops paths (such as check_struct_ops_btf_id() and the
BPF_F_VTYPE_BTF_OBJ_FD branch in bpf_struct_ops_map_alloc()) guard against
this by calling btf_try_get_module() to verify BTF_MODULE_F_LIVE before
calling bpf_struct_ops_find() or bpf_struct_ops_find_value().  Here,
bpf_struct_ops_resolve_attach() never calls btf_try_get_module(), and
local_to_kern_attr() calls it only after find_desc_by_local_type() has
already searched struct_ops_tab and cached st_ops_desc.

> +	if (!member) {
> +		btf_put(kern_btf);
> +		return -ENOENT;
> +	}
> +	attr->attach_btf_id = st_ops_desc->type_id;
> +	attr->expected_attach_type = member - btf_type_member(st_ops_desc->type);
> +	btf_put(*btf);
> +	*btf = kern_btf;
> +	return 0;
> +}
> +
> +u32 bpf_struct_ops_map_update_size(const struct bpf_map *map)
> +{
> +	const struct bpf_struct_ops_map *st_map = (const struct bpf_struct_ops_map *)map;
> +
> +	return st_map->local_type ? st_map->local_type->size : map->value_size;
> +}
> +
> +/*
> + * 'data' has the layout of the local struct. Copy every member to the place
> + * of the member with the same name in the struct of the kernel.
> + */
> +static void *local_to_kern_value(const struct bpf_struct_ops_map *st_map, const void *data)
> +{
> +	const struct btf_type *t = st_map->local_type, *kern_t = st_map->st_ops_desc->type;
> +	const struct btf *btf = st_map->local_btf, *kern_btf = st_map->btf;
> +	const struct btf_member *member, *kern_member;
> +	const struct btf_type *mtype, *kern_mtype;
> +	struct bpf_struct_ops_value *value;
> +	u32 i, size, kern_size;
> +	int err = -EINVAL;
> +
> +	value = kvzalloc(st_map->map.value_size, GFP_USER);
> +	if (!value)
> +		return ERR_PTR(-ENOMEM);
> +
> +	for_each_member(i, t, member) {
> +		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;
> +		kern_member = find_member(kern_btf, kern_t,
> +					  btf_name_by_offset(btf, member->name_off));
> +		if (!kern_member) {
> +			/* This kernel doesn't have the member. That's fine when it's not in use. */
> +			if (!memchr_inv(src, 0, size))
> +				continue;
> +			err = -ENOENT;
> +			goto err;
> +		}
> +		if (__btf_member_bitfield_size(kern_t, kern_member) ||
> +		    IS_ERR(btf_resolve_size(kern_btf,
> +					    btf_type_by_id(kern_btf, kern_member->type),
> +					    &kern_size)) ||
> +		    size != kern_size)
> +			goto err;
> +		mtype = btf_type_skip_modifiers(btf, member->type, NULL);
> +		kern_mtype = btf_type_skip_modifiers(kern_btf, kern_member->type, NULL);
> +		if (BTF_INFO_KIND(mtype->info) != BTF_INFO_KIND(kern_mtype->info))
> +			goto err;
> +		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;
> +		memcpy(value->data + __btf_member_bit_offset(kern_t, kern_member) / 8, src, size);
> +	}
> +	err = check_zero_holes(btf, t, data);

[Severity: High]
Can a crafted local BTF cause local_to_kern_value() to read past the end of
data?

map_update_elem() allocates data with size st_map->local_type->size
(t->size).  Unlike kernel BTF, a user-loaded BTF validated by BPF_BTF_LOAD
allows a BTF_KIND_INT member where member_type->size exceeds the remaining
bytes in t->size: btf_int_check_meta() only requires
BITS_ROUNDUP_BYTES(nr_bits) <= t->size, and btf_int_check_member() checks
struct_size - bytes_offset >= BITS_ROUNDUP_BYTES(nr_copy_bits) rather than
member_type->size, while btf_resolve_size() returns size =
member_type->size.  Because neither local_to_kern_value() nor
check_zero_holes() verifies that moff + size <= t->size, memchr_inv(src, 0,
size) on an absent member (or memcpy() on a matched member) can read out of
bounds on the kernel heap.

Also, __btf_member_bitfield_size(t, member) returns 0 whenever
!btf_type_kflag(t), so non-kflag bitfields and non-byte-aligned bit offsets
(__btf_member_bit_offset(t, member) % 8 != 0) are not rejected.  And
because btf_struct_check_meta() only checks that member offsets are
non-decreasing rather than non-overlapping (moff >= prev_mend), overlapping
members in t can move prev_mend backwards in check_zero_holes() and bypass
padding hole checks.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com?part=1

  reply	other threads:[~2026-11-30 22:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 21:44 [PATCH bpf-next v2 0/4] bpf: Support struct_ops in light skeletons Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF Siddharth Nayyar
2026-11-30 22:05   ` sashiko-bot [this message]
2026-12-01  4:21   ` Alexei Starovoitov
2026-11-30 21:44 ` [PATCH bpf-next v2 2/4] libbpf: Support struct_ops in light skeleton loader Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 4/4] selftests/bpf: Add light skeleton struct_ops tests Siddharth Nayyar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-242072@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sidnayyar@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.