Skip to contentSkip to conversation

x86: Look up the PTE rather than assuming it

PATCH Superseded Aaron Lewis 3 participants 5 messages Oct 29, 2021 – Nov 1, 2021 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Look up the PTE rather than assuming it

Rather than assuming which PTE the SMEP test is running on, look it up to ensure we have the correct entry. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTE would be set. Switch to looking up the PTE to avoid this from happening.

---
 x86/access.c   | 9 ++++++---
 x86/cstart64.S | 1 -
 2 files changed, 6 insertions(+), 4 deletions(-)
x86/access.c +6 -3
index 4725bbd..a4d72d9 100644
@@ -204,7 +204,7 @@ static void set_cr0_wp(int wp)
 static unsigned set_cr4_smep(int smep)
 {
     unsigned long cr4 = shadow_cr4;
-    extern u64 ptl2[];
+    pteval_t *pte;
     unsigned r;
 
     cr4 &= ~CR4_SMEP_MASK;
@@ -213,11 +213,14 @@ static unsigned set_cr4_smep(int smep)
     if (cr4 == shadow_cr4)
         return 0;
 
+    pte = get_pte(phys_to_virt(read_cr3()), set_cr4_smep);
+    assert(pte);
+
     if (smep)
-        ptl2[2] &= ~PT_USER_MASK;
+        *pte &= ~PT_USER_MASK;
     r = write_cr4_checking(cr4);
     if (r || !smep) {
-        ptl2[2] |= PT_USER_MASK;
+        *pte |= PT_USER_MASK;
 
 	/* Flush to avoid spurious #PF */
 	invlpg((void *)(2 << 21));
x86/cstart64.S +0 -1
index 5c6ad38..4ba9943 100644
@@ -26,7 +26,6 @@ ring0stacktop:
 .data
 
 .align 4096
-.globl ptl2
 ptl2:
 i = 0
 	.rept 512 * 4
Signature · 1 line
2.33.1.1089.g2158813163f-goog
#1 lore raw
Quoted 25 lines · Aaron Lewis

Rather than assuming which PTE the SMEP test is running on, look it up to ensure we have the correct entry. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTE would be set. Switch to looking up the PTE to avoid this from happening.

---
 x86/access.c   | 9 ++++++---
 x86/cstart64.S | 1 -
 2 files changed, 6 insertions(+), 4 deletions(-)
x86/access.c +1 -1
index 4725bbd..a4d72d9 100644
@@ -204,7 +204,7 @@ static void set_cr0_wp(int wp)
 static unsigned set_cr4_smep(int smep)
 {
     unsigned long cr4 = shadow_cr4;
-    extern u64 ptl2[];
+    pteval_t *pte;
     unsigned r;
 
     cr4 &= ~CR4_SMEP_MASK;

x86/access.c static unsigned set_cr4_smep(int smep) · line 216

Quoted 4 lines
x86/access.c +1 -0
@@ -213,11 +213,14 @@ static unsigned set_cr4_smep(int smep)
     if (cr4 == shadow_cr4)
         return 0;
 
+    pte = get_pte(phys_to_virt(read_cr3()), set_cr4_smep);

What guarantees are there that set_cr4_smep() and the rest of the test are mapped by the same PTE? I 100% agree the current code is ugly, e.g. I can't remember how ptl2[2] is guaranteed to be used, but if we're going to fix it then we should aim for a more robust solution.

x86/access.c static unsigned set_cr4_smep(int smep) · line 226

Quoted 12 lines
x86/access.c +4 -2
+    assert(pte);
+
     if (smep)
-        ptl2[2] &= ~PT_USER_MASK;
+        *pte &= ~PT_USER_MASK;
     r = write_cr4_checking(cr4);
     if (r || !smep) {
-        ptl2[2] |= PT_USER_MASK;
+        *pte |= PT_USER_MASK;
 
 	/* Flush to avoid spurious #PF */
 	invlpg((void *)(2 << 21));

This invlpg() should be updated as well.

Quoted 15 lines
x86/cstart64.S +0 -1
index 5c6ad38..4ba9943 100644
@@ -26,7 +26,6 @@ ring0stacktop:
 .data
 
 .align 4096
-.globl ptl2
 ptl2:
 i = 0
 	.rept 512 * 4
Signature · 1 line
2.33.1.1089.g2158813163f-goog
#2 lore raw
Quoted 30 lines · Sean Christopherson
Quoted 28 lines · Aaron Lewis

Rather than assuming which PTE the SMEP test is running on, look it up to ensure we have the correct entry. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTE would be set. Switch to looking up the PTE to avoid this from happening.

---
 x86/access.c   | 9 ++++++---
 x86/cstart64.S | 1 -
 2 files changed, 6 insertions(+), 4 deletions(-)
x86/access.c +1 -1
index 4725bbd..a4d72d9 100644
@@ -204,7 +204,7 @@ static void set_cr0_wp(int wp)
 static unsigned set_cr4_smep(int smep)
 {
     unsigned long cr4 = shadow_cr4;
-    extern u64 ptl2[];
+    pteval_t *pte;
     unsigned r;
 
     cr4 &= ~CR4_SMEP_MASK;
@@ -213,11 +213,14 @@ static unsigned set_cr4_smep(int smep)
     if (cr4 == shadow_cr4)
         return 0;
 
Quoted 6 lines

x86/access.c static unsigned set_cr4_smep(int smep) · line 216

Quoted 1 line
x86/access.c +1 -0
+    pte = get_pte(phys_to_virt(read_cr3()), set_cr4_smep);

What guarantees are there that set_cr4_smep() and the rest of the test are mapped by the same PTE? I 100% agree the current code is ugly, e.g. I can't remember how ptl2[2] is guaranteed to be used, but if we're going to fix it then we should aim for a more robust solution.

One possible solution is to put labels around the code that has to run with SMEP and then to implement a function to modify the PTEs for a range of addresses, using gcc's double-ampersand syntax to get the addresses of the labels.

Quoted 30 lines

x86/access.c static unsigned set_cr4_smep(int smep) · line 226

Quoted 12 lines
x86/access.c +4 -2
+    assert(pte);
+
     if (smep)
-        ptl2[2] &= ~PT_USER_MASK;
+        *pte &= ~PT_USER_MASK;
     r = write_cr4_checking(cr4);
     if (r || !smep) {
-        ptl2[2] |= PT_USER_MASK;
+        *pte |= PT_USER_MASK;
 
      /* Flush to avoid spurious #PF */
      invlpg((void *)(2 << 21));

This invlpg() should be updated as well.

Quoted 15 lines
x86/cstart64.S +0 -2
index 5c6ad38..4ba9943 100644
@@ -26,7 +26,6 @@ ring0stacktop:
 .data
 
 .align 4096
-.globl ptl2
 ptl2:
 i = 0
      .rept 512 * 4
--

2.33.1.1089.g2158813163f-goog

#3 lore raw
Quoted 39 lines · Jim Mattson
Quoted 30 lines · Sean Christopherson
Quoted 28 lines · Aaron Lewis

Rather than assuming which PTE the SMEP test is running on, look it up to ensure we have the correct entry. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTE would be set. Switch to looking up the PTE to avoid this from happening.

---
 x86/access.c   | 9 ++++++---
 x86/cstart64.S | 1 -
 2 files changed, 6 insertions(+), 4 deletions(-)
x86/access.c +1 -1
index 4725bbd..a4d72d9 100644
@@ -204,7 +204,7 @@ static void set_cr0_wp(int wp)
 static unsigned set_cr4_smep(int smep)
 {
     unsigned long cr4 = shadow_cr4;
-    extern u64 ptl2[];
+    pteval_t *pte;
     unsigned r;
 
     cr4 &= ~CR4_SMEP_MASK;
@@ -213,11 +213,14 @@ static unsigned set_cr4_smep(int smep)
     if (cr4 == shadow_cr4)
         return 0;
 
Quoted 6 lines

x86/access.c static unsigned set_cr4_smep(int smep) · line 216

Quoted 1 line
x86/access.c +1 -0
+    pte = get_pte(phys_to_virt(read_cr3()), set_cr4_smep);

What guarantees are there that set_cr4_smep() and the rest of the test are mapped by the same PTE? I 100% agree the current code is ugly, e.g. I can't remember how ptl2[2] is guaranteed to be used, but if we're going to fix it then we should aim for a more robust solution.

Quoted 5 lines

One possible solution is to put labels around the code that has to run with SMEP and then to implement a function to modify the PTEs for a range of addresses, using gcc's double-ampersand syntax to get the addresses of the labels.

Would it be safer to just mark the start and end of the text section, then I could remove the PT_USER_MASK flags from that whole range?

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
x86/access.c +4 -2
+    assert(pte);
+
     if (smep)
-        ptl2[2] &= ~PT_USER_MASK;
+        *pte &= ~PT_USER_MASK;
     r = write_cr4_checking(cr4);
     if (r || !smep) {
-        ptl2[2] |= PT_USER_MASK;
+        *pte |= PT_USER_MASK;
 
Quoted 4 lines
Quoted 4 lines

x86/access.c static unsigned set_cr4_smep(int smep) · line 226

Quoted 2 lines
x86/access.c +0 -0
      /* Flush to avoid spurious #PF */
      invlpg((void *)(2 << 21));

This invlpg() should be updated as well.

Good catch. I'll update this.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
x86/cstart64.S +0 -2
index 5c6ad38..4ba9943 100644
@@ -26,7 +26,6 @@ ring0stacktop:
 .data
 
 .align 4096
-.globl ptl2
 ptl2:
 i = 0
      .rept 512 * 4
--

2.33.1.1089.g2158813163f-goog

#4 lore raw
Quoted 41 lines · Aaron Lewis
Quoted 39 lines · Jim Mattson
Quoted 36 lines · Sean Christopherson
Quoted 25 lines · Aaron Lewis

Rather than assuming which PTE the SMEP test is running on, look it up to ensure we have the correct entry. If this test were to run on a different page table (ie: run in an L2 test) the wrong PTE would be set. Switch to looking up the PTE to avoid this from happening.

---
 x86/access.c   | 9 ++++++---
 x86/cstart64.S | 1 -
 2 files changed, 6 insertions(+), 4 deletions(-)
x86/access.c +1 -1
index 4725bbd..a4d72d9 100644
@@ -204,7 +204,7 @@ static void set_cr0_wp(int wp)
 static unsigned set_cr4_smep(int smep)
 {
     unsigned long cr4 = shadow_cr4;
-    extern u64 ptl2[];
+    pteval_t *pte;
     unsigned r;
 
     cr4 &= ~CR4_SMEP_MASK;

x86/access.c static unsigned set_cr4_smep(int smep) · line 216

Quoted 4 lines
x86/access.c +1 -0
@@ -213,11 +213,14 @@ static unsigned set_cr4_smep(int smep)
     if (cr4 == shadow_cr4)
         return 0;
 
+    pte = get_pte(phys_to_virt(read_cr3()), set_cr4_smep);

What guarantees are there that set_cr4_smep() and the rest of the test are mapped by the same PTE? I 100% agree the current code is ugly, e.g. I can't remember how ptl2[2] is guaranteed to be used, but if we're going to fix it then we should aim for a more robust solution.

Quoted 8 lines
Quoted 5 lines

One possible solution is to put labels around the code that has to run with SMEP and then to implement a function to modify the PTEs for a range of addresses, using gcc's double-ampersand syntax to get the addresses of the labels.

Would it be safer to just mark the start and end of the text section, then I could remove the PT_USER_MASK flags from that whole range?

That sounds ideal. There's already an stext, so you would just have to add etext.

Quoted 33 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
x86/access.c +4 -2
+    assert(pte);
+
     if (smep)
-        ptl2[2] &= ~PT_USER_MASK;
+        *pte &= ~PT_USER_MASK;
     r = write_cr4_checking(cr4);
     if (r || !smep) {
-        ptl2[2] |= PT_USER_MASK;
+        *pte |= PT_USER_MASK;
 
Quoted 4 lines
Quoted 4 lines

x86/access.c static unsigned set_cr4_smep(int smep) · line 226

Quoted 2 lines
x86/access.c +0 -0
      /* Flush to avoid spurious #PF */
      invlpg((void *)(2 << 21));

This invlpg() should be updated as well.

Good catch. I'll update this.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
x86/cstart64.S +0 -2
index 5c6ad38..4ba9943 100644
@@ -26,7 +26,6 @@ ring0stacktop:
 .data
 
 .align 4096
-.globl ptl2
 ptl2:
 i = 0
      .rept 512 * 4
--

2.33.1.1089.g2158813163f-goog