Skip to content

Commit 7ed620b

Browse files
Yinghai LuMatt Fleming
authored andcommitted
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 ] Signed-off-by: Yinghai Lu <yinghai@kernel.org> Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Reviewed-by: Mark Rutland <mark.rutland@arm.com> Tested-by: Mark Rutland <mark.rutland@arm.com> Cc: <stable@vger.kernel.org> Signed-off-by: Matt Fleming <matt.fleming@intel.com>
1 parent ce204e9 commit 7ed620b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

drivers/firmware/efi/libstub/efi-stub-helper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
170170
start = desc->phys_addr;
171171
end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
172172

173-
if ((start + size) > end || (start + size) > max)
174-
continue;
175-
176-
if (end - size > max)
173+
if (end > max)
177174
end = max;
178175

176+
if ((start + size) > end)
177+
continue;
178+
179179
if (round_down(end - size, align) < start)
180180
continue;
181181

0 commit comments

Comments
 (0)