From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Peter Hurley <peter@hurleysoftware.com>
Cc: Jiri Slaby <jslaby@suse.cz>, Alan Cox <alan@linux.intel.com>,
"H.J. Lu" <hjl.tools@gmail.com>, Josh Boyer <jwboyer@redhat.com>,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v3] pty: Fix input race when closing
Date: Mon, 13 Apr 2015 20:14:37 +0200 [thread overview]
Message-ID: <20150413181437.GA8144@kroah.com> (raw)
In-Reply-To: <1428945874-30893-1-git-send-email-peter@hurleysoftware.com>
On Mon, Apr 13, 2015 at 01:24:34PM -0400, Peter Hurley wrote:
> A read() from a pty master may mistakenly indicate EOF (errno == -EIO)
> after the pty slave has closed, even though input data remains to be read.
> For example,
>
> pty slave | input worker | pty master
> | |
> | | n_tty_read()
> pty_write() | | input avail? no
> add data | | sleep
> schedule worker --->| | .
> |---> flush_to_ldisc() | .
> pty_close() | fill read buffer | .
> wait for worker | wakeup reader --->| .
> | read buffer full? |---> input avail ? yes
> |<--- yes - exit worker | copy 4096 bytes to user
> TTY_OTHER_CLOSED <---| |<--- kick worker
> | |
>
> **** New read() before worker starts ****
>
> | | n_tty_read()
> | | input avail? no
> | | TTY_OTHER_CLOSED? yes
> | | return -EIO
>
> Several conditions are required to trigger this race:
> 1. the ldisc read buffer must become full so the input worker exits
> 2. the read() count parameter must be >= 4096 so the ldisc read buffer
> is empty
> 3. the subsequent read() occurs before the kicked worker has processed
> more input
>
> However, the underlying cause of the race is that data is pipelined, while
> tty state is not; ie., data already written by the pty slave end is not
> yet visible to the pty master end, but state changes by the pty slave end
> are visible to the pty master end immediately.
>
> Pipeline the TTY_OTHER_CLOSED state through input worker to the reader.
> 1. Introduce TTY_OTHER_DONE which is set by the input worker when
> TTY_OTHER_CLOSED is set and either the input buffers are flushed or
> input processing has completed. Readers/polls are woken when
> TTY_OTHER_DONE is set.
> 2. Reader/poll checks TTY_OTHER_DONE instead of TTY_OTHER_CLOSED.
> 3. A new input worker is started from pty_close() after setting
> TTY_OTHER_CLOSED, which ensures the TTY_OTHER_DONE state will be
> set if the last input worker is already finished (or just about to
> exit).
>
> Remove tty_flush_to_ldisc(); no in-tree callers.
>
> Fixes: 52bce7f8d4fc ("pty, n_tty: Simplify input processing on final close")
> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=96311
> BugLink: http://bugs.launchpad.net/bugs/1429756
> Cc: <stable@vger.kernel.org> # 3.19+
> Reported-by: Andy Whitcroft <apw@canonical.com>
> Reported-by: H.J. Lu <hjl.tools@gmail.com>
> Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
> ---
Thanks for this, it's a bit late at the moment for 4.1-rc1, but I'll get
it queued up after that.
thanks,
greg k-h
next prev parent reply other threads:[~2015-04-13 18:14 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1428629912-22611-1-git-send-email-peter@hurleysoftware.com>
2015-04-13 17:24 ` [PATCH v3] pty: Fix input race when closing Peter Hurley
2015-04-13 17:30 ` Peter Hurley
2015-04-13 18:14 ` Greg Kroah-Hartman [this message]
2015-05-05 10:10 ` 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=20150413181437.GA8144@kroah.com \
--to=gregkh@linuxfoundation.org \
--cc=alan@linux.intel.com \
--cc=hjl.tools@gmail.com \
--cc=jslaby@suse.cz \
--cc=jwboyer@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peter@hurleysoftware.com \
--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;
as well as URLs for NNTP newsgroup(s).