Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp948797rwb; Thu, 15 Dec 2022 04:45:13 -0800 (PST) X-Google-Smtp-Source: AMrXdXsjx+h2DMTj7D0cwjf5BQabjSj5JIBrtimNbYc7NIqOfpHTsvac2FkPcLXkkGFZeg5kVNGp X-Received: by 2002:a50:fc05:0:b0:46f:9a53:fdcc with SMTP id i5-20020a50fc05000000b0046f9a53fdccmr5310469edr.12.1671108312937; Thu, 15 Dec 2022 04:45:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671108312; cv=none; d=google.com; s=arc-20160816; b=vYlAszg9N4RS9fVIAsRp9GJwE4mXc7dz4Kcjtf27KtK5Iaq0la6s5cpMeWPO9dxjNc kKObuqER/q7/Q+zS2AWjmXcpnFVHg0c6aVvucDUSAfAWBrKSke5DqAazPiMrD2o2JfKb GR5ye1rTIrhGkNAevk2zUQ0OeBvg4roGZGjXx78rsxzGMG5fQDWDG0S/mCuquUu+rgIU Zrwn22WyU+dqmPa/JDi4Cv/U/p/d77/u3ZuK+G8vnGnYTrC8vSI8gnOZcMIN3Zabmc3o 9CRQMhPuOUitqddLyN9VBrvSSkxmCctvU8CxrFwqtDVKbqbYkXylzDJ+ecojdqX2U18f v8MQ== 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 Return-Path: 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.44.56; Thu, 15 Dec 2022 04:45:12 -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 + 68 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 Content-Transfer-Encoding: 8bit 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 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) { -- 2.37.4