Skip to content

Commit

Permalink
mm/slab: add naive detection of double free
Browse files Browse the repository at this point in the history
Similar to commit ce6fa91 ("mm/slub.c: add a naive detection of
double free or corruption"), add a very cheap double-free check for SLAB
under CONFIG_SLAB_FREELIST_HARDENED.  With this added, the
"SLAB_FREE_DOUBLE" LKDTM test passes under SLAB:

  lkdtm: Performing direct entry SLAB_FREE_DOUBLE
  lkdtm: Attempting double slab free ...
  ------------[ cut here ]------------
  WARNING: CPU: 2 PID: 2193 at mm/slab.c:757 ___cache _free+0x325/0x390

[[email protected]: fix misplaced __free_one()]
  Link: http://lkml.kernel.org/r/202006261306.0D82A2B@keescook
  Link: https://lore.kernel.org/lkml/[email protected]

Signed-off-by: Kees Cook <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Acked-by: Vlastimil Babka <[email protected]>
Acked-by: Randy Dunlap <[email protected]>	[build tested]
Cc: Roman Gushchin <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Alexander Popov <[email protected]>
Cc: Pekka Enberg <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Cc: Vinayak Menon <[email protected]>
Cc: Matthew Garrett <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: Vijayanand Jitta <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
kees authored and torvalds committed Aug 7, 2020
1 parent 3404be6 commit dabc3e2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions mm/slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,16 @@ static int transfer_objects(struct array_cache *to,
return nr;
}

/* &alien->lock must be held by alien callers. */
static __always_inline void __free_one(struct array_cache *ac, void *objp)
{
/* Avoid trivial double-free. */
if (IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
WARN_ON_ONCE(ac->avail > 0 && ac->entry[ac->avail - 1] == objp))
return;
ac->entry[ac->avail++] = objp;
}

#ifndef CONFIG_NUMA

#define drain_alien_cache(cachep, alien) do { } while (0)
Expand Down Expand Up @@ -767,7 +777,7 @@ static int __cache_free_alien(struct kmem_cache *cachep, void *objp,
STATS_INC_ACOVERFLOW(cachep);
__drain_alien_cache(cachep, ac, page_node, &list);
}
ac->entry[ac->avail++] = objp;
__free_one(ac, objp);
spin_unlock(&alien->lock);
slabs_destroy(cachep, &list);
} else {
Expand Down Expand Up @@ -3466,7 +3476,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,
}
}

ac->entry[ac->avail++] = objp;
__free_one(ac, objp);
}

/**
Expand Down

0 comments on commit dabc3e2

Please sign in to comment.