* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
[not found] <577f7e55.4668420a.84f17.5cb9SMTPIN_ADDED_MISSING@mx.google.com>
@ 2016-07-08 13:45 ` Christoph Lameter
2016-07-08 16:07 ` Kees Cook
0 siblings, 1 reply; 9+ messages in thread
From: Christoph Lameter @ 2016-07-08 13:45 UTC (permalink / raw)
To: Michael Ellerman
Cc: Kees Cook, kernel-hardening, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov, lin,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Schauf ler, Andrew Morton, linuxppc-dev, David S. Miller
On Fri, 8 Jul 2016, Michael Ellerman wrote:
> > I wonder if this code should be using size_from_object() instead of s->size?
>
> Hmm, not sure. Who's SLUB maintainer? :)
Me.
s->size is the size of the whole object including debugging info etc.
ksize() gives you the actual usable size of an object.
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-08 13:45 ` [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support Christoph Lameter
@ 2016-07-08 16:07 ` Kees Cook
2016-07-08 16:20 ` Christoph Lameter
0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2016-07-08 16:07 UTC (permalink / raw)
To: Christoph Lameter
Cc: Michael Ellerman, kernel-hardening, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov, lin,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Schauf ler, Andrew Morton, linuxppc-dev, David S. Miller
On Fri, Jul 8, 2016 at 9:45 AM, Christoph Lameter <cl@linux.com> wrote:
> On Fri, 8 Jul 2016, Michael Ellerman wrote:
>
>> > I wonder if this code should be using size_from_object() instead of s->size?
BTW, I can't reproduce this on x86 yet...
>>
>> Hmm, not sure. Who's SLUB maintainer? :)
>
> Me.
>
> s->size is the size of the whole object including debugging info etc.
> ksize() gives you the actual usable size of an object.
Is check_valid_pointer() making sure the pointer is within the usable
size? It seemed like it was checking that it was within the slub
object (checks against s->size, wants it above base after moving
pointer to include redzone, etc).
I think a potential problem with Michael's fix is that the ptr in
__check_heap_object() may not point at the _start_ of the usable
object, so doing the red zone shift isn't quite right.
This finds the ptr's offset within the slub object (since s->size is
the slub object size):
offset = (ptr - page_address(page)) % s->size;
But this looks at object_size and doesn't take into account actual size:
if (offset <= s->object_size && n <= s->object_size - offset)
return NULL;
I think offset needs to be adjusted by the size of padding, which the
restore_red_left() call had the same effect, but may not cover all
padding conditions? I'm not sure.
Should it be:
/* Find offset within slab object. */
offset = (ptr - page_address(page)) % s->size;
/* Adjust offset for meta data and padding. */
offset -= s->size - s->object_size;
/* Make sure offset and size are within bounds of the
allocation size. */
if (offset <= s->object_size && n <= s->object_size - offset)
return NULL;
?
-Kees
--
Kees Cook
Chrome OS & Brillo Security
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-08 16:07 ` Kees Cook
@ 2016-07-08 16:20 ` Christoph Lameter
2016-07-08 17:41 ` Kees Cook
0 siblings, 1 reply; 9+ messages in thread
From: Christoph Lameter @ 2016-07-08 16:20 UTC (permalink / raw)
To: Kees Cook
Cc: Michael Ellerman, kernel-hardening, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov, lin,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Schauf ler, Andrew Morton, linuxppc-dev, David S. Miller
On Fri, 8 Jul 2016, Kees Cook wrote:
> Is check_valid_pointer() making sure the pointer is within the usable
> size? It seemed like it was checking that it was within the slub
> object (checks against s->size, wants it above base after moving
> pointer to include redzone, etc).
check_valid_pointer verifies that a pointer is pointing to the start of an
object. It is used to verify the internal points that SLUB used and
should not be modified to do anything different.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-08 16:20 ` Christoph Lameter
@ 2016-07-08 17:41 ` Kees Cook
2016-07-08 20:48 ` Kees Cook
0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2016-07-08 17:41 UTC (permalink / raw)
To: Christoph Lameter
Cc: Michael Ellerman, kernel-hardening, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Schauf ler, Andrew Morton, linuxppc-dev, David S. Miller,
linux-arm-kernel
On Fri, Jul 8, 2016 at 12:20 PM, Christoph Lameter <cl@linux.com> wrote:
> On Fri, 8 Jul 2016, Kees Cook wrote:
>
>> Is check_valid_pointer() making sure the pointer is within the usable
>> size? It seemed like it was checking that it was within the slub
>> object (checks against s->size, wants it above base after moving
>> pointer to include redzone, etc).
>
> check_valid_pointer verifies that a pointer is pointing to the start of an
> object. It is used to verify the internal points that SLUB used and
> should not be modified to do anything different.
Yup, no worries -- I won't touch it. :) I just wanted to verify my
understanding.
And after playing a bit more, I see that the only thing to the left is
padding and redzone. SLUB layout, from what I saw:
offset: what's there
-------
start: padding, redzone
red_left_pad: object itself
inuse: rest of metadata
size: start of next slub object
(and object_size == inuse - red_left_pad)
i.e. a pointer must be between red_left_pad and inuse, which is the
same as pointer - ref_left_pad being less than object_size.
So, as found already, the position in the usercopy check needs to be
bumped down by red_left_pad, which is what Michael's fix does, so I'll
include it in the next version.
Thanks!
-Kees
--
Kees Cook
Chrome OS & Brillo Security
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-08 17:41 ` Kees Cook
@ 2016-07-08 20:48 ` Kees Cook
[not found] ` <57809299.84b3370a.5390c.ffff9e58SMTPIN_ADDED_BROKEN@mx.google.com>
2016-07-11 6:08 ` Joonsoo Kim
0 siblings, 2 replies; 9+ messages in thread
From: Kees Cook @ 2016-07-08 20:48 UTC (permalink / raw)
To: Christoph Lameter
Cc: Michael Ellerman, kernel-hardening, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Schauf ler, Andrew Morton, linuxppc-dev, David S. Miller,
linux-arm-kernel
On Fri, Jul 8, 2016 at 1:41 PM, Kees Cook <keescook@chromium.org> wrote:
> On Fri, Jul 8, 2016 at 12:20 PM, Christoph Lameter <cl@linux.com> wrote:
>> On Fri, 8 Jul 2016, Kees Cook wrote:
>>
>>> Is check_valid_pointer() making sure the pointer is within the usable
>>> size? It seemed like it was checking that it was within the slub
>>> object (checks against s->size, wants it above base after moving
>>> pointer to include redzone, etc).
>>
>> check_valid_pointer verifies that a pointer is pointing to the start of an
>> object. It is used to verify the internal points that SLUB used and
>> should not be modified to do anything different.
>
> Yup, no worries -- I won't touch it. :) I just wanted to verify my
> understanding.
>
> And after playing a bit more, I see that the only thing to the left is
> padding and redzone. SLUB layout, from what I saw:
>
> offset: what's there
> -------
> start: padding, redzone
> red_left_pad: object itself
> inuse: rest of metadata
> size: start of next slub object
>
> (and object_size == inuse - red_left_pad)
>
> i.e. a pointer must be between red_left_pad and inuse, which is the
> same as pointer - ref_left_pad being less than object_size.
>
> So, as found already, the position in the usercopy check needs to be
> bumped down by red_left_pad, which is what Michael's fix does, so I'll
> include it in the next version.
Actually, after some offline chats, I think this is better, since it
makes sure the ptr doesn't end up somewhere weird before we start the
calculations. This leaves the pointer as-is, but explicitly handles
the redzone on the offset instead, with no wrapping, etc:
/* Find offset within object. */
offset = (ptr - page_address(page)) % s->size;
+ /* Adjust for redzone and reject if within the redzone. */
+ if (s->flags & SLAB_RED_ZONE) {
+ if (offset < s->red_left_pad)
+ return s->name;
+ offset -= s->red_left_pad;
+ }
+
/* Allow address range falling entirely within object size. */
if (offset <= s->object_size && n <= s->object_size - offset)
return NULL;
-Kees
--
Kees Cook
Chrome OS & Brillo Security
^ permalink raw reply [flat|nested] 9+ messages in thread[parent not found: <57809299.84b3370a.5390c.ffff9e58SMTPIN_ADDED_BROKEN@mx.google.com>]
* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
[not found] ` <57809299.84b3370a.5390c.ffff9e58SMTPIN_ADDED_BROKEN@mx.google.com>
@ 2016-07-09 6:17 ` Valdis.Kletnieks
2016-07-09 17:07 ` Kees Cook
0 siblings, 1 reply; 9+ messages in thread
From: Valdis.Kletnieks @ 2016-07-09 6:17 UTC (permalink / raw)
To: kernel-hardening
Cc: Kees Cook, Christoph Lameter, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Sc hauf ler, Andrew Morton, linuxppc-dev, David S. Miller,
linux-arm-kernel
[-- Attachment #1: Type: text/plain, Size: 2070 bytes --]
On Sat, 09 Jul 2016 15:58:20 +1000, Michael Ellerman said:
> I then get two hits, which may or may not be valid:
>
> [ 2.309556] usercopy: kernel memory overwrite attempt detected to d000000003510028 (kernfs_node_cache) (64 bytes)
> [ 2.309995] CPU: 7 PID: 2241 Comm: wait-for-root Not tainted 4.7.0-rc3-00099-g97872fc89d41 #64
> [ 2.310480] Call Trace:
> [ 2.310556] [c0000001f4773bf0] [c0000000009bdbe8] dump_stack+0xb0/0xf0 (unreliable)
> [ 2.311016] [c0000001f4773c30] [c00000000029cf44] __check_object_size+0x74/0x320
> [ 2.311472] [c0000001f4773cb0] [c00000000005d4d0] copy_from_user+0x60/0xd4
> [ 2.311873] [c0000001f4773cf0] [c0000000008b38f4] __get_filter+0x74/0x160
> [ 2.312230] [c0000001f4773d30] [c0000000008b408c] sk_attach_filter+0x2c/0xc0
> [ 2.312596] [c0000001f4773d60] [c000000000871c34] sock_setsockopt+0x954/0xc00
> [ 2.313021] [c0000001f4773dd0] [c00000000086ac44] SyS_setsockopt+0x134/0x150
> [ 2.313380] [c0000001f4773e30] [c000000000009260] system_call+0x38/0x108
Yeah, 'ping' dies with a similar traceback going to rawv6_setsockopt(),
and 'trinity' dies a horrid death during initialization because it creates
some sctp sockets to fool around with. The problem in all these cases is that
setsockopt uses copy_from_user() to pull in the option value, and the allocation
isn't tagged with USERCOPY to whitelist it.
Unfortunately, I haven't been able to track down where in net/ the memory is
allocated, nor is there any good hint in the grsecurity patch that I can find
where they do the tagging.
And the fact that so far, I'm only had ping and trinity killed in setsockopt()
hints that *most* setsockopt() calls must be going through a code path that
does allocate suitable memory, and these two have different paths. I can't
believe they're the only two binaries that call setsockopt().....
Just saw your second mail, now I'm wondering why *my* laptop doesn't die a
horrid death when systemd starts up. Mine is
systemd-230-3.gitea68351.fc25.x86_64 - maybe there's something
release-dependent going on?
[-- Attachment #2: Type: application/pgp-signature, Size: 848 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-09 6:17 ` Valdis.Kletnieks
@ 2016-07-09 17:07 ` Kees Cook
0 siblings, 0 replies; 9+ messages in thread
From: Kees Cook @ 2016-07-09 17:07 UTC (permalink / raw)
To: Valdis Kletnieks
Cc: kernel-hardening, Christoph Lameter, Jan Kara, Catalin Marinas,
Will Deacon, Linux-MM, sparclinux, linux-ia64, Andrea Arcangeli,
linux-arch, x86, Russell King, PaX Team, Borislav Petkov,
Mathias Krause, Fenghua Yu, Rik van Riel, David Rientjes,
Tony Luck, Andy Lutomirski, Joonsoo Kim, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Sc hauf ler, Andrew Morton, linuxppc-dev, David S. Miller,
linux-arm-kernel
On Fri, Jul 8, 2016 at 11:17 PM, <Valdis.Kletnieks@vt.edu> wrote:
> Yeah, 'ping' dies with a similar traceback going to rawv6_setsockopt(),
> and 'trinity' dies a horrid death during initialization because it creates
> some sctp sockets to fool around with. The problem in all these cases is that
> setsockopt uses copy_from_user() to pull in the option value, and the allocation
> isn't tagged with USERCOPY to whitelist it.
Just a note to clear up confusion: this series doesn't include the
whitelist protection, so this appears to be either bugs in the slub
checker or bugs in the code using the cfq_io_cq cache. I suspect the
former. :)
-Kees
--
Kees Cook
Chrome OS & Brillo Security
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-08 20:48 ` Kees Cook
[not found] ` <57809299.84b3370a.5390c.ffff9e58SMTPIN_ADDED_BROKEN@mx.google.com>
@ 2016-07-11 6:08 ` Joonsoo Kim
1 sibling, 0 replies; 9+ messages in thread
From: Joonsoo Kim @ 2016-07-11 6:08 UTC (permalink / raw)
To: Kees Cook
Cc: Christoph Lameter, Michael Ellerman, kernel-hardening, Jan Kara,
Catalin Marinas, Will Deacon, Linux-MM, sparclinux, linux-ia64,
Andrea Arcangeli, linux-arch, x86, Russell King, PaX Team,
Borislav Petkov, Mathias Krause, Fenghua Yu, Rik van Riel,
David Rientjes, Tony Luck, Andy Lutomirski, Dmitry Vyukov,
Laura Abbott, Brad Spengler, Ard Biesheuvel, LKML, Pekka Enberg,
Case y Schauf ler, Andrew Morton, linuxppc-dev, David S. Miller,
linux-arm-kernel
On Fri, Jul 08, 2016 at 04:48:38PM -0400, Kees Cook wrote:
> On Fri, Jul 8, 2016 at 1:41 PM, Kees Cook <keescook@chromium.org> wrote:
> > On Fri, Jul 8, 2016 at 12:20 PM, Christoph Lameter <cl@linux.com> wrote:
> >> On Fri, 8 Jul 2016, Kees Cook wrote:
> >>
> >>> Is check_valid_pointer() making sure the pointer is within the usable
> >>> size? It seemed like it was checking that it was within the slub
> >>> object (checks against s->size, wants it above base after moving
> >>> pointer to include redzone, etc).
> >>
> >> check_valid_pointer verifies that a pointer is pointing to the start of an
> >> object. It is used to verify the internal points that SLUB used and
> >> should not be modified to do anything different.
> >
> > Yup, no worries -- I won't touch it. :) I just wanted to verify my
> > understanding.
> >
> > And after playing a bit more, I see that the only thing to the left is
> > padding and redzone. SLUB layout, from what I saw:
> >
> > offset: what's there
> > -------
> > start: padding, redzone
> > red_left_pad: object itself
> > inuse: rest of metadata
> > size: start of next slub object
> >
> > (and object_size == inuse - red_left_pad)
> >
> > i.e. a pointer must be between red_left_pad and inuse, which is the
> > same as pointer - ref_left_pad being less than object_size.
> >
> > So, as found already, the position in the usercopy check needs to be
> > bumped down by red_left_pad, which is what Michael's fix does, so I'll
> > include it in the next version.
>
> Actually, after some offline chats, I think this is better, since it
> makes sure the ptr doesn't end up somewhere weird before we start the
> calculations. This leaves the pointer as-is, but explicitly handles
> the redzone on the offset instead, with no wrapping, etc:
>
> /* Find offset within object. */
> offset = (ptr - page_address(page)) % s->size;
>
> + /* Adjust for redzone and reject if within the redzone. */
> + if (s->flags & SLAB_RED_ZONE) {
> + if (offset < s->red_left_pad)
> + return s->name;
> + offset -= s->red_left_pad;
> + }
> +
> /* Allow address range falling entirely within object size. */
> if (offset <= s->object_size && n <= s->object_size - offset)
> return NULL;
>
As Christoph saids, please use slab_ksize() rather than
s->object_size.
Otherwise, looks good to me.
Thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 0/9] mm: Hardened usercopy
@ 2016-07-06 22:25 Kees Cook
2016-07-06 22:25 ` [PATCH 9/9] mm: SLUB hardened usercopy support Kees Cook
0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2016-07-06 22:25 UTC (permalink / raw)
To: linux-kernel
Cc: Kees Cook, Rik van Riel, Casey Schaufler, PaX Team,
Brad Spengler, Russell King, Catalin Marinas, Will Deacon,
Ard Biesheuvel, Benjamin Herrenschmidt, Michael Ellerman,
Tony Luck, Fenghua Yu, David S. Miller, x86, Christoph Lameter,
Pekka Enberg, David Rientjes, Joonsoo Kim, Andrew Morton,
Andy Lutomirski, Borislav Petkov, Mathias Krause, Jan Kara,
Vitaly Wool, Andrea Arcangeli, Dmitry Vyukov, Laura Abbott,
linux-arm-kernel, linux-ia64, linuxppc-dev, sparclinux,
linux-arch, linux-mm, kernel-hardening
Hi,
This is a start of the mainline port of PAX_USERCOPY[1]. After I started
writing tests (now in lkdtm in -next) for Casey's earlier port[2], I
kept tweaking things further and further until I ended up with a whole
new patch series. To that end, I took Rik's feedback and made a number
of other changes and clean-ups as well.
Based on my understanding, PAX_USERCOPY was designed to catch a few
classes of flaws around the use of copy_to_user()/copy_from_user(). These
changes don't touch get_user() and put_user(), since these operate on
constant sized lengths, and tend to be much less vulnerable. There
are effectively three distinct protections in the whole series,
each of which I've given a separate CONFIG, though this patch set is
only the first of the three intended protections. (Generally speaking,
PAX_USERCOPY covers what I'm calling CONFIG_HARDENED_USERCOPY (this) and
CONFIG_HARDENED_USERCOPY_WHITELIST (future), and PAX_USERCOPY_SLABS covers
CONFIG_HARDENED_USERCOPY_SPLIT_KMALLOC (future).)
This series, which adds CONFIG_HARDENED_USERCOPY, checks that objects
being copied to/from userspace meet certain criteria:
- if address is a heap object, the size must not exceed the object's
allocated size. (This will catch all kinds of heap overflow flaws.)
- if address range is in the current process stack, it must be within the
current stack frame (if such checking is possible) or at least entirely
within the current process's stack. (This could catch large lengths that
would have extended beyond the current process stack, or overflows if
their length extends back into the original stack.)
- if the address range is part of kernel data, rodata, or bss, allow it.
- if address range is page-allocated, that it doesn't span multiple
allocations.
- if address is within the kernel text, reject it.
- everything else is accepted
The patches in the series are:
- The core copy_to/from_user() checks, without the slab object checks:
1- mm: Hardened usercopy
- Per-arch enablement of the protection:
2- x86/uaccess: Enable hardened usercopy
3- ARM: uaccess: Enable hardened usercopy
4- arm64/uaccess: Enable hardened usercopy
5- ia64/uaccess: Enable hardened usercopy
6- powerpc/uaccess: Enable hardened usercopy
7- sparc/uaccess: Enable hardened usercopy
- The heap allocator implementation of object size checking:
8- mm: SLAB hardened usercopy support
9- mm: SLUB hardened usercopy support
Some notes:
- This is expected to apply on top of -next which contains fixes for the
position of _etext on both arm and arm64.
- I couldn't detect a measurable performance change with these features
enabled. Kernel build times were unchanged, hackbench was unchanged,
etc. I think we could flip this to "on by default" at some point.
- The SLOB support extracted from grsecurity seems entirely broken. I
have no idea what's going on there, I spent my time testing SLAB and
SLUB. Having someone else look at SLOB would be nice, but this series
doesn't depend on it.
Additional features that would be nice, but aren't blocking this series:
- Needs more architecture support for stack frame checking (only x86 now).
Thanks!
-Kees
[1] https://grsecurity.net/download.php "grsecurity - test kernel patch"
[2] http://www.openwall.com/lists/kernel-hardening/2016/05/19/5
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 9/9] mm: SLUB hardened usercopy support
2016-07-06 22:25 [PATCH 0/9] mm: Hardened usercopy Kees Cook
@ 2016-07-06 22:25 ` Kees Cook
[not found] ` <577ddc18.d351190a.1fa54.ffffbe79SMTPIN_ADDED_BROKEN@mx.google.com>
0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2016-07-06 22:25 UTC (permalink / raw)
To: linux-kernel
Cc: Kees Cook, Rik van Riel, Casey Schaufler, PaX Team,
Brad Spengler, Russell King, Catalin Marinas, Will Deacon,
Ard Biesheuvel, Benjamin Herrenschmidt, Michael Ellerman,
Tony Luck, Fenghua Yu, David S. Miller, x86, Christoph Lameter,
Pekka Enberg, David Rientjes, Joonsoo Kim, Andrew Morton,
Andy Lutomirski, Borislav Petkov, Mathias Krause, Jan Kara,
Vitaly Wool, Andrea Arcangeli, Dmitry Vyukov, Laura Abbott,
linux-arm-kernel, linux-ia64, linuxppc-dev, sparclinux,
linux-arch, linux-mm, kernel-hardening
Under CONFIG_HARDENED_USERCOPY, this adds object size checking to the
SLUB allocator to catch any copies that may span objects.
Based on code from PaX and grsecurity.
Signed-off-by: Kees Cook <keescook@chromium.org>
---
init/Kconfig | 1 +
mm/slub.c | 27 +++++++++++++++++++++++++++
2 files changed, 28 insertions(+)
diff --git a/init/Kconfig b/init/Kconfig
index 798c2020ee7c..1c4711819dfd 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1765,6 +1765,7 @@ config SLAB
config SLUB
bool "SLUB (Unqueued Allocator)"
+ select HAVE_HARDENED_USERCOPY_ALLOCATOR
help
SLUB is a slab allocator that minimizes cache line usage
instead of managing queues of cached objects (SLAB approach).
diff --git a/mm/slub.c b/mm/slub.c
index 825ff4505336..0c8ace04f075 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3614,6 +3614,33 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
EXPORT_SYMBOL(__kmalloc_node);
#endif
+#ifdef CONFIG_HARDENED_USERCOPY
+/*
+ * Rejects objects that are incorrectly sized.
+ *
+ * Returns NULL if check passes, otherwise const char * to name of cache
+ * to indicate an error.
+ */
+const char *__check_heap_object(const void *ptr, unsigned long n,
+ struct page *page)
+{
+ struct kmem_cache *s;
+ unsigned long offset;
+
+ /* Find object. */
+ s = page->slab_cache;
+
+ /* Find offset within object. */
+ offset = (ptr - page_address(page)) % s->size;
+
+ /* Allow address range falling entirely within object size. */
+ if (offset <= s->object_size && n <= s->object_size - offset)
+ return NULL;
+
+ return s->name;
+}
+#endif /* CONFIG_HARDENED_USERCOPY */
+
static size_t __ksize(const void *object)
{
struct page *page;
--
2.7.4
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2016-07-11 6:04 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <577f7e55.4668420a.84f17.5cb9SMTPIN_ADDED_MISSING@mx.google.com>
2016-07-08 13:45 ` [kernel-hardening] Re: [PATCH 9/9] mm: SLUB hardened usercopy support Christoph Lameter
2016-07-08 16:07 ` Kees Cook
2016-07-08 16:20 ` Christoph Lameter
2016-07-08 17:41 ` Kees Cook
2016-07-08 20:48 ` Kees Cook
[not found] ` <57809299.84b3370a.5390c.ffff9e58SMTPIN_ADDED_BROKEN@mx.google.com>
2016-07-09 6:17 ` Valdis.Kletnieks
2016-07-09 17:07 ` Kees Cook
2016-07-11 6:08 ` Joonsoo Kim
2016-07-06 22:25 [PATCH 0/9] mm: Hardened usercopy Kees Cook
2016-07-06 22:25 ` [PATCH 9/9] mm: SLUB hardened usercopy support Kees Cook
[not found] ` <577ddc18.d351190a.1fa54.ffffbe79SMTPIN_ADDED_BROKEN@mx.google.com>
2016-07-07 18:56 ` [kernel-hardening] " Kees Cook
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®