Skip to contentSkip to conversation

arm/kasan:fix the arry size of kasan_early_shadow_pte

PATCH Applied Hailong liu 4 participants 7 messages Jan 9, 2021 – Jan 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm/kasan:fix the arry size of kasan_early_shadow_pte

From: Hailong Liu <[email protected]>

The size of kasan_early_shadow_pte[] now is PTRS_PER_PTE which defined to 512 for arm architecture. This means that it only covers the prev Linux pte entries, but not the HWTABLE pte entries for arm.

The reason it works well current is that the symbol kasan_early_shadow_page immediately following kasan_early_shadow_pte in memory is page aligned, which makes kasan_early_shadow_pte look like a 4KB size array. But we can't ensure the order always right with different compiler/linker, nor more bss symbols be introduced.

We had a test with QEMU + vexpress:put a 512KB-size symbol with attribute __section(".bss..page_aligned") after kasan_early_shadow_pte, and poison it after kasan_early_init(). Then enabled CONFIG_KASAN, it failed to boot up.

---
 include/linux/kasan.h | 6 +++++-
 mm/kasan/init.c       | 3 ++-
 2 files changed, 7 insertions(+), 2 deletions(-)
include/linux/kasan.h +5 -1
index 5e0655fb2a6f..fe1ae73ff8b5 100644
@@ -35,8 +35,12 @@ struct kunit_kasan_expectation {
 #define KASAN_SHADOW_INIT 0
 #endif
 
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif
+
 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
-extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
+extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS];
 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
mm/kasan/init.c +2 -1
index bc0ad208b3a7..7ca0b92d5886 100644
@@ -64,7 +64,8 @@ static inline bool kasan_pmd_table(pud_t pud)
 	return false;
 }
 #endif
-pte_t kasan_early_shadow_pte[PTRS_PER_PTE] __page_aligned_bss;
+pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]
+	__page_aligned_bss;
 
 static inline bool kasan_pte_table(pmd_t pmd)
 {
Signature · 1 line
2.17.1
#1 lore raw

(+ Linus)

Quoted 64 lines · Hailong liu

From: Hailong Liu <[email protected]>

The size of kasan_early_shadow_pte[] now is PTRS_PER_PTE which defined to 512 for arm architecture. This means that it only covers the prev Linux pte entries, but not the HWTABLE pte entries for arm.

The reason it works well current is that the symbol kasan_early_shadow_page immediately following kasan_early_shadow_pte in memory is page aligned, which makes kasan_early_shadow_pte look like a 4KB size array. But we can't ensure the order always right with different compiler/linker, nor more bss symbols be introduced.

We had a test with QEMU + vexpress:put a 512KB-size symbol with attribute __section(".bss..page_aligned") after kasan_early_shadow_pte, and poison it after kasan_early_init(). Then enabled CONFIG_KASAN, it failed to boot up.

---
 include/linux/kasan.h | 6 +++++-
 mm/kasan/init.c       | 3 ++-
 2 files changed, 7 insertions(+), 2 deletions(-)
include/linux/kasan.h +5 -1
index 5e0655fb2a6f..fe1ae73ff8b5 100644
@@ -35,8 +35,12 @@ struct kunit_kasan_expectation {
 #define KASAN_SHADOW_INIT 0
 #endif
 
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif
+
 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
-extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
+extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS];
 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
mm/kasan/init.c +2 -2
index bc0ad208b3a7..7ca0b92d5886 100644
@@ -64,7 +64,8 @@ static inline bool kasan_pmd_table(pud_t pud)
        return false;
 }
 #endif
-pte_t kasan_early_shadow_pte[PTRS_PER_PTE] __page_aligned_bss;
+pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]
+       __page_aligned_bss;
 
 static inline bool kasan_pte_table(pmd_t pmd)
 {
--

2.17.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Reviewed-by on v1 1/1
#2 lore raw
In reply to Hailong liu
Quoted 12 lines · Hailong liu

From: Hailong Liu <[email protected]>

The size of kasan_early_shadow_pte[] now is PTRS_PER_PTE which defined to 512 for arm architecture. This means that it only covers the prev Linux pte entries, but not the HWTABLE pte entries for arm.

The reason it works well current is that the symbol kasan_early_shadow_page immediately following kasan_early_shadow_pte in memory is page aligned, which makes kasan_early_shadow_pte look like a 4KB size array. But we can't ensure the order always right with different compiler/linker, nor more bss symbols be introduced.

Quoted 6 lines

We had a test with QEMU + vexpress:put a 512KB-size symbol with attribute __section(".bss..page_aligned") after kasan_early_shadow_pte, and poison it after kasan_early_init(). Then enabled CONFIG_KASAN, it failed to boot up.

OK I see the problem, I think.

include/linux/kasan.h struct kunit_kasan_expectation { · line 40

Quoted 3 lines
include/linux/kasan.h +3 -0
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif

Can this even happen? We have either pgtable-2level.h or pgtable-3level.h, both of which define PTE_HWTABLE_PTRS.

include/linux/kasan.h struct kunit_kasan_expectation { · line 43

Quoted 3 lines

extern unsigned char kasan_early_shadow_page[PAGE_SIZE];

include/linux/kasan.h +1 -1
-extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
+extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS];

Yeah this looks exactly like bm_pte so it makes sense.

If you drop the first ifndef,

Yours,
Linus Walleij

author
#3 lore raw
Quoted 21 lines · Linus Walleij
Quoted 12 lines · Hailong liu

From: Hailong Liu <[email protected]>

The size of kasan_early_shadow_pte[] now is PTRS_PER_PTE which defined to 512 for arm architecture. This means that it only covers the prev Linux pte entries, but not the HWTABLE pte entries for arm.

The reason it works well current is that the symbol kasan_early_shadow_page immediately following kasan_early_shadow_pte in memory is page aligned, which makes kasan_early_shadow_pte look like a 4KB size array. But we can't ensure the order always right with different compiler/linker, nor more bss symbols be introduced.

Quoted 6 lines

We had a test with QEMU + vexpress:put a 512KB-size symbol with attribute __section(".bss..page_aligned") after kasan_early_shadow_pte, and poison it after kasan_early_init(). Then enabled CONFIG_KASAN, it failed to boot up.

Quoted 9 lines

OK I see the problem, I think.

include/linux/kasan.h struct kunit_kasan_expectation { · line 40

Quoted 3 lines
include/linux/kasan.h +3 -0
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif

Can this even happen? We have either pgtable-2level.h or pgtable-3level.h, both of which define PTE_HWTABLE_PTRS.

I guess not for arm. But I'm not sure for other ARCHs.

Quoted 6 lines

include/linux/kasan.h struct kunit_kasan_expectation { · line 43

Quoted 3 lines

extern unsigned char kasan_early_shadow_page[PAGE_SIZE];

include/linux/kasan.h +1 -1
-extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
+extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS];

Yeah this looks exactly like bm_pte so it makes sense.

Yes, it comes from prototype of pm_pte. And it does solve the problems our project encountered and seems to work well for now.

Quoted 5 lines

If you drop the first ifndef,

Yours,
Linus Walleij

Thanks.

#4 lore raw
Quoted 9 lines · hailong
Quoted 7 lines

include/linux/kasan.h struct kunit_kasan_expectation { · line 40

Quoted 3 lines
include/linux/kasan.h +3 -0
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif

Can this even happen? We have either pgtable-2level.h or pgtable-3level.h, both of which define PTE_HWTABLE_PTRS.

I guess not for arm. But I'm not sure for other ARCHs.

Oh it's a generic include. Sorry for the confusion.

All good then!

Yours,
Linus Walleij

maintainer
#5 lore raw
Quoted 10 lines · On Sun, 10 Jan 2021 13:03:49 +0100 Linus Walleij <[email protected]>…
Quoted 8 lines · hailong
Quoted 7 lines

include/linux/kasan.h struct kunit_kasan_expectation { · line 40

Quoted 3 lines
include/linux/kasan.h +3 -0
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif

Can this even happen? We have either pgtable-2level.h or pgtable-3level.h, both of which define PTE_HWTABLE_PTRS.

Quoted 6 lines
Quoted 1 line

I guess not for arm. But I'm not sure for other ARCHs.

Oh it's a generic include. Sorry for the confusion.

All good then!

This code is 2+ years old. Do we think it warrants a cc:stable?

#6 lore raw
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Sun, 10 Jan 2021 13:03:49 +0100 Linus Walleij <[email protected]>…
Quoted 8 lines · hailong
Quoted 7 lines

include/linux/kasan.h struct kunit_kasan_expectation { · line 40

Quoted 3 lines
include/linux/kasan.h +3 -0
+#ifndef PTE_HWTABLE_PTRS
+#define PTE_HWTABLE_PTRS 0
+#endif

Can this even happen? We have either pgtable-2level.h or pgtable-3level.h, both of which define PTE_HWTABLE_PTRS.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

I guess not for arm. But I'm not sure for other ARCHs.

Oh it's a generic include. Sorry for the confusion.

All good then!

This code is 2+ years old. Do we think it warrants a cc:stable?

Not needed - ARM only gained Kasan support this cycle, and this patch does not affect any other architectures