From patchwork Thu Dec 15 12:38:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeniy Baskov X-Patchwork-Id: 33630 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp328010wrn; Thu, 15 Dec 2022 04:44:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf51egs+itBGDlV/dXwjA7mOmJMR5JREHcB7q3HTo1Wy4CG82XToE8HRrXncg0/OW80nojis X-Received: by 2002:a05:6402:370a:b0:461:2505:832d with SMTP id ek10-20020a056402370a00b004612505832dmr32610751edb.10.1671108247575; Thu, 15 Dec 2022 04:44:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671108247; cv=none; d=google.com; s=arc-20160816; b=qjMFu6P4E7c76JFKZCE7ZmSI6crBl8yn9OZOw7E3OBxQ7jU45rzYcqkLm9IdzW5B0S e0zjZF0NIAwvMbtV84AtV6WOEJxEs1fSV0jOJwML1+9x3cmimmS17VCDn9onc75Fnw/s pCRzluep+z4r8UMTJ3A1fyRqN3ZGFFzcVPoqE/NjG9W0BdE5wDfHx7JX705N51kh5ZSI AiQOg5I2h4BPEn+y4twyCSWLIOdM++Hy0+qBBC+BPa277f3kbEtOMO7PL4n/6ZAy4M5Z fkNr5M0sB+I13GGv2fCCzMOdYF/db6I57KZwWdK6L3EstlTgx9+pKvzmarl343sJ3Yep I1EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=IpU0tjNymTHz9fJiOLjTQ5CfC4DkOKpdBU+NiXX6lIQ=; b=WWRlo1VdOsvK8L+pdzcs372SEZf3qD0woKEJEhADE1p7f64uGO79ngUENyll4vQ0sZ oH9jr5Fn+knBiyXcOayYyU/YzVyYgsru+rLYObZKeMV9MF4GEGGhFK/ta+TZHsFK0Zfm 5QmYtJjqObd424Q8m9HS67TBJkKcPQ2v+bh+YSyrwGJB+CJi114BbdnWlXtBpfM/Ct36 L+UdRI7hPcno6HzPfnaQdbRJpQc71SKik4Rq0kcW0ondkkoZ+nulWpMwoBwjMoVQuy8k MpBd9e6JCJkq9tZSKnyKRgQucTU2qsE8fZj0OCM+0TIeUUZ77WiwTK2Gnar0bbM4IszQ OUAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=cEKjvhTi; 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=NONE dis=NONE) header.from=ispras.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i2-20020aa7c702000000b0046c46bc5c9csi13769962edq.327.2022.12.15.04.43.41; Thu, 15 Dec 2022 04:44:07 -0800 (PST) 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=@ispras.ru header.s=default header.b=cEKjvhTi; 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=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229844AbiLOMnF (ORCPT + 99 others); Thu, 15 Dec 2022 07:43:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229806AbiLOMm2 (ORCPT ); Thu, 15 Dec 2022 07:42:28 -0500 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D03DF06; Thu, 15 Dec 2022 04:42:28 -0800 (PST) Received: from localhost.localdomain (unknown [83.149.199.65]) by mail.ispras.ru (Postfix) with ESMTPSA id 365E140737A3; Thu, 15 Dec 2022 12:42:26 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 365E140737A3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1671108146; bh=IpU0tjNymTHz9fJiOLjTQ5CfC4DkOKpdBU+NiXX6lIQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cEKjvhTiUKC/j3Emhuk9OQ4EdoQy7BjYYNtoIQbwX1GYrPCES0YmjcPMs/QlaqLTk yiffNspkkAAfp10zvnOms8EKuT/YwiYDDytk+5TO1hiWIvTjerfrP5EoZM7YMsQmhC Czbs+fvh7m+unlip1AyPmssh7gW2kpmQTdWwvtFU= From: Evgeniy Baskov To: Ard Biesheuvel Cc: Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , "Limonciello, Mario" , joeyli , lvc-project@linuxtesting.org, x86@kernel.org, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4 21/26] efi/x86: Explicitly set sections memory attributes Date: Thu, 15 Dec 2022 15:38:12 +0300 Message-Id: X-Mailer: git-send-email 2.37.4 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752284001603129316?= X-GMAIL-MSGID: =?utf-8?q?1752284001603129316?= Explicitly change sections memory attributes in efi_pe_entry in case of incorrect EFI implementations and to reduce access rights to compressed kernel blob. By default it is set executable due to restriction in maximum number of sections that can fit before zero page. Tested-by: Peter Jones Signed-off-by: Evgeniy Baskov --- drivers/firmware/efi/libstub/x86-stub.c | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 1f0a2e7075c3..60697fcd8950 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -27,6 +27,12 @@ const efi_dxe_services_table_t *efi_dxe_table; u32 image_offset __section(".data"); static efi_loaded_image_t *image __section(".data"); +extern char _head[], _ehead[]; +extern char _compressed[], _ecompressed[]; +extern char _text[], _etext[]; +extern char _rodata[], _erodata[]; +extern char _data[]; + static efi_status_t preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) { @@ -343,6 +349,52 @@ void __noreturn efi_exit(efi_handle_t handle, efi_status_t status) asm("hlt"); } + +/* + * Manually setup memory protection attributes for each ELF section + * since we cannot do it properly by using PE sections. + */ +static void setup_sections_memory_protection(unsigned long image_base) +{ +#ifdef 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("Unable to locate EFI DXE services table\n"); + efi_dxe_table = NULL; + return; + } + + /* .setup [image_base, _head] */ + efi_adjust_memory_range_protection(image_base, + (unsigned long)_head - image_base, + EFI_MEMORY_RO | EFI_MEMORY_XP); + /* .head.text [_head, _ehead] */ + efi_adjust_memory_range_protection((unsigned long)_head, + (unsigned long)_ehead - (unsigned long)_head, + EFI_MEMORY_RO); + /* .rodata..compressed [_compressed, _ecompressed] */ + efi_adjust_memory_range_protection((unsigned long)_compressed, + (unsigned long)_ecompressed - (unsigned long)_compressed, + EFI_MEMORY_RO | EFI_MEMORY_XP); + /* .text [_text, _etext] */ + efi_adjust_memory_range_protection((unsigned long)_text, + (unsigned long)_etext - (unsigned long)_text, + EFI_MEMORY_RO); + /* .rodata [_rodata, _erodata] */ + efi_adjust_memory_range_protection((unsigned long)_rodata, + (unsigned long)_erodata - (unsigned long)_rodata, + EFI_MEMORY_RO | EFI_MEMORY_XP); + /* .data, .bss [_data, _end] */ + efi_adjust_memory_range_protection((unsigned long)_data, + (unsigned long)_end - (unsigned long)_data, + EFI_MEMORY_XP); +#else + (void)image_base; +#endif +} + void __noreturn efi_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, struct boot_params *boot_params); @@ -687,6 +739,8 @@ asmlinkage unsigned long efi_main(efi_handle_t handle, efi_dxe_table = NULL; } + setup_sections_memory_protection(bzimage_addr - image_offset); + #ifdef CONFIG_CMDLINE_BOOL status = efi_parse_options(CONFIG_CMDLINE); if (status != EFI_SUCCESS) {