public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Serge Semin <Sergey.Semin@baikalelectronics.ru>,
	Hannes Reinecke <hare@suse.de>,
	Damien Le Moal <damien.lemoal@opensource.wdc.com>,
	Sasha Levin <sashal@kernel.org>,
	hdegoede@redhat.com, axboe@kernel.dk, linux-ide@vger.kernel.org
Subject: [PATCH AUTOSEL 4.19 06/19] ata: libahci_platform: Sanity check the DT child nodes number
Date: Wed, 12 Oct 2022 20:26:05 -0400	[thread overview]
Message-ID: <20221013002623.1895576-6-sashal@kernel.org> (raw)
In-Reply-To: <20221013002623.1895576-1-sashal@kernel.org>

From: Serge Semin <Sergey.Semin@baikalelectronics.ru>

[ 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 <Sergey.Semin@baikalelectronics.ru>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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


  parent reply	other threads:[~2022-10-13  0:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13  0:26 [PATCH AUTOSEL 4.19 01/19] media: cx88: Fix a null-ptr-deref bug in buffer_prepare() Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 02/19] scsi: 3w-9xxx: Avoid disabling device if failing to enable it Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 03/19] nbd: Fix hung when signal interrupts nbd_start_device_ioctl() Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 04/19] power: supply: adp5061: fix out-of-bounds read in adp5061_get_chg_type() Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 05/19] staging: vt6655: fix potential memory leak Sasha Levin
2022-10-13  0:26 ` Sasha Levin [this message]
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 07/19] hid: topre: Add driver fixing report descriptor Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 08/19] HID: roccat: Fix use-after-free in roccat_read() Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 09/19] HSI: ssi_protocol: fix potential resource leak in ssip_pn_open() Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 10/19] md/raid5: Wait for MD_SB_CHANGE_PENDING in raid5d Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 11/19] usb: host: xhci: Fix potential memory leak in xhci_alloc_stream_info() Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 12/19] usb: musb: Fix musb_gadget.c rxstate overflow bug Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 13/19] Revert "usb: storage: Add quirk for Samsung Fit flash" Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 14/19] staging: rtl8192u: Fix return type of ieee80211_xmit Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 15/19] staging: octeon: Fix return type of cvm_oct_xmit and cvm_oct_xmit_pow Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 16/19] nvme: copy firmware_rev on each init Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 17/19] usb: idmouse: fix an uninit-value in idmouse_open Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 18/19] fsi: master-ast-cf: Fix missing of_node_put in fsi_master_acf_probe Sasha Levin
2022-10-13  0:26 ` [PATCH AUTOSEL 4.19 19/19] clk: bcm2835: Make peripheral PLLC critical Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221013002623.1895576-6-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=Sergey.Semin@baikalelectronics.ru \
    --cc=axboe@kernel.dk \
    --cc=damien.lemoal@opensource.wdc.com \
    --cc=hare@suse.de \
    --cc=hdegoede@redhat.com \
    --cc=linux-ide@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox