From 0d8b6de03a40652ca9cf3d5cf3d16d28b99ad915 Mon Sep 17 00:00:00 2001 From: wangmingrong1 Date: Thu, 19 Sep 2024 20:09:48 +0800 Subject: [PATCH] mm/kasan: Map the generated shadow area to the fixed segment Since the size of the shadow area will change during the script merging of global variables, it will lead to unpredictable number of links. After fixing, only three links are required. Signed-off-by: wangmingrong1 --- .../arm/qemu/qemu-armv7a/scripts/dramboot.ld | 14 ++-- .../qemu/qemu-armv8a/scripts/dramboot.ld | 10 +-- .../qemu-rv/rv-virt/scripts/ld-kernel.script | 60 ++++++++-------- .../risc-v/qemu-rv/rv-virt/scripts/ld.script | 48 +++++++------ .../sg2000/milkv_duos/scripts/ld.script | 69 ++++++++++++------- tools/kasan_global.py | 10 ++- 6 files changed, 126 insertions(+), 85 deletions(-) diff --git a/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld b/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld index f419093cbb..06079d999b 100644 --- a/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld +++ b/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld @@ -28,16 +28,18 @@ SECTIONS . = 0x40101000; /* where the global variable out-of-bounds detection information located */ + #ifdef CONFIG_MM_KASAN_GLOBAL - .kasan.unused : - { + .kasan.unused : { *(.data..LASANLOC*) - } > ROM - .kasan.global : - { + } + .kasan.global : { KEEP (*(.data..LASAN0)) KEEP (*(.data.rel.local..LASAN0)) - } > ROM + } + .kasan.shadows : { + *(.kasan.shadows) + } #endif .text : { diff --git a/boards/arm64/qemu/qemu-armv8a/scripts/dramboot.ld b/boards/arm64/qemu/qemu-armv8a/scripts/dramboot.ld index d1997f26bc..44f0deaa7b 100644 --- a/boards/arm64/qemu/qemu-armv8a/scripts/dramboot.ld +++ b/boards/arm64/qemu/qemu-armv8a/scripts/dramboot.ld @@ -34,16 +34,18 @@ SECTIONS . = 0x40280000; /* uboot load address */ /* where the global variable out-of-bounds detection information located */ + #ifdef CONFIG_MM_KASAN_GLOBAL - .kasan.unused : - { + .kasan.unused : { *(.data..LASANLOC*) } - .kasan.global : - { + .kasan.global : { KEEP (*(.data..LASAN0)) KEEP (*(.data.rel.local..LASAN0)) } + .kasan.shadows : { + *(.kasan.shadows) + } #endif _start = .; diff --git a/boards/risc-v/qemu-rv/rv-virt/scripts/ld-kernel.script b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-kernel.script index 193a833f32..fb01640420 100644 --- a/boards/risc-v/qemu-rv/rv-virt/scripts/ld-kernel.script +++ b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-kernel.script @@ -60,16 +60,18 @@ SECTIONS . = 0x80000000; /* where the global variable out-of-bounds detection information located */ + #ifdef CONFIG_MM_KASAN_GLOBAL - .kasan.unused : - { + .kasan.unused : { *(.data..LASANLOC*) } - .kasan.global : - { + .kasan.global : { KEEP (*(.data..LASAN0)) KEEP (*(.data.rel.local..LASAN0)) } + .kasan.shadows : { + *(.kasan.shadows) + } #endif .text : @@ -111,17 +113,19 @@ SECTIONS _erodata = . ; } - .tdata : { + .tdata : + { _stdata = ABSOLUTE(.); *(.tdata .tdata.* .gnu.linkonce.td.*); _etdata = ABSOLUTE(.); - } + } - .tbss : { + .tbss : + { _stbss = ABSOLUTE(.); *(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon); _etbss = ABSOLUTE(.); - } + } _eronly = ABSOLUTE(.); @@ -136,9 +140,10 @@ SECTIONS _edata = . ; } - /* Page tables here, align to 4K boundary */ + /* Page tables here, align to 4K boundary */ - .pgtables (NOLOAD) : ALIGN(0x1000) { + .pgtables (NOLOAD) : ALIGN(0x1000) + { *(.pgtables) . = ALIGN(4); } > ksram @@ -155,25 +160,26 @@ SECTIONS _ebss = . ; } > ksram - /* Stack top */ + /* Stack top */ - .stack_top : { - . = ALIGN(32); - _ebss = ABSOLUTE(.); + .stack_top : + { + . = ALIGN(32); + _ebss = ABSOLUTE(.); } > ksram - /* Stabs debugging sections. */ + /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_info 0 : { *(.debug_info) } - .debug_line 0 : { *(.debug_line) } - .debug_pubnames 0 : { *(.debug_pubnames) } - .debug_aranges 0 : { *(.debug_aranges) } + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } } diff --git a/boards/risc-v/qemu-rv/rv-virt/scripts/ld.script b/boards/risc-v/qemu-rv/rv-virt/scripts/ld.script index e2c0bbd820..b91e06cc1a 100644 --- a/boards/risc-v/qemu-rv/rv-virt/scripts/ld.script +++ b/boards/risc-v/qemu-rv/rv-virt/scripts/ld.script @@ -27,16 +27,18 @@ SECTIONS . = TEXT_ADDR; /* where the global variable out-of-bounds detection information located */ + #ifdef CONFIG_MM_KASAN_GLOBAL - .kasan.unused : - { + .kasan.unused : { *(.data..LASANLOC*) } - .kasan.global : - { + .kasan.global : { KEEP (*(.data..LASAN0)) KEEP (*(.data.rel.local..LASAN0)) } + .kasan.shadows : { + *(.kasan.shadows) + } #endif .text : @@ -60,7 +62,7 @@ SECTIONS _etext = . ; } - .init_section : + .init_section : { _sinit = ABSOLUTE(.); KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) @@ -78,17 +80,19 @@ SECTIONS _erodata = . ; } - .tdata : { + .tdata : + { _stdata = ABSOLUTE(.); *(.tdata .tdata.* .gnu.linkonce.td.*); _etdata = ABSOLUTE(.); - } + } - .tbss : { + .tbss : + { _stbss = ABSOLUTE(.); *(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon); _etbss = ABSOLUTE(.); - } + } _eronly = ABSOLUTE(.); @@ -116,18 +120,18 @@ SECTIONS _ebss = . ; } - /* Stabs debugging sections. */ + /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_info 0 : { *(.debug_info) } - .debug_line 0 : { *(.debug_line) } - .debug_pubnames 0 : { *(.debug_pubnames) } - .debug_aranges 0 : { *(.debug_aranges) } + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } } diff --git a/boards/risc-v/sg2000/milkv_duos/scripts/ld.script b/boards/risc-v/sg2000/milkv_duos/scripts/ld.script index de72bf932b..511b293ffc 100644 --- a/boards/risc-v/sg2000/milkv_duos/scripts/ld.script +++ b/boards/risc-v/sg2000/milkv_duos/scripts/ld.script @@ -51,6 +51,21 @@ SECTIONS { . = 0x80200000; + /* where the global variable out-of-bounds detection information located */ + +#ifdef CONFIG_MM_KASAN_GLOBAL + .kasan.unused : { + *(.data..LASANLOC*) + } + .kasan.global : { + KEEP (*(.data..LASAN0)) + KEEP (*(.data.rel.local..LASAN0)) + } + .kasan.shadows : { + *(.kasan.shadows) + } +#endif + .text : { _stext = . ; @@ -82,17 +97,19 @@ SECTIONS _erodata = . ; } - .tdata : { + .tdata : + { _stdata = ABSOLUTE(.); *(.tdata .tdata.* .gnu.linkonce.td.*); _etdata = ABSOLUTE(.); - } + } - .tbss : { + .tbss : + { _stbss = ABSOLUTE(.); *(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon); _etbss = ABSOLUTE(.); - } + } _eronly = ABSOLUTE(.); @@ -120,32 +137,34 @@ SECTIONS _ebss = . ; } > ksram - /* Page tables here, align to 4K boundary */ + /* Page tables here, align to 4K boundary */ - .pgtables (NOLOAD) : ALIGN(0x1000) { - *(.pgtables) - . = ALIGN(4); + .pgtables (NOLOAD) : ALIGN(0x1000) + { + *(.pgtables) + . = ALIGN(4); } > ksram - /* Stack top */ + /* Stack top */ - .stack_top : { - . = ALIGN(32); - _ebss = ABSOLUTE(.); + .stack_top : + { + . = ALIGN(32); + _ebss = ABSOLUTE(.); } > ksram - /* Stabs debugging sections. */ + /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_info 0 : { *(.debug_info) } - .debug_line 0 : { *(.debug_line) } - .debug_pubnames 0 : { *(.debug_pubnames) } - .debug_aranges 0 : { *(.debug_aranges) } + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } } diff --git a/tools/kasan_global.py b/tools/kasan_global.py index 061725fa96..ff00b98c2f 100755 --- a/tools/kasan_global.py +++ b/tools/kasan_global.py @@ -39,6 +39,9 @@ KASAN_MAX_DATA_GAP = 1 << 16 # generated by the compiler is located KASAN_SECTION = ".kasan.global" +# Segments stored in the shadow area +KASAN_SHADOW_SECTION = ".kasan.shadows" + # The structure of parsing strings required for 32-bit and 64 bit KASAN_GLOBAL_STRUCT_32 = Struct( "beg" / Int32ul, @@ -197,9 +200,14 @@ def create_kasan_file(config: Config, region_list=[]): region: KASanRegion = None with open(config.outpath, "w") as file: + file.write("#include \n") + # Write the kasan region array for i in range(len(region_list)): - file.write("static const unsigned char\nglobals_region%d[] = {\n" % (i)) + file.write( + 'static const unsigned char locate_data("%s")' + "\nglobals_region%d[] = {\n" % (KASAN_SHADOW_SECTION, i) + ) region = region_list[i] # Fill the array of regions