iio: imu: kmx61: Fix potential time-of-check to time-of-use race
PATCH v2 1/1 iio: imu: kmx61: Fix potential time-of-check to time-of-use race
A time-of-check to time-of-use race condition exists in kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifies data->ev_enable_state to force returning from the function, the second thread continues execution regardless. Fix this by moving the data->ev_enable_state check inside of the critical section.
--- drivers/iio/imu/kmx61.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/imu/kmx61.c +3 -3
@@ -942,11 +942,11 @@ 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) + goto err_unlock; +if (!state && data->motion_trig_on) { data->ev_enable_state = false; goto err_unlock;
Signature · 1 line
2.54.0
Quoted 38 lines · Maxwell Doose
A time-of-check to time-of-use race condition exists in
kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifiesdata->ev_enable_stateto force returning from the function, the second thread continues execution regardless. Fix this by moving thedata->ev_enable_statecheck inside of the critical section.Fixes fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")Reported-by sashiko <[email protected]>Signed-off-by Maxwell Doose <[email protected]>--- drivers/iio/imu/kmx61.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/imu/kmx61.c +3
-4@@ -942,11 +942,11 @@ 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) + goto err_unlock; +if (!state && data->motion_trig_on) { data->ev_enable_state = false; goto err_unlock; --
Quoted 2 lines
2.54.0
Silly me, forgot the changelog.
v2:
- Started from scratch due to messy git tree
- Updated commit message per Andy and Jonathan
best regards,
max
Quoted 28 lines · Maxwell Doose
A time-of-check to time-of-use race condition exists in
kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifiesdata->ev_enable_stateto force returning from the function, the second thread continues execution regardless. Fix this by moving thedata->ev_enable_statecheck inside of the critical section.Fixes fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")Reported-by sashiko <[email protected]>Signed-off-by Maxwell Doose <[email protected]>--- drivers/iio/imu/kmx61.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/imu/kmx61.c +0
-2@@ -942,11 +942,11 @@ 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 ;
drivers/iio/imu/kmx61.c static int kmx61_write_event_config(struct iio_dev *indio_dev,
Quoted 10 lines
drivers/iio/imu/kmx61.c +3
-2-mutex_lock (&data->lock); +if (state && data->ev_enable_state) + goto err_unlock; +if (!state && data->motion_trig_on) { data->ev_enable_state = false; goto err_unlock; --
This seems simple enough.
Signature · 3 lines
Kind regards CJD
On Tue, May 12, 2026 at 08:36:38PM -0500, Maxwell Doose wrote:
The Subject can be made better:
iio: imu: kmx61: Fix potential TOCTOU race in kmx61_write_event_config()
Quoted 8 lines
A time-of-check to time-of-use race condition exists in
kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifiesdata->ev_enable_stateto force returning from the function, the second thread continues execution regardless. Fix this by moving thedata->ev_enable_statecheck inside of the critical section.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
On Tue, May 12, 2026 at 08:36:38PM -0500, Maxwell Doose wrote:
The Subject can be made better:
iio: imu: kmx61: Fix potential TOCTOU race in
kmx61_write_event_config()
That all sounds good. Will wait for Jonathan to see if he just wants to merge as is, change the subject, or ask for v3.
best regards,
max
Quoted 14 lines
Quoted 8 lines
A time-of-check to time-of-use race condition exists in
kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifiesdata->ev_enable_stateto force returning from the function, the second thread continues execution regardless. Fix this by moving thedata->ev_enable_statecheck inside of the critical section.--
With Best Regards,
Andy Shevchenko
Jonathan Cameron
maintainerQuoted 9 lines · On Tue, 12 May 2026 20:36:38 -0500 Maxwell Doose <[email protected]> wrote:
A time-of-check to time-of-use race condition exists in
kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifiesdata->ev_enable_stateto force returning from the function, the second thread continues execution regardless. Fix this by moving thedata->ev_enable_statecheck inside of the critical section.
Quoted 4 lines
Fixes fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")Reported-by sashiko <[email protected]>Signed-off-by Maxwell Doose <[email protected]>
Sashiko has more to say.. Please take a look. I'm rather nervous about trying to fix the bugs it's finding around reference counting without anyone testing.
Maybe we can get enough confidence with emulation or stubbing.
https://sashiko.dev/#/patchset/20260513013638.147606-1-m32285159%40gmail.com
Quoted 23 lines
--- drivers/iio/imu/kmx61.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/imu/kmx61.c +3
-3@@ -942,11 +942,11 @@ 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) + goto err_unlock; +if (!state && data->motion_trig_on) { data->ev_enable_state = false; goto err_unlock;
Quoted 18 lines · Jonathan Cameron
Quoted 9 lines · On Tue, 12 May 2026 20:36:38 -0500 Maxwell Doose <[email protected]> wrote:
A time-of-check to time-of-use race condition exists in
kmx61_write_event_config(). If two threads enter the function at the same time, both threads may pass the check and get to the lock. Thus, when the first thread releases the lock allowing the second thread to start execution after the first thread modifiesdata->ev_enable_stateto force returning from the function, the second thread continues execution regardless. Fix this by moving thedata->ev_enable_statecheck inside of the critical section.Quoted 4 lines
Fixes fd3ae7a9f21c ("iio: imu: kmx61: Add support for any motion trigger")Reported-by sashiko <[email protected]>Signed-off-by Maxwell Doose <[email protected]>
Quoted 8 lines
Sashiko has more to say.. Please take a look. I'm rather nervous about trying to fix the bugs it's finding around reference counting without anyone testing.
Maybe we can get enough confidence with emulation or stubbing.
https://sashiko.dev/#/patchset/20260513013638.147606-1-m32285159%40gmail.com
Yea, unfortunately I don't have the hardware with me. If someone else wants to test then maybe...I haven't messed around with QEMU much so honestly I wouldn't know how to emulate such specific hardware.
best regards,
max
Quoted 24 lines
Quoted 23 lines
--- drivers/iio/imu/kmx61.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/imu/kmx61.c +3
-3@@ -942,11 +942,11 @@ 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) + goto err_unlock; +if (!state && data->motion_trig_on) { data->ev_enable_state = false; goto err_unlock;