bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Luis Gerhorst <luis.gerhorst@fau.de>
Cc: andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org,
	daniel@iogearbox.net, 	haoluo@google.com,
	john.fastabend@gmail.com, jolsa@kernel.org, kpsingh@kernel.org,
		linux-kernel@vger.kernel.org, martin.lau@linux.dev,
	sdf@fomichev.me, 	song@kernel.org,
	syzkaller-bugs@googlegroups.com, yonghong.song@linux.dev
Subject: Re: [syzbot] [bpf?] KASAN: slab-use-after-free Read in do_check
Date: Wed, 11 Jun 2025 10:20:40 -0700	[thread overview]
Message-ID: <b6931bd0dd72327c55287862f821ca6c4c3eb69a.camel@gmail.com> (raw)
In-Reply-To: <87frg6gysw.fsf@fau.de>

On Wed, 2025-06-11 at 16:03 +0200, Luis Gerhorst wrote:
> Eduard Zingerman <eddyz87@gmail.com> writes:
> 
> > Accessed memory is freed at an error path in push_stack():
> > 
> >   static struct bpf_verifier_state *push_stack(...)
> >   {
> >   	...
> >   err:
> >   	free_verifier_state(env->cur_state, true); // <-- KASAN points here
> >   	...
> >   }
> > 
> > And is accessed after being freed here:
> > 
> >   static int do_check(struct bpf_verifier_env *env)
> >   {
> >   	...
> > 		err = do_check_insn(env, &do_print_state);
> > KASAN -->	if (state->speculative && error_recoverable_with_nospec(err)) ...
> >   	...
> >   }
> >   
> > [...]
> > 
> > Either 'state = env->cur_state' is needed after 'do_check_insn()' or
> > error path should not free env->cur_state (seems logical).
> 
> Sorry, this was my error from [1]. Thanks for the pointer.
> 
> Yes, I think the former makes sense (with the respective `state &&`
> added to the if).
> 
> The latter might also be possible, but I guess it would require more
> significant changes.

do_check_common() has the following logic:

   out:
         /* check for NULL is necessary, since cur_state can be freed inside                                                                                                                                                                                                                           
          * do_check() under memory pressure.                                                                                                                                                                                                                                                          
          */
         if (env->cur_state) {
                 free_verifier_state(state: env->cur_state, free_self: true);
                 env->cur_state = NULL;
         }
         while (!pop_stack(env, prev_insn_idx: NULL, insn_idx: NULL, pop_log: false));
         if (!ret && pop_log)
                 bpf_vlog_reset(log: &env->log, new_pos: 0);
         free_states(env);
         return ret;

Same cleanup cycles are done in push_stack() and push_async_cb(),
both functions are only reachable from do_check_common() via
do_check() -> do_check_insn().

Hence, I think that cur state should not be freed in push_*()
functions and pop_stack() loop there is not needed.

> state->speculative does not make sense if the error path of push_stack()
> ran. In that case, `state->speculative &&
> error_recoverable_with_nospec(err)` as a whole should already never
> evaluate to true (because all cases where push_stack() fails also return
> a non-recoverable error -ENOMEM/-EFAULT).
> 
> Alternatively to adding `state = env->cur_state` and `state &&`, turning
> the check around would avoid the use-after-free. However, I think your
> idea is better because it is more explicit compared to this:
> 
> 	if (error_recoverable_with_nospec(err) && state->speculative) ...
> 
> Does this make sense to you? If yes I can send the fix later today.

I think this flip makes perfect sense and should be done.

[...]

  reply	other threads:[~2025-06-11 17:20 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-11 12:36 [syzbot] [bpf?] KASAN: slab-use-after-free Read in do_check syzbot
2025-06-11 13:02 ` Eduard Zingerman
2025-06-11 14:03   ` Luis Gerhorst
2025-06-11 17:20     ` Eduard Zingerman [this message]
2025-06-11 21:07       ` [PATCH bpf-next] bpf: Fix state use-after-free on push_stack() err Luis Gerhorst
2025-06-11 22:23         ` Eduard Zingerman
2025-06-11 23:10         ` patchwork-bot+netdevbpf
2025-06-11 21:14       ` [PATCH bpf-next] bpf: Remove redundant free_verifier_state()/pop_stack() Luis Gerhorst
2025-06-11 22:36         ` Eduard Zingerman
2025-06-13  9:01           ` [PATCH bpf-next v2] " Luis Gerhorst
2025-06-13 21:17             ` Eduard Zingerman
2025-06-13 22:06               ` Alexei Starovoitov
2025-06-13  9:07           ` [PATCH bpf-next] " Luis Gerhorst
2025-06-11 21:32       ` [syzbot] [bpf?] KASAN: slab-use-after-free Read in do_check Luis Gerhorst
2025-06-11 21:43         ` Eduard Zingerman
2025-06-11 21:40 ` Eduard Zingerman
2025-06-11 23:00   ` syzbot

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=b6931bd0dd72327c55287862f821ca6c4c3eb69a.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luis.gerhorst@fau.de \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=syzkaller-bugs@googlegroups.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;
as well as URLs for NNTP newsgroup(s).