linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Evgeniy Baskov <baskov@ispras.ru>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>, Andy Lutomirski <luto@kernel.org>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Alexey Khoroshilov <khoroshilov@ispras.ru>,
	Peter Jones <pjones@redhat.com>,
	"Limonciello, Mario" <mario.limonciello@amd.com>,
	joeyli <jlee@suse.com>,
	lvc-project@linuxtesting.org, x86@kernel.org,
	linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH v4 21/26] efi/x86: Explicitly set sections memory attributes
Date: Sat, 11 Mar 2023 18:09:33 +0300	[thread overview]
Message-ID: <f29b8efc018819cbb8202d13795ba89e@ispras.ru> (raw)
In-Reply-To: <CAMj1kXGzXLp20nbg-NoToENbDQhn1b0Gpi2s8f9DgSSM28BbeQ@mail.gmail.com>

On 2023-03-10 18:20, Ard Biesheuvel wrote:
> On Thu, 15 Dec 2022 at 13:42, Evgeniy Baskov <baskov@ispras.ru> wrote:
>> 
>> 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 <pjones@redhat.com>
>> Signed-off-by: Evgeniy Baskov <baskov@ispras.ru>
> 
> I don't think we need this patch. Firmware that cares about W^X will
> map the PE image with R-X for text/rodata and RW- for data/bss, which
> is sufficient, and firmware that doesn't is a lost cause anyway.

This patch were here mainly here to make .rodata non-executable and for
the UEFI handover protocol, for which attributes are usually not getting
applied.

Since the UEFI handover protocol is deprecated, I'll exclude patches 
from
v5 and maybe submit it separately modified to apply attributes only when
booting via this protocol.

> 
> 
>> ---
>>  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
>> 

  reply	other threads:[~2023-03-11 15:09 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-15 12:37 [PATCH v4 00/26] x86_64: Improvements at compressed kernel stage Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 01/26] x86/boot: Align vmlinuz sections on page size Evgeniy Baskov
2023-03-10 14:43   ` Ard Biesheuvel
2023-03-11 14:30     ` Evgeniy Baskov
2023-03-11 14:42       ` Ard Biesheuvel
2022-12-15 12:37 ` [PATCH v4 02/26] x86/build: Remove RWX sections and align on 4KB Evgeniy Baskov
2023-03-10 14:45   ` Ard Biesheuvel
2023-03-11 14:31     ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 03/26] x86/boot: Set cr0 to known state in trampoline Evgeniy Baskov
2023-03-10 14:48   ` Ard Biesheuvel
2022-12-15 12:37 ` [PATCH v4 04/26] x86/boot: Increase boot page table size Evgeniy Baskov
2023-03-08  9:24   ` Ard Biesheuvel
2022-12-15 12:37 ` [PATCH v4 05/26] x86/boot: Support 4KB pages for identity mapping Evgeniy Baskov
2023-03-08  9:42   ` Ard Biesheuvel
2023-03-08 16:11     ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 06/26] x86/boot: Setup memory protection for bzImage code Evgeniy Baskov
2023-03-08 10:47   ` Ard Biesheuvel
2023-03-08 16:15     ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 07/26] x86/build: Check W^X of vmlinux during build Evgeniy Baskov
2023-03-08  9:34   ` Ard Biesheuvel
2023-03-08 16:05     ` Evgeniy Baskov
2022-12-15 12:37 ` [PATCH v4 08/26] x86/boot: Map memory explicitly Evgeniy Baskov
2023-03-08  9:38   ` Ard Biesheuvel
2023-03-08 10:28     ` Ard Biesheuvel
2023-03-08 16:09       ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 09/26] x86/boot: Remove mapping from page fault handler Evgeniy Baskov
2023-03-10 14:49   ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 10/26] efi/libstub: Move helper function to related file Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 11/26] x86/boot: Make console interface more abstract Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 12/26] x86/boot: Make kernel_add_identity_map() a pointer Evgeniy Baskov
2023-03-10 14:52   ` Ard Biesheuvel
2023-03-11 14:34     ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 13/26] x86/boot: Split trampoline and pt init code Evgeniy Baskov
2023-03-10 14:56   ` Ard Biesheuvel
2023-03-11 14:37     ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 14/26] x86/boot: Add EFI kernel extraction interface Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 15/26] efi/x86: Support extracting kernel from libstub Evgeniy Baskov
2023-03-09 16:00   ` Ard Biesheuvel
2023-03-09 17:05     ` Evgeniy Baskov
2023-03-09 16:49   ` Ard Biesheuvel
2023-03-09 17:10     ` Evgeniy Baskov
2023-03-09 17:11       ` Ard Biesheuvel
2023-03-10 15:08   ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 16/26] x86/boot: Reduce lower limit of physical KASLR Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 17/26] x86/boot: Reduce size of the DOS stub Evgeniy Baskov
2023-03-10 14:59   ` Ard Biesheuvel
2023-03-11 14:49     ` Evgeniy Baskov
2023-03-11 17:27       ` Ard Biesheuvel
2023-03-12 12:10         ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 18/26] tools/include: Add simplified version of pe.h Evgeniy Baskov
2023-03-10 15:01   ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 19/26] x86/build: Cleanup tools/build.c Evgeniy Baskov
2023-03-09 15:57   ` Ard Biesheuvel
2023-03-09 16:25     ` Evgeniy Baskov
2023-03-09 16:50       ` Ard Biesheuvel
2023-03-09 17:22         ` Evgeniy Baskov
2023-03-09 17:37           ` Ard Biesheuvel
2022-12-15 12:38 ` [PATCH v4 20/26] x86/build: Make generated PE more spec compliant Evgeniy Baskov
2023-03-10 15:17   ` Ard Biesheuvel
2023-03-11 15:02     ` Evgeniy Baskov
2023-03-11 17:31       ` Ard Biesheuvel
2023-03-12 12:01         ` Evgeniy Baskov
2023-03-12 13:09           ` Ard Biesheuvel
2023-03-13  9:11             ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 21/26] efi/x86: Explicitly set sections memory attributes Evgeniy Baskov
2023-03-10 15:20   ` Ard Biesheuvel
2023-03-11 15:09     ` Evgeniy Baskov [this message]
2023-03-11 17:39       ` Ard Biesheuvel
2023-03-12 12:10         ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 22/26] efi/libstub: Add memory attribute protocol definitions Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 23/26] efi/libstub: Use memory attribute protocol Evgeniy Baskov
2023-03-10 16:13   ` Ard Biesheuvel
2023-03-11 15:14     ` Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 24/26] efi/libstub: make memory protection warnings include newlines Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 25/26] efi/x86: don't try to set page attributes on 0-sized regions Evgeniy Baskov
2022-12-15 12:38 ` [PATCH v4 26/26] efi/x86: don't set unsupported memory attributes Evgeniy Baskov
2022-12-15 19:21 ` [PATCH v4 00/26] x86_64: Improvements at compressed kernel stage Peter Jones
2022-12-19 14:08   ` Evgeniy Baskov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f29b8efc018819cbb8202d13795ba89e@ispras.ru \
    --to=baskov@ispras.ru \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=jlee@suse.com \
    --cc=khoroshilov@ispras.ru \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=mario.limonciello@amd.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pjones@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).