staging: Fix atomicity violation in get_serial_info()
Subsystems (4)
PATCH 1/1 staging: Fix atomicity violation in get_serial_info()
Atomicity violation occurs during consecutive reads of the members of gb_tty. Consider a scenario where, because the consecutive reads of gb_tty members are not protected by a lock, the value of gb_tty may still be changing during the read process.
gb_tty->port.close_delay and gb_tty->port.closing_wait are updated together, such as in the set_serial_info() function. If during the read process, gb_tty->port.close_delay and gb_tty->port.closing_wait are still being updated, it is possible that gb_tty->port.close_delay is updated while gb_tty->port.closing_wait is not. In this case, the code first reads gb_tty->port.close_delay and then gb_tty->port.closing_wait. A new gb_tty->port.close_delay and an old gb_tty->port.closing_wait could be read. Such values, whether before or after the update, should not coexist as they represent an intermediate state.
This could result in a mismatch of the values read for gb_tty->minor, gb_tty->port.close_delay, and gb_tty->port.closing_wait, which in turn could cause ss->close_delay and ss->closing_wait to be mismatched.
To address this issue, we have enclosed all sequential read operations of the gb_tty variable within a lock. This ensures that the value of gb_tty remains unchanged throughout the process, guaranteeing its validity.
This possible bug is found by an experimental static analysis tool developed by our team. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including data races and atomicity violations.
--- drivers/staging/greybus/uart.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/staging/greybus/uart.c +2 -0
@@ -595,12 +595,14 @@ static int get_serial_info(struct tty_struct *tty, {struct gb_tty *gb_tty = tty->driver_data; +mutex_lock (&gb_tty->port.mutex); ss->line = gb_tty->minor; ss->close_delay =jiffies_to_msecs (gb_tty->port.close_delay) /10 ; ss->closing_wait = gb_tty->port.closing_wait ==ASYNC_CLOSING_WAIT_NONE ?ASYNC_CLOSING_WAIT_NONE :jiffies_to_msecs (gb_tty->port.closing_wait) /10 ; +mutex_unlock (&gb_tty->port.mutex);return 0 ; }
Signature · 1 line
2.34.1
Quoted 24 lines · Qiu-ji Chen
Atomicity violation occurs during consecutive reads of the members of
gb_tty. Consider a scenario where, because the consecutive reads ofgb_ttymembers are not protected by a lock, the value ofgb_ttymay still be changing during the read process.
gb_tty->port.close_delayandgb_tty->port.closing_waitare updated together, such as in theset_serial_info()function. If during the read process,gb_tty->port.close_delayandgb_tty->port.closing_waitare still being updated, it is possible thatgb_tty->port.close_delayis updated whilegb_tty->port.closing_waitis not. In this case, the code first readsgb_tty->port.close_delayand thengb_tty->port.closing_wait. A newgb_tty->port.close_delayand an oldgb_tty->port.closing_waitcould be read. Such values, whether before or after the update, should not coexist as they represent an intermediate state.This could result in a mismatch of the values read for
gb_tty->minor,gb_tty->port.close_delay, andgb_tty->port.closing_wait, which in turn could causess->close_delayandss->closing_waitto be mismatched.To address this issue, we have enclosed all sequential read operations of the
gb_ttyvariable within a lock. This ensures that the value ofgb_ttyremains unchanged throughout the process, guaranteeing its validity.
Quoted 6 lines
This possible bug is found by an experimental static analysis tool developed by our team. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including data races and atomicity violations.
Ideally a commit message should say what the bug looks like to the user. Obviously when you're doing static analysis and not using the code, it's more difficult to tell the impact.
I would say that this commit message is confusing and makes it seem like a bigger deal than it is. The "ss" struct is information that we're going to send to the user. It's not used again in the kernel.
Could you re-write the commit message to say something like, "Our static checker found a bug where set serial takes a mutex and get serial doesn't. Fortunately, the impact of this is relatively minor. It doesn't cause a crash or anything. If the user races set serial and get serial there is a chance that the get serial information will be garbage."
regards,
dan carpenter
Johan Hovold
maintainerQuoted 16 lines · Qiu-ji Chen
Atomicity violation occurs during consecutive reads of the members of
gb_tty. Consider a scenario where, because the consecutive reads ofgb_ttymembers are not protected by a lock, the value ofgb_ttymay still be changing during the read process.
gb_tty->port.close_delayandgb_tty->port.closing_waitare updated together, such as in theset_serial_info()function. If during the read process,gb_tty->port.close_delayandgb_tty->port.closing_waitare still being updated, it is possible thatgb_tty->port.close_delayis updated whilegb_tty->port.closing_waitis not. In this case, the code first readsgb_tty->port.close_delayand thengb_tty->port.closing_wait. A newgb_tty->port.close_delayand an oldgb_tty->port.closing_waitcould be read. Such values, whether before or after the update, should not coexist as they represent an intermediate state.
Quoted 1 line
This could result in a mismatch of the values read for
gb_tty->minor,
No, gb_tty minor is only set at probe().
Quoted 2 lines
gb_tty->port.close_delay, andgb_tty->port.closing_wait, which in turn could causess->close_delayandss->closing_waitto be mismatched.
Sure, but that's a pretty minor issue as Dan already pointed out.
Quoted 11 lines
To address this issue, we have enclosed all sequential read operations of the
gb_ttyvariable within a lock. This ensures that the value ofgb_ttyremains unchanged throughout the process, guaranteeing its validity.This possible bug is found by an experimental static analysis tool developed by our team. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including data races and atomicity violations.
Fixes b71e571adaa5 ("staging: greybus: uart: fix TIOCSSERIAL jiffies conversions")
And this obviously isn't the correct commit to blame. Please be more careful.
Quoted 1 line
Since this is unlikely to cause any issues for a user, I don't think stable backport is warranted either.
Quoted 10 lines
Signed-off-by Qiu-ji Chen <[email protected]>--- drivers/staging/greybus/uart.c | 2 ++ 1 file changed, 2 insertions(+)drivers/staging/greybus/uart.c +0
-0
drivers/staging/greybus/uart.c static int get_serial_info(struct tty_struct *tty, · line 599
Quoted 5 lines
drivers/staging/greybus/uart.c +1
-0@@ -595,12 +595,14 @@ static int get_serial_info(struct tty_struct *tty, {struct gb_tty *gb_tty = tty->driver_data; +mutex_lock (&gb_tty->port.mutex); ss->line = gb_tty->minor;
gb_tty is not protected by the port mutex.
drivers/staging/greybus/uart.c static int get_serial_info(struct tty_struct *tty, · line 608
Quoted 9 lines
drivers/staging/greybus/uart.c +1
-0ss->close_delay =jiffies_to_msecs (gb_tty->port.close_delay) /10 ; ss->closing_wait = gb_tty->port.closing_wait ==ASYNC_CLOSING_WAIT_NONE ?ASYNC_CLOSING_WAIT_NONE :jiffies_to_msecs (gb_tty->port.closing_wait) /10 ; +mutex_unlock (&gb_tty->port.mutex);return 0 ; }
Johan