netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Westphal <fw@strlen.de>
To: eric gisse <jowr.pi@gmail.com>
Cc: netfilter-devel@vger.kernel.org, minipli@googlemail.com
Subject: Re: bug report: use after free bug leading to kernel panic
Date: Fri, 31 Oct 2014 17:50:42 +0100	[thread overview]
Message-ID: <20141031165042.GN10069@breakpoint.cc> (raw)
In-Reply-To: <CAHZ_AjsbkkXNMG8oZfOhr6WbUmV=4eK0j23ijbP=cNkfVdoAjQ@mail.gmail.com>

eric gisse <jowr.pi@gmail.com> wrote:
> Background:
> 
> This was discovered on a server running a tor exit node (crazy high
> packet flow) with a firewall that uses a few connection tracking rules
> in the INPUT chain:
> 
> # iptables-save | grep conn
> -A INPUT -m comment --comment "001-v4 drop invalid traffic" -m
> conntrack --ctstate INVALID -j DROP
> -A INPUT -m comment --comment "990-v4 accept existing connections" -m
> conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
> 
> The kernel was not stock, but rather was modified with grsecurity. I
> worked with the grsecurity folks first on this issue (
> https://forums.grsecurity.net/viewtopic.php?f=1&t=4071 ) to isolate
> and explain what's going on. They were very helpful.

Thanks for reporting.

> because netconsole is ... inconsistent with when choosing to work. As
> an aside, what is the ideal way to get kernel oops output anyway?

booting into a crash-kernel has worked for me in the past to salvage
original trace from memory.

> Note: please Ignore the xt_* modules as they were not in use at the
> time, and were not present for either the 3.16.5 panics or the 3.17.1
> + sanitize test case patch.

Just to be clear, the 3.16.5 panic is also with pax memory
sanitizing...?

> The spot of code that's causing grief:
> 
> # addr2line -e vmlinux -fip ffffffff814b58ce
> nf_ct_tuplehash_to_ctrack at
> /usr/src/linux/include/net/netfilter/nf_conntrack.h:122
>  (inlined by) nf_ct_key_equal at
> /usr/src/linux/net/netfilter/nf_conntrack_core.c:393
>  (inlined by) ____nf_conntrack_find at
> /usr/src/linux/net/netfilter/nf_conntrack_core.c:422
>  (inlined by) __nf_conntrack_find_get at
> /usr/src/linux/net/netfilter/nf_conntrack_core.c:453

Thanks.
So this happens when we walk the conntrack hash lists to find
a matching entry.

> 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.

(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.

  reply	other threads:[~2014-10-31 16:50 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 [this message]
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

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=20141031165042.GN10069@breakpoint.cc \
    --to=fw@strlen.de \
    --cc=jowr.pi@gmail.com \
    --cc=minipli@googlemail.com \
    --cc=netfilter-devel@vger.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).