[-next] slub: play init_on_free=1 well with SLAB_RED_ZONE
diff mbox series

Message ID 1561058881-9814-1-git-send-email-cai@lca.pw
State New
Headers show
Series
  • [-next] slub: play init_on_free=1 well with SLAB_RED_ZONE
Related show

Commit Message

Qian Cai June 20, 2019, 7:28 p.m. UTC
The linux-next commit "mm: security: introduce init_on_alloc=1 and
init_on_free=1 boot options" [1] does not play well with SLAB_RED_ZONE
as it will overwrite the right-side redzone with all zeros and triggers
endless errors below. Fix it by only wiping out the slab object size and
leave the redzone along. This has a side-effect that it does not wipe
out the slab object metadata like the free pointer and the tracking data
for SLAB_STORE_USER which does seem important anyway, so just to keep
the code simple.

[1] https://patchwork.kernel.org/patch/10999465/

BUG kmalloc-64 (Tainted: G    B            ): Redzone overwritten

INFO: 0x(____ptrval____)-0x(____ptrval____). First byte 0x0 instead of
0xcc
INFO: Slab 0x(____ptrval____) objects=163 used=4 fp=0x(____ptrval____)
flags=0x3fffc000000201
INFO: Object 0x(____ptrval____) @offset=58008 fp=0x(____ptrval____)

Redzone (____ptrval____): cc cc cc cc cc cc cc cc
........
Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
................
Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
................
Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
................
Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
................
Redzone (____ptrval____): 00 00 00 00 00 00 00 00
........
Padding (____ptrval____): 00 00 00 00 00 00 00 00
........
CPU: 0 PID: 1 Comm: swapper/0 Tainted: G    B
5.2.0-rc5-next-20190620+ #2
Call Trace:
[c00000002b72f4b0] [c00000000089ce5c] dump_stack+0xb0/0xf4 (unreliable)
[c00000002b72f4f0] [c0000000003e13d8] print_trailer+0x23c/0x264
[c00000002b72f580] [c0000000003d0468] check_bytes_and_report+0x138/0x160
[c00000002b72f620] [c0000000003d33dc] check_object+0x2ac/0x3e0
[c00000002b72f690] [c0000000003da15c] free_debug_processing+0x1ec/0x680
[c00000002b72f780] [c0000000003da944] __slab_free+0x354/0x6d0
[c00000002b72f840] [c00000000015600c]
__kthread_create_on_node+0x15c/0x260
[c00000002b72f910] [c000000000156144] kthread_create_on_node+0x34/0x50
[c00000002b72f930] [c000000000146fd0] create_worker+0xf0/0x230
[c00000002b72f9e0] [c00000000014fc6c] workqueue_prepare_cpu+0xdc/0x280
[c00000002b72fa60] [c00000000011b27c] cpuhp_invoke_callback+0x1bc/0x1220
[c00000002b72fb00] [c00000000011e7d8] _cpu_up+0x168/0x340
[c00000002b72fb80] [c00000000011eafc] do_cpu_up+0x14c/0x210
[c00000002b72fc10] [c000000000aedc90] smp_init+0x17c/0x1f0
[c00000002b72fcb0] [c000000000ac4a4c] kernel_init_freeable+0x358/0x7cc
[c00000002b72fdb0] [c0000000000106ec] kernel_init+0x2c/0x150
[c00000002b72fe20] [c00000000000b4cc] ret_from_kernel_thread+0x5c/0x70
FIX kmalloc-64: Restoring 0x(____ptrval____)-0x(____ptrval____)=0xcc

FIX kmalloc-64: Object at 0x(____ptrval____) not freed

Signed-off-by: Qian Cai <cai@lca.pw>
---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Kees Cook June 21, 2019, 1:14 a.m. UTC | #1
On Thu, Jun 20, 2019 at 03:28:01PM -0400, Qian Cai wrote:
> The linux-next commit "mm: security: introduce init_on_alloc=1 and
> init_on_free=1 boot options" [1] does not play well with SLAB_RED_ZONE
> as it will overwrite the right-side redzone with all zeros and triggers
> endless errors below. Fix it by only wiping out the slab object size and
> leave the redzone along. This has a side-effect that it does not wipe
> out the slab object metadata like the free pointer and the tracking data
> for SLAB_STORE_USER which does seem important anyway, so just to keep
> the code simple.
> 
> [1] https://patchwork.kernel.org/patch/10999465/
> 
> BUG kmalloc-64 (Tainted: G    B            ): Redzone overwritten
> 
> INFO: 0x(____ptrval____)-0x(____ptrval____). First byte 0x0 instead of
> 0xcc
> INFO: Slab 0x(____ptrval____) objects=163 used=4 fp=0x(____ptrval____)
> flags=0x3fffc000000201
> INFO: Object 0x(____ptrval____) @offset=58008 fp=0x(____ptrval____)
> 
> Redzone (____ptrval____): cc cc cc cc cc cc cc cc
> ........
> Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> ................
> Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> ................
> Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> ................
> Object (____ptrval____): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> ................
> Redzone (____ptrval____): 00 00 00 00 00 00 00 00
> ........
> Padding (____ptrval____): 00 00 00 00 00 00 00 00
> ........
> CPU: 0 PID: 1 Comm: swapper/0 Tainted: G    B
> 5.2.0-rc5-next-20190620+ #2
> Call Trace:
> [c00000002b72f4b0] [c00000000089ce5c] dump_stack+0xb0/0xf4 (unreliable)
> [c00000002b72f4f0] [c0000000003e13d8] print_trailer+0x23c/0x264
> [c00000002b72f580] [c0000000003d0468] check_bytes_and_report+0x138/0x160
> [c00000002b72f620] [c0000000003d33dc] check_object+0x2ac/0x3e0
> [c00000002b72f690] [c0000000003da15c] free_debug_processing+0x1ec/0x680
> [c00000002b72f780] [c0000000003da944] __slab_free+0x354/0x6d0
> [c00000002b72f840] [c00000000015600c]
> __kthread_create_on_node+0x15c/0x260
> [c00000002b72f910] [c000000000156144] kthread_create_on_node+0x34/0x50
> [c00000002b72f930] [c000000000146fd0] create_worker+0xf0/0x230
> [c00000002b72f9e0] [c00000000014fc6c] workqueue_prepare_cpu+0xdc/0x280
> [c00000002b72fa60] [c00000000011b27c] cpuhp_invoke_callback+0x1bc/0x1220
> [c00000002b72fb00] [c00000000011e7d8] _cpu_up+0x168/0x340
> [c00000002b72fb80] [c00000000011eafc] do_cpu_up+0x14c/0x210
> [c00000002b72fc10] [c000000000aedc90] smp_init+0x17c/0x1f0
> [c00000002b72fcb0] [c000000000ac4a4c] kernel_init_freeable+0x358/0x7cc
> [c00000002b72fdb0] [c0000000000106ec] kernel_init+0x2c/0x150
> [c00000002b72fe20] [c00000000000b4cc] ret_from_kernel_thread+0x5c/0x70
> FIX kmalloc-64: Restoring 0x(____ptrval____)-0x(____ptrval____)=0xcc
> 
> FIX kmalloc-64: Object at 0x(____ptrval____) not freed
> 
> Signed-off-by: Qian Cai <cai@lca.pw>
> ---
>  mm/slub.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index a384228ff6d3..787971d4fa36 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -1437,7 +1437,7 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s,
>  		do {
>  			object = next;
>  			next = get_freepointer(s, object);
> -			memset(object, 0, s->size);
> +			memset(object, 0, s->object_size);

I think this should be more dynamic -- we _do_ want to wipe all
of object_size in the case where it's just alignment and padding
adjustments. If redzones are enabled, let's remove that portion only.

-Kees

>  			set_freepointer(s, object, next);
>  		} while (object != old_tail);
>  
> -- 
> 1.8.3.1
>
Kees Cook June 21, 2019, 1:19 a.m. UTC | #2
On Thu, Jun 20, 2019 at 06:14:33PM -0700, Kees Cook wrote:
> On Thu, Jun 20, 2019 at 03:28:01PM -0400, Qian Cai wrote:
> > diff --git a/mm/slub.c b/mm/slub.c
> > index a384228ff6d3..787971d4fa36 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -1437,7 +1437,7 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s,
> >  		do {
> >  			object = next;
> >  			next = get_freepointer(s, object);
> > -			memset(object, 0, s->size);
> > +			memset(object, 0, s->object_size);
> 
> I think this should be more dynamic -- we _do_ want to wipe all
> of object_size in the case where it's just alignment and padding
> adjustments. If redzones are enabled, let's remove that portion only.

(Sorry, I meant: all of object's "size", not object_size.)
Qian Cai June 21, 2019, 12:55 p.m. UTC | #3
On Thu, 2019-06-20 at 18:19 -0700, Kees Cook wrote:
> On Thu, Jun 20, 2019 at 06:14:33PM -0700, Kees Cook wrote:
> > On Thu, Jun 20, 2019 at 03:28:01PM -0400, Qian Cai wrote:
> > > diff --git a/mm/slub.c b/mm/slub.c
> > > index a384228ff6d3..787971d4fa36 100644
> > > --- a/mm/slub.c
> > > +++ b/mm/slub.c
> > > @@ -1437,7 +1437,7 @@ static inline bool slab_free_freelist_hook(struct
> > > kmem_cache *s,
> > >  		do {
> > >  			object = next;
> > >  			next = get_freepointer(s, object);
> > > -			memset(object, 0, s->size);
> > > +			memset(object, 0, s->object_size);
> > 
> > I think this should be more dynamic -- we _do_ want to wipe all
> > of object_size in the case where it's just alignment and padding
> > adjustments. If redzones are enabled, let's remove that portion only.
> 
> (Sorry, I meant: all of object's "size", not object_size.)
> 

I suppose Alexander is going to revise the series anyway, so he can probably
take care of the issue here in the new version as well. Something like this,

memset(object, 0, s->object_size);
memset(object, 0, s->size - s->inuse);
Alexander Potapenko June 25, 2019, 4:16 p.m. UTC | #4
On Fri, Jun 21, 2019 at 2:55 PM Qian Cai <cai@lca.pw> wrote:
>
> On Thu, 2019-06-20 at 18:19 -0700, Kees Cook wrote:
> > On Thu, Jun 20, 2019 at 06:14:33PM -0700, Kees Cook wrote:
> > > On Thu, Jun 20, 2019 at 03:28:01PM -0400, Qian Cai wrote:
> > > > diff --git a/mm/slub.c b/mm/slub.c
> > > > index a384228ff6d3..787971d4fa36 100644
> > > > --- a/mm/slub.c
> > > > +++ b/mm/slub.c
> > > > @@ -1437,7 +1437,7 @@ static inline bool slab_free_freelist_hook(struct
> > > > kmem_cache *s,
> > > >           do {
> > > >                   object = next;
> > > >                   next = get_freepointer(s, object);
> > > > -                 memset(object, 0, s->size);
> > > > +                 memset(object, 0, s->object_size);
> > >
> > > I think this should be more dynamic -- we _do_ want to wipe all
> > > of object_size in the case where it's just alignment and padding
> > > adjustments. If redzones are enabled, let's remove that portion only.
> >
> > (Sorry, I meant: all of object's "size", not object_size.)
> >
>
> I suppose Alexander is going to revise the series anyway, so he can probably
> take care of the issue here in the new version as well. Something like this,
>
> memset(object, 0, s->object_size);
> memset(object, 0, s->size - s->inuse);
Looks like we also need to account for the redzone size. I'm testing
the fix right now.


--
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

Patch
diff mbox series

diff --git a/mm/slub.c b/mm/slub.c
index a384228ff6d3..787971d4fa36 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1437,7 +1437,7 @@  static inline bool slab_free_freelist_hook(struct kmem_cache *s,
 		do {
 			object = next;
 			next = get_freepointer(s, object);
-			memset(object, 0, s->size);
+			memset(object, 0, s->object_size);
 			set_freepointer(s, object, next);
 		} while (object != old_tail);