Skip to contentSkip to conversation

efi: Fix .data section size calculations when .sbat is present

PATCH Superseded Vitaly Kuznetsov 2 participants 6 messages Jun 18, 2025 – Jun 20, 2025 EFI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi: Fix .data section size calculations when .sbat is present

Commit 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section") neglected to adjust the sizes of the .data section when CONFIG_EFI_SBAT_FILE is set. As the result, the produced PE binary is incorrect and some tools complain about it. E.g. 'sbsign' reports:

 # sbsign --key my.key --cert my.crt arch/arm64/boot/vmlinuz.efi
 warning: file-aligned section .data extends beyond end of file
 warning: checksum areas are greater than image size. Invalid section table?

Note, '__data_size' was also used in the PE optional header. The field is supposed to reflect "The size of the initialized data section, or the sum of all such sections if there are multiple data sections.". While OVMF based firmware doesn't seem to care much about what's there, it sounds like including .sbat in the calculation is more correct.

Fixes 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section")
---
 drivers/firmware/efi/libstub/zboot-header.S | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc9..65df5f52e138 100644
@@ -41,7 +41,7 @@ __efistub_efi_zboot_header:
 	.short		.Lpe_opt_magic
 	.byte		0, 0
 	.long		_etext - .Lefi_header_end
-	.long		__data_size
+	.long		__all_data_size
 	.long		0
 	.long		__efistub_efi_zboot_entry - .Ldoshdr
 	.long		.Lefi_header_end - .Ldoshdr
drivers/firmware/efi/libstub/zboot.lds +4 -2
index 4b8d5cd3dfa2..f423204fba0f 100644
@@ -58,6 +58,8 @@ SECTIONS
 PROVIDE(__efistub__gzdata_size =
 		ABSOLUTE(__efistub__gzdata_end - __efistub__gzdata_start));
 
-PROVIDE(__data_rawsize = ABSOLUTE(_edata - _etext));
-PROVIDE(__data_size = ABSOLUTE(_end - _etext));
+PROVIDE(__data_rawsize = ABSOLUTE(_edata - _data));
+PROVIDE(__data_size = ABSOLUTE(_end - _data));
+/* The sum of all data sections, i.e. .data and .sbat */
+PROVIDE(__all_data_size = ABSOLUTE(_end - _etext));
 PROVIDE(__sbat_size = ABSOLUTE(_esbat - _sbat));
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 16 lines · Vitaly Kuznetsov

Commit 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section") neglected to adjust the sizes of the .data section when CONFIG_EFI_SBAT_FILE is set. As the result, the produced PE binary is incorrect and some tools complain about it. E.g. 'sbsign' reports:

 # sbsign --key my.key --cert my.crt arch/arm64/boot/vmlinuz.efi
 warning: file-aligned section .data extends beyond end of file
 warning: checksum areas are greater than image size. Invalid section table?

Note, '__data_size' was also used in the PE optional header. The field is supposed to reflect "The size of the initialized data section, or the sum of all such sections if there are multiple data sections.". While OVMF based firmware doesn't seem to care much about what's there, it sounds like including .sbat in the calculation is more correct.

Quoted 8 lines
Fixes 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section")
---
 drivers/firmware/efi/libstub/zboot-header.S | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

Thanks for the fix.

drivers/firmware/efi/libstub/zboot-header.S __efistub_efi_zboot_header: · line 44

Quoted 10 lines
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc9..65df5f52e138 100644
@@ -41,7 +41,7 @@ __efistub_efi_zboot_header:
        .short          .Lpe_opt_magic
        .byte           0, 0
        .long           _etext - .Lefi_header_end
-       .long           __data_size
+       .long           __all_data_size

Frankly, I'm not sure if this is even worth the hassle.

There is also a 'size of uninitialized data' field, but given that the .data section has both initialized data and uninitialized data, and the fact that no loaders appear to care about these fields, let's just not bother.

Quoted 21 lines
drivers/firmware/efi/libstub/zboot-header.S +0 -0
        .long           0
        .long           __efistub_efi_zboot_entry - .Ldoshdr
        .long           .Lefi_header_end - .Ldoshdr
drivers/firmware/efi/libstub/zboot.lds +4 -3
index 4b8d5cd3dfa2..f423204fba0f 100644
@@ -58,6 +58,8 @@ SECTIONS
 PROVIDE(__efistub__gzdata_size =
                ABSOLUTE(__efistub__gzdata_end - __efistub__gzdata_start));
 
-PROVIDE(__data_rawsize = ABSOLUTE(_edata - _etext));
-PROVIDE(__data_size = ABSOLUTE(_end - _etext));
+PROVIDE(__data_rawsize = ABSOLUTE(_edata - _data));
+PROVIDE(__data_size = ABSOLUTE(_end - _data));
+/* The sum of all data sections, i.e. .data and .sbat */
+PROVIDE(__all_data_size = ABSOLUTE(_end - _etext));
 PROVIDE(__sbat_size = ABSOLUTE(_esbat - _sbat));
--

2.49.0

maintainer
#2 lore raw
Quoted 42 lines · Ard Biesheuvel
Quoted 16 lines · Vitaly Kuznetsov

Commit 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section") neglected to adjust the sizes of the .data section when CONFIG_EFI_SBAT_FILE is set. As the result, the produced PE binary is incorrect and some tools complain about it. E.g. 'sbsign' reports:

 # sbsign --key my.key --cert my.crt arch/arm64/boot/vmlinuz.efi
 warning: file-aligned section .data extends beyond end of file
 warning: checksum areas are greater than image size. Invalid section table?

Note, '__data_size' was also used in the PE optional header. The field is supposed to reflect "The size of the initialized data section, or the sum of all such sections if there are multiple data sections.". While OVMF based firmware doesn't seem to care much about what's there, it sounds like including .sbat in the calculation is more correct.

Quoted 8 lines
Fixes 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section")
---
 drivers/firmware/efi/libstub/zboot-header.S | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

Thanks for the fix.

drivers/firmware/efi/libstub/zboot-header.S __efistub_efi_zboot_header: · line 44

Quoted 10 lines
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc9..65df5f52e138 100644
@@ -41,7 +41,7 @@ __efistub_efi_zboot_header:
        .short          .Lpe_opt_magic
        .byte           0, 0
        .long           _etext - .Lefi_header_end
-       .long           __data_size
+       .long           __all_data_size

Frankly, I'm not sure if this is even worth the hassle.

Quoted 5 lines

There is also a 'size of uninitialized data' field, but given that the .data section has both initialized data and uninitialized data, and the fact that no loaders appear to care about these fields, let's just not bother.

... or add .sbat to SizeOfCode instead. (the preceding field)

#3 lore raw
Quoted 43 lines · Ard Biesheuvel
Quoted 42 lines · Ard Biesheuvel
Quoted 16 lines · Vitaly Kuznetsov

Commit 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section") neglected to adjust the sizes of the .data section when CONFIG_EFI_SBAT_FILE is set. As the result, the produced PE binary is incorrect and some tools complain about it. E.g. 'sbsign' reports:

 # sbsign --key my.key --cert my.crt arch/arm64/boot/vmlinuz.efi
 warning: file-aligned section .data extends beyond end of file
 warning: checksum areas are greater than image size. Invalid section table?

Note, '__data_size' was also used in the PE optional header. The field is supposed to reflect "The size of the initialized data section, or the sum of all such sections if there are multiple data sections.". While OVMF based firmware doesn't seem to care much about what's there, it sounds like including .sbat in the calculation is more correct.

Quoted 8 lines
Fixes 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section")
---
 drivers/firmware/efi/libstub/zboot-header.S | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

Thanks for the fix.

drivers/firmware/efi/libstub/zboot-header.S __efistub_efi_zboot_header: · line 44

Quoted 10 lines
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc9..65df5f52e138 100644
@@ -41,7 +41,7 @@ __efistub_efi_zboot_header:
        .short          .Lpe_opt_magic
        .byte           0, 0
        .long           _etext - .Lefi_header_end
-       .long           __data_size
+       .long           __all_data_size

Frankly, I'm not sure if this is even worth the hassle.

Quoted 8 lines
Quoted 5 lines

There is also a 'size of uninitialized data' field, but given that the .data section has both initialized data and uninitialized data, and the fact that no loaders appear to care about these fields, let's just not bother.

... or add .sbat to SizeOfCode instead. (the preceding field)

I was wondering how to make it consistent with yet-unmerged x86 patch. In v3:
https://lore.kernel.org/linux-efi/[email protected]/
.sbat was accounted as SizeOfCode but the section is "IMAGE_SCN_CNT_INITIALIZED_DATA", not "IMAGE_SCN_CNT_CODE" so I sent v4 yesterday:
https://lore.kernel.org/linux-efi/[email protected]/
which made .sbat accounted in SizeOfInitializedData.

Signature · 1 line
Vitaly
maintainer
#4 lore raw
Quoted 55 lines · Vitaly Kuznetsov
Quoted 43 lines · Ard Biesheuvel
Quoted 42 lines · Ard Biesheuvel
Quoted 16 lines · Vitaly Kuznetsov

Commit 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section") neglected to adjust the sizes of the .data section when CONFIG_EFI_SBAT_FILE is set. As the result, the produced PE binary is incorrect and some tools complain about it. E.g. 'sbsign' reports:

 # sbsign --key my.key --cert my.crt arch/arm64/boot/vmlinuz.efi
 warning: file-aligned section .data extends beyond end of file
 warning: checksum areas are greater than image size. Invalid section table?

Note, '__data_size' was also used in the PE optional header. The field is supposed to reflect "The size of the initialized data section, or the sum of all such sections if there are multiple data sections.". While OVMF based firmware doesn't seem to care much about what's there, it sounds like including .sbat in the calculation is more correct.

Quoted 8 lines
Fixes 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section")
---
 drivers/firmware/efi/libstub/zboot-header.S | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

Thanks for the fix.

drivers/firmware/efi/libstub/zboot-header.S __efistub_efi_zboot_header: · line 44

Quoted 10 lines
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc9..65df5f52e138 100644
@@ -41,7 +41,7 @@ __efistub_efi_zboot_header:
        .short          .Lpe_opt_magic
        .byte           0, 0
        .long           _etext - .Lefi_header_end
-       .long           __data_size
+       .long           __all_data_size

Frankly, I'm not sure if this is even worth the hassle.

Quoted 8 lines
Quoted 5 lines

There is also a 'size of uninitialized data' field, but given that the .data section has both initialized data and uninitialized data, and the fact that no loaders appear to care about these fields, let's just not bother.

... or add .sbat to SizeOfCode instead. (the preceding field)

Quoted 9 lines

I was wondering how to make it consistent with yet-unmerged x86 patch. In v3:
https://lore.kernel.org/linux-efi/[email protected]/
.sbat was accounted as SizeOfCode but the section is "IMAGE_SCN_CNT_INITIALIZED_DATA", not "IMAGE_SCN_CNT_CODE" so I sent v4 yesterday:
https://lore.kernel.org/linux-efi/[email protected]/
which made .sbat accounted in SizeOfInitializedData.

I agree that making it consistent is preferred, but I really don't see the point of obsessing over this - the field is basically unused in practice.

#5 lore raw
Quoted 66 lines · Ard Biesheuvel
Quoted 55 lines · Vitaly Kuznetsov
Quoted 43 lines · Ard Biesheuvel
Quoted 42 lines · Ard Biesheuvel
Quoted 16 lines · Vitaly Kuznetsov

Commit 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section") neglected to adjust the sizes of the .data section when CONFIG_EFI_SBAT_FILE is set. As the result, the produced PE binary is incorrect and some tools complain about it. E.g. 'sbsign' reports:

 # sbsign --key my.key --cert my.crt arch/arm64/boot/vmlinuz.efi
 warning: file-aligned section .data extends beyond end of file
 warning: checksum areas are greater than image size. Invalid section table?

Note, '__data_size' was also used in the PE optional header. The field is supposed to reflect "The size of the initialized data section, or the sum of all such sections if there are multiple data sections.". While OVMF based firmware doesn't seem to care much about what's there, it sounds like including .sbat in the calculation is more correct.

Quoted 8 lines
Fixes 0f9a1739dd0e ("efi: zboot specific mechanism for embedding SBAT section")
---
 drivers/firmware/efi/libstub/zboot-header.S | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

Thanks for the fix.

drivers/firmware/efi/libstub/zboot-header.S __efistub_efi_zboot_header: · line 44

Quoted 10 lines
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc9..65df5f52e138 100644
@@ -41,7 +41,7 @@ __efistub_efi_zboot_header:
        .short          .Lpe_opt_magic
        .byte           0, 0
        .long           _etext - .Lefi_header_end
-       .long           __data_size
+       .long           __all_data_size

Frankly, I'm not sure if this is even worth the hassle.

Quoted 8 lines
Quoted 5 lines

There is also a 'size of uninitialized data' field, but given that the .data section has both initialized data and uninitialized data, and the fact that no loaders appear to care about these fields, let's just not bother.

... or add .sbat to SizeOfCode instead. (the preceding field)

Quoted 9 lines

I was wondering how to make it consistent with yet-unmerged x86 patch. In v3:
https://lore.kernel.org/linux-efi/[email protected]/
.sbat was accounted as SizeOfCode but the section is "IMAGE_SCN_CNT_INITIALIZED_DATA", not "IMAGE_SCN_CNT_CODE" so I sent v4 yesterday:
https://lore.kernel.org/linux-efi/[email protected]/
which made .sbat accounted in SizeOfInitializedData.

Quoted 4 lines

I agree that making it consistent is preferred, but I really don't see the point of obsessing over this - the field is basically unused in practice.

OK, let me then just do v2 of this patch with '__all_data_size' magic dropped.

Signature · 1 line
Vitaly