All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Yuansheng Yang <0xbeac@gmail.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal
Date: Fri, 4 Dec 2026 18:50:20 +0200	[thread overview]
Message-ID: <axLvTO2L7V8OmA7V@ashevche-desk.local> (raw)
In-Reply-To: <20261203193202.550807-1-0xbeac@gmail.com>

On Fri, Dec 04, 2026 at 03:32:02AM +0800, Yuansheng Yang wrote:
> Removing a loop trigger via configfs frees struct iio_loop_info in
> iio_loop_release() while the trigger is still referenced by an IIO
> device, and leaves any running loop kthread behind. Later disabling or
> re-enabling the buffer calls iio_loop_trigger_set_state(), dereferencing
> the freed iio_loop_info via iio_trigger_get_drvdata():
> 
>   BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state
>   Call Trace:
>    iio_loop_trigger_set_state
>    iio_trigger_detach_poll_func
>    iio_disable_buffers
>    __iio_update_buffers
>    enable_store
> 
> Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with
> a mutex. In iio_trig_loop_remove(), stop any running kthread and clear
> the trigger's drvdata to NULL before calling iio_trigger_free(). In
> iio_loop_trigger_set_state(), return 0 on disable or -ENODEV on enable
> when drvdata is NULL, and avoid storing an ERR_PTR() in loop_trig->task
> if kthread_run() fails.

> Gemini was used to assist with code path verification, QEMU KASAN
> reproduction, and drafting the initial patch; the final code and this
> changelog were reviewed and edited by me.

This should be moved to the comment block.

...

>  static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state)
>  {
> -	struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig);
> +	struct iio_loop_info *loop_trig;
> +	struct task_struct *task;
> +
> +	guard(mutex)(&iio_loop_lock);

> +	loop_trig = iio_trigger_get_drvdata(trig);

Leave this line as it was before...

> +	if (!loop_trig)
> +		return state ? -ENODEV : 0;

...and just add two conditionals to each of the existing if (state) branches
below.

>  	if (state) {
> -		loop_trig->task = kthread_run(iio_loop_thread,
> -					      trig, trig->name);
> -		if (IS_ERR(loop_trig->task)) {
> +		if (loop_trig->task)
> +			return -EBUSY;
> +
> +		task = kthread_run(iio_loop_thread, trig, trig->name);
> +		if (IS_ERR(task)) {
>  			dev_err(&trig->dev,
>  				"failed to create trigger loop thread\n");
> -			return PTR_ERR(loop_trig->task);
> +			return PTR_ERR(task);
>  		}
> -	} else {
> +		loop_trig->task = task;
> +	} else if (loop_trig->task) {
>  		kthread_stop(loop_trig->task);
> +		loop_trig->task = NULL;
>  	}

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-12-04 16:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 19:32 [PATCH v2] iio: trigger: loop: fix use-after-free on configfs removal Yuansheng Yang
2026-12-04 16:50 ` Andy Shevchenko [this message]
2026-12-04 19:09   ` Yuansheng Yang

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=axLvTO2L7V8OmA7V@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=0xbeac@gmail.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=stable@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.