From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 60002ECAAD3 for ; Fri, 9 Sep 2022 15:33:28 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DA84E84B52; Fri, 9 Sep 2022 17:33:11 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="JcocejDl"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5879684B67; Fri, 9 Sep 2022 17:33:06 +0200 (CEST) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 610F484B2B for ; Fri, 9 Sep 2022 17:33:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3978E6204E; Fri, 9 Sep 2022 15:33:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A793C43470; Fri, 9 Sep 2022 15:32:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662737579; bh=GKWz9ozpo3wSHes74jip+cDKqeZrzEvEZs5LsOb7GrI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=JcocejDl8AMXmZCvBtxfX4eg3BD6nY7vq9lvsKBBfhUX9b3Y5F9PCtGYOiUuqiZrD tMMxkwPjuFx5PErHeBQvtB5cmvojn1v3Sl1tenRERDxnqJzA/5Q6Cj4kSozQE0ARmW W4Ul20yXPtLyNb4hzEhLmey0veVWhglOdTuEtZm3sNxekHTWk5cdcj7hJkzhC3yg1w ZNz9Lij0s0KxUn4MaIqPnCe1ZB4PMFdsUeeT66XFILEVUarBGX9+j2pJu+gfp9vwYv CGOSFlSZVLNAOAi6GZiFtOETUo8B738Rl4zGRgzA+uyikwz+qze66+JTdGfr+pcmoA hU+N/lRLHMNoA== Received: by pali.im (Postfix) id C112A1301; Fri, 9 Sep 2022 17:32:56 +0200 (CEST) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: u-boot@lists.denx.de, =?UTF-8?q?Marek=20Beh=C3=BAn?= , Peng Fan , Tom Rini Subject: [PATCH 2/9] common/memsize.c: Fix get_effective_memsize() to check for overflow Date: Fri, 9 Sep 2022 17:32:39 +0200 Message-Id: <20220909153246.8455-3-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220909153246.8455-1-pali@kernel.org> References: <20220909153246.8455-1-pali@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Ensure that top of RAM can be represented by phys_size_t type. If RAM is too large or RAM base address is too upper then limit RAM size to prevent address space overflow. Signed-off-by: Pali Rohár --- common/memsize.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/common/memsize.c b/common/memsize.c index 31884acca0df..d11ed67632fb 100644 --- a/common/memsize.c +++ b/common/memsize.c @@ -94,11 +94,23 @@ long get_ram_size(long *base, long maxsize) phys_size_t __weak get_effective_memsize(void) { + phys_size_t ram_size = gd->ram_size; + + /* + * Check for overflow and limit ram size to some representable value. + * It is required that ram_base + ram_size must be representable by + * phys_size_t type and must be aligned by direct access, therefore + * calculate it from last 4kB sector which should work as alignment + * on any platform. + */ + if (gd->ram_base + ram_size < gd->ram_base) + ram_size = ((phys_size_t)~0xfffULL) - gd->ram_base; + #ifndef CONFIG_MAX_MEM_MAPPED - return gd->ram_size; + return ram_size; #else /* limit stack to what we can reasonable map */ - return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ? - CONFIG_MAX_MEM_MAPPED : gd->ram_size); + return ((ram_size > CONFIG_MAX_MEM_MAPPED) ? + CONFIG_MAX_MEM_MAPPED : ram_size); #endif } -- 2.20.1