All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mathias Krause <minipli@googlemail.com>
To: Florian Westphal <fw@strlen.de>
Cc: eric gisse <jowr.pi@gmail.com>,
	netfilter-devel@vger.kernel.org, PaX Team <pageexec@freemail.hu>,
	Brad Spengler <spender@grsecurity.net>
Subject: Re: bug report: use after free bug leading to kernel panic
Date: Fri, 31 Oct 2014 18:46:42 +0100	[thread overview]
Message-ID: <20141031174642.GA22605@jig.fritz.box> (raw)
In-Reply-To: <20141031165042.GN10069@breakpoint.cc>

On Fri, Oct 31, 2014 at 05:50:42PM +0100, Florian Westphal wrote:
> eric gisse <jowr.pi@gmail.com> wrote:
> > diff --git a/mm/slub.c b/mm/slub.c
> > index 3e8afcc07a76..08a7cbcf2274 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -2643,6 +2643,12 @@ static __always_inline void slab_free(struct kmem_cache *s,
> >  
> >  	slab_free_hook(s, x);
> >  
> > +	if (pax_sanitize_slab && !(s->flags & SLAB_NO_SANITIZE)) {
> > +		memset(x, PAX_MEMORY_SANITIZE_VALUE, s->object_size);
> > +		if (s->ctor)
> > +			s->ctor(x);
> > +	}
> > +
> 
> I am no SLUB expert, but this looks wrong.
> slab_free() is called directly via kmem_cache_free().
> 
> conntrack objects are alloc'd/free'd from a SLAB_DESTROY_BY_RCU cache.
> 
> It is therefore legal to access a conntrack object from another
> CPU even after kmem_cache_free() was invoked on another cpu, provided all
> readers that do so hold rcu_read_lock, and verify that object has not been
> freed yet by issuing appropriate atomic_inc_not_zero calls.
> 
> Therefore, object poisoning will only be safe from rcu callback, after
> accesses are known to be illegal/invalid.

Snap, you're right! I was misreading the following comment in
include/linux/slab.h to allow "free reuse" by the slab allocator as
well, e.g. for sanitizing/poisoning the object:

 * SLAB_DESTROY_BY_RCU - **WARNING** READ THIS!
 *  
 * This delays freeing the SLAB page by a grace period, it does _NOT_
 * delay object freeing. This means that if you do kmem_cache_free()
 * that memory location is free to be reused at any time. Thus it may
 * be possible to see another object there in the same RCU grace period.

But, in fact, that assumption is not true. I now see how the conntrack
code exploits this feature by testing &ct->ct_general.use. So if we
scratch that by writing '\xfe' everywhere over the object, that test
will no longer work.

I guess we need to change the slab sanitization feature in PaX to handle
SLAB_DESTROY_BY_RCU marked slabs the way they need to.

> 
> (not saying that conntrack is bug free..., we had races there in the
>  past).
> 
> From a short glance at SLUB it seems poisoning objects for SLAB_DESTROY_BY_RCU
> caches is safe in __free_slab(), but not earlier.
> 
> If you use different allocator, please tell us which one (check kernel
> config, slub is default).
> 
> If its reproduceable with poisoning done after the RCU grace periods
> have elapsed (i.e., where its not legal anymore to access the memory),
> please let us know and we can have another look at it.
> 

Thanks,
Mathias

      parent reply	other threads:[~2014-10-31 17:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-31 15:28 bug report: use after free bug leading to kernel panic eric gisse
2014-10-31 16:50 ` Florian Westphal
2014-10-31 17:30   ` eric gisse
2014-10-31 22:00     ` Florian Westphal
2014-11-01 10:56       ` Mathias Krause
2014-10-31 17:46   ` Mathias Krause [this message]

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=20141031174642.GA22605@jig.fritz.box \
    --to=minipli@googlemail.com \
    --cc=fw@strlen.de \
    --cc=jowr.pi@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pageexec@freemail.hu \
    --cc=spender@grsecurity.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.