Skip to content

Commit a7cb5d2

Browse files
melvertorvalds
authored andcommitted
kfence: fix is_kfence_address() for addresses below KFENCE_POOL_SIZE
Originally the addr != NULL check was meant to take care of the case where __kfence_pool == NULL (KFENCE is disabled). However, this does not work for addresses where addr > 0 && addr < KFENCE_POOL_SIZE. This can be the case on NULL-deref where addr > 0 && addr < PAGE_SIZE or any other faulting access with addr < KFENCE_POOL_SIZE. While the kernel would likely crash, the stack traces and report might be confusing due to double faults upon KFENCE's attempt to unprotect such an address. Fix it by just checking that __kfence_pool != NULL instead. Link: https://lkml.kernel.org/r/[email protected] Fixes: 0ce20dd ("mm: add Kernel Electric-Fence infrastructure") Signed-off-by: Marco Elver <[email protected]> Reported-by: Kuan-Ying Lee <[email protected]> Acked-by: Alexander Potapenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: <[email protected]> [5.12+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 57f2976 commit a7cb5d2

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

include/linux/kfence.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ extern atomic_t kfence_allocation_gate;
5151
static __always_inline bool is_kfence_address(const void *addr)
5252
{
5353
/*
54-
* The non-NULL check is required in case the __kfence_pool pointer was
55-
* never initialized; keep it in the slow-path after the range-check.
54+
* The __kfence_pool != NULL check is required to deal with the case
55+
* where __kfence_pool == NULL && addr < KFENCE_POOL_SIZE. Keep it in
56+
* the slow-path after the range-check!
5657
*/
57-
return unlikely((unsigned long)((char *)addr - __kfence_pool) < KFENCE_POOL_SIZE && addr);
58+
return unlikely((unsigned long)((char *)addr - __kfence_pool) < KFENCE_POOL_SIZE && __kfence_pool);
5859
}
5960

6061
/**

0 commit comments

Comments
 (0)