[PATCH] efi: sanity check in case memermap() at efi_mem_reserve_persistent() returns NULL

From: Masayoshi Mizuma
Date: Wed Nov 13 2019 - 15:57:07 EST


From: Masayoshi Mizuma <m.mizuma@xxxxxxxxxxxxxx>

System may crash as NULL pointer dereference in case memremap()
at efi_mem_reserve_persistent() returns NULL.

Add a sanity check to avoid that.

Signed-off-by: Masayoshi Mizuma <m.mizuma@xxxxxxxxxxxxxx>
Reported-by: HATAYAMA Daisuke <d.hatayama@xxxxxxxxxxxxxx>
---
drivers/firmware/efi/efi.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
index 07812d697..1d5ae7b95 100644
--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -1036,6 +1036,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; prsv = rsv->next) {
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;
--
2.21.0