Skip to content

Commit 2e7c954

Browse files
xairyakpm00
authored andcommitted
kasan: add return value for kasan_mempool_poison_object
Add a return value for kasan_mempool_poison_object that lets the caller know whether the allocation is affected by a double-free or an invalid-free bug. The caller can use this return value to stop operating on the object. Also introduce a check_page_allocation helper function to improve the code readability. Link: https://lkml.kernel.org/r/618af65273875fb9f56954285443279b15f1fcd9.1703024586.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <[email protected]> Cc: Alexander Lobakin <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Breno Leitao <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Evgenii Stepanov <[email protected]> Cc: Marco Elver <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 1bb8430 commit 2e7c954

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

include/linux/kasan.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static __always_inline void * __must_check kasan_krealloc(const void *object,
212212
return (void *)object;
213213
}
214214

215-
void __kasan_mempool_poison_object(void *ptr, unsigned long ip);
215+
bool __kasan_mempool_poison_object(void *ptr, unsigned long ip);
216216
/**
217217
* kasan_mempool_poison_object - Check and poison a mempool slab allocation.
218218
* @ptr: Pointer to the slab allocation.
@@ -225,16 +225,20 @@ void __kasan_mempool_poison_object(void *ptr, unsigned long ip);
225225
* without putting it into the quarantine (for the Generic mode).
226226
*
227227
* This function also performs checks to detect double-free and invalid-free
228-
* bugs and reports them.
228+
* bugs and reports them. The caller can use the return value of this function
229+
* to find out if the allocation is buggy.
229230
*
230231
* This function operates on all slab allocations including large kmalloc
231232
* allocations (the ones returned by kmalloc_large() or by kmalloc() with the
232233
* size > KMALLOC_MAX_SIZE).
234+
*
235+
* Return: true if the allocation can be safely reused; false otherwise.
233236
*/
234-
static __always_inline void kasan_mempool_poison_object(void *ptr)
237+
static __always_inline bool kasan_mempool_poison_object(void *ptr)
235238
{
236239
if (kasan_enabled())
237-
__kasan_mempool_poison_object(ptr, _RET_IP_);
240+
return __kasan_mempool_poison_object(ptr, _RET_IP_);
241+
return true;
238242
}
239243

240244
/*
@@ -293,7 +297,10 @@ static inline void *kasan_krealloc(const void *object, size_t new_size,
293297
{
294298
return (void *)object;
295299
}
296-
static inline void kasan_mempool_poison_object(void *ptr) {}
300+
static inline bool kasan_mempool_poison_object(void *ptr)
301+
{
302+
return true;
303+
}
297304
static inline bool kasan_check_byte(const void *address)
298305
{
299306
return true;

mm/kasan/common.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ bool __kasan_slab_free(struct kmem_cache *cache, void *object,
254254
return ____kasan_slab_free(cache, object, ip, true, init);
255255
}
256256

257-
static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip)
257+
static inline bool check_page_allocation(void *ptr, unsigned long ip)
258258
{
259259
if (!kasan_arch_is_ready())
260260
return false;
@@ -269,17 +269,14 @@ static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip)
269269
return true;
270270
}
271271

272-
/*
273-
* The object will be poisoned by kasan_poison_pages() or
274-
* kasan_mempool_poison_object().
275-
*/
276-
277272
return false;
278273
}
279274

280275
void __kasan_kfree_large(void *ptr, unsigned long ip)
281276
{
282-
____kasan_kfree_large(ptr, ip);
277+
check_page_allocation(ptr, ip);
278+
279+
/* The object will be poisoned by kasan_poison_pages(). */
283280
}
284281

285282
void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
@@ -429,7 +426,7 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag
429426
return ____kasan_kmalloc(slab->slab_cache, object, size, flags);
430427
}
431428

432-
void __kasan_mempool_poison_object(void *ptr, unsigned long ip)
429+
bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)
433430
{
434431
struct folio *folio;
435432

@@ -442,13 +439,15 @@ void __kasan_mempool_poison_object(void *ptr, unsigned long ip)
442439
* KMALLOC_MAX_SIZE, and kmalloc falls back onto page_alloc.
443440
*/
444441
if (unlikely(!folio_test_slab(folio))) {
445-
if (____kasan_kfree_large(ptr, ip))
446-
return;
442+
if (check_page_allocation(ptr, ip))
443+
return false;
447444
kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false);
445+
return true;
448446
} else {
449447
struct slab *slab = folio_slab(folio);
450448

451-
____kasan_slab_free(slab->slab_cache, ptr, ip, false, false);
449+
return !____kasan_slab_free(slab->slab_cache, ptr, ip,
450+
false, false);
452451
}
453452
}
454453

0 commit comments

Comments
 (0)