Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1640544

Re: next-20170510 refcount_inc() on zero / use-after-free in key_lookup()

From David Howells <dhowells@redhat.com>
Newsgroups linux.kernel
Subject Re: next-20170510 refcount_inc() on zero / use-after-free in key_lookup()
Date 2017-05-12 17:30 +0200
Message-ID <tGkFY-6vb-25@gated-at.bofh.it> (permalink)
References <tGjqA-5DF-85@gated-at.bofh.it>
Organization Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903

Show all headers | View raw


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

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

next-20170510 refcount_inc() on zero / use-after-free in key_lookup() Mark Rutland <mark.rutland@arm.com> - 2017-05-12 16:10 +0200
  Re: next-20170510 refcount_inc() on zero / use-after-free in key_lookup() David Howells <dhowells@redhat.com> - 2017-05-12 17:30 +0200
    Re: next-20170510 refcount_inc() on zero / use-after-free in  key_lookup() Mark Rutland <mark.rutland@arm.com> - 2017-05-12 18:30 +0200

csiph-web