mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rihyeon Kim <rihyeon8648@gmail.com>
To: bhelgaas@google.com
Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
	gregkh@linuxfoundation.org, tarunsahu@google.com,
	djeffery@redhat.com,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>
Subject: [PATCH v2 2/2] PCI: Free the device name on the pci_setup_device() error path
Date: Sun, 26 Jul 2026 18:32:00 +0900	[thread overview]
Message-ID: <20260726093200.146111-3-rihyeon8648@gmail.com> (raw)
In-Reply-To: <20260726093200.146111-1-rihyeon8648@gmail.com>

pci_setup_device() sets the device name with dev_set_name(), which
allocates it.  When the header type turns out to be unknown the function
returns -EIO, and pci_scan_device() then releases the device with kfree()
rather than put_device(), because the device has not been initialized with
device_initialize() yet.  The name is never freed, so it is leaked.

Free it there and clear the pointer, so the freed name is not left behind
in the kobject.

Yang Yingliang reported the same leak in 2022.

Fixes: 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
Suggested-by: Yang Yingliang <yangyingliang@huawei.com>
Link: https://lore.kernel.org/linux-pci/20220817084444.2876059-1-yangyingliang@huawei.com/
Assisted-by: Claude:claude-opus-5 sparse
Signed-off-by: Rihyeon Kim <rihyeon8648@gmail.com>
---
v2: new patch, picked up from Yang Yingliang's 2022 posting as suggested in
    review, with the NULL assignment added.

 drivers/pci/probe.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 3e6f0eb7d456..74d84093943b 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -2192,6 +2192,8 @@ int pci_setup_device(struct pci_dev *dev)
 	default:				    /* unknown header */
 		pci_err(dev, "unknown header type %02x, ignoring device\n",
 			dev->hdr_type);
+		kfree_const(dev->dev.kobj.name);
+		dev->dev.kobj.name = NULL;
 		pci_release_of_node(dev);
 		return -EIO;
 
-- 
2.43.0


  parent reply	other threads:[~2026-07-26  9:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-26  9:31 [PATCH v2 0/2] PCI: Fix dev_set_name() error handling in pci_setup_device() Rihyeon Kim
2026-07-26  9:31 ` [PATCH v2 1/2] PCI: Handle dev_set_name() failure " Rihyeon Kim
2026-08-04 13:31   ` tarunsahu
2026-08-06  9:42     ` Rihyeon Kim
2026-08-07 19:27   ` David Jeffery
2026-07-26  9:32 ` Rihyeon Kim [this message]
2026-08-07 19:45   ` [PATCH v2 2/2] PCI: Free the device name on the pci_setup_device() error path David Jeffery
2026-08-18  5:57     ` Rihyeon Kim
2026-08-18  6:12 ` [PATCH v2 0/2] PCI: Fix dev_set_name() error handling in pci_setup_device() Rihyeon Kim

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=20260726093200.146111-3-rihyeon8648@gmail.com \
    --to=rihyeon8648@gmail.com \
    --cc=bhelgaas@google.com \
    --cc=djeffery@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=tarunsahu@google.com \
    /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®