Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7197754rwi; Mon, 24 Oct 2022 11:05:18 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Nu4jJbhTjuMlVm13I6eejyHbxZInYeGSffLFXY26jkZ3Lz/gHHH3yyaLad2wCJX3nEcI4 X-Received: by 2002:a05:6a00:2382:b0:56b:a25d:76a3 with SMTP id f2-20020a056a00238200b0056ba25d76a3mr10900215pfc.8.1666634717734; Mon, 24 Oct 2022 11:05:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666634717; cv=none; d=google.com; s=arc-20160816; b=ty0y0DktKchqcc0xxYEGa6qkF70BwSLnyWCWNguF+gkg/tIE+ruw2dgfcPLmc3gkAB QE9XLwTX0mp5hi4kXG+XcSNL582rd5pbAFWnsWl1ybwHGwF3bZLhkj0/Aai5f/Ic9YE7 9bcBfO8Apw4/TB0EX7FbOzgHqydS/Go03nHQQjIdWl4jZjYwRYN+5emet0Z/KPpwumxF vYfV3ZjYJc+dkuX6yBhGuYKZB+kooJDDt15Jd8vL+2IcGd5NJ7hEKGJgcW3WDi1qdJxd JktKJ28DLaAvpajOY9UYrEIng0oQ4Qyhk28MZol4xqg6YZcxVAnzBV8uPK+X4R8tEXIa zoOw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9KBkjjH7Xhmv+crps0BG9KhyF+6+ZDXrsKPBAJdvD7k=; b=i32mkvedaR9Ufdb9b2P167r7lswxrHBU6WqJ7El1V2A6c2vmHP/U92NlygpsiDa++/ I5HY4i7UcdnAlUFlW+gidssVtFsCJz4g96buKSko+G0HFUQa4LkCJxqlEqVg0TdPOQJt Rr84cu8cID2n/RjqDQ1HfZkYN8nEQRWGweFzyCtxMMH3oY0fVWbKqBxK1pGWq36HRLE1 Ea6GnEKBE0kvtFTgHo7Qme74wMnaKsGiTE/s6JLkgv9Hf8hh4bJMcGYP+x0WN92/bYs+ 1/cdjcalW4opuJFLOFiGIUykYTC3/POrSRYc6cPQJ+ittaoBDhBXONzFwoEmoGBNhkcM +m9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rTii2JrY; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e69-20020a636948000000b0043c9fcccbd9si107784pgc.685.2022.10.24.11.05.04; Mon, 24 Oct 2022 11:05:17 -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=@linuxfoundation.org header.s=korg header.b=rTii2JrY; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235533AbiJXQym (ORCPT + 99 others); Mon, 24 Oct 2022 12:54:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235491AbiJXQtq (ORCPT ); Mon, 24 Oct 2022 12:49:46 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49C451F2E5; Mon, 24 Oct 2022 08:33:00 -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 09FB0B811C9; Mon, 24 Oct 2022 12:03:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65D9AC433C1; Mon, 24 Oct 2022 12:03:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613032; bh=8VBt0TV070VIaruyQ4lTpvVLgKOxHaLZWr95D3YRDZk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rTii2JrYBLYmz8cilJgRAIGpZ9Us514olfli1QJBlriIX0oNPO519bBT755iWNDcJ oLj3wks+TwCwmDCG3SuA09QFP6iVAsnoVRauxNfZJC29We8eSJmMpiK7Iq+rqP3rF8 tK+YTAwZorR7UE48bugPGSVH7rwEivGPO1QeEbbM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Serge Semin , Hannes Reinecke , Damien Le Moal , Sasha Levin Subject: [PATCH 4.19 212/229] ata: libahci_platform: Sanity check the DT child nodes number Date: Mon, 24 Oct 2022 13:32:11 +0200 Message-Id: <20221024113006.073177126@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112959.085534368@linuxfoundation.org> References: <20221024112959.085534368@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 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 6a55aac0c60f..63086f90bbf8 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -421,14 +421,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