From patchwork Fri Mar 1 08:44:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Duoming Zhou X-Patchwork-Id: 208698 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp942302dyb; Fri, 1 Mar 2024 00:45:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUccDezDPHDTHsGNwxboatsxSlhFadEwKuIp3q4/rv/YPIJk0hlcm8ugSGN6Kru4sOrabeQnpud9zRNBEej+8uVUeF1/Q== X-Google-Smtp-Source: AGHT+IE0fNvShZNiMqBWKDGQ/VITOJsSY0rz7jMlVM61QgpD1sfB62avM6i4ium4VXvhQDh9eBpW X-Received: by 2002:a05:6808:13d3:b0:3c1:7c55:373f with SMTP id d19-20020a05680813d300b003c17c55373fmr985827oiw.6.1709282743390; Fri, 01 Mar 2024 00:45:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709282743; cv=pass; d=google.com; s=arc-20160816; b=DLPH6ha+V/ehf4PLwMUE95eIb5ppIBXwy5iet0WyvMTzSlr9To8+PudEd3ns6cCzTF 5YTrnLm/LYM8G8ub6tvQ36lkSnwfEUoBLGxNWh+5jxaEPNNcaAuRZhc/rE/PcEn/Uu6a ciSwvqxObZwkcbVfpXi0xXe0mFREWmMHLTq3QXb+DZNnPB61HUS9EzhFPIsXR7b5wQZt r146qJqqooZCDorS93y3LxqsYKr00F8ul77Ky4QQrMYl2snYGuypv5k1VwZVdbB5Uuk9 zosc4H6KjaSyjdvjxcvVtnuStE1Jv/GA4hZyV60WV9U83Xz080as7kkVCmVJdL2Pbd3m NapQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:message-id:date :subject:cc:to:from; bh=VxBtDwLZqO70QAYfAwyVJ5O1oE1KcVyaetORDj0Zzqk=; fh=y7nX3fXfORRLXSEYhmPLxEaDuEjokiNPxvbdHQKimHQ=; b=03XA6ZjtVz8vpxVcyiGPciWxecBuNooXO8fui0/26WswasDB7JI0nPCxUiNZUfUUFL j26G3VGs8BE7mqbtqoY/3kUpoyN0moNrR0mkDdRENZ97mW0NwPlDlcbsj1nNTgLuUMj6 xq7n8W9xcVNxP630EQogped4xHmOEjQfO8r+EK8HW5JXNNp1D32iM/9pw98J+Aiate90 3UnivaDUtUBLPhIfGwJ4F85I0BzqOfdrTehNccbey3K7o5ZPWB5B8RxAGXu22x4BsmP0 ywdllA5O5H/XjueUblKK9NENY5uLpIVDc9wUQiwDYYrJLslDHPKkoaMFZmivD7cw5oNO d/BA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-88101-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88101-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c18-20020a634e12000000b005c65c11ad09si3099017pgb.768.2024.03.01.00.45.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 00:45:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88101-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-88101-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88101-ouuuleilei=gmail.com@vger.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 331912862AB for ; Fri, 1 Mar 2024 08:45:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC7E56A34F; Fri, 1 Mar 2024 08:45:00 +0000 (UTC) Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [52.237.72.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C7E6669E04; Fri, 1 Mar 2024 08:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.237.72.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709282699; cv=none; b=nz8R+vaPPIcNNuHU4xuHwBEeGJ6a1zB6rAAhgwrKglYL9DukrI3O/uETtHRtgVGveR7Ea82hXt1WtSRHrIEhr8yL85K8vYK1oWqemk02EM+mqdXzLbP0jb5UZ1t8jrgrCzIgqG/Y7yZFI9Hj0THiBnN5yVfW5XV48eSEBxo+Ghk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709282699; c=relaxed/simple; bh=MSl3qouA9Em17NHJkvcD9Pim1E7a+VZjVpD0WGutwrE=; h=From:To:Cc:Subject:Date:Message-Id; b=ncTeqBtzLQBnygjFngA7zMEJh6mgHntAz/fWRmgMJCTorVQ6P5yGL0vY9aiu1cmljSuatNaRqrwU+9HnOp4NdfVHtCjj4rjMTFftuLDAmmYf5Sptegm4ZQVzbarEw0EioG2Jm1DHjo122r1l40stJ7OYbdyU1s30beDsJ4Ef8sg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn; spf=pass smtp.mailfrom=zju.edu.cn; arc=none smtp.client-ip=52.237.72.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zju.edu.cn Received: from ubuntu.localdomain (unknown [106.117.76.127]) by mail-app2 (Coremail) with SMTP id by_KCgDnKqp2leFlV9txAg--.2261S2; Fri, 01 Mar 2024 16:44:46 +0800 (CST) From: Duoming Zhou To: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org, mturquette@baylibre.com, sboyd@kernel.org, michal.simek@amd.com, Duoming Zhou Subject: [PATCH v4] clk: zynq: Prevent null pointer dereference caused by kmalloc failure Date: Fri, 1 Mar 2024 16:44:37 +0800 Message-Id: <20240301084437.16084-1-duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: by_KCgDnKqp2leFlV9txAg--.2261S2 X-Coremail-Antispam: 1UD129KBjvJXoW7urW7Jr43Aw4ktF1ftF13Arb_yoW8uF13pF WxG3s0yF4jgF4qgan2kF18ZFyS9a12gay7Kw15tws2vFn8JayUJr45CF95ZF18XrW8uaya vF1q9r18u3WUAF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvq14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26rxl 6s0DM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s 0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xII jxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r12 6r1DMxkIecxEwVAFwVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8Jw C20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAF wI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjx v20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2 jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0x ZFpf9x0JUa38nUUUUU= X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAwIHAWXg0lML4wAZsy Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792306481984294850 X-GMAIL-MSGID: 1792312862179945425 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 Acked-by: Michal Simek --- 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 7bdeaff2bfd..c28d3dacf0f 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);