public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Hillf Danton <hdanton@sina.com>
Cc: dhowells@redhat.com,
	syzbot+3538a6a72efa8b059c38@syzkaller.appspotmail.com,
	linux-afs@lists.infradead.org, linux-kernel@vger.kernel.org,
	syzkaller-bugs@googlegroups.com
Subject: Re: [syzbot] KASAN: use-after-free Read in rxrpc_lookup_local
Date: Wed, 07 Dec 2022 16:30:41 +0000	[thread overview]
Message-ID: <1504932.1670430641@warthog.procyon.org.uk> (raw)
In-Reply-To: <20221207134234.7879-1-hdanton@sina.com>

Hillf Danton <hdanton@sina.com> wrote:

> > Hmmm...  That can't be the whole explanation.  As you say, the hlist_del is
> > done under the mutex in rxrpc_destroy_local() - the same as the
> > hlist_add/hlist_replace and the search in rxrpc_lookup_local().
> 
> The uaf is simple and due to local ep freed without being deleted from
> rxnet->local_endpoints while the list walker in rxrpc_lookup_local() stumbles
> on it. rxrpc_destroy_local() is irrelevant as per the syzbot report.

No, that's not the explanation.  We can't get that far unless the I/O thread
got started - kthread_run() has to wait for the task_struct pointer to become
available - and the I/O thread should remove the link before it exits and
before the rxrpc_local object is deallocated.

I've tracked the problem down.  It's due to kthread(), specifically this bit:

	preempt_disable();
	complete(done);
	schedule_preempt_disabled();
	preempt_enable();

	ret = -EINTR;
	if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
		cgroup_kthread_ready();
		__kthread_parkme(self);
		ret = threadfn(data);
	}

So the complete() is done before we've decided if we're going to call
threadfn() or return an error.  This permits kthread_run() to resume before
we've checked KTHREAD_SHOULD_STOP - thus if kthread_stop() is called quickly
enough by the rxrpc socket being released, kthread() skips calling threadfn(),
but kthread_run() returns success.

The fact that the thread didn't start doesn't get seen until kthread_stop() is
called.

I think the above code needs rearranging slightly.  KTHREAD_SHOULD_STOP should
be tested and, if needed EINTR set, *before* complete(done).  That way
kthread_run() will return -EINTR in this case.

David


  parent reply	other threads:[~2022-12-07 16:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221207030740.7663-1-hdanton@sina.com>
2022-12-07  3:42 ` [syzbot] KASAN: use-after-free Read in rxrpc_lookup_local syzbot
2022-12-07 11:57 ` David Howells
     [not found] ` <20221207134234.7879-1-hdanton@sina.com>
2022-12-07 16:30   ` David Howells [this message]
2022-12-06 16:34 syzbot
2022-12-06 21:09 ` David Howells
2022-12-07  3:09   ` syzbot
2022-12-07 17:43 ` David Howells
2022-12-07 23:16   ` syzbot
     [not found] ` <20221208035617.8037-1-hdanton@sina.com>
2022-12-08  8:53   ` David Howells
2022-12-08 11:20 ` David Howells
2022-12-08 12:02   ` 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=1504932.1670430641@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=hdanton@sina.com \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzbot+3538a6a72efa8b059c38@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox