From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED63F2C9C for ; Tue, 7 Feb 2023 13:11:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7188FC433D2; Tue, 7 Feb 2023 13:11:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1675775466; bh=NaqXkfFZtpoDH9Rm1mz+i9p6uXwYGnK7GsGYT4K3MM0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zHwInzKFzkWV3erUP2PLlElu9RUnFn9Fm8WzKX/mO0KMUF5lNDT5XaBKyvkDLp0wf bAjd6u9jiVG5CvBK+39kBZINf5JhajQEXgaLs7aBgmnoNFvsw8hI8DEi4IuH7mCT/N 7gmxVQfHTbjt9PhGXbMSplnh158Yc4p/7/z09DAE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Anton Gusev , Ard Biesheuvel , Sasha Levin Subject: [PATCH 5.15 050/120] efi: fix potential NULL deref in efi_mem_reserve_persistent Date: Tue, 7 Feb 2023 13:57:01 +0100 Message-Id: <20230207125620.909127447@linuxfoundation.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230207125618.699726054@linuxfoundation.org> References: <20230207125618.699726054@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anton Gusev [ Upstream commit 966d47e1f27c45507c5df82b2a2157e5a4fd3909 ] 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 [ardb: return -ENOMEM instead of breaking out of the loop] Signed-off-by: Ard Biesheuvel Signed-off-by: Sasha Levin --- 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 a2765d668856..332739f3eded 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -950,6 +950,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; -- 2.39.0