efi: fix potential NULL deref in efi_mem_reserve_persistent
authorAnton Gusev <aagusev@ispras.ru>
Fri, 3 Feb 2023 13:22:13 +0000 (16:22 +0300)
committerArd Biesheuvel <ardb@kernel.org>
Fri, 3 Feb 2023 13:52:10 +0000 (14:52 +0100)
When iterating on a linked list, a result of memremap is dereferenced
without checking it for NULL.

This patch adds a check that falls back on allocating a new page in
case memremap doesn't succeed.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 18df7577adae ("efi/memreserve: deal with memreserve entries in unmapped memory")
Signed-off-by: Anton Gusev <aagusev@ispras.ru>
[ardb: return -ENOMEM instead of breaking out of the loop]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
drivers/firmware/efi/efi.c

index a2b0cbc8741c2ff1cf175c4d70a7eba6b7d37050..1e0b016fdc2b121722aab37f11a8d4da7c0a146d 100644 (file)
@@ -1007,6 +1007,8 @@ int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
        /* first try to find a slot in an existing linked list entry */
        for (prsv = efi_memreserve_root->next; prsv; ) {
                rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
+               if (!rsv)
+                       return -ENOMEM;
                index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
                if (index < rsv->size) {
                        rsv->entry[index].base = addr;