[RFC PATCH v2 15/19] heki: x86: Initialize permissions counters for pages in vmap()/vunmap()
Mickaël Salaün
mic at digikod.net
Mon Nov 13 02:23:22 UTC 2023
From: Madhavan T. Venkataraman <madvenka at linux.microsoft.com>
When a page gets mapped, create permissions counters for it and
initialize them based on the specified permissions.
When a page gets unmapped, update the counters appropriately.
Cc: Borislav Petkov <bp at alien8.de>
Cc: Dave Hansen <dave.hansen at linux.intel.com>
Cc: H. Peter Anvin <hpa at zytor.com>
Cc: Ingo Molnar <mingo at redhat.com>
Cc: Kees Cook <keescook at chromium.org>
Cc: Mickaël Salaün <mic at digikod.net>
Cc: Paolo Bonzini <pbonzini at redhat.com>
Cc: Sean Christopherson <seanjc at google.com>
Cc: Thomas Gleixner <tglx at linutronix.de>
Cc: Vitaly Kuznetsov <vkuznets at redhat.com>
Cc: Wanpeng Li <wanpengli at tencent.com>
Signed-off-by: Madhavan T. Venkataraman <madvenka at linux.microsoft.com>
---
Changes since v1:
* New patch
---
include/linux/heki.h | 11 ++++++++++-
mm/vmalloc.c | 7 +++++++
virt/heki/counters.c | 20 ++++++++++++++++++++
3 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/include/linux/heki.h b/include/linux/heki.h
index 86c787d121e0..d660994d34d0 100644
--- a/include/linux/heki.h
+++ b/include/linux/heki.h
@@ -68,7 +68,11 @@ struct heki_hypervisor {
* pointer into this heki structure.
*
* During guest kernel boot, permissions counters for each guest page are
- * initialized based on the page's current permissions.
+ * initialized based on the page's current permissions. Beyond this point,
+ * the counters are updated whenever:
+ *
+ * - a page is mapped into the kernel address space
+ * - a page is unmapped from the kernel address space
*/
struct heki {
struct heki_hypervisor *hypervisor;
@@ -77,6 +81,7 @@ struct heki {
enum heki_cmd {
HEKI_MAP,
+ HEKI_UNMAP,
};
/*
@@ -109,6 +114,7 @@ void heki_counters_init(void);
void heki_walk(unsigned long va, unsigned long va_end, heki_func_t func,
struct heki_args *args);
void heki_map(unsigned long va, unsigned long end);
+void heki_unmap(unsigned long va, unsigned long end);
/* Arch-specific functions. */
void heki_arch_early_init(void);
@@ -125,6 +131,9 @@ static inline void heki_late_init(void)
static inline void heki_map(unsigned long va, unsigned long end)
{
}
+static inline void heki_unmap(unsigned long va, unsigned long end)
+{
+}
#endif /* CONFIG_HEKI */
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index a3fedb3ee0db..d9096502e571 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -40,6 +40,7 @@
#include <linux/pgtable.h>
#include <linux/hugetlb.h>
#include <linux/sched/mm.h>
+#include <linux/heki.h>
#include <asm/tlbflush.h>
#include <asm/shmparam.h>
@@ -301,6 +302,8 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
arch_sync_kernel_mappings(start, end);
+ heki_map(start, end);
+
return err;
}
@@ -419,6 +422,8 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end)
pgtbl_mod_mask mask = 0;
BUG_ON(addr >= end);
+ heki_unmap(start, end);
+
pgd = pgd_offset_k(addr);
do {
next = pgd_addr_end(addr, end);
@@ -564,6 +569,8 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
arch_sync_kernel_mappings(start, end);
+ heki_map(start, end);
+
return 0;
}
diff --git a/virt/heki/counters.c b/virt/heki/counters.c
index 7067449cabca..adc8d566b8a9 100644
--- a/virt/heki/counters.c
+++ b/virt/heki/counters.c
@@ -88,6 +88,13 @@ void heki_callback(struct heki_args *args)
heki_update_counters(counters, 0, permissions, 0);
break;
+ case HEKI_UNMAP:
+ if (WARN_ON_ONCE(!counters))
+ break;
+ heki_update_counters(counters, permissions, 0,
+ permissions);
+ break;
+
default:
WARN_ON_ONCE(1);
break;
@@ -124,6 +131,19 @@ void heki_map(unsigned long va, unsigned long end)
heki_func(va, end, &args);
}
+/*
+ * Find the mappings in the given range and revert the permission counters for
+ * them.
+ */
+void heki_unmap(unsigned long va, unsigned long end)
+{
+ struct heki_args args = {
+ .cmd = HEKI_UNMAP,
+ };
+
+ heki_func(va, end, &args);
+}
+
/*
* Permissions counters are associated with each guest page using the
* Memory Table feature. Initialize the permissions counters here.
--
2.42.1
More information about the Linux-security-module-archive
mailing list