From: Alex Elder <elder@riscstar.com>
To: bhelgaas@google.com, robh@kernel.org, saravanak@kernel.org
Cc: herve.codina@bootlin.com, daniel@riscstar.com,
mohd.anwar@oss.qualcomm.com, lorenzo.bianconi@oss.qualcomm.com,
linux-pci@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH v4 1/4] PCI: of: avoid allocations in of_pci_prop_compatible()
Date: Fri, 4 Sep 2026 08:46:03 -0500 [thread overview]
Message-ID: <20260904134607.1856121-2-elder@riscstar.com> (raw)
In-Reply-To: <20260904134607.1856121-1-elder@riscstar.com>
Three compatible strings are formatted in of_pci_prop_compatible().
Their sizes are known in advance, and the largest is 16 bytes.
Rather than dynamically allocating the space for those strings, just
set aside a buffer on the stack large enough to hold all three.
This avoids a problem that Sashiko pointed out, where an allocation
failure would cause subsequent crash because strlen() is called
unconditionally in of_changeset_add_prop_string_array().
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://lore.kernel.org/sashiko-reviews/a647bd56-7dc8-4fec-9d96-834622054cdf@riscstar.com
Signed-off-by: Alex Elder <elder@riscstar.com>
---
v4: - Added this fix to the beginning of the series
drivers/pci/of_property.c | 31 ++++++++++++++++++++-----------
1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c
index 75a358f73e694..a0632f932b5c6 100644
--- a/drivers/pci/of_property.c
+++ b/drivers/pci/of_property.c
@@ -324,27 +324,36 @@ static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs,
return ret;
}
+/* The three compatible property strings have max sizes 12+1, 15+1, and 13+1 */
+#define PROP_SIZE 16 /* Max size of each compatible string */
static int of_pci_prop_compatible(struct pci_dev *pdev,
struct of_changeset *ocs,
struct device_node *np)
{
const char *compat_strs[PROP_COMPAT_NUM] = { 0 };
+ char buf[PROP_COMPAT_NUM * PROP_SIZE] = { };
+ char bufp = buf;
int i, ret;
- compat_strs[PROP_COMPAT_PCI_VVVV_DDDD] =
- kasprintf(GFP_KERNEL, "pci%x,%x", pdev->vendor, pdev->device);
- compat_strs[PROP_COMPAT_PCICLASS_CCSSPP] =
- kasprintf(GFP_KERNEL, "pciclass,%06x", pdev->class);
- compat_strs[PROP_COMPAT_PCICLASS_CCSS] =
- kasprintf(GFP_KERNEL, "pciclass,%04x", pdev->class >> 8);
+ ret = snprintf(bufp, PROP_SIZE, "pci%x,%x", pdev->vendor, pdev->device);
+ if (ret >= PROP_SIZE)
+ return -EINVAL;
+ compat_strs[PROP_COMPAT_PCI_VVVV_DDDD] = bufp;
+ bufp += ret + 1;
- ret = of_changeset_add_prop_string_array(ocs, np, "compatible",
- compat_strs, PROP_COMPAT_NUM);
- for (i = 0; i < PROP_COMPAT_NUM; i++)
- kfree(compat_strs[i]);
+ ret = snprintf(bufp, PROP_SIZE, "pciclass,%06x", pdev->class);
+ if (ret >= PROP_SIZE)
+ return -EINVAL;
+ compat_strs[PROP_COMPAT_PCICLASS_CCSSPP] = bufp;
+ bufp += ret + 1;
- return ret;
+ ret = snprintf(bufp, PROP_SIZE, "pciclass,%04x", pdev->class >> 8);
+ compat_strs[PROP_COMPAT_PCICLASS_CCSS] = bufp;
+
+ return of_changeset_add_prop_string_array(ocs, np, "compatible",
+ compat_strs, PROP_COMPAT_NUM);
}
+#undef PROP_SIZE
int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
struct device_node *np)
--
2.53.0
next prev parent reply other threads:[~2026-09-04 13:46 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-04 13:46 [PATCH v4 0/4] PCI: of: warn on bogus device_type property Alex Elder
2026-09-04 13:46 ` Alex Elder [this message]
2026-09-04 13:59 ` [PATCH v4 1/4] PCI: of: avoid allocations in of_pci_prop_compatible() sashiko-bot
2026-09-04 14:04 ` Alex Elder
2026-09-04 13:46 ` [PATCH v4 2/4] PCI: of: drop the reg_num argument to of_pci_set_address() Alex Elder
2026-09-04 14:03 ` sashiko-bot
2026-09-04 13:46 ` [PATCH v4 3/4] PCI: of: don't zero flags in of_pci_get_addr_flags() Alex Elder
2026-09-04 14:06 ` sashiko-bot
2026-09-04 13:46 ` [PATCH v4 4/4] PCI: of: introduce of_pci_verify_node() Alex Elder
2026-09-04 14:09 ` sashiko-bot
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=20260904134607.1856121-2-elder@riscstar.com \
--to=elder@riscstar.com \
--cc=bhelgaas@google.com \
--cc=daniel@riscstar.com \
--cc=devicetree@vger.kernel.org \
--cc=herve.codina@bootlin.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lorenzo.bianconi@oss.qualcomm.com \
--cc=mohd.anwar@oss.qualcomm.com \
--cc=robh@kernel.org \
--cc=saravanak@kernel.org \
--cc=sashiko-bot@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