Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3926848rwi; Sat, 22 Oct 2022 02:21:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5kZHfFGKzW9/SmWiz614P/jAncV6+KMM8vMFnRHUoJlmx1k+uCIBhlIxqkWJQWA4CF6o4e X-Received: by 2002:a17:90a:5e03:b0:210:d4b3:4c98 with SMTP id w3-20020a17090a5e0300b00210d4b34c98mr18011603pjf.63.1666430470594; Sat, 22 Oct 2022 02:21:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666430470; cv=none; d=google.com; s=arc-20160816; b=b3kgiYIdX8viN0/vAG+t6JUzO00PVs/30P2qePpclOe5FZUYRkvpduaSPF6V1WZvkw lrR93KR/TX/BiF2rtDfgrG9k0lfNqjz1OoyEDedEdgparZHKyie0Vfuywohm+JLaDcp5 sPunIQgktIr9SRQuiHbF69eefsZD5kvbINmMv2KxS4/9zpnouJog5/K/+A0ui0dougSg Xhs+DHnlEhEsSfzFPtT65exjj397it3MKS79tBHuiLLF59QxJpeLRe8SxWm80HbS/ytF E+kl8iCxmI3Weam+Qr/og6bkQ6kd4UwuPRE1YNfKRFLK6K/0J7vJ4InckVDZQBQiPm2W yJlw== 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=lTFY5OeVKumRcKrxOMgwJQOcmNa8efBx2Q4pZMeHoiU=; b=kdjCTNLd28N8aEkTMp6H7guWOAt3OAhhurpJ7QvEnRuYHt0HXqmJx8dHGIDPFdEfli Sz7GR8cXGgmAvCFgF1mBRbKp0cNbger/3goPRBbh4vKulOGA59jxCb0uw0YhlNQb5cHp 5t8+Keh3k2E1G4DQHl5becPuw2c9whP8Ux2/7xIGIYPdGCe6RhN64goSBQY2iwV2WdxN vREPxfLxdpWFGEDjjiYnOjn0IppRzAakjojTzuWcc5VOOfeUvpFIlzsnCuWTe0CytC1Y 35ml4GGH6lGZob/I4xjr6/+JdSX6bBvPNPlk4qxbSLVleZ3tfMnOL4Ro7kZSZvFINFLk ljFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vfWDLaUm; 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 j191-20020a6380c8000000b0046ec396db31si3762218pgd.857.2022.10.22.02.20.36; Sat, 22 Oct 2022 02:21:10 -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=vfWDLaUm; 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 S234675AbiJVImR (ORCPT + 99 others); Sat, 22 Oct 2022 04:42:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234632AbiJVIjX (ORCPT ); Sat, 22 Oct 2022 04:39:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E5EF2505F0; Sat, 22 Oct 2022 01:05:41 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 0CAD160ADC; Sat, 22 Oct 2022 08:05:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06B19C433D6; Sat, 22 Oct 2022 08:05:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666425940; bh=YBukERdAxHkU57EcJR6ZhFZ2hlJha5Z6ys8hKW3Glgs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vfWDLaUmwChhDB3qlJCUO2D1Szh8FrnKcMzfN0i4cSInzQe2z5docbKt+ZoytklDH wftEZuDVVDgM+rVtlIexiyRbicpoNbKGgJW4U1iKsRkrZtfKG0nsYrYCSLBQGvYHgB rixL7VphuL30U9HjiBUnY0Rqf5ZVnW6gSeCZRhoM= 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.19 666/717] ata: libahci_platform: Sanity check the DT child nodes number Date: Sat, 22 Oct 2022 09:29:05 +0200 Message-Id: <20221022072527.857706859@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@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.3 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