From: Ard Biesheuvel <ardb@kernel.org>
To: linux-efi@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
Evgeniy Baskov <baskov@ispras.ru>, Borislav Petkov <bp@alien8.de>,
Alexey Khoroshilov <khoroshilov@ispras.ru>,
Peter Jones <pjones@redhat.com>,
"Limonciello, Mario" <mario.limonciello@amd.com>
Subject: [RFC PATCH 4/4] efi: x86: Split PE/COFF .text section into .text and .data
Date: Wed, 8 Mar 2023 21:22:09 +0100 [thread overview]
Message-ID: <20230308202209.2980947-5-ardb@kernel.org> (raw)
In-Reply-To: <20230308202209.2980947-1-ardb@kernel.org>
Modern PE loader implementations used by EFI will honour the PE section
permission attributes, and so we can use them to avoid mappings that are
writable and executable at the same time.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
| 17 ++++++++++++++++
arch/x86/boot/tools/build.c | 21 +++++++++++++++-----
2 files changed, 33 insertions(+), 5 deletions(-)
--git a/arch/x86/boot/header.S b/arch/x86/boot/header.S
index 4f1e1791cda4d316..a8ff8bbb17bca7d7 100644
--- a/arch/x86/boot/header.S
+++ b/arch/x86/boot/header.S
@@ -253,6 +253,23 @@ section_table:
IMAGE_SCN_MEM_READ | \
IMAGE_SCN_MEM_EXECUTE # Characteristics
+ .ascii ".data"
+ .byte 0
+ .byte 0
+ .byte 0
+ .long 0
+ .long 0x0 # startup_{32,64}
+ .long 0 # Size of initialized data
+ # on disk
+ .long 0x0 # startup_{32,64}
+ .long 0 # PointerToRelocations
+ .long 0 # PointerToLineNumbers
+ .word 0 # NumberOfRelocations
+ .word 0 # NumberOfLineNumbers
+ .long IMAGE_SCN_CNT_INITIALIZED_DATA | \
+ IMAGE_SCN_MEM_READ | \
+ IMAGE_SCN_MEM_WRITE # Characteristics
+
.set section_count, (. - section_table) / 40
#endif /* CONFIG_EFI_STUB */
diff --git a/arch/x86/boot/tools/build.c b/arch/x86/boot/tools/build.c
index 883e6359221cd588..b449c82feaadf2b8 100644
--- a/arch/x86/boot/tools/build.c
+++ b/arch/x86/boot/tools/build.c
@@ -119,6 +119,7 @@ static unsigned long efi_boot_params;
static unsigned long kernel_info;
static unsigned long startup_64;
static unsigned long _ehead;
+static unsigned long _data;
static unsigned long _end;
/*----------------------------------------------------------------------*/
@@ -347,10 +348,15 @@ static unsigned int update_pecoff_sections(unsigned int text_start, unsigned int
init_sz += CONFIG_PHYSICAL_ALIGN;
/*
- * Size of code: Subtract the size of the first sector (512 bytes)
- * which includes the header.
+ * Size of code: the size of the combined .text/.rodata section, which
+ * ends at the _data marker symbol.
*/
- put_unaligned_le32(text_sz + bss_sz, &hdr->text_size);
+ put_unaligned_le32(_data, &hdr->text_size);
+
+ /*
+ * Size of data: the size of the combined .data/.bss section.
+ */
+ put_unaligned_le32(text_sz - _data + bss_sz, &hdr->data_size);
/* Size of image */
put_unaligned_le32(init_sz, &hdr->image_size);
@@ -360,9 +366,13 @@ static unsigned int update_pecoff_sections(unsigned int text_start, unsigned int
*/
put_unaligned_le32(text_start + efi_pe_entry, &hdr->entry_point);
- update_pecoff_section_header_fields(".text", text_start, text_sz + bss_sz,
- text_sz, text_start);
+ update_pecoff_section_header_fields(".text", text_start, _data,
+ _data, text_start);
+ update_pecoff_section_header_fields(".data", text_start + _data,
+ text_sz - _data + bss_sz,
+ text_sz - _data,
+ text_start + _data);
return text_start + file_sz;
}
@@ -455,6 +465,7 @@ static void parse_zoffset(char *fname)
PARSE_ZOFS(p, kernel_info);
PARSE_ZOFS(p, startup_64);
PARSE_ZOFS(p, _ehead);
+ PARSE_ZOFS(p, _data);
PARSE_ZOFS(p, _end);
p = strchr(p, '\n');
--
2.39.2
next prev parent reply other threads:[~2023-03-08 20:22 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-08 20:22 [RFC PATCH 0/4] efi: x86: Use strict W^X mappings in PE/COFF header Ard Biesheuvel
2023-03-08 20:22 ` [RFC PATCH 1/4] efi: x86: Use private copy of struct setup_header Ard Biesheuvel
2023-03-08 20:22 ` [RFC PATCH 2/4] efi: x86: Move PE header after setup header Ard Biesheuvel
2023-03-09 17:45 ` Ard Biesheuvel
2023-03-08 20:22 ` [RFC PATCH 3/4] efi: x86: Drop alignment section header flags Ard Biesheuvel
2023-03-08 20:22 ` Ard Biesheuvel [this message]
2023-03-09 18:02 ` [RFC PATCH 4/4] efi: x86: Split PE/COFF .text section into .text and .data Evgeniy Baskov
2023-03-09 18:03 ` Ard Biesheuvel
2023-03-09 17:59 ` [RFC PATCH 0/4] efi: x86: Use strict W^X mappings in PE/COFF header Evgeniy Baskov
2023-03-09 18:09 ` Ard Biesheuvel
2023-03-09 18:37 ` 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=20230308202209.2980947-5-ardb@kernel.org \
--to=ardb@kernel.org \
--cc=baskov@ispras.ru \
--cc=bp@alien8.de \
--cc=khoroshilov@ispras.ru \
--cc=linux-efi@vger.kernel.org \
--cc=mario.limonciello@amd.com \
--cc=pjones@redhat.com \
/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