The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Stepan Ionichev <sozdayvek@gmail.com>
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	hcazarim@yahoo.com, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org, Salah Triki <salah.triki@gmail.com>
Subject: Re: [PATCH] iio: potentiostat: lmp91000: fix NULL deref in probe by reordering setup
Date: Fri, 22 May 2026 16:01:58 +0100	[thread overview]
Message-ID: <20260522160158.43cd1645@jic23-huawei> (raw)
In-Reply-To: <20260520185142.34015-1-sozdayvek@gmail.com>

On Wed, 20 May 2026 23:51:41 +0500
Stepan Ionichev <sozdayvek@gmail.com> wrote:

> lmp91000_probe() calls iio_trigger_set_immutable() with
> iio_channel_cb_get_iio_dev(data->cb_buffer) before data->cb_buffer is
> assigned. The struct is zero-initialised by devm_iio_device_alloc(), so
> cb_buffer is NULL on entry, and iio_channel_cb_get_iio_dev() does an
> unconditional cb_buffer->indio_dev which dereferences NULL.
> 
> Reorder probe to acquire cb_buffer first (handling -EPROBE_DEFER) and
> only then set the immutable trigger, register the trigger, set up the
> triggered buffer, and register the iio device. Move the cb_buffer
> release to the end of the cleanup chain so a late failure properly
> unwinds in reverse order.
> 
> Signed-off-by: Stepan Ionichev <sozdayvek@gmail.com>

Hi Stephan

Make sure to check the list before sending out a patch.
+CC Salah who is working on a fix for the same thing.

Jonathan

> ---
>  drivers/iio/potentiostat/lmp91000.c | 36 +++++++++++++----------------
>  1 file changed, 16 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/iio/potentiostat/lmp91000.c b/drivers/iio/potentiostat/lmp91000.c
> index eccc2a343..7a938a023 100644
> --- a/drivers/iio/potentiostat/lmp91000.c
> +++ b/drivers/iio/potentiostat/lmp91000.c
> @@ -330,17 +330,27 @@ static int lmp91000_probe(struct i2c_client *client)
>  	if (ret)
>  		return ret;
>  
> +	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
> +						 indio_dev);
> +	if (IS_ERR(data->cb_buffer)) {
> +		if (PTR_ERR(data->cb_buffer) == -ENODEV)
> +			return -EPROBE_DEFER;
> +		return PTR_ERR(data->cb_buffer);
> +	}
> +
> +	data->adc_chan = iio_channel_cb_get_channels(data->cb_buffer);
> +
>  	ret = iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data->cb_buffer),
>  					data->trig);
>  	if (ret) {
>  		dev_err(dev, "cannot set immutable trigger.\n");
> -		return ret;
> +		goto error_release_cb;
>  	}
>  
>  	ret = iio_trigger_register(data->trig);
>  	if (ret) {
>  		dev_err(dev, "cannot register iio trigger.\n");
> -		return ret;
> +		goto error_release_cb;
>  	}
>  
>  	ret = iio_triggered_buffer_setup(indio_dev, NULL,
> @@ -349,35 +359,21 @@ static int lmp91000_probe(struct i2c_client *client)
>  	if (ret)
>  		goto error_unreg_trigger;
>  
> -	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
> -						 indio_dev);
> -
> -	if (IS_ERR(data->cb_buffer)) {
> -		if (PTR_ERR(data->cb_buffer) == -ENODEV)
> -			ret = -EPROBE_DEFER;
> -		else
> -			ret = PTR_ERR(data->cb_buffer);
> -
> -		goto error_unreg_buffer;
> -	}
> -
> -	data->adc_chan = iio_channel_cb_get_channels(data->cb_buffer);
> -
>  	ret = iio_device_register(indio_dev);
>  	if (ret)
> -		goto error_unreg_cb_buffer;
> +		goto error_unreg_buffer;
>  
>  	return 0;
>  
> -error_unreg_cb_buffer:
> -	iio_channel_release_all_cb(data->cb_buffer);
> -
>  error_unreg_buffer:
>  	iio_triggered_buffer_cleanup(indio_dev);
>  
>  error_unreg_trigger:
>  	iio_trigger_unregister(data->trig);
>  
> +error_release_cb:
> +	iio_channel_release_all_cb(data->cb_buffer);
> +
>  	return ret;
>  }
>  


  reply	other threads:[~2026-05-22 15:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20 18:51 [PATCH] iio: potentiostat: lmp91000: fix NULL deref in probe by reordering setup Stepan Ionichev
2026-05-22 15:01 ` Jonathan Cameron [this message]
2026-05-23 13:45 ` Stepan Ionichev

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=20260522160158.43cd1645@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=hcazarim@yahoo.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=salah.triki@gmail.com \
    --cc=sozdayvek@gmail.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