memory attribute table support
Subsystems (3)
cover memory attribute table support
This implements ARM/arm64 support for dealing with the UEFI memory attribute table, which allows the OS to map UEFI Runtime Services regions with stricter permissions.
I have tried to implement the early discovery and validation of the table as generically as possible. I think it should be reusable on x86 as well, but I haven't tried myself. Note that this also depends on the way we deal with the reservation of BootServicesData regions goind forward.
Note that for 32-bit ARM, this depends on [1], which addresses an issue with memremap() on that architecture
Patches #1 and #2 implement support for the EFI_MEMORY_RO and EFI_MEMORY_XP attributes, and as it turns out, this code supports both the (obsolete) PropertiesTable and the new Memory Attributes table, so I moved them to the beginning of the series.
Patch #3 adds the GUID and type definitions for the memory attributes table.
Patch #4 implements the generic handling of the table.
Patch #5 wires it up for ARM and arm64.
[1] http://thread.gmane.org/gmane.linux.kernel/2157907
Ard Biesheuvel (5):
ARM: efi: apply strict permissons for UEFI Runtime Services regions
arm64: efi: apply strict permissons for UEFI Runtime Services regions
efi: add support for the EFI_MEMORY_ATTRIBUTES_TABLE config table
efi: implement generic support for the Memory Attributes table arm*: efi: take the Memory Attributes table into account
arch/arm/include/asm/efi.h | 1 + arch/arm/kernel/efi.c | 41 ++++++ arch/arm64/include/asm/efi.h | 2 + arch/arm64/kernel/efi.c | 27 +++- drivers/firmware/efi/Makefile | 2 +- drivers/firmware/efi/arm-init.c | 1 + drivers/firmware/efi/arm-runtime.c | 3 + drivers/firmware/efi/efi.c | 2 + drivers/firmware/efi/memattr.c | 137 ++++++++++++++++++++ include/linux/efi.h | 19 +++ 10 files changed, 229 insertions(+), 6 deletions(-) create mode 100644 drivers/firmware/efi/memattr.c
Signature · 1 line
2.5.0
PATCH 1/5 ARM: efi: apply strict permissons for UEFI Runtime Services regions
Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
--- arch/arm/include/asm/efi.h | 1 + arch/arm/kernel/efi.c | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+)
arch/arm/include/asm/efi.h +1 -0
@@ -22,6 +22,7 @@void efi_init (void );int efi_create_mapping (struct mm_struct *mm,efi_memory_desc_t *md); +int efi_set_mapping_permissions (struct mm_struct *mm,efi_memory_desc_t *md);#define efi_call_virt (f, ...) \ ({ \
arch/arm/kernel/efi.c +41 -0
52 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH 1/5 ARM: efi: apply strict permissons for UEFI Runtime Services regions
Quoted 11 lines · Ard Biesheuvel
Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm/include/asm/efi.h | 1 + arch/arm/kernel/efi.c | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+)
Looks fine from an EFI perspective, but it would be nice for somebody else with ARM knowledge to ACK it.
PATCH 1/5 ARM: efi: apply strict permissons for UEFI Runtime Services regions
Quoted 7 lines · Matt Fleming
Quoted 6 lines · Ard Biesheuvel
Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm/include/asm/efi.h | 1 + arch/arm/kernel/efi.c | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+)Looks fine from an EFI perspective, but it would be nice for somebody else with ARM knowledge to ACK it.
I agree.
Note that the whole memory attribute series needs to wait for some memremap() changes that I proposed for ARM (or at least the bits where we wire it up for arm64+ARM)
Thanks,
Ard.
PATCH 1/5 ARM: efi: apply strict permissons for UEFI Runtime Services regions
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines · Matt Fleming
Quoted 11 lines · Ard Biesheuvel
Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm/include/asm/efi.h | 1 + arch/arm/kernel/efi.c | 41 ++++++++++++++++++++ 2 files changed, 42 insertions(+)
Quoted 8 lines
Quoted 2 lines
Looks fine from an EFI perspective, but it would be nice for somebody else with ARM knowledge to ACK it.
I agree.
Note that the whole memory attribute series needs to wait for some
memremap()changes that I proposed for ARM (or at least the bits where we wire it up for arm64+ARM)
Thanks for the reminder. I'll definitely hold off on applying this for now.
PATCH 2/5 arm64: efi: apply strict permissons for UEFI Runtime Services regions
In reply to Ard BiesheuvelRecent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
Note that in the absence of permission attributes, we still only map regions of type EFI_RUNTIME_SERVICE_CODE with the executable bit set. Also, we base the mapping attributes of EFI_MEMORY_MAPPED_IO on the type directly rather than on the absence of the EFI_MEMORY_WB attribute. This is more correct, but is also required for compatibility with the upcoming support for the Memory Attributes Table, which only carries permission attributes, not memory type attributes.
--- arch/arm64/kernel/efi.c | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-)
arch/arm64/kernel/efi.c +22 -5
@@ -24,15 +24,32 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)/* * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be * executable, everything else can be mapped with the XN bits -* set. +* set. Also take the new (optional) RO/XP bits into account. */ -if ((md->attribute &EFI_MEMORY_WB ) ==0 ) +if (md->type ==EFI_MEMORY_MAPPED_IO ) prot_val = PROT_DEVICE_nGnRE; -else if (md->type ==EFI_RUNTIME_SERVICES_CODE || - !PAGE_ALIGNED (md->phys_addr)) +else if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), +"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" )) +/* +* If the region is not aligned to the page size of the OS, we +* can not use strict permissions, since that would also affect +* the mapping attributes of the adjacent regions. +*/ prot_val =pgprot_val (PAGE_KERNEL_EXEC ); -else +else if ((md->attribute & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == + (EFI_MEMORY_XP |EFI_MEMORY_RO )) +/* R-- */ + prot_val =pgprot_val (PAGE_KERNEL_RO ); +else if (md->attribute &EFI_MEMORY_RO ) +/* R-X */ + prot_val =pgprot_val (PAGE_KERNEL_ROX ); +else if (md->attribute &EFI_MEMORY_XP || + md->type !=EFI_RUNTIME_SERVICES_CODE ) +/* RW- */ prot_val =pgprot_val (PAGE_KERNEL ); +else +/* RWX */ + prot_val =pgprot_val (PAGE_KERNEL_EXEC ); create_pgd_mapping(mm, md->phys_addr, md->virt_addr, md->num_pages << EFI_PAGE_SHIFT,
Signature · 1 line
2.5.0
PATCH 2/5 arm64: efi: apply strict permissons for UEFI Runtime Services regions
Quoted 51 lines · Ard Biesheuvel
Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
Note that in the absence of permission attributes, we still only map regions of type
EFI_RUNTIME_SERVICE_CODEwith the executable bit set. Also, we base the mapping attributes ofEFI_MEMORY_MAPPED_IOon the type directly rather than on the absence of theEFI_MEMORY_WBattribute. This is more correct, but is also required for compatibility with the upcoming support for the Memory Attributes Table, which only carries permission attributes, not memory type attributes.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/kernel/efi.c | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-)arch/arm64/kernel/efi.c +16
-5@@ -24,15 +24,32 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)/* * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be * executable, everything else can be mapped with the XN bits -* set. +* set. Also take the new (optional) RO/XP bits into account. */ -if ((md->attribute &EFI_MEMORY_WB ) ==0 ) +if (md->type ==EFI_MEMORY_MAPPED_IO ) prot_val = PROT_DEVICE_nGnRE; -else if (md->type ==EFI_RUNTIME_SERVICES_CODE || - !PAGE_ALIGNED (md->phys_addr)) +else if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), +"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" )) +/* +* If the region is not aligned to the page size of the OS, we +* can not use strict permissions, since that would also affect +* the mapping attributes of the adjacent regions. +*/ prot_val =pgprot_val (PAGE_KERNEL_EXEC ); -else +else if ((md->attribute & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == + (EFI_MEMORY_XP |EFI_MEMORY_RO )) +/* R-- */ + prot_val =pgprot_val (PAGE_KERNEL_RO ); +else if (md->attribute &EFI_MEMORY_RO ) +/* R-X */ + prot_val =pgprot_val (PAGE_KERNEL_ROX );
arch/arm64/kernel/efi.c int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md) · line 55
Quoted 10 lines
arch/arm64/kernel/efi.c +6
-0+else if (md->attribute &EFI_MEMORY_XP || + md->type !=EFI_RUNTIME_SERVICES_CODE ) +/* RW- */ prot_val =pgprot_val (PAGE_KERNEL ); +else +/* RWX */ + prot_val =pgprot_val (PAGE_KERNEL_EXEC ); create_pgd_mapping(mm, md->phys_addr, md->virt_addr, md->num_pages << EFI_PAGE_SHIFT,
The actual logic looks fine but it seems like there's quite a lot going on in this function which is fairly difficult to decipher with the if/else if clauses.
Would you be open to splitting this out a little? It's just a suggestion, but maybe something like this,
---
arch/arm64/kernel/efi.c +35 -26
@@ -17,39 +17,48 @@#include <asm/efi.h> -int __init efi_create_mapping (struct mm_struct *mm,efi_memory_desc_t *md) +/* +* Only regions of type EFI_RUNTIME_SERVICES_CODE need to be +* executable, everything else can be mapped with the XN bits +* set. Also take the new (optional) RO/XP bits into account. +*/ +static __init pteval_t create_mapping_protection (efi_memory_desc_t *md) { -pteval_t prot_val; +u64 attr = md->attribute; +u32 type = md->type; -/* -* Only regions of type EFI_RUNTIME_SERVICES_CODE need to be -* executable, everything else can be mapped with the XN bits -* set. Also take the new (optional) RO/XP bits into account. -*/ -if (md->type ==EFI_MEMORY_MAPPED_IO ) - prot_val = PROT_DEVICE_nGnRE; -else if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), -"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" )) +if (type ==EFI_MEMORY_MAPPED_IO ) +return PROT_DEVICE_nGnRE; + +if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), +"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" ))/* * If the region is not aligned to the page size of the OS, we * can not use strict permissions, since that would also affect * the mapping attributes of the adjacent regions. */ - prot_val =pgprot_val (PAGE_KERNEL_EXEC ); -else if ((md->attribute & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == - (EFI_MEMORY_XP |EFI_MEMORY_RO )) -/* R-- */ - prot_val =pgprot_val (PAGE_KERNEL_RO ); -else if (md->attribute &EFI_MEMORY_RO ) -/* R-X */ - prot_val =pgprot_val (PAGE_KERNEL_ROX ); -else if (md->attribute &EFI_MEMORY_XP || - md->type !=EFI_RUNTIME_SERVICES_CODE ) -/* RW- */ - prot_val =pgprot_val (PAGE_KERNEL ); -else -/* RWX */ - prot_val =pgprot_val (PAGE_KERNEL_EXEC ); +return pgprot_val (PAGE_KERNEL_EXEC ); + +/* R-- */ +if ((attr & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == + (EFI_MEMORY_XP |EFI_MEMORY_RO )) +return pgprot_val (PAGE_KERNEL_RO ); + +/* R-X */ +if (attr &EFI_MEMORY_RO ) +return pgprot_val (PAGE_KERNEL_ROX ); + +/* RW- */ +if (attr &EFI_MEMORY_XP || type !=EFI_RUNTIME_SERVICES_CODE ) +return pgprot_val (PAGE_KERNEL ); + +/* RWX */ +return pgprot_val (PAGE_KERNEL_EXEC ); +} + +int __init efi_create_mapping (struct mm_struct *mm,efi_memory_desc_t *md) +{ +pteval_t prot_val =create_mapping_protection (md); create_pgd_mapping(mm, md->phys_addr, md->virt_addr, md->num_pages << EFI_PAGE_SHIFT,
PATCH 2/5 arm64: efi: apply strict permissons for UEFI Runtime Services regions
Quoted 67 lines · Matt Fleming
Quoted 51 lines · Ard Biesheuvel
Recent UEFI versions expose permission attributes for runtime services memory regions, either in the UEFI memory map or in the separate memory attributes table. This allows the kernel to map these regions with stricter permissions, rather than the RWX permissions that are used by default. So wire this up in our mapping routine.
Note that in the absence of permission attributes, we still only map regions of type
EFI_RUNTIME_SERVICE_CODEwith the executable bit set. Also, we base the mapping attributes ofEFI_MEMORY_MAPPED_IOon the type directly rather than on the absence of theEFI_MEMORY_WBattribute. This is more correct, but is also required for compatibility with the upcoming support for the Memory Attributes Table, which only carries permission attributes, not memory type attributes.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/kernel/efi.c | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-)arch/arm64/kernel/efi.c +16
-5@@ -24,15 +24,32 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)/* * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be * executable, everything else can be mapped with the XN bits -* set. +* set. Also take the new (optional) RO/XP bits into account. */ -if ((md->attribute &EFI_MEMORY_WB ) ==0 ) +if (md->type ==EFI_MEMORY_MAPPED_IO ) prot_val = PROT_DEVICE_nGnRE; -else if (md->type ==EFI_RUNTIME_SERVICES_CODE || - !PAGE_ALIGNED (md->phys_addr)) +else if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), +"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" )) +/* +* If the region is not aligned to the page size of the OS, we +* can not use strict permissions, since that would also affect +* the mapping attributes of the adjacent regions. +*/ prot_val =pgprot_val (PAGE_KERNEL_EXEC ); -else +else if ((md->attribute & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == + (EFI_MEMORY_XP |EFI_MEMORY_RO )) +/* R-- */ + prot_val =pgprot_val (PAGE_KERNEL_RO ); +else if (md->attribute &EFI_MEMORY_RO ) +/* R-X */ + prot_val =pgprot_val (PAGE_KERNEL_ROX );arch/arm64/kernel/efi.c int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md) · line 55
Quoted 10 lines
arch/arm64/kernel/efi.c +6
-0+else if (md->attribute &EFI_MEMORY_XP || + md->type !=EFI_RUNTIME_SERVICES_CODE ) +/* RW- */ prot_val =pgprot_val (PAGE_KERNEL ); +else +/* RWX */ + prot_val =pgprot_val (PAGE_KERNEL_EXEC ); create_pgd_mapping(mm, md->phys_addr, md->virt_addr, md->num_pages << EFI_PAGE_SHIFT,The actual logic looks fine but it seems like there's quite a lot going on in this function which is fairly difficult to decipher with the if/else if clauses.
Quoted 3 lines
Would you be open to splitting this out a little? It's just a suggestion, but maybe something like this,
Sure, that looks a lot better. I will fold that into v2
Quoted 81 lines
---
arch/arm64/kernel/efi.c +35
-26@@ -17,39 +17,48 @@#include <asm/efi.h> -int __init efi_create_mapping (struct mm_struct *mm,efi_memory_desc_t *md) +/* +* Only regions of type EFI_RUNTIME_SERVICES_CODE need to be +* executable, everything else can be mapped with the XN bits +* set. Also take the new (optional) RO/XP bits into account. +*/ +static __init pteval_t create_mapping_protection (efi_memory_desc_t *md) { -pteval_t prot_val; +u64 attr = md->attribute; +u32 type = md->type; -/* -* Only regions of type EFI_RUNTIME_SERVICES_CODE need to be -* executable, everything else can be mapped with the XN bits -* set. Also take the new (optional) RO/XP bits into account. -*/ -if (md->type ==EFI_MEMORY_MAPPED_IO ) - prot_val = PROT_DEVICE_nGnRE; -else if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), -"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" )) +if (type ==EFI_MEMORY_MAPPED_IO ) +return PROT_DEVICE_nGnRE; + +if (WARN_ONCE (!PAGE_ALIGNED (md->phys_addr), +"UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?" ))/* * If the region is not aligned to the page size of the OS, we * can not use strict permissions, since that would also affect * the mapping attributes of the adjacent regions. */ - prot_val =pgprot_val (PAGE_KERNEL_EXEC ); -else if ((md->attribute & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == - (EFI_MEMORY_XP |EFI_MEMORY_RO )) -/* R-- */ - prot_val =pgprot_val (PAGE_KERNEL_RO ); -else if (md->attribute &EFI_MEMORY_RO ) -/* R-X */ - prot_val =pgprot_val (PAGE_KERNEL_ROX ); -else if (md->attribute &EFI_MEMORY_XP || - md->type !=EFI_RUNTIME_SERVICES_CODE ) -/* RW- */ - prot_val =pgprot_val (PAGE_KERNEL ); -else -/* RWX */ - prot_val =pgprot_val (PAGE_KERNEL_EXEC ); +return pgprot_val (PAGE_KERNEL_EXEC ); + +/* R-- */ +if ((attr & (EFI_MEMORY_XP |EFI_MEMORY_RO )) == + (EFI_MEMORY_XP |EFI_MEMORY_RO )) +return pgprot_val (PAGE_KERNEL_RO ); + +/* R-X */ +if (attr &EFI_MEMORY_RO ) +return pgprot_val (PAGE_KERNEL_ROX ); + +/* RW- */ +if (attr &EFI_MEMORY_XP || type !=EFI_RUNTIME_SERVICES_CODE ) +return pgprot_val (PAGE_KERNEL ); + +/* RWX */ +return pgprot_val (PAGE_KERNEL_EXEC ); +} + +int __init efi_create_mapping (struct mm_struct *mm,efi_memory_desc_t *md) +{ +pteval_t prot_val =create_mapping_protection (md); create_pgd_mapping(mm, md->phys_addr, md->virt_addr, md->num_pages << EFI_PAGE_SHIFT,
PATCH 3/5 efi: add support for the EFI_MEMORY_ATTRIBUTES_TABLE config table
In reply to Ard BiesheuvelThis declares the GUID and struct typedef for the new memory attributes table which contains the permissions that can be used to apply stricter permissions to UEFI Runtime Services memory regions.
--- drivers/firmware/efi/efi.c | 2 ++ include/linux/efi.h | 13 +++++++++++++ 2 files changed, 15 insertions(+)
drivers/firmware/efi/efi.c +2 -0
@@ -43,6 +43,7 @@ struct efi __read_mostly efi = { .config_table = EFI_INVALID_TABLE_ADDR, .esrt = EFI_INVALID_TABLE_ADDR, .properties_table = EFI_INVALID_TABLE_ADDR, + .mem_attr_table = EFI_INVALID_TABLE_ADDR, };EXPORT_SYMBOL (efi);
@@ -338,6 +339,7 @@ static __initdata efi_config_table_type_t common_tables[] = { {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga}, {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt}, {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table}, + {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table}, {NULL_GUID, NULL, NULL}, };
include/linux/efi.h +13 -0
@@ -619,6 +619,10 @@ void efi_native_runtime_setup(void);EFI_GUID (0x880aaca3 ,0x4adc ,0x4a04 , \ 0x90, 0x79, 0xb7, 0x47, 0x34, 0x08, 0x25, 0xe5) +#define EFI_MEMORY_ATTRIBUTES_TABLE_GUID \ + EFI_GUID(0xdcfa911d, 0x26eb, 0x469f, \ + 0xa2, 0x20, 0x38, 0xb7, 0xdc, 0x46, 0x12, 0x20) + typedef struct { efi_guid_t guid; u64 table;
@@ -843,6 +847,14 @@ typedef struct {#define EFI_INVALID_TABLE_ADDR (~0UL) +typedef struct { +u32 version; +u32 num_entries; +u32 desc_size; +u32 reserved; +efi_memory_desc_t entry[0 ]; +}efi_memory_attributes_table_t ; +/* * All runtime access to EFI goes through this structure: */
@@ -864,6 +876,7 @@ extern struct efi {unsigned long config_table;/* config tables */ unsigned long esrt;/* ESRT table */ unsigned long properties_table;/* properties table */ +unsigned long mem_attr_table;/* memory attributes table */ efi_get_time_t *get_time;efi_set_time_t *set_time;efi_get_wakeup_time_t *get_wakeup_time;
Signature · 1 line
2.5.0
PATCH 4/5 efi: implement generic support for the Memory Attributes table
This implements shared support for discovering the presence of the Memory Attributes table, and for parsing and validating its contents.
The table is validated against the construction rules in the UEFI spec. Since this is a new table, it makes sense to complain if we encounter a table that does not follow those rules.
The parsing and validation routine takes a callback that can be specified per architecture, that gets passed each unique validated region, with the virtual address retrieved from the ordinary memory map.
--- drivers/firmware/efi/Makefile | 2 +- drivers/firmware/efi/memattr.c | 137 ++++++++++++++++++++ include/linux/efi.h | 6 + 3 files changed, 144 insertions(+), 1 deletion(-)
drivers/firmware/efi/Makefile +1 -1
@@ -9,7 +9,7 @@ # KASAN_SANITIZE_runtime-wrappers.o := n -obj-$(CONFIG_EFI) += efi.o vars.o reboot.o +obj-$(CONFIG_EFI) += efi.o vars.o reboot.o memattr.o obj-$(CONFIG_EFI_VARS) += efivars.o obj-$(CONFIG_EFI_ESRT) += esrt.o obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.o
drivers/firmware/efi/memattr.c new file +137 -0
137 lines not on this page · raw message
include/linux/efi.h +6 -0
@@ -966,6 +966,12 @@ extern void __init efi_fake_memmap(void);static inline void efi_fake_memmap (void ) { }#endif +typedef int (*efi_memattr_perm_setter )(struct mm_struct *,efi_memory_desc_t *); + +extern int efi_memattr_init (void ); +extern int efi_memattr_apply_permissions (struct mm_struct *mm, +efi_memattr_perm_setter fn); +/* Iterate through an efi_memory_map */ #define for_each_efi_memory_desc (m, md) \ for ((md) = (m)->map; \
Signature · 1 line
2.5.0
PATCH 5/5 arm*: efi: take the Memory Attributes table into account
Call into the generic memory attributes table support code at the appropriate times during the init sequence so that the UEFI Runtime Services region are mapped according to the strict permissions it specifies.
--- arch/arm64/include/asm/efi.h | 2 ++ drivers/firmware/efi/arm-init.c | 1 + drivers/firmware/efi/arm-runtime.c | 3 +++ 3 files changed, 6 insertions(+)
arch/arm64/include/asm/efi.h +2 -0
@@ -14,6 +14,8 @@ extern void efi_init(void);int efi_create_mapping (struct mm_struct *mm,efi_memory_desc_t *md); +#define efi_set_mapping_permissions efi_create_mapping +#define efi_call_virt (f, ...) \ ({ \ efi_##f##_t *__f; \
drivers/firmware/efi/arm-init.c +1 -0
@@ -202,6 +202,7 @@ void __init efi_init(void)return ;reserve_regions (); +efi_memattr_init ();early_memunmap (memmap.map, params.mmap_size); memblock_mark_nomap(params.mmap & PAGE_MASK, PAGE_ALIGN(params.mmap_size +
drivers/firmware/efi/arm-runtime.c +3 -0
@@ -65,6 +65,9 @@ static bool __init efi_virtmap_init(void)return false; } } + +if (efi_memattr_apply_permissions (&efi_mm, efi_set_mapping_permissions)) +return false;return true; }
Signature · 1 line
2.5.0
PATCH 4/5 efi: implement generic support for the Memory Attributes table
In reply to Ard BiesheuvelQuoted 37 lines · Ard Biesheuvel
This implements shared support for discovering the presence of the Memory Attributes table, and for parsing and validating its contents.
The table is validated against the construction rules in the UEFI spec. Since this is a new table, it makes sense to complain if we encounter a table that does not follow those rules.
The parsing and validation routine takes a callback that can be specified per architecture, that gets passed each unique validated region, with the virtual address retrieved from the ordinary memory map.
Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/Makefile | 2 +- drivers/firmware/efi/memattr.c | 137 ++++++++++++++++++++ include/linux/efi.h | 6 + 3 files changed, 144 insertions(+), 1 deletion(-)drivers/firmware/efi/Makefile +1
-1@@ -9,7 +9,7 @@ # KASAN_SANITIZE_runtime-wrappers.o := n -obj-$(CONFIG_EFI) += efi.o vars.o reboot.o +obj-$(CONFIG_EFI) += efi.o vars.o reboot.o memattr.o obj-$(CONFIG_EFI_VARS) += efivars.o obj-$(CONFIG_EFI_ESRT) += esrt.o obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.odrivers/firmware/efi/memattr.c new file +0
-0
drivers/firmware/efi/memattr.c line 8
Quoted 8 lines
drivers/firmware/efi/memattr.c new file +8
-0@@ -0,0 +1,137 @@ +/* +* Copyright (C) 2016 Linaro Ltd. <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ +
Please include,
#define pr_fmt(fmt) "efi: " fmt
here. I've seen in the past that reporters will sometimes miss error messages when searching for EFI failures if they are not prefixed with "efi: ".
Quoted 22 lines
drivers/firmware/efi/memattr.c new file +22
-0+#include <linux/efi.h> +#include <linux/init.h> +#include <linux/io.h> +#include <linux/memblock.h> + +#include <asm/early_ioremap.h> + +static int __initdata tbl_size; + +/* +* Reserve the memory associated with the Memory Attributes configuration +* table, if it exists. +*/ +int __init efi_memattr_init (void ) +{ +efi_memory_attributes_table_t *tbl; + +if (efi.mem_attr_table ==EFI_INVALID_TABLE_ADDR ) +return 0 ; + + tbl =early_memremap (efi.mem_attr_table,sizeof (*tbl)); +if (!tbl) {
drivers/firmware/efi/memattr.c line 40
Quoted 10 lines
drivers/firmware/efi/memattr.c new file +10
-0+pr_err ("Failed to map EFI Memory Attribute table @ 0x%lx\n" , + efi.mem_attr_table); +return -ENOMEM ; + } + + tbl_size =sizeof (*tbl) + tbl->num_entries * tbl->desc_size; +memblock_reserve (efi.mem_attr_table, tbl_size); +early_memunmap (tbl,sizeof (*tbl)); +return 0 ; +}
We should really be checking tbl::version here so that if the version gets bumped in the future, perhaps due to a change in the structure layout, we don't try and reserve a crazy amount of memory.
drivers/firmware/efi/memattr.c line 48
Quoted 7 lines
drivers/firmware/efi/memattr.c new file +7
-0+/* +* Returns a copy @out of the UEFI memory descriptor @in if it is covered +* entirely by a UEFI memory map entry with matching attributes. The virtual +* address of @out is set according to the matching entry that was found. +*/ +static bool validate_entry (const efi_memory_desc_t *in,efi_memory_desc_t *out) +{
Might I suggest "entry_is_valid" or "valid_entry" for this function? A boolean return value implies we're asking a question.
drivers/firmware/efi/memattr.c line 55
Quoted 7 lines
drivers/firmware/efi/memattr.c new file +7
-0+u64 in_paddr = in->phys_addr; +u64 in_size = in->num_pages <<EFI_PAGE_SHIFT ; +efi_memory_desc_t *md; + +if (in->type !=EFI_RUNTIME_SERVICES_CODE && + in->type !=EFI_RUNTIME_SERVICES_DATA ) { +pr_warn ("MEMATTR table entry type should be RuntimeServiceCode or RuntimeServicesData\n" );
Hehe, I'm not a slave to the 80-column rule but 106-columns is pretty extreme. Could we reword this to shorten the string length?
drivers/firmware/efi/memattr.c line 56
Quoted 2 lines
drivers/firmware/efi/memattr.c new file +2
-0+return false; + }
Newline here please.
drivers/firmware/efi/memattr.c line 61
Quoted 4 lines
drivers/firmware/efi/memattr.c new file +4
-0+if (!(in->attribute & (EFI_MEMORY_RO |EFI_MEMORY_XP ))) { +pr_warn ("MEMATTR table entry attributes invalid: RO and XP bits both cleared\n" ); +return false; + }
Newline here please.
drivers/firmware/efi/memattr.c line 67
Quoted 6 lines
drivers/firmware/efi/memattr.c new file +6
-0+if (PAGE_SIZE >EFI_PAGE_SIZE && + (!PAGE_ALIGNED (in->phys_addr) || + !PAGE_ALIGNED (in->num_pages <<EFI_PAGE_SHIFT ))) { +pr_warn ("MEMATTR table entry misaligned\n" ); +return false; + }
Could you provide a comment for this check? I realise it's checking conformance to the region alignment rules for arm64, but a comment to that effect would be useful.
drivers/firmware/efi/memattr.c line 73
Quoted 6 lines
drivers/firmware/efi/memattr.c new file +6
-0+for_each_efi_memory_desc (&memmap, md) { +u64 md_paddr = md->phys_addr; +u64 md_size = md->num_pages <<EFI_PAGE_SHIFT ; + +if (!(md->attribute &EFI_MEMORY_RUNTIME )) +continue ;
Newline here please.
drivers/firmware/efi/memattr.c line 76
Quoted 3 lines
drivers/firmware/efi/memattr.c new file +3
-0+if (md->virt_addr ==0 ) +/* no virtual mapping has been installed by the stub */ +break ;
Please use braces when there are multiple lines under the if().
drivers/firmware/efi/memattr.c line 77
Quoted 1 line
drivers/firmware/efi/memattr.c new file +1
-0+if (md_paddr <= in_paddr && (in_paddr - md_paddr) < md_size) {
Let's save ourselves one level of indentation here and invert this check, continuing if the inverted check is false.
drivers/firmware/efi/memattr.c line 85
Quoted 8 lines
drivers/firmware/efi/memattr.c new file +8
-0+/* +* This entry covers the start of @in, check whether +* it covers the end as well. +*/ +if (md_paddr + md_size < in_paddr + in_size) { +pr_warn ("MEMATTR table entry covers multiple UEFI memory map regions\n" ); +return false; + }
Newline please.
drivers/firmware/efi/memattr.c line 89
Quoted 4 lines
drivers/firmware/efi/memattr.c new file +4
-0+if (md->type != in->type) { +pr_warn ("MEMATTR table entry type deviates from UEFI memory map region type\n" ); +return false; + }
Newline please.
Quoted 4 lines
drivers/firmware/efi/memattr.c new file +4
-0+ *out = *in; + out->virt_addr = in_paddr + + (md->virt_addr - md->phys_addr); +return true;
drivers/firmware/efi/memattr.c line 103
Quoted 10 lines
drivers/firmware/efi/memattr.c new file +10
-0+ } + } +return false; +} + +int __init efi_memattr_apply_permissions (struct mm_struct *mm, +efi_memattr_perm_setter fn) +{ +efi_memory_attributes_table_t *tbl; +int i, ret;
It might be worth a comment above this function explaining that it should only be invoked after the machine has entered virtual mode and SetVirtualAddressMap() has been called.
Quoted 39 lines
drivers/firmware/efi/memattr.c new file +34
-0+ +if (tbl_size <=sizeof (*tbl)) +return 0 ; + + tbl =memremap (efi.mem_attr_table, tbl_size,MEMREMAP_WB ); +if (!tbl) { +pr_err ("Failed to map EFI Memory Attribute table @ 0x%lx\n" , + efi.mem_attr_table); +return -ENOMEM ; + } + +if (efi_enabled (EFI_DBG )) +pr_info ("Processing UEFI Memory Attributes table:\n" ); + +for (i = ret =0 ; ret ==0 && i < tbl->num_entries; i++) { +efi_memory_desc_t md; +unsigned long size; +bool valid; +char buf[64 ]; + + valid =validate_entry ((void *)tbl->entry + i * tbl->desc_size, + &md); + size = md.num_pages <<EFI_PAGE_SHIFT ; +if (efi_enabled (EFI_DBG ) || !valid) +pr_info (" 0x%012llx-0x%012llx %s\n" , md.phys_addr, + md.phys_addr + size -1 , +efi_md_typeattr_format (buf,sizeof (buf), &md)); + +if (valid) + ret =fn (mm, &md); + } +memunmap (tbl); +return ret; +}include/linux/efi.h +0
-0
include/linux/efi.h extern void __init efi_fake_memmap(void); · line 970
Quoted 5 lines
include/linux/efi.h +2
-0@@ -966,6 +966,12 @@ extern void __init efi_fake_memmap(void);static inline void efi_fake_memmap (void ) { }#endif +typedef int (*efi_memattr_perm_setter )(struct mm_struct *,efi_memory_desc_t *); +
Please include a comment explaining what an 'efi_memattr_perm_setter' is and the rules (if any) constraining the implementation.
Quoted 10 lines
include/linux/efi.h +4
-0+extern int efi_memattr_init (void ); +extern int efi_memattr_apply_permissions (struct mm_struct *mm, +efi_memattr_perm_setter fn); +/* Iterate through an efi_memory_map */ #define for_each_efi_memory_desc (m, md) \ for ((md) = (m)->map; \Signature · 1 line
2.5.0
PATCH 3/5 efi: add support for the EFI_MEMORY_ATTRIBUTES_TABLE config table
In reply to Ard BiesheuvelQuoted 9 lines · Ard Biesheuvel
This declares the GUID and struct typedef for the new memory attributes table which contains the permissions that can be used to apply stricter permissions to UEFI Runtime Services memory regions.
Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/efi.c | 2 ++ include/linux/efi.h | 13 +++++++++++++ 2 files changed, 15 insertions(+)
Looks good Ard.