Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5562353pxb; Mon, 28 Mar 2022 14:15:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4WpTwBja4ton4+eJXNdiUaBg54vSnhmQuyELXI+DOdVi2/dAPnM4L77GVRQAT81HcP1kt X-Received: by 2002:a05:6808:11c4:b0:2d9:c395:f15e with SMTP id p4-20020a05680811c400b002d9c395f15emr589039oiv.47.1648502137031; Mon, 28 Mar 2022 14:15:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648502137; cv=none; d=google.com; s=arc-20160816; b=telLf/3fYyvwCUmh/GTOgPiWuIZpbdxSiTky3T294g19o34ztsxgO3HVsPVR7Xzcq7 tiF852fyBhDrm0nZl9FFq1U8TzWPZdh/Jwjbr9cS56+uvkDRZuyqMam2nZ0g3flZBjpA /M+Osca4bsacNcxRdMuCJG5ppt1KJdcwTfup658+96+hgf9QBp1E1sFr9Vl2ZPd2iCbm 3hiHoggTTBX4z5wjkqgHkMcwjLOlFbrBfx/AKvTrgS+bD+ouZ9EugCWVSNT1noiWHFVj bPzD1uxFAQBTpO2Yp9+dAJAZ6YZR0J5/r4WHjoI2umulZSwOfrNQlG5xDKVeXKZu6Pyx 2iNA== 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=fb2zYSGjaw0bsuQW3hhcnSRLLbYGnRID7wQq5QULof8=; b=dJQXO9U+plwB9E6LhC9KHcCSCQWT8bHAWF2bP9fiwYsvt4/h9wzjKcIu/yCHpZK2BU gStacBQJtXkBdpOO/ebGXafGsAjO0G5Xq8GxJZPi06pDb7uUwmy8scMfJskM9rKH1Ydz LK2aBHsCxOkw5BAk1IBUSYYULheym5rwIELiaOvcREeWyKV2IjXHrhmTFmKMEmBc7wSS 2froiAU5qY4rozVjvZsIHtG7e9+NCdztrX331LsByO/uc5lonyof4uibM1nrKmgnULAR UvRDg+VK4MLhRjzgVIV7MFHUePL89PAYibPGHHJRnT0sX4R1PwhqMakvWVmPtlmHhw6h 2jJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=eV7UHlVU; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id s9-20020a05680810c900b002ef0c34769esi15053734ois.286.2022.03.28.14.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 14:15:37 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=eV7UHlVU; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E937D76661; Mon, 28 Mar 2022 14:05:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239502AbiC1Psm (ORCPT + 99 others); Mon, 28 Mar 2022 11:48:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238960AbiC1PqJ (ORCPT ); Mon, 28 Mar 2022 11:46:09 -0400 Received: from mail.baikalelectronics.ru (mail.baikalelectronics.com [87.245.175.226]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D3514A93E; Mon, 28 Mar 2022 08:44:14 -0700 (PDT) Received: from mail.baikalelectronics.ru (unknown [192.168.51.25]) by mail.baikalelectronics.ru (Postfix) with ESMTP id 07BF81E28D2; Thu, 24 Mar 2022 03:16:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.baikalelectronics.ru 07BF81E28D2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baikalelectronics.ru; s=mail; t=1648080996; bh=fb2zYSGjaw0bsuQW3hhcnSRLLbYGnRID7wQq5QULof8=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=eV7UHlVUPdeaHjFuf5hb3HAuZNgmzoSFaOZBjfzdp1ZtsHgN7YCeLgLqCz6y354ho gs3bOz1eUF9mSJhPz4CTz7R1yZQg3QtcenBp/3uXhPzZHypbGAm0H/jfjgAj9HMygJ Ig2ksu1uYFbkTMBWmhIT9esMKzT9R3jOYrEaWYKc= Received: from localhost (192.168.168.10) by mail (192.168.51.25) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 24 Mar 2022 03:16:35 +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 07/21] ata: libahci_platform: Sanity check the DT child nodes number Date: Thu, 24 Mar 2022 03:16:14 +0300 Message-ID: <20220324001628.13028-8-Sergey.Semin@baikalelectronics.ru> In-Reply-To: <20220324001628.13028-1-Sergey.Semin@baikalelectronics.ru> References: <20220324001628.13028-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.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 --- drivers/ata/libahci_platform.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c index 4fb9629c03ab..845042295b97 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -470,15 +470,21 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev, } } - hpriv->nports = child_nodes = of_get_child_count(dev->of_node); - /* - * If no sub-node was found, we still need to set nports to - * one in order to be able to use the + * Too many sub-nodes most likely means having something wrong with + * firmware. 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) + child_nodes = of_get_child_count(dev->of_node); + if (child_nodes > AHCI_MAX_PORTS) { + rc = -EINVAL; + goto err_out; + } else if (!child_nodes) { hpriv->nports = 1; + } else { + hpriv->nports = child_nodes; + } hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL); if (!hpriv->phys) { -- 2.35.1