Skip to content

Commit 63e0b6a

Browse files
ardbiesheuvelwilldeacon
authored andcommitted
arm64: mm: Unmap kernel data/bss entirely from the linear map
The linear aliases of the kernel text and rodata are also mapped read-only in the linear map. Given that the contents of these regions are mostly identical to the version in the loadable image, mapping them read-only and leaving their contents visible is a reasonable hardening measure. Data and bss, however, are now also mapped read-only but the contents of these regions are more likely to contain data that we'd rather not leak. So let's unmap these entirely in the linear map when the kernel is running normally. When going into hibernation or waking up from it, these regions need to be mapped, so map the region initially, and toggle the valid bit so map/unmap the region as needed. Doing so is required because pages covering the kernel image are marked as PageReserved, and therefore disregarded for snapshotting by the hibernate logic unless they are mapped. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Kevin Brodsky <kevin.brodsky@arm.com> Signed-off-by: Will Deacon <will@kernel.org>
1 parent f2ba877 commit 63e0b6a

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

arch/arm64/mm/mmu.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/mm.h>
2525
#include <linux/vmalloc.h>
2626
#include <linux/set_memory.h>
27+
#include <linux/suspend.h>
2728
#include <linux/kfence.h>
2829
#include <linux/pkeys.h>
2930
#include <linux/mm_inline.h>
@@ -1056,6 +1057,29 @@ static void __init __map_memblock(phys_addr_t start, phys_addr_t end,
10561057
end - start, prot, early_pgtable_alloc, flags);
10571058
}
10581059

1060+
static void mark_linear_data_alias_valid(bool valid)
1061+
{
1062+
set_memory_valid((unsigned long)lm_alias(__init_end),
1063+
(unsigned long)(__bss_stop - __init_end) / PAGE_SIZE,
1064+
valid);
1065+
}
1066+
1067+
static int arm64_hibernate_pm_notify(struct notifier_block *nb,
1068+
unsigned long mode, void *unused)
1069+
{
1070+
switch (mode) {
1071+
default:
1072+
break;
1073+
case PM_POST_HIBERNATION:
1074+
mark_linear_data_alias_valid(false);
1075+
break;
1076+
case PM_HIBERNATION_PREPARE:
1077+
mark_linear_data_alias_valid(true);
1078+
break;
1079+
}
1080+
return 0;
1081+
}
1082+
10591083
void __init mark_linear_text_alias_ro(void)
10601084
{
10611085
/*
@@ -1064,6 +1088,21 @@ void __init mark_linear_text_alias_ro(void)
10641088
update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
10651089
(unsigned long)__init_begin - (unsigned long)_text,
10661090
PAGE_KERNEL_RO);
1091+
1092+
/*
1093+
* Register a PM notifier to remap the linear alias of data/bss as
1094+
* valid read-only before hibernation. This is needed because the
1095+
* snapshot logic disregards PageReserved pages (such as the ones
1096+
* covering the kernel image) unless they are mapped in the linear
1097+
* map.
1098+
*/
1099+
if (IS_ENABLED(CONFIG_HIBERNATION)) {
1100+
static struct notifier_block nb = {
1101+
.notifier_call = arm64_hibernate_pm_notify
1102+
};
1103+
1104+
register_pm_notifier(&nb);
1105+
}
10671106
}
10681107

10691108
#ifdef CONFIG_KFENCE
@@ -1193,10 +1232,8 @@ static void __init map_mem(void)
11931232
flags);
11941233
}
11951234

1196-
/* Map the kernel data/bss read-only in the linear map */
1197-
__map_memblock(init_end, kernel_end, PAGE_KERNEL_RO, flags);
1198-
flush_tlb_kernel_range((unsigned long)lm_alias(__init_end),
1199-
(unsigned long)lm_alias(__bss_stop));
1235+
/* Map the kernel data/bss as invalid in the linear map */
1236+
mark_linear_data_alias_valid(false);
12001237
}
12011238

12021239
void mark_rodata_ro(void)

0 commit comments

Comments
 (0)