From: Greg KH <gregkh@linuxfoundation.org>
To: Gui-Dong Han <2045gemini@gmail.com>
Cc: jirislaby@kernel.org, linux-kernel@vger.kernel.org,
linux-serial@vger.kernel.org, baijiaju1990@outlook.com,
stable@vger.kernel.org
Subject: Re: [PATCH] tty: fix atomicity violation in n_tty_read
Date: Fri, 12 Jan 2024 14:48:18 +0100 [thread overview]
Message-ID: <2024011212-disbelief-respect-5230@gregkh> (raw)
In-Reply-To: <20240112125801.2650-1-2045gemini@gmail.com>
On Fri, Jan 12, 2024 at 08:58:01PM +0800, Gui-Dong Han wrote:
> In n_tty_read():
> if (packet && tty->link->ctrl.pktstatus) {
> ...
> spin_lock_irq(&tty->link->ctrl.lock);
> cs = tty->link->ctrl.pktstatus;
> tty->link->ctrl.pktstatus = 0;
> spin_unlock_irq(&tty->link->ctrl.lock);
> *kb++ = cs;
> ...
>
> In n_tty_read() function, there is a potential atomicity violation issue.
> The tty->link->ctrl.pktstatus might be set to 0 after being checked, which
> could lead to incorrect values in the kernel space buffer
> pointer (kb/kbuf). The check if (packet && tty->link->ctrl.pktstatus)
> occurs outside the spin_lock_irq(&tty->link->ctrl.lock) block. This may
> lead to tty->link->ctrl.pktstatus being altered between the check and the
> lock, causing *kb++ = cs; to be assigned with a zero pktstatus value.
>
> 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.
Again, we can't do anything with 5.17 patches :(
> To resolve this atomicity issue, it is suggested to move the condition
> check if (packet && tty->link->ctrl.pktstatus) inside the spin_lock block.
> 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: 64d608db38ff ("tty: cumulate and document tty_struct::ctrl* members")
That is not where this code came from :(
> Cc: stable@vger.kernel.org
> Signed-off-by: Gui-Dong Han <2045gemini@gmail.com>
> ---
> drivers/tty/n_tty.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
> index f252d0b5a434..df54ab0c4d8c 100644
> --- a/drivers/tty/n_tty.c
> +++ b/drivers/tty/n_tty.c
> @@ -2222,19 +2222,23 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
> add_wait_queue(&tty->read_wait, &wait);
> while (nr) {
> /* First test for status change. */
> + spin_lock_irq(&tty->link->ctrl.lock);
What is this lock going to do for the performance? The n_tty_read path
is VERY tricky, and heavily used and tested, without a real reproducer
or proof of a bug here, we are going to be very loath to change anything
for obvious reasons.
Also, how was this tested?
thanks,
greg k-h
next prev parent reply other threads:[~2024-01-12 13:48 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-12 12:58 [PATCH] tty: fix atomicity violation in n_tty_read Gui-Dong Han
2024-01-12 13:48 ` Greg KH [this message]
2024-01-12 16:59 ` Gui-Dong Han
2024-01-14 19:43 ` Theodore Ts'o
2024-01-16 8:29 ` kernel test robot
2024-01-16 10:14 ` Ilpo Järvinen
2024-02-01 9:02 ` This is classified as spam [was: [PATCH] tty: fix atomicity violation in n_tty_read] Jiri Slaby
2024-02-01 14:23 ` Greg KH
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=2024011212-disbelief-respect-5230@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=2045gemini@gmail.com \
--cc=baijiaju1990@outlook.com \
--cc=jirislaby@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=stable@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