mm: kasan: Initial memory quarantine implementation
Subsystems (3)
Alexander Potapenko
reviewerPATCH v9 1/1 mm: kasan: Initial memory quarantine implementation
Quarantine isolates freed objects in a separate queue. The objects are returned to the allocator later, which helps to detect use-after-free errors.
Freed objects are first added to per-cpu quarantine queues. When a cache is destroyed or memory shrinking is requested, the objects are moved into the global quarantine queue. Whenever a kmalloc call allows memory reclaiming, the oldest objects are popped out of the global queue until the total size of objects in quarantine is less than 3/4 of the maximum quarantine size (which is a fraction of installed physical memory).
As long as an object remains in the quarantine, KASAN is able to report accesses to it, so the chance of reporting a use-after-free is increased. Once the object leaves quarantine, the allocator may reuse it, in which case the object is unpoisoned and KASAN can't detect incorrect accesses to it.
Right now quarantine support is only enabled in SLAB allocator. Unification of KASAN features in SLAB and SLUB will be done later.
This patch is based on the "mm: kasan: quarantine" patch originally prepared by Dmitry Chernenkov. A number of improvements have been suggested by Andrey Ryabinin.
---
v2: - added copyright comments
- per request from Joonsoo Kim made __cache_free() more straightforward
- added comments for smp_load_acquire()/smp_store_release()
v3: - incorporate changes introduced by the "mm, kasan: SLAB support" patch
v4: - fix kbuild compile-time error (missing ___cache_free() declaration)
and a warning (wrong format specifier)
v6: - extended the patch description
- dropped the unused qlist_remove() function
v9: - incorporate the fixes by Andrey Ryabinin:
* Fix comment styles,
* Get rid of some ifdefs
* Revert needless functions renames in quarantine patch
* Remove needless local_irq_save()/restore() in per_cpu_remove_cache()
* Add new 'struct qlist_node' instead of 'void **' types. This makes code a bit more redable.
- remove the non-deterministic quarantine test
- dropped smp_load_acquire()/smp_store_release()
--- include/linux/kasan.h | 13 ++- mm/kasan/Makefile | 1 + mm/kasan/kasan.c | 57 ++++++++-- mm/kasan/kasan.h | 21 +++- mm/kasan/quarantine.c | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++ mm/kasan/report.c | 1 + mm/mempool.c | 2 +- mm/slab.c | 12 ++- mm/slab.h | 2 + mm/slab_common.c | 2 + 10 files changed, 387 insertions(+), 15 deletions(-) create mode 100644 mm/kasan/quarantine.c
include/linux/kasan.h +11 -2
@@ -50,6 +50,8 @@ void kasan_free_pages(struct page *page, unsigned int order);void kasan_cache_create (struct kmem_cache *cache,size_t *size,unsigned long *flags); +void kasan_cache_shrink (struct kmem_cache *cache); +void kasan_cache_destroy (struct kmem_cache *cache);void kasan_poison_slab (struct page *page);void kasan_unpoison_object_data (struct kmem_cache *cache,void *object);
@@ -63,7 +65,8 @@ void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size,void kasan_krealloc (const void *object,size_t new_size,gfp_t flags);void kasan_slab_alloc (struct kmem_cache *s,void *object,gfp_t flags); -void kasan_slab_free (struct kmem_cache *s,void *object); +bool kasan_slab_free (struct kmem_cache *s,void *object); +void kasan_poison_slab_free (struct kmem_cache *s,void *object);struct kasan_cache {int alloc_meta_offset;
@@ -88,6 +91,8 @@ static inline void kasan_free_pages(struct page *page, unsigned int order) {}static inline void kasan_cache_create (struct kmem_cache *cache,size_t *size,unsigned long *flags) {} +static inline void kasan_cache_shrink (struct kmem_cache *cache) {} +static inline void kasan_cache_destroy (struct kmem_cache *cache) {}static inline void kasan_poison_slab (struct page *page) {} static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
@@ -105,7 +110,11 @@ static inline void kasan_krealloc(const void *object, size_t new_size,static inline void kasan_slab_alloc (struct kmem_cache *s,void *object,gfp_t flags) {} -static inline void kasan_slab_free (struct kmem_cache *s,void *object) {} +static inline bool kasan_slab_free (struct kmem_cache *s,void *object) +{ +return false; +} +static inline void kasan_poison_slab_free (struct kmem_cache *s,void *object) {}static inline int kasan_module_alloc (void *addr,size_t size) {return 0 ; }static inline void kasan_free_shadow (const struct vm_struct *vm) {}
mm/kasan/Makefile +1 -0
@@ -8,3 +8,4 @@ CFLAGS_REMOVE_kasan.o = -pg CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) obj-y := kasan.o report.o kasan_init.o +obj-$(CONFIG_SLAB) += quarantine.o
mm/kasan/kasan.c +49 -8
92 lines not on this page · raw message
mm/kasan/kasan.h +19 -2
@@ -62,6 +62,7 @@ struct kasan_global {enum kasan_state {KASAN_STATE_INIT ,KASAN_STATE_ALLOC , +KASAN_STATE_QUARANTINE ,KASAN_STATE_FREE };
@@ -79,9 +80,14 @@ struct kasan_alloc_meta {u32 reserved; }; +struct qlist_node { +struct qlist_node *next; +};struct kasan_free_meta { -/* Allocator freelist pointer, unused by KASAN. */ -void **freelist; +/* This field is used while the object is in the quarantine. +* Otherwise it might be used for the allocator freelist. +*/ +struct qlist_node quarantine_link;struct kasan_track track; };
@@ -105,4 +111,15 @@ static inline bool kasan_report_enabled(void)void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); +#ifdef CONFIG_SLAB +void quarantine_put (struct kasan_free_meta *info,struct kmem_cache *cache); +void quarantine_reduce (void ); +void quarantine_remove_cache (struct kmem_cache *cache); +#else +static inline void quarantine_put (struct kasan_free_meta *info, +struct kmem_cache *cache) { } +static inline void quarantine_reduce (void ) { } +static inline void quarantine_remove_cache (struct kmem_cache *cache) { } +#endif +#endif
mm/kasan/quarantine.c new file +291 -0
291 lines not on this page · raw message
mm/kasan/report.c +1 -0
@@ -151,6 +151,7 @@ static void object_err(struct kmem_cache *cache, struct page *page,print_track (&alloc_info->track);break ;case KASAN_STATE_FREE : +case KASAN_STATE_QUARANTINE :pr_err ("Object freed, allocated with size %u bytes\n" , alloc_info->alloc_size); free_info =get_free_info (cache, object);
mm/mempool.c +1 -1
@@ -105,7 +105,7 @@ static inline void poison_element(mempool_t *pool, void *element)static void kasan_poison_element (mempool_t *pool,void *element) {if (pool->alloc == mempool_alloc_slab) -kasan_slab_free (pool->pool_data, element); +kasan_poison_slab_free (pool->pool_data, element);if (pool->alloc == mempool_kmalloc)kasan_kfree (element);if (pool->alloc == mempool_alloc_pages)
mm/slab.c +10 -2
@@ -3327,9 +3327,17 @@ free_done:static inline void __cache_free (struct kmem_cache *cachep,void *objp,unsigned long caller) { -struct array_cache *ac =cpu_cache_get (cachep); +/* Put the object into the quarantine, don't touch it for now. */ +if (kasan_slab_free (cachep, objp)) +return ; + +___cache_free (cachep, objp, caller); +} -kasan_slab_free (cachep, objp); +void ___cache_free (struct kmem_cache *cachep,void *objp, +unsigned long caller) +{ +struct array_cache *ac =cpu_cache_get (cachep);check_irq_off ();kmemleak_free_recursive (objp, cachep->flags);
mm/slab.h +2 -0
@@ -462,4 +462,6 @@ void *slab_next(struct seq_file *m, void *p, loff_t *pos);void slab_stop (struct seq_file *m,void *p);int memcg_slab_show (struct seq_file *m,void *p); +void ___cache_free (struct kmem_cache *cache,void *x,unsigned long addr); +#endif /* MM_SLAB_H */
mm/slab_common.c +2 -0
@@ -715,6 +715,7 @@ void kmem_cache_destroy(struct kmem_cache *s)get_online_cpus ();get_online_mems (); +kasan_cache_destroy (s);mutex_lock (&slab_mutex); s->refcount--;
@@ -753,6 +754,7 @@ int kmem_cache_shrink(struct kmem_cache *cachep)get_online_cpus ();get_online_mems (); +kasan_cache_shrink (cachep); ret =__kmem_cache_shrink (cachep, false);put_online_mems ();put_online_cpus ();
Signature · 7 lines
2.8.0.rc3.226.g39d4020 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hi Alexander,
Quoted 69 lines · Alexander Potapenko
Quarantine isolates freed objects in a separate queue. The objects are returned to the allocator later, which helps to detect use-after-free errors.
Freed objects are first added to per-cpu quarantine queues. When a cache is destroyed or memory shrinking is requested, the objects are moved into the global quarantine queue. Whenever a kmalloc call allows memory reclaiming, the oldest objects are popped out of the global queue until the total size of objects in quarantine is less than 3/4 of the maximum quarantine size (which is a fraction of installed physical memory).
As long as an object remains in the quarantine, KASAN is able to report accesses to it, so the chance of reporting a use-after-free is increased. Once the object leaves quarantine, the allocator may reuse it, in which case the object is unpoisoned and KASAN can't detect incorrect accesses to it.
Right now quarantine support is only enabled in SLAB allocator. Unification of KASAN features in SLAB and SLUB will be done later.
This patch is based on the "mm: kasan: quarantine" patch originally prepared by Dmitry Chernenkov. A number of improvements have been suggested by Andrey Ryabinin.
Signed-off-by Alexander Potapenko <[email protected]>---
v2: - added copyright comments
- per request from Joonsoo Kim made__cache_free()more straightforward
- added comments for smp_load_acquire()/smp_store_release()v3: - incorporate changes introduced by the "mm, kasan: SLAB support" patch
v4: - fix kbuild compile-time error (missing
___cache_free()declaration)
and a warning (wrong format specifier)v6: - extended the patch description
- dropped the unusedqlist_remove()functionv9: - incorporate the fixes by Andrey Ryabinin:
* Fix comment styles,
* Get rid of some ifdefs
* Revert needless functions renames in quarantine patch
* Remove needless local_irq_save()/restore() inper_cpu_remove_cache()
* Add new 'structqlist_node' instead of 'void **' types. This makes code a bit more redable.
- remove the non-deterministic quarantine test
- dropped smp_load_acquire()/smp_store_release()--- include/linux/kasan.h | 13 ++- mm/kasan/Makefile | 1 + mm/kasan/kasan.c | 57 ++++++++-- mm/kasan/kasan.h | 21 +++- mm/kasan/quarantine.c | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++ mm/kasan/report.c | 1 + mm/mempool.c | 2 +- mm/slab.c | 12 ++- mm/slab.h | 2 + mm/slab_common.c | 2 + 10 files changed, 387 insertions(+), 15 deletions(-) create mode 100644 mm/kasan/quarantine.cinclude/linux/kasan.h +0
-0@@ -50,6 +50,8 @@ void kasan_free_pages(struct page *page, unsigned int order);
[...]
Quoted 101 lines
mm/kasan/quarantine.c new file +95
-0@@ -0,0 +1,291 @@ +/* +* KASAN quarantine. +* +* Author: Alexander Potapenko <[email protected]> +* Copyright (C) 2016 Google, Inc. +* +* Based on code by Dmitry Chernenkov. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +*/ + +#include <linux/gfp.h> +#include <linux/hash.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/percpu.h> +#include <linux/printk.h> +#include <linux/shrinker.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "../slab.h" +#include "kasan.h" + +/* Data structure and operations for quarantine queues. */ + +/* +* Each queue is a signle-linked list, which also stores the total size of +* objects inside of it. +*/ +struct qlist_head { +struct qlist_node *head; +struct qlist_node *tail; +size_t bytes; +}; + +#define QLIST_INIT { NULL, NULL, 0 } + +static bool qlist_empty (struct qlist_head *q) +{ +return !q->head; +} + +static void qlist_init (struct qlist_head *q) +{ + q->head = q->tail =NULL ; + q->bytes =0 ; +} + +static void qlist_put (struct qlist_head *q,struct qlist_node *qlink, +size_t size) +{ +if (unlikely (qlist_empty (q))) + q->head = qlink; +else + q->tail->next = qlink; + q->tail = qlink; + qlink->next =NULL ; + q->bytes += size; +} + +static void qlist_move_all (struct qlist_head *from,struct qlist_head *to) +{ +if (unlikely (qlist_empty (from))) +return ; + +if (qlist_empty (to)) { + *to = *from; +qlist_init (from); +return ; + } + + to->tail->next = from->head; + to->tail = from->tail; + to->bytes += from->bytes; + +qlist_init (from); +} + +static void qlist_move (struct qlist_head *from,struct qlist_node *last, +struct qlist_head *to,size_t size) +{ +if (unlikely (last == from->tail)) { +qlist_move_all (from, to); +return ; + }
mm/kasan/quarantine.c line 105
Quoted 10 lines
mm/kasan/quarantine.c new file +10
-0+if (qlist_empty (to)) + to->head = from->head; +else + to->tail->next = from->head; + to->tail = last; + from->head = last->next; + last->next =NULL ; + from->bytes -= size; + to->bytes += size; +}
I see conversation with Andrew in previous emails about moving this code above into generic library facility but I don't anything is going on here. I also feel like this belongs to lib/*.
Do I miss something or did you decide to do it later?
[...]
Signature · 7 lines
Best regards, Klimov Alexey -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Alexander Potapenko
reviewerHello Alexey,
Quoted 192 lines · Alexey Klimov
Hi Alexander,
Quoted 69 lines · Alexander Potapenko
Quarantine isolates freed objects in a separate queue. The objects are returned to the allocator later, which helps to detect use-after-free errors.
Freed objects are first added to per-cpu quarantine queues. When a cache is destroyed or memory shrinking is requested, the objects are moved into the global quarantine queue. Whenever a kmalloc call allows memory reclaiming, the oldest objects are popped out of the global queue until the total size of objects in quarantine is less than 3/4 of the maximum quarantine size (which is a fraction of installed physical memory).
As long as an object remains in the quarantine, KASAN is able to report accesses to it, so the chance of reporting a use-after-free is increased. Once the object leaves quarantine, the allocator may reuse it, in which case the object is unpoisoned and KASAN can't detect incorrect accesses to it.
Right now quarantine support is only enabled in SLAB allocator. Unification of KASAN features in SLAB and SLUB will be done later.
This patch is based on the "mm: kasan: quarantine" patch originally prepared by Dmitry Chernenkov. A number of improvements have been suggested by Andrey Ryabinin.
Signed-off-by Alexander Potapenko <[email protected]>---
v2: - added copyright comments
- per request from Joonsoo Kim made__cache_free()more straightforward
- added comments for smp_load_acquire()/smp_store_release()v3: - incorporate changes introduced by the "mm, kasan: SLAB support" patch
v4: - fix kbuild compile-time error (missing
___cache_free()declaration)
and a warning (wrong format specifier)v6: - extended the patch description
- dropped the unusedqlist_remove()functionv9: - incorporate the fixes by Andrey Ryabinin:
* Fix comment styles,
* Get rid of some ifdefs
* Revert needless functions renames in quarantine patch
* Remove needless local_irq_save()/restore() inper_cpu_remove_cache()
* Add new 'structqlist_node' instead of 'void **' types. This makes code a bit more redable.
- remove the non-deterministic quarantine test
- dropped smp_load_acquire()/smp_store_release()--- include/linux/kasan.h | 13 ++- mm/kasan/Makefile | 1 + mm/kasan/kasan.c | 57 ++++++++-- mm/kasan/kasan.h | 21 +++- mm/kasan/quarantine.c | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++ mm/kasan/report.c | 1 + mm/mempool.c | 2 +- mm/slab.c | 12 ++- mm/slab.h | 2 + mm/slab_common.c | 2 + 10 files changed, 387 insertions(+), 15 deletions(-) create mode 100644 mm/kasan/quarantine.cinclude/linux/kasan.h +0
-0@@ -50,6 +50,8 @@ void kasan_free_pages(struct page *page, unsigned int order);[...]
Quoted 101 lines
mm/kasan/quarantine.c new file +95
-0@@ -0,0 +1,291 @@ +/* +* KASAN quarantine. +* +* Author: Alexander Potapenko <[email protected]> +* Copyright (C) 2016 Google, Inc. +* +* Based on code by Dmitry Chernenkov. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +*/ + +#include <linux/gfp.h> +#include <linux/hash.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/percpu.h> +#include <linux/printk.h> +#include <linux/shrinker.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "../slab.h" +#include "kasan.h" + +/* Data structure and operations for quarantine queues. */ + +/* +* Each queue is a signle-linked list, which also stores the total size of +* objects inside of it. +*/ +struct qlist_head { +struct qlist_node *head; +struct qlist_node *tail; +size_t bytes; +}; + +#define QLIST_INIT { NULL, NULL, 0 } + +static bool qlist_empty (struct qlist_head *q) +{ +return !q->head; +} + +static void qlist_init (struct qlist_head *q) +{ + q->head = q->tail =NULL ; + q->bytes =0 ; +} + +static void qlist_put (struct qlist_head *q,struct qlist_node *qlink, +size_t size) +{ +if (unlikely (qlist_empty (q))) + q->head = qlink; +else + q->tail->next = qlink; + q->tail = qlink; + qlink->next =NULL ; + q->bytes += size; +} + +static void qlist_move_all (struct qlist_head *from,struct qlist_head *to) +{ +if (unlikely (qlist_empty (from))) +return ; + +if (qlist_empty (to)) { + *to = *from; +qlist_init (from); +return ; + } + + to->tail->next = from->head; + to->tail = from->tail; + to->bytes += from->bytes; + +qlist_init (from); +} + +static void qlist_move (struct qlist_head *from,struct qlist_node *last, +struct qlist_head *to,size_t size) +{ +if (unlikely (last == from->tail)) { +qlist_move_all (from, to); +return ; + }mm/kasan/quarantine.c line 105
Quoted 10 lines
mm/kasan/quarantine.c new file +10
-0+if (qlist_empty (to)) + to->head = from->head; +else + to->tail->next = from->head; + to->tail = last; + from->head = last->next; + last->next =NULL ; + from->bytes -= size; + to->bytes += size; +}I see conversation with Andrew in previous emails about moving this code above into generic library facility but I don't anything is going on here. I also feel like this belongs to
lib/*.
Do I miss something or did you decide to do it later?
Quoted 1 line
[...]
I remember Andrew said that, but I think it will be cleaner to make that change in a separate patch.
Quoted 2 lines
--
Best regards, Klimov Alexey
Signature · 16 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>