Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp742052lqh; Sat, 4 May 2024 23:17:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWJPyIuWNeAatS6UOxs+HKTFAZ+4d+NljZpxMUYrIE9bAqWMnhcdWGs9Vdl757D+7r2AY7CXPH4q9yropuJLYMKgnHD6qR71kVJx6IDLA== X-Google-Smtp-Source: AGHT+IF4EYDSdrtAw7xwz+JPM5omXjajotLf+kQgWE4jwhWaJ0yPIazlgvFLirSuMMG7ojWeW+Jr X-Received: by 2002:a17:907:6d10:b0:a59:bd5b:a0f3 with SMTP id sa16-20020a1709076d1000b00a59bd5ba0f3mr940749ejc.38.1714889873591; Sat, 04 May 2024 23:17:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714889873; cv=pass; d=google.com; s=arc-20160816; b=LxlY60ubl5Oo3MbHmowKZeILNPk5J0AqSLPYSl+NejkJhXh6EnB6ZooKBUFD74Yp7e 6g04w97pRHeSN0cXwgyrsIpAtu+QoL/f3D2W23HGAp5API4MTCHDwowrQJpua0iGQY0C 55DoYxSFDRq33VIC/ROeskN74II4qQ1TO8uI8xKcBz/tbRvmkqsi8d6YTN4tyrg9oxU3 HFmsPz2HRfyXhi+ziYNxYm614hO5PfgzKq60iOasTugWL6m3fwCULP0HAFR4dFW1qP/o qv0pQyp7iQByhyw5Q1RFrGGMgf1irBVqwnnjejj98VZTaGBpGsoF3nw7cXNVSfF497sI DiGA== 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=l1wg2cMRfB+aqQwo9LCdnh4EN2rtpVp3ErpWHzpP/iw=; fh=5ebfrwMyBQKXM1++aZVmc1YWfsGBoS+7StzC+3iaei8=; b=SSVWTjsBHFSE34uNfUC75JX+pzNPJmnI71R5hKbWZp1F/qs/LQRxcnueSiit5kgnhF 2sdwTwhfYOEH4t8ZUAPimYpqYmOcr89o/MY3qPwJf1duohFr8QI91tzGcustCNcNSy6Y N/9QxJ6MyduR12JALcmRWYiCXPugzImYPizYnzHkCWqb7y8LZZBMlxQBu9laMqL8Idmj jjc1yw4yT6uXlwbmmOGARe6Sfp2PDhpp2j/cK+NeBDsPNonGjW56mgJKjQM8k3AcE7A5 HeMtkXfO1AOc/al0+FSiWuxsC3nrljJqjdEVAnFd+MFNHth36I1tuVWxmM80WlwQtI+w ouVg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@t-argos.ru header.s=mail header.b=XjPssvqJ; arc=pass (i=1 spf=pass spfdomain=t-argos.ru dkim=pass dkdomain=t-argos.ru dmarc=pass fromdomain=t-argos.ru); spf=pass (google.com: domain of linux-kernel+bounces-168898-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168898-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=t-argos.ru Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id k8-20020a17090627c800b00a599b5d4d6asi2287773ejc.199.2024.05.04.23.17.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 May 2024 23:17:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168898-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@t-argos.ru header.s=mail header.b=XjPssvqJ; arc=pass (i=1 spf=pass spfdomain=t-argos.ru dkim=pass dkdomain=t-argos.ru dmarc=pass fromdomain=t-argos.ru); spf=pass (google.com: domain of linux-kernel+bounces-168898-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168898-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=t-argos.ru 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 188011F21761 for ; Sun, 5 May 2024 06:17:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B90FB4430; Sun, 5 May 2024 06:17:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=t-argos.ru header.i=@t-argos.ru header.b="XjPssvqJ" Received: from mx1.t-argos.ru (mx1.t-argos.ru [109.73.34.58]) (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 3ABF3184D; Sun, 5 May 2024 06:17:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.73.34.58 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714889864; cv=none; b=LzgpEk/rOkN8x7uttvEC0tDAaAZgy1Qb9jArEGLPVYNfiHXs/0FJiEjq2YufOc6+ymal6HrDeMmLz2c6Go6WXeB6vSTOXR08FUGvEG5gh2ELyztqvZEB8gTBcPSObCXRiWwV0Nv7YvtpP+CWqXU7dYZ363YJygNQe8G65ZmI5OQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714889864; c=relaxed/simple; bh=ZGeTVx2PorDAfHQ1Dxaam8AZyiVNKc4xHmndMzQM6dw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hLd4bmoGQ/d3an2xHLCzOTjb4mxBQ25khOhcLXpfRR7CeSzgM6nrLuPZzTHchiRuzEIN2w6i2yniG9cm/D1HfqzArfxpaIX94iRNCt6Y2qkz0tuRPSHsP/fa2P2PDbT4wxOYowtJu7CAqY1seP84iMVAh2trRVzOe5dJ2tp+paA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=t-argos.ru; spf=pass smtp.mailfrom=t-argos.ru; dkim=pass (2048-bit key) header.d=t-argos.ru header.i=@t-argos.ru header.b=XjPssvqJ; arc=none smtp.client-ip=109.73.34.58 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=t-argos.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=t-argos.ru Received: from mx1.t-argos.ru (localhost [127.0.0.1]) by mx1.t-argos.ru (Postfix) with ESMTP id B9688100002; Sun, 5 May 2024 09:17:12 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=t-argos.ru; s=mail; t=1714889832; bh=l1wg2cMRfB+aqQwo9LCdnh4EN2rtpVp3ErpWHzpP/iw=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=XjPssvqJK3zYkRouEOZ0tKvpJYQsUdXiSrwZKNS/vLodBCe0ks3voUF6xvfaW0h60 IijaWoHgthylFx2f/YvFq7b2XVG7xdhKM8vChcoeBix/wHkod2MTNkeBYk92PRQnlc 6wwJ8e4qItxNjW1I+JAinvhksQKtN0GoE/rVfimgixKNIyBY0PJjm8VvQDC8xM9T3U tXJalhVsD5JPnrDawymeWfXLxYl8YjQq7BKkag4UL5b16YQvqq8sutHJxVPwYWodv8 Z/W3C3SgKFeQbyXpA/4WJuvj7Uc3mmd5TqEFu5kOln4HGx5JKKpazv51W6V+uKMMaa YGEsUUw21ybxg== Received: from mx1.t-argos.ru.ru (ta-mail-02.ta.t-argos.ru [172.17.13.212]) by mx1.t-argos.ru (Postfix) with ESMTP; Sun, 5 May 2024 09:15:44 +0300 (MSK) Received: from localhost.localdomain (172.17.215.6) by ta-mail-02 (172.17.13.212) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Sun, 5 May 2024 09:15:24 +0300 From: Aleksandr Mishin To: Rob Herring CC: Aleksandr Mishin , Lorenzo Pieralisi , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Bjorn Helgaas , Manivannan Sadhasivam , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Serge Semin , Niklas Cassel , Yoshihiro Shimoda , Damien Le Moal , Siddharth Vadapalli , , , , Bjorn Helgaas Subject: [PATCH v4] PCI: dwc: keystone: Fix NULL pointer dereference in case of DT error in ks_pcie_setup_rc_app_regs() Date: Sun, 5 May 2024 09:15:17 +0300 Message-ID: <20240505061517.11527-1-amishin@t-argos.ru> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240329051947.28900-1-amishin@t-argos.ru> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: ta-mail-02.ta.t-argos.ru (172.17.13.212) To ta-mail-02 (172.17.13.212) X-KSMG-Rule-ID: 1 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 185057 [May 04 2024] X-KSMG-AntiSpam-Version: 6.1.0.4 X-KSMG-AntiSpam-Envelope-From: amishin@t-argos.ru X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 19 0.3.19 07c7fa124d1a1dc9662cdc5aace418c06ae99d2b, {Tracking_from_domain_doesnt_match_to}, 127.0.0.199:7.1.2;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;t-argos.ru:7.1.1;mx1.t-argos.ru.ru:7.1.1, FromAlignment: s X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean, bases: 2024/05/05 05:27:00 X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2024/05/05 03:18:00 #25094255 X-KSMG-AntiVirus-Status: Clean, skipped If IORESOURCE_MEM is not provided in Device Tree due to any error, resource_list_first_type() will return NULL and pci_parse_request_of_pci_ranges() will just emit a warning. This will cause a NULL pointer dereference. Fix this bug by adding NULL return check. Found by Linux Verification Center (linuxtesting.org) with SVACE. Fixes: 0f71c60ffd26 ("PCI: dwc: Remove storing of PCI resources") Suggested-by: Bjorn Helgaas Suggested-by: Manivannan Sadhasivam Signed-off-by: Aleksandr Mishin Reviewed-by: Manivannan Sadhasivam --- v1->v2: Add return code processing as suggested by Bjorn v2->v3: Return -ENODEV instead of -EINVAL as suggested by Manivannan v3->v4: Change variable name as suggested by Manivannan, add "Reviewed-by: Manivannan Sadhasivam " drivers/pci/controller/dwc/pci-keystone.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/drivers/pci/controller/dwc/pci-keystone.c b/drivers/pci/controller/dwc/pci-keystone.c index 844de4418724..67415c1a93ff 100644 --- a/drivers/pci/controller/dwc/pci-keystone.c +++ b/drivers/pci/controller/dwc/pci-keystone.c @@ -382,17 +382,22 @@ static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie) } while (val & DBI_CS2); } -static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie) +static int ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie) { u32 val; u32 num_viewport = ks_pcie->num_viewport; struct dw_pcie *pci = ks_pcie->pci; struct dw_pcie_rp *pp = &pci->pp; - u64 start, end; + struct resource_entry *entry; struct resource *mem; + u64 start, end; int i; - mem = resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM)->res; + entry = resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM); + if (!entry) + return -ENODEV; + + mem = entry->res; start = mem->start; end = mem->end; @@ -403,7 +408,7 @@ static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie) ks_pcie_clear_dbi_mode(ks_pcie); if (ks_pcie->is_am6) - return; + return 0; val = ilog2(OB_WIN_SIZE); ks_pcie_app_writel(ks_pcie, OB_SIZE, val); @@ -420,6 +425,8 @@ static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie) val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); val |= OB_XLAT_EN_VAL; ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); + + return 0; } static void __iomem *ks_pcie_other_map_bus(struct pci_bus *bus, @@ -814,7 +821,10 @@ static int __init ks_pcie_host_init(struct dw_pcie_rp *pp) return ret; ks_pcie_stop_link(pci); - ks_pcie_setup_rc_app_regs(ks_pcie); + ret = ks_pcie_setup_rc_app_regs(ks_pcie); + if (ret) + return ret; + writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8), pci->dbi_base + PCI_IO_BASE); -- 2.30.2