From patchwork Mon Oct 24 11:32:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9716 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp545388wru; Mon, 24 Oct 2022 09:34:18 -0700 (PDT) X-Google-Smtp-Source: AMsMyM66yTz7pOExksFcuKWN+FuC/lmhIErzRuxTbezVUt523+QQQ+zUkROnbqAP9OmR39PHnRuR X-Received: by 2002:a17:906:8a4a:b0:78d:5ff6:7507 with SMTP id gx10-20020a1709068a4a00b0078d5ff67507mr28221236ejc.194.1666629258132; Mon, 24 Oct 2022 09:34:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666629258; cv=none; d=google.com; s=arc-20160816; b=z4UeV41EczIFdNU4SBWatQxnpwNrAIfTyMRjIzl0JkJM562R6Xf2/n/XfnwgqBTKXw Jme0gqAzKdqfce5RslYPTP9YnSgL6SxF96h15iu3yx/Z5ZPTC/4DmW0t0hFjwjpgxEMX jLPkZhlWuD4rIYGama5njSdn1Um13HUbwswopBHAv3MH56QNZbuTeSRI08xN3an96DQq tsVlieqOGOzEjQPCR+GSdADwvbjkkvE/MErCHVW8lX+ZGKYCvA58bSO4Vbgq5A2p08lI LcxxyI/V2+w1mMHo4b//Vhbbcjc4L6GWiLpa0Uwgs9SjRWqOY9VmpSQBM/xlNlzmShyE vcEQ== 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=rOh68a7GDNGzXdPBOxs5Et1CbHu0gBFH71n60XgorFE=; b=IRYVmUQjo6bkhLj7qJvlLn4O4iPArMerC2pQAFCyN0GxFR4AUPVMvfMZiuKH/bPNSm m5bJ+AA+EVejdkJtN26ClJ3tuzLNvqgQ6b4M1JfhZjG8v22O20VIKsX/S7/jkPMUCipx ClfP51DM6f5dtqxmXQzVWtySTL5qUoLP+I57sGUC0XjagnNiIMCk5yI5QTxxW5YTRvPd e/rVtcADglY4jWAdUHWsap33WFALSfQe9h0oWSh2D3Qx0/ylZFrKlrWaRp3/zCj9RNJy uRqVRlL3jbq7V+Tmh0ozMJw/bw7ObsaF+SiXeW9ZA5T6/ZjGBn+5hEykqnEgZN6W2OaC JNHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KWzzTtTe; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qb21-20020a1709077e9500b00779c6c57dbfsi190394ejc.556.2022.10.24.09.33.54; Mon, 24 Oct 2022 09:34:18 -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=KWzzTtTe; 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 S233383AbiJXQUU (ORCPT + 99 others); Mon, 24 Oct 2022 12:20:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233606AbiJXQR5 (ORCPT ); Mon, 24 Oct 2022 12:17:57 -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 704241409E; Mon, 24 Oct 2022 08:04:29 -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 2EF41B81617; Mon, 24 Oct 2022 12:14:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A30DC433D6; Mon, 24 Oct 2022 12:14:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613692; bh=DEYN2CX6xftlzs5WQJZllkRHZ5Y381wGi7pAeZQhF7k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KWzzTtTeMfmRXl2EDDBCvWvJqCyBS0o2QbqAxa7AeM0cOPmGvcizI38PtW5MImGZ1 L+Wvpk7eGodYGx4w47/ZQ+3O+hbyNBOImlf4Zc+XHhHMW7jsWC0LPeSrqG0i9aTTiL R9LJ5NmHJvKX4P8xg87UEzVjhCE8GaVj+xLSg3gA= 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 5.4 234/255] ata: libahci_platform: Sanity check the DT child nodes number Date: Mon, 24 Oct 2022 13:32:24 +0200 Message-Id: <20221024113010.972718764@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113002.471093005@linuxfoundation.org> References: <20221024113002.471093005@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747587441061661663?= X-GMAIL-MSGID: =?utf-8?q?1747587441061661663?= 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 8a963d2a951d..c0ac25b80a1f 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);