UEFI NX support and NX Linux loader using shim loader protocol
v5 cover UEFI NX support and NX Linux loader using shim loader protocol
This consists of:
- Derivative of Fedora NX patches to make GRUB itself work under NX.
- Julian Andres Klode's loader framework patch (used in Debian and Ubuntu for the downstream loader).
- Shim loader protocol integration using the above loader framework.
- Check to disallow using the legacy Linux loader when NX is required.
Julian Andres Klode (1):
efi: Provide wrappers for load_image, start_image, unload_image
Mate Kukri (6):
modules: load module sections at page-aligned addresses
nx: add memory attribute get/set API
nx: set page permissions for loaded modules.
nx: set the nx compatible flag in EFI GRUB images
efi: Use shim's loader protocol for EFI image verification and loading
efi: Disallow fallback to legacy Linux loader when shim says NX is
required.
Peter Jones (3):
modules: make .module_license read-only
modules: strip .llvm_addrsig sections and similar.
modules: Don't allocate space for non-allocable sections.
docs/grub-dev.texi | 6 +- grub-core/genmod.sh.in | 5 +- grub-core/kern/arm/dl.c | 14 +++ grub-core/kern/arm64/dl.c | 14 +++ grub-core/kern/dl.c | 159 +++++++++++++++++++++++------ grub-core/kern/efi/efi.c | 57 +++++++++++ grub-core/kern/efi/mm.c | 127 +++++++++++++++++++++++ grub-core/kern/efi/sb.c | 61 +++++++---- grub-core/kern/emu/full.c | 14 +++ grub-core/kern/i386/dl.c | 14 +++ grub-core/kern/ia64/dl.c | 9 ++ grub-core/kern/mips/dl.c | 8 ++ grub-core/kern/powerpc/dl.c | 9 ++ grub-core/kern/riscv/dl.c | 14 +++ grub-core/kern/sparc64/dl.c | 9 ++ grub-core/kern/x86_64/dl.c | 14 +++ grub-core/loader/efi/chainloader.c | 13 +-- grub-core/loader/efi/linux.c | 40 +++----- include/grub/dl.h | 52 +++++++++- include/grub/efi/api.h | 32 ++++++ include/grub/efi/efi.h | 42 ++++++++ include/grub/efi/memory.h | 2 + include/grub/efi/pe32.h | 2 + include/grub/efi/sb.h | 5 +- include/grub/mm.h | 33 ++++++ util/mkimage.c | 1 + 26 files changed, 661 insertions(+), 95 deletions(-)
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 1/10 modules: make .module_license read-only
From: Peter Jones <[email protected]>
Currently .module_license is set writable (that is, the section has the SHF_WRITE flag set) in the module's ELF headers. This probably never actually matters, but it can't possibly be correct.
This patch sets that data as "const", which causes that flag not to be set.
--- include/grub/dl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/grub/dl.h +1 -1
@@ -119,7 +119,7 @@ grub_mod_fini (void)#define ATTRIBUTE_USED __unused__#endif #define GRUB_MOD_LICENSE (license) \ - static char grub_module_license[] __attribute__ ((section (GRUB_MOD_SECTION (module_license)), ATTRIBUTE_USED)) = "LICENSE=" license; + static const char grub_module_license[] __attribute__ ((section (GRUB_MOD_SECTION (module_license)), ATTRIBUTE_USED)) = "LICENSE=" license;#define GRUB_MOD_DEP (name) \ static const char grub_module_depend_##name[] \ __attribute__((section(GRUB_MOD_SECTION(moddeps)), ATTRIBUTE_USED)) = #name
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 2/10 modules: strip .llvm_addrsig sections and similar.
In reply to Mate KukriFrom: Peter Jones <[email protected]>
Currently grub modules built with clang or gcc have several sections which we don't actually need or support.
We already have a list of section to skip in genmod.sh, and this patch adds the following sections to that list (as well as a few newlines):
.note.gnu.property
.llvm*
Note that the glob there won't work without a new enough linker, but the failure is just reversion to the status quo, so that's not a big problem.
--- grub-core/genmod.sh.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
grub-core/genmod.sh.in +4 -1
@@ -57,8 +57,11 @@ if test x@TARGET_APPLE_LINKER@ != x1; then @TARGET_STRIP@ --strip-unneeded \ -K grub_mod_init -K grub_mod_fini \ -K _grub_mod_init -K _grub_mod_fini \ - -R .note.gnu.gold-version -R .note.GNU-stack \ + -R .note.GNU-stack \ + -R .note.gnu.gold-version \ + -R .note.gnu.property \ -R .gnu.build.attributes \ + -R '.llvm*' \ -R .rel.gnu.build.attributes \ -R .rela.gnu.build.attributes \ -R .eh_frame -R .rela.eh_frame -R .rel.eh_frame \
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 3/10 modules: Don't allocate space for non-allocable sections.
In reply to Mate KukriFrom: Peter Jones <[email protected]>
Currently when loading grub modules, we allocate space for all sections, including those without SHF_ALLOC set. We then copy the sections that /do/ have SHF_ALLOC set into the allocated memory, leaving some of our allocation untouched forever. Additionally, on platforms with GOT fixups and trampolines, we currently compute alignment round-ups for the sections and sections with sh_size = 0.
This patch removes the extra space from the allocation computation, and makes the allocation computation loop skip empty sections as the loading loop does.
--- grub-core/kern/dl.c | 3 +++ 1 file changed, 3 insertions(+)
grub-core/kern/dl.c +3 -0
@@ -237,6 +237,9 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e) i < e->e_shnum; i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize)) { +if (s->sh_size ==0 || !(s->sh_flags &SHF_ALLOC )) +continue ; + tsize =ALIGN_UP (tsize, s->sh_addralign) + s->sh_size;if (talign < s->sh_addralign) talign = s->sh_addralign;
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 4/10 modules: load module sections at page-aligned addresses
In reply to Mate KukriCurrently we load module sections at whatever alignment gcc+ld happened to dump into the ELF section header, which is often less then the page size. Since NX protections are page based, this alignment must be rounded up to page size on platforms supporting NX protections.
This patch switches most EFI platforms to load module sections at 4kB page-aligned addresses. To do so, it adds an new per-arch function, grub_arch_dl_min_alignment(), which returns the alignment needed for dynamically loaded sections (in bytes). Currently it sets it to 4096 when GRUB_MACHINE_EFI is true on x86_64, i386, arm, arm64, and emu, and 1-byte alignment on everything else.
It then changes the allocation size computation and the loader code in grub_dl_load_segments() to align the locations and sizes up to these boundaries, and fills any added padding with zeros.
All of this happens before relocations are applied, so the relocations factor that in with no change.
--- docs/grub-dev.texi | 6 ++--- grub-core/kern/arm/dl.c | 14 ++++++++++ grub-core/kern/arm64/dl.c | 14 ++++++++++ grub-core/kern/dl.c | 52 +++++++++++++++++++++++++------------ grub-core/kern/emu/full.c | 14 ++++++++++ grub-core/kern/i386/dl.c | 14 ++++++++++ grub-core/kern/ia64/dl.c | 9 +++++++ grub-core/kern/mips/dl.c | 8 ++++++ grub-core/kern/powerpc/dl.c | 9 +++++++ grub-core/kern/riscv/dl.c | 14 ++++++++++ grub-core/kern/sparc64/dl.c | 9 +++++++ grub-core/kern/x86_64/dl.c | 14 ++++++++++ include/grub/dl.h | 4 +++ include/grub/efi/memory.h | 2 ++ 14 files changed, 164 insertions(+), 19 deletions(-)
docs/grub-dev.texi +3 -3
@@ -996,9 +996,9 @@ declare startup asm file ($cpu_$platform_startup) as well as any other files (e.g. init.c and callwrap.S) (e.g. $cpu_$platform = kern/$cpu/$platform/init.c). At this stage you will also need to add dummy dl.c and cache.S with functions grub_err_t grub_arch_dl_check_header (void *ehdr), grub_err_t -grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c) and -void grub_arch_sync_caches (void *address, grub_size_t len) (cache.S). They -won't be used for now. +grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c), grub_uint32_t +grub_arch_dl_min_alignment (void), and void grub_arch_sync_caches (void +*address, grub_size_t len) (cache.S). They won't be used for now. You will need to create directory include/$cpu/$platform and a file include/$cpu/types.h. The latter following this template:
grub-core/kern/arm/dl.c +14 -0
@@ -24,6 +24,7 @@#include <grub/mm.h> #include <grub/i18n.h> #include <grub/arm/reloc.h> +#include <grub/efi/memory.h> struct trampoline_arm {
@@ -278,3 +279,16 @@ grub_arch_dl_check_header (void *ehdr)return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +#ifdef GRUB_MACHINE_EFI +return GRUB_EFI_DL_ALIGN ; +#else +return GRUB_DEFAULT_DL_ALIGN ; +#endif +}
grub-core/kern/arm64/dl.c +14 -0
@@ -24,6 +24,7 @@#include <grub/mm.h> #include <grub/i18n.h> #include <grub/cpu/reloc.h> +#include <grub/efi/memory.h> #define LDR 0x58000050#define BR 0xd61f0200
@@ -196,3 +197,16 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +#ifdef GRUB_MACHINE_EFI +return GRUB_EFI_DL_ALIGN ; +#else +return GRUB_DEFAULT_DL_ALIGN ; +#endif +}
grub-core/kern/dl.c +36 -16
114 lines not on this page · raw message
grub-core/kern/emu/full.c +14 -0
@@ -23,6 +23,7 @@#include <grub/misc.h> #include <grub/emu/misc.h> #include <grub/disk.h> +#include <grub/efi/memory.h> const int grub_no_modules =1 ;
@@ -67,3 +68,16 @@ grub_arch_dl_init_linker (void) }#endif + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +#ifdef GRUB_MACHINE_EFI +return GRUB_EFI_DL_ALIGN ; +#else +return GRUB_DEFAULT_DL_ALIGN ; +#endif +}
grub-core/kern/i386/dl.c +14 -0
@@ -22,6 +22,7 @@#include <grub/misc.h> #include <grub/err.h> #include <grub/i18n.h> +#include <grub/efi/memory.h> /* Check if EHDR is a valid ELF header. */ grub_err_t
@@ -79,3 +80,16 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +#ifdef GRUB_MACHINE_EFI +return GRUB_EFI_DL_ALIGN ; +#else +return GRUB_DEFAULT_DL_ALIGN ; +#endif +}
grub-core/kern/ia64/dl.c +9 -0
@@ -148,3 +148,12 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr, }return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +return GRUB_DEFAULT_DL_ALIGN ; +}
grub-core/kern/mips/dl.c +8 -0
@@ -272,3 +272,11 @@ grub_arch_dl_init_linker (void)grub_dl_register_symbol ("_gp_disp" , &_gp_disp_dummy,0 ,0 ); } +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +return GRUB_DEFAULT_DL_ALIGN ; +}
grub-core/kern/powerpc/dl.c +9 -0
@@ -167,3 +167,12 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +return GRUB_DEFAULT_DL_ALIGN ; +}
grub-core/kern/riscv/dl.c +14 -0
@@ -23,6 +23,7 @@#include <grub/err.h> #include <grub/mm.h> #include <grub/i18n.h> +#include <grub/efi/memory.h> /* * Instructions and instruction encoding are documented in the RISC-V
@@ -344,3 +345,16 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +#ifdef GRUB_MACHINE_EFI +return GRUB_EFI_DL_ALIGN ; +#else +return GRUB_DEFAULT_DL_ALIGN ; +#endif +}
grub-core/kern/sparc64/dl.c +9 -0
@@ -189,3 +189,12 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +return GRUB_DEFAULT_DL_ALIGN ; +}
grub-core/kern/x86_64/dl.c +14 -0
@@ -22,6 +22,7 @@#include <grub/misc.h> #include <grub/err.h> #include <grub/i18n.h> +#include <grub/efi/memory.h> /* Check if EHDR is a valid ELF header. */ grub_err_t
@@ -119,3 +120,16 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,return GRUB_ERR_NONE ; } + +/* +* Tell the loader what our minimum section alignment is. +*/ +grub_size_t +grub_arch_dl_min_alignment (void ) +{ +#ifdef GRUB_MACHINE_EFI +return GRUB_EFI_DL_ALIGN ; +#else +return GRUB_DEFAULT_DL_ALIGN ; +#endif +}
include/grub/dl.h +4 -0
@@ -264,6 +264,8 @@ grub_err_t grub_arch_dl_check_header (void *ehdr);grub_err_t grub_arch_dl_relocate_symbols (grub_dl_t mod,void *ehdr,Elf_Shdr *s,grub_dl_segment_t seg); +grub_size_t +grub_arch_dl_min_alignment (void );#endif #if defined (_mips)
@@ -308,4 +310,6 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,#endif +#define GRUB_DEFAULT_DL_ALIGN 1 +#endif /* ! GRUB_DL_H */
include/grub/efi/memory.h +2 -0
@@ -31,6 +31,8 @@#define GRUB_MMAP_REGISTER_BY_FIRMWARE 1 +#define GRUB_EFI_DL_ALIGN GRUB_EFI_PAGE_SIZE +grub_err_t grub_machine_mmap_register (grub_uint64_t start,grub_uint64_t size,int type,int handle);grub_err_t grub_machine_mmap_unregister (int handle);
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 04/10] modules: load module sections at page-aligned addresses
Quoted 45 lines · Mate Kukri
Currently we load module sections at whatever alignment gcc+ld happened to dump into the ELF section header, which is often less then the page size. Since NX protections are page based, this alignment must be rounded up to page size on platforms supporting NX protections.
This patch switches most EFI platforms to load module sections at 4kB page-aligned addresses. To do so, it adds an new per-arch function,
grub_arch_dl_min_alignment(), which returns the alignment needed for dynamically loaded sections (in bytes). Currently it sets it to 4096 whenGRUB_MACHINE_EFIis true onx86_64, i386, arm, arm64, and emu, and 1-byte alignment on everything else.It then changes the allocation size computation and the loader code in
grub_dl_load_segments()to align the locations and sizes up to these boundaries, and fills any added padding with zeros.All of this happens before relocations are applied, so the relocations factor that in with no change.
Signed-off-by Peter Jones <[email protected]>Signed-off-by Laszlo Ersek <[email protected]>Signed-off-by Jan Setje-Eilers <[email protected]>Signed-off-by Mate Kukri <[email protected]>--- docs/grub-dev.texi | 6 ++--- grub-core/kern/arm/dl.c | 14 ++++++++++ grub-core/kern/arm64/dl.c | 14 ++++++++++ grub-core/kern/dl.c | 52 +++++++++++++++++++++++++------------ grub-core/kern/emu/full.c | 14 ++++++++++ grub-core/kern/i386/dl.c | 14 ++++++++++ grub-core/kern/ia64/dl.c | 9 +++++++ grub-core/kern/mips/dl.c | 8 ++++++ grub-core/kern/powerpc/dl.c | 9 +++++++ grub-core/kern/riscv/dl.c | 14 ++++++++++ grub-core/kern/sparc64/dl.c | 9 +++++++ grub-core/kern/x86_64/dl.c | 14 ++++++++++ include/grub/dl.h | 4 +++ include/grub/efi/memory.h | 2 ++ 14 files changed, 164 insertions(+), 19 deletions(-)docs/grub-dev.texi +0
-0
docs/grub-dev.texi declare startup asm file ($cpu_$platform_startup) as well as any other files · line 1001
Quoted 9 lines
docs/grub-dev.texi +3
-3@@ -996,9 +996,9 @@ declare startup asm file ($cpu_$platform_startup) as well as any other files (e.g. init.c and callwrap.S) (e.g. $cpu_$platform = kern/$cpu/$platform/init.c). At this stage you will also need to add dummy dl.c and cache.S with functions grub_err_t grub_arch_dl_check_header (void *ehdr), grub_err_t -grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c) and -void grub_arch_sync_caches (void *address, grub_size_t len) (cache.S). They -won't be used for now. +grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c), grub_uint32_t +grub_arch_dl_min_alignment (void), and void grub_arch_sync_caches (void +*address, grub_size_t len) (cache.S). They won't be used for now.
I would put full function names in one line instead of breaking them in random places. However, if you make suggested change below then you do not need to update the docs...
[...]
include/grub/dl.h grub_err_t grub_arch_dl_check_header (void *ehdr); · line 268
Quoted 10 lines
include/grub/dl.h +2
-0@@ -264,6 +264,8 @@ grub_err_t grub_arch_dl_check_header (void *ehdr);grub_err_t grub_arch_dl_relocate_symbols (grub_dl_t mod,void *ehdr,Elf_Shdr *s,grub_dl_segment_t seg); +grub_size_t +grub_arch_dl_min_alignment (void );
You can drop this function and simplify this patch if you do...
include/grub/dl.h grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp, · line 313
Quoted 8 lines
include/grub/dl.h +1
-0#endif #if defined (_mips)@@ -308,4 +310,6 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,#endif +#define GRUB_DEFAULT_DL_ALIGN 1
#ifdef GRUB_MACHINE_EFI
#define GRUB_DL_ALIGN GRUB_EFI_PAGE_SIZE
#else
#define GRUB_DL_ALIGN 1
#endif
... then use GRUB_DL_ALIGN instead of grub_arch_dl_min_alignment()...
Of course you need at the beginning of this file:
#ifdef GRUB_MACHINE_EFI #include <grub/efi/memory.h> #endif
Quoted 6 lines
include/grub/dl.h +0
-0#endif /* ! GRUB_DL_H */ include/grub/efi/memory.h +0
-0
include/grub/efi/memory.h line 38
Quoted 8 lines
include/grub/efi/memory.h +2
-0@@ -31,6 +31,8 @@#define GRUB_MMAP_REGISTER_BY_FIRMWARE 1 +#define GRUB_EFI_DL_ALIGN GRUB_EFI_PAGE_SIZE +grub_err_t grub_machine_mmap_register (grub_uint64_t start,grub_uint64_t size,int type,int handle);grub_err_t grub_machine_mmap_unregister (int handle);
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 5/10 nx: add memory attribute get/set API
In reply to Mate KukriFor NX, we need to set the page access permission attributes for write and execute permissions.
This patch adds two new primitives, grub_set_mem_attrs() and grub_clear_mem_attrs(), and associated constant definitions, to be used for that purpose.
For most platforms, it adds a dummy implementation that returns GRUB_ERR_NONE.
On EFI platforms, it implements the primitives using the EFI Memory Attribute Protocol (defined in UEFI 2.10 specification).
--- grub-core/kern/efi/mm.c | 127 ++++++++++++++++++++++++++++++++++++++++ include/grub/efi/api.h | 25 ++++++++ include/grub/mm.h | 33 +++++++++++ 3 files changed, 185 insertions(+)
grub-core/kern/efi/mm.c +127 -0
@@ -689,3 +689,130 @@ grub_efi_get_ram_base(grub_addr_t *base_addr)return GRUB_ERR_NONE ; }#endif + +static inline grub_uint64_t +grub_mem_attrs_to_uefi_mem_attrs (grub_uint64_t attrs) +{ +grub_uint64_t ret =GRUB_EFI_MEMORY_RP | +GRUB_EFI_MEMORY_RO | +GRUB_EFI_MEMORY_XP ; + +if (attrs &GRUB_MEM_ATTR_R ) + ret &= ~GRUB_EFI_MEMORY_RP ; + +if (attrs &GRUB_MEM_ATTR_W ) + ret &= ~GRUB_EFI_MEMORY_RO ; + +if (attrs &GRUB_MEM_ATTR_X ) + ret &= ~GRUB_EFI_MEMORY_XP ; + +return ret; +} + +static inline grub_uint64_t +uefi_mem_attrs_to_grub_mem_attrs (grub_uint64_t attrs) +{ +grub_uint64_t ret =GRUB_MEM_ATTR_R | +GRUB_MEM_ATTR_W | +GRUB_MEM_ATTR_X ; + +if (attrs &GRUB_EFI_MEMORY_RP ) + ret &= ~GRUB_MEM_ATTR_R ; + +if (attrs &GRUB_EFI_MEMORY_RO ) + ret &= ~GRUB_MEM_ATTR_W ; + +if (attrs &GRUB_EFI_MEMORY_XP ) + ret &= ~GRUB_MEM_ATTR_X ; + +return ret; +} + +grub_err_t +grub_get_mem_attrs (grub_addr_t addr,grub_size_t size,grub_uint64_t *attrs) +{ +grub_efi_memory_attribute_protocol_t *proto; +grub_efi_physical_address_t physaddr = addr; +grub_guid_t protocol_guid =GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID ; +grub_efi_status_t efi_status; + +if (physaddr &0xfff || size &0xfff || size ==0 || attrs ==NULL ) + { +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("grub_get_mem_attrs() called with invalid arguments" )); + } + + proto =grub_efi_locate_protocol (&protocol_guid,0 ); +if (!proto) + { +/* No protocol -> do nothing, all memory is RWX in boot services */ + *attrs =GRUB_MEM_ATTR_R |GRUB_MEM_ATTR_W |GRUB_MEM_ATTR_X ; +return GRUB_ERR_NONE ; + } + + efi_status = proto->get_memory_attributes (proto, physaddr, size, attrs); +if (efi_status !=GRUB_EFI_SUCCESS ) + { +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("grub_get_mem_attrs() called with invalid arguments" )); + } + + *attrs =uefi_mem_attrs_to_grub_mem_attrs (*attrs); + +grub_dprintf ("nx" ,"get 0x%" PRIxGRUB_ADDR"-0x%" PRIxGRUB_ADDR":%c%c%c\n" , + addr, addr + size -1 , + (*attrs &GRUB_MEM_ATTR_R ) ?'r' :'-' , + (*attrs &GRUB_MEM_ATTR_W ) ?'w' :'-' , + (*attrs &GRUB_MEM_ATTR_X ) ?'x' :'-' ); + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_update_mem_attrs (grub_addr_t addr,grub_size_t size, +grub_uint64_t set_attrs,grub_uint64_t clear_attrs) +{ +grub_efi_memory_attribute_protocol_t *proto; +grub_efi_physical_address_t physaddr = addr; +grub_guid_t protocol_guid =GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID ; +grub_efi_status_t efi_status =GRUB_EFI_SUCCESS ; +grub_uint64_t uefi_set_attrs, uefi_clear_attrs; + + +if (physaddr &0xfff || size &0xfff || size ==0 ) + { +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("grub_update_mem_attrs() called with invalid arguments" )); + } + + proto =grub_efi_locate_protocol (&protocol_guid,0 ); +if (!proto) + { +/* No protocol -> do nothing, all memory is RWX in boot services */ +return GRUB_ERR_NONE ; + } + + uefi_set_attrs =grub_mem_attrs_to_uefi_mem_attrs (set_attrs); + uefi_clear_attrs =grub_mem_attrs_to_uefi_mem_attrs (clear_attrs); +if (uefi_set_attrs) + efi_status = proto->set_memory_attributes (proto, physaddr, size, uefi_set_attrs); +if (efi_status ==GRUB_EFI_SUCCESS && uefi_clear_attrs) + efi_status = proto->clear_memory_attributes (proto, physaddr, size, uefi_clear_attrs); + +if (efi_status !=GRUB_EFI_SUCCESS ) + { +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("grub_update_mem_attrs() called with invalid arguments" )); + } + +grub_dprintf ("nx" ,"set +%s%s%s -%s%s%s on 0x%" PRIxGRUB_ADDR"-0x%" PRIxGRUB_ADDR"\n" , + (set_attrs &GRUB_MEM_ATTR_R ) ?"r" :"" , + (set_attrs &GRUB_MEM_ATTR_W ) ?"w" :"" , + (set_attrs &GRUB_MEM_ATTR_X ) ?"x" :"" , + (clear_attrs &GRUB_MEM_ATTR_R ) ?"r" :"" , + (clear_attrs &GRUB_MEM_ATTR_W ) ?"w" :"" , + (clear_attrs &GRUB_MEM_ATTR_X ) ?"x" :"" , + addr, addr + size -1 ); + +return GRUB_ERR_NONE ; +}
include/grub/efi/api.h +25 -0
@@ -379,6 +379,11 @@ {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f } \ } +#define GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID \ + { 0xf4560cf6, 0x40ec, 0x4b4a, \ + { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 } \ + } +struct grub_efi_sal_system_table {grub_uint32_t signature;
@@ -1815,4 +1820,24 @@ struct initrd_media_device_path { } GRUB_PACKED;typedef struct initrd_media_device_path initrd_media_device_path_t ; +struct grub_efi_memory_attribute_protocol +{ + grub_efi_status_t (__grub_efi_api *get_memory_attributes) ( + struct grub_efi_memory_attribute_protocol *this, + grub_efi_physical_address_t base_address, + grub_efi_uint64_t length, + grub_efi_uint64_t *attributes); + grub_efi_status_t (__grub_efi_api *set_memory_attributes) ( + struct grub_efi_memory_attribute_protocol *this, + grub_efi_physical_address_t base_address, + grub_efi_uint64_t length, + grub_efi_uint64_t attributes); + grub_efi_status_t (__grub_efi_api *clear_memory_attributes) ( + struct grub_efi_memory_attribute_protocol *this, + grub_efi_physical_address_t base_address, + grub_efi_uint64_t length, + grub_efi_uint64_t attributes); +}; +typedef struct grub_efi_memory_attribute_protocol grub_efi_memory_attribute_protocol_t ; +#endif /* ! GRUB_EFI_API_HEADER */
include/grub/mm.h +33 -0
@@ -23,6 +23,7 @@#include <grub/err.h> #include <grub/types.h> #include <grub/symbol.h> +#include <grub/err.h> #include <config.h> #ifndef NULL
@@ -56,6 +57,38 @@ void *EXPORT_FUNC(grub_realloc) (void *ptr, grub_size_t size);void *EXPORT_FUNC (grub_memalign ) (grub_size_t align,grub_size_t size);#endif +#define GRUB_MEM_ATTR_R 0x0000000000000004LLU +#define GRUB_MEM_ATTR_W 0x0000000000000002LLU +#define GRUB_MEM_ATTR_X 0x0000000000000001LLU + +#ifdef GRUB_MACHINE_EFI +grub_err_t EXPORT_FUNC (grub_get_mem_attrs ) (grub_addr_t addr, +grub_size_t size, +grub_uint64_t *attrs); +grub_err_t EXPORT_FUNC (grub_update_mem_attrs ) (grub_addr_t addr, +grub_size_t size, +grub_uint64_t set_attrs, +grub_uint64_t clear_attrs); +#else /* !GRUB_MACHINE_EFI */ +static inline grub_err_t +grub_get_mem_attrs (grub_addr_t addr __attribute__((__unused__)), +grub_size_t size __attribute__((__unused__)), +grub_uint64_t *attrs __attribute__((__unused__))) +{ + *attrs =GRUB_MEM_ATTR_R |GRUB_MEM_ATTR_W |GRUB_MEM_ATTR_X ; +return GRUB_ERR_NONE ; +} + +static inline grub_err_t +grub_update_mem_attrs (grub_addr_t addr __attribute__((__unused__)), +grub_size_t size __attribute__((__unused__)), +grub_uint64_t set_attrs __attribute__((__unused__)), +grub_uint64_t clear_attrs __attribute__((__unused__))) +{ +return GRUB_ERR_NONE ; +} +#endif /* GRUB_MACHINE_EFI */ +void grub_mm_check_real (const char *file,int line);#define grub_mm_check () grub_mm_check_real (GRUB_FILE, __LINE__);
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 6/10 nx: set page permissions for loaded modules.
In reply to Mate KukriFor NX, we need to set write and executable permissions on the sections of grub modules when we load them.
On sections with SHF_ALLOC set, which is typically everything except .modname and the symbol and string tables, this patch clears the Read Only flag on sections that have the ELF flag SHF_WRITE set, and clears the No eXecute flag on sections with SHF_EXECINSTR set. In all other cases it sets both flags.
--- grub-core/kern/dl.c | 104 ++++++++++++++++++++++++++++++++++++++------ include/grub/dl.h | 46 ++++++++++++++++++++ 2 files changed, 137 insertions(+), 13 deletions(-)
grub-core/kern/dl.c +91 -13
@@ -615,25 +615,97 @@ grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)grub_dl_segment_t seg;grub_err_t err; -/* Find the target segment. */ -for (seg = mod->segment; seg; seg = seg->next) -if (seg->section == s->sh_info) -break ; + seg =grub_dl_find_segment (mod, s->sh_info); +if (!seg) +continue ; -if (seg) - { -if (!mod->symtab) -return grub_error (GRUB_ERR_BAD_MODULE ,"relocation without symbol table" ); +if (!mod->symtab) +return grub_error (GRUB_ERR_BAD_MODULE ,"relocation without symbol table" ); - err =grub_arch_dl_relocate_symbols (mod, ehdr, s, seg); -if (err) -return err; - } + err =grub_arch_dl_relocate_symbols (mod, ehdr, s, seg); +if (err) +return err; }return GRUB_ERR_NONE ; } +/* Only define this on EFI to save space in core */ +#ifdef GRUB_MACHINE_EFI +static grub_err_t +grub_dl_set_mem_attrs (grub_dl_t mod,void *ehdr) +{ +unsigned i; +const Elf_Shdr *s; +const Elf_Ehdr *e = ehdr; +grub_err_t err; +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) +grub_size_t arch_addralign =grub_arch_dl_min_alignment (); +grub_addr_t tgaddr; +grub_size_t tgsz; +#endif + +for (i =0 , s = (const Elf_Shdr *)((const char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize)) + { +grub_dl_segment_t seg; +grub_uint64_t set_attrs =GRUB_MEM_ATTR_R ; +grub_uint64_t clear_attrs =GRUB_MEM_ATTR_W |GRUB_MEM_ATTR_X ; + + seg =grub_dl_find_segment (mod, i); +if (!seg) +continue ; + +if (seg->size ==0 || !(s->sh_flags &SHF_ALLOC )) +continue ; + +if (s->sh_flags &SHF_WRITE ) + { + set_attrs |=GRUB_MEM_ATTR_W ; + clear_attrs &= ~GRUB_MEM_ATTR_W ; + } + +if (s->sh_flags &SHF_EXECINSTR ) + { + set_attrs |=GRUB_MEM_ATTR_X ; + clear_attrs &= ~GRUB_MEM_ATTR_X ; + } + + err =grub_update_mem_attrs ((grub_addr_t)(seg->addr), seg->size, + set_attrs, clear_attrs); +if (err !=GRUB_ERR_NONE ) +return err; + } + +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) + tgaddr =grub_min ((grub_addr_t )mod->tramp, (grub_addr_t )mod->got); + tgsz =grub_max ((grub_addr_t )mod->trampptr, (grub_addr_t )mod->gotptr) - tgaddr; + +if (tgsz) + { + tgsz =ALIGN_UP (tgsz, arch_addralign); + +if (tgaddr < (grub_addr_t )mod->base || + tgsz > (grub_addr_t)-1 - tgaddr || + tgaddr + tgsz > (grub_addr_t )mod->base + mod->sz) +return grub_error (GRUB_ERR_BUG , +"BUG: trying to protect pages outside of module " +"allocation (\"%s\"): module base %p, size 0x%" + PRIxGRUB_SIZE"; tramp/GOT base 0x%" PRIxGRUB_ADDR +", size 0x%" PRIxGRUB_SIZE, + mod->name, mod->base, mod->sz, tgaddr, tgsz); + err =grub_update_mem_attrs (tgaddr, tgsz,GRUB_MEM_ATTR_R |GRUB_MEM_ATTR_X , +GRUB_MEM_ATTR_W ); +if (err !=GRUB_ERR_NONE ) +return err; + } +#endif + +return GRUB_ERR_NONE ; +} +#endif +/* Load a module from core memory. */ grub_dl_t grub_dl_load_core_noinit (void *addr,grub_size_t size)
@@ -667,6 +739,7 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size) mod->ref_count =1 ;grub_dprintf ("modules" ,"relocating to %p\n" , mod); +/* Me, Vladimir Serbinenko, hereby I add this module check as per new GNU module policy. Note that this license check is informative only. Modules have to be licensed under GPLv3 or GPLv3+ (optionally
@@ -680,7 +753,12 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size) || grub_dl_resolve_dependencies (mod, e) || grub_dl_load_segments (mod, e) || grub_dl_resolve_symbols (mod, e) - || grub_dl_relocate_symbols (mod, e)) + || grub_dl_relocate_symbols (mod, e) +#ifdef GRUB_MACHINE_EFI + || grub_dl_set_mem_attrs (mod, e)) +#else + ) +#endif { mod->fini =0 ;grub_dl_unload (mod);
include/grub/dl.h +46 -0
58 lines not on this page · raw message
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 06/10] nx: set page permissions for loaded modules.
Quoted 24 lines · Mate Kukri
For NX, we need to set write and executable permissions on the sections of grub modules when we load them.
On sections with
SHF_ALLOCset, which is typically everything except .modname and the symbol and string tables, this patch clears the Read Only flag on sections that have the ELF flagSHF_WRITEset, and clears the No eXecute flag on sections withSHF_EXECINSTRset. In all other cases it sets both flags.Signed-off-by Peter Jones <[email protected]>Signed-off-by Robbie Harwood <[email protected]>Signed-off-by Laszlo Ersek <[email protected]>Signed-off-by Jan Setje-Eilers <[email protected]>Signed-off-by Mate Kukri <[email protected]>--- grub-core/kern/dl.c | 104 ++++++++++++++++++++++++++++++++++++++------ include/grub/dl.h | 46 ++++++++++++++++++++ 2 files changed, 137 insertions(+), 13 deletions(-)grub-core/kern/dl.c +0
-0
grub-core/kern/dl.c grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) · line 618
Quoted 8 lines
grub-core/kern/dl.c +1
-4@@ -615,25 +615,97 @@ grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)grub_dl_segment_t seg;grub_err_t err; -/* Find the target segment. */ -for (seg = mod->segment; seg; seg = seg->next) -if (seg->section == s->sh_info) -break ; + seg =grub_dl_find_segment (mod, s->sh_info);
Wrong coding style, missing space before "(". Please fix similar mistakes below...
Quoted 7 lines
grub-core/kern/dl.c +2
-4+if (!seg) +continue ; -if (seg) - { -if (!mod->symtab) -return grub_error (GRUB_ERR_BAD_MODULE ,"relocation without symbol table" );
grub-core/kern/dl.c grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) · line 627
Quoted 10 lines
grub-core/kern/dl.c +5
-4+if (!mod->symtab) +return grub_error (GRUB_ERR_BAD_MODULE ,"relocation without symbol table" ); - err =grub_arch_dl_relocate_symbols (mod, ehdr, s, seg); -if (err) -return err; - } + err =grub_arch_dl_relocate_symbols (mod, ehdr, s, seg); +if (err) +return err;
This whole grub_dl_relocate_symbols() change should go to separate patch. It looks like code simplification.
Quoted 17 lines
grub-core/kern/dl.c +12
-0}return GRUB_ERR_NONE ; } +/* Only define this on EFI to save space in core */ +#ifdef GRUB_MACHINE_EFI +static grub_err_t +grub_dl_set_mem_attrs (grub_dl_t mod,void *ehdr) +{ +unsigned i; +const Elf_Shdr *s; +const Elf_Ehdr *e = ehdr; +grub_err_t err; +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) +grub_size_t arch_addralign =grub_arch_dl_min_alignment (); +grub_addr_t tgaddr;
grub-core/kern/dl.c grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) · line 654
Quoted 10 lines
grub-core/kern/dl.c +10
-0+grub_size_t tgsz; +#endif + +for (i =0 , s = (const Elf_Shdr *)((const char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize)) + { +grub_dl_segment_t seg; +grub_uint64_t set_attrs =GRUB_MEM_ATTR_R ; +grub_uint64_t clear_attrs =GRUB_MEM_ATTR_W |GRUB_MEM_ATTR_X ;
Wrong coding style, missing space before and after "|"...
Quoted 18 lines
grub-core/kern/dl.c +18
-0+ seg =grub_dl_find_segment (mod, i); +if (!seg) +continue ; + +if (seg->size ==0 || !(s->sh_flags &SHF_ALLOC )) +continue ; + +if (s->sh_flags &SHF_WRITE ) + { + set_attrs |=GRUB_MEM_ATTR_W ; + clear_attrs &= ~GRUB_MEM_ATTR_W ; + } + +if (s->sh_flags &SHF_EXECINSTR ) + { + set_attrs |=GRUB_MEM_ATTR_X ; + clear_attrs &= ~GRUB_MEM_ATTR_X ; + }
grub-core/kern/dl.c grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) · line 683
Quoted 10 lines
grub-core/kern/dl.c +10
-0+ + err =grub_update_mem_attrs ((grub_addr_t)(seg->addr), seg->size, + set_attrs, clear_attrs); +if (err !=GRUB_ERR_NONE ) +return err; + } + +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) + tgaddr =grub_min ((grub_addr_t )mod->tramp, (grub_addr_t )mod->got); + tgsz =grub_max ((grub_addr_t )mod->trampptr, (grub_addr_t )mod->gotptr) - tgaddr;
Wrong coding style, missing spaces after grub_min/grub_max and (grub_addr_t) casts, i.e. "(grub_addr_t) mod->gotptr). Please fix similar problems everywhere.
Quoted 5 lines
grub-core/kern/dl.c +5
-0+if (tgsz) + { + tgsz =ALIGN_UP (tgsz, arch_addralign); + + if (tgaddr < (grub_addr_t)mod->base ||
grub-core/kern/dl.c grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) · line 699
Quoted 10 lines
grub-core/kern/dl.c +10
-0+ tgsz > (grub_addr_t)-1 - tgaddr || + tgaddr + tgsz > (grub_addr_t)mod->base + mod->sz) +return grub_error (GRUB_ERR_BUG , +"BUG: trying to protect pages outside of module " +"allocation (\"%s\"): module base %p, size 0x%" + PRIxGRUB_SIZE"; tramp/GOT base 0x%" PRIxGRUB_ADDR +", size 0x%" PRIxGRUB_SIZE, + mod->name, mod->base, mod->sz, tgaddr, tgsz); + err =grub_update_mem_attrs (tgaddr, tgsz,GRUB_MEM_ATTR_R |GRUB_MEM_ATTR_X , +GRUB_MEM_ATTR_W );
You do not need wrap this line.
grub-core/kern/dl.c grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) · line 703
Quoted 4 lines
grub-core/kern/dl.c +4
-0+if (err !=GRUB_ERR_NONE ) + return err; + } +#endif
If you add an empty version of grub_dl_set_mem_attrs() here then you will simplify its call below...
grub-core/kern/dl.c grub_dl_load_core_noinit (void *addr, grub_size_t size) · line 742
Quoted 12 lines
grub-core/kern/dl.c +5
-0+ return GRUB_ERR_NONE; +} +#endif +/* Load a module from core memory. */ grub_dl_t grub_dl_load_core_noinit (void *addr,grub_size_t size)@@ -667,6 +739,7 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size) mod->ref_count =1 ;grub_dprintf ("modules" ,"relocating to %p\n" , mod); +
Please drop this change.
Quoted 30 lines
grub-core/kern/dl.c +6
-1/* Me, Vladimir Serbinenko, hereby I add this module check as per new GNU module policy. Note that this license check is informative only. Modules have to be licensed under GPLv3 or GPLv3+ (optionally @@ -680,7 +753,12 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size) || grub_dl_resolve_dependencies (mod, e) || grub_dl_load_segments (mod, e) || grub_dl_resolve_symbols (mod, e) - || grub_dl_relocate_symbols (mod, e)) + || grub_dl_relocate_symbols (mod, e) +#ifdef GRUB_MACHINE_EFI + || grub_dl_set_mem_attrs (mod, e)) +#else + ) +#endif { mod->fini =0 ;grub_dl_unload (mod);include/grub/dl.h +1
-0@@ -27,6 +27,7 @@#include <grub/elf.h> #include <grub/list.h> #include <grub/misc.h> +#include <grub/mm.h> #endif /*
include/grub/dl.h grub_dl_is_persistent (grub_dl_t mod) · line 258
Quoted 4 lines
include/grub/dl.h +1
-0@@ -254,6 +255,51 @@ grub_dl_is_persistent (grub_dl_t mod)return mod->persistent; } +#pragma GCC diagnostic ignored "-Wcast-align"
I commented this in previous version and it does not seem solved. Please take into account my comments from v4 or tell where I am wrong.
include/grub/dl.h grub_dl_is_persistent (grub_dl_t mod) · line 270
Quoted 11 lines
include/grub/dl.h +11
-0+static inline const char * +grub_dl_get_section_name (const Elf_Ehdr *e,const Elf_Shdr *s) +{ +Elf_Shdr *str_s; +const char *str; + + str_s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize); + str = (char *) e + str_s->sh_offset; + +return str + s->sh_name; +}
This function definition does not seem to belong to this patch.
Quoted 7 lines
include/grub/dl.h +7
-0+static inline long +grub_dl_find_section_index (Elf_Ehdr *e,const char *name) +{ +Elf_Shdr *s; +const char *str; +unsigned i; +
include/grub/dl.h grub_dl_is_persistent (grub_dl_t mod) · line 288
Quoted 10 lines
include/grub/dl.h +10
-0+ s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize); + str = (char *) e + s->sh_offset; + +for (i =0 , s = (Elf_Shdr *) ((char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize)) +if (grub_strcmp (str + s->sh_name, name) ==0 ) +return (long )i; +return -1 ; +}
Ditto.
Quoted 4 lines
include/grub/dl.h +4
-0+/* Return the segment for a section of index N */ +static inline grub_dl_segment_t +grub_dl_find_segment (grub_dl_t mod,unsigned n) +{
include/grub/dl.h grub_dl_is_persistent (grub_dl_t mod) · line 303
Quoted 10 lines
include/grub/dl.h +9
-0+grub_dl_segment_t seg; + +for (seg = mod->segment; seg; seg = seg->next) +if (seg->section == n) +return seg; + + return NULL; +} +#endif
Should not "-Wcast-align" be restored here? Again, I commented this in v4...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 7/10 nx: set the nx compatible flag in EFI GRUB images
In reply to Mate KukriFor NX, we need the GRUB binary to announce that it is compatible with the NX feature. This implies that when loading the executable GRUB image, several attributes are true:
- the binary doesn't need an executable stack
- the binary doesn't need sections to be both executable and writable
- the binary knows how to use the EFI Memory Attributes protocol on code it is loading.
This patch
- adds a definition for the PE DLL Characteristics flag GRUB_PE32_NX_COMPAT
- changes grub-mkimage to set that flag.
--- include/grub/efi/pe32.h | 2 ++ util/mkimage.c | 1 + 2 files changed, 3 insertions(+)
include/grub/efi/pe32.h +2 -0
@@ -231,6 +231,8 @@ struct grub_pe64_optional_header#define GRUB_PE32_SUBSYSTEM_EFI_APPLICATION 10 +#define GRUB_PE32_NX_COMPAT 0x0100 +#define GRUB_PE32_NUM_DATA_DIRECTORIES 16struct grub_pe32_section_table
util/mkimage.c +1 -0
@@ -1417,6 +1417,7 @@ grub_install_generate_image (const char *dir, const char *prefix,#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wdangling-pointer"#endif +PE_OHDR (o32, o64, dll_characteristics) =grub_host_to_target16 (GRUB_PE32_NX_COMPAT );PE_OHDR (o32, o64, header_size) =grub_host_to_target32 (header_size);PE_OHDR (o32, o64, entry_addr) =grub_host_to_target32 (layout.start_address);PE_OHDR (o32, o64, image_base) =0 ;
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v5 07/10] nx: set the nx compatible flag in EFI GRUB images
I don't see any surprises with the first 7, if you want, you're welcome to change my SOB to RB. If someone forwards me the last three, I'm happy to take a look at them as well.
-jan
Quoted 40 lines · Mate Kukri
For NX, we need the GRUB binary to announce that it is compatible with the NX feature. This implies that when loading the executable GRUB image, several attributes are true:
- the binary doesn't need an executable stack
- the binary doesn't need sections to be both executable and writable
- the binary knows how to use the EFI Memory Attributes protocol on code it is loading.This patch
- adds a definition for the PE DLL Characteristics flagGRUB_PE32_NX_COMPAT
- changes grub-mkimage to set that flag.Signed-off-by Peter Jones <[email protected]>Signed-off-by Jan Setje-Eilers <[email protected]>Signed-off-by Mate Kukri <[email protected]>Reviewed-by Daniel Kiper <[email protected]>--- include/grub/efi/pe32.h | 2 ++ util/mkimage.c | 1 + 2 files changed, 3 insertions(+)include/grub/efi/pe32.h +2
-0@@ -231,6 +231,8 @@ struct grub_pe64_optional_header#define GRUB_PE32_SUBSYSTEM_EFI_APPLICATION 10 +#define GRUB_PE32_NX_COMPAT 0x0100 +#define GRUB_PE32_NUM_DATA_DIRECTORIES 16struct grub_pe32_section_table util/mkimage.c +0
-0
util/mkimage.c grub_install_generate_image (const char *dir, const char *prefix, · line 1423
Quoted 7 lines
util/mkimage.c +1
-0@@ -1417,6 +1417,7 @@ grub_install_generate_image (const char *dir, const char *prefix,#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wdangling-pointer"#endif +PE_OHDR (o32, o64, dll_characteristics) =grub_host_to_target16 (GRUB_PE32_NX_COMPAT );PE_OHDR (o32, o64, header_size) =grub_host_to_target32 (header_size);PE_OHDR (o32, o64, entry_addr) =grub_host_to_target32 (layout.start_address);PE_OHDR (o32, o64, image_base) =0 ;
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 8/10 efi: Provide wrappers for load_image, start_image, unload_image
In reply to Mate KukriFrom: Julian Andres Klode <[email protected]>
These can be used to register a different implementation later, for example, when shim provides a protocol with those functions.
--- grub-core/kern/efi/efi.c | 57 ++++++++++++++++++++++++++++++ grub-core/loader/efi/chainloader.c | 13 +++---- grub-core/loader/efi/linux.c | 12 +++---- include/grub/efi/efi.h | 37 +++++++++++++++++++ 4 files changed, 104 insertions(+), 15 deletions(-)
grub-core/kern/efi/efi.c +57 -0
@@ -1049,3 +1049,60 @@ grub_efi_find_configuration_table (const grub_guid_t *target_guid)return 0 ; } + +static const grub_efi_loader_t *override_loader =NULL ; + +grub_err_t +grub_efi_register_loader (const grub_efi_loader_t *loader) +{ +if (override_loader !=NULL ) +return grub_error (GRUB_ERR_BUG ,"trying to register different loader" ); + override_loader = loader; +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_efi_unregister_loader (const grub_efi_loader_t *loader) +{ +if (loader != override_loader) +return grub_error (GRUB_ERR_BUG ,"trying to unregister different loader" ); + + override_loader =NULL ; +return GRUB_ERR_NONE ; +} + +grub_efi_status_t +grub_efi_load_image (grub_efi_boolean_t boot_policy, +grub_efi_handle_t parent_image_handle, +grub_efi_device_path_t *file_path,void *source_buffer, +grub_efi_uintn_t source_size, +grub_efi_handle_t *image_handle) +{ +if (override_loader !=NULL ) +return override_loader->load_image (boot_policy, parent_image_handle, + file_path, source_buffer, source_size, + image_handle); +return grub_efi_system_table->boot_services->load_image ( + boot_policy, parent_image_handle, file_path, source_buffer, source_size, + image_handle); +} + +grub_efi_status_t +grub_efi_start_image (grub_efi_handle_t image_handle, +grub_efi_uintn_t *exit_data_size, +grub_efi_char16_t **exit_data) +{ +if (override_loader !=NULL ) +return override_loader->start_image (image_handle, exit_data_size, + exit_data); +return grub_efi_system_table->boot_services->start_image ( + image_handle, exit_data_size, exit_data); +} + +grub_efi_status_t +grub_efi_unload_image (grub_efi_handle_t image_handle) +{ +if (override_loader !=NULL ) +return override_loader->unload_image (image_handle); +return grub_efi_system_table->boot_services->unload_image (image_handle); +}
grub-core/loader/efi/chainloader.c +5 -8
@@ -50,14 +50,12 @@ grub_chainloader_unload (void *context) {grub_efi_handle_t image_handle = (grub_efi_handle_t ) context;grub_efi_loaded_image_t *loaded_image; -grub_efi_boot_services_t *b; loaded_image =grub_efi_get_loaded_image (image_handle);if (loaded_image !=NULL )grub_free (loaded_image->load_options); - b = grub_efi_system_table->boot_services; - b->unload_image (image_handle); +grub_efi_unload_image (image_handle);grub_dl_unref (my_mod);return GRUB_ERR_NONE ;
@@ -73,7 +71,7 @@ grub_chainloader_boot (void *context)grub_efi_char16_t *exit_data =NULL ; b = grub_efi_system_table->boot_services; - status = b->start_image (image_handle, &exit_data_size, &exit_data); + status =grub_efi_start_image (image_handle, &exit_data_size, &exit_data);if (status !=GRUB_EFI_SUCCESS ) {if (exit_data)
@@ -343,9 +341,8 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), }#endif - status = b->load_image (0 , grub_efi_image_handle, file_path, - boot_image, size, - &image_handle); + status =grub_efi_load_image (0 , grub_efi_image_handle, file_path, + boot_image, size, &image_handle);if (status !=GRUB_EFI_SUCCESS ) {if (status ==GRUB_EFI_OUT_OF_RESOURCES )
@@ -422,7 +419,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)), b->free_pages (address, pages);if (image_handle !=NULL ) - b->unload_image (image_handle); +grub_efi_unload_image (image_handle);grub_dl_unref (my_mod);
grub-core/loader/efi/linux.c +5 -7
@@ -187,7 +187,6 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) {grub_efi_memory_mapped_device_path_t *mempath;grub_efi_handle_t image_handle; -grub_efi_boot_services_t *b;grub_efi_status_t status;grub_efi_loaded_image_t *loaded_image;int len;
@@ -207,10 +206,9 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) mempath[1 ].header.subtype =GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE ; mempath[1 ].header.length =sizeof (grub_efi_device_path_t); - b = grub_efi_system_table->boot_services; - status = b->load_image (0 , grub_efi_image_handle, - (grub_efi_device_path_t *) mempath, - (void *) addr, size, &image_handle); + status =grub_efi_load_image (0 , grub_efi_image_handle, + (grub_efi_device_path_t *)mempath, + (void *)addr, size, &image_handle);if (status !=GRUB_EFI_SUCCESS )return grub_error (GRUB_ERR_BAD_OS ,"cannot load image" );
@@ -235,14 +233,14 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) (grub_uint8_t *) args, len, NULL);grub_dprintf ("linux" ,"starting image %p\n" , image_handle); - status = b->start_image (image_handle,0 ,NULL ); + status =grub_efi_start_image (image_handle,0 ,NULL );/* When successful, not reached */ grub_error (GRUB_ERR_BAD_OS ,"start_image() returned 0x%" PRIxGRUB_EFI_UINTN_T, status);grub_efi_free_pages ((grub_addr_t ) loaded_image->load_options,GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); unload: - b->unload_image (image_handle); +grub_efi_unload_image (image_handle);return grub_errno; }
include/grub/efi/efi.h +37 -0
@@ -132,6 +132,43 @@ grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file,grub_err_t grub_arch_efi_linux_boot_image (grub_addr_t addr,grub_size_t size,char *args); +grub_efi_status_t +EXPORT_FUNC (grub_efi_load_image ) (grub_efi_boolean_t boot_policy, +grub_efi_handle_t parent_image_handle, +grub_efi_device_path_t *file_path, +void *source_buffer,grub_efi_uintn_t source_size, +grub_efi_handle_t *image_handle); + +grub_efi_status_t +EXPORT_FUNC (grub_efi_start_image ) (grub_efi_handle_t image_handle, +grub_efi_uintn_t *exit_data_size, +grub_efi_char16_t **exit_data); + +grub_efi_status_t +EXPORT_FUNC (grub_efi_unload_image ) (grub_efi_handle_t image_handle); + +typedef struct grub_efi_loader +{ + grub_efi_status_t (__grub_efi_api *load_image) (grub_efi_boolean_t boot_policy, + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, + void *source_buffer, + grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle); + + grub_efi_status_t (__grub_efi_api *start_image) (grub_efi_handle_t image_handle, + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data); + + grub_efi_status_t (__grub_efi_api *unload_image) (grub_efi_handle_t image_handle); +}grub_efi_loader_t ; + +grub_err_t +EXPORT_FUNC (grub_efi_register_loader ) (const grub_efi_loader_t *loader); + +grub_err_t +EXPORT_FUNC (grub_efi_unregister_loader ) (const grub_efi_loader_t *loader); +grub_addr_t grub_efi_section_addr (const char *section);void grub_efi_mm_init (void );
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 9/10 efi: Use shim's loader protocol for EFI image verification and loading
In reply to Mate Kukri--- grub-core/kern/efi/sb.c | 39 +++++++++++++----------------------- grub-core/loader/efi/linux.c | 16 --------------- include/grub/efi/api.h | 5 +++++ include/grub/efi/efi.h | 19 +++++++++++------- include/grub/efi/sb.h | 3 --- 5 files changed, 31 insertions(+), 51 deletions(-)
grub-core/kern/efi/sb.c +14 -25
@@ -31,8 +31,9 @@#include <grub/verify.h> static grub_guid_t shim_lock_guid =GRUB_EFI_SHIM_LOCK_GUID ; +static grub_guid_t shim_loader_guid =GRUB_EFI_SHIM_IMAGE_LOADER_GUID ; -static bool shim_lock_enabled = false; +static grub_efi_loader_t *shim_loader =NULL ;/* * Determine whether we're in secure boot mode.
@@ -95,14 +96,6 @@ grub_efi_get_secureboot (void)if (!(attr &GRUB_EFI_VARIABLE_RUNTIME_ACCESS ) && *moksbstate ==1 ) { secureboot =GRUB_EFI_SECUREBOOT_MODE_DISABLED ; -/* -* TODO: Replace this all with shim's LoadImage protocol, delegating policy to it. -* -* We need to set shim_lock_enabled here because we disabled secure boot -* validation *inside* shim but not in the firmware, so we set this variable -* here to trigger that code path, whereas the actual verifier is not enabled. -*/ - shim_lock_enabled = true; goto out; }
@@ -183,14 +176,16 @@ shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),static grub_err_t shim_lock_verifier_write (void *context __attribute__ ((unused)),void *buf,grub_size_t size) { -grub_efi_shim_lock_protocol_t *sl =grub_efi_locate_protocol (&shim_lock_guid,0 ); +grub_efi_handle_t image_handle; -if (!sl) -return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim_lock protocol not found" )); +if (!shim_loader) +return grub_error (GRUB_ERR_ACCESS_DENIED ,N_ ("shim loader protocol not found" )); -if (sl->verify (buf, size) !=GRUB_EFI_SUCCESS ) +if (shim_loader->load_image (false, grub_efi_image_handle,NULL , buf, size, &image_handle) !=GRUB_EFI_SUCCESS )return grub_error (GRUB_ERR_BAD_SIGNATURE ,N_ ("bad shim signature" )); + shim_loader->unload_image (image_handle); +return GRUB_ERR_NONE ; }
@@ -205,11 +200,10 @@ voidgrub_shim_lock_verifier_setup (void) {struct grub_module_header *header; -grub_efi_shim_lock_protocol_t *sl = -grub_efi_locate_protocol (&shim_lock_guid,0 ); + shim_loader =grub_efi_locate_protocol (&shim_loader_guid,0 ); -/* shim_lock is missing, check if GRUB image is built with --disable-shim-lock. */ -if (!sl) +/* shim loader protocol is missing, check if GRUB image is built with --disable-shim-lock. */ +if (!shim_loader) {FOR_MODULES (header) {
@@ -222,17 +216,12 @@ grub_shim_lock_verifier_setup (void)if (grub_efi_get_secureboot () !=GRUB_EFI_SECUREBOOT_MODE_ENABLED )return ; +/* register loader */ +grub_efi_register_loader (shim_loader); +/* Enforce shim_lock_verifier. */ grub_verifier_register (&shim_lock_verifier); - shim_lock_enabled = true; -grub_env_set ("shim_lock" ,"y" );grub_env_export ("shim_lock" ); } - -bool -grub_is_shim_lock_enabled (void ) -{ -return shim_lock_enabled; -}
grub-core/loader/efi/linux.c +0 -16
@@ -460,22 +460,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),grub_dl_ref (my_mod); -if (grub_is_shim_lock_enabled () == true) - { -#if defined(__i386__) || defined(__x86_64__) -grub_dprintf ("linux" ,"shim_lock enabled, falling back to legacy Linux kernel loader\n" ); - - err =grub_cmd_linux_x86_legacy (cmd, argc, argv); - -if (err ==GRUB_ERR_NONE ) -return GRUB_ERR_NONE ; -else - goto fail; -#else -grub_dprintf ("linux" ,"shim_lock enabled, trying Linux kernel EFI stub loader\n" ); -#endif - } -if (argc ==0 ) {grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("filename expected" ));
include/grub/efi/api.h +5 -0
@@ -364,6 +364,11 @@ { 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } \ } +#define GRUB_EFI_SHIM_IMAGE_LOADER_GUID \ + { 0x1f492041, 0xfadb, 0x4e59, \ + {0x9e, 0x57, 0x7c, 0xaf, 0xe7, 0x3a, 0x55, 0xab } \ + } +#define GRUB_EFI_RNG_PROTOCOL_GUID \ { 0x3152bca5, 0xeade, 0x433d, \ { 0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 } \
include/grub/efi/efi.h +12 -7
@@ -150,15 +150,20 @@ EXPORT_FUNC (grub_efi_unload_image) (grub_efi_handle_t image_handle);typedef struct grub_efi_loader { grub_efi_status_t (__grub_efi_api *load_image) (grub_efi_boolean_t boot_policy, - grub_efi_handle_t parent_image_handle, - grub_efi_device_path_t *file_path, - void *source_buffer, - grub_efi_uintn_t source_size, - grub_efi_handle_t *image_handle); + grub_efi_handle_t parent_image_handle, + grub_efi_device_path_t *file_path, + void *source_buffer, + grub_efi_uintn_t source_size, + grub_efi_handle_t *image_handle); grub_efi_status_t (__grub_efi_api *start_image) (grub_efi_handle_t image_handle, - grub_efi_uintn_t *exit_data_size, - grub_efi_char16_t **exit_data); + grub_efi_uintn_t *exit_data_size, + grub_efi_char16_t **exit_data); + + grub_efi_status_t (__grub_efi_api *exit) (grub_efi_handle_t image_handle, + grub_efi_status_t exit_status, + grub_efi_uintn_t exit_data_size, + grub_efi_char16_t *exit_data); grub_efi_status_t (__grub_efi_api *unload_image) (grub_efi_handle_t image_handle); }grub_efi_loader_t ;
include/grub/efi/sb.h +0 -3
@@ -31,9 +31,6 @@extern grub_uint8_t EXPORT_FUNC (grub_efi_get_secureboot ) (void ); -extern bool -EXPORT_FUNC (grub_is_shim_lock_enabled ) (void ); -extern void grub_shim_lock_verifier_setup (void );#else
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v5 10/10 efi: Disallow fallback to legacy Linux loader when shim says NX is required.
In reply to Mate Kukri--- grub-core/kern/efi/sb.c | 28 ++++++++++++++++++++++++++++ grub-core/loader/efi/linux.c | 12 +++++++----- include/grub/efi/api.h | 2 ++ include/grub/efi/sb.h | 2 ++ 4 files changed, 39 insertions(+), 5 deletions(-)
grub-core/kern/efi/sb.c +28 -0
@@ -225,3 +225,31 @@ grub_shim_lock_verifier_setup (void)grub_env_set ("shim_lock" ,"y" );grub_env_export ("shim_lock" ); } + +int +grub_efi_check_nx_required (void ) +{ +int nx_required =1 ;/* assume required, unless we can prove otherwise */ +grub_efi_status_t status; +grub_size_t mok_policy_sz =0 ; +char *mok_policy =NULL ; +grub_uint32_t mok_policy_attrs =0 ; + + status =grub_efi_get_variable_with_attributes ("MokPolicy" , + &(grub_guid_t )GRUB_EFI_SHIM_LOCK_GUID , + &mok_policy_sz, + (void **)&mok_policy, + &mok_policy_attrs); +if (status !=GRUB_EFI_SUCCESS || + mok_policy_sz !=1 || + mok_policy ==NULL || + mok_policy_attrs !=GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS ) + goto out; + + nx_required = !!(mok_policy[0 ] &GRUB_MOK_POLICY_NX_REQUIRED ); + + out: +grub_free (mok_policy); + +return nx_required; +}
grub-core/loader/efi/linux.c +7 -5
@@ -472,21 +472,23 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), kernel_size =grub_file_size (file); - if (grub_arch_efi_linux_load_image_header (file, &lh) != GRUB_ERR_NONE)#if !defined(__i386__) && !defined(__x86_64__) +if (grub_arch_efi_linux_load_image_header (file, &lh) !=GRUB_ERR_NONE ) goto fail;#else - goto fallback; - -if (!initrd_use_loadfile2) +if (grub_arch_efi_linux_load_image_header (file, &lh) !=GRUB_ERR_NONE || + !initrd_use_loadfile2) { +/* We cannot use the legacy loader when NX is required */ +if (grub_efi_check_nx_required ()) + goto fail; +/* * This is a EFI stub image but it is too old to implement the LoadFile2 * based initrd loading scheme, and Linux/x86 does not support the DT * based method either. So fall back to the x86-specific loader that * enters Linux in EFI mode but without going through its EFI stub. */ -fallback:grub_file_close (file);return grub_cmd_linux_x86_legacy (cmd, argc, argv); }
include/grub/efi/api.h +2 -0
@@ -1785,6 +1785,8 @@ struct grub_efi_block_io };typedef struct grub_efi_block_io grub_efi_block_io_t ; +#define GRUB_MOK_POLICY_NX_REQUIRED 0x1 +struct grub_efi_shim_lock_protocol {/*
include/grub/efi/sb.h +2 -0
@@ -33,6 +33,8 @@ EXPORT_FUNC (grub_efi_get_secureboot) (void);extern void grub_shim_lock_verifier_setup (void ); +extern int +EXPORT_FUNC (grub_efi_check_nx_required ) (void );#else static inline grub_uint8_t grub_efi_get_secureboot (void )
Signature · 7 lines
2.39.2 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel