arm64: efi: kaslr: Fix occasional random alloc (and boot) failure
authorBenjamin Herrenschmidt <benh@kernel.crashing.org>
Tue, 20 Jul 2021 11:14:05 +0000 (21:14 +1000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 18 Aug 2021 06:59:14 +0000 (08:59 +0200)
commitafcb84e6cf8cf8ab1fb8b4bfa44c6caad64ae581
treec26eb9b49a7268813b831f06d1a24166895f7057
parente0ee8d9c31b5a670f35a4ff7e2daf59967f2f27a
arm64: efi: kaslr: Fix occasional random alloc (and boot) failure

[ Upstream commit 4152433c397697acc4b02c4a10d17d5859c2730d ]

The EFI stub random allocator used for kaslr on arm64 has a subtle
bug. In function get_entry_num_slots() which counts the number of
possible allocation "slots" for the image in a given chunk of free
EFI memory, "last_slot" can become negative if the chunk is smaller
than the requested allocation size.

The test "if (first_slot > last_slot)" doesn't catch it because
both first_slot and last_slot are unsigned.

I chose not to make them signed to avoid problems if this is ever
used on architectures where there are meaningful addresses with the
top bit set. Instead, fix it with an additional test against the
allocation size.

This can cause a boot failure in addition to a loss of randomisation
due to another bug in the arm64 stub fixed separately.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Fixes: 2ddbfc81eac8 ("efi: stub: add implementation of efi_random_alloc()")
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/firmware/efi/libstub/randomalloc.c