Re: [PATCH v2 06/14] x86: Add arch specific kasan functions

From: Maciej Wieczor-Retman
Date: Fri Feb 21 2025 - 03:41:45 EST


On 2025-02-20 at 00:30:34 +0100, Andrey Konovalov wrote:
>On Tue, Feb 18, 2025 at 9:18 AM Maciej Wieczor-Retman
><maciej.wieczor-retman@xxxxxxxxx> wrote:
>>
>> KASAN's software tag-based mode needs multiple macros/functions to
>> handle tag and pointer interactions - mainly to set and retrieve tags
>> from the top bits of a pointer.
>>
>> Mimic functions currently used by arm64 but change the tag's position to
>> bits [60:57] in the pointer.
>>
>> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@xxxxxxxxx>
>> ---
>> arch/x86/include/asm/kasan.h | 32 ++++++++++++++++++++++++++++++--
>> 1 file changed, 30 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h
>> index de75306b932e..8829337a75fa 100644
>> --- a/arch/x86/include/asm/kasan.h
>> +++ b/arch/x86/include/asm/kasan.h
>> @@ -3,6 +3,8 @@
>> #define _ASM_X86_KASAN_H
>>
>> #include <linux/const.h>
>> +#include <linux/kasan-tags.h>
>> +#include <linux/types.h>
>> #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
>> #define KASAN_SHADOW_SCALE_SHIFT 3
>>
>> @@ -24,8 +26,33 @@
>> KASAN_SHADOW_SCALE_SHIFT)))
>>
>> #ifndef __ASSEMBLY__
>> +#include <linux/bitops.h>
>> +#include <linux/bitfield.h>
>> +#include <linux/bits.h>
>> +
>> +#define arch_kasan_set_tag(addr, tag) __tag_set(addr, tag)
>
>But __tag_set is defined below. I think these need to be reordered.

Oh, right. I'll fix it.

>
>> +#define arch_kasan_reset_tag(addr) __tag_reset(addr)
>> +#define arch_kasan_get_tag(addr) __tag_get(addr)
>> +
>> +#ifdef CONFIG_KASAN_SW_TAGS
>> +
>> +#define __tag_shifted(tag) FIELD_PREP(GENMASK_ULL(60, 57), tag)
>> +#define __tag_reset(addr) (sign_extend64((u64)(addr), 56))
>> +#define __tag_get(addr) ((u8)FIELD_GET(GENMASK_ULL(60, 57), (u64)addr))
>> +#else
>> +#define __tag_shifted(tag) 0UL
>> +#define __tag_reset(addr) (addr)
>> +#define __tag_get(addr) 0
>> +#endif /* CONFIG_KASAN_SW_TAGS */
>>
>> #ifdef CONFIG_KASAN
>> +
>> +static inline const void *__tag_set(const void *addr, u8 tag)
>
>A bit weird that __tag_set is defined under CONFIG_KASAN:
>CONFIG_KASAN_SW_TAGS (or no condition, like on arm64) would make more
>sense.

Ah sorry, I misread the arm code. I'll try doing a no condition.

>
>> +{
>> + u64 __addr = (u64)addr & ~__tag_shifted(KASAN_TAG_KERNEL);
>> + return (const void *)(__addr | __tag_shifted(tag));
>> +}
>> +
>> void __init kasan_early_init(void);
>> void __init kasan_init(void);
>> void __init kasan_populate_shadow_for_vaddr(void *va, size_t size, int nid);
>> @@ -34,8 +61,9 @@ static inline void kasan_early_init(void) { }
>> static inline void kasan_init(void) { }
>> static inline void kasan_populate_shadow_for_vaddr(void *va, size_t size,
>> int nid) { }
>> -#endif
>>
>> -#endif
>> +#endif /* CONFIG_KASAN */
>> +
>> +#endif /* __ASSEMBLY__ */
>>
>> #endif
>> --
>> 2.47.1
>>

--
Kind regards
Maciej Wieczór-Retman