Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Siddharth Nayyar" <sidnayyar@google.com>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Quentin Monnet" <qmo@kernel.org>,
	"Shuah Khan" <shuah@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	"Anton Protopopov" <a.s.protopopov@gmail.com>,
	"Giuliano Procida" <gprocida@google.com>,
	"Neill Kapron" <nkapron@google.com>,
	"Thiébaud Weksteen" <tweek@google.com>,
	"Maxime Ripard" <mripard@kernel.org>
Subject: Re: [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF
Date: Tue, 01 Dec 2026 04:21:44 +0000	[thread overview]
Message-ID: <DN93XA2FA90P.3HBPPF7PFIDA6@gmail.com> (raw)
In-Reply-To: <20261130-lskel-struct-ops-v2-1-8b76e65073ab@google.com>

On Mon, Nov 30, 2026 at 01:44 PM Siddharth Nayyar <sidnayyar@google.com> 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

  reply	other threads:[~2026-12-01  4:21 UTC|newest]

Thread overview: 6+ 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-12-01  4:21   ` Alexei Starovoitov [this message]
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=DN93XA2FA90P.3HBPPF7PFIDA6@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=a.s.protopopov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=gprocida@google.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mripard@kernel.org \
    --cc=nkapron@google.com \
    --cc=qmo@kernel.org \
    --cc=shuah@kernel.org \
    --cc=sidnayyar@google.com \
    --cc=song@kernel.org \
    --cc=tweek@google.com \
    --cc=yonghong.song@linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox