From: Greg KH <gregkh@linuxfoundation.org>
To: Tuo Li <islituo@gmail.com>
Cc: jirislaby@kernel.org, baijiaju1990@gmail.com,
linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org
Subject: Re: [PATCH] tty: n_gsm: fix a possible data race in gsmtty_open
Date: Thu, 4 Jul 2024 10:49:20 +0200 [thread overview]
Message-ID: <2024070411-unrevised-sash-927f@gregkh> (raw)
In-Reply-To: <20240704082658.189577-1-islituo@gmail.com>
On Thu, Jul 04, 2024 at 04:26:58PM +0800, Tuo Li wrote:
> In the function gsmtty_open(), the counter port->count is updated without
> holding the lock port->lock, which may lead to a data race.
>
> This possible data race is found by an experimental static analysis tool.
> This tool analyzes the locking APIs to deduce locking rules about which
> variable should be protected by which lock. And then the tool checks
> whether a given variable access violates the deduced locking rules, to
> detect data races. The above possible data race is reported, when the tool
> analyzes the source code of Linux 6.2.
>
> Referring to the function tty_port_open() in tty_port.c:
> spin_lock_irq(&port->lock);
> ++port->count;
> spin_unlock_irq(&port->lock);
>
> we add a lock/unlock pair to protect the access to port->count, to fix
> this possible data race.
>
> Fixes: 86176ed90545 ("TTY: n_gsm, use tty_port_install")
> Signed-off-by: Tuo Li <islituo@gmail.com>
> ---
> drivers/tty/n_gsm.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> index be35f7334ecd..b709f1a6bd2d 100644
> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -4307,7 +4307,9 @@ static int gsmtty_open(struct tty_struct *tty, struct file *filp)
> struct gsm_dlci *dlci = tty->driver_data;
> struct tty_port *port = &dlci->port;
>
> + spin_lock_irq(&port->lock);
> port->count++;
> + spin_unlock_irq(&port->lock);
Open counts are almost never a good idea, especially like this. Please
fix to use a proper reference count instead which will fix this issue.
Also, you mention kernel 6.2, which is VERY old, especially for this
driver, please redo this against the latest kernel tree which has lots
and lots of fixes for this code in it since that obsolete release.
And finally, as you are using an automated tool, you need to describe it
better, see the documentation for how to do so.
thanks,
greg k-h
prev parent reply other threads:[~2024-07-04 8:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-04 8:26 [PATCH] tty: n_gsm: fix a possible data race in gsmtty_open Tuo Li
2024-07-04 8:49 ` Greg KH [this message]
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=2024070411-unrevised-sash-927f@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=baijiaju1990@gmail.com \
--cc=islituo@gmail.com \
--cc=jirislaby@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).