Skip to contentSkip to conversation

x86/EFI: sanitize DLL characteristics in binary

PATCH In review Jan Beulich 2 participants 4 messages Apr 2, 2025 – Apr 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/EFI: sanitize DLL characteristics in binary

In GNU ld --disable-reloc-section implies --disable-dynamicbase (and also --disable-high-entropy-va, just fyi). Therefore to yield functionally identical binaries independent of whether mkreloc needs to come into play, add --dynamicbase as well.

GNU ld further defaults to --high-entropy-va (along with --dynamicbase and --nxcompat) unless "Cygwin-like". This seems wrong to me; that default should be dependent upon "MinGW-like" instead; for the purpose of building EFI binaries with a PE32+-capable ELF linker neither "Cygwin-like" nor "MinGW-like" ought to be true. We certainly don't mean to have this bit set in the DLL characteristics, so suppress its setting.

Sadly while --high-entropy-va is supported by GNU ld 2.25, --disable-high-entropy-va was introduced only in 2.36. Luckily the defaulting to --high-entropy-va was also only introduced in 2.36. Plus --disable-reloc-section was introduced precisely there, too. Hence leverage the probing we do as to base relocation generation, to also determine whether to pass --disable-high-entropy-va.

---
I remain uncertain about the applicability of DLL characteristics to non-DLLs, i.e. images without IMAGE_FILE_DLL set; current MS doc says nothing either way. Yet producing consistent binaries still seems desirable, even if the field wasn't to be used by loaders.

xen/arch/x86/arch.mk +3 -0
@@ -113,6 +113,7 @@ efi-nr-fixups := $(shell LC_ALL=C $(OBJD
 
 ifeq ($(efi-nr-fixups),2)
 MKRELOC := :
+EFI_LDFLAGS += --disable-high-entropy-va
 else
 MKRELOC := arch/x86/efi/mkreloc
 # If the linker produced fixups but not precisely two of them, we need to
@@ -123,6 +124,8 @@ EFI_LDFLAGS += --disable-reloc-section
 endif
 endif
 
+EFI_LDFLAGS += --dynamicbase
+
 endif # $(XEN_BUILD_PE)
 
 export XEN_BUILD_EFI XEN_BUILD_PE
#1 lore raw

Ping: [PATCH] x86/EFI: sanitize DLL characteristics in binary

Quoted 13 lines · Jan Beulich

In GNU ld --disable-reloc-section implies --disable-dynamicbase (and also --disable-high-entropy-va, just fyi). Therefore to yield functionally identical binaries independent of whether mkreloc needs to come into play, add --dynamicbase as well.

GNU ld further defaults to --high-entropy-va (along with --dynamicbase and --nxcompat) unless "Cygwin-like". This seems wrong to me; that default should be dependent upon "MinGW-like" instead; for the purpose of building EFI binaries with a PE32+-capable ELF linker neither "Cygwin-like" nor "MinGW-like" ought to be true. We certainly don't mean to have this bit set in the DLL characteristics, so suppress its setting.

Quoted 8 lines

Sadly while --high-entropy-va is supported by GNU ld 2.25, --disable-high-entropy-va was introduced only in 2.36. Luckily the defaulting to --high-entropy-va was also only introduced in 2.36. Plus --disable-reloc-section was introduced precisely there, too. Hence leverage the probing we do as to base relocation generation, to also determine whether to pass --disable-high-entropy-va.

Despite the file being modified suggesting this needs an x86 maintainer ack, I think it's rather one of the two of you who should ack (or otherwise comment on) this.

Jan

Quoted 25 lines

---
I remain uncertain about the applicability of DLL characteristics to non-DLLs, i.e. images without IMAGE_FILE_DLL set; current MS doc says nothing either way. Yet producing consistent binaries still seems desirable, even if the field wasn't to be used by loaders.

xen/arch/x86/arch.mk +3 -0
@@ -113,6 +113,7 @@ efi-nr-fixups := $(shell LC_ALL=C $(OBJD
 
 ifeq ($(efi-nr-fixups),2)
 MKRELOC := :
+EFI_LDFLAGS += --disable-high-entropy-va
 else
 MKRELOC := arch/x86/efi/mkreloc
 # If the linker produced fixups but not precisely two of them, we need to
@@ -123,6 +124,8 @@ EFI_LDFLAGS += --disable-reloc-section
 endif
 endif
 
+EFI_LDFLAGS += --dynamicbase
+
 endif # $(XEN_BUILD_PE)
 
 export XEN_BUILD_EFI XEN_BUILD_PE
Acked-by on v1 1/1
#2 lore raw
In reply to Jan Beulich
Quoted 38 lines · Jan Beulich

In GNU ld --disable-reloc-section implies --disable-dynamicbase (and also --disable-high-entropy-va, just fyi). Therefore to yield functionally identical binaries independent of whether mkreloc needs to come into play, add --dynamicbase as well.

GNU ld further defaults to --high-entropy-va (along with --dynamicbase and --nxcompat) unless "Cygwin-like". This seems wrong to me; that default should be dependent upon "MinGW-like" instead; for the purpose of building EFI binaries with a PE32+-capable ELF linker neither "Cygwin-like" nor "MinGW-like" ought to be true. We certainly don't mean to have this bit set in the DLL characteristics, so suppress its setting.

Sadly while --high-entropy-va is supported by GNU ld 2.25, --disable-high-entropy-va was introduced only in 2.36. Luckily the defaulting to --high-entropy-va was also only introduced in 2.36. Plus --disable-reloc-section was introduced precisely there, too. Hence leverage the probing we do as to base relocation generation, to also determine whether to pass --disable-high-entropy-va.

---
I remain uncertain about the applicability of DLL characteristics to non-DLLs, i.e. images without IMAGE_FILE_DLL set; current MS doc says nothing either way. Yet producing consistent binaries still seems desirable, even if the field wasn't to be used by loaders.

xen/arch/x86/arch.mk +1 -0
@@ -113,6 +113,7 @@ efi-nr-fixups := $(shell LC_ALL=C $(OBJD
 
  ifeq ($(efi-nr-fixups),2)
  MKRELOC := :
+EFI_LDFLAGS += --disable-high-entropy-va
  else
  MKRELOC := arch/x86/efi/mkreloc
  # If the linker produced fixups but not precisely two of them, we need to

xen/arch/x86/arch.mk EFI_LDFLAGS += --disable-reloc-section · line 131

Quoted 8 lines
xen/arch/x86/arch.mk +2 -0
@@ -123,6 +124,8 @@ EFI_LDFLAGS += --disable-reloc-section
  endif
  endif
 
+EFI_LDFLAGS += --dynamicbase
+
  endif # $(XEN_BUILD_PE)
 
  export XEN_BUILD_EFI XEN_BUILD_PE

Seems straightforward, will rely on your knowledge of binutils that there are no unattended effects from the flags.

#3 lore raw
Quoted 19 lines · Daniel P. Smith
Quoted 18 lines · Jan Beulich
xen/arch/x86/arch.mk +3 -0
@@ -113,6 +113,7 @@ efi-nr-fixups := $(shell LC_ALL=C $(OBJD
 
  ifeq ($(efi-nr-fixups),2)
  MKRELOC := :
+EFI_LDFLAGS += --disable-high-entropy-va
  else
  MKRELOC := arch/x86/efi/mkreloc
  # If the linker produced fixups but not precisely two of them, we need to
@@ -123,6 +124,8 @@ EFI_LDFLAGS += --disable-reloc-section
  endif
  endif
 
+EFI_LDFLAGS += --dynamicbase
+
  endif # $(XEN_BUILD_PE)
 
Quoted 6 lines

xen/arch/x86/arch.mk EFI_LDFLAGS += --disable-reloc-section · line 131

Quoted 1 line
xen/arch/x86/arch.mk +0 -0
  export XEN_BUILD_EFI XEN_BUILD_PE

Seems straightforward, will rely on your knowledge of binutils that there are no unattended effects from the flags.

Thanks, but to clarify: If there's an issue here, it would be with the PE loaders in the various EFI implementations; the binutils side is of little concern (as to the effects of the flags).

Jan