From: "Théo Lebrun" <theo.lebrun@bootlin.com>
To: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Cc: "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>,
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>,
stable@vger.kernel.org
Subject: Re: [PATCH 0/2] driver core: platform: avoid use-after-free on device name
Date: Thu, 20 Feb 2025 16:46:59 +0100 [thread overview]
Message-ID: <D7XE2DSESCHX.328BJ5KCEFH0A@bootlin.com> (raw)
In-Reply-To: <2025022004-scheming-expend-b9b3@gregkh>
On Thu Feb 20, 2025 at 3:06 PM CET, Greg Kroah-Hartman wrote:
> On Thu, Feb 20, 2025 at 02:31:29PM +0100, Théo Lebrun wrote:
>> On Thu Feb 20, 2025 at 1:41 PM CET, Greg Kroah-Hartman wrote:
>> > On Tue, Feb 18, 2025 at 12:00:11PM +0100, Théo Lebrun wrote:
>> >> The solution proposed is to add a flag to platform_device that tells if
>> >> it is responsible for freeing its name. We can then duplicate the
>> >> device name inside of_device_add() instead of copying the pointer.
>> >
>> > Ick.
>> >
>> >> What is done elsewhere?
>> >> - Platform bus code does a copy of the argument name that is stored
>> >> alongside the struct platform_device; see platform_device_alloc()[1].
>> >> - Other busses duplicate the device name; either through a dynamic
>> >> allocation [2] or through an array embedded inside devices [3].
>> >> - Some busses don't have a separate name; when they want a name they
>> >> take it from the device [4].
>> >
>> > Really ick.
>> >
>> > Let's do the right thing here and just get rid of the name pointer
>> > entirely in struct platform_device please. Isn't that the correct
>> > thing that way the driver core logic will work properly for all of this.
>>
>> I would agree, if it wasn't for this consideration that is found in the
>> commit message [0]:
>
> What, that the of code is broken? Then it should be fixed, why does it
> need a pointer to a name at all anyway? It shouldn't be needed there
> either.
I cannot guess why it originally has a separate pdev->name field.
All I can tell you is a good reason to have one, as quoted below.
>> > 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.
>>
>> I'd be fine sending a V2 that removes the field *and the fallback* [1],
>> but I don't have the full scope in mind to know what would become broken.
>>
>> [0]: https://lore.kernel.org/lkml/20250218-pdev-uaf-v1-2-5ea1a0d3aba0@bootlin.com/
>> [1]: https://elixir.bootlin.com/linux/v6.13.3/source/drivers/base/platform.c#L1357
>
> The fallback will not need to be removed, properly point to the name of
> the device and it should work correctly.
No, it will not work correctly, as the above quote indicates.
Let's assume we remove the field, this situation would be broken:
- OF allocates platform devices and gives them names.
- A device matches with a driver, which gets probed.
- During the probe, driver does a dev_set_name().
- Afterwards, the upcoming platform_match() against other drivers are
called with another device name.
We should be safe as there are guardraids to not probe twice a device,
see __driver_probe_device() that checks dev->driver is NULL. But it
isn't a situation we should be in.
Another broken situation:
- OF allocates platform devices and gives them names.
- A device matches with a driver, which gets probed based on its name.
- During the probe, driver does a dev_set_name().
- Module is removed.
- Module is re-added, the (driver, device) pair don't end up matching
again because the device name changed.
I might be missing other edge-cases.
Conclusion: we need a constant name for platform devices as we want the
return value of platform_match() to stay stable across time.
Regards,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
next prev parent reply other threads:[~2025-02-20 15:47 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 ` [PATCH 2/2] driver core: platform: avoid use-after-free on pdev->name Théo Lebrun
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 [this message]
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=D7XE2DSESCHX.328BJ5KCEFH0A@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