Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp630705rwp; Wed, 12 Jul 2023 20:49:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlHj3no9XfNq06kIqKW3j9t+UBbL0YYZxWMRqoXlbH/2q7Hp00mwwgDDUw+9bM59asKm/jrT X-Received: by 2002:a05:6402:3451:b0:51e:1af0:3a90 with SMTP id l17-20020a056402345100b0051e1af03a90mr545770edc.37.1689220147986; Wed, 12 Jul 2023 20:49:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689220147; cv=none; d=google.com; s=arc-20160816; b=ysGZIq9zEx3iA3crLZt3DIOkiITO10NnwyF3EGUUmgAekwgc5MTP9JXb5tOMmhfs2F c4d7tVRwgy1x00IgKn1AcC/68w2bh+mxSmR6dd68x0YHi4eixB9JT48jMcmvi2XXdKPn J+SXl4o8BxnMe7W+lUrKj+1KKd1R86J8DZJV5BjowM/RdkbyHvA5hfrs8EPMv3ZSx1yJ HTpoCm9olpAqLZpL/4DTW7G6lje7ePMZU2xiFWQeapLObJh1NXNdvM3UbG+f4KYJiXRS 5UivQNjk4OU4BriLtD13f5fcEUKTaPvdDqJ01vKFIRCEtwJjCPHqF7v9FrgbDFMAceCe zZzQ== 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 :message-id:date:subject:cc:to:from; bh=BhHU9eW7VlNl816m/HE4Te2lqieAMTMsnqc02zpP0nw=; fh=BDDFGzP6DtAXrcnaNDA87KqSZT/NeOSnb+Ik/sQKV/g=; b=hINUt7Zh6Ode9CCf7F+hd5rSBhcUWACaVPQlsqN2ERtsDWWtgb6Sr4X2pg1ZYGfsRO nql83btArxU29dlPHhVFnIF+dDOgdiSSGiJzcikdMzI1WRIGX+UJwVsV75/61e4DfAsy /GjNIs0qcNn/qPA/5qZ6PVvKV8POcDtDg92M1Q0ZbTjoRfDp0HVq68k26zVhTZeOv4Kv R6Jv8EP8UNxfFtGS8NW0gvRk5gbvgpTBv40/9Z+VwRK0CebkVAhCFO8JHQNM1HUJoM9G jISrFDEc5FpBh32BLopGUrMNa8uM4Kq98NIOfrWNhkTpC2J/MBfi7PFC4N0uW8jPLB8v E/6A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id by26-20020a170906a2da00b0099233cac125si6588811ejb.915.2023.07.12.20.48.44; Wed, 12 Jul 2023 20:49:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233527AbjGMDUD (ORCPT + 99 others); Wed, 12 Jul 2023 23:20:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233507AbjGMDT6 (ORCPT ); Wed, 12 Jul 2023 23:19:58 -0400 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E01271FDE; Wed, 12 Jul 2023 20:19:54 -0700 (PDT) Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 36D3JIv5061613; Thu, 13 Jul 2023 11:19:18 +0800 (+08) (envelope-from Chunyan.Zhang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4R1fvB4D1dz2M4K4c; Thu, 13 Jul 2023 11:18:14 +0800 (CST) Received: from ubt.spreadtrum.com (10.0.73.70) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Thu, 13 Jul 2023 11:19:16 +0800 From: Chunyan Zhang To: Greg Kroah-Hartman , Jiri Slaby CC: , Baolin Wang , Orson Zhai , Chunyan Zhang , Chunyan Zhang , LKML Subject: [PATCH V2 1/2] serial: sprd: Assign sprd_port after initialized to avoid wrong access Date: Thu, 13 Jul 2023 11:19:03 +0800 Message-ID: <20230713031904.12106-1-chunyan.zhang@unisoc.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.0.73.70] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 36D3JIv5061613 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The global pointer 'sprd_port' may not zero when sprd_probe returns failure, that is a risk for sprd_port to be accessed afterward, and may lead to unexpected errors. For example: There are two UART ports, UART1 is used for console and configured in kernel command line, i.e. "console="; The UART1 probe failed and the memory allocated to sprd_port[1] was released, but sprd_port[1] was not set to NULL; In UART2 probe, the same virtual address was allocated to sprd_port[2], and UART2 probe process finally will go into sprd_console_setup() to register UART1 as console since it is configured as preferred console (filled to console_cmdline[]), but the console parameters (sprd_port[1]) belong to UART2. So move the sprd_port[] assignment to where the port already initialized can avoid the above issue. Fixes: b7396a38fb28 ("tty/serial: Add Spreadtrum sc9836-uart driver support") Signed-off-by: Chunyan Zhang --- V2: - Leave sprd_remove() to keep the unrelated code logic the same. --- drivers/tty/serial/sprd_serial.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/drivers/tty/serial/sprd_serial.c b/drivers/tty/serial/sprd_serial.c index b58f51296ace..11de338a6122 100644 --- a/drivers/tty/serial/sprd_serial.c +++ b/drivers/tty/serial/sprd_serial.c @@ -1106,7 +1106,7 @@ static bool sprd_uart_is_console(struct uart_port *uport) static int sprd_clk_init(struct uart_port *uport) { struct clk *clk_uart, *clk_parent; - struct sprd_uart_port *u = sprd_port[uport->line]; + struct sprd_uart_port *u = container_of(uport, struct sprd_uart_port, port); clk_uart = devm_clk_get(uport->dev, "uart"); if (IS_ERR(clk_uart)) { @@ -1149,22 +1149,22 @@ static int sprd_probe(struct platform_device *pdev) { struct resource *res; struct uart_port *up; + struct sprd_uart_port *sport; int irq; int index; int ret; index = of_alias_get_id(pdev->dev.of_node, "serial"); - if (index < 0 || index >= ARRAY_SIZE(sprd_port)) { + if (index < 0 || index >= UART_NR_MAX) { dev_err(&pdev->dev, "got a wrong serial alias id %d\n", index); return -EINVAL; } - sprd_port[index] = devm_kzalloc(&pdev->dev, sizeof(*sprd_port[index]), - GFP_KERNEL); - if (!sprd_port[index]) + sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); + if (!sport) return -ENOMEM; - up = &sprd_port[index]->port; + up = &sport->port; up->dev = &pdev->dev; up->line = index; up->type = PORT_SPRD; @@ -1195,7 +1195,7 @@ static int sprd_probe(struct platform_device *pdev) * Allocate one dma buffer to prepare for receive transfer, in case * memory allocation failure at runtime. */ - ret = sprd_rx_alloc_buf(sprd_port[index]); + ret = sprd_rx_alloc_buf(sport); if (ret) return ret; @@ -1208,12 +1208,21 @@ static int sprd_probe(struct platform_device *pdev) } sprd_ports_num++; + sprd_port[index] = sport; + ret = uart_add_one_port(&sprd_uart_driver, up); if (ret) - sprd_remove(pdev); + goto clean_port; platform_set_drvdata(pdev, up); + return 0; + +clean_port: + sprd_port[index] = NULL; + sprd_ports_num--; + uart_unregister_driver(&sprd_uart_driver); + sprd_remove(pdev); return ret; } -- 2.41.0