Commit 28e78736 authored by Yinghai Lu's avatar Yinghai Lu Committed by Luis Henriques

efi/libstub: Fix boundary checking in efi_high_alloc()

commit 7ed620bb upstream.

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 ]
Signed-off-by: default avatarYinghai Lu <yinghai@kernel.org>
Reviewed-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: default avatarMark Rutland <mark.rutland@arm.com>
Tested-by: default avatarMark Rutland <mark.rutland@arm.com>
Signed-off-by: default avatarMatt Fleming <matt.fleming@intel.com>
[ luis: backported to 3.16:
  - file rename: drivers/firmware/efi/libstub/efi-stub-helper.c ->
    drivers/firmware/efi/efi-stub-helper.c ]
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent 1ff480c6
...@@ -157,12 +157,12 @@ static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg, ...@@ -157,12 +157,12 @@ static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
start = desc->phys_addr; start = desc->phys_addr;
end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
if ((start + size) > end || (start + size) > max) if (end > max)
continue;
if (end - size > max)
end = max; end = max;
if ((start + size) > end)
continue;
if (round_down(end - size, align) < start) if (round_down(end - size, align) < start)
continue; continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment