From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (NAM12-MW2-obe.outbound.protection.outlook.com [40.107.244.66]) by mx.groups.io with SMTP id smtpd.web09.1195.1619636997285245444 for ; Wed, 28 Apr 2021 12:09:57 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=LzB3R4BR; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.244.66, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G7O+ueKe3197lXhraANYCsjInsp/Eh3iVuOEBIUyc4qkff/qlgHXLrZqTTX2aq/h530ccZ01ylCy9jlvBL3kuTvVH+3E0ab6SdjOGPBtZlWyK7KwN+5VqLyf3DdFphkeAoGSMJIirzeZQQya0sa0D6KvMlZZpgyEvGDJw5IfeVDMfc1TMCcwxFjpyh4J7lsNxHk/OpPwGZpGdGDx0vTiE9jLtPxSTlTSDIZG6jpejbAR6yV+WRhXL8c0ciRKHSfv741TcrK5h9l1d9PXek7ekGGiBL2pt58OR1mKZs8O4dSCqdj3br3blH1R5OuXhtvkPMOqIxA5sUeRufg1NyzNeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LakTW1vMaEUADtDTQda30LEiBzpvB/GLN2WOrDH3fdc=; b=hZDgrvQ7PzvSSuc7Ilkot5h0CodY58vp8lPdBS10OxItc+MgC/lGEYvSmUfVZ6gwqXAa46TJT9OawFdkOhamhwpeyzw51RXNOUy6/vdsICpMdfm9mf8Bf6AiJbtQXNQTPFvhoJPRQC/gXQgPrVDeOsBmFEtz038KqqteC4ydp2X9v/WnORdSxWxxRKZNTJoRj7SilXbtqi/Exljb/7Mkoia2moNatfhePiVJ/3x9gT93SfFKWJb4f1GmtC5u+Y1HYA84ARxGRXMoHnfd7kfDmpokjxYqDs3TwwQnSMHbO3GNWu5UXlne72IZmfIZ9FrO4sz771CySxe+PGQ3XwFf8g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LakTW1vMaEUADtDTQda30LEiBzpvB/GLN2WOrDH3fdc=; b=LzB3R4BR8uFcDl2kOf9cahdlrrfPe69S7NfGgfEAHl3Wq24hTEvWOp/8jnrN72/xhpiFwPfY42ewbtBFYfBTuTRO+6JMw34b/YPAEykNJaZAcFsRBu9H+h9WGY+j6V0duYUcIhX+dodaoPU0qiNSGSL/12SEVb3SLmo2tdyhNgA= Authentication-Results: intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=none action=none header.from=amd.com; Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM6PR12MB3866.namprd12.prod.outlook.com (2603:10b6:5:1c6::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21; Wed, 28 Apr 2021 19:09:55 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::b914:4704:ad6f:aba9]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::b914:4704:ad6f:aba9%12]) with mapi id 15.20.4065.026; Wed, 28 Apr 2021 19:09:55 +0000 Subject: Re: [edk2-devel] [PATCH 3/3] OvmfPkg/PlatformPei: Mark TPM MMIO range as unencrypted for SEV To: Laszlo Ersek , devel@edk2.groups.io Cc: Joerg Roedel , Borislav Petkov , Ard Biesheuvel , Jordan Justen , Brijesh Singh , James Bottomley , Jiewen Yao , Min Xu References: <1f64ca5689ec86c427e4db8c41da598896dca4ba.1618959281.git.thomas.lendacky@amd.com> <8417023b-b3d6-5268-e92a-0c6f5ebfff6b@redhat.com> <4261e15a-84a0-11a7-2981-9eeb538f6da9@redhat.com> <311cdc78-d818-bea4-16a1-01077bfc1140@amd.com> <21f40236-88f6-5886-1345-5a8b9ac1f732@amd.com> <0ad9369d-d863-5bfc-1326-ff4b631bd04e@amd.com> <3f2e589d-7258-c372-c1ab-60992351dbde@redhat.com> From: "Lendacky, Thomas" Message-ID: Date: Wed, 28 Apr 2021 14:09:52 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 In-Reply-To: <3f2e589d-7258-c372-c1ab-60992351dbde@redhat.com> X-Originating-IP: [67.79.209.213] X-ClientProxiedBy: SN2PR01CA0082.prod.exchangelabs.com (2603:10b6:800::50) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from office-linux.texastahm.com (67.79.209.213) by SN2PR01CA0082.prod.exchangelabs.com (2603:10b6:800::50) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.27 via Frontend Transport; Wed, 28 Apr 2021 19:09:54 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: da5f1f9f-cd11-4f53-aac9-08d90a79353a X-MS-TrafficTypeDiagnostic: DM6PR12MB3866: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: t6dNO2S0RiXWxEXoExnaJdxp/3gtyyQifpmejfsRgfuWMtQcwuFnylSRW+hsThJ01eL0f5PubDXVshvHZh80TKjzdJZVV5PvomNDgZYyYxvE+VR6WoYZjoyGpNRVfNq6J6GLXzFWf0CmLydw1VNnjoCne6eyH1jl+Erkx9u4PIuUymYL0rdrL/2I2KGJ1QqcSw6nhOwHj5XGEEhYuT4DD1LCnTCIkMYDpe+W3UlUfhnaq4Z0sjPzZaFv4FpkB8cj1zQghgPuddBoTBVMcloW52N0M8xag/qaCoMVup/vJJdf7/ElAJfiX4Bl04fVrp/dbltjif3MoReluEqYirG6L7QP39hVF4r5iMJCoajQEjK93CVayuYC7cuA8owXzBLqq6q9lICTs/ias8/iE8CdfDqlkcVd1NS6LrhrwgFmk211+e0ksXkPlPASYa6E1c7lFV2UQw30YLt9mm92txcOu94r3x3ReEj5wiHV8RXvvv5dWqXUlKh0HzIh++TeUXAEmpe5G6vMbekv2WAsHuXtD6RuwKM2Q0SO1p46zWg/KxzN4vHwfwAgALygE/Pcec4TzkXS2mkPqS02olfYZzns1HLUP0DbgFMJ7+N9w8E4vasKo+mjiL60ix8dVzgKdynE1EWzAOjnigEHVsz4qKA/FvqytWeuXNpUNP/EN+tVpEk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(376002)(346002)(39860400002)(136003)(396003)(8936002)(2906002)(186003)(6486002)(54906003)(31686004)(16526019)(53546011)(6506007)(36756003)(2616005)(66476007)(66556008)(26005)(86362001)(38100700002)(83380400001)(31696002)(5660300002)(316002)(8676002)(4326008)(6512007)(478600001)(956004)(66946007)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?utf-8?B?Um81dVlVSG9QaHNIdnh0eHhXNnlpNnYxaHh4Qk9IbVNVYk1CYmF1VlRDenlL?= =?utf-8?B?ZmkrTEI1RkNPNmE4a1BKdXZQM3lqSVlIUnhUWlRuRmM5aEZaTVBOMUxyRFVF?= =?utf-8?B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?= =?utf-8?B?d3FRMnp4Qmowb1J2R3h3dFF2NzRtMWNZcDZ4ZHZ2ZWM0Q2NISEJsVkkwMXdv?= =?utf-8?B?WUt2aS9kV3hsUTViR05KTUpvWGxUNVBCUXBvUlZDTnR4YTg3Q2JkQ3RkM2hU?= =?utf-8?B?Y3czamtxcmxUTXRCMitCWGREUjRUQmNiRkxVVWZBSEUxc204L0YxZUVka1Ar?= =?utf-8?B?c2xTaHdzZjIyKzVlVlNaZWZFWXI5WmRHOGkrN0haVjFuQ2VVWlJoYnNxUWpW?= =?utf-8?B?b2NNUU5RTEZLRW1NMWd5S1JrNEkzZ2Y0ZVJpT1pDQ2x4SDBiM1huQXcyL2ZD?= =?utf-8?B?MzYrcjhZdHNPSTU2czZ0c1Yxc2hJTVZLR00yZnk4enpJdnBETHA0Y1g3OTI5?= =?utf-8?B?YjJoTjBmTmltaWxnRkE5RTdmN1BDVlVRS0ZIazNVNkh6enVRRDhmSUIxSi9q?= =?utf-8?B?WVZ6RWJsTjV4WmtWRSs2L1E1dlg1RnZxbDBWdlVzK3U0bHZEZjFvQ0tFV1py?= =?utf-8?B?azZMSENGenhTT2JDV1R5Ukt1ZzQyYUxwMmFmSSt6SWNCdEVUY2paV0kvc0sr?= =?utf-8?B?ZlJQd3FKSENubTAzZDIrRGxHOUF0eGpOS3p6Wm4xZHhnQkF4SkZGTUg0aUs3?= =?utf-8?B?bjNJZGNXNlhORm9nREhHQms1WXhOdTBTUUVkNmNkWWU5QVBJU0NjMzRydzFD?= =?utf-8?B?SDZYM3VLWnNmK2JWRXptL09WckVtTWNkdkhpTWR1amtMRTFQMGhrNWUvY0hk?= =?utf-8?Q?jLvYLlK21aKaGBrE1kf1tZvvLwk4+eNk0h+lkWZ?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: da5f1f9f-cd11-4f53-aac9-08d90a79353a X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2021 19:09:55.1413 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Yxzdl2YSjlgwRzc2ihQl167gAIiol+BSaFmYGukN4PIQSo5AzQtJdWS5sA6TRNKXG2VLEz/Mb5+q+jVMSu0ATQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3866 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 4/28/21 11:12 AM, Laszlo Ersek wrote: > On 04/27/21 16:58, Tom Lendacky wrote: >> On 4/26/21 9:21 AM, Tom Lendacky wrote: >>> On 4/26/21 7:07 AM, Laszlo Ersek wrote: >>>> On 04/23/21 22:02, Tom Lendacky wrote: > >>>>> 1. SEV works with the current encrypted mapping, it is only the SEV-ES >>>>> support that fails because of the ValidateMmioMemory() check. I can do >>>>> the mapping change just for SEV-ES since it is X64 only. This works, >>>>> because MemEncryptSevClearPageEncMask() will not return UNSUPPORTED >>>>> when running in 64-bit. >>>> >>>> Can we really say "SEV works" though? Because, even using an X64 PEI >>>> phase, and enabling only SEV (not SEV-ES), TPM access will be broken in >>>> the PEI phase. Is my understanding correct? >>> >>> Because the memory range is marked as MMIO, we'll take a nested page fault >>> (NPF). The GPA passed as part of the NPF does not include the c-bit. So we >>> do in fact work properly with a TPM in SEV. > > Thanks for the explanation. > > Here's what bothers me about it: > > In AmdSevDxe, we clear the C-bit from MMIO and NonExistent areas in the > GCD memory space map. This occurs early in the DXE phase (see "APRIORI > DXE" in the FDF files). The justification is that we want the flash and > (for example) the PCI MMIO apertures decrypted. > > Now, I realize there is a difference between flash and TPM. TPM is > purely MMIO (no KVM memslot), but flash (when it is not in programming > mode) is backed by a read-only KVM memslot. IOW, flash is "actual > memory", and so it is affected by SEV. TPM is never "actual memory", so > (according to your explanation, AIUI) it always traps to QEMU, per > access, and the C-bit doesn't interfere with that. > > This is consistent with two facts about OVMF's PEI phase: > > - We use IO Port-based fw_cfg (never DMA), if SEV is enabled (see > "QemuFwCfgPei.c"). > > - We access PCI config space via IO Ports (0xCF8, 0xCFC), never ECAM. > (This was not motivated by SEV, see commit 7523788faa51, but it does > play nice with SEV, in the PEI phase -- I think?) > > What I'm confused about, now, in retrospect, is the reference to the PCI > MMIO aperture, in AmdSevDxe. If that area isn't backed by a KVM memslot > *either* -- similarly to the TPM area --, then decrypting *that* in > AmdSevDxe (via "nonexistent") is not strictly necessary. Is that correct? It is necessary for (and was added for) SEV-ES. The explicit mapping of the PCI MMCONFIG range as unencrypted was added for SEV-ES so that the SEV-ES mitigation would not terminate the guest because MMIO was being performed against an encrypted address. There's a nice comment in OvmfPkg/PlatformPei/Platform.c that talks about how the MMCONFIG range is not added as MMIO, but instead as reserved memory. Because of that, the loop through the memory space map in AmdSevDxe.c does not mark the MMCONFIG range as unencrypted. > > I'm not asking for any code changes, just trying to form a consistent view. > > Another question (still for "base SEV"): when OVMF is built with > SMM_REQUIRE, PlatformPei performs a (read-only) variable access. See the > ReadOnlyVariable2->GetVariable() call in the RefreshMemTypeInfo() > function. When SEV is active, does control reach RefreshMemTypeInfo() on > your end? And does ReadOnlyVariable2->GetVariable() succeed for you? > > (There is a DEBUG_VERBOSE message in OnReadOnlyVariable2Available(), and > a DEBUG_ERROR in RefreshMemTypeInfo(), so the above questions can be > answered just from the log; no need to modify the code for that.) Yes, control reaches that point. But, notably, with a legacy VM I see the following messages: RefreshMemTypeInfo: GetVariable(): Not Found But with an SEV VM I see: Firmware Volume for Variable Store is corrupted RefreshMemTypeInfo: GetVariable(): Not Found So I get the "Not Found" in both cases. But with SEV, I see the "corrupted" message from InitRealNonVolatileVariableStore() in MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c. I imagine that since the range is marked encrypted, it reads the header incorrectly and fails. Thanks, Tom > > Basically, with SEV enabled, I expect ReadOnlyVariable2->GetVariable() > to fail -- or even to remain unreached, as FaultTolerantWritePei and > VariablePei could bail out earlier (before installing the Variable PPI), > due to failing flash accesses. In case I'm *not* wrong -- it's not the > end of the world, I'm only asking this question too for the sake of > clarifying "C-bit vs. MMIO". > > More or less it seems to boil down to whether there is a KVM memslot or > not -- which is *not* equivalent to OVMF considering the area MMIO or not. > > >>> SEV-ES would also work >>> properly if the mitigation for accessing an encrypted address was removed >>> from the #VC handler. It is only this added mitigation to protect MMIO >>> that results in an issue with the TPM in PEI. >> >> So I'm thinking that I can have TpmMmioSevDecryptPeim.c do this: >> >> // >> // If SEV or SEV-ES is active, MMIO succeeds against an encrypted physical >> // address because the nested page fault (NPF) that occurs on access does not >> // include the encryption bit in the guest physical address provided to the >> // hypervisor. >> // >> // However, if SEV-ES is active, before performing the actual MMIO, an >> // additional MMIO mitigation check is performed in the #VC handler to ensure >> // that MMIO is being done to an unencrypted address. To prevent guest >> // termination in this scenario, mark the range unencrypted ahead of access. >> // >> if (MemEncryptSevEsIsEnabled ()) { >> // Do MemEncryptSevClearPageEncMask() ... >> } >> >> Let me submit the next version with this and see what you think. > > Yep, I'll review that now. > > Thanks > Laszlo > >> >> Thanks, >> Tom >> >>> >>>> >>>> I think the behavior you currently see is actually what we want, we >>>> should double down on it -- if MemEncryptSevClearPageEncMask() fails, >>>> report an explicit DEBUG_ERROR, and call CpuDeadLoop(). If the firmware >>>> is built with TPM_ENABLE, and SEV is active, then an IA32 PEI phase is >>>> simply unusable. Silently pretending that the TPM is not there, even >>>> though it may have been configured on the QEMU command line, we just >>>> failed to communicate with it, is not a good idea, IMO. >>> >>> However, because the c-bit is not part of the NPF, we do communicate >>> successfully with the TPM. >>> >>> So we could actually do following: >>> - For IA32: >>> - Remove the Depex on gOvmfTpmMmioAccessiblePpiGuid >>> - Do not add OvmfPkg/Tcg/TpmMmioSevDecryptPei/TpmMmioSevDecryptPei.inf >>> >>> - For X64: >>> - Add the Depex on gOvmfTpmMmioAccessiblePpiGuid >>> - Add OvmfPkg/Tcg/TpmMmioSevDecryptPei/TpmMmioSevDecryptPei.inf >>> >>> That might be confusing, though. So we could just do option #3 below. >>> >>> Thanks, >>> Tom >>> >>>> >>>> This is somewhat similar IMO to the S3Verification() function in >>>> "OvmfPkg/PlatformPei/Platform.c". >>>> >>>> TPM_ENABLE, SEV, IA32 PEI phase: pick any two. >>>> >>>> Thanks, >>>> Laszlo >>>> >>>>> >>>>> 2. Call MemEncryptSevClearPageEncMask() for SEV or SEV-ES, but don't check >>>>> the return status. >>>>> >>>>> 3. Create Ia32 and X64 versions of internal functions, where the Ia32 >>>>> version simply returns SUCCESS because it can't do anything and the X64 >>>>> version calls MemEncryptSevClearPageEncMask(), allowing the main code >>>>> to ASSERT on any errors. >>>>> >>>>> I'm leaning towards #1, because this is an SEV-ES only issue. Thoughts? >>>>> >>>>> Thanks, >>>>> Tom >>>>> >>>>>> >>>>>> One thing I found is that the Bhyve package makes reference to the >>>>>> OvmfPkg/Bhyve/Tcg directory, but that directory does not exist. So I don't >>>>>> think that TPM enablement has been tested. I didn't update the Bhyve >>>>>> support for that reason. >>>>>> >>>>>> Thanks, >>>>>> Tom >>>>>> >>>>>>> Thanks! >>>>>>> Laszlo >>>>>>> >>>>> >>>> >> >