Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  *  arch/arm/include/asm/kasan_def.h
0004  *
0005  *  Copyright (c) 2018 Huawei Technologies Co., Ltd.
0006  *
0007  *  Author: Abbott Liu <liuwenliang@huawei.com>
0008  */
0009 
0010 #ifndef __ASM_KASAN_DEF_H
0011 #define __ASM_KASAN_DEF_H
0012 
0013 #ifdef CONFIG_KASAN
0014 
0015 /*
0016  * Define KASAN_SHADOW_OFFSET,KASAN_SHADOW_START and KASAN_SHADOW_END for
0017  * the Arm kernel address sanitizer. We are "stealing" lowmem (the 4GB
0018  * addressable by a 32bit architecture) out of the virtual address
0019  * space to use as shadow memory for KASan as follows:
0020  *
0021  * +----+ 0xffffffff
0022  * |    |                           \
0023  * |    | |-> Static kernel image (vmlinux) BSS and page table
0024  * |    |/
0025  * +----+ PAGE_OFFSET
0026  * |    |                           \
0027  * |    | |->  Loadable kernel modules virtual address space area
0028  * |    |/
0029  * +----+ MODULES_VADDR = KASAN_SHADOW_END
0030  * |    |                       \
0031  * |    | |-> The shadow area of kernel virtual address.
0032  * |    |/
0033  * +----+->  TASK_SIZE (start of kernel space) = KASAN_SHADOW_START the
0034  * |    |\   shadow address of MODULES_VADDR
0035  * |    | |
0036  * |    | |
0037  * |    | |-> The user space area in lowmem. The kernel address
0038  * |    | |   sanitizer do not use this space, nor does it map it.
0039  * |    | |
0040  * |    | |
0041  * |    | |
0042  * |    | |
0043  * |    |/
0044  * ------ 0
0045  *
0046  * 1) KASAN_SHADOW_START
0047  *   This value begins with the MODULE_VADDR's shadow address. It is the
0048  *   start of kernel virtual space. Since we have modules to load, we need
0049  *   to cover also that area with shadow memory so we can find memory
0050  *   bugs in modules.
0051  *
0052  * 2) KASAN_SHADOW_END
0053  *   This value is the 0x100000000's shadow address: the mapping that would
0054  *   be after the end of the kernel memory at 0xffffffff. It is the end of
0055  *   kernel address sanitizer shadow area. It is also the start of the
0056  *   module area.
0057  *
0058  * 3) KASAN_SHADOW_OFFSET:
0059  *   This value is used to map an address to the corresponding shadow
0060  *   address by the following formula:
0061  *
0062  *  shadow_addr = (address >> 3) + KASAN_SHADOW_OFFSET;
0063  *
0064  *  As you would expect, >> 3 is equal to dividing by 8, meaning each
0065  *  byte in the shadow memory covers 8 bytes of kernel memory, so one
0066  *  bit shadow memory per byte of kernel memory is used.
0067  *
0068  *  The KASAN_SHADOW_OFFSET is provided in a Kconfig option depending
0069  *  on the VMSPLIT layout of the system: the kernel and userspace can
0070  *  split up lowmem in different ways according to needs, so we calculate
0071  *  the shadow offset depending on this.
0072  */
0073 
0074 #define KASAN_SHADOW_SCALE_SHIFT    3
0075 #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
0076 #define KASAN_SHADOW_END    ((UL(1) << (32 - KASAN_SHADOW_SCALE_SHIFT)) \
0077                  + KASAN_SHADOW_OFFSET)
0078 #define KASAN_SHADOW_START      ((KASAN_SHADOW_END >> 3) + KASAN_SHADOW_OFFSET)
0079 
0080 #endif
0081 #endif