All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leon Hwang <leon.hwang@linux.dev>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev,
	song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev, hawk@kernel.org,
	john.fastabend@gmail.com, sdf@fomichev.me, rostedt@goodmis.org,
	mhiramat@kernel.org, mathieu.desnoyers@efficios.com,
	shuah@kernel.org, kuniyu@google.com, liuhangbin@gmail.com,
	mkarsten@uwaterloo.ca, masashi.honma@gmail.com,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, martin.lau@kernel.org,
	mason@kernel.org
Subject: Re: [PATCH bpf-next v4 5/6] libbpf: Add log_opts support for bpf_link_create
Date: Mon, 26 Oct 2026 23:08:11 +0800	[thread overview]
Message-ID: <8b2c8f88-d03b-4146-9bf7-95c4981ecacc@linux.dev> (raw)
In-Reply-To: <c6da6df95673d3a5ab5c4e757500658821e9e9e8d0652f737eca82690765a9a8@mail.kernel.org>

On 2026/10/25 00:13, bot+bpf-ci@kernel.org wrote:
>> libbpf: Add log_opts support for bpf_link_create
> 
> TL;DR: In bpf_link_create(), the shared iter_info/log_opts slot is keyed only
> on iter_info_len, so an iter_info pointer with len 0 may be misread as
> log_opts, possibly causing out-of-bounds reads/writes in libbpf.
> 
>> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
>> index 9a2107d4f6d8..00efa90a6ed9 100644
>> --- a/tools/lib/bpf/bpf.c
>> +++ b/tools/lib/bpf/bpf.c
> 
> [ ... ]
> 
>> @@ -803,6 +805,14 @@ int bpf_link_create(int prog_fd, int target_fd,
>>  	iter_info_len = OPTS_GET(opts, iter_info_len, 0);
>>  	target_btf_id = OPTS_GET(opts, target_btf_id, 0);
>>  
>> +	if (iter_info_len && attach_type != BPF_TRACE_ITER)
>> +		return libbpf_err(-EINVAL);
>> +
>> +	if (iter_info_len)
>> +		iter_info = OPTS_GET(opts, iter_info, (void *)0);
>> +	else
>> +		log_opts = OPTS_GET(opts, log_opts, (void *)0);
>> +
>>  	/* validate we don't have unexpected combinations of non-zero fields */
>>  	if (iter_info_len || target_btf_id) {
>>  		if (iter_info_len && target_btf_id)
> 
> For BPF_TRACE_ITER, iter_info_len is now the only thing that decides whether
> the shared iter_info/log_opts slot holds a union bpf_iter_link_info pointer
> or a struct bpf_log_opts pointer.
> 
> Can a caller that sets opts->iter_info but leaves opts->iter_info_len at 0
> end up with its union bpf_iter_link_info being read as a struct
> bpf_log_opts here?

Users should not do like this. By convention, iter_info_len==0 means no
iter_info.

> 
> bpf_program__attach_iter() copies opts->link_info into
> link_create_opts.iter_info and opts->link_info_len into iter_info_len
> without checking that they agree, and nothing in bpf.h or libbpf.h says
> log_opts is only used when iter_info_len == 0, or that iter_info needs
> iter_info_len.
> 
> Before this patch the pointer went to the kernel, and bpf_iter_link_attach()
> rejected the mismatch cleanly:
> 
>     if (bpfptr_is_null(ulinfo) ^ !linfo_len)
>         return -EINVAL;
> 
> With this patch, __sys_bpf_fd() runs OPTS_VALID(log_opts, bpf_log_opts) on
> the 16-byte union bpf_iter_link_info.  The first 8 bytes of that union
> (map.map_fd, or task.tid/pid, or cgroup.order/cgroup_fd) become
> log_opts->sz:
> 
> - If map_fd or tid is in the range 8..27, sz passes validation, and size and
>   level are read from bytes 16..24, which lie past the end of the 16-byte
>   object.
> 
> - If sz is 28 to 31, OPTS_HAS(log_opts, true_size) is true, so
>   OPTS_SET(log_opts, true_size, ...) writes 4 bytes at offset 24, which is 8
>   bytes past the end of the caller's object.  Can this corrupt the caller's
>   stack or heap?
> 
> - If sz is larger (a typical tid, or a cgroup fd shifted into the high 32
>   bits), libbpf_is_mem_zeroed() scans caller memory from offset 28 up to sz.
>   It usually stops with a "bpf_log_opts has non-zero extra bytes" warning,
>   but it reads out of bounds first.
> 
> In addition, attr.link_create.iter_info is now NULL with iter_info_len of 0,
> which the kernel accepts.  If validation does pass, could a task iterator
> attach to all tasks instead of the requested tid, and a map iterator fail
> with -EBADF instead of -EINVAL?
> 
> The commit message says log_opts support for iter links is future work.
> Would it be better to treat the slot as iter_info whenever
> attach_type == BPF_TRACE_ITER, and read log_opts only for other attach
> types, instead of keying only on iter_info_len?  That would keep the old
> behaviour for iter links exactly.

Not exactly. iter_info and iter_info_len must be set at the same time
for iter links. If iter_info_len is 0, iter_info must be NULL or zeroed
memory. Users must not misuse them by iter_info_len==0 but iter_info is
not zeroed memory.

Thanks,
Leon


  reply	other threads:[~2026-10-26 15:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-24 15:36 [PATCH bpf-next v4 0/6] bpf: Add syscall common attrs support for xdp link_create Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 1/6] bpf: Add syscall common attrs support for link_create Leon Hwang
2026-10-24 15:50   ` sashiko-bot
2026-10-24 15:36 ` [PATCH bpf-next v4 2/6] bpf: Add syscall common attrs support for xdp link_create Leon Hwang
2026-10-25  1:19   ` Alexei Starovoitov
2026-10-26 15:06     ` Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 3/6] bpf: Retire tracepoint bpf_xdp_link_attach_failed Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 4/6] libbpf: Factor out __sys_bpf_fd helper Leon Hwang
2026-10-24 16:13   ` bot+bpf-ci
2026-10-26 15:07     ` Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 5/6] libbpf: Add log_opts support for bpf_link_create Leon Hwang
2026-10-24 16:13   ` bot+bpf-ci
2026-10-26 15:08     ` Leon Hwang [this message]
2026-10-24 15:36 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add tests to verify link_create logs Leon Hwang

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=8b2c8f88-d03b-4146-9bf7-95c4981ecacc@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=liuhangbin@gmail.com \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=masashi.honma@gmail.com \
    --cc=mason@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mkarsten@uwaterloo.ca \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --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 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.