Skip to contentSkip to conversation

x86/boot: EFISTUB section alignment

PATCH In review Evgeniy Baskov 3 participants 9 messages Feb 5, 2024 – Feb 6, 2024

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

cover x86/boot: EFISTUB section alignment

I had these patches fixing [1] laying in my local branch for a while.

The key problem was that sections were not in the same order in section table as their VMAs and the '.compat' section virtual size was not aligned on section alignment.

[1] https://lore.kernel.org/linux-efi/CAHzAAWQ6srV6LVNdmfbJhOwhBw5ZzxxZZ07aHt9oKkfYAdvuQQ@mail.gmail.com/T/#u

Evgeniy Baskov (2):
x86/boot: Order sections by their RVAs
x86/boot: Align .compat virtual size

 arch/x86/boot/header.S | 58 +++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 29 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 x86/boot: Order sections by their RVAs

Image loaders which check the contiguity of executable images consider sections adjacent in the section table to be adjacent in virtual memory.

Sort sections by their RVAs in PE section table.

---
 arch/x86/boot/header.S | 56 +++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 28 deletions(-)
arch/x86/boot/header.S +28 -28
index b2771710ed98..097b84ab288c 100644
@@ -153,6 +153,34 @@ section_table:
 		IMAGE_SCN_MEM_READ		| \
 		IMAGE_SCN_MEM_DISCARDABLE	# Characteristics
 
+	.ascii	".text"
+	.byte	0
+	.byte	0
+	.byte	0
+	.long	ZO__data
+	.long	setup_size
+	.long	ZO__data			# Size of initialized data
+						# on disk
+	.long	setup_size
+	.long	0				# PointerToRelocations
+	.long	0				# PointerToLineNumbers
+	.word	0				# NumberOfRelocations
+	.word	0				# NumberOfLineNumbers
+	.long	IMAGE_SCN_CNT_CODE		| \
+		IMAGE_SCN_MEM_READ		| \
+		IMAGE_SCN_MEM_EXECUTE		# Characteristics
+
+	.ascii	".data\0\0\0"
+	.long	ZO__end - ZO__data		# VirtualSize
+	.long	setup_size + ZO__data		# VirtualAddress
+	.long	ZO__edata - ZO__data		# SizeOfRawData
+	.long	setup_size + ZO__data		# PointerToRawData
+
+	.long	0, 0, 0
+	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
+		IMAGE_SCN_MEM_READ		| \
+		IMAGE_SCN_MEM_WRITE		# Characteristics
+
 #ifdef CONFIG_EFI_MIXED
 	.asciz	".compat"
 
@@ -185,34 +213,6 @@ pecompat_fstart:
 	.set	pecompat_vsize, 0
 	.set	pecompat_fstart, setup_size
 #endif
-	.ascii	".text"
-	.byte	0
-	.byte	0
-	.byte	0
-	.long	ZO__data
-	.long	setup_size
-	.long	ZO__data			# Size of initialized data
-						# on disk
-	.long	setup_size
-	.long	0				# PointerToRelocations
-	.long	0				# PointerToLineNumbers
-	.word	0				# NumberOfRelocations
-	.word	0				# NumberOfLineNumbers
-	.long	IMAGE_SCN_CNT_CODE		| \
-		IMAGE_SCN_MEM_READ		| \
-		IMAGE_SCN_MEM_EXECUTE		# Characteristics
-
-	.ascii	".data\0\0\0"
-	.long	ZO__end - ZO__data		# VirtualSize
-	.long	setup_size + ZO__data		# VirtualAddress
-	.long	ZO__edata - ZO__data		# SizeOfRawData
-	.long	setup_size + ZO__data		# PointerToRawData
-
-	.long	0, 0, 0
-	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
-		IMAGE_SCN_MEM_READ		| \
-		IMAGE_SCN_MEM_WRITE		# Characteristics
-
 	.set	section_count, (. - section_table) / 40
 #endif /* CONFIG_EFI_STUB */
 
Signature · 1 line
2.43.0
#7 lore raw

PATCH 2/2 x86/boot: Align .compat virtual size

In reply to Evgeniy Baskov

Even if this section is discardable, it still needs to have virtual memory size aligned on section alignment to be compliant.

Align compat section on section alignment. This adds ~4k of memory after the kernel image during EFISTUB execution but, it's not a problem on x86 architecture.

---
 arch/x86/boot/header.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/header.S +1 -1
index 097b84ab288c..128e35f5c89b 100644
@@ -184,7 +184,7 @@ section_table:
 #ifdef CONFIG_EFI_MIXED
 	.asciz	".compat"
 
-	.long	8				# VirtualSize
+	.long	pecompat_vsize			# VirtualSize
 	.long	setup_size + ZO__end		# VirtualAddress
 	.long	pecompat_fsize			# SizeOfRawData
 	.long	pecompat_fstart			# PointerToRawData
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 1/2] x86/boot: Order sections by their RVAs

In reply to Evgeniy Baskov
Quoted 11 lines · Evgeniy Baskov

Image loaders which check the contiguity of executable images consider sections adjacent in the section table to be adjacent in virtual memory.

Sort sections by their RVAs in PE section table.

---
 arch/x86/boot/header.S | 56 +++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 28 deletions(-)

Hi,

I just sent a fix for this issue:

https://lore.kernel.org/linux-efi/[email protected]/T/#u

Could you please check whether it works for you as well?

Quoted 77 lines
arch/x86/boot/header.S +28 -29
index b2771710ed98..097b84ab288c 100644
@@ -153,6 +153,34 @@ section_table:
                IMAGE_SCN_MEM_READ              | \
                IMAGE_SCN_MEM_DISCARDABLE       # Characteristics
 
+       .ascii  ".text"
+       .byte   0
+       .byte   0
+       .byte   0
+       .long   ZO__data
+       .long   setup_size
+       .long   ZO__data                        # Size of initialized data
+                                               # on disk
+       .long   setup_size
+       .long   0                               # PointerToRelocations
+       .long   0                               # PointerToLineNumbers
+       .word   0                               # NumberOfRelocations
+       .word   0                               # NumberOfLineNumbers
+       .long   IMAGE_SCN_CNT_CODE              | \
+               IMAGE_SCN_MEM_READ              | \
+               IMAGE_SCN_MEM_EXECUTE           # Characteristics
+
+       .ascii  ".data\0\0\0"
+       .long   ZO__end - ZO__data              # VirtualSize
+       .long   setup_size + ZO__data           # VirtualAddress
+       .long   ZO__edata - ZO__data            # SizeOfRawData
+       .long   setup_size + ZO__data           # PointerToRawData
+
+       .long   0, 0, 0
+       .long   IMAGE_SCN_CNT_INITIALIZED_DATA  | \
+               IMAGE_SCN_MEM_READ              | \
+               IMAGE_SCN_MEM_WRITE             # Characteristics
+
 #ifdef CONFIG_EFI_MIXED
        .asciz  ".compat"
 
@@ -185,34 +213,6 @@ pecompat_fstart:
        .set    pecompat_vsize, 0
        .set    pecompat_fstart, setup_size
 #endif
-       .ascii  ".text"
-       .byte   0
-       .byte   0
-       .byte   0
-       .long   ZO__data
-       .long   setup_size
-       .long   ZO__data                        # Size of initialized data
-                                               # on disk
-       .long   setup_size
-       .long   0                               # PointerToRelocations
-       .long   0                               # PointerToLineNumbers
-       .word   0                               # NumberOfRelocations
-       .word   0                               # NumberOfLineNumbers
-       .long   IMAGE_SCN_CNT_CODE              | \
-               IMAGE_SCN_MEM_READ              | \
-               IMAGE_SCN_MEM_EXECUTE           # Characteristics
-
-       .ascii  ".data\0\0\0"
-       .long   ZO__end - ZO__data              # VirtualSize
-       .long   setup_size + ZO__data           # VirtualAddress
-       .long   ZO__edata - ZO__data            # SizeOfRawData
-       .long   setup_size + ZO__data           # PointerToRawData
-
-       .long   0, 0, 0
-       .long   IMAGE_SCN_CNT_INITIALIZED_DATA  | \
-               IMAGE_SCN_MEM_READ              | \
-               IMAGE_SCN_MEM_WRITE             # Characteristics
-
        .set    section_count, (. - section_table) / 40
 #endif /* CONFIG_EFI_STUB */
 
--

2.43.0

maintainer
#8 lore raw

Re: [PATCH 2/2] x86/boot: Align .compat virtual size

In reply to Evgeniy Baskov
Quoted 4 lines · Evgeniy Baskov

Even if this section is discardable, it still needs to have virtual memory size aligned on section alignment to be compliant.

It does not. The PE/COFF spec does not require a section to end on an address that is aligned to the section alignment of the image.

Quoted 25 lines

Align compat section on section alignment. This adds ~4k of memory after the kernel image during EFISTUB execution but, it's not a problem on x86 architecture.

---
 arch/x86/boot/header.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/header.S +1 -2
index 097b84ab288c..128e35f5c89b 100644
@@ -184,7 +184,7 @@ section_table:
 #ifdef CONFIG_EFI_MIXED
        .asciz  ".compat"
 
-       .long   8                               # VirtualSize
+       .long   pecompat_vsize                  # VirtualSize
        .long   setup_size + ZO__end            # VirtualAddress
        .long   pecompat_fsize                  # SizeOfRawData
        .long   pecompat_fstart                 # PointerToRawData
--

2.43.0

#3 lore raw

Re: [PATCH 1/2] x86/boot: Order sections by their RVAs

In reply to Ard Biesheuvel
Quoted 14 lines · Ard Biesheuvel
Quoted 4 lines · Evgeniy Baskov

Image loaders which check the contiguity of executable images consider sections adjacent in the section table to be adjacent in virtual memory.

Quoted 8 lines

Sort sections by their RVAs in PE section table.

---
 arch/x86/boot/header.S | 56 
+++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 28 deletions(-)
Quoted 8 lines

Hi,

I just sent a fix for this issue:

https://lore.kernel.org/linux-efi/[email protected]/T/#u

Could you please check whether it works for you as well?

Yes, your patch works fine as well. Thanks!

Quoted 80 lines
Quoted 80 lines
arch/x86/boot/header.S +7 -0
index b2771710ed98..097b84ab288c 100644
@@ -153,6 +153,34 @@ section_table:
                IMAGE_SCN_MEM_READ              | \
                IMAGE_SCN_MEM_DISCARDABLE       # Characteristics
 
+       .ascii  ".text"
+       .byte   0
+       .byte   0
+       .byte   0
+       .long   ZO__data
+       .long   setup_size
+       .long   ZO__data                        # Size of initialized 

data

arch/x86/boot/header.S +21 -7
+                                               # on disk
+       .long   setup_size
+       .long   0                               # PointerToRelocations
+       .long   0                               # PointerToLineNumbers
+       .word   0                               # NumberOfRelocations
+       .word   0                               # NumberOfLineNumbers
+       .long   IMAGE_SCN_CNT_CODE              | \
+               IMAGE_SCN_MEM_READ              | \
+               IMAGE_SCN_MEM_EXECUTE           # Characteristics
+
+       .ascii  ".data\0\0\0"
+       .long   ZO__end - ZO__data              # VirtualSize
+       .long   setup_size + ZO__data           # VirtualAddress
+       .long   ZO__edata - ZO__data            # SizeOfRawData
+       .long   setup_size + ZO__data           # PointerToRawData
+
+       .long   0, 0, 0
+       .long   IMAGE_SCN_CNT_INITIALIZED_DATA  | \
+               IMAGE_SCN_MEM_READ              | \
+               IMAGE_SCN_MEM_WRITE             # Characteristics
+
 #ifdef CONFIG_EFI_MIXED
        .asciz  ".compat"
@@ -185,34 +213,6 @@ pecompat_fstart:
        .set    pecompat_vsize, 0
        .set    pecompat_fstart, setup_size
 #endif
-       .ascii  ".text"
-       .byte   0
-       .byte   0
-       .byte   0
-       .long   ZO__data
-       .long   setup_size
-       .long   ZO__data                        # Size of initialized 

data

arch/x86/boot/header.S +0 -22
-                                               # on disk
-       .long   setup_size
-       .long   0                               # PointerToRelocations
-       .long   0                               # PointerToLineNumbers
-       .word   0                               # NumberOfRelocations
-       .word   0                               # NumberOfLineNumbers
-       .long   IMAGE_SCN_CNT_CODE              | \
-               IMAGE_SCN_MEM_READ              | \
-               IMAGE_SCN_MEM_EXECUTE           # Characteristics
-
-       .ascii  ".data\0\0\0"
-       .long   ZO__end - ZO__data              # VirtualSize
-       .long   setup_size + ZO__data           # VirtualAddress
-       .long   ZO__edata - ZO__data            # SizeOfRawData
-       .long   setup_size + ZO__data           # PointerToRawData
-
-       .long   0, 0, 0
-       .long   IMAGE_SCN_CNT_INITIALIZED_DATA  | \
-               IMAGE_SCN_MEM_READ              | \
-               IMAGE_SCN_MEM_WRITE             # Characteristics
-
        .set    section_count, (. - section_table) / 40
 #endif /* CONFIG_EFI_STUB */
 
--

2.43.0

maintainer
#4 lore raw

Re: [PATCH 1/2] x86/boot: Order sections by their RVAs

Quoted 19 lines · Evgeniy Baskov
Quoted 17 lines · Ard Biesheuvel
Quoted 5 lines · Evgeniy Baskov

Image loaders which check the contiguity of executable images consider sections adjacent in the section table to be adjacent in virtual memory.

Quoted 8 lines

Sort sections by their RVAs in PE section table.

---
 arch/x86/boot/header.S | 56
+++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 28 deletions(-)

Hi,

Quoted 9 lines
Quoted 6 lines

I just sent a fix for this issue:

https://lore.kernel.org/linux-efi/[email protected]/T/#u

Could you please check whether it works for you as well?

Yes, your patch works fine as well. Thanks!

Thanks for confirming!

I tried your patch too, and it makes the signing tools unhappy (see below), so I am going to stick with my version. But thanks for sending a fix in any case.

$ sbsign --key ~/sb/PK.key --cert ~/sb/PK.crt arch/x86/boot/bzImage
warning: gap in section table:
    .setup  : 0x00001000 - 0x00003a00,
    .text   : 0x00004000 - 0x00d0b000,
warning: gap in section table:
    .data   : 0x00d0b000 - 0x00d0c200,
    .compat : 0x00003a00 - 0x00004000,
gaps in the section table may result in different checksums
Signing Unsigned original image
#5 lore raw

Re: [PATCH 1/2] x86/boot: Order sections by their RVAs

Quoted 5 lines · Ard Biesheuvel

Thanks for confirming!

I tried your patch too, and it makes the signing tools unhappy (see below), so I am going to stick with my version. But thanks for sending a fix in any case.

Tyvm Ard and Evginy!

@Ard, Could I ask whether this fix is definitely/likely/probable to go into Linux kernel 6.8 final version? (I don't know as much as I probably should about release cadences, and relation between release cadences, for kernel efi vs. kernel other bits!)

maintainer
#6 lore raw

Re: [PATCH 1/2] x86/boot: Order sections by their RVAs

Quoted 8 lines · Mike Beaton
Quoted 5 lines · Ard Biesheuvel

Thanks for confirming!

I tried your patch too, and it makes the signing tools unhappy (see below), so I am going to stick with my version. But thanks for sending a fix in any case.

Quoted 6 lines

Tyvm Ard and Evginy!

@Ard, Could I ask whether this fix is definitely/likely/probable to go into Linux kernel 6.8 final version? (I don't know as much as I probably should about release cadences, and relation between release cadences, for kernel efi vs. kernel other bits!)

This will go into v6.8 as a fix and be backported to v6.7-stable. Once I am confident that all the EFI stub changes for x86 in v6.7 are in good shape, they will be backported to v6.6 as well. (v6.6 is a long term supported stable kernel)