Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp178071rwi; Wed, 12 Oct 2022 18:10:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Y+NSTyxteoNRh4Uaw2AqYWw8wKGd3WKb/IMgtCnhEGt9EL0ldvwCd5cCLxVIXvWQ1AJkY X-Received: by 2002:a05:6a00:16c4:b0:535:890:d4a with SMTP id l4-20020a056a0016c400b0053508900d4amr33702563pfc.0.1665623424830; Wed, 12 Oct 2022 18:10:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665623424; cv=none; d=google.com; s=arc-20160816; b=HUHQnXm2d0TGDAEP0M9Eg4DFd+ZTeay0sVYNEqdrZeKkyF84gEtPxsADd1QvVr5OLL v4ubfVbuC8Ez+0H0/hZ67Yv5NajZdwNmJ/XTD2FveYUn0Cs/B2RSO3IsMhFitYzmJF5L Om3zH5xY2gLQD0GRjfrIhIsARwVOsuXOdeJeKOVp0rgsiKMt8MIwOAVRxQfQW1WDXzWC ILshLaIgI6hodn9n2Gk+Klvf/D04Kb8fboCENnNVVkSclmpH2+zjlgw3krs+8Incjfz5 4SU5UflKj+ePrUcTMAQNwioVJ+2cEJw1g4WOfgWnpVTPKKY8Gsge9yqamiXKzGCxmDT6 nJ9w== 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; bh=lTFY5OeVKumRcKrxOMgwJQOcmNa8efBx2Q4pZMeHoiU=; b=TswsHSoEzBHRoVfClp5RwtPn3HPYXXFk6O/NQGfx+Y8dN6cmHFgmVHUML9smPCVdJO YD5TwRYF+6QbGM50RZgOd2oJeZ+Hu6SGTMwW8zRutw9UyzZuIQ11THklkEWqrda2mHS/ 6G1FS8tJNeVE7AUAOICeA/90nQqJsOJug+2ZeWo9tXOhYP23vJUVc2Yd28+0dOhHC3+5 Sqp4QTuvrGbabj6vPCHJIEcP1rSVIGRW2tkYzNI/K8QChOCyipGTN9qbQnLYRXDdiM9K fZOvttvz4gMfHx2YS/E4HhRvWKvmIbrMtVN9UDS2Zq1EvUA0bV0D6Cso1SSL5zkY6JLJ iOlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ydyvt0tc; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c17-20020a170902d49100b00176b28d9acdsi1395759plg.354.2022.10.12.18.10.12; Wed, 12 Oct 2022 18:10:24 -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=@kernel.org header.s=k20201202 header.b=Ydyvt0tc; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229986AbiJMAS0 (ORCPT + 99 others); Wed, 12 Oct 2022 20:18:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbiJMARl (ORCPT ); Wed, 12 Oct 2022 20:17:41 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3977142C96; Wed, 12 Oct 2022 17:16:58 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 84EECB81CC2; Thu, 13 Oct 2022 00:16:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63AB9C43470; Thu, 13 Oct 2022 00:16:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620212; bh=YBukERdAxHkU57EcJR6ZhFZ2hlJha5Z6ys8hKW3Glgs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ydyvt0tcfOsGWnBUpolOhmKedLrrR4AfRC1FpP0kPY5rlnB5TwPGhtHkRZg9lEJVD qw2vjdpYMAcDlnKMGMyovNwRvS//l/dpy0jfFXUUn65kqQKLVQiqzaVXmEmE9vF11O /m9Gao0G2VPdEavc9T1ZnlwZqGu+4FddwHq2p83WgrUo37B3gu3VTAMv/gB8yC8qiD BFkd1pfcr4bXRDuky+7ARrKzi0EFSog+wi4l7b21z8PsCPtasQXrll7xSsSTE1T7hz Eoe+ydAxo1gUm1ImYT6xmFFplvGz5/akljM1oatYmcgkNGkUYbPVKUvX+6QbprQnNq 8rUa7sklLRObA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Serge Semin , Hannes Reinecke , Damien Le Moal , Sasha Levin , hdegoede@redhat.com, axboe@kernel.dk, linux-ide@vger.kernel.org Subject: [PATCH AUTOSEL 6.0 24/67] ata: libahci_platform: Sanity check the DT child nodes number Date: Wed, 12 Oct 2022 20:15:05 -0400 Message-Id: <20221013001554.1892206-24-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013001554.1892206-1-sashal@kernel.org> References: <20221013001554.1892206-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 From: Serge Semin [ Upstream commit 3c132ea6508b34956e5ed88d04936983ec230601 ] 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 5 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 Reviewed-by: Hannes Reinecke Signed-off-by: Damien Le Moal Signed-off-by: Sasha Levin --- drivers/ata/libahci_platform.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c index 32495ae96567..986f1923a76d 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -451,14 +451,24 @@ 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) + if (child_nodes) + hpriv->nports = child_nodes; + else hpriv->nports = 1; hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL); -- 2.35.1