Skip to contentSkip to conversation

Handle UEFI NX-restricted page tables

PATCH New Baskov Evgeniy 1 participant 3 messages Mar 3, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Handle UEFI NX-restricted page tables

UEFI specification does not guarantee all memory to executable and/or writable. There are some firmware implementations that enforce stricter memory protection attributes and that prevents Linux kernel from booting normally causing page fault during boot process.

This patch uses DXE services to check and modified memory attributes while booting via EFISTUB, in such way that memory regions used by the kernel until extraction and are expected to be executable have appropriate attributes.

Unlike v2 of the patch this version only modifies memory attributes if it is really required to reduce the probability of facing firmware bugs. "Really required" in this case means:

- DXE services table is published by EFI;
- GetMemorySpaceDescriptor() works can return descriptor(s) of regions, where attributes modification might be required;
- EFI_MEMORY_RO or EFI_MEMORY_XP is set for these regions;
- Region describes system memory, i.e. generic RAM.

Also we cannot simply replace EFI_LOADER_DATA with EFI_LOADER_CODE to mitigate the issue, since:

- It is not guaranteed by specification that memory allocated with loader code type is executable. And firmware where this issue is present is modified in such way that memory EFI_LOADER_CODE is not executable;
- Linux still uses memory not allocated via EFI boot services for trampoline code placement, that would remain non-executable after replacement of EFI_LOADER_DATA with EFI_LOADER_CODE.

Baskov Evgeniy (2):
efi: declare DXE services table
libstub: ensure allocated memory to be executable

 arch/x86/include/asm/efi.h              |    5 +
 drivers/firmware/efi/Kconfig            |   12 ++
 drivers/firmware/efi/libstub/efistub.h  |   74 ++++++++++++++
 drivers/firmware/efi/libstub/x86-stub.c |   10 +-
 include/linux/efi.h                     |    2 
 drivers/firmware/efi/libstub/x86-stub.c |  110 +++++++++++++++++++++-
 6 files changed, 208 insertions(+), 5 deletions(-)
#1 lore raw

PATCH v3 1/2 libstub: declare DXE services table

UEFI DXE services are not yet used in kernel code
but are required to manipulate page table memory
protection flags.

Add required declarations to use DXE services functions.

arch/x86/include/asm/efi.h +5 -0
index 03cb12775043..4614d54383ac 100644
@@ -352,6 +352,11 @@ static inline u32 efi64_convert_status(efi_status_t status)
 						   runtime),		\
 				    func, __VA_ARGS__))
 
+#define efi_dxe_call(func, ...)						\
+	(efi_is_native()						\
+		? efi_dxe_table->func(__VA_ARGS__)			\
+		: __efi64_thunk_map(efi_dxe_table, func, __VA_ARGS__))
+
 #else /* CONFIG_EFI_MIXED */
 
 static inline bool efi_is_64bit(void)
drivers/firmware/efi/libstub/efistub.h +74 -0

92 lines not on this page · raw message

drivers/firmware/efi/libstub/x86-stub.c +9 -1
index 01ddd4502e28..47fa1c8e7f40 100644
@@ -22,6 +22,7 @@
 #define MAXMEM_X86_64_4LEVEL (1ull << 46)
 
 const efi_system_table_t *efi_system_table;
+const efi_dxe_services_table_t *efi_dxe_table;
 extern u32 image_offset;
 static efi_loaded_image_t *image = NULL;
 
@@ -677,11 +678,18 @@ unsigned long efi_main(efi_handle_t handle,
 	efi_status_t status;
 
 	efi_system_table = sys_table_arg;
-
 	/* Check if we were booted by the EFI firmware */
 	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 == NULL ||
+	    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
+		efi_warn("Unable to locate EFI DXE services table\n");
+		efi_dxe_table = NULL;
+	}
+
 	/*
 	 * If the kernel isn't already loaded at a suitable address,
 	 * relocate it.
include/linux/efi.h +2 -0
index ccd4d3f91c98..8935efba57d2 100644
@@ -383,6 +383,7 @@ void efi_native_runtime_setup(void);
 #define EFI_LOAD_FILE_PROTOCOL_GUID		EFI_GUID(0x56ec3091, 0x954c, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
 #define EFI_LOAD_FILE2_PROTOCOL_GUID		EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e,  0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
 #define EFI_RT_PROPERTIES_TABLE_GUID		EFI_GUID(0xeb66918a, 0x7eef, 0x402a,  0x84, 0x2e, 0x93, 0x1d, 0x21, 0xc3, 0x8a, 0xe9)
+#define EFI_DXE_SERVICES_TABLE_GUID		EFI_GUID(0x05ad34ba, 0x6f02, 0x4214,  0x95, 0x2e, 0x4d, 0xa0, 0x39, 0x8e, 0x2b, 0xb9)
 
 #define EFI_IMAGE_SECURITY_DATABASE_GUID	EFI_GUID(0xd719b2cb, 0x3d3a, 0x4596,  0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f)
 #define EFI_SHIM_LOCK_GUID			EFI_GUID(0x605dab50, 0xe046, 0x4300,  0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
@@ -435,6 +436,7 @@ typedef struct {
 } efi_config_table_type_t;
 
 #define EFI_SYSTEM_TABLE_SIGNATURE ((u64)0x5453595320494249ULL)
+#define EFI_DXE_SERVICES_TABLE_SIGNATURE ((u64)0x565245535f455844ULL)
 
 #define EFI_2_30_SYSTEM_TABLE_REVISION  ((2 << 16) | (30))
 #define EFI_2_20_SYSTEM_TABLE_REVISION  ((2 << 16) | (20))
Signature · 1 line
2.35.1
#2 lore raw

PATCH v3 2/2 libstub: ensure allocated memory to be executable

In reply to Baskov Evgeniy

There are UEFI versions that restrict execution of memory regions, preventing the kernel from booting. Parts that needs to be executable are:

* Area used for trampoline placement.
* All memory regions that the kernel may be relocated before and during extraction.

Use DXE services to ensure aforementioned address ranges to be executable. Only modify attributes that does not have appropriate attributes.

drivers/firmware/efi/Kconfig +12 -0
index 2c3dac5ecb36..f44f8b746e42 100644
@@ -91,6 +91,18 @@ config EFI_SOFT_RESERVE
 
 	  If unsure, say Y.
 
+config EFI_DXE_MEM_ATTRIBUTES
+	bool "Adjust memory attributes in EFISTUB"
+	depends on EFI && EFI_STUB && X86
+	default y
+	help
+	  UEFI specification does not guarantee all memory to be
+	  accessible for both write and execute as the kernel expects
+	  it to be.
+	  Use DXE services to check and alter memory protection
+	  attributes during boot via EFISTUB to ensure that memory
+	  ranges used by the kernel are writable and executable.
+
 config EFI_PARAMS_FROM_FDT
 	bool
 	help
drivers/firmware/efi/libstub/x86-stub.c +106 -4

136 lines not on this page · raw message

Signature · 1 line
2.35.1