Blob Blame History Raw
From 9ceee7d0841a8f7d7644021ba7d4cc1fbc7966e3 Mon Sep 17 00:00:00 2001
From: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
Date: Wed, 10 Mar 2021 00:31:27 -0800
Subject: [PATCH] firmware/efi: Fix a use after bug in efi_mem_reserve_persistent
Git-commit: 9ceee7d0841a8f7d7644021ba7d4cc1fbc7966e3
Patch-mainline: v5.12-rc4
References: git-fixes

In the for loop in efi_mem_reserve_persistent(), prsv = rsv->next
use the unmapped rsv. Use the unmapped pages will cause segment
fault.

Fixes: 18df7577adae6 ("efi/memreserve: deal with memreserve entries in unmapped memory")
Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Takashi Iwai <tiwai@suse.de>

---
 drivers/firmware/efi/efi.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -939,7 +939,7 @@ int __ref efi_mem_reserve_persistent(phy
 	}
 
 	/* first try to find a slot in an existing linked list entry */
-	for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
+	for (prsv = efi_memreserve_root->next; prsv; ) {
 		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
 		/* implement atomic_fetch_add_unless for
 		 * index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
@@ -958,6 +958,7 @@ int __ref efi_mem_reserve_persistent(phy
 			memunmap(rsv);
 			return 0;
 		}
+		prsv = rsv->next;
 		memunmap(rsv);
 	}