Skip to contentSkip to conversation

efi: fix boundary checking in efi_high_alloc()

PATCH In review Yinghai Lu 4 participants 4 messages Feb 20, 2015 – Feb 24, 2015 EFI

lore
Share & tools
lore

Yinghai Lu

author
#0 lore raw

PATCH 1/1 efi: fix boundary checking in efi_high_alloc()

While adding support loading kernel and initrd above 4G to grub2 in legacy mode, I was referring to efi_high_alloc().
That will allocate buffer for kernel and then initrd, and initrd will use kernel buffer start as limit.

During testing found two buffers will be overlapped when initrd size is very big like 400M.

It turns out efi_high_alloc() boundary checking is not right. end - size will be the new start, and should not compare new start with max, we need to make sure end is smaller than max.

---
 drivers/firmware/efi/libstub/efi-stub-helper.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +4 -4
Index: linux-2.6/drivers/firmware/efi/libstub/efi-stub-helper.c
===================================================================
@@ -183,12 +183,12 @@ again:
 		start = desc->phys_addr;
 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
 
-		if ((start + size) > end || (start + size) > max)
-			continue;
-
-		if (end - size > max)
+		if (end > max)
 			end = max;
 
+		if ((start + size) > end)
+			continue;
+
 		if (round_down(end - size, align) < start)
 			continue;
 
#1 lore raw
Quoted 12 lines · Yinghai Lu

While adding support loading kernel and initrd above 4G to grub2 in legacy mode, I was referring to efi_high_alloc().
That will allocate buffer for kernel and then initrd, and initrd will use kernel buffer start as limit.

During testing found two buffers will be overlapped when initrd size is very big like 400M.

It turns out efi_high_alloc() boundary checking is not right. end - size will be the new start, and should not compare new start with max, we need to make sure end is smaller than max.

Quoted 5 lines
---
 drivers/firmware/efi/libstub/efi-stub-helper.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

The patch looks fine but this changelog could do with some finessing because I spent 30 minutes trying to understand what the bug was, and more specifically, what values to feed into the allocation algorithm to trigger it.

Basically, with the current efi_high_alloc() code it's possible to allocate memory above 'max', because efi_high_alloc() doesn't check that the tail of the allocation is below 'max'.

If you have an EFI memory map with a single entry that looks like so,

[0xc0000000-0xc0004000]

And want to allocate 0x3000 bytes below 0xc0003000 the current code will allocate [0xc0001000-0xc0004000], not [0xc0000000-0xc0003000] like you would expect.

I've queued up the below patch. If the Linaro/ARM gang could test it out I'd really appreciate it.

---

Quoted 1 line

From 1e7295b5d4c5226a160a9167e61b581e388f7f9a Mon Sep 17 00:00:00 2001

From: Yinghai Lu <[email protected]>
Date: Thu, 19 Feb 2015 20:18:03 -0800
Subject: [PATCH] efi/libstub: Fix boundary checking in efi_high_alloc()

While adding support loading kernel and initrd above 4G to grub2 in legacy mode, I was referring to efi_high_alloc().
That will allocate buffer for kernel and then initrd, and initrd will use kernel buffer start as limit.

During testing found two buffers will be overlapped when initrd size is very big like 400M.

It turns out efi_high_alloc() boundary checking is not right. end - size will be the new start, and should not compare new start with max, we need to make sure end is smaller than max.

[ Basically, with the current efi_high_alloc() code it's possible to
allocate memory above 'max', because efi_high_alloc() doesn't check that the tail of the allocation is below 'max'.

If you have an EFI memory map with a single entry that looks like so,

[0xc0000000-0xc0004000]

And want to allocate 0x3000 bytes below 0xc0003000 the current code will allocate [0xc0001000-0xc0004000], not [0xc0000000-0xc0003000] like you would expect. - Matt ]

---
 drivers/firmware/efi/libstub/efi-stub-helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +4 -4
index 9bd9fbb5bea8..c927bccd92bd 100644
@@ -170,12 +170,12 @@ again:
 		start = desc->phys_addr;
 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
 
-		if ((start + size) > end || (start + size) > max)
-			continue;
-
-		if (end - size > max)
+		if (end > max)
 			end = max;
 
+		if ((start + size) > end)
+			continue;
+
 		if (round_down(end - size, align) < start)
 			continue;
 
Signature · 4 lines
1.9.3

-- 
Matt Fleming, Intel Open Source Technology Center
Reviewed-by Tested-by
#2 lore raw

Hi Matt,

Quoted 23 lines

From 1e7295b5d4c5226a160a9167e61b581e388f7f9a Mon Sep 17 00:00:00 2001
From: Yinghai Lu <[email protected]>
Date: Thu, 19 Feb 2015 20:18:03 -0800
Subject: [PATCH] efi/libstub: Fix boundary checking in efi_high_alloc()

While adding support loading kernel and initrd above 4G to grub2 in legacy mode, I was referring to efi_high_alloc().
That will allocate buffer for kernel and then initrd, and initrd will use kernel buffer start as limit.

During testing found two buffers will be overlapped when initrd size is very big like 400M.

It turns out efi_high_alloc() boundary checking is not right. end - size will be the new start, and should not compare new start with max, we need to make sure end is smaller than max.

[ Basically, with the current efi_high_alloc() code it's possible to
allocate memory above 'max', because efi_high_alloc() doesn't check that the tail of the allocation is below 'max'.

If you have an EFI memory map with a single entry that looks like so,

Quoted 8 lines
   [0xc0000000-0xc0004000]

And want to allocate 0x3000 bytes below 0xc0003000 the current code will allocate [0xc0001000-0xc0004000], not [0xc0000000-0xc0003000] like you would expect. - Matt ]

I've convinced myself that the new logic is sound, and with this patch applied atop of v4.0-rc1 I don't see regressions on the platforms I have access to. So:

Ard, Leif:

On a related note, I think that the logic for deciding where to place the kernel and DTB isn't quite right. The kernel needs to be in the same naturally-aligned 512M region as the DTB in order to be able to map it, but the kernel could get relocated above the max address we'll consider for the DTB if there isn't sufficient space for the kernel between dram_base and dram_base + 512M.

We should try to use the fixmap to map the DTB so it can be located anywhere in physical memory. That will make things easier for the stub and other loaders.

Thanks,
Mark.

Quoted 36 lines
---
 drivers/firmware/efi/libstub/efi-stub-helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +4 -4
index 9bd9fbb5bea8..c927bccd92bd 100644
@@ -170,12 +170,12 @@ again:
 		start = desc->phys_addr;
 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
 
-		if ((start + size) > end || (start + size) > max)
-			continue;
-
-		if (end - size > max)
+		if (end > max)
 			end = max;
 
+		if ((start + size) > end)
+			continue;
+
 		if (round_down(end - size, align) < start)
 			continue;
 
Signature · 8 lines
1.9.3

-- 
Matt Fleming, Intel Open Source Technology Center
--
To unsubscribe from this list: send the line "unsubscribe linux-efi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by
#3 lore raw
Quoted 34 lines · Mark Rutland

Hi Matt,

Quoted 23 lines

From 1e7295b5d4c5226a160a9167e61b581e388f7f9a Mon Sep 17 00:00:00 2001
From: Yinghai Lu <[email protected]>
Date: Thu, 19 Feb 2015 20:18:03 -0800
Subject: [PATCH] efi/libstub: Fix boundary checking in efi_high_alloc()

While adding support loading kernel and initrd above 4G to grub2 in legacy mode, I was referring to efi_high_alloc().
That will allocate buffer for kernel and then initrd, and initrd will use kernel buffer start as limit.

During testing found two buffers will be overlapped when initrd size is very big like 400M.

It turns out efi_high_alloc() boundary checking is not right. end - size will be the new start, and should not compare new start with max, we need to make sure end is smaller than max.

[ Basically, with the current efi_high_alloc() code it's possible to
allocate memory above 'max', because efi_high_alloc() doesn't check that the tail of the allocation is below 'max'.

If you have an EFI memory map with a single entry that looks like so,

Quoted 8 lines
   [0xc0000000-0xc0004000]

And want to allocate 0x3000 bytes below 0xc0003000 the current code will allocate [0xc0001000-0xc0004000], not [0xc0000000-0xc0003000] like you would expect. - Matt ]

Quoted 7 lines

I've convinced myself that the new logic is sound, and with this patch applied atop of v4.0-rc1 I don't see regressions on the platforms I have access to. So:

Quoted 9 lines

Ard, Leif:

On a related note, I think that the logic for deciding where to place the kernel and DTB isn't quite right. The kernel needs to be in the same naturally-aligned 512M region as the DTB in order to be able to map it, but the kernel could get relocated above the max address we'll consider for the DTB if there isn't sufficient space for the kernel between dram_base and dram_base + 512M.

It also assumes that dram_base itself is 512M aligned, which may not necessarily be the case, so yes, that logic seems broken.

Quoted 4 lines

We should try to use the fixmap to map the DTB so it can be located anywhere in physical memory. That will make things easier for the stub and other loaders.

Yes, that would be useful, but I think it shouldn't be /that/ hard to fix the stub

Signature · 1 line
Ard.
Quoted 35 lines
Quoted 35 lines
---
 drivers/firmware/efi/libstub/efi-stub-helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +4 -5
index 9bd9fbb5bea8..c927bccd92bd 100644
@@ -170,12 +170,12 @@ again:
              start = desc->phys_addr;
              end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
 
-             if ((start + size) > end || (start + size) > max)
-                     continue;
-
-             if (end - size > max)
+             if (end > max)
                      end = max;
 
+             if ((start + size) > end)
+                     continue;
+
              if (round_down(end - size, align) < start)
                      continue;
 
--

1.9.3

--
Matt Fleming, Intel Open Source Technology Center
--
To unsubscribe from this list: send the line "unsubscribe linux-efi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html