linux-staging.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: greybus: fix a possible data-inconsistency due to data race in get_serial_info()
@ 2023-06-26  8:43 Tuo Li
  2023-07-03 13:23 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Tuo Li @ 2023-06-26  8:43 UTC (permalink / raw)
  To: dtwlin, johan, elder, gregkh
  Cc: greybus-dev, linux-staging, linux-kernel, baijiaju1990, Tuo Li,
	BassCheck

The variables gb_tty->port.close_delay and gb_tty->port.closing_wait are
ofter accessed together while holding the lock gb_tty->port.mutex. Here is 
an example in set_serial_info():

  mutex_lock(&gb_tty->port.mutex);
  ...
  gb_tty->port.close_delay = close_delay;
  gb_tty->port.closing_wait = closing_wait;
  ...
  mutex_unlock(&gb_tty->port.mutex);

However, they are accessed without holding the lock gb_tty->port.mutex when
are accessed in get_serial_info():

  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;

In my opinion, this may be a harmful race, because ss->close_delay can be
inconsistent with ss->closing_wait if gb_tty->port.close_delay and 
gb_tty->port.closing_wait are updated by another thread after the 
assignment to ss->close_delay.
Besides, the select operator may return wrong value if 
gb_tty->port.closing_wait is updated right after the condition is 
calculated.

To fix this possible data-inconsistency caused by data race, a lock and 
unlock pair is added when accessing different fields of gb_tty->port.

Reported-by: BassCheck <bass@buaa.edu.cn>
Signed-off-by: Tuo Li <islituo@gmail.com>
---
 drivers/staging/greybus/uart.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index 20a34599859f..b8875517ea6a 100644
--- a/drivers/staging/greybus/uart.c
+++ b/drivers/staging/greybus/uart.c
@@ -596,12 +596,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;
 }
-- 
2.34.1


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

* Re: [PATCH] staging: greybus: fix a possible data-inconsistency due to data race in get_serial_info()
  2023-06-26  8:43 [PATCH] staging: greybus: fix a possible data-inconsistency due to data race in get_serial_info() Tuo Li
@ 2023-07-03 13:23 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2023-07-03 13:23 UTC (permalink / raw)
  To: Tuo Li
  Cc: dtwlin, johan, elder, greybus-dev, linux-staging, linux-kernel,
	baijiaju1990, BassCheck

On Mon, Jun 26, 2023 at 04:43:39PM +0800, Tuo Li wrote:
> The variables gb_tty->port.close_delay and gb_tty->port.closing_wait are
> ofter accessed together while holding the lock gb_tty->port.mutex. Here is 
> an example in set_serial_info():
> 
>   mutex_lock(&gb_tty->port.mutex);
>   ...
>   gb_tty->port.close_delay = close_delay;
>   gb_tty->port.closing_wait = closing_wait;
>   ...
>   mutex_unlock(&gb_tty->port.mutex);
> 
> However, they are accessed without holding the lock gb_tty->port.mutex when
> are accessed in get_serial_info():
> 
>   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;
> 
> In my opinion, this may be a harmful race, because ss->close_delay can be
> inconsistent with ss->closing_wait if gb_tty->port.close_delay and 
> gb_tty->port.closing_wait are updated by another thread after the 
> assignment to ss->close_delay.

And how can that happen?

Also you have trailing whitespace in your changelog text :(

> Besides, the select operator may return wrong value if 
> gb_tty->port.closing_wait is updated right after the condition is 
> calculated.
> 
> To fix this possible data-inconsistency caused by data race, a lock and 
> unlock pair is added when accessing different fields of gb_tty->port.
> 
> Reported-by: BassCheck <bass@buaa.edu.cn>

As per the documentation for research tools like this, you need to
explain how this was tested.

thanks,

greg k-h

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

end of thread, other threads:[~2023-07-03 13:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-26  8:43 [PATCH] staging: greybus: fix a possible data-inconsistency due to data race in get_serial_info() Tuo Li
2023-07-03 13:23 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).