public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Mark Rutland <mark.rutland@arm.com>
Cc: dhowells@redhat.com, linux-kernel@vger.kernel.org,
	Elena Reshetova <elena.reshetova@intel.com>,
	keyrings@vger.kernel.org, Kees Cook <keescook@chromium.org>,
	Hans Liljestrand <ishkamiel@gmail.com>,
	David Windsor <dwindsor@gmail.com>,
	James Morris <james.l.morris@oracle.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>
Subject: Re: next-20170510 refcount_inc() on zero / use-after-free in key_lookup()
Date: Fri, 12 May 2017 16:29:39 +0100	[thread overview]
Message-ID: <1265.1494602979@warthog.procyon.org.uk> (raw)
In-Reply-To: <20170512140023.GA18818@leverpostej>

Mark Rutland <mark.rutland@arm.com> wrote:

> From a quick look at key_lookup(), the following looks very suspicious:
> 
> found:
>         /* pretend it doesn't exist if it is awaiting deletion */
>         if (refcount_read(&key->usage) == 0)
>                 goto not_found;
> 
>         /* this races with key_put(), but that doesn't matter since key_put()
>          * doesn't actually change the key
>          */
>         __key_get(key);
> 
> ... as if we can race with key_put(), we can see a zero refcount here,
> and the race *does* matter.

No, it doesn't.

If key_put() reduces a refcount to 0, it doesn't do anything other than poke
the gc thread:

	void key_put(struct key *key)
	{
		if (key) {
			key_check(key);

			if (refcount_dec_and_test(&key->usage))
				schedule_work(&key_gc_work);
		}
	}

in particular, no indication of the reduced key is passed.

The gc thread scans the entire key serial tree under the key_serial_lock
looking for keys that are no longer ref'd.  No one else is allowed to remove
keys from the tree.  This means that the gc thread can safely leave a cursor
pointing into the midst of the tree with no locks held whilst it yields to the
scheduler.

The code you quoted above in key_lookup() is inside the key_serial_lock, so it
prevents the gc thread from culling a key when it resurrects it.

So the problem isn't the key code, it's the refcount code.

As I've said before, the refcount code needs an increment op that permits
inc-from-0.  In this case, it's perfectly okay.

David

  reply	other threads:[~2017-05-12 15:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-12 14:00 next-20170510 refcount_inc() on zero / use-after-free in key_lookup() Mark Rutland
2017-05-12 15:29 ` David Howells [this message]
2017-05-12 16:22   ` Mark Rutland

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=1265.1494602979@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=dwindsor@gmail.com \
    --cc=elena.reshetova@intel.com \
    --cc=ishkamiel@gmail.com \
    --cc=james.l.morris@oracle.com \
    --cc=keescook@chromium.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.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