Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3395755pxf; Mon, 22 Mar 2021 05:35:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4e+7r92bex7u0JjaFsRoSAJlPDMv4DGOJ5ol00+BtfShcTQP1JGrffPqPjsfsquh/yB+m X-Received: by 2002:a05:6402:22bb:: with SMTP id cx27mr25188647edb.148.1616416525675; Mon, 22 Mar 2021 05:35:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616416525; cv=none; d=google.com; s=arc-20160816; b=qQJWC5EaRLcVXcPKN+ElGn7TOCV8psgj/UF4YFIU4FZuwP7cUleq8TTPG4RR2Ixp84 JRR3a7KVWLFMsmJVI5NJotBaoKQD0s3IJZ6ysD5sr10Xfrc7WHl5JJnm6i+HprUkfiYy zGCtryC0Zm9qlKGh5TDvGW84o8/P7nryTMFcv8205U3iTkW9Yf9OwgIo+WgjyrK+OIiC juiGqg+vaL77AvbiD56EThli0bEWdHswn22Nw+LkL9o9JKqED2YZB45kQwGl/zQp8pyx ySFEOwMqHBU4XXEoImldbsnaIVZZjAyn0yafg/W3vadw1ZANsEG0zoI3m8TIMqbTgd5y c6+A== 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=+FlaJgJWzmAQxRbXN9sgp1Blb3/5ildKab7C+RcmivU=; b=ybMthzZGdBnF+hlsR/U+z3r6345aEELLUFkOvkpLC1AGPBt77vDFgvQANehgC0ccyd oxpA37CSxR3o9PrUzL5XCnWlEFjwWn4JfPmXZ0P30ENnr4BZNw0nrr/gJDRHv9AZzxo0 NXlc8LzaxlO1do20k0A80KGgRAYjGVTEegR9BB4JabNkmZDSdcIH5qLpKl9mt0GJyXU1 9XMExnjjvL0zPUbZji+ctVc6g6u/hZxNtkxJBUrgGsccP+jTdAIlQ0klmd8xjBhk/fmd V12AtDXfMljJlR3sWKPP33aE/9yK2hH3FgQkZcED8Ie+3755sg0wTwKYOyq/x6Ma1vce odww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=R2FGRodi; 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 r27si10639307edw.299.2021.03.22.05.35.03; Mon, 22 Mar 2021 05:35:25 -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=R2FGRodi; 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 S231355AbhCVMeJ (ORCPT + 99 others); Mon, 22 Mar 2021 08:34:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:54830 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231461AbhCVMcJ (ORCPT ); Mon, 22 Mar 2021 08:32:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8BB58619AC; Mon, 22 Mar 2021 12:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1616416325; bh=B3PPLBDMOBIwY84Bxhkm1yXvgoUED/+sozaxtpbearc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R2FGRodiiVKMnQZYLUtrgBPNE3iUgg5/d6jj1rKTvo3QpsqBYtbh40v3UytD2Ieec XpAFZW18x86+kW6FDjlIK5ddZ0XGMkSL9LZDrFO4mxd3w3jwCh/B+96+asIfbhQLuH cVwOMaUOveCF35HuNk63M9Hqx9oeipAg/IAJdy3A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Geert Uytterhoeven , Atish Patra , Palmer Dabbelt Subject: [PATCH 5.11 062/120] RISC-V: Fix out-of-bounds accesses in init_resources() Date: Mon, 22 Mar 2021 13:27:25 +0100 Message-Id: <20210322121931.759944628@linuxfoundation.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210322121929.669628946@linuxfoundation.org> References: <20210322121929.669628946@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: Geert Uytterhoeven commit ce989f1472ae350e844b10c880b22543168fbc92 upstream. init_resources() allocates an array of resources, based on the current total number of memory regions and reserved memory regions. However, allocating this array using memblock_alloc() might increase the number of reserved memory regions. If that happens, populating the array later based on the new number of regions will cause out-of-bounds writes beyond the end of the allocated array. Fix this by allocating one more entry, which may or may not be used. Fixes: 797f0375dd2ef5cd ("RISC-V: Do not allocate memblock while iterating reserved memblocks") Signed-off-by: Geert Uytterhoeven Reviewed-by: Atish Patra Cc: stable@vger.kernel.org Signed-off-by: Palmer Dabbelt Signed-off-by: Greg Kroah-Hartman --- arch/riscv/kernel/setup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -147,7 +147,8 @@ static void __init init_resources(void) bss_res.end = __pa_symbol(__bss_stop) - 1; bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; - mem_res_sz = (memblock.memory.cnt + memblock.reserved.cnt) * sizeof(*mem_res); + /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */ + mem_res_sz = (memblock.memory.cnt + memblock.reserved.cnt + 1) * sizeof(*mem_res); mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES); if (!mem_res) panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);