Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
@ 2026-05-12 12:03 Maxwell Doose
  2026-05-12 15:17 ` Maxwell Doose
  2026-05-12 15:54 ` David Lechner
  0 siblings, 2 replies; 7+ messages in thread
From: Maxwell Doose @ 2026-05-12 12:03 UTC (permalink / raw)
  To: jic23
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Daniel Baluta,
	open list:IIO SUBSYSTEM AND DRIVERS, open list

A Time-of-check to Time-of-use race condition is present in
kmx61_write_event_config(). Move the mutex_lock() call above it to fix
it.

Fixes: fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")
Signed-off-by: Maxwell Doose <m32285159@gmail.com>
---
 drivers/iio/imu/kmx61.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/imu/kmx61.c b/drivers/iio/imu/kmx61.c
index 3cd91d8a89ee..9aa00acc7f14 100644
--- a/drivers/iio/imu/kmx61.c
+++ b/drivers/iio/imu/kmx61.c
@@ -942,11 +942,13 @@ static int kmx61_write_event_config(struct iio_dev *indio_dev,
 	struct kmx61_data *data = kmx61_get_data(indio_dev);
 	int ret = 0;
 
-	if (state && data->ev_enable_state)
-		return 0;
-
 	mutex_lock(&data->lock);
 
+	if (state && data->ev_enable_state) {
+		ret = 0;
+		goto err_unlock;
+	}
+
 	if (!state && data->motion_trig_on) {
 		data->ev_enable_state = false;
 		goto err_unlock;
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
  2026-05-12 12:03 [PATCH] iio: imu: kmx61: Fix TOCTOU race condition Maxwell Doose
@ 2026-05-12 15:17 ` Maxwell Doose
  2026-05-12 15:24   ` Andy Shevchenko
  2026-05-12 15:54 ` David Lechner
  1 sibling, 1 reply; 7+ messages in thread
From: Maxwell Doose @ 2026-05-12 15:17 UTC (permalink / raw)
  To: Maxwell Doose, jic23
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, Daniel Baluta,
	open list:IIO SUBSYSTEM AND DRIVERS, open list

On Tue May 12, 2026 at 7:03 AM CDT, Maxwell Doose wrote:
> A Time-of-check to Time-of-use race condition is present in
> kmx61_write_event_config(). Move the mutex_lock() call above it to fix
> it.
>
> Fixes: fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
> ---
>  drivers/iio/imu/kmx61.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/iio/imu/kmx61.c b/drivers/iio/imu/kmx61.c
> index 3cd91d8a89ee..9aa00acc7f14 100644
> --- a/drivers/iio/imu/kmx61.c
> +++ b/drivers/iio/imu/kmx61.c
> @@ -942,11 +942,13 @@ static int kmx61_write_event_config(struct iio_dev *indio_dev,
>  	struct kmx61_data *data = kmx61_get_data(indio_dev);
>  	int ret = 0;
>  
> -	if (state && data->ev_enable_state)
> -		return 0;
> -
>  	mutex_lock(&data->lock);
>  
> +	if (state && data->ev_enable_state) {
> +		ret = 0;
> +		goto err_unlock;
> +	}
> +
>  	if (!state && data->motion_trig_on) {
>  		data->ev_enable_state = false;
>  		goto err_unlock;

Whoops, forgot to add reported-by and closes tags, so:

Reported-by: sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260507223337.48437-1-m32285159%40gmail.com

best regards,
max

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
  2026-05-12 15:17 ` Maxwell Doose
@ 2026-05-12 15:24   ` Andy Shevchenko
  2026-05-12 15:30     ` Maxwell Doose
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-05-12 15:24 UTC (permalink / raw)
  To: Maxwell Doose
  Cc: jic23, David Lechner, Nuno Sá, Andy Shevchenko,
	Daniel Baluta, open list:IIO SUBSYSTEM AND DRIVERS, open list

On Tue, May 12, 2026 at 6:17 PM Maxwell Doose <m32285159@gmail.com> wrote:
>
> On Tue May 12, 2026 at 7:03 AM CDT, Maxwell Doose wrote:
> > A Time-of-check to Time-of-use race condition is present in
> > kmx61_write_event_config(). Move the mutex_lock() call above it to fix
> > it.

I think you want to elaborate a bit more on this. Id est explain why
ev_enable_state needs to be protected. Not everybody is willing to go
to some site to read some AI reports and interpreted them.


> Reported-by: sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260507223337.48437-1-m32285159%40gmail.com


-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
  2026-05-12 15:24   ` Andy Shevchenko
@ 2026-05-12 15:30     ` Maxwell Doose
  2026-05-12 17:10       ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Maxwell Doose @ 2026-05-12 15:30 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: jic23, David Lechner, Nuno Sá, Andy Shevchenko,
	Daniel Baluta, open list:IIO SUBSYSTEM AND DRIVERS, open list

On Tue, May 12, 2026 at 10:25 AM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Tue, May 12, 2026 at 6:17 PM Maxwell Doose <m32285159@gmail.com> wrote:
> >
> > On Tue May 12, 2026 at 7:03 AM CDT, Maxwell Doose wrote:
> > > A Time-of-check to Time-of-use race condition is present in
> > > kmx61_write_event_config(). Move the mutex_lock() call above it to fix
> > > it.
>
> I think you want to elaborate a bit more on this. Id est explain why
> ev_enable_state needs to be protected. Not everybody is willing to go
> to some site to read some AI reports and interpreted them.
>

Can do that for v2. I believe that it needs to be protected since
later we set ev_enable_state to false (basically right after). Could
be wrong of course, but Jonathan did confirm the TOCTOU.

best regards,
max



>
> > Reported-by: sashiko <sashiko-bot@kernel.org>
> > Closes: https://sashiko.dev/#/patchset/20260507223337.48437-1-m32285159%40gmail.com
>
>
> --
> With Best Regards,
> Andy Shevchenko

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
  2026-05-12 12:03 [PATCH] iio: imu: kmx61: Fix TOCTOU race condition Maxwell Doose
  2026-05-12 15:17 ` Maxwell Doose
@ 2026-05-12 15:54 ` David Lechner
  1 sibling, 0 replies; 7+ messages in thread
From: David Lechner @ 2026-05-12 15:54 UTC (permalink / raw)
  To: Maxwell Doose, jic23
  Cc: Nuno Sá, Andy Shevchenko, Daniel Baluta,
	open list:IIO SUBSYSTEM AND DRIVERS, open list

On 5/12/26 7:03 AM, Maxwell Doose wrote:
> A Time-of-check to Time-of-use race condition is present in
> kmx61_write_event_config(). Move the mutex_lock() call above it to fix
> it.
> 
> Fixes: fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
> ---
>  drivers/iio/imu/kmx61.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/imu/kmx61.c b/drivers/iio/imu/kmx61.c
> index 3cd91d8a89ee..9aa00acc7f14 100644
> --- a/drivers/iio/imu/kmx61.c
> +++ b/drivers/iio/imu/kmx61.c
> @@ -942,11 +942,13 @@ static int kmx61_write_event_config(struct iio_dev *indio_dev,
>  	struct kmx61_data *data = kmx61_get_data(indio_dev);
>  	int ret = 0;
>  
> -	if (state && data->ev_enable_state)
> -		return 0;
> -
>  	mutex_lock(&data->lock);
>  
> +	if (state && data->ev_enable_state) {
> +		ret = 0;
> +		goto err_unlock;
> +	}
> +
>  	if (!state && data->motion_trig_on) {
>  		data->ev_enable_state = false;
>  		goto err_unlock;

There are actually 3 other drivers that have identical code
which likely need the same fix.

And in all of these, there is an write_event() callback that
reads ev_enable_state without holding the mutex that looks
suspicious too.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
  2026-05-12 15:30     ` Maxwell Doose
@ 2026-05-12 17:10       ` Jonathan Cameron
  2026-05-12 17:37         ` Maxwell Doose
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2026-05-12 17:10 UTC (permalink / raw)
  To: Maxwell Doose
  Cc: Andy Shevchenko, David Lechner, Nuno Sá, Andy Shevchenko,
	Daniel Baluta, open list:IIO SUBSYSTEM AND DRIVERS, open list

On Tue, 12 May 2026 10:30:42 -0500
Maxwell Doose <m32285159@gmail.com> wrote:

> On Tue, May 12, 2026 at 10:25 AM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Tue, May 12, 2026 at 6:17 PM Maxwell Doose <m32285159@gmail.com> wrote:  
> > >
> > > On Tue May 12, 2026 at 7:03 AM CDT, Maxwell Doose wrote:  
> > > > A Time-of-check to Time-of-use race condition is present in
> > > > kmx61_write_event_config(). Move the mutex_lock() call above it to fix
> > > > it.  
> >
> > I think you want to elaborate a bit more on this. Id est explain why
> > ev_enable_state needs to be protected. Not everybody is willing to go
> > to some site to read some AI reports and interpreted them.
> >  
> 
> Can do that for v2. I believe that it needs to be protected since
> later we set ev_enable_state to false (basically right after). Could
> be wrong of course, but Jonathan did confirm the TOCTOU.

I'd talk more about how we'd get a race.  If two calls enter the function
at the same time (which is easy to do) they may both pass this check before
getting to the lock.  Therefore we end up with at best pointless repeated
work, at worst a reference or similar count issue. You'd need to look closely
at what is protected to be sure whether it benign waste of time or a real
bug.

Jonathan

> 
> best regards,
> max
> 
> 
> 
> >  
> > > Reported-by: sashiko <sashiko-bot@kernel.org>
> > > Closes: https://sashiko.dev/#/patchset/20260507223337.48437-1-m32285159%40gmail.com  
> >
> >
> > --
> > With Best Regards,
> > Andy Shevchenko  


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: imu: kmx61: Fix TOCTOU race condition
  2026-05-12 17:10       ` Jonathan Cameron
@ 2026-05-12 17:37         ` Maxwell Doose
  0 siblings, 0 replies; 7+ messages in thread
From: Maxwell Doose @ 2026-05-12 17:37 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Andy Shevchenko, David Lechner, Nuno Sá, Andy Shevchenko,
	Daniel Baluta, open list:IIO SUBSYSTEM AND DRIVERS, open list

On Tue, May 12, 2026 at 12:10 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Tue, 12 May 2026 10:30:42 -0500
> Maxwell Doose <m32285159@gmail.com> wrote:
>
> > On Tue, May 12, 2026 at 10:25 AM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:
> > >
> > > On Tue, May 12, 2026 at 6:17 PM Maxwell Doose <m32285159@gmail.com> wrote:
> > > >
> > > > On Tue May 12, 2026 at 7:03 AM CDT, Maxwell Doose wrote:
> > > > > A Time-of-check to Time-of-use race condition is present in
> > > > > kmx61_write_event_config(). Move the mutex_lock() call above it to fix
> > > > > it.
> > >
> > > I think you want to elaborate a bit more on this. Id est explain why
> > > ev_enable_state needs to be protected. Not everybody is willing to go
> > > to some site to read some AI reports and interpreted them.
> > >
> >
> > Can do that for v2. I believe that it needs to be protected since
> > later we set ev_enable_state to false (basically right after). Could
> > be wrong of course, but Jonathan did confirm the TOCTOU.
>
> I'd talk more about how we'd get a race.  If two calls enter the function
> at the same time (which is easy to do) they may both pass this check before
> getting to the lock.  Therefore we end up with at best pointless repeated
> work, at worst a reference or similar count issue. You'd need to look closely
> at what is protected to be sure whether it benign waste of time or a real
> bug.
>

Well, since we're accessing the shared state via kmx61_get_data (by
the way of iio_priv), we could check that value, pass the check, and
then have the value change before we acquire the lock. TOCTOU race,
no? If data->ev_enable_state is false then becomes true after we check
the value but before we get the lock, then ev_enable_state changes to
whatever the input variable state is. Anyways, just saying that this
is certainly a bug. Would coming across it be common? Probably not,
likely one of the much rarer ones, but still worth fixing.

best regards,
max




> Jonathan
>
> >
> > best regards,
> > max
> >
> >
> >
> > >
> > > > Reported-by: sashiko <sashiko-bot@kernel.org>
> > > > Closes: https://sashiko.dev/#/patchset/20260507223337.48437-1-m32285159%40gmail.com
> > >
> > >
> > > --
> > > With Best Regards,
> > > Andy Shevchenko
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-05-12 17:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-12 12:03 [PATCH] iio: imu: kmx61: Fix TOCTOU race condition Maxwell Doose
2026-05-12 15:17 ` Maxwell Doose
2026-05-12 15:24   ` Andy Shevchenko
2026-05-12 15:30     ` Maxwell Doose
2026-05-12 17:10       ` Jonathan Cameron
2026-05-12 17:37         ` Maxwell Doose
2026-05-12 15:54 ` David Lechner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox