Test for CR0.WP=0/1 r/o write access
cover Test for CR0.WP=0/1 r/o write access
This mini-series adds an explicit test that verifies a page fault will occur for attempts to write to an r/o page while CR0.WP=1.
There are existing tests already, e.g. in pks.c, pku.c, smap.c or even access.c that implicitly test it as well. However, they all either explicitly (via INVLPG) or implicitly (via CR3 reload) flush the TLB before such an access which might lead to false positives if the access succeeded before, e.g. because CR0.WP was 0 before.
Better to have an explicit test, especially to back up the changes of [1].
Please apply!
Thanks,
Mathias
[1] https://lore.kernel.org/kvm/[email protected]/
Mathias Krause (2):
x86: Use existing CR0.WP / CR4.SMEP bit definitions x86/access: CR0.WP toggling write access test
x86/access.c | 57 +++++++++++++++++++++++++++++++++++++++++++--------- x86/pks.c | 5 ++--- x86/pku.c | 5 ++--- 3 files changed, 52 insertions(+), 15 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/2 x86: Use existing CR0.WP / CR4.SMEP bit definitions
Use the existing bit definitions from x86/processor.h instead of defining our own.
--- x86/access.c | 11 ++++------- x86/pks.c | 5 ++--- x86/pku.c | 5 ++--- 3 files changed, 8 insertions(+), 13 deletions(-)
x86/access.c +4 -7
@@ -20,9 +20,6 @@ static int invalid_mask;#define PT_BASE_ADDR_MASK ((pt_element_t)((((pt_element_t)1 << 36) - 1) & PAGE_MASK))#define PT_PSE_BASE_ADDR_MASK (PT_BASE_ADDR_MASK & ~(1ull << 21)) -#define CR0_WP_MASK (1UL << 16) -#define CR4_SMEP_MASK (1UL << 20) -#define PFERR_PRESENT_MASK (1U << 0)#define PFERR_WRITE_MASK (1U << 1)#define PFERR_USER_MASK (1U << 2)
@@ -239,9 +236,9 @@ static void set_cr0_wp(int wp) {unsigned long cr0 = shadow_cr0; - cr0 &= ~CR0_WP_MASK ; + cr0 &= ~X86_CR0_WP ;if (wp) - cr0 |=CR0_WP_MASK ; + cr0 |=X86_CR0_WP ;if (cr0 != shadow_cr0) {write_cr0 (cr0); shadow_cr0 = cr0;
@@ -272,9 +269,9 @@ static unsigned set_cr4_smep(ac_test_t *at, int smep)unsigned long cr4 = shadow_cr4;unsigned r; - cr4 &= ~CR4_SMEP_MASK ; + cr4 &= ~X86_CR4_SMEP ;if (smep) - cr4 |=CR4_SMEP_MASK ; + cr4 |=X86_CR4_SMEP ;if (cr4 == shadow_cr4)return 0 ;
x86/pks.c +2 -3
@@ -5,7 +5,6 @@#include "x86/vm.h" #include "x86/msr.h" -#define CR0_WP_MASK (1UL << 16)#define PTE_PKEY_BIT 59#define SUPER_BASE (1 << 23)#define SUPER_VAR (v) (*((__typeof__(&(v))) (((unsigned long)&v) + SUPER_BASE)))
@@ -18,9 +17,9 @@ static void set_cr0_wp(int wp) {unsigned long cr0 =read_cr0 (); - cr0 &= ~CR0_WP_MASK ; + cr0 &= ~X86_CR0_WP ;if (wp) - cr0 |=CR0_WP_MASK ; + cr0 |=X86_CR0_WP ;write_cr0 (cr0); }
x86/pku.c +2 -3
@@ -5,7 +5,6 @@#include "x86/vm.h" #include "x86/msr.h" -#define CR0_WP_MASK (1UL << 16)#define PTE_PKEY_BIT 59#define USER_BASE (1 << 23)#define USER_VAR (v) (*((__typeof__(&(v))) (((unsigned long)&v) + USER_BASE)))
@@ -18,9 +17,9 @@ static void set_cr0_wp(int wp) {unsigned long cr0 =read_cr0 (); - cr0 &= ~CR0_WP_MASK ; + cr0 &= ~X86_CR0_WP ;if (wp) - cr0 |=CR0_WP_MASK ; + cr0 |=X86_CR0_WP ;write_cr0 (cr0); }
Signature · 1 line
2.39.2
PATCH 2/2 x86/access: CR0.WP toggling write access test
In reply to Mathias KrauseWe already have tests that verify a write access to an r/o page is successful when CR0.WP=0, but we lack a test that explicitly verifies that the same access will fail after we set CR0.WP=1 without flushing any associated TLB entries either explicitly (INVLPG) or implicitly (write to CR3). Add such a test.
--- x86/access.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-)
x86/access.c +44 -2
@@ -575,9 +575,10 @@ fault: at->expected_error &= ~PFERR_FETCH_MASK ; } -static void ac_set_expected_status (ac_test_t *at) +static void __ac_set_expected_status (ac_test_t *at,bool flush) { -invlpg (at->virt); +if (flush) +invlpg (at->virt);if (at->ptep) at->expected_pte = *at->ptep;
@@ -599,6 +600,11 @@ static void ac_set_expected_status(ac_test_t *at)ac_emulate_access (at, at->flags); } +static void ac_set_expected_status (ac_test_t *at) +{ +__ac_set_expected_status (at, true); +} +static pt_element_t ac_get_pt (ac_test_t *at,int i,pt_element_t *ptep) {pt_element_t pte;
@@ -1061,6 +1067,41 @@ err:return 0 ; } +static int check_write_cr0wp (ac_pt_env_t *pt_env) +{ +ac_test_t at1; + +ac_test_init (&at1,0xffff923042007000ul , pt_env); + + at1.flags =AC_PDE_PRESENT_MASK |AC_PTE_PRESENT_MASK | +AC_PDE_ACCESSED_MASK |AC_PTE_ACCESSED_MASK | +AC_CPU_CR0_WP_MASK | +AC_ACCESS_WRITE_MASK ; +ac_test_setup_ptes (&at1); + +/* +* Write to r/o page with cr0.wp=0, then try again +* with cr0.wp=1 and expect a page fault to happen. +*/ +if (!ac_test_do_access (&at1)) { +printf ("%s: CR0.WP=0 r/o write fail\n" , __FUNCTION__); + goto err; + } + + at1.flags &= ~AC_CPU_CR0_WP_MASK ; +__ac_set_expected_status (&at1, false); + +if (!ac_test_do_access (&at1)) { +printf ("%s: CR0.WP=1 r/o write deny fail\n" , __FUNCTION__); + goto err; + } + +return 1 ; + +err: +return 0 ; +} +static int check_effective_sp_permissions (ac_pt_env_t *pt_env) {unsigned long ptr1 =0xffff923480000000 ;
@@ -1150,6 +1191,7 @@ const ac_test_fn ac_test_cases[] = check_pfec_on_prefetch_pte, check_large_pte_dirty_for_nowp, check_smep_andnot_wp, + check_write_cr0wp, check_effective_sp_permissions, };
Signature · 1 line
2.39.2
Re: [kvm-unit-tests PATCH 2/2] x86/access: CR0.WP toggling write access test
Quoted 5 lines · Mathias Krause
We already have tests that verify a write access to an r/o page is successful when CR0.WP=0, but we lack a test that explicitly verifies that the same access will fail after we set CR0.WP=1 without flushing any associated TLB entries either explicitly (INVLPG) or implicitly (write to CR3). Add such a test.
This test is insufficient to test all corner cases, as noted in [1]. Especially the emulator needs to be tested as well.
I'll send a v2 soon!
Mathias
Re: [kvm-unit-tests PATCH 2/2] x86/access: CR0.WP toggling write access test
Quoted 2 lines · Mathias Krause
This test is insufficient to test all corner cases, as noted in [1]. [...]
Forgot the link: