Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7232513pxb; Thu, 18 Feb 2021 05:11:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJwekV4Fp3J0VfcbuDB53Qz+LejF8HeBwlVQct9H1rp4FFpJMpEetVIs6T6kkXYvFvBJZgnB X-Received: by 2002:a50:d4d5:: with SMTP id e21mr3948701edj.373.1613653884112; Thu, 18 Feb 2021 05:11:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613653884; cv=none; d=google.com; s=arc-20160816; b=Odgb8WBdtQl+QGEnjQhldCb6b7YL4lo1u/jLlnO1rTyk3WdH+FSILr8HGXeBG+Cg4D HLESky9o6aidhaG1tqV10IujUKYFyxQyC8+8Ziqq0/KDRU1xd66Px5H0sjeSRkCnqGa4 PieSVJ13ZY96XXhVdlHOx1v0Gbv9aZIzjkJAmHb+f+BWLTAYYj+gZg8FZC9BuZhVHkU9 TE1YUpQgBwGKfW1seHyTVJm/i8kOd0vQmMNUVMttOJAwFqMSJXblWXQ10HMLMNK34fjH EiD+hZBfpKo0//3DXt/v6fhPF5tFNx0pKlrZglhAlctysBbVl20eM5dm+NQ/8lqtUYYw fUQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=Das80Xa+0355EBhybXILqNR+qeqmcyqjl0JLRW5SaAw=; b=RL7T7+CWh/sMR/FQk7hs0Ap/08JIgDzlwtHs2hisSMbzBLifTe8BMV3phgHHBskpkU WJ+6aHxXKVITOFcr7lC2ixFrhsX6WMKR4/E+j0jTmR7kHn368uXWamU5vwN8vijL1/S0 YVLnpNHU6ahtgDKI5dc4zuUGSBuoiLhwc6dIQ++01fZ5RaSeLE6WxNiIfk+UpYI9zgYN mzknZWpGUPEfNhHW2KObshPmiAGNGt/s/3bM6afbyyRmPJwixNaJ4pLRNOW8c3+RQanr UbkJvLqI04mIiGgYkijcY/YHQ7fNy/KMKFr3jupzaZPrnMxi9efEWtHyg2rgjo9+2HlD Idpw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cs15si3493423ejc.653.2021.02.18.05.10.59; Thu, 18 Feb 2021 05:11:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233452AbhBRNKD (ORCPT + 99 others); Thu, 18 Feb 2021 08:10:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231371AbhBRLjZ (ORCPT ); Thu, 18 Feb 2021 06:39:25 -0500 Received: from theia.8bytes.org (8bytes.org [IPv6:2a01:238:4383:600:38bc:a715:4b6d:a889]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD2B7C061756; Thu, 18 Feb 2021 03:28:47 -0800 (PST) Received: by theia.8bytes.org (Postfix, from userid 1000) id 017B6247; Thu, 18 Feb 2021 12:25:03 +0100 (CET) Date: Thu, 18 Feb 2021 12:25:00 +0100 From: Joerg Roedel To: Andy Lutomirski Cc: X86 ML , Joerg Roedel , stable , "H. Peter Anvin" , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , Arvind Sankar , LKML , kvm list , Linux Virtualization Subject: Re: [PATCH 2/3] x86/sev-es: Check if regs->sp is trusted before adjusting #VC IST stack Message-ID: <20210218112500.GH7302@8bytes.org> References: <20210217120143.6106-1-joro@8bytes.org> <20210217120143.6106-3-joro@8bytes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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