Skip to contentSkip to conversation

calculation overflow in grub_mm_init_region

DISCUSSION Leif Lindholm 3 participants 8 messages Aug 29, 2013 – Sep 11, 2013

lore
Share & tools
lore
#0 lore raw

When allocating memory for the heap on ARMv7 UEFI, the init code pretty much just allocates a chunk from the top of available RAM.

This means that when grub_mm_init_region is called for a region extending to the top of the 32-bit address space, addr + size == 0. However, this is not taken into account by arithmetic in this function, causing Grub to fail on one of my platforms[1] before "Welcome to GRUB".

Having some trouble getting my head around the grub_mm_init_region code right now (where is grub_mm_base initialised?), so don't have a patch.

/
Leif

[1] Versatile Express V2P-CA15_A7, also known as TC2, has 2GB of RAM

    starting at 0x80000000.  
#1 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

Quoted 9 lines · Leif Lindholm

When allocating memory for the heap on ARMv7 UEFI, the init code pretty much just allocates a chunk from the top of available RAM.

This means that when grub_mm_init_region is called for a region extending to the top of the 32-bit address space, addr + size == 0. However, this is not taken into account by arithmetic in this function, causing Grub to fail on one of my platforms[1] before "Welcome to GRUB".

Quoted 3 lines

Having some trouble getting my head around the grub_mm_init_region code right now (where is grub_mm_base initialised?), so don't have a patch.

So, sat down to dissect the code a bit, and turns out that apart from a debug message (which is #ifdef 0 by default), grub_mm_init_region() is actually innocent.

The error lies in get_header_from_pointer() (also kern/mm.c), and its comparisons regarding whether a pointer lies within the current region or not.

My suggested fix is as follows (with a little bit of refactoring to help my brain).

/
Leif

=== modified file 'grub-core/kern/mm.c'

kern/mm.c 2013-09-10 11:13:36 +0000 renamed from kern/mm.c 2013-04-20 15:39:49 +0000 +10 -3
@@ -86,13 +86,20 @@
 static void
 get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r)
 {
+  grub_addr_t block_start = (grub_addr_t) ptr;
+
   if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1))
     grub_fatal ("unaligned pointer %p", ptr);
 
   for (*r = grub_mm_base; *r; *r = (*r)->next)
-    if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1)
-       && (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size)
-      break;
+    {
+      grub_addr_t region_start = (grub_addr_t) ((*r) + 1);
+      grub_addr_t region_end = (grub_addr_t) ((*r) + 1) + (*r)->size;
+
+      if (block_start > region_start)
+       if ((block_start <= region_end) || (region_end == 0))
+         break;
+    }
 
   if (! *r)
     grub_fatal ("out of range pointer %p", ptr);
#2 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

Quoting Leif Lindholm, who wrote the following on Tue, 10 Sep 2013:

Quoted 40 lines · Leif Lindholm
Quoted 9 lines

When allocating memory for the heap on ARMv7 UEFI, the init code pretty much just allocates a chunk from the top of available RAM.

This means that when grub_mm_init_region is called for a region extending to the top of the 32-bit address space, addr + size == 0. However, this is not taken into account by arithmetic in this function, causing Grub to fail on one of my platforms[1] before "Welcome to GRUB".

Quoted 3 lines

Having some trouble getting my head around the grub_mm_init_region code right now (where is grub_mm_base initialised?), so don't have a patch.

So, sat down to dissect the code a bit, and turns out that apart from a debug message (which is #ifdef 0 by default), grub_mm_init_region() is actually innocent.

The error lies in get_header_from_pointer() (also kern/mm.c), and its comparisons regarding whether a pointer lies within the current region or not.

My suggested fix is as follows (with a little bit of refactoring to help my brain).

/
Leif

=== modified file 'grub-core/kern/mm.c'

kern/mm.c 2013-09-10 11:13:36 +0000 renamed from kern/mm.c 2013-04-20 15:39:49 +0000 +0 -0
@@ -86,13 +86,20 @@

static void
get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r) {

kern/mm.c 2013-09-10 11:13:36 +0000 renamed from kern/mm.c 2013-04-20 15:39:49 +0000 +2 -0
+  grub_addr_t block_start = (grub_addr_t) ptr;
+
  if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1))
    grub_fatal ("unaligned pointer %p", ptr);
 

kern/mm.c 2013-09-10 11:13:36 +0000 line 100

Quoted 10 lines
kern/mm.c 2013-09-10 11:13:36 +0000 renamed from kern/mm.c 2013-04-20 15:39:49 +0000 +6 -3
  for (*r = grub_mm_base; *r; *r = (*r)->next)
-    if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1)
-       && (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size)
-      break;
+    {
+      grub_addr_t region_start = (grub_addr_t) ((*r) + 1);
+      grub_addr_t region_end = (grub_addr_t) ((*r) + 1) + (*r)->size;
+
+      if (block_start > region_start)
+       if ((block_start <= region_end) || (region_end == 0))

Why would region_end be zero? That sounds like an invalid value.

--S

#3 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

Quoted 1 line · Seth Goldberg

Why would region_end be zero? That sounds like an invalid value.

Because when a heap region sits at the top of RAM, region base + region size == 0.

/
Leif

#4 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

Quoted 5 lines · Leif Lindholm
Quoted 1 line · Seth Goldberg

Why would region_end be zero? That sounds like an invalid value.

Because when a heap region sits at the top of RAM, region base + region size == 0.

(On that specific platform. In general, when it sits at the top of addressable memory.)

/
Leif

#6 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

In reply to Leif Lindholm

kern/mm.c 2013-09-10 11:13:36 +0000 line 102

Quoted 13 lines
   for (*r = grub_mm_base; *r; *r = (*r)->next)
kern/mm.c 2013-09-10 11:13:36 +0000 renamed from kern/mm.c 2013-04-20 15:39:49 +0000 +8 -3
-    if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1)
-       && (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size)
-      break;
+    {
+      grub_addr_t region_start = (grub_addr_t) ((*r) + 1);
+      grub_addr_t region_end = (grub_addr_t) ((*r) + 1) + (*r)->size;
+
+      if (block_start > region_start)
+       if ((block_start <= region_end) || (region_end == 0))
+         break;
+    }

This fix looks correct but as indicated by us not having discovered this bug earlier, this is a very unusual case and it's difficult to ensure that similar bug doesn't pop up in another place or that we don't suffer a regression. I'd prefer to exclude top 4K of adressable memory from heap as safety measure. Are you ok with this approach?

#7 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

Quoted 5 lines · Vladimir 'φ-coder/phcoder' Serbinenko

This fix looks correct but as indicated by us not having discovered this bug earlier, this is a very unusual case and it's difficult to ensure that similar bug doesn't pop up in another place or that we don't suffer a regression. I'd prefer to exclude top 4K of adressable memory from heap as safety measure. Are you ok with this approach?

I am happy with that.

/
Leif

#5 lore raw

Re: calculation overflow in grub_mm_init_region (patch)

In reply to Leif Lindholm
Quoted 5 lines · Leif Lindholm
Quoted 1 line · Seth Goldberg

Why would region_end be zero? That sounds like an invalid value.

Because when a heap region sits at the top of RAM, region base + region size == 0.

So, an overflow. Ugh. Reserving the last 4k would be fine, too, yes.

--S

Quoted 8 lines

/
Leif

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