Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4599717rwb; Tue, 8 Aug 2023 10:45:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHpT08i3stcaHYR4OEIr6a6OG7vNSsYHN4lqTag7ta+5nRhFMqoG9xqUAoiGStQDPPwkgz3 X-Received: by 2002:a17:902:c3c3:b0:1bb:bb70:c23e with SMTP id j3-20020a170902c3c300b001bbbb70c23emr372528plj.18.1691516706110; Tue, 08 Aug 2023 10:45:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691516706; cv=none; d=google.com; s=arc-20160816; b=T8GzplgxgQgfk1pTQ/VOOLODnTGjHOR7uCJnRC4B4kedeMYNMDOY+y0d0B7h5jSC9W 30QLb+AAVh0s0s9HAbRwSUocLm9ODF+8Bcbzwh8ccRW3h0ne6lDOW3jviII1Q10C5omW 1MS4QMFokR7tTWVW2jbhxEjtE8/1LUGVc0mbJ0ljX2kxM4AhFcdQ9M/6pZIaZrJQj4NR 7rfE+kHVfUN6tXK2hiXgL0wl7M4wCurLvNhJU7dTLMJfmRm/xT9jwBbUQk3Anvlg8D1K AqOO0zXO78kwcPAM5NO89Pd35ERh3LoMua7LZiP1X8+1yUUGN1eOtP/IF7DCENaqSwrp ljww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=M0BvrQvqMSSJHj4LC7zMyeoB/NTCNR6U66Wzhqm4D/M=; fh=upm4t26Ojn/J6N+Q9k8Qqzx52VymNDAlYlARzLeNh5s=; b=YeDWr1OE0OIFPPVX5vlA4n8YX/XMnFEJusNAhNQh4HHpc+8UpQeFbgYf2x9KVF1b99 Xvy8CXzDjcNSkndxtPHesDuMZHH3+G3YOGxkM9wJi0LOoYT4ls9AVvkTz/EV9B6E+UJz vMN7YIcKOjeroH34gUDJeLSKmT8guvJcxkWk64xkyJW5G9zF8qv4MUKx2QHfpsVA5oAF ur3F1kFn9hkJYt1IeIRbh/TdSclxmid/n5aiShsKJ3ZJYsFLqLNPX4LxrIw5OJfA0ykC rk3033wy6QwIVIvBtpmXVlRF5pJd0PqgjpyeOFIDMyWaDr92rr5KyjMA/8ZPMW8uIQyw YLIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=f5pE9sGZ; dkim=neutral (no key) header.i=@linutronix.de header.b=X3iWX8SX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ku15-20020a170903288f00b001b5589848absi7661399plb.234.2023.08.08.10.44.53; Tue, 08 Aug 2023 10:45:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=f5pE9sGZ; dkim=neutral (no key) header.i=@linutronix.de header.b=X3iWX8SX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233087AbjHHQfG (ORCPT + 99 others); Tue, 8 Aug 2023 12:35:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233056AbjHHQeK (ORCPT ); Tue, 8 Aug 2023 12:34:10 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 461869096; Tue, 8 Aug 2023 08:52:26 -0700 (PDT) Date: Tue, 08 Aug 2023 08:44:00 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691484240; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M0BvrQvqMSSJHj4LC7zMyeoB/NTCNR6U66Wzhqm4D/M=; b=f5pE9sGZlytbtraJj8zd82nYJ8nFp0h4xWNsN2eIVrN1Sl9AB0yJ8hFCSYMARfFMigVeZI qzlH0RvgpYLxki3qlgk/Zn0ebu5Im47k8mpSCjoWDzRMYgaey1arxpYRneG/is71S1EQs3 bOST4thZhRnoGSFCdE6E07278dv/QU9klhrC4T01GtNvS3gvbfH+Gy2VLjdMAD+IPSgHFH aPWtV1FaP/5525mLn1B6kgAatYrKYkTsiFsNYLmu1YSeH9mnyrsX7tbWdgyQf1FPNEmpbg IpBKgm81seRWaJsouJVJoSbJ80zvcumHRoWeYXCfsl0a7TagnDfgD817k4Bqtw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691484240; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M0BvrQvqMSSJHj4LC7zMyeoB/NTCNR6U66Wzhqm4D/M=; b=X3iWX8SX4DkCUCCAlbLopOZjFDMElh4lxVCWYZZx+1oEvpmwKfvCoyaf1QCUQFUbc8IBy2 w8/XgtdOY/FasTAQ== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] x86/efistub: Prefer EFI memory attributes protocol over DXE services Cc: Ard Biesheuvel , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230807162720.545787-18-ardb@kernel.org> References: <20230807162720.545787-18-ardb@kernel.org> MIME-Version: 1.0 Message-ID: <169148424042.27769.129666689958946834.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/boot branch of tip: Commit-ID: 11078876b7a6a1b7226344fecab968945c806832 Gitweb: https://git.kernel.org/tip/11078876b7a6a1b7226344fecab968945c806832 Author: Ard Biesheuvel AuthorDate: Mon, 07 Aug 2023 18:27:14 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 07 Aug 2023 20:54:15 +02:00 x86/efistub: Prefer EFI memory attributes protocol over DXE services Currently, the EFI stub relies on DXE services in some cases to clear non-execute restrictions from page allocations that need to be executable. This is dodgy, because DXE services are not specified by UEFI but by PI, and they are not intended for consumption by OS loaders. However, no alternative existed at the time. Now, there is a new UEFI protocol that should be used instead, so if it exists, prefer it over the DXE services calls. Signed-off-by: Ard Biesheuvel Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230807162720.545787-18-ardb@kernel.org --- drivers/firmware/efi/libstub/x86-stub.c | 29 +++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index af5f506..acb1c65 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -26,6 +26,7 @@ const efi_system_table_t *efi_system_table; const efi_dxe_services_table_t *efi_dxe_table; u32 image_offset __section(".data"); static efi_loaded_image_t *image = NULL; +static efi_memory_attribute_protocol_t *memattr; typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t; union sev_memory_acceptance_protocol { @@ -233,12 +234,18 @@ void efi_adjust_memory_range_protection(unsigned long start, unsigned long rounded_start, rounded_end; unsigned long unprotect_start, unprotect_size; - if (efi_dxe_table == NULL) - return; - rounded_start = rounddown(start, EFI_PAGE_SIZE); rounded_end = roundup(start + size, EFI_PAGE_SIZE); + if (memattr != NULL) { + efi_call_proto(memattr, clear_memory_attributes, rounded_start, + rounded_end - rounded_start, EFI_MEMORY_XP); + return; + } + + if (efi_dxe_table == NULL) + return; + /* * Don't modify memory region attributes, they are * already suitable, to lower the possibility to @@ -801,6 +808,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, struct boot_params *boot_params) { + efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID; unsigned long bzimage_addr = (unsigned long)startup_32; unsigned long buffer_start, buffer_end; struct setup_header *hdr = &boot_params->hdr; @@ -812,13 +820,18 @@ void __noreturn efi_stub_entry(efi_handle_t handle, if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) efi_exit(handle, EFI_INVALID_PARAMETER); - efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); - if (efi_dxe_table && - efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) { - efi_warn("Ignoring DXE services table: invalid signature\n"); - efi_dxe_table = NULL; + if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) { + efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); + if (efi_dxe_table && + efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) { + efi_warn("Ignoring DXE services table: invalid signature\n"); + efi_dxe_table = NULL; + } } + /* grab the memory attributes protocol if it exists */ + efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr); + status = efi_setup_5level_paging(); if (status != EFI_SUCCESS) { efi_err("efi_setup_5level_paging() failed!\n");