From: "Théo Lebrun" <theo.lebrun@bootlin.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Rob Herring <robh@kernel.org>,
Saravana Kannan <saravanak@google.com>,
"David S. Miller" <davem@davemloft.net>,
Grant Likely <grant.likely@secretlab.ca>
Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
"Liam Girdwood" <lgirdwood@gmail.com>,
"Mark Brown" <broonie@kernel.org>,
"Jaroslav Kysela" <perex@perex.cz>,
"Takashi Iwai" <tiwai@suse.com>,
"Binbin Zhou" <zhoubinbin@loongson.cn>,
linux-sound@vger.kernel.org,
"Vladimir Kondratiev" <vladimir.kondratiev@mobileye.com>,
"Grégory Clement" <gregory.clement@bootlin.com>,
"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
"Tawfik Bayouk" <tawfik.bayouk@mobileye.com>,
"Théo Lebrun" <theo.lebrun@bootlin.com>,
stable@vger.kernel.org
Subject: [PATCH 2/2] driver core: platform: avoid use-after-free on pdev->name
Date: Tue, 18 Feb 2025 12:00:13 +0100 [thread overview]
Message-ID: <20250218-pdev-uaf-v1-2-5ea1a0d3aba0@bootlin.com> (raw)
In-Reply-To: <20250218-pdev-uaf-v1-0-5ea1a0d3aba0@bootlin.com>
The issue is with this:
int of_device_add(struct platform_device *ofdev)
{
// ...
ofdev->name = dev_name(&ofdev->dev);
// ...
}
We store the current device name pointer. If the device name changes
through a `dev_set_name(dev, "foo")` call:
- old device name is freed: kfree(dev->name);
- new device name is allocated: kmalloc(...);
- notice pdev->name is still the old device name, ie a freed pointer.
OF is at fault here, taking the pointer to the device name in
of_device_add().
The new PLATFORM_DEVICE_FLAG_FREE_NAME flag tells platform devices if
they own their pdev->name pointer and if it requires a kfree() call.
Considerations:
- The generic case in platform_device_register_full() is not faulty
because it allocates memory for storing the name adjacent to the
`struct platform_device` alloc; see platform_device_alloc():
struct platform_object *pa;
pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
We cannot rely on this codepath in all cases because OF wants to
change the name after the platform device creation.
- kfree_const() cannot solve the issue: either we allocated pdev->name
separately or it is part of the platform_object allocation.
pdev->name is never coming from read-only data.
- It is important to duplicate! pdev->name must not change to make sure
the platform_match() return value is stable over time. If we updated
pdev->name alongside dev->name, once a device probes and changes its
name then the platform_match() return value would change.
- In of_device_add(), we make sure to kstrdup() the new name before
freeing the old one; if alloc fails, we leave the device as-is.
Fixes: eca3930163ba ("of: Merge of_platform_bus_type with platform_bus_type")
Cc: <stable@vger.kernel.org>
Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
---
drivers/base/platform.c | 2 ++
drivers/of/platform.c | 12 +++++++++++-
include/linux/platform_device.h | 1 +
3 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index e2284482c7ba7c12fe2ab3c715e7d1daa3f65021..3548714d6ba408abc6c7ab0f3e7496c6e27ba060 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -563,6 +563,8 @@ static void platform_device_release(struct device *dev)
kfree(pa->pdev.mfd_cell);
kfree(pa->pdev.resource);
kfree(pa->pdev.driver_override);
+ if (pa->pdev.flags & PLATFORM_DEVICE_FLAG_FREE_NAME)
+ kfree(pa->pdev.name);
kfree(pa);
}
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index c6d8afb284e88061eb6fb0ba02e429cec702664c..ef6f341fd9b77a9e0ed6969c3f322b9bc91d0e8d 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -44,11 +44,21 @@ EXPORT_SYMBOL(of_find_device_by_node);
int of_device_add(struct platform_device *ofdev)
{
+ char *new_name;
+
BUG_ON(ofdev->dev.of_node == NULL);
+ new_name = kstrdup(dev_name(&ofdev->dev), GFP_KERNEL);
+ if (!new_name)
+ return -ENOMEM;
+
+ if (ofdev->flags & PLATFORM_DEVICE_FLAG_FREE_NAME)
+ kfree(ofdev->name);
+
/* name and id have to be set so that the platform bus doesn't get
* confused on matching */
- ofdev->name = dev_name(&ofdev->dev);
+ ofdev->name = new_name;
+ ofdev->flags |= PLATFORM_DEVICE_FLAG_FREE_NAME;
ofdev->id = PLATFORM_DEVID_NONE;
/*
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index d842b21ba3791f974fa62f52bd160ef5820261c1..203016afc3899ffa05f38b9d4ce3bfc02d5b75ef 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -25,6 +25,7 @@ struct platform_device {
int id;
u8 flags;
#define PLATFORM_DEVICE_FLAG_ID_AUTO BIT(0)
+#define PLATFORM_DEVICE_FLAG_FREE_NAME BIT(1)
struct device dev;
u64 platform_dma_mask;
struct device_dma_parameters dma_parms;
--
2.48.1
next prev parent reply other threads:[~2025-02-18 11:00 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-18 11:00 [PATCH 0/2] driver core: platform: avoid use-after-free on device name Théo Lebrun
2025-02-18 11:00 ` [PATCH 1/2] driver core: platform: turn pdev->id_auto into pdev->flags Théo Lebrun
2025-02-18 11:00 ` Théo Lebrun [this message]
2025-02-20 12:41 ` [PATCH 0/2] driver core: platform: avoid use-after-free on device name Greg Kroah-Hartman
2025-02-20 13:31 ` Théo Lebrun
2025-02-20 14:06 ` Greg Kroah-Hartman
2025-02-20 15:46 ` Théo Lebrun
2025-02-20 16:19 ` Greg Kroah-Hartman
2025-02-20 18:26 ` Théo Lebrun
2025-02-20 18:55 ` Greg Kroah-Hartman
2025-02-21 8:46 ` Thomas Petazzoni
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=20250218-pdev-uaf-v1-2-5ea1a0d3aba0@bootlin.com \
--to=theo.lebrun@bootlin.com \
--cc=broonie@kernel.org \
--cc=dakr@kernel.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=grant.likely@secretlab.ca \
--cc=gregkh@linuxfoundation.org \
--cc=gregory.clement@bootlin.com \
--cc=lgirdwood@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=saravanak@google.com \
--cc=stable@vger.kernel.org \
--cc=tawfik.bayouk@mobileye.com \
--cc=thomas.petazzoni@bootlin.com \
--cc=tiwai@suse.com \
--cc=vladimir.kondratiev@mobileye.com \
--cc=zhoubinbin@loongson.cn \
/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