slub: Potential stack overflow
Subsystems (2)
PATCH 1/1 slub: Potential stack overflow
I discovered that we can overflow stack if CONFIG_SLUB_DEBUG=y and use slabs with many objects, since list_slab_objects() and process_slab() use DECLARE_BITMAP(map, page->objects);
With 65535 bits, we use 8192 bytes of stack ...
A possible fix is to lower MAX_OBJS_PER_PAGE so that these bitmaps dont use more than a third of THREAD_SIZE. I suspect plain memory allocation in these functions is not an option.
Using non dynamic stack allocation makes the problem more obvious if somebody runs checkstack.pl
---
mm/slub.c +8 -2
@@ -167,7 +167,13 @@#define OO_SHIFT 16#define OO_MASK ((1 << OO_SHIFT) - 1) + +#ifdef CONFIG_SLUB_DEBUG +/* We use an onstack BITMAP while debugging, make sure this wont be too big */ +#define MAX_OBJS_PER_PAGE min_t(int, 65535, 8*(THREAD_SIZE/3)) +#else #define MAX_OBJS_PER_PAGE 65535/* since page.objects is u16 */ +#endif /* Internal SLUB flags */ #define __OBJECT_POISON 0x80000000/* Poison object */
@@ -2426,7 +2432,7 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,#ifdef CONFIG_SLUB_DEBUG void *addr =page_address (page);void *p; -DECLARE_BITMAP (map, page->objects); +DECLARE_BITMAP (map,MAX_OBJS_PER_PAGE );bitmap_zero (map, page->objects);slab_err (s, page,"%s" , text);
@@ -3651,7 +3657,7 @@ static void process_slab(struct loc_track *t, struct kmem_cache *s, struct page *page, enum track_item alloc) { void *addr = page_address(page); -DECLARE_BITMAP (map, page->objects); +DECLARE_BITMAP (map,MAX_OBJS_PER_PAGE );void *p;bitmap_zero (map, page->objects);
Quoted 3 lines · Eric Dumazet
I discovered that we can overflow stack if
CONFIG_SLUB_DEBUG=yand use slabs with many objects, sincelist_slab_objects()andprocess_slab()useDECLARE_BITMAP(map, page->objects);
Maybe we better allocate the bitmap via kmalloc then.
Quoted 8 lines · Christoph Lameter
Quoted 3 lines · Eric Dumazet
I discovered that we can overflow stack if
CONFIG_SLUB_DEBUG=yand use slabs with many objects, sincelist_slab_objects()andprocess_slab()useDECLARE_BITMAP(map, page->objects);Maybe we better allocate the bitmap via kmalloc then.
Hmm...
Are we allowed to nest in these two functions ?
GFP_KERNEL, GFP_ATOMIC ?
These are debugging functions, what happens if kmalloc() returns NULL ?
Quoted 1 line · Eric Dumazet
Are we allowed to nest in these two functions ?
This is kmem_cache_close() no danger of nesting.
Quoted 1 line
These are debugging functions, what happens if
kmalloc()returns NULL ?
Then you return ENOMEM and the user gets an error. We already do that in validate_slab_cache().
Hmmm... In this case we called from list_slab_objects() which gets called from free_partial() (which took a spinlock!) which gets called from kmem_cache_close().
Its just a debugging aid so no problem if it fails. GFP_ATOMIC?
Quoted 8 lines · Christoph Lameter
Quoted 1 line · Eric Dumazet
Are we allowed to nest in these two functions ?
This is
kmem_cache_close()no danger of nesting.Quoted 1 line
These are debugging functions, what happens if
kmalloc()returns NULL ?
Quoted 8 lines
Then you return ENOMEM and the user gets an error. We already do that in
validate_slab_cache().Hmmm... In this case we called from
list_slab_objects()which gets called fromfree_partial()(which took a spinlock!) which gets called fromkmem_cache_close().Its just a debugging aid so no problem if it fails.
GFP_ATOMIC?
OK, here is second version of the patch, thanks !
[PATCH] slub: Potential stack overflow
I discovered that we can overflow stack if CONFIG_SLUB_DEBUG=y and use slabs with many objects, since list_slab_objects() and process_slab() use DECLARE_BITMAP(map, page->objects);
With 65535 bits, we use 8192 bytes of stack ...
A possible solution is to allocate memory, using GFP_ATOMIC, and do nothing if allocation fails.
---
mm/slub.c +10 -4
@@ -2426,9 +2426,11 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,#ifdef CONFIG_SLUB_DEBUG void *addr =page_address (page);void *p; -DECLARE_BITMAP (map, page->objects); +long *map =kzalloc (BITS_TO_LONGS (page->objects) *sizeof (long ), +GFP_ATOMIC ); -bitmap_zero (map, page->objects); +if (!map) +return ;slab_err (s, page,"%s" , text);slab_lock (page);for_each_free_object (p, s, page->freelist)
@@ -2443,6 +2445,7 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page, } }slab_unlock (page); +kfree (map);#endif }
@@ -3651,16 +3654,19 @@ static void process_slab(struct loc_track *t, struct kmem_cache *s, struct page *page, enum track_item alloc) { void *addr = page_address(page); -DECLARE_BITMAP (map, page->objects); +long *map =kzalloc (BITS_TO_LONGS (page->objects) *sizeof (long ), +GFP_ATOMIC );void *p; -bitmap_zero (map, page->objects); +if (!map) +return ;for_each_free_object (p, s, page->freelist)set_bit (slab_index (p, s, addr), map);for_each_object (p, s, addr, page->objects)if (!test_bit (slab_index (p, s, addr), map))add_location (t, s,get_track (s, p, alloc)); +kfree (map); } static int list_locations(struct kmem_cache *s, char *buf,
Quoted 11 lines · Eric Dumazet
mm/slub.c +3
-2@@ -3651,16 +3654,19 @@ static void process_slab(struct loc_track *t, struct kmem_cache *s, struct page *page, enum track_item alloc) { void *addr = page_address(page); -DECLARE_BITMAP (map, page->objects); +long *map =kzalloc (BITS_TO_LONGS (page->objects) *sizeof (long ), +GFP_ATOMIC );void *p; -bitmap_zero (map, page->objects); +if (!map)
mm/slub.c static void process_slab(struct loc_track *t, struct kmem_cache *s, · line 3671
Quoted 10 lines
mm/slub.c +2
-0+return ;for_each_free_object (p, s, page->freelist)set_bit (slab_index (p, s, addr), map);for_each_object (p, s, addr, page->objects)if (!test_bit (slab_index (p, s, addr), map))add_location (t, s,get_track (s, p, alloc)); +kfree (map); }
Hmmm... Thats another case. We should alloate the map higher up there I guess and pass the address in so that one allocation can be used for all slabs. validate_slab_cache() does that.
Here is a patch for the second case. I think its better since it results in an error display and it avoids the alloc for each slab. Add this piece to your patch?
--- mm/slub.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
mm/slub.c +7 -5
@@ -3648,10 +3648,10 @@ static int add_location(struct loc_track }static void process_slab (struct loc_track *t,struct kmem_cache *s, -struct page *page,enum track_item alloc) +struct page *page,enum track_item alloc, +unsigned long *map) {void *addr =page_address (page); -DECLARE_BITMAP (map, page->objects);void *p;bitmap_zero (map, page->objects);
@@ -3670,8 +3670,10 @@ static int list_locations(struct kmem_caunsigned long i;struct loc_track t = {0 ,0 ,NULL };int node; +unsigned long *map =kmalloc (BITS_TO_LONGS (oo_objects (s->max)) * +sizeof (unsigned long ),GFP_KERNEL ); -if (!alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), +if (!map || !alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ),GFP_TEMPORARY ))return sprintf (buf,"Out of memory\n" );
@@ -3688,9 +3690,9 @@ static int list_locations(struct kmem_caspin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);list_for_each_entry (page, &n->full, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);spin_unlock_irqrestore (&n->list_lock, flags); }
Quoted 5 lines · Christoph Lameter
Here is a patch for the second case. I think its better since it results in an error display and it avoids the alloc for each slab. Add this piece to your patch?
Signed-off-by Christoph Lameter <[email protected]>
Sure, here is third version :
Thanks
[PATCH] slub: Potential stack overflow
I discovered that we can overflow stack if CONFIG_SLUB_DEBUG=y and use slabs with many objects, since list_slab_objects() and process_slab() use DECLARE_BITMAP(map, page->objects);
With 65535 bits, we use 8192 bytes of stack ...
Switch these allocations to dynamic allocations.
--- mm/slub.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
mm/slub.c +16 -9
@@ -2426,9 +2426,11 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,#ifdef CONFIG_SLUB_DEBUG void *addr =page_address (page);void *p; -DECLARE_BITMAP (map, page->objects); +long *map =kzalloc (BITS_TO_LONGS (page->objects) *sizeof (long ), +GFP_ATOMIC ); -bitmap_zero (map, page->objects); +if (!map) +return ;slab_err (s, page,"%s" , text);slab_lock (page);for_each_free_object (p, s, page->freelist)
@@ -2443,6 +2445,7 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page, } }slab_unlock (page); +kfree (map);#endif }
@@ -3648,10 +3651,10 @@ static int add_location(struct loc_track *t, struct kmem_cache *s, }static void process_slab (struct loc_track *t,struct kmem_cache *s, -struct page *page,enum track_item alloc) +struct page *page,enum track_item alloc, +long *map) {void *addr =page_address (page); -DECLARE_BITMAP (map, page->objects);void *p;bitmap_zero (map, page->objects);
@@ -3670,11 +3673,14 @@ static int list_locations(struct kmem_cache *s, char *buf,unsigned long i;struct loc_track t = {0 ,0 ,NULL };int node; +unsigned long *map =kmalloc (BITS_TO_LONGS (oo_objects (s->max)) * +sizeof (unsigned long ),GFP_KERNEL ); -if (!alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), -GFP_TEMPORARY )) +if (!map || !alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), +GFP_TEMPORARY )) { +kfree (map);return sprintf (buf,"Out of memory\n" ); - + }/* Push back cpu slabs */ flush_all (s);
@@ -3688,9 +3694,9 @@ static int list_locations(struct kmem_cache *s, char *buf,spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);list_for_each_entry (page, &n->full, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);spin_unlock_irqrestore (&n->list_lock, flags); }
@@ -3741,6 +3747,7 @@ static int list_locations(struct kmem_cache *s, char *buf, }free_loc_track (&t); +kfree (map);if (!t.count) len +=sprintf (buf,"No data\n" );return len;
Quoted 8 lines · Eric Dumazet
Quoted 5 lines · Christoph Lameter
Here is a patch for the second case. I think its better since it results in an error display and it avoids the alloc for each slab. Add this piece to your patch?
Signed-off-by Christoph Lameter <[email protected]>Sure, here is third version :
Christoph, does this look OK to you? I think Eric has all your later additions and kfree() fixlets here.
Quoted 94 lines
[PATCH] slub: Potential stack overflow
I discovered that we can overflow stack if
CONFIG_SLUB_DEBUG=yand use slabs with many objects, sincelist_slab_objects()andprocess_slab()useDECLARE_BITMAP(map, page->objects);With 65535 bits, we use 8192 bytes of stack ...
Switch these allocations to dynamic allocations.
Signed-off-by Eric Dumazet <[email protected]>Signed-off-by Christoph Lameter <[email protected]>--- mm/slub.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)mm/slub.c +16
-9@@ -2426,9 +2426,11 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,#ifdef CONFIG_SLUB_DEBUG void *addr =page_address (page);void *p; -DECLARE_BITMAP (map, page->objects); +long *map =kzalloc (BITS_TO_LONGS (page->objects) *sizeof (long ), +GFP_ATOMIC ); -bitmap_zero (map, page->objects); +if (!map) +return ;slab_err (s, page,"%s" , text);slab_lock (page);for_each_free_object (p, s, page->freelist)@@ -2443,6 +2445,7 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page, } }slab_unlock (page); +kfree (map);#endif }@@ -3648,10 +3651,10 @@ static int add_location(struct loc_track *t, struct kmem_cache *s, }static void process_slab (struct loc_track *t,struct kmem_cache *s, -struct page *page,enum track_item alloc) +struct page *page,enum track_item alloc, +long *map) {void *addr =page_address (page); -DECLARE_BITMAP (map, page->objects);void *p;bitmap_zero (map, page->objects);@@ -3670,11 +3673,14 @@ static int list_locations(struct kmem_cache *s, char *buf,unsigned long i;struct loc_track t = {0 ,0 ,NULL };int node; +unsigned long *map =kmalloc (BITS_TO_LONGS (oo_objects (s->max)) * +sizeof (unsigned long ),GFP_KERNEL ); -if (!alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), -GFP_TEMPORARY )) +if (!map || !alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), +GFP_TEMPORARY )) { +kfree (map);return sprintf (buf,"Out of memory\n" ); - + }/* Push back cpu slabs */ flush_all (s);@@ -3688,9 +3694,9 @@ static int list_locations(struct kmem_cache *s, char *buf,spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);list_for_each_entry (page, &n->full, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);spin_unlock_irqrestore (&n->list_lock, flags); }@@ -3741,6 +3747,7 @@ static int list_locations(struct kmem_cache *s, char *buf, }free_loc_track (&t); +kfree (map);if (!t.count) len +=sprintf (buf,"No data\n" );return len;
Quoted 2 lines · Pekka Enberg
Christoph, does this look OK to you? I think Eric has all your later additions and
kfree()fixlets here.
Yes just dont know how to add an Ack given that there is already a signoff.
Pekka Enberg
AppliedQuoted 17 lines · Eric Dumazet
Quoted 5 lines · Christoph Lameter
Here is a patch for the second case. I think its better since it results in an error display and it avoids the alloc for each slab. Add this piece to your patch?
Signed-off-by Christoph Lameter <[email protected]>Sure, here is third version :
Thanks
[PATCH] slub: Potential stack overflow
I discovered that we can overflow stack if
CONFIG_SLUB_DEBUG=yand use slabs with many objects, sincelist_slab_objects()andprocess_slab()useDECLARE_BITMAP(map, page->objects);
Quoted 6 lines
With 65535 bits, we use 8192 bytes of stack ...
Switch these allocations to dynamic allocations.
Signed-off-by Eric Dumazet <[email protected]>Signed-off-by Christoph Lameter <[email protected]>
Applied.
Here is a patch for the second case. I think its better since it results in an error display and it avoids the alloc for each slab. Add this piece to your patch?
V1->V2 Fix missing kfree
--- mm/slub.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
mm/slub.c +8 -5
@@ -3648,10 +3648,10 @@ static int add_location(struct loc_track }static void process_slab (struct loc_track *t,struct kmem_cache *s, -struct page *page,enum track_item alloc) +struct page *page,enum track_item alloc, +unsigned long *map) {void *addr =page_address (page); -DECLARE_BITMAP (map, page->objects);void *p;bitmap_zero (map, page->objects);
@@ -3670,8 +3670,10 @@ static int list_locations(struct kmem_caunsigned long i;struct loc_track t = {0 ,0 ,NULL };int node; +unsigned long *map =kmalloc (BITS_TO_LONGS (oo_objects (s->max)) * +sizeof (unsigned long ),GFP_KERNEL ); -if (!alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), +if (!map || !alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ),GFP_TEMPORARY ))return sprintf (buf,"Out of memory\n" );
@@ -3688,11 +3690,12 @@ static int list_locations(struct kmem_caspin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);list_for_each_entry (page, &n->full, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);spin_unlock_irqrestore (&n->list_lock, flags); } +kfree (map);for (i =0 ; i < t.count; i++) {struct location *l = &t.loc[i];
Quoted 3 lines · Christoph Lameter
Here is a patch for the second case. I think its better since it results in an error display and it avoids the alloc for each slab. Add this piece to your patch?
Yes, I did it
Quoted 27 lines
V1->V2Fix missing kfreeSigned-off-by Christoph Lameter <[email protected]>--- mm/slub.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)Index:
linux-2.6/mm/slub.c
===================================================================mm/slub.c +2
-2@@ -3648,10 +3648,10 @@ static int add_location(struct loc_track }static void process_slab (struct loc_track *t,struct kmem_cache *s, -struct page *page,enum track_item alloc) +struct page *page,enum track_item alloc, +unsigned long *map) {void *addr =page_address (page); -DECLARE_BITMAP (map, page->objects);void *p;bitmap_zero (map, page->objects);
mm/slub.c static int list_locations(struct kmem_ca · line 3677
Quoted 9 lines
mm/slub.c +3
-1@@ -3670,8 +3670,10 @@ static int list_locations(struct kmem_caunsigned long i;struct loc_track t = {0 ,0 ,NULL };int node; +unsigned long *map =kmalloc (BITS_TO_LONGS (oo_objects (s->max)) * +sizeof (unsigned long ),GFP_KERNEL ); -if (!alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ), +if (!map || !alloc_loc_track (&t,PAGE_SIZE /sizeof (struct location ),GFP_TEMPORARY ))
I also added a kfree(map); here
Quoted 18 lines
mm/slub.c +3
-3return sprintf (buf,"Out of memory\n" );@@ -3688,11 +3690,12 @@ static int list_locations(struct kmem_caspin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (page, &n->partial, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);list_for_each_entry (page, &n->full, lru) -process_slab (&t, s, page, alloc); +process_slab (&t, s, page, alloc, map);spin_unlock_irqrestore (&n->list_lock, flags); } +kfree (map);for (i =0 ; i < t.count; i++) {struct location *l = &t.loc[i]; --