When should we use KASAN (kernel address sanitizer) instead of ASAN (address sanitizer)? Can't we use ASAN instead of KASAN for the kernel? Why?
CodePudding user response:
ASAN is not going to be useful, because a kernel is not a normal program and has way more complex memory and address space semantics. It needs special treatment, and KASAN knows how to handle things because it's designed exactly for this purpose. Not to mention, that the way ASAN is implemented only allows it to be used in userspace applications on top of an operating system.
A simple userspace process only has:
- One virtual address space
- Automatic variables on stack
- Globals in bss/data/rodata sections
- Dynamically allocated data through e.g.
malloc()
A kernel on the other hand has:
- A lot of different memory allocators (kmalloc, vmalloc, alloc_pages, etc.) for different purposes
- A lot of different data sections (kernel data, module data, etc.)
- Multiple address spaces to manage and switch between
- DMA addresses, user virtual addresses and physical addresses
- Different ways of handling concurrency (RCU, locks of various kinds, refcounts, etc.)
- A lot more complex stuff to handle...
A kernel is in general much more complex than any userspace program, and therefore you need an address sanitizer specifically designed with this in mind, like KASAN.