Skip to contentSkip to conversation

appendedsig: Fix grub-mkimage with an unaligned appended signature size

PATCH Superseded Sudhakar Kuppusamy 2 participants 6 messages Dec 3, 2025 – Dec 22, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 appendedsig: Fix grub-mkimage with an unaligned appended signature size

The grub-mkimage does not add zero padding when creating core.elf with an appended signature size that is not aligned to a multiple of 4 bytes. Firmware is unable to read the magic string "~Module signature appended~" from core.elf.

To fix this, round an appended signature size + appended signature ELF note size up to the nearest multiple of a 4-byte alignment and truncate it to the appended signature size.

Example:
grub-mkimage -O powerpc-ieee1275 -o core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

---
 util/grub-mkimagexx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
util/grub-mkimagexx.c +7 -1
index 7fe2e35e6..07a80a36d 100644
@@ -248,7 +248,13 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
   if (appsig_size)
     {
       phnum++;
-      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+      /*
+       * Rounds a appended signature size + appended signature note size up to
+       * the nearest multiple of a 4-byte alignment.
+       */
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      /* Truncating to appended signature size. */
+      footer_size -= appsig_size;
     }
 
   if (image_target->id != IMAGE_LOONGSON_ELF)
Signature · 7 lines
2.50.1 (Apple Git-155)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 24 lines · Sudhakar Kuppusamy

The grub-mkimage does not add zero padding when creating core.elf with an appended signature size that is not aligned to a multiple of 4 bytes. Firmware is unable to read the magic string "~Module signature appended~" from core.elf.

To fix this, round an appended signature size + appended signature ELF note size up to the nearest multiple of a 4-byte alignment and truncate it to the appended signature size.

Example:
grub-mkimage -O powerpc-ieee1275 -o core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

---
 util/grub-mkimagexx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
util/grub-mkimagexx.c +0 -0
index 7fe2e35e6..07a80a36d 100644
@@ -248,7 +248,13 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
   if (appsig_size)

util/grub-mkimagexx.c SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc · line 257

Quoted 10 lines
util/grub-mkimagexx.c +7 -1
     {
       phnum++;
-      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+      /*
+       * Rounds a appended signature size + appended signature note size up to
+       * the nearest multiple of a 4-byte alignment.
+       */
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      /* Truncating to appended signature size. */
+      footer_size -= appsig_size;

Should not this be

  footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
  appsig_size += ALIGN_UP (appsig_size, 4);

?

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
Quoted 42 lines · Daniel Kiper
Quoted 24 lines · Sudhakar Kuppusamy

The grub-mkimage does not add zero padding when creating core.elf with an appended signature size that is not aligned to a multiple of 4 bytes. Firmware is unable to read the magic string "~Module signature appended~" from core.elf.

To fix this, round an appended signature size + appended signature ELF note size up to the nearest multiple of a 4-byte alignment and truncate it to the appended signature size.

Example:
grub-mkimage -O powerpc-ieee1275 -o core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

---
util/grub-mkimagexx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
util/grub-mkimagexx.c +0 -0
index 7fe2e35e6..07a80a36d 100644
@@ -248,7 +248,13 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
  if (appsig_size)

util/grub-mkimagexx.c SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc · line 257

Quoted 10 lines
util/grub-mkimagexx.c +7 -1
    {
      phnum++;
-      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+      /*
+       * Rounds a appended signature size + appended signature note size up to
+       * the nearest multiple of a 4-byte alignment.
+       */
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      /* Truncating to appended signature size. */
+      footer_size -= appsig_size;

Should not this be

Quoted 4 lines

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); appsig_size += ALIGN_UP (appsig_size, 4);

?

Thank you Daniel for the review.

Did you meant footer_size += ALIGN_UP (appsig_size, 4); if yes, then it should be like

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); footer_size += ALIGN_UP (appsig_size, 4);
footer_size -= appsig_size;

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
Quoted 44 lines · Sudhakar Kuppusamy
Quoted 39 lines · Daniel Kiper
Quoted 24 lines · Sudhakar Kuppusamy

The grub-mkimage does not add zero padding when creating core.elf with an appended signature size that is not aligned to a multiple of 4 bytes. Firmware is unable to read the magic string "~Module signature appended~" from core.elf.

To fix this, round an appended signature size + appended signature ELF note size up to the nearest multiple of a 4-byte alignment and truncate it to the appended signature size.

Example:
grub-mkimage -O powerpc-ieee1275 -o core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

---
util/grub-mkimagexx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
util/grub-mkimagexx.c +0 -0
index 7fe2e35e6..07a80a36d 100644
@@ -248,7 +248,13 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
  if (appsig_size)

util/grub-mkimagexx.c SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc · line 257

Quoted 10 lines
util/grub-mkimagexx.c +7 -1
    {
      phnum++;
-      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+      /*
+       * Rounds a appended signature size + appended signature note size up to
+       * the nearest multiple of a 4-byte alignment.
+       */
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      /* Truncating to appended signature size. */
+      footer_size -= appsig_size;

Should not this be

Quoted 4 lines

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); appsig_size += ALIGN_UP (appsig_size, 4);

?

Quoted 7 lines

Thank you Daniel for the review.

Did you meant footer_size += ALIGN_UP (appsig_size, 4); if yes, then it should be like

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); footer_size += ALIGN_UP (appsig_size, 4);
footer_size -= appsig_size;

This subtraction makes me very suspicious. Something is off or not sufficiently explained. E.g. the commit message does not say anything which part of ELF file has to be aligned.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 53 lines · Daniel Kiper
Quoted 48 lines · Sudhakar Kuppusamy
Quoted 39 lines · Daniel Kiper
Quoted 24 lines · Sudhakar Kuppusamy

The grub-mkimage does not add zero padding when creating core.elf with an appended signature size that is not aligned to a multiple of 4 bytes. Firmware is unable to read the magic string "~Module signature appended~" from core.elf.

To fix this, round an appended signature size + appended signature ELF note size up to the nearest multiple of a 4-byte alignment and truncate it to the appended signature size.

Example:
grub-mkimage -O powerpc-ieee1275 -o core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

---
util/grub-mkimagexx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
util/grub-mkimagexx.c +0 -0
index 7fe2e35e6..07a80a36d 100644
@@ -248,7 +248,13 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
 if (appsig_size)

util/grub-mkimagexx.c SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc · line 257

Quoted 10 lines
util/grub-mkimagexx.c +7 -1
   {
     phnum++;
-      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+      /*
+       * Rounds a appended signature size + appended signature note size up to
+       * the nearest multiple of a 4-byte alignment.
+       */
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      /* Truncating to appended signature size. */
+      footer_size -= appsig_size;

Should not this be

Quoted 4 lines

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); appsig_size += ALIGN_UP (appsig_size, 4);

?

Thank you Daniel for the review.

Did you meant footer_size += ALIGN_UP (appsig_size, 4); if yes, then it should be like

Quoted 7 lines
Quoted 3 lines

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); footer_size += ALIGN_UP (appsig_size, 4);
footer_size -= appsig_size;

This subtraction makes me very suspicious. Something is off or not sufficiently explained. E.g. the commit message does not say anything which part of ELF file has to be aligned.

Sorry Daniel. I will correct the commit message and add the proper comments for subtraction.

We should manually truncate the core Image with appsig_size after creating the core image before signing it if we do not subtract the appsig_size with footer_size.

Example:

grub-mkimage -O powerpc-ieee1275 -o ./core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

truncate -s -490 ./core.elf

sign-file SHA256 ./grub.key ./grub.pem ./core.elf ./core.elf.signed

If subtract the appsig_size from the footer_size, then it reduce one step for GRUB image signing.

Example:

grub-mkimage -O powerpc-ieee1275 -o ./core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

sign-file SHA256 ./grub.key ./grub.pem ./core.elf ./core.elf.signed

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw
Quoted 72 lines · Sudhakar Kuppusamy
Quoted 50 lines · Daniel Kiper
Quoted 48 lines · Sudhakar Kuppusamy
Quoted 39 lines · Daniel Kiper
Quoted 24 lines · Sudhakar Kuppusamy

The grub-mkimage does not add zero padding when creating core.elf with an appended signature size that is not aligned to a multiple of 4 bytes. Firmware is unable to read the magic string "~Module signature appended~" from core.elf.

To fix this, round an appended signature size + appended signature ELF note size up to the nearest multiple of a 4-byte alignment and truncate it to the appended signature size.

Example:
grub-mkimage -O powerpc-ieee1275 -o core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

---
util/grub-mkimagexx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
util/grub-mkimagexx.c +0 -0
index 7fe2e35e6..07a80a36d 100644
@@ -248,7 +248,13 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
 if (appsig_size)

util/grub-mkimagexx.c SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc · line 257

Quoted 10 lines
util/grub-mkimagexx.c +7 -1
   {
     phnum++;
-      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+      /*
+       * Rounds a appended signature size + appended signature note size up to
+       * the nearest multiple of a 4-byte alignment.
+       */
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      /* Truncating to appended signature size. */
+      footer_size -= appsig_size;

Should not this be

Quoted 4 lines

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); appsig_size += ALIGN_UP (appsig_size, 4);

?

Thank you Daniel for the review.

Did you meant footer_size += ALIGN_UP (appsig_size, 4); if yes, then it should be like

Quoted 7 lines
Quoted 3 lines

footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); footer_size += ALIGN_UP (appsig_size, 4);
footer_size -= appsig_size;

This subtraction makes me very suspicious. Something is off or not sufficiently explained. E.g. the commit message does not say anything which part of ELF file has to be aligned.

Sorry Daniel. I will correct the commit message and add the proper comments for subtraction.

We should manually truncate the core Image with appsig_size after creating the core image before signing it if we do not subtract the appsig_size with footer_size.

Example:

grub-mkimage -O powerpc-ieee1275 -o ./core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

truncate -s -490 ./core.elf

sign-file SHA256 ./grub.key ./grub.pem ./core.elf ./core.elf.signed

Quoted 8 lines

If subtract the appsig_size from the footer_size, then it reduce one step for GRUB image signing.

Example:

grub-mkimage -O powerpc-ieee1275 -o ./core.elf -d grub-core -p /grub2 -x \
kernel.der --appended-signature-size 490 ...

sign-file SHA256 ./grub.key ./grub.pem ./core.elf ./core.elf.signed

I think you are looking for this:
footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4); footer_size += ALIGN_UP_OVERHEAD (appsig_size, 4);

Though still commit message begs for better explanation of the issue.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel