Skip to contentSkip to conversation

arm64: mm: Map fixmap PTE tables r/o in the linear map

RFC PATCH Superseded Ard Biesheuvel 3 participants 8 messages Aug 5 – Aug 27 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 arm64: mm: Map fixmap PTE tables r/o in the linear map

From: Ard Biesheuvel <[email protected]>

Without physical KASLR, the fixmap page tables will appear at an a priori known offset in the physical address space, and due to the lack of randomization, the linear map carries a writeable alias of the fixmap PTE pages, which appears at an offset in the kernel VA space that is also predictable.

Given that the placement of the fixmap area is never randomized either, a single store to this linear alias region is sufficient to map any physical page with any permissions at a known offset in the kernel VA space, including on top of the PTI trampoline.

Avoid this, by remapping the fixmap PTE pages read-only in the linear map. This is possible because all updates to bm_pte[] occur via the mapping of the kernel image in the vmap area. A read-only mapping is still needed for things like ptdump that walk the page tables.

Cc Ryan Roberts <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Liz Prucka <[email protected]>
Cc Seth Jenkins <[email protected]>
Cc Kees Cook <[email protected]>
Cc Jann Horn <[email protected]>
---
 arch/arm64/include/asm/set_memory.h |  2 ++
 arch/arm64/mm/fixmap.c              |  7 +++++++
 arch/arm64/mm/pageattr.c            | 10 ++++++++++
 3 files changed, 19 insertions(+)
arch/arm64/include/asm/set_memory.h +2 -0
index 90f61b17275e..a685fb534c3e 100644
@@ -11,6 +11,8 @@ bool can_set_direct_map(void);
 
 int set_memory_valid(unsigned long addr, int numpages, int enable);
 
+int set_direct_map_ro(unsigned long addr, int numpages);
+
 int set_direct_map_invalid_noflush(struct page *page);
 int set_direct_map_default_noflush(struct page *page);
 int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid);
arch/arm64/mm/fixmap.c +7 -0
index f66a0016dd02..fcb571dffe82 100644
@@ -14,6 +14,7 @@
 #include <asm/fixmap.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/pgalloc.h>
+#include <asm/set_memory.h>
 #include <asm/tlbflush.h>
 
 /* ensure that the fixmap region does not grow down into the PCI I/O region */
@@ -173,3 +174,9 @@ void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
 
 	return dt_virt;
 }
+
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);
+}
+late_initcall(fixmap_remap_ro);
arch/arm64/mm/pageattr.c +10 -0
index bbe98ac9ad8c..5072b14d4f9d 100644
@@ -251,6 +251,16 @@ int set_memory_valid(unsigned long addr, int numpages, int enable)
 					__pgprot(PTE_PRESENT_VALID_KERNEL));
 }
 
+int set_direct_map_ro(unsigned long addr, int numpages)
+{
+	if (!can_set_direct_map())
+		return 0;
+
+	return __change_memory_common(addr, PAGE_SIZE * numpages,
+				      __pgprot(PTE_RDONLY),
+				      __pgprot(PTE_WRITE));
+}
+
 int set_direct_map_invalid_noflush(struct page *page)
 {
 	pgprot_t clear_mask = __pgprot(PTE_PRESENT_VALID_KERNEL);
Signature · 1 line
2.55.0.571.g244d577d93-goog
#1 lore raw
Quoted 8 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Without physical KASLR, the fixmap page tables will appear at an a priori known offset in the physical address space, and due to the lack of randomization, the linear map carries a writeable alias of the fixmap PTE pages, which appears at an offset in the kernel VA space that is also predictable.

Quoted 7 lines

Given that the placement of the fixmap area is never randomized either, a single store to this linear alias region is sufficient to map any physical page with any permissions at a known offset in the kernel VA space, including on top of the PTI trampoline.

Avoid this, by remapping the fixmap PTE pages read-only in the linear map.

Sounds good, logical next step after unmapping the rest of data/BSS from the linear map :)

Quoted 45 lines

This is possible because all updates to bm_pte[] occur via the mapping of the kernel image in the vmap area. A read-only mapping is still needed for things like ptdump that walk the page tables.

Cc Ryan Roberts <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Liz Prucka <[email protected]>
Cc Seth Jenkins <[email protected]>
Cc Kees Cook <[email protected]>
Cc Jann Horn <[email protected]>
---
 arch/arm64/include/asm/set_memory.h |  2 ++
 arch/arm64/mm/fixmap.c              |  7 +++++++
 arch/arm64/mm/pageattr.c            | 10 ++++++++++
 3 files changed, 19 insertions(+)
arch/arm64/include/asm/set_memory.h +2 -0
index 90f61b17275e..a685fb534c3e 100644
@@ -11,6 +11,8 @@ bool can_set_direct_map(void);
 
 int set_memory_valid(unsigned long addr, int numpages, int enable);
 
+int set_direct_map_ro(unsigned long addr, int numpages);
+
 int set_direct_map_invalid_noflush(struct page *page);
 int set_direct_map_default_noflush(struct page *page);
 int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid);
arch/arm64/mm/fixmap.c +1 -0
index f66a0016dd02..fcb571dffe82 100644
@@ -14,6 +14,7 @@
 #include <asm/fixmap.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/pgalloc.h>
+#include <asm/set_memory.h>
 #include <asm/tlbflush.h>
 
 /* ensure that the fixmap region does not grow down into the PCI I/O region */

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 180

Quoted 7 lines
arch/arm64/mm/fixmap.c +4 -0
@@ -173,3 +174,9 @@ void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
 
 	return dt_virt;
 }
+
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);

Should we not also remap bm_pmd and bm_pud?

For that matter, do we need RW access via the linear map for any page annotated with __bss_pgtbl? I suppose that might be the case for kasan_early_shadow_* but I don't know enough about KASAN to tell for sure.

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 182

Quoted 2 lines
arch/arm64/mm/fixmap.c +2 -0
+}
+late_initcall(fixmap_remap_ro);

Is mark_rodata_ro() definitely too early to remap these pages RO?

- Kevin

Quoted 21 lines
arch/arm64/mm/pageattr.c +10 -0
index bbe98ac9ad8c..5072b14d4f9d 100644
@@ -251,6 +251,16 @@ int set_memory_valid(unsigned long addr, int numpages, int enable)
 					__pgprot(PTE_PRESENT_VALID_KERNEL));
 }
 
+int set_direct_map_ro(unsigned long addr, int numpages)
+{
+	if (!can_set_direct_map())
+		return 0;
+
+	return __change_memory_common(addr, PAGE_SIZE * numpages,
+				      __pgprot(PTE_RDONLY),
+				      __pgprot(PTE_WRITE));
+}
+
 int set_direct_map_invalid_noflush(struct page *page)
 {
 	pgprot_t clear_mask = __pgprot(PTE_PRESENT_VALID_KERNEL);
#2 lore raw
Quoted 14 lines · Kevin Brodsky
Quoted 13 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Without physical KASLR, the fixmap page tables will appear at an a priori known offset in the physical address space, and due to the lack of randomization, the linear map carries a writeable alias of the fixmap PTE pages, which appears at an offset in the kernel VA space that is also predictable.

Given that the placement of the fixmap area is never randomized either, a single store to this linear alias region is sufficient to map any physical page with any permissions at a known offset in the kernel VA space, including on top of the PTI trampoline.

Quoted 6 lines
Quoted 2 lines

Avoid this, by remapping the fixmap PTE pages read-only in the linear map.

Sounds good, logical next step after unmapping the rest of data/BSS from the linear map :)

Indeed :-)

Quoted 55 lines
Quoted 45 lines

This is possible because all updates to bm_pte[] occur via the mapping of the kernel image in the vmap area. A read-only mapping is still needed for things like ptdump that walk the page tables.

Cc Ryan Roberts <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Liz Prucka <[email protected]>
Cc Seth Jenkins <[email protected]>
Cc Kees Cook <[email protected]>
Cc Jann Horn <[email protected]>
---
 arch/arm64/include/asm/set_memory.h |  2 ++
 arch/arm64/mm/fixmap.c              |  7 +++++++
 arch/arm64/mm/pageattr.c            | 10 ++++++++++
 3 files changed, 19 insertions(+)
arch/arm64/include/asm/set_memory.h +2 -0
index 90f61b17275e..a685fb534c3e 100644
@@ -11,6 +11,8 @@ bool can_set_direct_map(void);
 
 int set_memory_valid(unsigned long addr, int numpages, int enable);
 
+int set_direct_map_ro(unsigned long addr, int numpages);
+
 int set_direct_map_invalid_noflush(struct page *page);
 int set_direct_map_default_noflush(struct page *page);
 int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid);
arch/arm64/mm/fixmap.c +1 -0
index f66a0016dd02..fcb571dffe82 100644
@@ -14,6 +14,7 @@
 #include <asm/fixmap.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/pgalloc.h>
+#include <asm/set_memory.h>
 #include <asm/tlbflush.h>
 
 /* ensure that the fixmap region does not grow down into the PCI I/O region */

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 180

Quoted 7 lines
arch/arm64/mm/fixmap.c +4 -0
@@ -173,3 +174,9 @@ void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
 
 	return dt_virt;
 }
+
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);

Should we not also remap bm_pmd and bm_pud?

Quoted 4 lines

For that matter, do we need RW access via the linear map for any page annotated with __bss_pgtbl? I suppose that might be the case for kasan_early_shadow_* but I don't know enough about KASAN to tell for sure.

bm_pte[] is special because it is only ever written via the kernel mapping, and never via the linear map. This is why it is being singled out in this patch.

Whether or not bm_pmd[] can be treated as a special case depends on the page
size: with 4k pages, the whole array covers a virtual region of 1G, which is currently guaranteed to be shared only with the PCI I/O space (but we could move that out). With 16k pages, it covers 64G, and so it is shared with the vmemmap and other virtual mappings in the vmalloc region, and so the current kernel mapping code expects to be able to write those entries.

What we might do is generalize the logic that uses the fixmap to modify pgd level entries in swapper_pg_dir, and use it for all modifications at PMD level or higher if those tables are in .rodata

But this is a bit more complicated than this change, so I decided to present this as a separate change.

Quoted 5 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 182

Quoted 2 lines
arch/arm64/mm/fixmap.c +2 -0
+}
+late_initcall(fixmap_remap_ro);

Is mark_rodata_ro() definitely too early to remap these pages RO?

No, we might just call this from there, afaict.

#3 lore raw
Quoted 7 lines · Ard Biesheuvel
Quoted 7 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 180

Quoted 5 lines

[...]

arch/arm64/mm/fixmap.c +3 -0
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);

Should we not also remap bm_pmd and bm_pud?

Quoted 7 lines
Quoted 4 lines

For that matter, do we need RW access via the linear map for any page annotated with __bss_pgtbl? I suppose that might be the case for kasan_early_shadow_* but I don't know enough about KASAN to tell for sure.

bm_pte[] is special because it is only ever written via the kernel mapping, and never via the linear map. This is why it is being singled out in this patch.

Right I see __set_fixmap(). This is a very special case of calling __set_pte() on a pointer derived from a global (i.e. pointing to the kernel image and not the LM) so it makes sense to treat it differently.

I do wonder whether there is that much value in protecting bm_pte while all page tables in init_pg_dir (also at a fixed offset in the LM) remain writeable though.

Either way all this is interesting for my series protecting page tables with pkeys [1] - it seems that to protect the fixmap, the easiest option would be to change the pkey of .pgtlb in both the kernel image and LM (the former for bm_pte, and the latter for everything else).

[1] https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Whether or not bm_pmd[] can be treated as a special case depends on the page
size: with 4k pages, the whole array covers a virtual region of 1G, which is currently guaranteed to be shared only with the PCI I/O space (but we could move that out). With 16k pages, it covers 64G, and so it is shared with the vmemmap and other virtual mappings in the vmalloc region, and so the current kernel mapping code expects to be able to write those entries.

Quoted 6 lines

What we might do is generalize the logic that uses the fixmap to modify pgd level entries in swapper_pg_dir, and use it for all modifications at PMD level or higher if those tables are in .rodata

But this is a bit more complicated than this change, so I decided to present this as a separate change.

Definitely, this is orthogonal to this patch. I wonder what the performance impact would be if we used the fixmap for setting all kernel entries at PMD and above. It would be nice to keep that logic easily togglable, because with the kpkeys approach I mentioned above we have a (most likely) much cheaper way of protecting these page tables.

Quoted 5 lines
Quoted 4 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 182

Quoted 2 lines
arch/arm64/mm/fixmap.c +2 -0
+}
+late_initcall(fixmap_remap_ro);

Is mark_rodata_ro() definitely too early to remap these pages RO?

No, we might just call this from there, afaict.

Sounds good, could we then use update_mapping_prot()? I suppose not as that would require mapping bm_pte separately in map_mem()?

- Kevin

#4 lore raw
Quoted 16 lines · Kevin Brodsky
Quoted 7 lines · Ard Biesheuvel
Quoted 7 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 180

Quoted 5 lines

[...]

arch/arm64/mm/fixmap.c +3 -0
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);

Should we not also remap bm_pmd and bm_pud?

Quoted 7 lines
Quoted 4 lines

For that matter, do we need RW access via the linear map for any page annotated with __bss_pgtbl? I suppose that might be the case for kasan_early_shadow_* but I don't know enough about KASAN to tell for sure.

bm_pte[] is special because it is only ever written via the kernel mapping, and never via the linear map. This is why it is being singled out in this patch.

Quoted 8 lines

Right I see __set_fixmap(). This is a very special case of calling __set_pte() on a pointer derived from a global (i.e. pointing to the kernel image and not the LM) so it makes sense to treat it differently.

I do wonder whether there is that much value in protecting bm_pte while all page tables in init_pg_dir (also at a fixed offset in the LM) remain writeable though.

Yes, so those need to be taken care of as well.

Quoted 7 lines

Either way all this is interesting for my series protecting page tables with pkeys [1] - it seems that to protect the fixmap, the easiest option would be to change the pkey of .pgtlb in both the kernel image and LM (the former for bm_pte, and the latter for everything else).

[1] https://lore.kernel.org/all/[email protected]/

Thanks, I've been meaning to go through those but they are a bit overwhelming :-)

Quoted 11 lines
Quoted 11 lines

Whether or not bm_pmd[] can be treated as a special case depends on the page
size: with 4k pages, the whole array covers a virtual region of 1G, which is currently guaranteed to be shared only with the PCI I/O space (but we could move that out). With 16k pages, it covers 64G, and so it is shared with the vmemmap and other virtual mappings in the vmalloc region, and so the current kernel mapping code expects to be able to write those entries.

What we might do is generalize the logic that uses the fixmap to modify pgd level entries in swapper_pg_dir, and use it for all modifications at PMD level or higher if those tables are in .rodata

Quoted 9 lines
Quoted 2 lines

But this is a bit more complicated than this change, so I decided to present this as a separate change.

Definitely, this is orthogonal to this patch. I wonder what the performance impact would be if we used the fixmap for setting all kernel entries at PMD and above. It would be nice to keep that logic easily togglable, because with the kpkeys approach I mentioned above we have a (most likely) much cheaper way of protecting these page tables.

I'm currently experimenting with using __put_kernel_nofault() to update the descriptors at all levels (including PTE) and handling the fault using the fixmap approach. That way, all statically allocated page tables could move to .rodata (except bm_pte[]) and there should be no substantial performance impact.

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 182

Quoted 2 lines
arch/arm64/mm/fixmap.c +2 -0
+}
+late_initcall(fixmap_remap_ro);

Is mark_rodata_ro() definitely too early to remap these pages RO?

No, we might just call this from there, afaict.

Sounds good, could we then use update_mapping_prot()? I suppose not as that would require mapping bm_pte separately in map_mem()?

Yeah, but perhaps we should do the latter anyway, so we can remap the region even if !can_set_direct_map().

#5 lore raw
Quoted 20 lines · Ard Biesheuvel
Quoted 19 lines · Kevin Brodsky
Quoted 7 lines · Ard Biesheuvel
Quoted 7 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 180

Quoted 5 lines

[...]

arch/arm64/mm/fixmap.c +3 -0
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);

Should we not also remap bm_pmd and bm_pud?

Quoted 7 lines
Quoted 4 lines

For that matter, do we need RW access via the linear map for any page annotated with __bss_pgtbl? I suppose that might be the case for kasan_early_shadow_* but I don't know enough about KASAN to tell for sure.

bm_pte[] is special because it is only ever written via the kernel mapping, and never via the linear map. This is why it is being singled out in this patch.

Right I see __set_fixmap(). This is a very special case of calling __set_pte() on a pointer derived from a global (i.e. pointing to the kernel image and not the LM) so it makes sense to treat it differently.

Quoted 5 lines
Quoted 4 lines

I do wonder whether there is that much value in protecting bm_pte while all page tables in init_pg_dir (also at a fixed offset in the LM) remain writeable though.

Yes, so those need to be taken care of as well.

But init_pg_dir needs to remain writeable in the LM, right?

Quoted 8 lines
Quoted 7 lines

Either way all this is interesting for my series protecting page tables with pkeys [1] - it seems that to protect the fixmap, the easiest option would be to change the pkey of .pgtlb in both the kernel image and LM (the former for bm_pte, and the latter for everything else).

[1] https://lore.kernel.org/all/[email protected]/

Thanks, I've been meaning to go through those but they are a bit overwhelming :-)

Yes there's a fair bit to go through, sorry for that... You'll probably be most interested in patch 12, 13 and 19-21 - that's where page tables are being mapped with a special pkey. The cover letter should give enough context for them to make sense on their own.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines

Whether or not bm_pmd[] can be treated as a special case depends on the page
size: with 4k pages, the whole array covers a virtual region of 1G, which is currently guaranteed to be shared only with the PCI I/O space (but we could move that out). With 16k pages, it covers 64G, and so it is shared with the vmemmap and other virtual mappings in the vmalloc region, and so the current kernel mapping code expects to be able to write those entries.

What we might do is generalize the logic that uses the fixmap to modify pgd level entries in swapper_pg_dir, and use it for all modifications at PMD level or higher if those tables are in .rodata

Quoted 8 lines
Quoted 2 lines

But this is a bit more complicated than this change, so I decided to present this as a separate change.

Definitely, this is orthogonal to this patch. I wonder what the performance impact would be if we used the fixmap for setting all kernel entries at PMD and above. It would be nice to keep that logic easily togglable, because with the kpkeys approach I mentioned above we have a (most likely) much cheaper way of protecting these page tables.

Quoted 5 lines

I'm currently experimenting with using __put_kernel_nofault() to update the descriptors at all levels (including PTE) and handling the fault using the fixmap approach. That way, all statically allocated page tables could move to .rodata (except bm_pte[]) and there should be no substantial performance impact.

Doesn't that still mean modifying a fixmap PTE every time we update kernel page tables? That doesn't exactly sound cheap.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 182

Quoted 2 lines
arch/arm64/mm/fixmap.c +2 -0
+}
+late_initcall(fixmap_remap_ro);

Is mark_rodata_ro() definitely too early to remap these pages RO?

No, we might just call this from there, afaict.

Sounds good, could we then use update_mapping_prot()? I suppose not as that would require mapping bm_pte separately in map_mem()?

Yeah, but perhaps we should do the latter anyway, so we can remap the region even if !can_set_direct_map().

Yep makes sense. Not having to add yet another set_direct_map_* function is definitely welcome ;)

- Kevin

#6 lore raw
Quoted 34 lines · Kevin Brodsky
Quoted 20 lines · Ard Biesheuvel
Quoted 19 lines · Kevin Brodsky
Quoted 7 lines · Ard Biesheuvel
Quoted 7 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 180

Quoted 5 lines

[...]

arch/arm64/mm/fixmap.c +3 -0
+static int __init fixmap_remap_ro(void)
+{
+	return set_direct_map_ro((unsigned long)lm_alias(&bm_pte), NR_BM_PTE_TABLES);

Should we not also remap bm_pmd and bm_pud?

Quoted 7 lines
Quoted 4 lines

For that matter, do we need RW access via the linear map for any page annotated with __bss_pgtbl? I suppose that might be the case for kasan_early_shadow_* but I don't know enough about KASAN to tell for sure.

bm_pte[] is special because it is only ever written via the kernel mapping, and never via the linear map. This is why it is being singled out in this patch.

Right I see __set_fixmap(). This is a very special case of calling __set_pte() on a pointer derived from a global (i.e. pointing to the kernel image and not the LM) so it makes sense to treat it differently.

Quoted 5 lines
Quoted 4 lines

I do wonder whether there is that much value in protecting bm_pte while all page tables in init_pg_dir (also at a fixed offset in the LM) remain writeable though.

Yes, so those need to be taken care of as well.

But init_pg_dir needs to remain writeable in the LM, right?

Quoted 5 lines
Quoted 5 lines

Either way all this is interesting for my series protecting page tables with pkeys [1] - it seems that to protect the fixmap, the easiest option would be to change the pkey of .pgtlb in both the kernel image and LM (the former for bm_pte, and the latter for everything else).

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

[1] https://lore.kernel.org/all/[email protected]/

Thanks, I've been meaning to go through those but they are a bit overwhelming :-)

Yes there's a fair bit to go through, sorry for that... You'll probably be most interested in patch 12, 13 and 19-21 - that's where page tables are being mapped with a special pkey. The cover letter should give enough context for them to make sense on their own.

OK, that helps ... :-)

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 7 lines

Whether or not bm_pmd[] can be treated as a special case depends on the page
size: with 4k pages, the whole array covers a virtual region of 1G, which is currently guaranteed to be shared only with the PCI I/O space (but we could move that out). With 16k pages, it covers 64G, and so it is shared with the vmemmap and other virtual mappings in the vmalloc region, and so the current kernel mapping code expects to be able to write those entries.

Quoted 6 lines

What we might do is generalize the logic that uses the fixmap to modify pgd level entries in swapper_pg_dir, and use it for all modifications at PMD level or higher if those tables are in .rodata

But this is a bit more complicated than this change, so I decided to present this as a separate change.

Definitely, this is orthogonal to this patch. I wonder what the performance impact would be if we used the fixmap for setting all kernel entries at PMD and above. It would be nice to keep that logic easily togglable, because with the kpkeys approach I mentioned above we have a (most likely) much cheaper way of protecting these page tables.

Quoted 9 lines
Quoted 5 lines

I'm currently experimenting with using __put_kernel_nofault() to update the descriptors at all levels (including PTE) and handling the fault using the fixmap approach. That way, all statically allocated page tables could move to .rodata (except bm_pte[]) and there should be no substantial performance impact.

Doesn't that still mean modifying a fixmap PTE every time we update kernel page tables? That doesn't exactly sound cheap.

Only the ones that were allocated statically, which cover the region around the kernel image, and the special cases (fixmap, kasan). Everything else is allocated dynamically, using memblock_alloc() if very early during the boot.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

arch/arm64/mm/fixmap.c void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) · line 182

Quoted 2 lines
arch/arm64/mm/fixmap.c +2 -0
+}
+late_initcall(fixmap_remap_ro);

Is mark_rodata_ro() definitely too early to remap these pages RO?

No, we might just call this from there, afaict.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Sounds good, could we then use update_mapping_prot()? I suppose not as that would require mapping bm_pte separately in map_mem()?

Yeah, but perhaps we should do the latter anyway, so we can remap the region even if !can_set_direct_map().

Yep makes sense. Not having to add yet another set_direct_map_* function is definitely welcome ;)

Ack.

#7 lore raw
Quoted 5 lines · Ard Biesheuvel
Quoted 5 lines
Quoted 5 lines

I'm currently experimenting with using __put_kernel_nofault() to update the descriptors at all levels (including PTE) and handling the fault using the fixmap approach. That way, all statically allocated page tables could move to .rodata (except bm_pte[]) and there should be no substantial performance impact.

Quoted 7 lines
Quoted 3 lines

Doesn't that still mean modifying a fixmap PTE every time we update kernel page tables? That doesn't exactly sound cheap.

Only the ones that were allocated statically, which cover the region around the kernel image, and the special cases (fixmap, kasan). Everything else is allocated dynamically, using memblock_alloc() if very early during the boot.

Ah yes I see! Then it does sound very reasonable, those page tables should rarely be written if at all. For kpkeys that would mean we would just need to map bm_pte with the special pkey to complete the protection, the rest being already RO or pkey-protected.

- Kevin