]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
PCI: Test for bit underflow in pcie_set_readrq()
authorKees Cook <kees@kernel.org>
Fri, 5 Sep 2025 05:28:41 +0000 (22:28 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Oct 2025 13:08:52 +0000 (14:08 +0100)
[ Upstream commit 00e58ff924b3a684b076f9512fe2753be87b50e1 ]

In preparation for the future commit ("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. :)

Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Closes: https://lore.kernel.org/linux-pci/CA+G9fYuysVr6qT8bjF6f08WLyCJRG7aXAeSd2F7=zTaHHd7L+Q@mail.gmail.com/
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20250905052836.work.425-kees@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/pci/pci.c

index 3d1365f558d3a35ca66e58a01e44519e23a028f3..0dd548e2b3676897b6def78d849502672a30f868 100644 (file)
@@ -6048,6 +6048,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))
@@ -6065,7 +6066,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);