Skip to content

Commit c55191e

Browse files
Ard Biesheuvelwildea01
authored andcommitted
arm64: mm: apply r/o permissions of VM areas to its linear alias as well
On arm64, we use block mappings and contiguous hints to map the linear region, to minimize the TLB footprint. However, this means that the entire region is mapped using read/write permissions, which we cannot modify at page granularity without having to take intrusive measures to prevent TLB conflicts. This means the linear aliases of pages belonging to read-only mappings (executable or otherwise) in the vmalloc region are also mapped read/write, and could potentially be abused to modify things like module code, bpf JIT code or other read-only data. So let's fix this, by extending the set_memory_ro/rw routines to take the linear alias into account. The consequence of enabling this is that we can no longer use block mappings or contiguous hints, so in cases where the TLB footprint of the linear region is a bottleneck, performance may be affected. Therefore, allow this feature to be runtime en/disabled, by setting rodata=full (or 'on' to disable just this enhancement, or 'off' to disable read-only mappings for code and r/o data entirely) on the kernel command line. Also, allow the default value to be set via a Kconfig option. Tested-by: Laura Abbott <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent b34d2ef commit c55191e

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

arch/arm64/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,20 @@ config ARM64_SSBD
958958

959959
If unsure, say Y.
960960

961+
config RODATA_FULL_DEFAULT_ENABLED
962+
bool "Apply r/o permissions of VM areas also to their linear aliases"
963+
default y
964+
help
965+
Apply read-only attributes of VM areas to the linear alias of
966+
the backing pages as well. This prevents code or read-only data
967+
from being modified (inadvertently or intentionally) via another
968+
mapping of the same memory page. This additional enhancement can
969+
be turned off at runtime by passing rodata=[off|on] (and turned on
970+
with rodata=full if this option is set to 'n')
971+
972+
This requires the linear region to be mapped down to pages,
973+
which may adversely affect performance in some cases.
974+
961975
menuconfig ARMV8_DEPRECATED
962976
bool "Emulate deprecated/obsolete ARMv8 instructions"
963977
depends on COMPAT

arch/arm64/include/asm/mmu_context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <asm/sysreg.h>
3636
#include <asm/tlbflush.h>
3737

38+
extern bool rodata_full;
39+
3840
static inline void contextidr_thread_switch(struct task_struct *next)
3941
{
4042
if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))

arch/arm64/mm/mmu.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ static void __init map_mem(pgd_t *pgdp)
451451
struct memblock_region *reg;
452452
int flags = 0;
453453

454-
if (debug_pagealloc_enabled())
454+
if (rodata_full || debug_pagealloc_enabled())
455455
flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
456456

457457
/*
@@ -552,7 +552,19 @@ static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
552552

553553
static int __init parse_rodata(char *arg)
554554
{
555-
return strtobool(arg, &rodata_enabled);
555+
int ret = strtobool(arg, &rodata_enabled);
556+
if (!ret) {
557+
rodata_full = false;
558+
return 0;
559+
}
560+
561+
/* permit 'full' in addition to boolean options */
562+
if (strcmp(arg, "full"))
563+
return -EINVAL;
564+
565+
rodata_enabled = true;
566+
rodata_full = true;
567+
return 0;
556568
}
557569
early_param("rodata", parse_rodata);
558570

arch/arm64/mm/pageattr.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct page_change_data {
2525
pgprot_t clear_mask;
2626
};
2727

28+
bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED);
29+
2830
static int change_page_range(pte_t *ptep, pgtable_t token, unsigned long addr,
2931
void *data)
3032
{
@@ -64,6 +66,7 @@ static int change_memory_common(unsigned long addr, int numpages,
6466
unsigned long size = PAGE_SIZE*numpages;
6567
unsigned long end = start + size;
6668
struct vm_struct *area;
69+
int i;
6770

6871
if (!PAGE_ALIGNED(addr)) {
6972
start &= PAGE_MASK;
@@ -93,6 +96,18 @@ static int change_memory_common(unsigned long addr, int numpages,
9396
if (!numpages)
9497
return 0;
9598

99+
/*
100+
* If we are manipulating read-only permissions, apply the same
101+
* change to the linear mapping of the pages that back this VM area.
102+
*/
103+
if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY ||
104+
pgprot_val(clear_mask) == PTE_RDONLY)) {
105+
for (i = 0; i < area->nr_pages; i++) {
106+
__change_memory_common((u64)page_address(area->pages[i]),
107+
PAGE_SIZE, set_mask, clear_mask);
108+
}
109+
}
110+
96111
/*
97112
* Get rid of potentially aliasing lazily unmapped vm areas that may
98113
* have permissions set that deviate from the ones we are setting here.

0 commit comments

Comments
 (0)