kasan: fix kasan_byte_accessible() to be consistent with actual checks
Subsystems (2)
PATCH 1/1 kasan: fix kasan_byte_accessible() to be consistent with actual checks
We can sometimes end up with kasan_byte_accessible() being called on non-slab memory. For example ksize() and krealloc() may end up calling it on KFENCE allocated memory. In this case the memory will be tagged with KASAN_SHADOW_INIT, which a subsequent patch ("kasan:
initialize shadow to TAG_INVALID for SW_TAGS") will set to the same value as KASAN_TAG_INVALID, causing kasan_byte_accessible() to fail when called on non-slab memory.
This highlighted the fact that the check in kasan_byte_accessible() was inconsistent with checks as implemented for loads and stores (kasan_check_range() in SW tags mode and hardware-implemented checks in HW tags mode). kasan_check_range() does not have a check for KASAN_TAG_INVALID, and instead has a comparison against KASAN_SHADOW_START. In HW tags mode, we do not have either, but we do set TCR_EL1.TCMA which corresponds with the comparison against KASAN_TAG_KERNEL.
Therefore, update kasan_byte_accessible() for both SW and HW tags modes to correspond with the respective checks on loads and stores.
--- mm/kasan/kasan.h | 3 +-- mm/kasan/sw_tags.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-)
mm/kasan/kasan.h +1 -2
@@ -368,8 +368,7 @@ static inline bool kasan_byte_accessible(const void *addr)u8 ptr_tag =get_tag (addr);u8 mem_tag =hw_get_mem_tag ((void *)addr); -return (mem_tag !=KASAN_TAG_INVALID ) && - (ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag); + return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag; }#else /* CONFIG_KASAN_HW_TAGS */
mm/kasan/sw_tags.c +5 -3
@@ -121,10 +121,12 @@ bool kasan_check_range(unsigned long addr, size_t size, bool write,bool kasan_byte_accessible (const void *addr) {u8 tag =get_tag (addr); -u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (kasan_reset_tag (addr))); +void *untagged_addr =kasan_reset_tag (addr); +u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (untagged_addr)); -return (shadow_byte !=KASAN_TAG_INVALID ) && - (tag ==KASAN_TAG_KERNEL || tag == shadow_byte); +return untagged_addr >= +kasan_shadow_to_mem ((void *)KASAN_SHADOW_START ) && + (tag ==KASAN_TAG_KERNEL || tag == shadow_byte); }#define DEFINE_HWASAN_LOAD_STORE (size) \
Signature · 1 line
2.31.0.208.g409f899ff0-goog
Andrey Konovalov
reviewerQuoted 47 lines · Peter Collingbourne
We can sometimes end up with
kasan_byte_accessible()being called on non-slab memory. For exampleksize()andkrealloc()may end up calling it on KFENCE allocated memory. In this case the memory will be tagged withKASAN_SHADOW_INIT, which a subsequent patch ("kasan:
initialize shadow toTAG_INVALIDforSW_TAGS") will set to the same value asKASAN_TAG_INVALID, causingkasan_byte_accessible()to fail when called on non-slab memory.This highlighted the fact that the check in
kasan_byte_accessible()was inconsistent with checks as implemented for loads and stores (kasan_check_range()in SW tags mode and hardware-implemented checks in HW tags mode).kasan_check_range()does not have a check forKASAN_TAG_INVALID, and instead has a comparison againstKASAN_SHADOW_START. In HW tags mode, we do not have either, but we do set TCR_EL1.TCMA which corresponds with the comparison againstKASAN_TAG_KERNEL.Therefore, update
kasan_byte_accessible()for both SW and HW tags modes to correspond with the respective checks on loads and stores.Signed-off-by Peter Collingbourne <[email protected]>--- mm/kasan/kasan.h | 3 +-- mm/kasan/sw_tags.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-)mm/kasan/kasan.h +1
-2@@ -368,8 +368,7 @@ static inline bool kasan_byte_accessible(const void *addr)u8 ptr_tag =get_tag (addr);u8 mem_tag =hw_get_mem_tag ((void *)addr); -return (mem_tag !=KASAN_TAG_INVALID ) && - (ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag); + return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag; }#else /* CONFIG_KASAN_HW_TAGS */ mm/kasan/sw_tags.c +0
-0
mm/kasan/sw_tags.c bool kasan_check_range(unsigned long addr, size_t size, bool write, · line 125
Quoted 6 lines
mm/kasan/sw_tags.c +2
-1@@ -121,10 +121,12 @@ bool kasan_check_range(unsigned long addr, size_t size, bool write,bool kasan_byte_accessible (const void *addr) {u8 tag =get_tag (addr); -u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (kasan_reset_tag (addr))); +void *untagged_addr =kasan_reset_tag (addr); +u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (untagged_addr));
Hi Peter,
Let's move dereferencing shadow memory past the KASAN_SHADOW_START check. Otherwise, in case the check is to fail, accessing shadow will likely crash the kernel.
Thanks!
Quoted 12 lines
mm/kasan/sw_tags.c +3
-3-return (shadow_byte !=KASAN_TAG_INVALID ) && - (tag ==KASAN_TAG_KERNEL || tag == shadow_byte); +return untagged_addr >= +kasan_shadow_to_mem ((void *)KASAN_SHADOW_START ) && + (tag ==KASAN_TAG_KERNEL || tag == shadow_byte); }#define DEFINE_HWASAN_LOAD_STORE (size) \ --2.31.0.208.g409f899ff0-goog
Quoted 58 lines · Andrey Konovalov
Quoted 47 lines · Peter Collingbourne
We can sometimes end up with
kasan_byte_accessible()being called on non-slab memory. For exampleksize()andkrealloc()may end up calling it on KFENCE allocated memory. In this case the memory will be tagged withKASAN_SHADOW_INIT, which a subsequent patch ("kasan:
initialize shadow toTAG_INVALIDforSW_TAGS") will set to the same value asKASAN_TAG_INVALID, causingkasan_byte_accessible()to fail when called on non-slab memory.This highlighted the fact that the check in
kasan_byte_accessible()was inconsistent with checks as implemented for loads and stores (kasan_check_range()in SW tags mode and hardware-implemented checks in HW tags mode).kasan_check_range()does not have a check forKASAN_TAG_INVALID, and instead has a comparison againstKASAN_SHADOW_START. In HW tags mode, we do not have either, but we do set TCR_EL1.TCMA which corresponds with the comparison againstKASAN_TAG_KERNEL.Therefore, update
kasan_byte_accessible()for both SW and HW tags modes to correspond with the respective checks on loads and stores.Signed-off-by Peter Collingbourne <[email protected]>--- mm/kasan/kasan.h | 3 +-- mm/kasan/sw_tags.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-)mm/kasan/kasan.h +1
-2@@ -368,8 +368,7 @@ static inline bool kasan_byte_accessible(const void *addr)u8 ptr_tag =get_tag (addr);u8 mem_tag =hw_get_mem_tag ((void *)addr); -return (mem_tag !=KASAN_TAG_INVALID ) && - (ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag); + return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag; }#else /* CONFIG_KASAN_HW_TAGS */ mm/kasan/sw_tags.c +0
-0mm/kasan/sw_tags.c bool kasan_check_range(unsigned long addr, size_t size, bool write, · line 125
Quoted 6 lines
mm/kasan/sw_tags.c +2
-1@@ -121,10 +121,12 @@ bool kasan_check_range(unsigned long addr, size_t size, bool write,bool kasan_byte_accessible (const void *addr) {u8 tag =get_tag (addr); -u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (kasan_reset_tag (addr))); +void *untagged_addr =kasan_reset_tag (addr); +u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (untagged_addr));Hi Peter,
Quoted 5 lines
Let's move dereferencing shadow memory past the
KASAN_SHADOW_STARTcheck. Otherwise, in case the check is to fail, accessing shadow will likely crash the kernel.Thanks!
Makes sense, fixed in v2.
Peter