Skip to contentSkip to conversation

iio: inkern: guard fwnode_xlate with info_exist_lock

PATCH Superseded Ilia Snezhkov 3 participants 3 messages Nov 25 – Nov 26 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: inkern: guard fwnode_xlate with info_exist_lock

__fwnode_iio_channel_get() dereferences indio_dev->info->fwnode_xlate and invokes it without holding info_exist_lock. Concurrent iio_device_unregister() clears indio_dev->info under info_exist_lock, allowing a NULL dereference or callback execution during provider teardown.

Hold iio_dev_opaque->info_exist_lock with scoped_guard(mutex, ...) matching the rest of inkern.c, return -ENODEV if indio_dev->info is NULL, and bounds-check the translated index against indio_dev->num_channels before indexing indio_dev->channels.

Found and drafted with an AI coding assistant.

Fixes acd8256723f2 ("iio: inkern: Add of_xlate function to struct iio_info")
---
 drivers/iio/inkern.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/iio/inkern.c +14 -5
index 1f22275..1a64b71 100644
@@ -145,6 +145,7 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
 				    struct fwnode_handle *fwnode, int index)
 {
 	struct fwnode_reference_args iiospec;
+	struct iio_dev_opaque *iio_dev_opaque;
 	struct device *idev;
 	struct iio_dev *indio_dev;
 	int err;
@@ -162,15 +163,23 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
 	}
 
 	indio_dev = dev_to_iio_dev(idev);
+	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	channel->indio_dev = indio_dev;
-	if (indio_dev->info->fwnode_xlate)
-		index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
-	else
-		index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
+	scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
+		if (!indio_dev->info)
+			index = -ENODEV;
+		else if (indio_dev->info->fwnode_xlate)
+			index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
+		else
+			index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
+		if (index >= indio_dev->num_channels)
+			index = -EINVAL;
+		if (index >= 0)
+			channel->channel = &indio_dev->channels[index];
+	}
 	fwnode_handle_put(iiospec.fwnode);
 	if (index < 0)
 		goto err_put;
-	channel->channel = &indio_dev->channels[index];
 
 	return 0;
 
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

drivers/iio/inkern.c static int __fwnode_iio_channel_get(struct iio_channel *channel, · line 170

Quoted 3 lines · Ilia Snezhkov
drivers/iio/inkern.c +3 -0
+	scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
+		if (!indio_dev->info)
+			index = -ENODEV;

Taking info_exist_lock here matches the rest of inkern.c, but -ENODEV already means "no channel on this node, keep looking" in the callers. Without a name, __fwnode_iio_channel_get_by_name() passes it back up, fwnode_iio_channel_get_by_name() then walks the parents and iio_channel_get() falls back to iio_channel_get_sys(), and iio_channel_get_all() falls back to the map list on -ENODEV from fwnode_iio_channel_get_all(). So if the race is hit, the lookup carries on instead of failing. A code that isn't used as that sentinel would avoid it.

drivers/iio/inkern.c static int __fwnode_iio_channel_get(struct iio_channel *channel, · line 176

Quoted 2 lines
drivers/iio/inkern.c +2 -0
+		if (index >= indio_dev->num_channels)
+			index = -EINVAL;

__fwnode_iio_simple_xlate() already checks args[0] against num_channels, so this only matters for drivers with their own fwnode_xlate. Could the commit message say that, or could it be a separate patch? It is unrelated to the locking.

Thanks,
Arnav

#2 lore raw
In reply to Ilia Snezhkov
Quoted 11 lines · On Wed, Nov 25, 2026 at 11:48:27PM +0000, Ilia Snezhkov via B4 Relay wrote:

__fwnode_iio_channel_get() dereferences indio_dev->info->fwnode_xlate and invokes it without holding info_exist_lock. Concurrent iio_device_unregister() clears indio_dev->info under info_exist_lock, allowing a NULL dereference or callback execution during provider teardown.

Hold iio_dev_opaque->info_exist_lock with scoped_guard(mutex, ...) matching the rest of inkern.c, return -ENODEV if indio_dev->info is NULL, and bounds-check the translated index against indio_dev->num_channels before indexing indio_dev->channels.

Sounds legit.

Quoted 1 line

Found and drafted with an AI coding assistant.

Move this to the comment block after the '---' line below.

Quoted 3 lines
Fixes acd8256723f2 ("iio: inkern: Add of_xlate function to struct iio_info")

---

...

drivers/iio/inkern.c static int __fwnode_iio_channel_get(struct iio_channel *channel, · line 167

Quoted 3 lines
drivers/iio/inkern.c +1 -0
 	indio_dev = dev_to_iio_dev(idev);
+	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	channel->indio_dev = indio_dev;

More naturally the new code line looks better when placed here.

Quoted 16 lines
drivers/iio/inkern.c +12 -4
-	if (indio_dev->info->fwnode_xlate)
-		index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
-	else
-		index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
+	scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
+		if (!indio_dev->info)
+			index = -ENODEV;
+		else if (indio_dev->info->fwnode_xlate)
+			index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
+		else
+			index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
+		if (index >= indio_dev->num_channels)
+			index = -EINVAL;
+		if (index >= 0)
+			channel->channel = &indio_dev->channels[index];
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko