From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 8E1FF80331 for ; Tue, 21 Mar 2017 03:36:11 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D46FF2E6061; Tue, 21 Mar 2017 10:36:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D46FF2E6061 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=lersek@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D46FF2E6061 Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-42.phx2.redhat.com [10.3.116.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF9CC7836F; Tue, 21 Mar 2017 10:36:10 +0000 (UTC) To: Ard Biesheuvel , edk2-devel@lists.01.org References: <1490088209-8564-1-git-send-email-ard.biesheuvel@linaro.org> <1490088209-8564-2-git-send-email-ard.biesheuvel@linaro.org> Cc: sigmaepsilon92@gmail.com From: Laszlo Ersek Message-ID: <19fedc88-4567-72c6-fca3-8c81e95c6d5e@redhat.com> Date: Tue, 21 Mar 2017 11:36:08 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <1490088209-8564-2-git-send-email-ard.biesheuvel@linaro.org> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 21 Mar 2017 10:36:12 +0000 (UTC) Subject: Re: [PATCH 1/2] ArmVirtPkg/HighMemDxe: use CPU arch protocol to apply memprotect policy X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Mar 2017 10:36:11 -0000 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit On 03/21/17 10:23, Ard Biesheuvel wrote: > Instead of invoking gDS->SetMemorySpaceAttributes to set the EFI_MEMORY_XP > attribute on newly added regions, which is guaranteed to fail if the same > attribute was not declared as a capability of the region when it as added, > invoke the CPU arch protocol directly to set the EFI_MEMORY_XP attribute > if our memory protection policy demands it. > > Reported-by: Michael Zimmermann > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Ard Biesheuvel > --- > ArmVirtPkg/HighMemDxe/HighMemDxe.c | 31 +++++++++++++++----- > ArmVirtPkg/HighMemDxe/HighMemDxe.inf | 1 + > 2 files changed, 25 insertions(+), 7 deletions(-) > > diff --git a/ArmVirtPkg/HighMemDxe/HighMemDxe.c b/ArmVirtPkg/HighMemDxe/HighMemDxe.c > index f70978f6414f..4e41120deff3 100644 > --- a/ArmVirtPkg/HighMemDxe/HighMemDxe.c > +++ b/ArmVirtPkg/HighMemDxe/HighMemDxe.c > @@ -20,6 +20,7 @@ > #include > #include > > +#include > #include > > EFI_STATUS > @@ -30,6 +31,7 @@ InitializeHighMemDxe ( > ) > { > FDT_CLIENT_PROTOCOL *FdtClient; > + EFI_CPU_ARCH_PROTOCOL *Cpu; > EFI_STATUS Status, FindNodeStatus; > INT32 Node; > CONST UINT32 *Reg; > @@ -43,6 +45,10 @@ InitializeHighMemDxe ( > (VOID **)&FdtClient); > ASSERT_EFI_ERROR (Status); > > + Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, > + (VOID **)&Cpu); > + ASSERT_EFI_ERROR (Status); > + > // > // Check for memory node and add the memory spaces except the lowest one > // > @@ -78,13 +84,24 @@ InitializeHighMemDxe ( > continue; > } > > + Status = gDS->SetMemorySpaceAttributes (CurBase, CurSize, > + EFI_MEMORY_WB); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_WARN, > + "%a: gDS->SetMemorySpaceAttributes() failed on region 0x%lx - 0x%lx (%r)\n", > + __FUNCTION__, CurBase, CurBase + CurSize - 1, Status)); > + } > + > + // > + // Due to the ambiguous nature of the RO/XP GCD memory space attributes, > + // it is impossible to add a memory space with the XP attribute in a way > + // that does not result in the XP attribute being set on *all* UEFI > + // memory map entries that are carved from it, including code regions > + // that require executable permissions. > // > - // Take care not to strip any permission attributes that will have been > - // set by DxeCore on the region we just added if a strict permission > - // policy is in effect for EfiConventionalMemory regions. > - // Unfortunately, we cannot interrogate the GCD memory space map for > - // those permissions, since they are not recorded there (for historical > - // reasons), so check the policy directly. > + // So instead, we never set the RO/XP attributes in the GCD memory space > + // capabilities or attribute fields, and apply any protections directly > + // on the page table mappings by going through the cpu arch protocol. > // > Attributes = EFI_MEMORY_WB; > if ((PcdGet64 (PcdDxeNxMemoryProtectionPolicy) & > @@ -92,7 +109,7 @@ InitializeHighMemDxe ( > Attributes |= EFI_MEMORY_XP; > } > > - Status = gDS->SetMemorySpaceAttributes (CurBase, CurSize, Attributes); > + Status = Cpu->SetMemoryAttributes (Cpu, CurBase, CurSize, Attributes); > > if (EFI_ERROR (Status)) { > DEBUG ((EFI_D_ERROR, > diff --git a/ArmVirtPkg/HighMemDxe/HighMemDxe.inf b/ArmVirtPkg/HighMemDxe/HighMemDxe.inf > index 89c743ebe058..ac1761974f52 100644 > --- a/ArmVirtPkg/HighMemDxe/HighMemDxe.inf > +++ b/ArmVirtPkg/HighMemDxe/HighMemDxe.inf > @@ -41,6 +41,7 @@ [LibraryClasses] > UefiDriverEntryPoint > > [Protocols] > + gEfiCpuArchProtocolGuid ## CONSUMES > gFdtClientProtocolGuid ## CONSUMES > > [Pcd] > gEfiCpuArchProtocolGuid is already part of the DEPEX, so: Reviewed-by: Laszlo Ersek