Re: [PATCH] iio: inkern: guard fwnode_xlate with info_exist_lock

From: Andy Shevchenko

Date: Thu Nov 26 2026 - 04:25:46 EST


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.

> Found and drafted with an AI coding assistant.

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

> Fixes: acd8256723f2 ("iio: inkern: Add of_xlate function to struct iio_info")
> Signed-off-by: Ilia Snezhkov <iasnezhkov@xxxxxxxxx>
> ---

...

> 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.

> - 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];
> + }

--
With Best Regards,
Andy Shevchenko