Skip to contentSkip to conversation

INVERTv2 mbox

MODERATED Andi Kleen 1 participant 1 message Aug 7, 2018

Subsystems (3)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

Here's the INVERTv2 patches in mbox format.

-Andi

From 4c5dbc75464405ab865fe7a94eaff41c389e315b Mon Sep 17 00:00:00 2001
From: Andi Kleen <[email protected]>
Date: Mon, 6 Aug 2018 15:53:41 -0700
Subject: [PATCH 1/4] x86/speculation/l1tf: Enable invertion for non PROTNONE
!P maps
Status: O
Content-Length: 858
Lines: 28

For kernel mappings PAGE_PROTNONE is not necessarily set for non present mapping. To enable PTE inversion for those mappings enable it for any non present mapping, not just prot_none.

---
v2: Actually use correct check (Linus)

---
 arch/x86/include/asm/pgtable-invert.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/pgtable-invert.h +1 -1
index 177564187fc0..6ceb0cd968e3 100644
@@ -6,7 +6,7 @@
 
 static inline bool __pte_needs_invert(u64 val)
 {
-	return (val & (_PAGE_PRESENT|_PAGE_PROTNONE)) == _PAGE_PROTNONE;
+	return (val & _PAGE_PRESENT) == 0;
 }
 
 /* Get a mask to xor with the page table entry to get the correct pfn. */
Signature · 30 lines
2.17.1


From 61f33504351e577fab07c5bd0b17e0e703651683 Mon Sep 17 00:00:00 2001
From: Andi Kleen <[email protected]>
Date: Mon, 6 Aug 2018 15:55:01 -0700
Subject: [PATCH 2/4] x86/speculation/l1tf: Make pmd/pud_mknotpresent invert
Status: O
Content-Length: 1974
Lines: 70

Some cases in THP like:
- MADV_FREE
- mprotect
- split
invalidate the PMD for a short time to avoid races.

Make sure the PMD is inverted while this happens too. The
window for an L1TF attack here is very small, but better to close
it.

We fix pmd/pud_mknotpresent to use the right low level functions
to correctly invert.

pud_mknotpresent is not used by anything, but fixed too.

Signed-off-by: Andi Kleen <[email protected]>
---
 arch/x86/include/asm/pgtable.h | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
arch/x86/include/asm/pgtable.h +12 -10
index 26fd42a91946..4e612062bd5a 100644
@@ -410,11 +410,6 @@ static inline pmd_t pmd_mkwrite(pmd_t pmd)
 	return pmd_set_flags(pmd, _PAGE_RW);
 }
 
-static inline pmd_t pmd_mknotpresent(pmd_t pmd)
-{
-	return pmd_clear_flags(pmd, _PAGE_PRESENT | _PAGE_PROTNONE);
-}
-
 static inline pud_t pud_set_flags(pud_t pud, pudval_t set)
 {
 	pudval_t v = native_pud_val(pud);
@@ -469,11 +464,6 @@ static inline pud_t pud_mkwrite(pud_t pud)
 	return pud_set_flags(pud, _PAGE_RW);
 }
 
-static inline pud_t pud_mknotpresent(pud_t pud)
-{
-	return pud_clear_flags(pud, _PAGE_PRESENT | _PAGE_PROTNONE);
-}
-
 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
 static inline int pte_soft_dirty(pte_t pte)
 {
@@ -577,6 +567,18 @@ static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot)
 	return __pud(pfn | check_pgprot(pgprot));
 }
 
+static inline pmd_t pmd_mknotpresent(pmd_t pmd)
+{
+	return pfn_pmd(pmd_pfn(pmd),
+		      __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE)));
+}
+
+static inline pud_t pud_mknotpresent(pud_t pud)
+{
+	return pfn_pud(pud_pfn(pud),
+	      __pgprot(pud_flags(pud) & ~(_PAGE_PRESENT|_PAGE_PROTNONE)));
+}
+
 static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask);
 
 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
Signature · 26 lines
2.17.1


From eacf6f9d9777e8cda01b9ee6b30eec3a8432ebf6 Mon Sep 17 00:00:00 2001
From: Andi Kleen <[email protected]>
Date: Mon, 6 Aug 2018 15:58:13 -0700
Subject: [PATCH 3/4] x86/speculation/l1tf: Make the mmio tracer invert
Status: O
Content-Length: 2008
Lines: 61

The mmio tracer sets io mapping PTEs and PMDs to non present when enabled.
Make it use the right low level macros to actually invert them
to protect against L1TF.

Unfortunately I wasn't able to test this, since the procedure described
in Documentation/trace/mmiotrace.rst doesn't result in any tracing
activity, with or without my patch, when starting X.
Since it's unlikely that mmio tracing is used in any production environment
it might be reasonable to leave it alone. However the patch is simple
enough that it may be still acceptable.

Signed-off-by: Andi Kleen <[email protected]>
---
 arch/x86/mm/kmmio.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
arch/x86/mm/kmmio.c +13 -10
index 7c8686709636..a4c0519f9490 100644
@@ -126,24 +126,27 @@ static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long addr)
 
 static void clear_pmd_presence(pmd_t *pmd, bool clear, pmdval_t *old)
 {
+	pmd_t new_pmd;
 	pmdval_t v = pmd_val(*pmd);
 	if (clear) {
-		*old = v & _PAGE_PRESENT;
-		v &= ~_PAGE_PRESENT;
-	} else	/* presume this has been called with clear==true previously */
-		v |= *old;
-	set_pmd(pmd, __pmd(v));
+		*old = v;
+		new_pmd = pmd_mknotpresent(*pmd);
+	} else { /* presume this has been called with clear==true previously */
+		new_pmd = __pmd(*old);
+	}
+	set_pmd(pmd, new_pmd);
 }
 
 static void clear_pte_presence(pte_t *pte, bool clear, pteval_t *old)
 {
 	pteval_t v = pte_val(*pte);
 	if (clear) {
-		*old = v & _PAGE_PRESENT;
-		v &= ~_PAGE_PRESENT;
-	} else	/* presume this has been called with clear==true previously */
-		v |= *old;
-	set_pte_atomic(pte, __pte(v));
+		*old = v;
+		/* nothing should care about address */
+		pte_clear(&init_mm, 0, pte);
+	} else { /* presume this has been called with clear==true previously */
+		set_pte_atomic(pte, __pte(*old));
+	}
 }
 
 static int clear_page_presence(struct kmmio_fault_page *f, bool clear)
Signature · 24 lines
2.17.1


From 3e624f975b19ae3db42632b974f82bc7a1a526d1 Mon Sep 17 00:00:00 2001
From: Andi Kleen <[email protected]>
Date: Mon, 6 Aug 2018 16:01:53 -0700
Subject: [PATCH 4/4] x86/speculation/l1tf: Make set_memory_np invert
Status: O
Content-Length: 1445
Lines: 42

Make pageattr set_memory_np invert to protect these kernel mappings
against L1TF. This mainly affects DEBUG_PAGEALLOC and other
debugging options.

The fix is to use the standard low level macros to set up the PTEs,
instead of open coding the PTE manipulation.

Passes the CPA self test.

Signed-off-by: Andi Kleen <[email protected]>
---
 arch/x86/mm/pageattr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/mm/pageattr.c +4 -4
index 3bded76e8d5c..7bb6f65c79de 100644
@@ -1014,8 +1014,8 @@ static long populate_pmd(struct cpa_data *cpa,
 
 		pmd = pmd_offset(pud, start);
 
-		set_pmd(pmd, __pmd(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
-				   massage_pgprot(pmd_pgprot)));
+		set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
+					canon_pgprot(pmd_pgprot))));
 
 		start	  += PMD_SIZE;
 		cpa->pfn  += PMD_SIZE >> PAGE_SHIFT;
@@ -1087,8 +1087,8 @@ static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
 	 * Map everything starting from the Gb boundary, possibly with 1G pages
 	 */
 	while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
-		set_pud(pud, __pud(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
-				   massage_pgprot(pud_pgprot)));
+		set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
+				   canon_pgprot(pud_pgprot))));
 
 		start	  += PUD_SIZE;
 		cpa->pfn  += PUD_SIZE >> PAGE_SHIFT;
Signature · 1 line
2.17.1