linux-sgx.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Dave Hansen <dave.hansen@intel.com>, linux-sgx@vger.kernel.org
Cc: stable@vger.kernel.org, Sean Christopherson <seanjc@google.com>,
	Haitao Huang <haitao.huang@linux.intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Jethro Beekman <jethro@fortanix.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5] x86/sgx: Fix use-after-free in sgx_mmu_notifier_release()
Date: Sat, 30 Jan 2021 21:20:39 +0200	[thread overview]
Message-ID: <fa43948ba860d6ac99adabad3d8b6ff11f5d2239.camel@kernel.org> (raw)
In-Reply-To: <9dd2a962-2328-8784-9aed-b913502e1102@intel.com>

On Thu, 2021-01-28 at 08:33 -0800, Dave Hansen wrote:
> On 1/28/21 4:58 AM, Jarkko Sakkinen wrote:
> > The most trivial example of a race condition can be demonstrated by this
> > sequence where mm_list contains just one entry:
> > 
> > CPU A                           CPU B
> > -> sgx_release()
> >                                 -> sgx_mmu_notifier_release()
> >                                 -> list_del_rcu()
> >                                 <- list_del_rcu()
> > -> kref_put()
> > -> sgx_encl_release()
> >                                 -> synchronize_srcu()
> > -> cleanup_srcu_struct()
> 
> This is missing some key details including a clear, unambiguous, problem
> statement.  To me, the patch should concentrate on the SRCU warning
> since that's where we started.  Here's the detail that needs to be added
> about the issue and the locking in general in this path:
> 
> sgx_release() also does this:
> 
>         mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);
> 
> which does another synchronize_srcu() on the mmu_notifier's srcu_struct.
>  *But*, it only does this if its own list_del_rcu() is successful.  It
> does all of this before the kref_put().
> 
> In other words, sgx_release() can *only* get to this buggy path if
> sgx_mmu_notifier_release() races with sgx_release and does a
> list_del_rcu() first.
> 
> The key to this patch is that the sgx_mmu_notifier_release() will now
> take an 'encl' reference in that case, which prevents kref_put() from
> calling sgx_release() which cleans up and frees 'encl'.
> 
> I was actually also hoping to see some better comments about the new
> refcount, and the locking in general.  There are *TWO* struct_srcu's in
> play, a spinlock and a refcount.  I took me several days with Sean and
> your help to identify the actual path and get a proper fix (versions 1-4
> did *not* fix the race).

This was really good input, thank you. It made realize something but
now I need a sanity check.

I think that this bug fix is *neither* a legit one :-)

Example scenario would such that all removals "side-channel" through
the notifier callback. Then mmu_notifier_unregister() gets called
exactly zero times. No MMU notifier srcu sync would be then happening.

NOTE: There's bunch of other examples, I'm just giving one.

How I think this should be actually fixed is:

1. Whenever MMU notifier is *registered* kref_get() should be called for
   the enclave reference count.
2. *BOTH* sgx_release() and sgx_mmu_notifier_release() should
   decrease the refcount when they process an entry.
   
I.e. the fix that I sent does kref_get() in wrong location. Please
sanity check my conclusion. 
 
> Also, the use-after-free is *fixed* in sgx_mmu_notifier_release() but
> does not *occur* in sgx_mmu_notifier_release().  The subject here is a
> bit misleading in that regard.

Right, this is a valid point. It's incorrect. So if I just change the
short summary by substituting sgx_mmu_notifier_release() with
sgx_release()?

/Jarkko

  reply	other threads:[~2021-01-30 19:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-28 12:58 [PATCH v5] x86/sgx: Fix use-after-free in sgx_mmu_notifier_release() Jarkko Sakkinen
2021-01-28 16:33 ` Dave Hansen
2021-01-30 19:20   ` Jarkko Sakkinen [this message]
2021-01-30 19:26     ` Jarkko Sakkinen
2021-02-03 15:46     ` Dave Hansen
2021-02-03 21:54       ` Jarkko Sakkinen

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=fa43948ba860d6ac99adabad3d8b6ff11f5d2239.camel@kernel.org \
    --to=jarkko@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@intel.com \
    --cc=haitao.huang@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jethro@fortanix.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /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).