Skip to content

Commit fca4d41

Browse files
committed
x86/mm: Introduce _PAGE_SAVED_DIRTY
Some OSes have a greater dependence on software available bits in PTEs than Linux. That left the hardware architects looking for a way to represent a new memory type (shadow stack) within the existing bits. They chose to repurpose a lightly-used state: Write=0,Dirty=1. So in order to support shadow stack memory, Linux should avoid creating memory with this PTE bit combination unless it intends for it to be shadow stack. The reason it's lightly used is that Dirty=1 is normally set by HW _before_ a write. A write with a Write=0 PTE would typically only generate a fault, not set Dirty=1. Hardware can (rarely) both set Dirty=1 *and* generate the fault, resulting in a Write=0,Dirty=1 PTE. Hardware which supports shadow stacks will no longer exhibit this oddity. So that leaves Write=0,Dirty=1 PTEs created in software. To avoid inadvertently created shadow stack memory, in places where Linux normally creates Write=0,Dirty=1, it can use the software-defined _PAGE_SAVED_DIRTY in place of the hardware _PAGE_DIRTY. In other words, whenever Linux needs to create Write=0,Dirty=1, it instead creates Write=0,SavedDirty=1 except for shadow stack, which is Write=0,Dirty=1. There are six bits left available to software in the 64-bit PTE after consuming a bit for _PAGE_SAVED_DIRTY. For 32 bit, the same bit as _PAGE_BIT_UFFD_WP is used, since user fault fd is not supported on 32 bit. This leaves one unused software bit on 32 bit (_PAGE_BIT_SOFT_DIRTY, as this is also not supported on 32 bit). Implement only the infrastructure for _PAGE_SAVED_DIRTY. Changes to actually begin creating _PAGE_SAVED_DIRTY PTEs will follow once other pieces are in place. Since this SavedDirty shifting is done for all x86 CPUs, this leaves the possibility for the hardware oddity to still create Write=0,Dirty=1 PTEs in rare cases. Since these CPUs also don't support shadow stack, this will be harmless as it was before the introduction of SavedDirty. Implement the shifting logic to be branchless. Embed the logic of whether to do the shifting (including checking the Write bits) so that it can be called by future callers that would otherwise need additional branching logic. This efficiency allows the logic of when to do the shifting to be centralized, making the code easier to reason about. Co-developed-by: Yu-cheng Yu <[email protected]> Signed-off-by: Yu-cheng Yu <[email protected]> Signed-off-by: Rick Edgecombe <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Tested-by: Pengfei Xu <[email protected]> Tested-by: John Allen <[email protected]> Tested-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/all/20230613001108.3040476-11-rick.p.edgecombe%40intel.com
1 parent a956efc commit fca4d41

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

arch/x86/include/asm/pgtable.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,53 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
302302
return native_make_pte(v & ~clear);
303303
}
304304

305+
/*
306+
* Write protection operations can result in Dirty=1,Write=0 PTEs. But in the
307+
* case of X86_FEATURE_USER_SHSTK, these PTEs denote shadow stack memory. So
308+
* when creating dirty, write-protected memory, a software bit is used:
309+
* _PAGE_BIT_SAVED_DIRTY. The following functions take a PTE and transition the
310+
* Dirty bit to SavedDirty, and vice-vesra.
311+
*
312+
* This shifting is only done if needed. In the case of shifting
313+
* Dirty->SavedDirty, the condition is if the PTE is Write=0. In the case of
314+
* shifting SavedDirty->Dirty, the condition is Write=1.
315+
*/
316+
static inline pgprotval_t mksaveddirty_shift(pgprotval_t v)
317+
{
318+
pgprotval_t cond = (~v >> _PAGE_BIT_RW) & 1;
319+
320+
v |= ((v >> _PAGE_BIT_DIRTY) & cond) << _PAGE_BIT_SAVED_DIRTY;
321+
v &= ~(cond << _PAGE_BIT_DIRTY);
322+
323+
return v;
324+
}
325+
326+
static inline pgprotval_t clear_saveddirty_shift(pgprotval_t v)
327+
{
328+
pgprotval_t cond = (v >> _PAGE_BIT_RW) & 1;
329+
330+
v |= ((v >> _PAGE_BIT_SAVED_DIRTY) & cond) << _PAGE_BIT_DIRTY;
331+
v &= ~(cond << _PAGE_BIT_SAVED_DIRTY);
332+
333+
return v;
334+
}
335+
336+
static inline pte_t pte_mksaveddirty(pte_t pte)
337+
{
338+
pteval_t v = native_pte_val(pte);
339+
340+
v = mksaveddirty_shift(v);
341+
return native_make_pte(v);
342+
}
343+
344+
static inline pte_t pte_clear_saveddirty(pte_t pte)
345+
{
346+
pteval_t v = native_pte_val(pte);
347+
348+
v = clear_saveddirty_shift(v);
349+
return native_make_pte(v);
350+
}
351+
305352
static inline pte_t pte_wrprotect(pte_t pte)
306353
{
307354
return pte_clear_flags(pte, _PAGE_RW);
@@ -414,6 +461,24 @@ static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
414461
return native_make_pmd(v & ~clear);
415462
}
416463

464+
/* See comments above mksaveddirty_shift() */
465+
static inline pmd_t pmd_mksaveddirty(pmd_t pmd)
466+
{
467+
pmdval_t v = native_pmd_val(pmd);
468+
469+
v = mksaveddirty_shift(v);
470+
return native_make_pmd(v);
471+
}
472+
473+
/* See comments above mksaveddirty_shift() */
474+
static inline pmd_t pmd_clear_saveddirty(pmd_t pmd)
475+
{
476+
pmdval_t v = native_pmd_val(pmd);
477+
478+
v = clear_saveddirty_shift(v);
479+
return native_make_pmd(v);
480+
}
481+
417482
static inline pmd_t pmd_wrprotect(pmd_t pmd)
418483
{
419484
return pmd_clear_flags(pmd, _PAGE_RW);
@@ -485,6 +550,24 @@ static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)
485550
return native_make_pud(v & ~clear);
486551
}
487552

553+
/* See comments above mksaveddirty_shift() */
554+
static inline pud_t pud_mksaveddirty(pud_t pud)
555+
{
556+
pudval_t v = native_pud_val(pud);
557+
558+
v = mksaveddirty_shift(v);
559+
return native_make_pud(v);
560+
}
561+
562+
/* See comments above mksaveddirty_shift() */
563+
static inline pud_t pud_clear_saveddirty(pud_t pud)
564+
{
565+
pudval_t v = native_pud_val(pud);
566+
567+
v = clear_saveddirty_shift(v);
568+
return native_make_pud(v);
569+
}
570+
488571
static inline pud_t pud_mkold(pud_t pud)
489572
{
490573
return pud_clear_flags(pud, _PAGE_ACCESSED);

arch/x86/include/asm/pgtable_types.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#define _PAGE_BIT_SOFTW2 10 /* " */
2222
#define _PAGE_BIT_SOFTW3 11 /* " */
2323
#define _PAGE_BIT_PAT_LARGE 12 /* On 2MB or 1GB pages */
24-
#define _PAGE_BIT_SOFTW4 58 /* available for programmer */
24+
#define _PAGE_BIT_SOFTW4 57 /* available for programmer */
25+
#define _PAGE_BIT_SOFTW5 58 /* available for programmer */
2526
#define _PAGE_BIT_PKEY_BIT0 59 /* Protection Keys, bit 1/4 */
2627
#define _PAGE_BIT_PKEY_BIT1 60 /* Protection Keys, bit 2/4 */
2728
#define _PAGE_BIT_PKEY_BIT2 61 /* Protection Keys, bit 3/4 */
@@ -34,6 +35,13 @@
3435
#define _PAGE_BIT_SOFT_DIRTY _PAGE_BIT_SOFTW3 /* software dirty tracking */
3536
#define _PAGE_BIT_DEVMAP _PAGE_BIT_SOFTW4
3637

38+
#ifdef CONFIG_X86_64
39+
#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW5 /* Saved Dirty bit */
40+
#else
41+
/* Shared with _PAGE_BIT_UFFD_WP which is not supported on 32 bit */
42+
#define _PAGE_BIT_SAVED_DIRTY _PAGE_BIT_SOFTW2 /* Saved Dirty bit */
43+
#endif
44+
3745
/* If _PAGE_BIT_PRESENT is clear, we use these: */
3846
/* - if the user mapped it with PROT_NONE; pte_present gives true */
3947
#define _PAGE_BIT_PROTNONE _PAGE_BIT_GLOBAL
@@ -117,6 +125,18 @@
117125
#define _PAGE_SOFTW4 (_AT(pteval_t, 0))
118126
#endif
119127

128+
/*
129+
* The hardware requires shadow stack to be Write=0,Dirty=1. However,
130+
* there are valid cases where the kernel might create read-only PTEs that
131+
* are dirty (e.g., fork(), mprotect(), uffd-wp(), soft-dirty tracking). In
132+
* this case, the _PAGE_SAVED_DIRTY bit is used instead of the HW-dirty bit,
133+
* to avoid creating a wrong "shadow stack" PTEs. Such PTEs have
134+
* (Write=0,SavedDirty=1,Dirty=0) set.
135+
*/
136+
#define _PAGE_SAVED_DIRTY (_AT(pteval_t, 1) << _PAGE_BIT_SAVED_DIRTY)
137+
138+
#define _PAGE_DIRTY_BITS (_PAGE_DIRTY | _PAGE_SAVED_DIRTY)
139+
120140
#define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE)
121141

122142
/*
@@ -125,9 +145,9 @@
125145
* instance, and is *not* included in this mask since
126146
* pte_modify() does modify it.
127147
*/
128-
#define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \
129-
_PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY | \
130-
_PAGE_SOFT_DIRTY | _PAGE_DEVMAP | _PAGE_ENC | \
148+
#define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \
149+
_PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY_BITS | \
150+
_PAGE_SOFT_DIRTY | _PAGE_DEVMAP | _PAGE_ENC | \
131151
_PAGE_UFFD_WP)
132152
#define _HPAGE_CHG_MASK (_PAGE_CHG_MASK | _PAGE_PSE)
133153

@@ -188,10 +208,16 @@ enum page_cache_mode {
188208

189209
#define __PAGE_KERNEL (__PP|__RW| 0|___A|__NX|___D| 0|___G)
190210
#define __PAGE_KERNEL_EXEC (__PP|__RW| 0|___A| 0|___D| 0|___G)
211+
212+
/*
213+
* Page tables needs to have Write=1 in order for any lower PTEs to be
214+
* writable. This includes shadow stack memory (Write=0, Dirty=1)
215+
*/
191216
#define _KERNPG_TABLE_NOENC (__PP|__RW| 0|___A| 0|___D| 0| 0)
192217
#define _KERNPG_TABLE (__PP|__RW| 0|___A| 0|___D| 0| 0| _ENC)
193218
#define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0)
194219
#define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC)
220+
195221
#define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX|___D| 0|___G)
196222
#define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0|___D| 0|___G)
197223
#define __PAGE_KERNEL_NOCACHE (__PP|__RW| 0|___A|__NX|___D| 0|___G| __NC)

arch/x86/include/asm/tlbflush.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ static inline bool pte_flags_need_flush(unsigned long oldflags,
286286
const pteval_t flush_on_clear = _PAGE_DIRTY | _PAGE_PRESENT |
287287
_PAGE_ACCESSED;
288288
const pteval_t software_flags = _PAGE_SOFTW1 | _PAGE_SOFTW2 |
289-
_PAGE_SOFTW3 | _PAGE_SOFTW4;
289+
_PAGE_SOFTW3 | _PAGE_SOFTW4 |
290+
_PAGE_SAVED_DIRTY;
290291
const pteval_t flush_on_change = _PAGE_RW | _PAGE_USER | _PAGE_PWT |
291292
_PAGE_PCD | _PAGE_PSE | _PAGE_GLOBAL | _PAGE_PAT |
292293
_PAGE_PAT_LARGE | _PAGE_PKEY_BIT0 | _PAGE_PKEY_BIT1 |

0 commit comments

Comments
 (0)