Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2491126lqp; Sun, 24 Mar 2024 23:21:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV92EfBApllq/oLb8+zb1Rt28u+vlRmWRoGejfnfLkKyGy/S4BU6GmmjVUbEPTPQVuQ9hM7vDUkieikMim1TV5MUDB1bomhpkWMEpz48Q== X-Google-Smtp-Source: AGHT+IGEtRDXejElxlTOwNkA5Gf4NX8xuqJriOdVSscD0EAG1K+Q+nOqp40UeJhQo2PBiV6s1twI X-Received: by 2002:a50:9b46:0:b0:566:44ca:9f5c with SMTP id a6-20020a509b46000000b0056644ca9f5cmr3857177edj.8.1711347707180; Sun, 24 Mar 2024 23:21:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711347707; cv=pass; d=google.com; s=arc-20160816; b=ZmRLx+GWDXXUGZSt4V1JYPbhmyHBxAZN34aK34gaOqxFJX3q95I3SCI+edk3GVHrI4 zHV1daMDDqpTpqNtD+yUq2y3fRh4qVIufVnIVtKXC4GiHYZasdV5M4FK5UJFSSDNACgz /BhqGAFml3I+veoX6Joh1uDXUd82yYVrHEyNCedHmYvIi1LBMkcu8kzEl1nvLD78lSHT Ooabs7qHsQisq6sGcrDiVU/LXzPTzHO5vXgI2ZWT2lr1UpV6i4NoVpQsjGUhfAazbxXB IbqPmil273peC1V8j4Wu/9Ls3aLjVyvMAP2VXa3BJpI5uPrI252aXkY8AFnD2OCqYEwH tKOA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ozV5uZINkrhxWYodVet3JJP16qJMmMvCrlDD7YBGIBk=; fh=KgNp+pExLxhj/p6Ia7SuKy80oWZUC2+PEawxAXmyhws=; b=o9tu+9yb76tT/PoDKaMsFDLP5FMY2wDgfcyQcIdwojP68+riwjtA1vuIi3tU0DiRre hVyf/Yjrj7V2zb3Cn890aT7Fq+edIslKQwQLKwNbD3ZBXu46AjH/FMA8S1LDD/aXAeUT ynKgJKDhs9dEkmYK/ehEe9+txGJ8Qad2jHnOMaULTAR3X3BCK4SwFrlffYLop/wCJ/zN PUrRBqy2gz2EPNzpOsXantg5+Y/6zyKk64dg2Y2jYLtFTl86ajNv5JG+q9PW9/xGO0l7 w1kbX9rrip5O0IHC9hUoH2tjuqoSQ3ATp6L7u8g1xIQ7iemcWOG2JAzt3ac+lH0wH1v9 ADIQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GcD9bK9r; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-114607-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-114607-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id u4-20020a50a404000000b00565b4b94462si2308621edb.455.2024.03.24.23.21.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 23:21:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-114607-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GcD9bK9r; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-114607-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-114607-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E3C371F2B3D4 for ; Mon, 25 Mar 2024 06:20:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E675C187E70; Sun, 24 Mar 2024 23:55:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GcD9bK9r" 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 CA21D15E1FB; Sun, 24 Mar 2024 23:17:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322264; cv=none; b=GSxbU3SlyO3QO38G8NL4rzmSp+3Y7pGkoCmTBV1vX6UN132eNHKq0mtRhf9TyzCl9808Z+9jX7NheNaeel/Rs3Mz4+/FfomKVwbD/aWUNavoX+iM7LdR2DVRVl5mpHwoMh4L+4huy/UsefopdK6UTWihV7Sw/QMsDqBgdpcEEwo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322264; c=relaxed/simple; bh=yndw88zz5hL38S4CIm5fOYrwYgVggB3WZzDKWBTNnLg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Kat9OjdJM/vAkZWK1OBb7fxWO1aupSj8j9cq1QdILqei+bdLt8TCErCnMfodfzcBkVPg3zyU0BOjEIHRfRbdX3P9RAmuJVOD8RYINZvOUkj7NfkKPqltz8yejuwuOWameJDnBXm+ymwNMc1EV7pqcUdaqDOFr+0NZEDpWcfNLqY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GcD9bK9r; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0089C43394; Sun, 24 Mar 2024 23:17:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711322263; bh=yndw88zz5hL38S4CIm5fOYrwYgVggB3WZzDKWBTNnLg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GcD9bK9rGPyuFHvkjrosAENuIPzBccuKOqp4epQslzXYhCBs/gcZscBuzP86dk5Ws NbXB6I8Jv8nxPKzEo4dpYEpdIIvE80X1DY7IhuTqnn5Kz6HeCEQ81xoiKwsUBllTMy 1c4JvDUmb2G9hY8F5aCZy/nS7yCYQjnZ3MHY9uCSLjiJnVhAEqDpi+tQUUWUgA67mF SmOz2TILZKLjW+IXz2G0bt8WQOSse00jFSv5S1gSFtU/idncZzI7XlTRK+/lq7702j MnmO89cezOeCUwe0e/V0XUgVE3ekGXggvPLNk2P+PFYOIQqsq9gbNrJ0oalVSynMq9 v6iLDRJg0Bjjg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Duoming Zhou , Michal Simek , Stephen Boyd , Sasha Levin Subject: [PATCH 6.1 346/451] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Date: Sun, 24 Mar 2024 19:10:22 -0400 Message-ID: <20240324231207.1351418-347-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324231207.1351418-1-sashal@kernel.org> References: <20240324231207.1351418-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Duoming Zhou [ Upstream commit 7938e9ce39d6779d2f85d822cc930f73420e54a6 ] The kmalloc() in zynq_clk_setup() will return null if the physical memory has run out. As a result, if we use snprintf() to write data to the null address, the null pointer dereference bug will happen. This patch uses a stack variable to replace the kmalloc(). Fixes: 0ee52b157b8e ("clk: zynq: Add clock controller driver") Suggested-by: Michal Simek Suggested-by: Stephen Boyd Signed-off-by: Duoming Zhou Link: https://lore.kernel.org/r/20240301084437.16084-1-duoming@zju.edu.cn Acked-by: Michal Simek Signed-off-by: Stephen Boyd Signed-off-by: Sasha Levin --- drivers/clk/zynq/clkc.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/clk/zynq/clkc.c b/drivers/clk/zynq/clkc.c index 7bdeaff2bfd68..c28d3dacf0fb2 100644 --- a/drivers/clk/zynq/clkc.c +++ b/drivers/clk/zynq/clkc.c @@ -42,6 +42,7 @@ static void __iomem *zynq_clkc_base; #define SLCR_SWDT_CLK_SEL (zynq_clkc_base + 0x204) #define NUM_MIO_PINS 54 +#define CLK_NAME_LEN 16 #define DBG_CLK_CTRL_CLKACT_TRC BIT(0) #define DBG_CLK_CTRL_CPU_1XCLKACT BIT(1) @@ -215,7 +216,7 @@ static void __init zynq_clk_setup(struct device_node *np) int i; u32 tmp; int ret; - char *clk_name; + char clk_name[CLK_NAME_LEN]; unsigned int fclk_enable = 0; const char *clk_output_name[clk_max]; const char *cpu_parents[4]; @@ -426,12 +427,10 @@ static void __init zynq_clk_setup(struct device_node *np) "gem1_emio_mux", CLK_SET_RATE_PARENT, SLCR_GEM1_CLK_CTRL, 0, 0, &gem1clk_lock); - tmp = strlen("mio_clk_00x"); - clk_name = kmalloc(tmp, GFP_KERNEL); for (i = 0; i < NUM_MIO_PINS; i++) { int idx; - snprintf(clk_name, tmp, "mio_clk_%2.2d", i); + snprintf(clk_name, CLK_NAME_LEN, "mio_clk_%2.2d", i); idx = of_property_match_string(np, "clock-names", clk_name); if (idx >= 0) can_mio_mux_parents[i] = of_clk_get_parent_name(np, @@ -439,7 +438,6 @@ static void __init zynq_clk_setup(struct device_node *np) else can_mio_mux_parents[i] = dummy_nm; } - kfree(clk_name); clk_register_mux(NULL, "can_mux", periph_parents, 4, CLK_SET_RATE_NO_REPARENT, SLCR_CAN_CLK_CTRL, 4, 2, 0, &canclk_lock); -- 2.43.0