Andy Lutomirski
2021-Feb-17 18:09 UTC
[PATCH 2/3] x86/sev-es: Check if regs->sp is trusted before adjusting #VC IST stack
On Wed, Feb 17, 2021 at 4:02 AM Joerg Roedel <joro at 8bytes.org> wrote:> > From: Joerg Roedel <jroedel at suse.de> > > The code in the NMI handler to adjust the #VC handler IST stack is > needed in case an NMI hits when the #VC handler is still using its IST > stack. > But the check for this condition also needs to look if the regs->sp > value is trusted, meaning it was not set by user-space. Extend the > check to not use regs->sp when the NMI interrupted user-space code or > the SYSCALL gap. > > Reported-by: Andy Lutomirski <luto at kernel.org> > Fixes: 315562c9af3d5 ("x86/sev-es: Adjust #VC IST Stack on entering NMI handler") > Cc: stable at vger.kernel.org # 5.10+ > Signed-off-by: Joerg Roedel <jroedel at suse.de> > --- > arch/x86/kernel/sev-es.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c > index 84c1821819af..0df38b185d53 100644 > --- a/arch/x86/kernel/sev-es.c > +++ b/arch/x86/kernel/sev-es.c > @@ -144,7 +144,9 @@ void noinstr __sev_es_ist_enter(struct pt_regs *regs) > old_ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]); > > /* Make room on the IST stack */ > - if (on_vc_stack(regs->sp)) > + if (on_vc_stack(regs->sp) && > + !user_mode(regs) && > + !from_syscall_gap(regs)) > new_ist = ALIGN_DOWN(regs->sp, 8) - sizeof(old_ist); > else >Can you get rid of the linked list hack while you're at it? This code is unnecessarily convoluted right now, and it seems to be just asking for weird bugs. Just stash the old value in a local variable, please. Meanwhile, I'm pretty sure I can break this whole scheme if the hypervisor is messing with us. As a trivial example, the sequence SYSCALL gap -> #VC -> NMI -> #VC will go quite poorly. Is this really better than just turning IST off for #VC and documenting that we are not secure against a malicious hypervisor yet? --Andy
Joerg Roedel
2021-Feb-18 11:25 UTC
[PATCH 2/3] x86/sev-es: Check if regs->sp is trusted before adjusting #VC IST stack
Hi Andy, On Wed, Feb 17, 2021 at 10:09:46AM -0800, Andy Lutomirski wrote:> Can you get rid of the linked list hack while you're at it? This code > is unnecessarily convoluted right now, and it seems to be just asking > for weird bugs. Just stash the old value in a local variable, please.Yeah, the linked list is not really necessary right now, because of the way nested NMI handling works and given that these functions are only used in the NMI handler right now. The whole #VC handling code was written with future requirements in mind, like what is needed when debugging registers get virtualized and #HV gets enabled. Until its clear whether __sev_es_ist_enter/exit() is needed in any of these paths, I'd like to keep the linked list for now. It is more complicated but allows nesting.> Meanwhile, I'm pretty sure I can break this whole scheme if the > hypervisor is messing with us. As a trivial example, the sequence > SYSCALL gap -> #VC -> NMI -> #VC will go quite poorly.I don't see how this would break, can you elaborate? What I think happens is: SYSCALL gap (RSP is from userspace and untrusted) -> #VC - Handler on #VC IST stack detects that it interrupted the SYSCALL gap and switches to the task stack. -> NMI - Now running on NMI IST stack. Depending on whether the stack switch in the #VC handler already happened, the #VC IST entry is adjusted so that a subsequent #VC will not overwrite the interrupted handlers stack frame. -> #VC - Handler runs on the adjusted #VC IST stack and switches itself back to the NMI IST stack. This is safe wrt. nested NMIs as long as nested NMIs itself are safe. As a rule of thumb, think of the #VC handler as trying to be a non-IST handler by switching itself to the interrupted stack or the task stack. If it detects that this is not possible (which can't happen right now, but with SNP), it will kill the guest. Also #VC is currently not safe against #MC, but this is the same as with NMI and #MC. And more care is needed when SNP gets enabled and #VCs can happen in the stack switching/stack adjustment code itself. I will probably just add a check then to kill the guest if an SNP related #VC comes from noinstr code.> Is this really better than just turning IST off for #VC and > documenting that we are not secure against a malicious hypervisor yet?It needs to be IST, even without SNP, because #DB is IST too. When the hypervisor intercepts #DB then any #DB exception will be turned into #VC, so #VC needs to be handled anywhere a #DB can happen. And with SNP we need to be able to at least detect a malicious HV so we can reliably kill the guest. Otherwise the HV could potentially take control over the guest's execution flow and make it reveal its secrets. Regards, Joerg