mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: "Philipp Stanner" <phasta@kernel.org>,
	"Bingbu Cao" <bingbu.cao@linux.intel.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Sasha Levin" <sashal@kernel.org>,
	linux-pci@vger.kernel.org
Subject: [PATCH AUTOSEL 6.14 35/44] PCI: Check BAR index for validity
Date: Thu,  3 Apr 2025 15:13:04 -0400	[thread overview]
Message-ID: <20250403191313.2679091-35-sashal@kernel.org> (raw)
In-Reply-To: <20250403191313.2679091-1-sashal@kernel.org>

From: Philipp Stanner <phasta@kernel.org>

[ Upstream commit b1a7f99967fc0c052db8e65b449c7b32b1e9177f ]

Many functions in PCI use accessor macros such as pci_resource_len(),
which take a BAR index. That index, however, is never checked for
validity, potentially resulting in undefined behavior by overflowing the
array pci_dev.resource in the macro pci_resource_n().

Since many users of those macros directly assign the accessed value to
an unsigned integer, the macros cannot be changed easily anymore to
return -EINVAL for invalid indexes. Consequently, the problem has to be
mitigated in higher layers.

Add pci_bar_index_valid(). Use it where appropriate.

Link: https://lore.kernel.org/r/20250312080634.13731-4-phasta@kernel.org
Closes: https://lore.kernel.org/all/adb53b1f-29e1-3d14-0e61-351fd2d3ff0d@linux.intel.com/
Reported-by: Bingbu Cao <bingbu.cao@linux.intel.com>
Signed-off-by: Philipp Stanner <phasta@kernel.org>
[kwilczynski: correct if-statement condition the pci_bar_index_is_valid()
helper function uses, tidy up code comments]
Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
[bhelgaas: fix typo]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/pci/devres.c | 16 ++++++++++++++--
 drivers/pci/iomap.c  | 29 +++++++++++++++++++++--------
 drivers/pci/pci.c    |  6 ++++++
 drivers/pci/pci.h    | 16 ++++++++++++++++
 4 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c
index 3431a7df3e0d9..d2c09589c537e 100644
--- a/drivers/pci/devres.c
+++ b/drivers/pci/devres.c
@@ -577,7 +577,7 @@ static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev,
 {
 	void __iomem **legacy_iomap_table;
 
-	if (bar >= PCI_STD_NUM_BARS)
+	if (!pci_bar_index_is_valid(bar))
 		return -EINVAL;
 
 	legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
@@ -622,7 +622,7 @@ static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, int bar)
 {
 	void __iomem **legacy_iomap_table;
 
-	if (bar >= PCI_STD_NUM_BARS)
+	if (!pci_bar_index_is_valid(bar))
 		return;
 
 	legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
@@ -655,6 +655,9 @@ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
 	void __iomem *mapping;
 	struct pcim_addr_devres *res;
 
+	if (!pci_bar_index_is_valid(bar))
+		return NULL;
+
 	res = pcim_addr_devres_alloc(pdev);
 	if (!res)
 		return NULL;
@@ -722,6 +725,9 @@ void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar,
 	int ret;
 	struct pcim_addr_devres *res;
 
+	if (!pci_bar_index_is_valid(bar))
+		return IOMEM_ERR_PTR(-EINVAL);
+
 	res = pcim_addr_devres_alloc(pdev);
 	if (!res)
 		return IOMEM_ERR_PTR(-ENOMEM);
@@ -823,6 +829,9 @@ static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name,
 	int ret;
 	struct pcim_addr_devres *res;
 
+	if (!pci_bar_index_is_valid(bar))
+		return -EINVAL;
+
 	res = pcim_addr_devres_alloc(pdev);
 	if (!res)
 		return -ENOMEM;
@@ -991,6 +1000,9 @@ void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar,
 	void __iomem *mapping;
 	struct pcim_addr_devres *res;
 
+	if (!pci_bar_index_is_valid(bar))
+		return IOMEM_ERR_PTR(-EINVAL);
+
 	res = pcim_addr_devres_alloc(pdev);
 	if (!res)
 		return IOMEM_ERR_PTR(-ENOMEM);
diff --git a/drivers/pci/iomap.c b/drivers/pci/iomap.c
index 9fb7cacc15cde..fe706ed946dfd 100644
--- a/drivers/pci/iomap.c
+++ b/drivers/pci/iomap.c
@@ -9,6 +9,8 @@
 
 #include <linux/export.h>
 
+#include "pci.h" /* for pci_bar_index_is_valid() */
+
 /**
  * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
  * @dev: PCI device that owns the BAR
@@ -33,12 +35,19 @@ void __iomem *pci_iomap_range(struct pci_dev *dev,
 			      unsigned long offset,
 			      unsigned long maxlen)
 {
-	resource_size_t start = pci_resource_start(dev, bar);
-	resource_size_t len = pci_resource_len(dev, bar);
-	unsigned long flags = pci_resource_flags(dev, bar);
+	resource_size_t start, len;
+	unsigned long flags;
+
+	if (!pci_bar_index_is_valid(bar))
+		return NULL;
+
+	start = pci_resource_start(dev, bar);
+	len = pci_resource_len(dev, bar);
+	flags = pci_resource_flags(dev, bar);
 
 	if (len <= offset || !start)
 		return NULL;
+
 	len -= offset;
 	start += offset;
 	if (maxlen && len > maxlen)
@@ -77,16 +86,20 @@ void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
 				 unsigned long offset,
 				 unsigned long maxlen)
 {
-	resource_size_t start = pci_resource_start(dev, bar);
-	resource_size_t len = pci_resource_len(dev, bar);
-	unsigned long flags = pci_resource_flags(dev, bar);
+	resource_size_t start, len;
+	unsigned long flags;
 
-
-	if (flags & IORESOURCE_IO)
+	if (!pci_bar_index_is_valid(bar))
 		return NULL;
 
+	start = pci_resource_start(dev, bar);
+	len = pci_resource_len(dev, bar);
+	flags = pci_resource_flags(dev, bar);
+
 	if (len <= offset || !start)
 		return NULL;
+	if (flags & IORESOURCE_IO)
+		return NULL;
 
 	len -= offset;
 	start += offset;
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 869d204a70a37..da82d734d09c0 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -3921,6 +3921,9 @@ EXPORT_SYMBOL(pci_enable_atomic_ops_to_root);
  */
 void pci_release_region(struct pci_dev *pdev, int bar)
 {
+	if (!pci_bar_index_is_valid(bar))
+		return;
+
 	/*
 	 * This is done for backwards compatibility, because the old PCI devres
 	 * API had a mode in which the function became managed if it had been
@@ -3965,6 +3968,9 @@ EXPORT_SYMBOL(pci_release_region);
 static int __pci_request_region(struct pci_dev *pdev, int bar,
 				const char *name, int exclusive)
 {
+	if (!pci_bar_index_is_valid(bar))
+		return -EINVAL;
+
 	if (pci_is_managed(pdev)) {
 		if (exclusive == IORESOURCE_EXCLUSIVE)
 			return pcim_request_region_exclusive(pdev, bar, name);
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 01e51db8d285a..d22755de688b8 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -167,6 +167,22 @@ static inline void pci_wakeup_event(struct pci_dev *dev)
 	pm_wakeup_event(&dev->dev, 100);
 }
 
+/**
+ * pci_bar_index_is_valid - Check whether a BAR index is within valid range
+ * @bar: BAR index
+ *
+ * Protects against overflowing &struct pci_dev.resource array.
+ *
+ * Return: true for valid index, false otherwise.
+ */
+static inline bool pci_bar_index_is_valid(int bar)
+{
+	if (bar >= 0 && bar < PCI_NUM_RESOURCES)
+		return true;
+
+	return false;
+}
+
 static inline bool pci_has_subordinate(struct pci_dev *pci_dev)
 {
 	return !!(pci_dev->subordinate);
-- 
2.39.5


  parent reply	other threads:[~2025-04-03 19:14 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-03 19:12 [PATCH AUTOSEL 6.14 01/44] drm: allow encoder mode_set even when connectors change for crtc Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 02/44] drm/virtio: Set missing bo->attached flag Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 03/44] drm/rockchip: Don't change hdmi reference clock rate Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 04/44] drm/xe/bmg: Add new PCI IDs Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 05/44] drm/xe/ptl: Update the PTL pci id table Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 06/44] drm/xe/pf: Don't send BEGIN_ID if VF has no context/doorbells Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 07/44] drm/xe/vf: Don't try to trigger a full GT reset if VF Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 08/44] drm/amd/display: Update Cursor request mode to the beginning prefetch always Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 09/44] drm/amd/display: Guard Possible Null Pointer Dereference Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 10/44] drm/amd/display: add workaround flag to link to force FFE preset Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 11/44] drm/amdgpu: Unlocked unmap only clear page table leaves Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 12/44] drm: panel-orientation-quirks: Add support for AYANEO 2S Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 13/44] drm: panel-orientation-quirks: Add quirks for AYA NEO Flip DS and KB Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 14/44] drm: panel-orientation-quirks: Add quirk for AYA NEO Slide Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 15/44] drm: panel-orientation-quirks: Add new quirk for GPD Win 2 Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 16/44] drm: panel-orientation-quirks: Add quirk for OneXPlayer Mini (Intel) Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 17/44] drm/debugfs: fix printk format for bridge index Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 18/44] drm/bridge: panel: forbid initializing a panel with unknown connector type Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 19/44] drm/amd/display: Update FIXED_VS Link Rate Toggle Workaround Usage Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 20/44] drm/amd/display: stop DML2 from removing pipes based on planes Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 21/44] drivers: base: devres: Allow to release group on device release Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 22/44] drm/amdkfd: clamp queue size to minimum Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 23/44] drm/amdkfd: Fix mode1 reset crash issue Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 24/44] drm/amdkfd: Fix pqm_destroy_queue race with GPU reset Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 25/44] drm/amdkfd: debugfs hang_hws skip GPU with MES Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 26/44] drm/xe/xelp: Move Wa_16011163337 from tunings to workarounds Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 27/44] drm/mediatek: mtk_dpi: Move the input_2p_en bit to platform data Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 28/44] drm/mediatek: mtk_dpi: Explicitly manage TVD clock in power on/off Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 29/44] drm/rockchip: stop passing non struct drm_device to drm_err() and friends Sasha Levin
2025-04-03 19:12 ` [PATCH AUTOSEL 6.14 30/44] PCI: Add Rockchip Vendor ID Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 31/44] drm/amdgpu: handle amdgpu_cgs_create_device() errors in amd_powerplay_create() Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 32/44] drm/amd/display: Prevent VStartup Overflow Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 33/44] PCI: Enable Configuration RRS SV early Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 34/44] drm/amdgpu: Fix the race condition for draining retry fault Sasha Levin
2025-04-03 19:13 ` Sasha Levin [this message]
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 36/44] PCI: vmd: Make vmd_dev::cfg_lock a raw_spinlock_t type Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 37/44] drm/amdgpu: grab an additional reference on the gang fence v2 Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 38/44] s390/pci: Support mmap() of PCI resources except for ISM devices Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 39/44] fbdev: omapfb: Add 'plane' value check Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 40/44] tracing: probe-events: Log error for exceeding the number of arguments Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 41/44] tracing: probe-events: Add comments about entry data storing code Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 42/44] ktest: Fix Test Failures Due to Missing LOG_FILE Directories Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 43/44] tpm, tpm_tis: Workaround failed command reception on Infineon devices Sasha Levin
2025-04-03 19:13 ` [PATCH AUTOSEL 6.14 44/44] tpm: End any active auth session before shutdown 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=20250403191313.2679091-35-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=bingbu.cao@linux.intel.com \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=phasta@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

all inboxes | Powered by JetHome®