Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp697770iob; Fri, 13 May 2022 10:25:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDQKa3H/lATB7VJDAzvCqL9svtpHAZTivRct9SWZkBsGWH5XhBuM83mCmr8W+w3RTYpdhW X-Received: by 2002:a17:907:9487:b0:6f8:747a:845e with SMTP id dm7-20020a170907948700b006f8747a845emr5242605ejc.299.1652462705525; Fri, 13 May 2022 10:25:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652462705; cv=none; d=google.com; s=arc-20160816; b=NNEk/h8oEfOL8wux/co0AQ0RmPh608UTeS7vdZEjrCPGcd0/FnRptpqSgj2+Jfagg8 DTD4+4be8hqTATaBJiizJORO4FVwAHF8Y7z35Ou+QZOMG5rDybfYIplFMDhwxuByOlfA IbIf95TevnmmurnIPB8A+wtk3pCSq+/raTHHAFMtnbNiVpDvw2cyalHONkF19BO7USWg BSTn7/8AgchmzkUA//Jy64VNMyqJWdcof18/Qhl4ERhH7U5HQZcKJ091bVVgk4uERL/n etwyVrCA2MNNQ/g1vKYwwRxDGnPW7R1ZmOZJjcOp1cxUSSM3rsJMOk115YsnU0AQCOAi 4a7A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=99+QgVY0zCV7jNTyccztfB8TN/d7Y8hNJt1G0Gv/SeE=; b=qNl6yT+Xg/iJd9EPn0qqVLBjpMJyP722aXUCHfC+DVFRjPsoIbweWJ8bfS46i6vv9+ meDgbi/7RTD1WXvjdA4UtvPPfpTOTBkPrAAeENfH2kUPLTEuTnHS2Wzfmvr84O/MF4wl 2nk1Qc+dB1wTU9TJBbSElDGQaq2lP1XQ0hLkWX8P68Qta4VOwHqKlcY6RBvs32il2d4e GUThnWVD0pZTFlPo4nfZgCT3j861JjjFHlMNzHDJrPEP9mRqLcQGe+Va9qbmpYYgHBW5 +RXRZ8njmyCGWXpIWqnyohlJetH/GwgPvJepG26roiZKy9X+hktGzCQ48FBBLBvOjFDs rM6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=hnRUhMha; 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 s9-20020a508d09000000b0041d7a31925csi2330322eds.140.2022.05.13.10.24.39; Fri, 13 May 2022 10:25:05 -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; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=hnRUhMha; 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 S232145AbiEKXTZ (ORCPT + 99 others); Wed, 11 May 2022 19:19:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349113AbiEKXSe (ORCPT ); Wed, 11 May 2022 19:18:34 -0400 Received: from mail.baikalelectronics.ru (mail.baikalelectronics.com [87.245.175.226]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 76115188E7E; Wed, 11 May 2022 16:18:28 -0700 (PDT) Received: from mail.baikalelectronics.ru (unknown [192.168.51.25]) by mail.baikalelectronics.ru (Postfix) with ESMTP id 03E8FBBD; Thu, 12 May 2022 02:19:12 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.baikalelectronics.ru 03E8FBBD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baikalelectronics.ru; s=mail; t=1652311152; bh=99+QgVY0zCV7jNTyccztfB8TN/d7Y8hNJt1G0Gv/SeE=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=hnRUhMhapp+RfjwOzaKMoAqrq8cTTBsgRT2qVhJ4d2KSpfhHputT8LKujp5G/+qfr yzyaE7RRl0cXdkd01we2Qt3laRPr0rdONZWdraMlus5Lo53Ny0INMKrJXKMF4ch2GG Jr4kA03XBH3aIdl0PX2eznKBi48a5jD0ZHsX7iB4= Received: from localhost (192.168.53.207) by mail (192.168.51.25) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 12 May 2022 02:18:24 +0300 From: Serge Semin To: Damien Le Moal , Hans de Goede , Jens Axboe CC: Serge Semin , Serge Semin , Alexey Malahov , Pavel Parkhomenko , Rob Herring , , , Subject: [PATCH v3 09/23] ata: libahci_platform: Sanity check the DT child nodes number Date: Thu, 12 May 2022 02:17:56 +0300 Message-ID: <20220511231810.4928-10-Sergey.Semin@baikalelectronics.ru> In-Reply-To: <20220511231810.4928-1-Sergey.Semin@baikalelectronics.ru> References: <20220511231810.4928-1-Sergey.Semin@baikalelectronics.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MAIL.baikal.int (192.168.51.25) To mail (192.168.51.25) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 Having greater than (AHCI_MAX_PORTS = 32) ports detected isn't that critical from the further AHCI-platform initialization point of view since exceeding the ports upper limit will cause allocating more resources than will be used afterwards. But detecting too many child DT-nodes doesn't seem right since it's very unlikely to have it on an ordinary platform. In accordance with the AHCI specification there can't be more than 32 ports implemented at least due to having the CAP.NP field of 4 bits wide and the PI register of dword size. Thus if such situation is found the DTB must have been corrupted and the data read from it shouldn't be reliable. Let's consider that as an erroneous situation and halt further resources allocation. Note it's logically more correct to have the nports set only after the initialization value is checked for being sane. So while at it let's make sure nports is assigned with a correct value. Signed-off-by: Serge Semin --- Changelog v2: - Drop the else word from the child_nodes value checking if-else-if statement (@Damien) and convert the after-else part into the ternary operator-based statement. --- drivers/ata/libahci_platform.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c index 7ff6626fd569..4e54e19f07b2 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -480,15 +480,22 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev, } } - hpriv->nports = child_nodes = of_get_child_count(dev->of_node); + /* + * Too many sub-nodes most likely means having something wrong with + * the firmware. + */ + child_nodes = of_get_child_count(dev->of_node); + if (child_nodes > AHCI_MAX_PORTS) { + rc = -EINVAL; + goto err_out; + } /* * If no sub-node was found, we still need to set nports to * one in order to be able to use the * ahci_platform_[en|dis]able_[phys|regulators] functions. */ - if (!child_nodes) - hpriv->nports = 1; + hpriv->nports = child_nodes ?: 1; hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL); if (!hpriv->phys) { -- 2.35.1