|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH v8 62/65] x86/mm: zero stack on context switch
From: Roger Pau Monne <roger.pau@xxxxxxxxxx>
With per-CPU stacks the page-tables of a cpu_stack vCPU map, at
PCPU_STACK_VIRT(), the stack of the pCPU it runs on and no other, but
that stack holds what earlier work on the pCPU left there: the register
frames and working data of the vCPUs which ran there before. Zero it
before such a vCPU runs.
On a full context switch to a vCPU of a zero_stack domain, zero the
primary stack below the cpu_info block, and the event data FRED leaves
in it, and the #MC, NMI and #DB stacks when their handlers have run
since the last zeroing. Switching back to the vCPU whose state the
pCPU still holds, after a lazy switch to idle, zeroes nothing.
The #DF stack is not zeroed because handling of #DF results in a panic.
The shadow stacks are not zeroed. They hold return addresses within
Xen and, for events taken in Xen, Xen's own CS, LIP and SSP: no
guest-owned data, only a record of which paths in Xen ran for earlier
vCPUs. Zeroing them would take WRSS, word by word, around the
supervisor tokens.
The IST stacks are zeroed from the C preamble of the stack switch, on
the primary stack, with NMIs and #MC unmasked. An NMI or #MC arriving
meanwhile runs while the zeroing is suspended: on its IST page, or on
the primary stack where the ISTs are off (SVM, with an HVM vCPU coming
in) or, for an NMI, with FRED, below what is in use there. The zeroing
resumes once the handler has returned, so nothing in use is cleared,
and the primary stack's zeroing, which follows, clears what such a
handler leaves there. Each page's count is reset before the page is
zeroed, so such a handler marks its page for zeroing again: what it
leaves in the part already zeroed, like what an NMI, #MC or #DB leaves
once the counts have been read, stays until the next full switch to a
zero-stack vCPU. So does what an interrupt taken while the primary
stack is zeroed leaves below the point the zeroing has reached.
The zero-stack sub-option of asi= selects it, for HVM domains, a PVH
hardware domain included, in the forms vcpu-pt and cpu-stack take, and
is set by the whole-feature forms. Zeroing needs no page-tables, so it
does not depend on cpu-stack, and stays on when XPTI turns cpu-stack
off. Without cpu-stack, though, the vCPU's
page-tables still map every pCPU's stack, and zeroing clears only what
earlier work left on this one.
Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claude
Code:claude-opus-5-5
Signed-off-by: George Dunlap <gwd@xxxxxxxxxxxxxx>
---
Is it required to zero the stack when doing a non-lazy context switch from the
idle vCPU to the previously running vCPU?
d0v0 -> IDLE -> sync_execstate -> zero stack? -> d0v0
A non-lazy switch back to the vCPU which last ran (d0v0 -> idle, lazy
state given up -> d0v0) zeroes the stack: idle then ran on idle_pg_table,
where tasklets and softirqs may have left data the vCPU's page-tables do
not map. A lazy return does not: while lazy, idle runs on the vCPU's
page-tables, tasklets give the lazy state up first (do_tasklet_work()
calls sync_local_execstate()), and map_domain_page() completes the lazy
switch before mapping anything those page-tables leave out.
Changes in v8:
- New in this version.
Changes since previously posted version ("x86: adventures in Address Space
Isolation", v2):
- HVM only: zero-stack for HVM guests and a PVH hardware domain, in the
forms vcpu-pt and cpu-stack take; no PV knob, and nothing for a PV
hardware domain, whose page-tables map every pCPU's stack.
- Plain static booleans, without the late -1 resolution; the boot log
and the documentation cover the hardware domain knob.
- Also zero the event data FRED leaves in the cpu_info block for events
from ring 3.
- Count the IST stacks' use, and zero them, only with CONFIG_HVM, where
zero-stack can be in use.
- switch_stack_and_jump() evaluates its zero_stk argument once; the
layout assumption behind the zeroing is a BUILD_BUG_ON(), not an
ASSERT().
- Message: the IST-zeroing window is called out, with what an NMI or
an MCE arriving in it does (the zeroing is suspended, nothing in use
is cleared, and it runs on the primary stack where the ISTs are off,
as an NMI does with FRED);
the shadow stack paragraph reworded; what zero-stack gives without
cpu-stack. Docs: what the zero-stack bullet zeroes, FRED's event
data included, and what not.
Changes in v2:
- Zero the stack forward to use ERMS.
- Only zero the IST stacks if they have been used.
- Only zero the primary stack for full context switches.
---
docs/misc/xen-command-line.pandoc | 25 +++++++++----
xen/arch/x86/cpu/mcheck/mce.c | 5 +++
xen/arch/x86/domain.c | 13 +++++--
xen/arch/x86/include/asm/current.h | 56 +++++++++++++++++++++++++++---
xen/arch/x86/include/asm/domain.h | 6 ++++
xen/arch/x86/spec_ctrl.c | 50 +++++++++++++++++++++++---
xen/arch/x86/traps.c | 8 +++++
7 files changed, 145 insertions(+), 18 deletions(-)
diff --git a/docs/misc/xen-command-line.pandoc
b/docs/misc/xen-command-line.pandoc
index bf9f7f2130..5485843d8e 100644
--- a/docs/misc/xen-command-line.pandoc
+++ b/docs/misc/xen-command-line.pandoc
@@ -204,7 +204,8 @@ to appropriate auditing by Xen. Argo is disabled by
default.
### asi (x86)
> `= List of [ <bool>, {pv,hvm,dom0}=<bool>, {directmap,vmap-strict}=<bool>,
-> {vcpu-pt,cpu-stack}=<bool> | {vcpu-pt,cpu-stack}=hvm=<bool> ]`
+> {vcpu-pt,cpu-stack,zero-stack}=<bool> |
+> {vcpu-pt,cpu-stack,zero-stack}=hvm=<bool> ]`
> Default: `false`
@@ -215,16 +216,17 @@ mitigations for certain speculative related attacks, at
the cost of mapping
sensitive information on-demand. It might also offer some protection against
unmitigated speculation-related attacks.
-The mechanisms currently implemented are `vcpu-pt` and `cpu-stack`, for HVM
-domains including a PVH hardware domain, and `directmap`, for all domains
-including the hardware domain, PV or PVH. The plain boolean enables or
-disables all three, wherever they apply.
+The mechanisms currently implemented are `vcpu-pt`, `cpu-stack` and
+`zero-stack`, for HVM domains including a PVH hardware domain, and
+`directmap`, for all domains including the hardware domain, PV or PVH. The
+plain boolean enables or disables all four, wherever they apply.
* `pv=`, `hvm=` and `dom0=` enable the features for PV domains or HVM domains
other than the hardware domain, or for the hardware domain, PV or PVH. The
plain boolean does all three: `asi=1,dom0=0` leaves the hardware domain
- out. The forms naming no domain type (un-suffixed `vcpu-pt=<bool>` and
- `cpu-stack=<bool>`, and `directmap=<bool>`) cover the hardware domain too.
+ out. The forms naming no domain type (un-suffixed `vcpu-pt=<bool>`,
+ `cpu-stack=<bool>` and `zero-stack=<bool>`, and `directmap=<bool>`) cover
+ the hardware domain too.
**WARNING: manual de-selection of enabled options will invalidate any
protection offered by the feature. The fine grained options provided below
@@ -244,6 +246,15 @@ are meant to be used for debugging purposes only.**
a boot-log warning, while XPTI is enabled for the hardware domain or for
PV domains (see `xpti`), by default or explicitly.
+* `zero-stack` zeroes, on a context switch to a vCPU of the selected
+ domains, the CPU's primary stack below its `cpu_info` block, and FRED's
+ event data in it, and the #MC, NMI and #DB stacks if their handlers have
+ run since they were last zeroed, so that what earlier work left there is
+ gone before the vCPU runs. It zeroes nothing on the return to a vCPU
+ whose state the CPU still holds, and never the #DF stack or the shadow
+ stacks. It does not depend on `cpu-stack`; without it, the vCPU's
+ page-tables still map every CPU's stack.
+
* `directmap` (only available when Xen is built with
`CONFIG_SPARSE_DIRECTMAP`) runs guest contexts on a sparse view of the
directmap, which maps the memory Xen needs to reach at all times (the Xen
diff --git a/xen/arch/x86/cpu/mcheck/mce.c b/xen/arch/x86/cpu/mcheck/mce.c
index e435e13ad7..db5bfff615 100644
--- a/xen/arch/x86/cpu/mcheck/mce.c
+++ b/xen/arch/x86/cpu/mcheck/mce.c
@@ -93,10 +93,15 @@ struct mce_callbacks __ro_after_init mce_callbacks = {
static const typeof(mce_callbacks.handler) __initconst_cf_clobber __used
default_handler = unexpected_machine_check;
+DEFINE_PER_CPU(unsigned int, slice_mce_count);
+
/* Call the installed machine check handler for this CPU setup. */
void do_machine_check(const struct cpu_user_regs *regs)
{
+ if ( IS_ENABLED(CONFIG_HVM) ) /* For zero-stack. */
+ this_cpu(slice_mce_count)++;
+
mce_enter();
alternative_vcall(mce_callbacks.handler, regs);
mce_exit();
diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index 8ca6018118..b5d5ed7325 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -2317,6 +2317,7 @@ void noreturn context_switch(struct vcpu *prev, struct
vcpu *next)
struct cpu_info *info = get_cpu_info();
const struct domain *prevd = prev->domain, *nextd = next->domain;
unsigned int dirty_cpu = read_atomic(&next->dirty_cpu);
+ bool lazy = false;
ASSERT(prev != next);
ASSERT(local_irq_is_enabled());
@@ -2364,6 +2365,7 @@ void noreturn context_switch(struct vcpu *prev, struct
vcpu *next)
if ( is_idle_domain(nextd) )
mapcache_flush_zapped();
local_irq_enable();
+ lazy = true;
}
else
{
@@ -2403,12 +2405,19 @@ void noreturn context_switch(struct vcpu *prev, struct
vcpu *next)
/* Ensure that the vcpu has an up-to-date time base. */
update_vcpu_system_time(next);
- reset_stack_and_call_ind(nextd->arch.ctxt_switch->tail);
+ /*
+ * Context switches to the idle vCPU (either lazy or full) will never
+ * trigger zeroing of the stack, because the idle domain doesn't have ASI
+ * enabled. Switching back to the previously running vCPU after a lazy
+ * switch shouldn't zero the stack either.
+ */
+ reset_stack_and_call_ind(nextd->arch.ctxt_switch->tail,
+ !lazy && nextd->arch.asi.zero_stack);
}
void noreturn continue_running(struct vcpu *same)
{
- reset_stack_and_call_ind(same->domain->arch.ctxt_switch->tail);
+ reset_stack_and_call_ind(same->domain->arch.ctxt_switch->tail, false);
}
int __sync_local_execstate(void)
diff --git a/xen/arch/x86/include/asm/current.h
b/xen/arch/x86/include/asm/current.h
index cc518b68b3..ce74cf0071 100644
--- a/xen/arch/x86/include/asm/current.h
+++ b/xen/arch/x86/include/asm/current.h
@@ -11,6 +11,7 @@
#include <xen/page-size.h>
#include <asm/cpu-user-regs.h>
+#include <asm/idt.h>
/*
* Xen's cpu stacks are 8 pages (8-page aligned), arranged as:
@@ -184,6 +185,12 @@ DECLARE_PER_CPU(struct vcpu *, current_vcpu);
# define SHADOW_STACK_WORK ""
#endif
+#define ZERO_STACK \
+ "test %[stk_size], %[stk_size];" \
+ "jz .L_skip_zeroing.%=;" \
+ "rep stosb;" \
+ ".L_skip_zeroing.%=:"
+
#if __GNUC__ >= 9
# define ssaj_has_attr_noreturn(fn) __builtin_has_attribute(fn, __noreturn__)
#else
@@ -191,13 +198,45 @@ DECLARE_PER_CPU(struct vcpu *, current_vcpu);
# define ssaj_has_attr_noreturn(fn) true
#endif
-#define switch_stack_and_jump(fn, instr, constr) \
+DECLARE_PER_CPU(unsigned int, slice_mce_count);
+DECLARE_PER_CPU(unsigned int, slice_nmi_count);
+DECLARE_PER_CPU(unsigned int, slice_db_count);
+
+#define switch_stack_and_jump(fn, instr, constr, zero_stk) \
({ \
unsigned int tmp; \
+ bool ssaj_zero_stk = IS_ENABLED(CONFIG_HVM) && (zero_stk); \
+ \
BUILD_BUG_ON(!ssaj_has_attr_noreturn(fn)); \
+ /* guest_cpu_user_regs() is the cpu_info block's start. */ \
+ BUILD_BUG_ON(offsetof(struct cpu_info, guest_cpu_user_regs)); \
+ if ( ssaj_zero_stk ) \
+ { \
+ unsigned long stack_top = get_stack_bottom() & \
+ ~(STACK_SIZE - 1); \
+ \
+ /* FRED's event data from ring 3. */ \
+ get_cpu_info()->_fred = (struct fred_info){}; \
+ if ( this_cpu(slice_mce_count) ) \
+ { \
+ this_cpu(slice_mce_count) = 0; \
+ clear_page((void *)stack_top + IST_MCE * PAGE_SIZE); \
+ } \
+ if ( this_cpu(slice_nmi_count) ) \
+ { \
+ this_cpu(slice_nmi_count) = 0; \
+ clear_page((void *)stack_top + IST_NMI * PAGE_SIZE); \
+ } \
+ if ( this_cpu(slice_db_count) ) \
+ { \
+ this_cpu(slice_db_count) = 0; \
+ clear_page((void *)stack_top + IST_DB * PAGE_SIZE); \
+ } \
+ } \
__asm__ __volatile__ ( \
SHADOW_STACK_WORK \
"mov %[stk], %%rsp;" \
+ ZERO_STACK \
CHECK_FOR_LIVEPATCH_WORK \
instr "[fun]" \
: [val] "=&r" (tmp), \
@@ -208,19 +247,26 @@ DECLARE_PER_CPU(struct vcpu *, current_vcpu);
((PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8), \
[stack_mask] "i" (STACK_SIZE - 1), \
_ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, \
- __FILE__, NULL) \
+ __FILE__, NULL), \
+ /* For stack zeroing. */ \
+ "D" ((void *)guest_cpu_user_regs() - \
+ PRIMARY_STACK_SIZE + sizeof(struct cpu_info)), \
+ [stk_size] "c" \
+ (ssaj_zero_stk ? PRIMARY_STACK_SIZE - \
+ sizeof(struct cpu_info) : 0), \
+ "a" (0) \
: "memory" ); \
unreachable(); \
})
#define reset_stack_and_jump(fn) \
- switch_stack_and_jump(fn, "jmp %c", "i")
+ switch_stack_and_jump(fn, "jmp %c", "i", false)
/* The constraint may only specify non-call-clobbered registers. */
-#define reset_stack_and_call_ind(fn) \
+#define reset_stack_and_call_ind(fn, zero_stk) \
({ \
(void)((fn) == (typeof(dom_xen->arch.ctxt_switch->tail))NULL); \
- switch_stack_and_jump(fn, "INDIRECT_CALL %", "b"); \
+ switch_stack_and_jump(fn, "INDIRECT_CALL %", "b", zero_stk); \
})
/*
diff --git a/xen/arch/x86/include/asm/domain.h
b/xen/arch/x86/include/asm/domain.h
index ef60cdda11..40f72d02d4 100644
--- a/xen/arch/x86/include/asm/domain.h
+++ b/xen/arch/x86/include/asm/domain.h
@@ -501,6 +501,12 @@ struct arch_domain
*/
bool cpu_stack:1;
+ /*
+ * Zero the CPU's stack on a full (not lazy) switch to one of its
+ * vCPUs.
+ */
+ bool zero_stack:1;
+
/* The domain's root page-tables carry the sparse directmap view. */
bool sparse_dmap:1;
} asi;
diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c
index eb8da9ec28..f46aaed765 100644
--- a/xen/arch/x86/spec_ctrl.c
+++ b/xen/arch/x86/spec_ctrl.c
@@ -102,6 +102,13 @@ bool __ro_after_init opt_vcpu_pt_hwdom;
static bool __ro_after_init opt_cpu_stack_hvm;
static bool __ro_after_init opt_pvh_cpu_stack_hwdom;
+/*
+ * Zero the CPU's stack on a full context switch to a vCPU of an HVM domain,
+ * or of a PVH hardware domain (zero-stack).
+ */
+static bool __ro_after_init opt_zero_stack_hvm;
+static bool __ro_after_init opt_pvh_zero_stack_hwdom;
+
static int __init cf_check parse_spec_ctrl(const char *s)
{
const char *ss;
@@ -563,6 +570,7 @@ static int __init cf_check parse_asi(const char *s)
{
opt_vcpu_pt_hwdom = opt_vcpu_pt_hvm = true;
opt_pvh_cpu_stack_hwdom = opt_cpu_stack_hvm = true;
+ opt_pvh_zero_stack_hwdom = opt_zero_stack_hvm = true;
set_sparse_dmap(true, true, true, true);
}
@@ -578,6 +586,7 @@ static int __init cf_check parse_asi(const char *s)
case 1:
opt_vcpu_pt_hwdom = opt_vcpu_pt_hvm = val;
opt_pvh_cpu_stack_hwdom = opt_cpu_stack_hvm = val;
+ opt_pvh_zero_stack_hwdom = opt_zero_stack_hvm = val;
set_sparse_dmap(true, true, true, val);
break;
@@ -587,11 +596,13 @@ static int __init cf_check parse_asi(const char *s)
else if ( (val = parse_boolean("hvm", s, ss)) >= 0 )
{
opt_vcpu_pt_hvm = opt_cpu_stack_hvm = val;
+ opt_zero_stack_hvm = val;
set_sparse_dmap(false, true, false, val);
}
else if ( (val = parse_boolean("dom0", s, ss)) >= 0 )
{
opt_vcpu_pt_hwdom = opt_pvh_cpu_stack_hwdom = val;
+ opt_pvh_zero_stack_hwdom = val;
set_sparse_dmap(false, false, true, val);
}
else if ( (val = parse_boolean("directmap", s, ss)) >= 0 )
@@ -665,6 +676,28 @@ static int __init cf_check parse_asi(const char *s)
break;
}
}
+ else if ( (val = parse_boolean("zero-stack", s, ss)) != -1 )
+ {
+ switch ( val )
+ {
+ case 1:
+ case 0:
+ opt_pvh_zero_stack_hwdom = opt_zero_stack_hvm = val;
+ break;
+
+ case -2:
+ s += strlen("zero-stack=");
+ if ( (val = parse_boolean("hvm", s, ss)) >= 0 )
+ {
+ opt_zero_stack_hvm = val;
+ break;
+ }
+ fallthrough;
+ default:
+ rc = -EINVAL;
+ break;
+ }
+ }
else if ( *s )
rc = -EINVAL;
break;
@@ -874,23 +907,28 @@ static void __init print_details(enum ind_thunk thunk)
{
bool hwdom_pt = opt_dom0_pvh && opt_vcpu_pt_hwdom;
+ bool hwdom_zs = opt_dom0_pvh && opt_pvh_zero_stack_hwdom;
bool hwdom_sd = sparse_dmap_hwdom() &&
(opt_dom0_pvh ? hwdom_pt : !opt_xpti_hwdom);
printk(" ASI directmap mode: %s\n",
sparse_dmap_configured() ? "sparse" : "full");
- printk(" ASI features for Dom0:%s%s%s%s\n",
- hwdom_pt || hwdom_sd ? "" : " None",
+ printk(" ASI features for Dom0:%s%s%s%s%s\n",
+ hwdom_pt || hwdom_zs || hwdom_sd
+ ? "" : " None",
hwdom_pt ? " vCPU-PT" : "",
hwdom_pt && opt_pvh_cpu_stack_hwdom
? " CPU-STACK" : "",
+ hwdom_zs ? " ZERO-STACK" : "",
hwdom_sd ? " sparse-directmap" : "");
if ( IS_ENABLED(CONFIG_HVM) )
- printk(" ASI features for HVM VMs:%s%s%s%s\n",
- opt_vcpu_pt_hvm ? "" : " None",
+ printk(" ASI features for HVM VMs:%s%s%s%s%s\n",
+ opt_vcpu_pt_hvm || opt_zero_stack_hvm
+ ? "" : " None",
opt_vcpu_pt_hvm ? " vCPU-PT" : "",
opt_vcpu_pt_hvm && opt_cpu_stack_hvm
? " CPU-STACK" : "",
+ opt_zero_stack_hvm ? " ZERO-STACK" : "",
opt_vcpu_pt_hvm && sparse_dmap_hvm()
? " sparse-directmap" : "");
if ( IS_ENABLED(CONFIG_PV) )
@@ -2126,6 +2164,10 @@ void spec_ctrl_init_domain(struct domain *d)
(is_hardware_domain(d) ? opt_pvh_cpu_stack_hwdom
: opt_cpu_stack_hvm);
+ d->arch.asi.zero_stack = !pv &&
+ (is_hardware_domain(d) ? opt_pvh_zero_stack_hwdom
+ : opt_zero_stack_hvm);
+
/*
* The sparse directmap view needs a mapcache: PV domains all have one,
* HVM domains through per-vCPU page-tables. A PV domain using XPTI
diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c
index 6ecbe171fc..6d3c16bebb 100644
--- a/xen/arch/x86/traps.c
+++ b/xen/arch/x86/traps.c
@@ -1920,6 +1920,7 @@ static void unknown_nmi_error(const struct cpu_user_regs
*regs,
static nmi_callback_t *__read_mostly nmi_callback;
DEFINE_PER_CPU(unsigned int, nmi_count);
+DEFINE_PER_CPU(unsigned int, slice_nmi_count);
void do_nmi(const struct cpu_user_regs *regs)
{
@@ -1929,6 +1930,8 @@ void do_nmi(const struct cpu_user_regs *regs)
bool handle_unknown = false;
this_cpu(nmi_count)++;
+ if ( IS_ENABLED(CONFIG_HVM) ) /* For zero-stack. */
+ this_cpu(slice_nmi_count)++;
nmi_enter();
/*
@@ -2050,11 +2053,16 @@ void asmlinkage do_device_not_available(struct
cpu_user_regs *regs)
void nocall sysenter_eflags_saved(void);
+DEFINE_PER_CPU(unsigned int, slice_db_count);
+
/* Handle #DB. @dbg is PENDING_DBG, a.k.a. %dr6 with positive polarity. */
static void handle_DB(struct cpu_user_regs *regs, unsigned long dbg)
{
struct vcpu *v = current;
+ if ( IS_ENABLED(CONFIG_HVM) ) /* For zero-stack. */
+ this_cpu(slice_db_count)++;
+
/*
* At the time of writing (March 2018), on the subject of %dr6:
*
--
2.56.0
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |