linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Hurley <peter@hurleysoftware.com>
To: Maximiliano Curia <maxy@debian.org>
Cc: Margarita Manterola <margamanterola@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jslaby@suse.cz>,
	Linux kernel <linux-kernel@vger.kernel.org>
Subject: Re: Large pastes into readline enabled programs causes breakage from v2.6.31 onwards
Date: Mon, 19 Aug 2013 08:25:18 -0400	[thread overview]
Message-ID: <52120EAE.5060906@hurleysoftware.com> (raw)
In-Reply-To: <20130808175839.GB21618@gnuservers.com.ar>

On 08/08/2013 01:58 PM, Maximiliano Curia wrote:
> Hi,
>
>> n_tty_set_room() in drivers/tty/n_tty.c (3.10 mainline)
>
>>  From n_tty_set_room():
>
>> 	/*
>> 	 * If we are doing input canonicalization, and there are no
>> 	 * pending newlines, let characters through without limit, so
>> 	 * that erase characters will be handled.  Other excess
>> 	 * characters will be beeped.
>> 	 */
>> 	if (left <= 0)
>> 		left = ldata->icanon && !ldata->canon_data;
>> 	old_left = tty->receive_room;
>> 	tty->receive_room = left;
>
> I took a long look at this code and thought about how it could be made to work
> for readline's case and also for the canonical readers.  I came up with this
> simple patch:
>
> diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
> index 4bf0fc0..2ba7f4e 100644
> --- a/drivers/tty/n_tty.c
> +++ b/drivers/tty/n_tty.c
> @@ -149,7 +149,8 @@ static int set_room(struct tty_struct *tty)
>           * characters will be beeped.
>           */
>          if (left <= 0)
> -               left = ldata->icanon && !ldata->canon_data;
> +               if (waitqueue_active(&tty->read_wait))
> +                       left = ldata->icanon && !ldata->canon_data;
>          old_left = tty->receive_room;
>          tty->receive_room = left;
>
> This is of course just an idea, but I tested this and it worked correctly for
> the cases I was testing.
>
> The effect of this patch is that when there is a canonical reader waiting for
> input, it maintains the previous behavior, but when there's no reader (like
> when readline is changing modes), it blocks and doesn't lose any characters.

Apologies for taking so long to reply.

My primary concern is canonical readers not become stuck with a full
read buffer, even with bogus input data (IOW, that an error condition will
not prevent a reader from making forward progress). I believe that won't
happen with this change, but what I really need in this case is a detailed
analysis from you of why that won't happen. That analysis should be in
the patch changelog. (Feel free to send me private mail if you need help
preparing a patch.)

And the patch above has a bug that allows a negative 'left' to be
assigned to tty->receive_room which will be interpreted as a very large
positive value.

This approach still has several drawbacks.

1) Since additional state is reset when the termios is changed by
readline(), the canonical line buffer state will be bogus.
This renders the termios change by readline() pointless; the
caller will not be able to retrieve expected input properly.

2) Since the input data is interpreted with the current termios when
data is received, any embedded control characters will not be
interpreted properly; again, the caller will not be able to retrieve
expected input properly.

> Another approach would be to recalculate the size of canon_data when the mode
> is changed, but this would probably be much more invasive, and awfully less
> efficient since it would imply going through the buffer.

This approach is not possible prior to linux-next since the input worker
thread and the reader thread are not locked out of access to the read buffer
while changing the termios.

And while rescanning the read buffer is possible in linux-next (eg, to
compute the read_flags bitmap indicating the position of NLs), this doesn't
address embedded control characters not being reinterpreted. And completely
reinterpreting the read buffer makes interpreting when receiving pointless.

> What do you think? Is the proposed solution, or something along those lines,
> acceptable?

I'm wondering if this problem might be best addressed on the paste side
instead of the read side. Although this wouldn't be a magic bullet, it
would be easier to control when more paste data is added.

Regards,
Peter Hurley



  parent reply	other threads:[~2013-08-19 12:25 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-25 11:29 Large pastes into readline enabled programs causes breakage from v2.6.31 onwards Margarita Manterola
2013-07-25 23:09 ` Peter Hurley
2013-07-30 12:41   ` Maximiliano Curia
     [not found]   ` <20130730124117.41DC55E4006@freak.gnuservers.com.ar>
2013-07-30 16:08     ` Peter Hurley
2013-08-08 17:58       ` Maximiliano Curia
2013-08-17 15:28         ` Pavel Machek
2013-08-17 22:57           ` Margarita Manterola
2013-08-18  8:08             ` Geert Uytterhoeven
2013-09-03  5:17             ` Arkadiusz Miskiewicz
2013-10-24 16:00               ` Arkadiusz Miskiewicz
2013-10-29 13:50                 ` Maximiliano Curia
2013-10-30 11:21                   ` Peter Hurley
2013-11-17 18:29                     ` Pavel Machek
2013-11-17 21:38                       ` Margarita Manterola
2013-11-21  5:04                       ` Peter Hurley
2013-11-22 12:57                         ` Peter Hurley
2013-11-24  0:29                           ` One Thousand Gnomes
2013-11-24 11:55                             ` Peter Hurley
2013-11-26  1:16                               ` Peter Hurley
2013-12-03  0:18                                 ` Peter Hurley
2013-12-03  9:01                                   ` Stas Sergeev
2013-12-03 17:00                                     ` Peter Hurley
2013-12-03 19:18                                       ` Stas Sergeev
2013-12-03 23:53                                         ` Peter Hurley
2013-12-04 18:57                                           ` Stas Sergeev
2013-12-09 14:50                                             ` [PATCH v3] n_tty: Fix buffer overruns with larger-than-4k pastes Peter Hurley
     [not found]                                               ` <52A5EF3F.2070805@list.ru>
2013-12-09 17:10                                                 ` Peter Hurley
2013-12-10  6:15                                                   ` Stas Sergeev
2013-12-10 22:05                                                     ` Peter Hurley
2013-12-10 22:12                                                       ` [PATCH v4] " Peter Hurley
2013-12-17  0:57                                                         ` Greg Kroah-Hartman
2013-12-17  1:24                                                           ` Peter Hurley
2013-12-18 11:48                                                             ` Henrique de Moraes Holschuh
2013-12-18 13:41                                                               ` Peter Hurley
2014-01-28 12:03                                             ` Large pastes into readline enabled programs causes breakage from v2.6.31 onwards Pavel Machek
2014-01-28 12:17                                               ` Stas Sergeev
2014-01-28 13:31                                                 ` Peter Hurley
2013-08-19 12:25         ` Peter Hurley [this message]
2013-09-03 21:12           ` Maximiliano Curia
2013-09-12  1:36             ` Peter Hurley

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=52120EAE.5060906@hurleysoftware.com \
    --to=peter@hurleysoftware.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=margamanterola@gmail.com \
    --cc=maxy@debian.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).