public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI: Test for bit underflow in pcie_set_readrq()
@ 2025-09-05  5:28 Kees Cook
  2025-09-05  8:16 ` Anders Roxell
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Kees Cook @ 2025-09-05  5:28 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Kees Cook, Linux Kernel Functional Testing, Anders Roxell,
	Naresh Kamboju, lkft-triage, Linux Regressions, Arnd Bergmann,
	Dan Carpenter, Ben Copeland, linux-pci, linux-kernel,
	Peter Zijlstra (Intel), linux-hardening

After commit cbc654d18d37 ("bitops: Add __attribute_const__ to generic
ffs()-family implementations"), which allows GCC's value range tracker
to see past ffs(), GCC 8 on ARM thinks that it might be possible that
"ffs(rq) - 8" used here:

	v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);

could wrap below 0, leading to a very large value, which would be out of
range for the FIELD_PREP() usage:

drivers/pci/pci.c: In function 'pcie_set_readrq':
include/linux/compiler_types.h:572:38: error: call to '__compiletime_assert_471' declared with attribute error: FIELD_PREP: value too large for the field
...
drivers/pci/pci.c:5896:6: note: in expansion of macro 'FIELD_PREP'
  v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);
      ^~~~~~~~~~

If the result of the ffs() is bounds checked before being used in
FIELD_PREP(), the value tracker seems happy again. :)

Fixes: cbc654d18d37 ("bitops: Add __attribute_const__ to generic ffs()-family implementations")
Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Closes: https://lore.kernel.org/linux-pci/CA+G9fYuysVr6qT8bjF6f08WLyCJRG7aXAeSd2F7=zTaHHd7L+Q@mail.gmail.com/
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Anders Roxell <anders.roxell@linaro.org>
Cc: Naresh Kamboju <naresh.kamboju@linaro.org>
Cc: lkft-triage@lists.linaro.org
Cc: Linux Regressions <regressions@lists.linux.dev>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Dan Carpenter <dan.carpenter@linaro.org>
Cc: Ben Copeland <benjamin.copeland@linaro.org>
Cc: <lkft-triage@lists.linaro.org>
Cc: <linux-pci@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>
---
 drivers/pci/pci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index b0f4d98036cd..005b92e6585e 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5932,6 +5932,7 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 {
 	u16 v;
 	int ret;
+	unsigned int firstbit;
 	struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
 
 	if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
@@ -5949,7 +5950,10 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 			rq = mps;
 	}
 
-	v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);
+	firstbit = ffs(rq);
+	if (firstbit < 8)
+		return -EINVAL;
+	v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, firstbit - 8);
 
 	if (bridge->no_inc_mrrs) {
 		int max_mrrs = pcie_get_readrq(dev);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-09-08 21:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-05  5:28 [PATCH] PCI: Test for bit underflow in pcie_set_readrq() Kees Cook
2025-09-05  8:16 ` Anders Roxell
2025-09-05 10:52   ` Arnd Bergmann
2025-09-08 21:43   ` Kees Cook
2025-09-05  8:25 ` Arnd Bergmann
2025-09-08 21:46   ` Kees Cook
2025-09-08 20:53 ` Bjorn Helgaas
2025-09-08 21:39   ` Kees Cook
2025-09-08 21:51 ` Bjorn Helgaas
2025-09-08 21:57   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox