From nobody Thu May 2 03:18:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=trmm.net ARC-Seal: i=1; a=rsa-sha256; t=1601467244; cv=none; d=zohomail.com; s=zohoarc; b=DgEVwwIOL4leZ6/nu+KkVG5aJtZOGEpkU9zFqWhuFJSNKBo69tWjUWSbs1ZeLmE7XGzODB8dgATn+hC7FOnKfNUzmbl+n/VwgtyKj6UhXvpploKPT2l3Pa3Zg8sa92wml7I+SaYnNPBYm4WzEVXwSthNHYeXIbNSggGk205LR/w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601467244; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fuQDXdbg1+ipk0PhSWbp5DV75e1S355Vy0YXf2aihFo=; b=CNqBnytxDXfd0ZszmhvbiLNNLZQvuMqnlHmCsnO8XA/dGdrU3XYhM9dKi2vxXaogZo1w7EdfhkzWiZyvTpTLgrt8m8B2DsgS8guAlyTz8ZNM+Szoh0CPW+qgVcXGMAG9JGIag1pjLJj1tWQvCqG4PCCEc4iamISTpbJWUIVFTvM= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 160146724413963.555988818697074; Wed, 30 Sep 2020 05:00:44 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.626.2077 (Exim 4.92) (envelope-from ) id 1kNamb-0001cr-EJ; Wed, 30 Sep 2020 12:00:29 +0000 Received: by outflank-mailman (output) from mailman id 626.2077; Wed, 30 Sep 2020 12:00:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamb-0001ck-93; Wed, 30 Sep 2020 12:00:29 +0000 Received: by outflank-mailman (input) for mailman id 626; Wed, 30 Sep 2020 12:00:28 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamZ-0001Yz-VW for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:28 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 60846684-bb07-4ce4-9a29-3f6bf5deabcb; Wed, 30 Sep 2020 12:00:25 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0NOG005366; Wed, 30 Sep 2020 06:00:23 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdC003035; Wed, 30 Sep 2020 06:00:19 -0600 (MDT) (envelope-from hudson@trmm.net) Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamZ-0001Yz-VW for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:28 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 60846684-bb07-4ce4-9a29-3f6bf5deabcb; Wed, 30 Sep 2020 12:00:25 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0NOG005366; Wed, 30 Sep 2020 06:00:23 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdC003035; Wed, 30 Sep 2020 06:00:19 -0600 (MDT) (envelope-from hudson@trmm.net) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 60846684-bb07-4ce4-9a29-3f6bf5deabcb Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: neutral (ame8.swcp.com: 62.251.112.184 is neither permitted nor denied by domain of hudson@trmm.net) receiver=ame8.swcp.com; client-ip=62.251.112.184; helo=diamond.fritz.box; envelope-from=hudson@trmm.net; x-software=spfmilter 2.001 http://www.acme.com/software/spfmilter/ with libspf2-1.2.10; X-Authentication-Warning: ame8.swcp.com: Host 62-251-112-184.ip.xs4all.nl [62.251.112.184] claimed to be diamond.fritz.box From: Trammell Hudson To: xen-devel@lists.xenproject.org Cc: roger.pau@citrix.com, jbeulich@suse.com, andrew.cooper3@citrix.com, wl@xen.org Subject: [PATCH v8 1/5] efi/boot.c: add file.need_to_free Date: Wed, 30 Sep 2020 08:00:07 -0400 Message-Id: <20200930120011.1622924-2-hudson@trmm.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200930120011.1622924-1-hudson@trmm.net> References: <20200930120011.1622924-1-hudson@trmm.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 X-Greylist: Message whitelisted by DRAC access database, not delayed by milter-greylist-4.6.2 (ame8.swcp.com [216.184.2.128]); Wed, 30 Sep 2020 06:00:21 -0600 (MDT) X-Virus-Scanned: clamav-milter 0.99.2 at ame8 X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ame8.swcp.com X-Spam-Status: No, hits=0.7 tests=NO_RECEIVED,NO_RELAYS,SPF_NEUTRAL version=3.4.1 X-Spam-Level: Content-Type: text/plain; charset="utf-8" The config file, kernel, initrd, etc should only be freed if they are allocated with the UEFI allocator. On x86 the ucode, and on ARM the dtb, are also marked as need_to_free when allocated or expanded. This also fixes a memory leak in ARM fdt_increase_size() if there is an error in building the new device tree. Signed-off-by: Trammell Hudson Reviewed-by: Jan Beulich --- xen/arch/arm/efi/efi-boot.h | 11 +++++++++-- xen/arch/x86/efi/efi-boot.h | 2 +- xen/common/efi/boot.c | 10 ++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/xen/arch/arm/efi/efi-boot.h b/xen/arch/arm/efi/efi-boot.h index 27dd0b1a94..c6200fda0e 100644 --- a/xen/arch/arm/efi/efi-boot.h +++ b/xen/arch/arm/efi/efi-boot.h @@ -314,7 +314,10 @@ static void __init *fdt_increase_size(struct file *fdt= file, int add_size) if ( fdt_size ) { if ( fdt_open_into(dtbfile.ptr, new_fdt, pages * EFI_PAGE_SIZE) ) + { + efi_bs->FreePages(fdt_addr, pages); return NULL; + } } else { @@ -326,7 +329,10 @@ static void __init *fdt_increase_size(struct file *fdt= file, int add_size) * system table that is passed in the FDT. */ if ( fdt_create_empty_tree(new_fdt, pages * EFI_PAGE_SIZE) ) + { + efi_bs->FreePages(fdt_addr, pages); return NULL; + } } =20 /* @@ -335,12 +341,13 @@ static void __init *fdt_increase_size(struct file *fd= tfile, int add_size) * code will free it. If the original FDT came from a configuration * table, we don't own that memory and can't free it. */ - if ( dtbfile.size ) + if ( dtbfile.need_to_free ) efi_bs->FreePages(dtbfile.addr, PFN_UP(dtbfile.size)); =20 /* Update 'file' info for new memory so we clean it up on error exits = */ dtbfile.addr =3D fdt_addr; dtbfile.size =3D pages * EFI_PAGE_SIZE; + dtbfile.need_to_free =3D true; return new_fdt; } =20 @@ -546,7 +553,7 @@ static void __init efi_arch_cpu(void) =20 static void __init efi_arch_blexit(void) { - if ( dtbfile.addr && dtbfile.size ) + if ( dtbfile.need_to_free ) efi_bs->FreePages(dtbfile.addr, PFN_UP(dtbfile.size)); if ( memmap ) efi_bs->FreePool(memmap); diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h index eef3f52789..1025000afd 100644 --- a/xen/arch/x86/efi/efi-boot.h +++ b/xen/arch/x86/efi/efi-boot.h @@ -689,7 +689,7 @@ static void __init efi_arch_cpu(void) =20 static void __init efi_arch_blexit(void) { - if ( ucode.addr ) + if ( ucode.need_to_free ) efi_bs->FreePages(ucode.addr, PFN_UP(ucode.size)); } =20 diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index 157fe0e8c5..c2ce0c7294 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -102,6 +102,7 @@ union string { =20 struct file { UINTN size; + bool need_to_free; union { EFI_PHYSICAL_ADDRESS addr; char *str; @@ -280,13 +281,13 @@ void __init noreturn blexit(const CHAR16 *str) if ( !efi_bs ) efi_arch_halt(); =20 - if ( cfg.addr ) + if ( cfg.need_to_free ) efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size)); - if ( kernel.addr ) + if ( kernel.need_to_free ) efi_bs->FreePages(kernel.addr, PFN_UP(kernel.size)); - if ( ramdisk.addr ) + if ( ramdisk.need_to_free ) efi_bs->FreePages(ramdisk.addr, PFN_UP(ramdisk.size)); - if ( xsm.addr ) + if ( xsm.need_to_free ) efi_bs->FreePages(xsm.addr, PFN_UP(xsm.size)); =20 efi_arch_blexit(); @@ -581,6 +582,7 @@ static bool __init read_file(EFI_FILE_HANDLE dir_handle= , CHAR16 *name, } else { + file->need_to_free =3D true; file->size =3D size; if ( file !=3D &cfg ) { --=20 2.25.1 From nobody Thu May 2 03:18:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=trmm.net ARC-Seal: i=1; a=rsa-sha256; t=1601467249; cv=none; d=zohomail.com; s=zohoarc; b=nNBYXeOG0cstiQcmnhq9rrlnSvrP70kU/BiIx1DSia+bKGYEag4a31YqzsC3CfTlFaE+UCmFNYUnSD7sjfL76GERV2n34AsCkLYVIv03A8h5v2jc3QuGEperjnEtXqnB03m8fIEtnNSe2XoCHTj19+nr+3Wlb0Ig7J4pi+vrAwo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601467249; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+NseZPbp2nGcHoZXp5Zeoihv1T7WphNog6hmUeRhFTI=; b=EBhd65DS2M5SmfXakXLnJGo3AVxvF1yBW5bMs0SB5TjOmdEMCFP8f7cOmRJc+7XS2xrT2RDO+7HpbS61oEF4Tjghx3rl+8eAseM+vrdDH1ZVmiGpAmyzadAbiuJ6er9Je8BmwXeBptE6TSl6sIzuhdLGfWGUsI2hSqvcSCPCwXo= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1601467249560105.04775863944099; Wed, 30 Sep 2020 05:00:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.627.2089 (Exim 4.92) (envelope-from ) id 1kNamd-0001fO-Lv; Wed, 30 Sep 2020 12:00:31 +0000 Received: by outflank-mailman (output) from mailman id 627.2089; Wed, 30 Sep 2020 12:00:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamd-0001fG-IQ; Wed, 30 Sep 2020 12:00:31 +0000 Received: by outflank-mailman (input) for mailman id 627; Wed, 30 Sep 2020 12:00:30 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamc-0001eL-4W for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:30 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 4aa224d5-133f-4b1e-b179-648e60b1d2d6; Wed, 30 Sep 2020 12:00:29 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0RMK005372; Wed, 30 Sep 2020 06:00:27 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdD003035; Wed, 30 Sep 2020 06:00:23 -0600 (MDT) (envelope-from hudson@trmm.net) Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamc-0001eL-4W for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:30 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 4aa224d5-133f-4b1e-b179-648e60b1d2d6; Wed, 30 Sep 2020 12:00:29 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0RMK005372; Wed, 30 Sep 2020 06:00:27 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdD003035; Wed, 30 Sep 2020 06:00:23 -0600 (MDT) (envelope-from hudson@trmm.net) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 4aa224d5-133f-4b1e-b179-648e60b1d2d6 Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: neutral (ame8.swcp.com: 62.251.112.184 is neither permitted nor denied by domain of hudson@trmm.net) receiver=ame8.swcp.com; client-ip=62.251.112.184; helo=diamond.fritz.box; envelope-from=hudson@trmm.net; x-software=spfmilter 2.001 http://www.acme.com/software/spfmilter/ with libspf2-1.2.10; X-Authentication-Warning: ame8.swcp.com: Host 62-251-112-184.ip.xs4all.nl [62.251.112.184] claimed to be diamond.fritz.box From: Trammell Hudson To: xen-devel@lists.xenproject.org Cc: roger.pau@citrix.com, jbeulich@suse.com, andrew.cooper3@citrix.com, wl@xen.org Subject: [PATCH v8 2/5] efi/boot.c: add handle_file_info() Date: Wed, 30 Sep 2020 08:00:08 -0400 Message-Id: <20200930120011.1622924-3-hudson@trmm.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200930120011.1622924-1-hudson@trmm.net> References: <20200930120011.1622924-1-hudson@trmm.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 X-Greylist: Message whitelisted by DRAC access database, not delayed by milter-greylist-4.6.2 (ame8.swcp.com [216.184.2.128]); Wed, 30 Sep 2020 06:00:26 -0600 (MDT) X-Virus-Scanned: clamav-milter 0.99.2 at ame8 X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ame8.swcp.com X-Spam-Status: No, hits=0.7 tests=NO_RECEIVED,NO_RELAYS,SPF_NEUTRAL version=3.4.1 X-Spam-Level: Content-Type: text/plain; charset="utf-8" Add a separate function to display the address ranges used by the files and call `efi_arch_handle_module()` on the modules. Signed-off-by: Trammell Hudson Acked-by: Jan Beulich --- xen/common/efi/boot.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index c2ce0c7294..93cfeba7e1 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -540,6 +540,22 @@ static char * __init split_string(char *s) return NULL; } =20 +static void __init handle_file_info(CHAR16 *name, + const struct file *file, const char *o= ptions) +{ + if ( file =3D=3D &cfg ) + return; + + PrintStr(name); + PrintStr(L": "); + DisplayUint(file->addr, 2 * sizeof(file->addr)); + PrintStr(L"-"); + DisplayUint(file->addr + file->size, 2 * sizeof(file->addr)); + PrintStr(newline); + + efi_arch_handle_module(file, name, options); +} + static bool __init read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name, struct file *file, const char *options) { @@ -584,16 +600,7 @@ static bool __init read_file(EFI_FILE_HANDLE dir_handl= e, CHAR16 *name, { file->need_to_free =3D true; file->size =3D size; - if ( file !=3D &cfg ) - { - PrintStr(name); - PrintStr(L": "); - DisplayUint(file->addr, 2 * sizeof(file->addr)); - PrintStr(L"-"); - DisplayUint(file->addr + size, 2 * sizeof(file->addr)); - PrintStr(newline); - efi_arch_handle_module(file, name, options); - } + handle_file_info(name, file, options); =20 ret =3D FileHandle->Read(FileHandle, &file->size, file->str); if ( !EFI_ERROR(ret) && file->size !=3D size ) --=20 2.25.1 From nobody Thu May 2 03:18:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=trmm.net ARC-Seal: i=1; a=rsa-sha256; t=1601467251; cv=none; d=zohomail.com; s=zohoarc; b=NrdoChMxKql4UcUJbuTRhHVpjPBL7K6kqNKzmraR928+JBNWKN5JA3RrDBBhPuA4iC07VS4ZraPFI4A4UxYCHKlKE4Jw71i5HzRHac4al+J32hZYxx/xsOUbzsVwaR7noxqOdfNmi5n5UMS2As1Io6xX4w+m9AFILnIGetu3898= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601467251; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=A6X4z/CEK+8iJd0N5ZKWan7465QO1HJEsGJ4v0baip4=; b=YYCOdOcZ/prz/KGiqdk/Y8pByaEzJRr45UUv5aNvGzlRilE8dnVpt+txudZQ3QRTW9x5nRivMDjRaflxLxd9Cm/kQlPggzlP1Jo/JIUvkpuxK27FsAu2/7N2imU8NJ2X+xL2kWFr/DHJTs3kKfMKOr2Y83B+vKLoyoMITieY7dg= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1601467251869292.2807269022618; Wed, 30 Sep 2020 05:00:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.628.2101 (Exim 4.92) (envelope-from ) id 1kNami-0001jv-U2; Wed, 30 Sep 2020 12:00:36 +0000 Received: by outflank-mailman (output) from mailman id 628.2101; Wed, 30 Sep 2020 12:00:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNami-0001jn-Qo; Wed, 30 Sep 2020 12:00:36 +0000 Received: by outflank-mailman (input) for mailman id 628; Wed, 30 Sep 2020 12:00:34 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamg-0001iP-P3 for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:34 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 23989c6d-8ac6-42b3-8cb7-ddba8fb81831; Wed, 30 Sep 2020 12:00:33 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0VVc005378; Wed, 30 Sep 2020 06:00:31 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdE003035; Wed, 30 Sep 2020 06:00:28 -0600 (MDT) (envelope-from hudson@trmm.net) Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamg-0001iP-P3 for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:34 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 23989c6d-8ac6-42b3-8cb7-ddba8fb81831; Wed, 30 Sep 2020 12:00:33 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0VVc005378; Wed, 30 Sep 2020 06:00:31 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdE003035; Wed, 30 Sep 2020 06:00:28 -0600 (MDT) (envelope-from hudson@trmm.net) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 23989c6d-8ac6-42b3-8cb7-ddba8fb81831 Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: neutral (ame8.swcp.com: 62.251.112.184 is neither permitted nor denied by domain of hudson@trmm.net) receiver=ame8.swcp.com; client-ip=62.251.112.184; helo=diamond.fritz.box; envelope-from=hudson@trmm.net; x-software=spfmilter 2.001 http://www.acme.com/software/spfmilter/ with libspf2-1.2.10; X-Authentication-Warning: ame8.swcp.com: Host 62-251-112-184.ip.xs4all.nl [62.251.112.184] claimed to be diamond.fritz.box From: Trammell Hudson To: xen-devel@lists.xenproject.org Cc: roger.pau@citrix.com, jbeulich@suse.com, andrew.cooper3@citrix.com, wl@xen.org Subject: [PATCH v8 3/5] efi/boot.c: wrap PrintStr/PrintErr to allow const CHAR16* arguments Date: Wed, 30 Sep 2020 08:00:09 -0400 Message-Id: <20200930120011.1622924-4-hudson@trmm.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200930120011.1622924-1-hudson@trmm.net> References: <20200930120011.1622924-1-hudson@trmm.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 X-Greylist: Message whitelisted by DRAC access database, not delayed by milter-greylist-4.6.2 (ame8.swcp.com [216.184.2.128]); Wed, 30 Sep 2020 06:00:30 -0600 (MDT) X-Virus-Scanned: clamav-milter 0.99.2 at ame8 X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ame8.swcp.com X-Spam-Status: No, hits=0.7 tests=NO_RECEIVED,NO_RELAYS,SPF_NEUTRAL version=3.4.1 X-Spam-Level: Content-Type: text/plain; charset="utf-8" This patch wraps the EFI OutputString() method so that they can be called with const arguments. The OutputString method does not modify its argument, although the prototype is missing const, so it is necssary to cast away the const when calling it. It also updates callers of PrintStr/PrintErr to remove unneeded un-const ca= sts. Signed-off-by: Trammell Hudson Reviewed-by: Jan Beulich --- xen/common/efi/boot.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index 93cfeba7e1..bd629eb658 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -151,10 +151,17 @@ static struct file __initdata cfg; static struct file __initdata kernel; static struct file __initdata ramdisk; static struct file __initdata xsm; -static CHAR16 __initdata newline[] =3D L"\r\n"; +static const CHAR16 __initconst newline[] =3D L"\r\n"; =20 -#define PrintStr(s) StdOut->OutputString(StdOut, s) -#define PrintErr(s) StdErr->OutputString(StdErr, s) +static void __init PrintStr(const CHAR16 *s) +{ + StdOut->OutputString(StdOut, (CHAR16 *)s ); +} + +static void __init PrintErr(const CHAR16 *s) +{ + StdErr->OutputString(StdErr, (CHAR16 *)s ); +} =20 /* * Include architecture specific implementation here, which references the @@ -275,7 +282,7 @@ static bool __init match_guid(const EFI_GUID *guid1, co= nst EFI_GUID *guid2) void __init noreturn blexit(const CHAR16 *str) { if ( str ) - PrintStr((CHAR16 *)str); + PrintStr(str); PrintStr(newline); =20 if ( !efi_bs ) @@ -316,7 +323,7 @@ static void __init PrintErrMesg(const CHAR16 *mesg, EFI= _STATUS ErrCode) EFI_STATUS ErrIdx =3D ErrCode & ~EFI_ERROR_MASK; =20 StdOut =3D StdErr; - PrintErr((CHAR16 *)mesg); + PrintErr(mesg); PrintErr(L": "); =20 if( (ErrIdx < ARRAY_SIZE(ErrCodeToStr)) && ErrCodeToStr[ErrIdx] ) @@ -540,7 +547,7 @@ static char * __init split_string(char *s) return NULL; } =20 -static void __init handle_file_info(CHAR16 *name, +static void __init handle_file_info(const CHAR16 *name, const struct file *file, const char *o= ptions) { if ( file =3D=3D &cfg ) @@ -562,7 +569,7 @@ static bool __init read_file(EFI_FILE_HANDLE dir_handle= , CHAR16 *name, EFI_FILE_HANDLE FileHandle =3D NULL; UINT64 size; EFI_STATUS ret; - CHAR16 *what =3D NULL; + const CHAR16 *what =3D NULL; =20 if ( !name ) PrintErrMesg(L"No filename", EFI_OUT_OF_RESOURCES); --=20 2.25.1 From nobody Thu May 2 03:18:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=trmm.net ARC-Seal: i=1; a=rsa-sha256; t=1601467506; cv=none; d=zohomail.com; s=zohoarc; b=JF6Kv91oC7xLNc5Vc4SQbAEnlv1ArUvrt5ZDoJT/RAnNbR4tSAEAPZhPpyZRo29UEJdHpX7WUgGxNom70g/a+0fYOIyy9nmqNjzUcfyDBG6oJX9CHUluB4mxyE9Sc2nr4gvvVtCvMxiqfRwSDa6QyWbBzn6UJPxl43GeJ0r8Eko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601467506; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oEjZ+Gco4aiZK7RRA6ogOAE4lZbMNJHmunfIXgNc1mc=; b=EPFodXT5hJzYD3IiBuE0rgppafCF3nUbm6pXS+VvhFCRLal39IZAorFdoodt8FVBuy2zgkSs3qY/EmCyeddoq2H4M+TYiQnMiP/WNhb5g4eQIDTaR2T7FiJ0DKOdkyHK2kBv9qqj3sXFciF+2DYHLgOf6aab9tZAK05185bBEV4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTP id 1601467506778339.8099629356693; Wed, 30 Sep 2020 05:05:06 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.629.2113 (Exim 4.92) (envelope-from ) id 1kNamm-0001nv-6x; Wed, 30 Sep 2020 12:00:40 +0000 Received: by outflank-mailman (output) from mailman id 629.2113; Wed, 30 Sep 2020 12:00:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamm-0001nm-3o; Wed, 30 Sep 2020 12:00:40 +0000 Received: by outflank-mailman (input) for mailman id 629; Wed, 30 Sep 2020 12:00:39 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNaml-0001mg-35 for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:39 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id e126dab4-d0fd-4d56-a782-66e4d8970471; Wed, 30 Sep 2020 12:00:38 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0btp005388; Wed, 30 Sep 2020 06:00:37 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdF003035; Wed, 30 Sep 2020 06:00:32 -0600 (MDT) (envelope-from hudson@trmm.net) Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNaml-0001mg-35 for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:39 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id e126dab4-d0fd-4d56-a782-66e4d8970471; Wed, 30 Sep 2020 12:00:38 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0btp005388; Wed, 30 Sep 2020 06:00:37 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdF003035; Wed, 30 Sep 2020 06:00:32 -0600 (MDT) (envelope-from hudson@trmm.net) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e126dab4-d0fd-4d56-a782-66e4d8970471 Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: neutral (ame8.swcp.com: 62.251.112.184 is neither permitted nor denied by domain of hudson@trmm.net) receiver=ame8.swcp.com; client-ip=62.251.112.184; helo=diamond.fritz.box; envelope-from=hudson@trmm.net; x-software=spfmilter 2.001 http://www.acme.com/software/spfmilter/ with libspf2-1.2.10; X-Authentication-Warning: ame8.swcp.com: Host 62-251-112-184.ip.xs4all.nl [62.251.112.184] claimed to be diamond.fritz.box From: Trammell Hudson To: xen-devel@lists.xenproject.org Cc: roger.pau@citrix.com, jbeulich@suse.com, andrew.cooper3@citrix.com, wl@xen.org Subject: [PATCH v8 4/5] efi: Enable booting unified hypervisor/kernel/initrd images Date: Wed, 30 Sep 2020 08:00:10 -0400 Message-Id: <20200930120011.1622924-5-hudson@trmm.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200930120011.1622924-1-hudson@trmm.net> References: <20200930120011.1622924-1-hudson@trmm.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 X-Greylist: Message whitelisted by DRAC access database, not delayed by milter-greylist-4.6.2 (ame8.swcp.com [216.184.2.128]); Wed, 30 Sep 2020 06:00:34 -0600 (MDT) X-Virus-Scanned: clamav-milter 0.99.2 at ame8 X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ame8.swcp.com X-Spam-Status: No, hits=0.7 tests=NO_RECEIVED,NO_RELAYS,SPF_NEUTRAL version=3.4.1 X-Spam-Level: Content-Type: text/plain; charset="utf-8" This patch adds support for bundling the xen.efi hypervisor, the xen.cfg configuration file, the Linux kernel and initrd, as well as the XSM, and architectural specific files into a single "unified" EFI executable. This allows an administrator to update the components independently without requiring rebuilding xen, as well as to replace the components in an existing image. The resulting EFI executable can be invoked directly from the UEFI Boot Manager, removing the need to use a separate loader like grub as well as removing dependencies on local filesystem access. And since it is a single file, it can be signed and validated by UEFI Secure Boot without requring the shim protocol. It is inspired by systemd-boot's unified kernel technique and borrows the function to locate PE sections from systemd's LGPL'ed code. During EFI boot, Xen looks at its own loaded image to locate the PE sections for the Xen configuration (`.config`), dom0 kernel (`.kernel`), dom0 initrd (`.ramdisk`), and XSM config (`.xsm`), which are included after building xen.efi using objcopy to add named sections for each input file. For x86, the CPU ucode can be included in a section named `.ucode`, which is loaded in the efi_arch_cfg_file_late() stage of the boot process. On ARM systems the Device Tree can be included in a section named `.dtb`, which is loaded during the efi_arch_cfg_file_early() stage of the boot process. Note that the system will fall back to loading files from disk if the named sections do not exist. This allows distributions to continue with the status quo if they want a signed kernel + config, while still allowing a user provided initrd (which is how the shim protocol currently works as well). This patch also adds constness to the section parameter of efi_arch_cfg_file_early() and efi_arch_cfg_file_late(), changes pe_find_section() to use a const CHAR16 section name, and adds pe_name_compare() to match section names. Signed-off-by: Trammell Hudson Reviewed-by: Jan Beulich --- .gitignore | 1 + docs/misc/efi.pandoc | 49 ++++++++++++ xen/arch/arm/efi/efi-boot.h | 25 ++++-- xen/arch/x86/efi/Makefile | 2 +- xen/arch/x86/efi/efi-boot.h | 11 ++- xen/common/efi/boot.c | 64 ++++++++++----- xen/common/efi/efi.h | 3 + xen/common/efi/pe.c | 153 ++++++++++++++++++++++++++++++++++++ 8 files changed, 279 insertions(+), 29 deletions(-) create mode 100644 xen/common/efi/pe.c diff --git a/.gitignore b/.gitignore index 5e8c47e2db..2857fd76eb 100644 --- a/.gitignore +++ b/.gitignore @@ -317,6 +317,7 @@ xen/arch/*/efi/boot.c xen/arch/*/efi/compat.c xen/arch/*/efi/ebmalloc.c xen/arch/*/efi/efi.h +xen/arch/*/efi/pe.c xen/arch/*/efi/runtime.c xen/common/config_data.S xen/common/config.gz diff --git a/docs/misc/efi.pandoc b/docs/misc/efi.pandoc index 23c1a2732d..ac3cd58cae 100644 --- a/docs/misc/efi.pandoc +++ b/docs/misc/efi.pandoc @@ -116,3 +116,52 @@ Filenames must be specified relative to the location o= f the EFI binary. =20 Extra options to be passed to Xen can also be specified on the command lin= e, following a `--` separator option. + +## Unified Xen kernel image + +The "Unified" kernel image can be generated by adding additional +sections to the Xen EFI executable with objcopy, similar to how +[systemd-boot uses the stub to add them to the Linux kernel](https://wiki.= archlinux.org/index.php/systemd-boot#Preparing_a_unified_kernel_image) + +The sections for the xen configuration file, the dom0 kernel, dom0 initrd, +XSM and CPU microcode should be added after the Xen `.pad` section, the +ending address of which can be located with: + +``` +objdump -h xen.efi \ + | perl -ane '/\.pad/ && printf "0x%016x\n", hex($F[2]) + hex($F[3])' +``` + +For all the examples the `.pad` section ended at 0xffff82d041000000. +All the sections are optional (`.config`, `.kernel`, `.ramdisk`, `.xsm`, +`.ucode` (x86) and `.dtb` (ARM)) and the order does not matter. +The virtual addresses do not need to be contiguous, although they should n= ot +be overlapping and should all be greater than the last virtual address of = the +hypervisor components. + +``` +objcopy \ + --add-section .config=3Dxen.cfg \ + --change-section-vma .config=3D0xffff82d041000000 + --add-section .ucode=3Ducode.bin \ + --change-section-vma .ucode=3D0xffff82d041010000 \ + --add-section .xsm=3Dxsm.cfg \ + --change-section-vma .xsm=3D0xffff82d041080000 \ + --add-section .kernel=3Dvmlinux \ + --change-section-vma .kernel=3D0xffff82d041100000 \ + --add-section .ramdisk=3Dinitrd.img \ + --change-section-vma .ramdisk=3D0xffff82d042000000 \ + xen.efi \ + xen.unified.efi +``` + +The unified executable can be signed with sbsigntool to make +it usable with UEFI secure boot: + +``` +sbsign \ + --key signing.key \ + --cert cert.pem \ + --output xen.signed.efi \ + xen.unified.efi +``` diff --git a/xen/arch/arm/efi/efi-boot.h b/xen/arch/arm/efi/efi-boot.h index c6200fda0e..f64a6604af 100644 --- a/xen/arch/arm/efi/efi-boot.h +++ b/xen/arch/arm/efi/efi-boot.h @@ -382,27 +382,36 @@ static void __init noreturn efi_arch_post_exit_boot(v= oid) efi_xen_start(fdt, fdt_totalsize(fdt)); } =20 -static void __init efi_arch_cfg_file_early(EFI_FILE_HANDLE dir_handle, cha= r *section) +static void __init efi_arch_cfg_file_early(const EFI_LOADED_IMAGE *image, + EFI_FILE_HANDLE dir_handle, + const char *section) { union string name; =20 /* * The DTB must be processed before any other entries in the configura= tion - * file, as the DTB is updated as modules are loaded. + * file, as the DTB is updated as modules are loaded. Prefer the one + * stored as a PE section in a unified image, and fall back to a file + * on disk if the section is not present. */ - name.s =3D get_value(&cfg, section, "dtb"); - if ( name.s ) + if ( !read_section(image, L"dtb", &dtbfile, NULL) ) { - split_string(name.s); - read_file(dir_handle, s2w(&name), &dtbfile, NULL); - efi_bs->FreePool(name.w); + name.s =3D get_value(&cfg, section, "dtb"); + if ( name.s ) + { + split_string(name.s); + read_file(dir_handle, s2w(&name), &dtbfile, NULL); + efi_bs->FreePool(name.w); + } } fdt =3D fdt_increase_size(&dtbfile, cfg.size + EFI_PAGE_SIZE); if ( !fdt ) blexit(L"Unable to create new FDT"); } =20 -static void __init efi_arch_cfg_file_late(EFI_FILE_HANDLE dir_handle, char= *section) +static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image, + EFI_FILE_HANDLE dir_handle, + const char *section) { } =20 diff --git a/xen/arch/x86/efi/Makefile b/xen/arch/x86/efi/Makefile index 770438a029..e857c0f2cc 100644 --- a/xen/arch/x86/efi/Makefile +++ b/xen/arch/x86/efi/Makefile @@ -8,7 +8,7 @@ cmd_objcopy_o_ihex =3D $(OBJCOPY) -I ihex -O binary $< $@ =20 boot.init.o: buildid.o =20 -EFIOBJ :=3D boot.init.o ebmalloc.o compat.o runtime.o +EFIOBJ :=3D boot.init.o pe.init.o ebmalloc.o compat.o runtime.o =20 $(call cc-option-add,cflags-stack-boundary,CC,-mpreferred-stack-boundary= =3D4) $(EFIOBJ): CFLAGS-stack-boundary :=3D $(cflags-stack-boundary) diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h index 1025000afd..2541ba1f32 100644 --- a/xen/arch/x86/efi/efi-boot.h +++ b/xen/arch/x86/efi/efi-boot.h @@ -272,14 +272,21 @@ static void __init noreturn efi_arch_post_exit_boot(v= oid) unreachable(); } =20 -static void __init efi_arch_cfg_file_early(EFI_FILE_HANDLE dir_handle, cha= r *section) +static void __init efi_arch_cfg_file_early(const EFI_LOADED_IMAGE *image, + EFI_FILE_HANDLE dir_handle, + const char *section) { } =20 -static void __init efi_arch_cfg_file_late(EFI_FILE_HANDLE dir_handle, char= *section) +static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image, + EFI_FILE_HANDLE dir_handle, + const char *section) { union string name; =20 + if ( read_section(image, L"ucode", &ucode, NULL) ) + return; + name.s =3D get_value(&cfg, section, "ucode"); if ( !name.s ) name.s =3D get_value(&cfg, "global", "ucode"); diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index bd629eb658..072b1cecd1 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -122,6 +122,8 @@ static CHAR16 *s2w(union string *str); static char *w2s(const union string *str); static bool read_file(EFI_FILE_HANDLE dir_handle, CHAR16 *name, struct file *file, const char *options); +static bool read_section(const EFI_LOADED_IMAGE *image, const CHAR16 *name, + struct file *file, const char *options); static size_t wstrlen(const CHAR16 * s); static int set_color(u32 mask, int bpp, u8 *pos, u8 *sz); static bool match_guid(const EFI_GUID *guid1, const EFI_GUID *guid2); @@ -631,6 +633,20 @@ static bool __init read_file(EFI_FILE_HANDLE dir_handl= e, CHAR16 *name, return true; } =20 +static bool __init read_section(const EFI_LOADED_IMAGE *image, + const CHAR16 *name, struct file *file, + const char *options) +{ + file->ptr =3D pe_find_section(image->ImageBase, image->ImageSize, + name, &file->size); + if ( !file->ptr ) + return false; + + handle_file_info(name, file, options); + + return true; +} + static void __init pre_parse(const struct file *cfg) { char *ptr =3D cfg->str, *end =3D ptr + cfg->size; @@ -1215,9 +1231,11 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *= SystemTable) /* Get the file system interface. */ dir_handle =3D get_parent_handle(loaded_image, &file_name); =20 - /* Read and parse the config file. */ - if ( !cfg_file_name ) + if ( read_section(loaded_image, L"config", &cfg, NULL) ) + PrintStr(L"Using builtin config file\r\n"); + else if ( !cfg_file_name ) { + /* Read and parse the config file. */ CHAR16 *tail; =20 while ( (tail =3D point_tail(file_name)) !=3D NULL ) @@ -1266,29 +1284,39 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE = *SystemTable) if ( !name.s ) blexit(L"No Dom0 kernel image specified."); =20 - efi_arch_cfg_file_early(dir_handle, section.s); + efi_arch_cfg_file_early(loaded_image, dir_handle, section.s); =20 option_str =3D split_string(name.s); - read_file(dir_handle, s2w(&name), &kernel, option_str); - efi_bs->FreePool(name.w); - - if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL, - (void **)&shim_lock)) && - (status =3D shim_lock->Verify(kernel.ptr, kernel.size)) !=3D = EFI_SUCCESS ) - PrintErrMesg(L"Dom0 kernel image could not be verified", statu= s); =20 - name.s =3D get_value(&cfg, section.s, "ramdisk"); - if ( name.s ) + if ( !read_section(loaded_image, L"kernel", &kernel, option_str) ) { - read_file(dir_handle, s2w(&name), &ramdisk, NULL); + read_file(dir_handle, s2w(&name), &kernel, option_str); efi_bs->FreePool(name.w); + + if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL, + (void **)&shim_lock)) && + (status =3D shim_lock->Verify(kernel.ptr, kernel.size)) != =3D EFI_SUCCESS ) + PrintErrMesg(L"Dom0 kernel image could not be verified", s= tatus); } =20 - name.s =3D get_value(&cfg, section.s, "xsm"); - if ( name.s ) + if ( !read_section(loaded_image, L"ramdisk", &ramdisk, NULL) ) { - read_file(dir_handle, s2w(&name), &xsm, NULL); - efi_bs->FreePool(name.w); + name.s =3D get_value(&cfg, section.s, "ramdisk"); + if ( name.s ) + { + read_file(dir_handle, s2w(&name), &ramdisk, NULL); + efi_bs->FreePool(name.w); + } + } + + if ( !read_section(loaded_image, L"xsm", &xsm, NULL) ) + { + name.s =3D get_value(&cfg, section.s, "xsm"); + if ( name.s ) + { + read_file(dir_handle, s2w(&name), &xsm, NULL); + efi_bs->FreePool(name.w); + } } =20 /* @@ -1324,7 +1352,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *S= ystemTable) } } =20 - efi_arch_cfg_file_late(dir_handle, section.s); + efi_arch_cfg_file_late(loaded_image, dir_handle, section.s); =20 efi_bs->FreePages(cfg.addr, PFN_UP(cfg.size)); cfg.addr =3D 0; diff --git a/xen/common/efi/efi.h b/xen/common/efi/efi.h index 4845d84913..663a8b5000 100644 --- a/xen/common/efi/efi.h +++ b/xen/common/efi/efi.h @@ -47,3 +47,6 @@ const CHAR16 *wmemchr(const CHAR16 *s, CHAR16 c, UINTN n); /* EFI boot allocator. */ void *ebmalloc(size_t size); void free_ebmalloc_unused_mem(void); + +const void *pe_find_section(const void *image_base, const size_t image_siz= e, + const CHAR16 *section_name, UINTN *size_out); diff --git a/xen/common/efi/pe.c b/xen/common/efi/pe.c new file mode 100644 index 0000000000..5c1700be03 --- /dev/null +++ b/xen/common/efi/pe.c @@ -0,0 +1,153 @@ +/* + * xen/common/efi/pe.c + * + * PE executable header parser. + * + * Derived from https://github.com/systemd/systemd/blob/master/src/boot/ef= i/pe.c + * commit 07d5ed536ec0a76b08229c7a80b910cb9acaf6b1 + * + * Copyright (C) 2015 Kay Sievers + * Copyright (C) 2020 Trammell Hudson + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + + +#include "efi.h" + +struct DosFileHeader { + UINT8 Magic[2]; + UINT16 LastSize; + UINT16 nBlocks; + UINT16 nReloc; + UINT16 HdrSize; + UINT16 MinAlloc; + UINT16 MaxAlloc; + UINT16 ss; + UINT16 sp; + UINT16 Checksum; + UINT16 ip; + UINT16 cs; + UINT16 RelocPos; + UINT16 nOverlay; + UINT16 reserved[4]; + UINT16 OEMId; + UINT16 OEMInfo; + UINT16 reserved2[10]; + UINT32 ExeHeader; +}; + +#if defined(__arm__) || defined (__aarch64__) +#define PE_HEADER_MACHINE 0xaa64 +#elif defined(__x86_64__) +#define PE_HEADER_MACHINE 0x8664 +#else +#error "Unknown architecture" +#endif + +struct PeFileHeader { + UINT16 Machine; + UINT16 NumberOfSections; + UINT32 TimeDateStamp; + UINT32 PointerToSymbolTable; + UINT32 NumberOfSymbols; + UINT16 SizeOfOptionalHeader; + UINT16 Characteristics; +}; + +struct PeHeader { + UINT8 Magic[4]; + struct PeFileHeader FileHeader; +}; + +struct PeSectionHeader { + CHAR8 Name[8]; + UINT32 VirtualSize; + UINT32 VirtualAddress; + UINT32 SizeOfRawData; + UINT32 PointerToRawData; + UINT32 PointerToRelocations; + UINT32 PointerToLinenumbers; + UINT16 NumberOfRelocations; + UINT16 NumberOfLinenumbers; + UINT32 Characteristics; +}; + +static bool __init pe_name_compare(const struct PeSectionHeader *sect, + const CHAR16 *name) +{ + size_t i; + + if ( sect->Name[0] !=3D '.' ) + return -1; + + for ( i =3D 1; i < sizeof(sect->Name); i++ ) + { + const char c =3D sect->Name[i]; + const CHAR16 cw =3D name[i - 1]; + + if ( cw =3D=3D L'\0' && c =3D=3D '\0' ) + return true; + if ( cw !=3D c ) + return false; + } + + return name[i - 1] =3D=3D L'\0'; +} + +const void *__init pe_find_section(const void *image, const UINTN image_si= ze, + const CHAR16 *section_name, UINTN *size= _out) +{ + const struct DosFileHeader *dos =3D image; + const struct PeHeader *pe; + const struct PeSectionHeader *sect; + UINTN offset, i; + + if ( image_size < sizeof(*dos) || + memcmp(dos->Magic, "MZ", 2) !=3D 0 ) + return NULL; + + offset =3D dos->ExeHeader; + pe =3D image + offset; + + offset +=3D sizeof(*pe); + if ( image_size < offset || + memcmp(pe->Magic, "PE\0\0", 4) !=3D 0 ) + return NULL; + + if ( pe->FileHeader.Machine !=3D PE_HEADER_MACHINE ) + return NULL; + + offset +=3D pe->FileHeader.SizeOfOptionalHeader; + + for ( i =3D 0; i < pe->FileHeader.NumberOfSections; i++ ) + { + sect =3D image + offset; + if ( image_size < offset + sizeof(*sect) ) + return NULL; + + if ( !pe_name_compare(sect, section_name) ) + { + offset +=3D sizeof(*sect); + continue; + } + + if ( image_size < sect->VirtualSize + sect->VirtualAddress ) + blexit(L"PE invalid section size + address"); + + if ( size_out ) + *size_out =3D sect->VirtualSize; + + return image + sect->VirtualAddress; + } + + return NULL; +} --=20 2.25.1 From nobody Thu May 2 03:18:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=trmm.net ARC-Seal: i=1; a=rsa-sha256; t=1601467260; cv=none; d=zohomail.com; s=zohoarc; b=RfRXlBKxbW4xGBpegGYjc2Bbifj9o77A+2x+wNhxSNJ8R9QbH3a8LwQDFxvz4nqmTDfELNoY451QXVL1wYXqF8s9ych0POrjaUd9eXsPe/GLotK+2TR2nGPxVXxQU8VUtMwmnH0GrKNBZiG+S/yvK8+0+2WHP6OWIIy4mOyWh2A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601467260; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dlDLOL2FDQH/qQvnMCGaI6C/u/n0I3kfIDkK5vBFrbg=; b=OJSIPRzBiDHGG93ogpEF4zR4U7RSECY1ypPaMSavm8yI5Gx9EhCgSAc12JjIlz16mz2lyoWzAvFdB1hPb7kElFsh/gtsrmJJrdxnLi2kopcZuCRawrtYXnrx53PKXVtc6xQ75TIZxU0TvBQqytrt6tONRXoZsLIf2/UZg5apD94= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1601467260872391.6732407431281; Wed, 30 Sep 2020 05:01:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.630.2125 (Exim 4.92) (envelope-from ) id 1kNamq-0001tq-Nt; Wed, 30 Sep 2020 12:00:44 +0000 Received: by outflank-mailman (output) from mailman id 630.2125; Wed, 30 Sep 2020 12:00:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamq-0001tb-Ji; Wed, 30 Sep 2020 12:00:44 +0000 Received: by outflank-mailman (input) for mailman id 630; Wed, 30 Sep 2020 12:00:43 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamp-0001mg-BR for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:43 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 3235fc8e-2f0f-49ad-87ab-7f89c1a625df; Wed, 30 Sep 2020 12:00:42 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0fgP005394; Wed, 30 Sep 2020 06:00:41 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdG003035; Wed, 30 Sep 2020 06:00:37 -0600 (MDT) (envelope-from hudson@trmm.net) Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kNamp-0001mg-BR for xen-devel@lists.xenproject.org; Wed, 30 Sep 2020 12:00:43 +0000 Received: from mx1a.swcp.com (unknown [216.184.2.64]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 3235fc8e-2f0f-49ad-87ab-7f89c1a625df; Wed, 30 Sep 2020 12:00:42 +0000 (UTC) Received: from ame8.swcp.com (ame8.swcp.com [216.184.2.163]) by mx1a.swcp.com (8.14.4/8.14.4/Debian-4) with ESMTP id 08UC0fgP005394; Wed, 30 Sep 2020 06:00:41 -0600 Received: from diamond.fritz.box (62-251-112-184.ip.xs4all.nl [62.251.112.184]) by ame8.swcp.com (8.15.2/8.15.2) with ESMTP id 08UC0BdG003035; Wed, 30 Sep 2020 06:00:37 -0600 (MDT) (envelope-from hudson@trmm.net) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3235fc8e-2f0f-49ad-87ab-7f89c1a625df Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: neutral (ame8.swcp.com: 62.251.112.184 is neither permitted nor denied by domain of hudson@trmm.net) receiver=ame8.swcp.com; client-ip=62.251.112.184; helo=diamond.fritz.box; envelope-from=hudson@trmm.net; x-software=spfmilter 2.001 http://www.acme.com/software/spfmilter/ with libspf2-1.2.10; X-Authentication-Warning: ame8.swcp.com: Host 62-251-112-184.ip.xs4all.nl [62.251.112.184] claimed to be diamond.fritz.box From: Trammell Hudson To: xen-devel@lists.xenproject.org Cc: roger.pau@citrix.com, jbeulich@suse.com, andrew.cooper3@citrix.com, wl@xen.org Subject: [PATCH v8 5/5] efi: Do not use command line if unified config is included Date: Wed, 30 Sep 2020 08:00:11 -0400 Message-Id: <20200930120011.1622924-6-hudson@trmm.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200930120011.1622924-1-hudson@trmm.net> References: <20200930120011.1622924-1-hudson@trmm.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 X-Greylist: Message whitelisted by DRAC access database, not delayed by milter-greylist-4.6.2 (ame8.swcp.com [216.184.2.128]); Wed, 30 Sep 2020 06:00:39 -0600 (MDT) X-Virus-Scanned: clamav-milter 0.99.2 at ame8 X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ame8.swcp.com X-Spam-Status: No, hits=0.7 tests=NO_RECEIVED,NO_RELAYS,SPF_NEUTRAL version=3.4.1 X-Spam-Level: Content-Type: text/plain; charset="utf-8" If a unified Xen image is used, then the bundled configuration, Xen command line, dom0 kernel, and ramdisk are prefered over any files listed in the config file or provided on the command line. Unlike the shim based verification, the PE signature on a unified image covers all of the Xen+config+kernel+initrd modules linked into the unified image. This also ensures that, on properly configured UEFI Secure B= oot platforms, the entire runtime will be measured into the TPM for unsealing secrets or remote attestation. Signed-off-by: Trammell Hudson --- xen/common/efi/boot.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index 072b1cecd1..07ea5b3512 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -950,6 +950,35 @@ static void __init setup_efi_pci(void) efi_bs->FreePool(handles); } =20 +/* + * Logic should remain sync'ed with linux/arch/x86/xen/efi.c + * Secure Boot is enabled iff 'SecureBoot' is set and the system is + * not in Setup Mode. + */ +static bool __init efi_secure_boot(void) +{ + static __initdata EFI_GUID global_guid =3D EFI_GLOBAL_VARIABLE; + uint8_t secboot, setupmode; + UINTN secboot_size =3D sizeof(secboot); + UINTN setupmode_size =3D sizeof(setupmode); + EFI_STATUS rc; + + rc =3D efi_rs->GetVariable(L"SecureBoot", &global_guid, + NULL, &secboot_size, &secboot); + if ( rc !=3D EFI_SUCCESS ) + return false; + + rc =3D efi_rs->GetVariable(L"SetupMode", &global_guid, + NULL, &setupmode_size, &setupmode); + if ( rc !=3D EFI_SUCCESS ) + return false; + + if ( secboot > 1 || setupmode > 1 ) + blexit(L"Invalid SecureBoot/SetupMode variables"); + + return secboot =3D=3D 1 && setupmode =3D=3D 0; +} + static void __init efi_variables(void) { EFI_STATUS status; @@ -1126,15 +1155,15 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE = *SystemTable) static EFI_GUID __initdata shim_lock_guid =3D SHIM_LOCK_PROTOCOL_GUID; EFI_LOADED_IMAGE *loaded_image; EFI_STATUS status; - unsigned int i, argc; - CHAR16 **argv, *file_name, *cfg_file_name =3D NULL, *options =3D NULL; + unsigned int i, argc =3D 0; + CHAR16 **argv =3D NULL, *file_name, *cfg_file_name =3D NULL, *options = =3D NULL; UINTN gop_mode =3D ~0; EFI_SHIM_LOCK_PROTOCOL *shim_lock; EFI_GRAPHICS_OUTPUT_PROTOCOL *gop =3D NULL; union string section =3D { NULL }, name; bool base_video =3D false; const char *option_str; - bool use_cfg_file; + bool use_cfg_file, secure; =20 __set_bit(EFI_BOOT, &efi_flags); __set_bit(EFI_LOADER, &efi_flags); @@ -1153,8 +1182,10 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *= SystemTable) PrintErrMesg(L"No Loaded Image Protocol", status); =20 efi_arch_load_addr_check(loaded_image); + secure =3D efi_secure_boot(); =20 - if ( use_cfg_file ) + if ( use_cfg_file && + !read_section(loaded_image, L"config", &cfg, NULL) ) { UINTN offset =3D 0; =20 @@ -1212,6 +1243,8 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *S= ystemTable) =20 PrintStr(L"Xen " __stringify(XEN_VERSION) "." __stringify(XEN_SUBVERSI= ON) XEN_EXTRAVERSION " (c/s " XEN_CHANGESET ") EFI loader\r\n"); + if ( secure ) + PrintStr(L"UEFI Secure Boot enabled\r\n"); =20 efi_arch_relocate_image(0); =20 @@ -1231,7 +1264,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *S= ystemTable) /* Get the file system interface. */ dir_handle =3D get_parent_handle(loaded_image, &file_name); =20 - if ( read_section(loaded_image, L"config", &cfg, NULL) ) + if ( cfg.ptr ) PrintStr(L"Using builtin config file\r\n"); else if ( !cfg_file_name ) { --=20 2.25.1