Skip to contentSkip to conversation

loader/i386/linux: Fix an underflow in the setup_header length calculation

PATCH In review Daniel Kiper 4 participants 5 messages Dec 2, 2019 – Dec 13, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 loader/i386/linux: Fix an underflow in the setup_header length calculation

Recent work around x86 Linux kernel loader revealed an underflow in the setup_header length calculation and another related issue. Both lead to the memory overwrite and later machine crash.

Currently when the GRUB copies the setup_header into the linux_params (struct boot_params, traditionally known as "zero page") it assumes the setup_header size as sizeof(linux_i386_kernel_header/lh). This is incorrect. It should use the value calculated accordingly to the Linux kernel boot protocol. Otherwise in case of pretty old kernel, to be exact Linux kernel boot protocol, the GRUB may write more into linux_params than it was expected to. Fortunately this is not very big issue. Though it has to be fixed. However, there is also an underflow which is grave. It happens when

sizeof(linux_i386_kernel_header/lh) > "real size of the setup_header".

Then len value wraps around and grub_file_read() reads whole kernel into the linux_params overwriting memory past it. This leads to the GRUB memory allocator breakage and finally to its crash during boot.

The patch fixes both issues. Additionally, it moves the code not related to grub_memset(linux_params)/grub_memcpy(linux_params)/grub_file_read(linux_params) section outside of it to not confuse the reader.

---
 grub-core/loader/i386/linux.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
grub-core/loader/i386/linux.c +8 -7
index d0501e229..ee95cd374 100644
@@ -761,17 +761,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     goto fail;
 
   grub_memset (&linux_params, 0, sizeof (linux_params));
-  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
-
-  linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
-  linux_params.kernel_alignment = (1 << align);
-  linux_params.ps_mouse = linux_params.padding10 =  0;
 
   /*
    * The Linux 32-bit boot protocol defines the setup header end
    * to be at 0x202 + the byte value at 0x201.
    */
-  len = 0x202 + *((char *) &linux_params.jump + 1);
+  len = 0x202 + *((char *) &lh.jump + 1);
 
   /* Verify the struct is big enough so we do not write past the end. */
   if (len > (char *) &linux_params.edd_mbr_sig_buffer - (char *) &linux_params) {
@@ -779,10 +774,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     goto fail;
   }
 
+  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, len - 0x1F1);
+
   /* We've already read lh so there is no need to read it second time. */
   len -= sizeof(lh);
 
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
+  if ((len > 0) &&
+      (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len))
     {
       if (!grub_errno)
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@@ -790,6 +788,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
+  linux_params.kernel_alignment = (1 << align);
+  linux_params.ps_mouse = linux_params.padding10 = 0;
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
 
   /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
Signature · 1 line
2.11.0
#1 lore raw

Hmmm... Nobody cares? Strange...

Adding 3mdeb folks...

Daniel

Quoted 79 lines · Daniel Kiper

Recent work around x86 Linux kernel loader revealed an underflow in the setup_header length calculation and another related issue. Both lead to the memory overwrite and later machine crash.

Currently when the GRUB copies the setup_header into the linux_params (struct boot_params, traditionally known as "zero page") it assumes the setup_header size as sizeof(linux_i386_kernel_header/lh). This is incorrect. It should use the value calculated accordingly to the Linux kernel boot protocol. Otherwise in case of pretty old kernel, to be exact Linux kernel boot protocol, the GRUB may write more into linux_params than it was expected to. Fortunately this is not very big issue. Though it has to be fixed. However, there is also an underflow which is grave. It happens when

sizeof(linux_i386_kernel_header/lh) > "real size of the setup_header".

Then len value wraps around and grub_file_read() reads whole kernel into the linux_params overwriting memory past it. This leads to the GRUB memory allocator breakage and finally to its crash during boot.

The patch fixes both issues. Additionally, it moves the code not related to grub_memset(linux_params)/grub_memcpy(linux_params)/grub_file_read(linux_params) section outside of it to not confuse the reader.

---
 grub-core/loader/i386/linux.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
grub-core/loader/i386/linux.c +8 -8
index d0501e229..ee95cd374 100644
@@ -761,17 +761,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     goto fail;
 
   grub_memset (&linux_params, 0, sizeof (linux_params));
-  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
-
-  linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
-  linux_params.kernel_alignment = (1 << align);
-  linux_params.ps_mouse = linux_params.padding10 =  0;
 
   /*
    * The Linux 32-bit boot protocol defines the setup header end
    * to be at 0x202 + the byte value at 0x201.
    */
-  len = 0x202 + *((char *) &linux_params.jump + 1);
+  len = 0x202 + *((char *) &lh.jump + 1);
 
   /* Verify the struct is big enough so we do not write past the end. */
   if (len > (char *) &linux_params.edd_mbr_sig_buffer - (char *) &linux_params) {
@@ -779,10 +774,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     goto fail;
   }
 
+  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, len - 0x1F1);
+
   /* We've already read lh so there is no need to read it second time. */
   len -= sizeof(lh);
 
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
+  if ((len > 0) &&
+      (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len))
     {
       if (!grub_errno)
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@@ -790,6 +788,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
+  linux_params.kernel_alignment = (1 << align);
+  linux_params.ps_mouse = linux_params.padding10 = 0;
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
 
   /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
--

2.11.0

#2 lore raw
Quoted 99 lines · Daniel Kiper

Hmmm... Nobody cares? Strange...

Adding 3mdeb folks...

Daniel

Quoted 1 line · Daniel Kiper

Recent work around x86 Linux kernel loader revealed an underflow in

the

Quoted 1 line

setup_header length calculation and another related issue. Both lead

to

Quoted 4 lines

the memory overwrite and later machine crash.

Currently when the GRUB copies the setup_header into the linux_params (struct boot_params, traditionally known as "zero page") it assumes

the

Quoted 2 lines

setup_header size as sizeof(linux_i386_kernel_header/lh). This is incorrect. It should use the value calculated accordingly to the

Linux

Quoted 3 lines

kernel boot protocol. Otherwise in case of pretty old kernel, to be exact Linux kernel boot protocol, the GRUB may write more into linux_params than it was expected to. Fortunately this is not very

big

Quoted 4 lines

issue. Though it has to be fixed. However, there is also an underflow which is grave. It happens when

sizeof(linux_i386_kernel_header/lh) > "real size of the

setup_header".

Quoted 2 lines

Then len value wraps around and grub_file_read() reads whole kernel

into

Quoted 4 lines

the linux_params overwriting memory past it. This leads to the GRUB memory allocator breakage and finally to its crash during boot.

The patch fixes both issues. Additionally, it moves the code not

related to

Quoted 1 line

grub_memset(linux_params)/grub_memcpy(linux_params)/grub_file_read(linux_params)

Review

Quoted 8 lines

section outside of it to not confuse the reader.

---
 grub-core/loader/i386/linux.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/grub-core/loader/i386/linux.c

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd

Quoted 4 lines

index d0501e229..ee95cd374 100644

grub-core/loader/i386/linux.c +0 -0
@@ -761,17 +761,12 @@ grub_cmd_linux (grub_command_t cmd

__attribute__ ((unused)),

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),

Quoted 4 lines
grub-core/loader/i386/linux.c +0 -1
     goto fail;
 
   grub_memset (&linux_params, 0, sizeof (linux_params));
-  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof

(lh) - 0x1F1);

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd · line 766

Quoted 2 lines
grub-core/loader/i386/linux.c +0 -2
-
-  linux_params.code32_start = prot_mode_target + lh.code32_start -

GRUB_LINUX_BZIMAGE_ADDR;

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),

Quoted 11 lines
grub-core/loader/i386/linux.c +1 -3
-  linux_params.kernel_alignment = (1 << align);
-  linux_params.ps_mouse = linux_params.padding10 =  0;
 
   /*
    * The Linux 32-bit boot protocol defines the setup header end
    * to be at 0x202 + the byte value at 0x201.
    */
-  len = 0x202 + *((char *) &linux_params.jump + 1);
+  len = 0x202 + *((char *) &lh.jump + 1);
 
   /* Verify the struct is big enough so we do not write past the

end. */

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd · line 772

Quoted 1 line
grub-core/loader/i386/linux.c +0 -0
   if (len > (char *) &linux_params.edd_mbr_sig_buffer - (char *)

&linux_params) {

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd

Quoted 1 line
grub-core/loader/i386/linux.c +0 -0
@@ -779,10 +774,13 @@ grub_cmd_linux (grub_command_t cmd

__attribute__ ((unused)),

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),

Quoted 4 lines
grub-core/loader/i386/linux.c +1 -0
     goto fail;
   }
 
+  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, len -

0x1F1);

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd · line 779

Quoted 2 lines
grub-core/loader/i386/linux.c +1 -0
+
   /* We've already read lh so there is no need to read it second

time. */

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),

Quoted 3 lines
grub-core/loader/i386/linux.c +0 -1
   len -= sizeof(lh);
 
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh),

len) != len)

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd · line 783

Quoted 2 lines
grub-core/loader/i386/linux.c +2 -0
+  if ((len > 0) &&
+      (grub_file_read (file, (char *) &linux_params + sizeof (lh),

len) != len))

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__

Quoted 4 lines
grub-core/loader/i386/linux.c +0 -0
     {
       if (!grub_errno)
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@@ -790,6 +788,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__

((unused)),

Quoted 3 lines
grub-core/loader/i386/linux.c +0 -0
       goto fail;
     }
 
Quoted 10 lines

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__ · line 791

Quoted 1 line
grub-core/loader/i386/linux.c +1 -0
+  linux_params.code32_start = prot_mode_target + lh.code32_start -

GRUB_LINUX_BZIMAGE_ADDR;

grub-core/loader/i386/linux.c grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),

Quoted 5 lines
grub-core/loader/i386/linux.c +2 -0
+  linux_params.kernel_alignment = (1 << align);
+  linux_params.ps_mouse = linux_params.padding10 = 0;
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
 
   /* These two are used (instead of cmd_line_ptr) by older versions

of Linux,

Quoted 2 lines

--
2.11.0

Please fix these problems. It is a huge headache for us in the kennel community.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Reviewed-by on v1 1/1
#3 lore raw
In reply to Daniel Kiper

Hello Daniel,

Quoted 16 lines · Daniel Kiper

Recent work around x86 Linux kernel loader revealed an underflow in the setup_header length calculation and another related issue. Both lead to the memory overwrite and later machine crash.

Currently when the GRUB copies the setup_header into the linux_params (struct boot_params, traditionally known as "zero page") it assumes the setup_header size as sizeof(linux_i386_kernel_header/lh). This is incorrect. It should use the value calculated accordingly to the Linux kernel boot protocol. Otherwise in case of pretty old kernel, to be exact Linux kernel boot protocol, the GRUB may write more into linux_params than it was expected to. Fortunately this is not very big issue. Though it has to be fixed. However, there is also an underflow which is grave. It happens when

sizeof(linux_i386_kernel_header/lh) > "real size of the setup_header".

Quoted 8 lines

Then len value wraps around and grub_file_read() reads whole kernel into the linux_params overwriting memory past it. This leads to the GRUB memory allocator breakage and finally to its crash during boot.

The patch fixes both issues. Additionally, it moves the code not related to grub_memset(linux_params)/grub_memcpy(linux_params)/grub_file_read(linux_params) section outside of it to not confuse the reader.

Maybe you should add the following tag?

Fixes e683cfb0cf5 ("loader/i386/linux: Calculate the setup_header length")
Quoted 2 lines

---

The patch looks good to me.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
Reviewed-by on v1 1/1
#4 lore raw
In reply to Daniel Kiper
Quoted 20 lines · Daniel Kiper

Recent work around x86 Linux kernel loader revealed an underflow in the setup_header length calculation and another related issue. Both lead to the memory overwrite and later machine crash.

Currently when the GRUB copies the setup_header into the linux_params (struct boot_params, traditionally known as "zero page") it assumes the setup_header size as sizeof(linux_i386_kernel_header/lh). This is incorrect. It should use the value calculated accordingly to the Linux kernel boot protocol. Otherwise in case of pretty old kernel, to be exact Linux kernel boot protocol, the GRUB may write more into linux_params than it was expected to. Fortunately this is not very big issue. Though it has to be fixed. However, there is also an underflow which is grave. It happens when

sizeof(linux_i386_kernel_header/lh) > "real size of the setup_header".

Then len value wraps around and grub_file_read() reads whole kernel into the linux_params overwriting memory past it. This leads to the GRUB memory allocator breakage and finally to its crash during boot.

Quoted 5 lines

The patch fixes both issues. Additionally, it moves the code not related to grub_memset(linux_params)/grub_memcpy(linux_params)/grub_file_read(linux_params) section outside of it to not confuse the reader.

Yes this looks correct. The length should be based off the jmp offset byte and not the size of the structure defined in GRUB.

Quoted 53 lines
---
 grub-core/loader/i386/linux.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
grub-core/loader/i386/linux.c +8 -7
index d0501e229..ee95cd374 100644
@@ -761,17 +761,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     goto fail;
 
   grub_memset (&linux_params, 0, sizeof (linux_params));
-  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
-
-  linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
-  linux_params.kernel_alignment = (1 << align);
-  linux_params.ps_mouse = linux_params.padding10 =  0;
 
   /*
    * The Linux 32-bit boot protocol defines the setup header end
    * to be at 0x202 + the byte value at 0x201.
    */
-  len = 0x202 + *((char *) &linux_params.jump + 1);
+  len = 0x202 + *((char *) &lh.jump + 1);
 
   /* Verify the struct is big enough so we do not write past the end. */
   if (len > (char *) &linux_params.edd_mbr_sig_buffer - (char *) &linux_params) {
@@ -779,10 +774,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     goto fail;
   }
 
+  grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, len - 0x1F1);
+
   /* We've already read lh so there is no need to read it second time. */
   len -= sizeof(lh);
 
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
+  if ((len > 0) &&
+      (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len))
     {
       if (!grub_errno)
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@@ -790,6 +788,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
+  linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
+  linux_params.kernel_alignment = (1 << align);
+  linux_params.ps_mouse = linux_params.padding10 = 0;
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
 
   /* These two are used (instead of cmd_line_ptr) by older versions of Linux,