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


Groups > linux.kernel > #1689259 > unrolled thread

[PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

Started byAlexander Popov <alex.popov@linux.com>
First post2017-07-17 18:50 +0200
Last post2017-07-17 20:30 +0200
Articles 7 — 4 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption Alexander Popov <alex.popov@linux.com> - 2017-07-17 18:50 +0200
    Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or  corruption Christopher Lameter <cl@linux.com> - 2017-07-17 19:00 +0200
    Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or  corruption Matthew Wilcox <willy@infradead.org> - 2017-07-17 20:00 +0200
      Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or  corruption Christopher Lameter <cl@linux.com> - 2017-07-17 20:20 +0200
        Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or  corruption Alexander Popov <alex.popov@linux.com> - 2017-07-17 21:10 +0200
          Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption Kees Cook <keescook@chromium.org> - 2017-07-17 21:20 +0200
      Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or  corruption Alexander Popov <alex.popov@linux.com> - 2017-07-17 20:30 +0200

#1689259 — [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

FromAlexander Popov <alex.popov@linux.com>
Date2017-07-17 18:50 +0200
Subject[PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption
Message-ID<u4hnA-7uP-27@gated-at.bofh.it>
Add an assertion similar to "fasttop" check in GNU C Library allocator:
an object added to a singly linked freelist should not point to itself.
That helps to detect some double free errors (e.g. CVE-2017-2636) without
slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
performance penalty.

Signed-off-by: Alexander Popov <alex.popov@linux.com>
---
 mm/slub.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mm/slub.c b/mm/slub.c
index 1d3f983..a106939b 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -261,6 +261,7 @@ static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
 
 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
 {
+	BUG_ON(object == fp); /* naive detection of double free or corruption */
 	*(void **)(object + s->offset) = fp;
 }
 
-- 
2.7.4

[toc] | [next] | [standalone]


#1689265 — Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

FromChristopher Lameter <cl@linux.com>
Date2017-07-17 19:00 +0200
SubjectRe: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption
Message-ID<u4hxf-7xZ-13@gated-at.bofh.it>
In reply to#1689259
On Mon, 17 Jul 2017, Alexander Popov wrote:

> Add an assertion similar to "fasttop" check in GNU C Library allocator:
> an object added to a singly linked freelist should not point to itself.
> That helps to detect some double free errors (e.g. CVE-2017-2636) without
> slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
> performance penalty.

We are adding up "unnoticable performance penalties". This is used
int both the critical allocation and free paths. Could this be
VM_BUG_ON()?

>
> Signed-off-by: Alexander Popov <alex.popov@linux.com>
> ---
>  mm/slub.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/mm/slub.c b/mm/slub.c
> index 1d3f983..a106939b 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -261,6 +261,7 @@ static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
>
>  static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
>  {
> +	BUG_ON(object == fp); /* naive detection of double free or corruption */
>  	*(void **)(object + s->offset) = fp;
>  }
>
>

[toc] | [prev] | [next] | [standalone]


#1689316 — Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

FromMatthew Wilcox <willy@infradead.org>
Date2017-07-17 20:00 +0200
SubjectRe: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption
Message-ID<u4itk-8aw-17@gated-at.bofh.it>
In reply to#1689259
On Mon, Jul 17, 2017 at 07:45:07PM +0300, Alexander Popov wrote:
> Add an assertion similar to "fasttop" check in GNU C Library allocator:
> an object added to a singly linked freelist should not point to itself.
> That helps to detect some double free errors (e.g. CVE-2017-2636) without
> slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
> performance penalty.

>  {
> +	BUG_ON(object == fp); /* naive detection of double free or corruption */
>  	*(void **)(object + s->offset) = fp;
>  }

Is BUG() the best response to this situation?  If it's a corruption, then
yes, but if we spot a double-free, then surely we should WARN() and return
without doing anything?

[toc] | [prev] | [next] | [standalone]


#1689334 — Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

FromChristopher Lameter <cl@linux.com>
Date2017-07-17 20:20 +0200
SubjectRe: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption
Message-ID<u4iMH-8w3-43@gated-at.bofh.it>
In reply to#1689316
On Mon, 17 Jul 2017, Matthew Wilcox wrote:

> On Mon, Jul 17, 2017 at 07:45:07PM +0300, Alexander Popov wrote:
> > Add an assertion similar to "fasttop" check in GNU C Library allocator:
> > an object added to a singly linked freelist should not point to itself.
> > That helps to detect some double free errors (e.g. CVE-2017-2636) without
> > slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
> > performance penalty.
>
> >  {
> > +	BUG_ON(object == fp); /* naive detection of double free or corruption */
> >  	*(void **)(object + s->offset) = fp;
> >  }
>
> Is BUG() the best response to this situation?  If it's a corruption, then
> yes, but if we spot a double-free, then surely we should WARN() and return
> without doing anything?

The double free debug checking already does the same thing in a more
thourough way (this one only checks if the last free was the same
address). So its duplicating a check that already exists. However, this
one is always on.

[toc] | [prev] | [next] | [standalone]


#1689365 — Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

FromAlexander Popov <alex.popov@linux.com>
Date2017-07-17 21:10 +0200
SubjectRe: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption
Message-ID<u4jz4-zE-15@gated-at.bofh.it>
In reply to#1689334
Hello Christopher,

Thanks for your reply.

On 17.07.2017 21:04, Christopher Lameter wrote:
> On Mon, 17 Jul 2017, Matthew Wilcox wrote:
> 
>> On Mon, Jul 17, 2017 at 07:45:07PM +0300, Alexander Popov wrote:
>>> Add an assertion similar to "fasttop" check in GNU C Library allocator:
>>> an object added to a singly linked freelist should not point to itself.
>>> That helps to detect some double free errors (e.g. CVE-2017-2636) without
>>> slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
>>> performance penalty.
>>
>>>  {
>>> +	BUG_ON(object == fp); /* naive detection of double free or corruption */
>>>  	*(void **)(object + s->offset) = fp;
>>>  }
>>
>> Is BUG() the best response to this situation?  If it's a corruption, then
>> yes, but if we spot a double-free, then surely we should WARN() and return
>> without doing anything?
> 
> The double free debug checking already does the same thing in a more
> thourough way (this one only checks if the last free was the same
> address). So its duplicating a check that already exists.

Yes, absolutely. Enabled slub_debug (or KASAN with its quarantine) can detect
more double-free errors. But it introduces much bigger performance penalty and
it's disabled by default.

> However, this one is always on.

Yes, I would propose to have this relatively cheap check enabled by default. I
think it will block a good share of double-free errors. Currently it's really
easy to turn such a double-free into use-after-free and exploit it, since, as I
wrote, next two kmalloc() calls return the same address. So we could make
exploiting harder for a relatively low price.

Christopher, if I change BUG_ON() to VM_BUG_ON(), it will be disabled by default
again, right?

Best regards,
Alexander

[toc] | [prev] | [next] | [standalone]


#1689373

FromKees Cook <keescook@chromium.org>
Date2017-07-17 21:20 +0200
Message-ID<u4jIJ-Dg-9@gated-at.bofh.it>
In reply to#1689365
On Mon, Jul 17, 2017 at 12:01 PM, Alexander Popov <alex.popov@linux.com> wrote:
> Hello Christopher,
>
> Thanks for your reply.
>
> On 17.07.2017 21:04, Christopher Lameter wrote:
>> On Mon, 17 Jul 2017, Matthew Wilcox wrote:
>>
>>> On Mon, Jul 17, 2017 at 07:45:07PM +0300, Alexander Popov wrote:
>>>> Add an assertion similar to "fasttop" check in GNU C Library allocator:
>>>> an object added to a singly linked freelist should not point to itself.
>>>> That helps to detect some double free errors (e.g. CVE-2017-2636) without
>>>> slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
>>>> performance penalty.
>>>
>>>>  {
>>>> +   BUG_ON(object == fp); /* naive detection of double free or corruption */
>>>>     *(void **)(object + s->offset) = fp;
>>>>  }
>>>
>>> Is BUG() the best response to this situation?  If it's a corruption, then
>>> yes, but if we spot a double-free, then surely we should WARN() and return
>>> without doing anything?
>>
>> The double free debug checking already does the same thing in a more
>> thourough way (this one only checks if the last free was the same
>> address). So its duplicating a check that already exists.
>
> Yes, absolutely. Enabled slub_debug (or KASAN with its quarantine) can detect
> more double-free errors. But it introduces much bigger performance penalty and
> it's disabled by default.
>
>> However, this one is always on.
>
> Yes, I would propose to have this relatively cheap check enabled by default. I
> think it will block a good share of double-free errors. Currently it's really
> easy to turn such a double-free into use-after-free and exploit it, since, as I
> wrote, next two kmalloc() calls return the same address. So we could make
> exploiting harder for a relatively low price.
>
> Christopher, if I change BUG_ON() to VM_BUG_ON(), it will be disabled by default
> again, right?

Let's merge this with the proposed CONFIG_FREELIST_HARDENED, then the
performance change is behind a config, and we gain the rest of the
freelist protections at the same time:

http://www.openwall.com/lists/kernel-hardening/2017/07/06/1

-Kees

-- 
Kees Cook
Pixel Security

[toc] | [prev] | [next] | [standalone]


#1689337 — Re: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption

FromAlexander Popov <alex.popov@linux.com>
Date2017-07-17 20:30 +0200
SubjectRe: [PATCH 1/1] mm/slub.c: add a naive detection of double free or corruption
Message-ID<u4iWm-7E-15@gated-at.bofh.it>
In reply to#1689316
On 17.07.2017 20:54, Matthew Wilcox wrote:
> On Mon, Jul 17, 2017 at 07:45:07PM +0300, Alexander Popov wrote:
>> Add an assertion similar to "fasttop" check in GNU C Library allocator:
>> an object added to a singly linked freelist should not point to itself.
>> That helps to detect some double free errors (e.g. CVE-2017-2636) without
>> slub_debug and KASAN. Testing with hackbench doesn't show any noticeable
>> performance penalty.
> 
>>  {
>> +	BUG_ON(object == fp); /* naive detection of double free or corruption */
>>  	*(void **)(object + s->offset) = fp;
>>  }
> 
> Is BUG() the best response to this situation?  If it's a corruption, then
> yes, but if we spot a double-free, then surely we should WARN() and return
> without doing anything?

Hello Matthew,

Double-free leads to the memory corruption too, since the next two kmalloc()
calls return the same address to their callers. And we can spot it early here.

--
Alexander

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web