From: Jiri Slaby <jslaby@suse.cz>
To: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: Emil Goode <emilgoode@gmail.com>,
gregkh@linuxfoundation.org, kernel-janitors@vger.kernel.org,
linux-kernel@vger.kernel.org, hyc@symas.com,
Jiri Slaby <jirislaby@gmail.com>
Subject: Re: [PATCH] drivers/tty: Use get_user instead of dereferencing user pointer
Date: Sat, 21 Apr 2012 11:04:04 +0200 [thread overview]
Message-ID: <4F927804.4090208@suse.cz> (raw)
In-Reply-To: <20120420220010.0fa29bee@pyramind.ukuu.org.uk>
On 04/20/2012 11:00 PM, Alan Cox wrote:
> It's not simple however, so can anyone work out or remember wtf the code
> should be doing ???
Huh.
The code was added by:
commit 26df6d13406d1a53b0bda08bd712f1924affd7cd
Author: hyc@symas.com <hyc@symas.com>
Date: Tue Jun 22 10:14:49 2010 -0700
tty: Add EXTPROC support for LINEMODE
====
The code is now:
retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
n -= retval;
tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
spin_lock_irqsave(&tty->read_lock, flags);
tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
tty->read_cnt -= n;
if (L_EXTPROC(tty) && tty->icanon && n == 1) {
if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
n--;
}
====
n after "n -= retval" means number of successfully copied chars. So the
test "n == 1" along with "!tty->read_cnt" actually should ensure we
copied everything and that is exactly one char. Further we test if that
one is EOF. If so, ignore that char by pretending we copied nothing.
However the implementation does not count with buffer wrapped like:
EOF..........................something
^----- tail
Here, the first call to copy_from_read_buf copies "something" and the
second one is to copy single EOF. But that would be ignored! Is this
expected?
So to fix the user buffer dereference, the following diff should help.
In any case the wrapped buffer is still to be fixed... (Or ignored.)
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -1630,6 +1630,7 @@ static int copy_from_read_buf(struct tty_struct *tty,
int retval;
size_t n;
unsigned long flags;
+ bool is_eof;
retval = 0;
spin_lock_irqsave(&tty->read_lock, flags);
@@ -1639,15 +1640,15 @@ static int copy_from_read_buf(struct tty_struct
*tty,
if (n) {
retval = copy_to_user(*b,
&tty->read_buf[tty->read_tail], n);
n -= retval;
+ is_eof = n == 1 &&
+ tty->read_buf[tty->read_tail] == EOF_CHAR(tty);
tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
spin_lock_irqsave(&tty->read_lock, flags);
tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
tty->read_cnt -= n;
/* Turn single EOF into zero-length read */
- if (L_EXTPROC(tty) && tty->icanon && n == 1) {
- if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
- n--;
- }
+ if (L_EXTPROC(tty) && tty->icanon && is_eof &&
!tty->read_cnt)
+ n = 0;
spin_unlock_irqrestore(&tty->read_lock, flags);
*b += n;
*nr -= n;
thanks,
--
js
suse labs
next prev parent reply other threads:[~2012-04-21 9:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-20 15:52 [PATCH] drivers/tty: Use get_user instead of dereferencing user pointer Emil Goode
2012-04-20 21:00 ` Alan Cox
2012-04-21 9:04 ` Jiri Slaby [this message]
2012-04-21 14:04 ` Howard Chu
2012-04-21 14:25 ` Howard Chu
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=4F927804.4090208@suse.cz \
--to=jslaby@suse.cz \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=emilgoode@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=hyc@symas.com \
--cc=jirislaby@gmail.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@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