public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Gui-Dong Han <2045gemini@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Jiri Slaby <jirislaby@kernel.org>,
	Tony Lindgren <tony@atomide.com>,
	 l.sanfilippo@kunbus.com, john.ogness@linutronix.de,
	tglx@linutronix.de,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 LKML <linux-kernel@vger.kernel.org>,
	 linux-serial <linux-serial@vger.kernel.org>,
	baijiaju1990@outlook.com,  stable@vger.kernel.org
Subject: Re: [PATCH] serial: core: Fix double fetch in uart_throttle/uart_unthrottle
Date: Fri, 12 Jan 2024 16:05:34 +0200 (EET)	[thread overview]
Message-ID: <3bbabf34-1eba-8983-439e-f23e811e80a8@linux.intel.com> (raw)
In-Reply-To: <20240112121844.17580-1-2045gemini@gmail.com>

On Fri, 12 Jan 2024, Gui-Dong Han wrote:

> In uart_throttle() and uart_unthrottle():
>     if (port->status & mask) {
>         port->ops->throttle/unthrottle(port);
>         mask &= ~port->status;
>     }
>     // Code segment utilizing the mask value to determine UART behavior
> 
> In uart_change_line_settings():
>     uart_port_lock_irq(uport);
>     // Code segment responsible for updating uport->status
>     uart_port_unlock_irq(uport);
> 
> In the uart_throttle() and uart_unthrottle() functions, there is a double
> fetch issue due to concurrent execution with uart_change_line_settings().
> In uart_throttle() and uart_unthrottle(), the check
> if (port->status & mask) is made, followed by mask &= ~port->status,
> where the relevant bits are cleared. However, port->status may be modified
> in uart_change_line_settings(). The current implementation does not ensure
> atomicity in the access and modification of port->status and mask. This
> can result in mask being updated based on a modified port->status value,
> leading to improper UART actions.
> 
> This possible bug is found by an experimental static analysis tool
> developed by our team, BassCheck[1]. 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. The above
> possible bug is reported when our tool analyzes the source code of
> Linux 5.17.
> 
> To resolve this double fetch, it is suggested to add a uart_port_lock pair
> in uart_throttle() and uart_unthrottle(). With this patch applied, our
> tool no longer reports the bug, with the kernel configuration allyesconfig
> for x86_64. Due to the absence of the requisite hardware, we are unable to
> conduct runtime testing of the patch. Therefore, our verification is
> solely based on code logic analysis.
> 
> [1] https://sites.google.com/view/basscheck/
> 
> Fixes: 391f93f2ec9f ("serial: core: Rework hw-assisted flow control support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Gui-Dong Han <2045gemini@gmail.com>
> ---
>  drivers/tty/serial/serial_core.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index 80085b151b34..9d905fdf2843 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c
> @@ -723,11 +723,13 @@ static void uart_throttle(struct tty_struct *tty)
>  		mask |= UPSTAT_AUTOXOFF;
>  	if (C_CRTSCTS(tty))
>  		mask |= UPSTAT_AUTORTS;
> -
> +
> +	uart_port_lock_irq(port);
>  	if (port->status & mask) {
>  		port->ops->throttle(port);
>  		mask &= ~port->status;
>  	}
> +	uart_port_unlock_irq(port);
>  
>  	if (mask & UPSTAT_AUTORTS)
>  		uart_clear_mctrl(port, TIOCM_RTS);
> @@ -753,10 +755,12 @@ static void uart_unthrottle(struct tty_struct *tty)
>  	if (C_CRTSCTS(tty))
>  		mask |= UPSTAT_AUTORTS;
>  
> +	uart_port_lock_irq(port);
>  	if (port->status & mask) {
>  		port->ops->unthrottle(port);
>  		mask &= ~port->status;
>  	}
> +	uart_port_unlock_irq(port);
>  
>  	if (mask & UPSTAT_AUTORTS)
>  		uart_set_mctrl(port, TIOCM_RTS);

Hi,

This is very bogus "fix". While change to the local variable gets 
"protected", uart_change_line_settings() can race after unlock and the 
value held in mask is again stale.

If, and it's a big if, this is a real problem, the patch does not fix 
anything! It proves your tool is flawed because it doesn't detect the 
race with uart_change_line_settings() issue still exists after this 
non-fix.

So NAK from me. Please provide a real fix instead if you think there is
a real issue.

Also, don't use vague wording like "leading to improper UART action" but 
describe precisely what goes wrong!

-- 
 i.


  parent reply	other threads:[~2024-01-12 14:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-12 12:18 [PATCH] serial: core: Fix double fetch in uart_throttle/uart_unthrottle Gui-Dong Han
2024-01-12 13:28 ` John Ogness
2024-01-12 13:45 ` Greg KH
2024-01-12 14:05 ` Ilpo Järvinen [this message]
2024-01-12 18:52   ` Gui-Dong Han

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=3bbabf34-1eba-8983-439e-f23e811e80a8@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=2045gemini@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=baijiaju1990@outlook.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=john.ogness@linutronix.de \
    --cc=l.sanfilippo@kunbus.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tony@atomide.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox