Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753144AbbDCOsG (ORCPT ); Fri, 3 Apr 2015 10:48:06 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:33798 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753081AbbDCOsA (ORCPT ); Fri, 3 Apr 2015 10:48:00 -0400 X-AuditID: cbfec7f4-b7f126d000001e9a-68-551ea7715e7d From: Andrey Ryabinin To: Andrew Morton Cc: Andrey Ryabinin , David Rientjes , Dave Kleikamp , Christoph Hellwig , Sebastian Ott , Mikulas Patocka , Catalin Marinas , LKML , linux-mm@kvack.org, jfs-discussion@lists.sourceforge.net, Dmitry Chernenkov , Dmitry Vyukov , Alexander Potapenko Subject: [PATCH] mm, mempool: kasan: poison mempool elements Date: Fri, 03 Apr 2015 17:47:47 +0300 Message-id: <1428072467-21668-1-git-send-email-a.ryabinin@samsung.com> X-Mailer: git-send-email 2.3.5 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrELMWRmVeSWpSXmKPExsVy+t/xq7qFy+VCDe7/Y7fY9usRm8Wc9WvY LN4v62G0WDgp1mLCwzZ2i/aPe5ktVq4+ymRx8WMrk8XlXXPYLO6t+c9qMbHjKpNF25KNTBan ug4zWty+84PZgc9jzbw1jB47Z91l91iwqdRj06pONo9Nnyaxe5yY8ZvF48GhzSweuxd8ZvLY fbOBzeP9vqtsHn1bVjF6fN4kF8ATxWWTkpqTWZZapG+XwJWx5NB3toLVshVPT6U1MF6Q6GLk 5JAQMJFomXudEcIWk7hwbz1bFyMXh5DAUkaJjf+uQDl9TBJ9UyezglSxCehJ/Ju1nQ3EFhHQ lVj1fBczSBGzwBdmiY//OllAEsICNhINb46C2SwCqhIft2wGa+AVcJO4PPceO8Q6OYmrXy+z T2DkXsDIsIpRNLU0uaA4KT3XUK84Mbe4NC9dLzk/dxMjJFy/7GBcfMzqEKMAB6MSD6/GRLlQ IdbEsuLK3EOMEhzMSiK8LcuAQrwpiZVVqUX58UWlOanFhxiZODilGhjnVmz3XDxZKoFXQSMj TMdvIruFtqpX0K8Ypk6Z+XX7XsxgT/8iwbJqx8b8OVnPPz4RMfqUXDRZYt+xxAtt/FGMWzJX ab5m7r0y49efuXsXOArY3gxr/ZtWs3MpywmzDJ7+PAHxjv+qp3dI3X+Tu3tlubNv0s/Lgqd+ vHLW9l3T6BDcFPPNtFOJpTgj0VCLuag4EQAlypc/NQIAAA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3886 Lines: 119 Mempools keep allocated objects in reserved for situations when ordinary allocation may not be possible to satisfy. These objects shouldn't be accessed before they leave the pool. This patch poison elements when get into the pool and unpoison when they leave it. This will let KASan to detect use-after-free of mempool's elements. Signed-off-by: Andrey Ryabinin --- include/linux/kasan.h | 2 ++ mm/kasan/kasan.c | 13 +++++++++++++ mm/mempool.c | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 5bb0744..5486d77 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -44,6 +44,7 @@ void kasan_poison_object_data(struct kmem_cache *cache, void *object); void kasan_kmalloc_large(const void *ptr, size_t size); void kasan_kfree_large(const void *ptr); +void kasan_kfree(void *ptr); void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size); void kasan_krealloc(const void *object, size_t new_size); @@ -71,6 +72,7 @@ static inline void kasan_poison_object_data(struct kmem_cache *cache, static inline void kasan_kmalloc_large(void *ptr, size_t size) {} static inline void kasan_kfree_large(const void *ptr) {} +static inline void kasan_kfree(void *ptr) {} static inline void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size) {} static inline void kasan_krealloc(const void *object, size_t new_size) {} diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index 936d816..6c513a6 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -389,6 +389,19 @@ void kasan_krealloc(const void *object, size_t size) kasan_kmalloc(page->slab_cache, object, size); } +void kasan_kfree(void *ptr) +{ + struct page *page; + + page = virt_to_head_page(ptr); + + if (unlikely(!PageSlab(page))) + kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page), + KASAN_FREE_PAGE); + else + kasan_slab_free(page->slab_cache, ptr); +} + void kasan_kfree_large(const void *ptr) { struct page *page = virt_to_page(ptr); diff --git a/mm/mempool.c b/mm/mempool.c index acd597f..f884e24 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -100,10 +101,31 @@ static inline void poison_element(mempool_t *pool, void *element) } #endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */ +static void kasan_poison_element(mempool_t *pool, void *element) +{ + if (pool->alloc == mempool_alloc_slab) + kasan_slab_free(pool->pool_data, element); + if (pool->alloc == mempool_kmalloc) + kasan_kfree(element); + if (pool->alloc == mempool_alloc_pages) + kasan_free_pages(element, (unsigned long)pool->pool_data); +} + +static void kasan_unpoison_element(mempool_t *pool, void *element) +{ + if (pool->alloc == mempool_alloc_slab) + kasan_slab_alloc(pool->pool_data, element); + if (pool->alloc == mempool_kmalloc) + kasan_krealloc(element, (size_t)pool->pool_data); + if (pool->alloc == mempool_alloc_pages) + kasan_alloc_pages(element, (unsigned long)pool->pool_data); +} + static void add_element(mempool_t *pool, void *element) { BUG_ON(pool->curr_nr >= pool->min_nr); poison_element(pool, element); + kasan_poison_element(pool, element); pool->elements[pool->curr_nr++] = element; } @@ -113,6 +135,7 @@ static void *remove_element(mempool_t *pool) BUG_ON(pool->curr_nr < 0); check_element(pool, element); + kasan_unpoison_element(pool, element); return element; } -- 2.3.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/