Skip to content

Commit cbfc35a

Browse files
Waiman-Longtorvalds
authored andcommitted
mm/slub: fix incorrect interpretation of s->offset
In a couple of places in the slub memory allocator, the code uses "s->offset" as a check to see if the free pointer is put right after the object. That check is no longer true with commit 3202fa6 ("slub: relocate freelist pointer to middle of object"). As a result, echoing "1" into the validate sysfs file, e.g. of dentry, may cause a bunch of "Freepointer corrupt" error reports like the following to appear with the system in panic afterwards. ============================================================================= BUG dentry(666:pmcd.service) (Tainted: G B): Freepointer corrupt ----------------------------------------------------------------------------- To fix it, use the check "s->offset == s->inuse" in the new helper function freeptr_outside_object() instead. Also add another helper function get_info_end() to return the end of info block (inuse + free pointer if not overlapping with object). Fixes: 3202fa6 ("slub: relocate freelist pointer to middle of object") Signed-off-by: Waiman Long <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Kees Cook <[email protected]> Acked-by: Rafael Aquini <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Vitaly Nikolenko <[email protected]> Cc: Silvio Cesare <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: David Rientjes <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Markus Elfring <[email protected]> Cc: Changbin Du <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 50e36be commit cbfc35a

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

mm/slub.c

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,32 @@ static void print_section(char *level, char *text, u8 *addr,
551551
metadata_access_disable();
552552
}
553553

554+
/*
555+
* See comment in calculate_sizes().
556+
*/
557+
static inline bool freeptr_outside_object(struct kmem_cache *s)
558+
{
559+
return s->offset >= s->inuse;
560+
}
561+
562+
/*
563+
* Return offset of the end of info block which is inuse + free pointer if
564+
* not overlapping with object.
565+
*/
566+
static inline unsigned int get_info_end(struct kmem_cache *s)
567+
{
568+
if (freeptr_outside_object(s))
569+
return s->inuse + sizeof(void *);
570+
else
571+
return s->inuse;
572+
}
573+
554574
static struct track *get_track(struct kmem_cache *s, void *object,
555575
enum track_item alloc)
556576
{
557577
struct track *p;
558578

559-
if (s->offset)
560-
p = object + s->offset + sizeof(void *);
561-
else
562-
p = object + s->inuse;
579+
p = object + get_info_end(s);
563580

564581
return p + alloc;
565582
}
@@ -686,10 +703,7 @@ static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
686703
print_section(KERN_ERR, "Redzone ", p + s->object_size,
687704
s->inuse - s->object_size);
688705

689-
if (s->offset)
690-
off = s->offset + sizeof(void *);
691-
else
692-
off = s->inuse;
706+
off = get_info_end(s);
693707

694708
if (s->flags & SLAB_STORE_USER)
695709
off += 2 * sizeof(struct track);
@@ -782,7 +796,7 @@ static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
782796
* object address
783797
* Bytes of the object to be managed.
784798
* If the freepointer may overlay the object then the free
785-
* pointer is the first word of the object.
799+
* pointer is at the middle of the object.
786800
*
787801
* Poisoning uses 0x6b (POISON_FREE) and the last byte is
788802
* 0xa5 (POISON_END)
@@ -816,11 +830,7 @@ static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
816830

817831
static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
818832
{
819-
unsigned long off = s->inuse; /* The end of info */
820-
821-
if (s->offset)
822-
/* Freepointer is placed after the object. */
823-
off += sizeof(void *);
833+
unsigned long off = get_info_end(s); /* The end of info */
824834

825835
if (s->flags & SLAB_STORE_USER)
826836
/* We also have user information there */
@@ -907,7 +917,7 @@ static int check_object(struct kmem_cache *s, struct page *page,
907917
check_pad_bytes(s, page, p);
908918
}
909919

910-
if (!s->offset && val == SLUB_RED_ACTIVE)
920+
if (!freeptr_outside_object(s) && val == SLUB_RED_ACTIVE)
911921
/*
912922
* Object and freepointer overlap. Cannot check
913923
* freepointer while object is allocated.
@@ -3587,6 +3597,11 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order)
35873597
*
35883598
* This is the case if we do RCU, have a constructor or
35893599
* destructor or are poisoning the objects.
3600+
*
3601+
* The assumption that s->offset >= s->inuse means free
3602+
* pointer is outside of the object is used in the
3603+
* freeptr_outside_object() function. If that is no
3604+
* longer true, the function needs to be modified.
35903605
*/
35913606
s->offset = size;
35923607
size += sizeof(void *);

0 commit comments

Comments
 (0)