Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp332968pxb; Mon, 16 Aug 2021 06:31:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwlMPaW7M+RJNy70gz0QwrtfsoqAby3x2QawpZ5J4J+eqz61FBeqfAgoInRFQtdWR7T6jU/ X-Received: by 2002:a50:ed05:: with SMTP id j5mr20589736eds.256.1629120707754; Mon, 16 Aug 2021 06:31:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629120707; cv=none; d=google.com; s=arc-20160816; b=PjZHi7Ky+31YG/1kX7OD5w5za43AU5IVCMkKEAbFfEjVqalWxz/F2xLr4HcbVuQ2xH pcVRxPJxyWqlHpEdhoXZ+dAcsP9/nVNEFOXwpLXlrTSDhpP0B7kjseFYuId094CXXds8 VC00WiUwYb4GdqVGRw+CtsHX5AXF8mF6gE953rfG83J824HSwau99XrjOi8tJIbtZh/+ 3aOvMRZt19jBIJ9mTah1/0oDJJUriizi+/XIAKHNCzm7niIvacfVUq3Gy6q0BO4fm/nK TUW1zDatKrgKV1v5ptAtKGzTI5eLhpvBWwewhgHhmbOKhWCOkWCR/OFmij5dfDS2RFV1 e8Rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EEyAkc1NwLcbtmzZ0eZu6PhrPftqLxyQuf0H5/fup/s=; b=gZqDGCR+QhFAneWNp8SKiV3YQp7FCahVlJdeAKHlCBSIIZQq6JkPOxyWG16t+sWGJP 9usKdp+kG5g5AKhmq0VXd68BIugC+2GRVFR643ordr2RFV2GK+NtMi773Lj5r2cQ+blD iG6xViR1n56FKl4qdz0/yxIWHKAz4OGoBA2BjCOU6gnBDYP/+Rr/Gf94xkuUiG4dGnGO kdt3AyZkvvjEwPv/m0pN1twxE1in/C4iRUmNT2UNVe3Q4gETogoadGINHBVQgtZfXCNr qTnTFZvGo2gvgRnwmub7NmejUP3ZepREVwT0kDOCKoR4DfWw4OAg8J2U9ODwmF8aQ7Er TDRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=siFNa4YV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id di1si9152995ejc.420.2021.08.16.06.31.23; Mon, 16 Aug 2021 06:31:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=siFNa4YV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241196AbhHPN3d (ORCPT + 99 others); Mon, 16 Aug 2021 09:29:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:44618 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239213AbhHPNSs (ORCPT ); Mon, 16 Aug 2021 09:18:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 10A16632A9; Mon, 16 Aug 2021 13:14:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1629119665; bh=9I19CVjzRFMAqjf5ap1zDjUFU8VQHXVP1hc56kXyw5A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=siFNa4YVnRYH7DuMJTCDhhAlTqxrMX1Yb0konNqwM8y3I4/lAc5nWxkWKHskAxI6g Mu+aPa1G9kVf3UAFCwGdmisSIUShKqd+R8vXbW2ZnVBqvjUB2Y0Z3FDsdqLNKuVWO1 3CJzqrk/ZgFTMf6GFLqDxKskN9yMYLtXrEX2Yfx4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Benjamin Herrenschmidt , Ard Biesheuvel , Sasha Levin Subject: [PATCH 5.13 114/151] arm64: efi: kaslr: Fix occasional random alloc (and boot) failure Date: Mon, 16 Aug 2021 15:02:24 +0200 Message-Id: <20210816125447.810959705@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210816125444.082226187@linuxfoundation.org> References: <20210816125444.082226187@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Benjamin Herrenschmidt [ 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 Fixes: 2ddbfc81eac8 ("efi: stub: add implementation of efi_random_alloc()") Signed-off-by: Ard Biesheuvel Signed-off-by: Sasha Levin --- drivers/firmware/efi/libstub/randomalloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index a408df474d83..724155b9e10d 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -30,6 +30,8 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1, (u64)ULONG_MAX); + if (region_end < size) + return 0; first_slot = round_up(md->phys_addr, align); last_slot = round_down(region_end - size + 1, align); -- 2.30.2