All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 00/11] tty: Fix buffer work access-after-free
@ 2012-12-14 18:22 Peter Hurley
  2012-12-14 18:22 ` [PATCH v2 01/11] tty: debug buffer work race with tty free Peter Hurley
                   ` (12 more replies)
  0 siblings, 13 replies; 181+ messages in thread
From: Peter Hurley @ 2012-12-14 18:22 UTC (permalink / raw)
  To: Alan Cox, Jiri Slaby
  Cc: linux-serial, Greg Kroah-Hartman, Ilya Zykov, Sasha Levin,
	linux-kernel, Peter Hurley

I wasn't sure if this is something to squeeze into 3.8, so don't yell
if not. At least Sasha can apply this and re-test against trinity.

Changes in v2:

- Please review "tty: Don't flush buffer when closing ldisc".
  This patch replaces the earlier
  "tty: Don't reschedule buffer work while closing". The text of
  this commit details why not calling n_tty_flush_buffer() is the
  correct thing to do, so I won't repeat it here.

- Jiri's debug patch "tty: debug buffer work race with tty free"
  has been included (albeit a slightly different version)
  Jiri, please sign off (or point out what you'd like changed).

- The test jig has been included in the commit message for
  "tty: Don't flush buffer when closing ldisc" as Alan requested.

- Ilya Zykov was added as the Signed-off-by: for the test jig in
  that same commit message.

- Sasha Levin was added as the Reported-by: in that same patch.


This patch series addresses the causes of flush_to_ldisc accessing
the tty after freeing.

The most common cause stems from the n_tty_close() path spuriously
scheduling buffer work, when the ldisc has already been halted.
This is fixed in 'tty: Don't flush buffer when closing ldisc'

The other causes have a central theme: incorrect order-of-operations
when halting a line discipline. In general, to prevent buffer work
from being scheduled requires:
  1. Disallowing further ldisc references
  2. Waiting for all existing ldisc references to be released
  3. Cancelling existing buffer work
If the wait takes place _after_ cancellation, then new work
can be scheduled by existing holder(s) of ldisc reference(s). That's
bad.

Halting the line discipline is performed when,
 - hanging up the tty (tty_ldisc_hangup())
 - TIOCSETD ioctl (tty_set_ldisc())
 - finally closing the tty (pair) (tty_ldisc_release())

Concurrent halts are governed by the following requirements:
1. tty_ldisc_release is not concurrent with the other two and so
   does not need lock or state protection during the ldiscs halt.
2. Accesses through tty->ldisc must be protected by the ldisc_mutex.
   The wait operation checks the user count (ldisc references)
   in tty->ldisc->users.
3. tty_set_ldisc() reschedules buffer work that was pending when
   the ldiscs were halted. That must be an atomic operation in
   conjunction with re-enabling the ldisc -- which necessitates
   locking concurrent halts (tty_ldisc_release is exempt here)
4. The legacy mutex cannot be held while waiting for ldisc
   reference(s) release -or- for cancelling buffer work.
5. Because of #4, the legacy mutex must be dropped prior to or
   during the halt. Which means reacquiring after the halt. But
   to preserve lock order the ldisc_mutex must be dropped and
   reacquired after reacquiring the legacy mutex.
6. Because of #5, the ldisc state may have changed while the
   ldisc mutex was dropped.

Note: this series does not include the lock correction initially
reported on by Sebastian Andrzej Siewior <bigeasy@linutronix.de> here
https://lkml.org/lkml/2012/11/21/267 . I commented on the latest
version here https://lkml.org/lkml/2012/12/3/362


Peter Hurley (11):
  tty: debug buffer work race with tty free
  tty: WARN if buffer work racing with tty free
  tty: Add diagnostic for halted line discipline
  tty: Refactor n_tty_flush_buffer
  tty: Don't flush buffer when closing ldisc
  tty: Refactor wait for ldisc refs out of tty_ldisc_hangup()
  tty: Remove unnecessary re-test of ldisc ref count
  tty: Fix ldisc halt sequence on hangup
  tty: Strengthen no-subsequent-use guarantee of tty_ldisc_halt()
  tty: Remove unnecessary buffer work flush
  tty: Halt both ldiscs concurrently

 drivers/tty/n_tty.c      |  34 +++++++---
 drivers/tty/pty.c        |   3 +-
 drivers/tty/tty_buffer.c |   5 +-
 drivers/tty/tty_io.c     |   4 +-
 drivers/tty/tty_ldisc.c  | 171 +++++++++++++++++++++++++++++------------------
 include/linux/tty.h      |   1 +
 6 files changed, 139 insertions(+), 79 deletions(-)

-- 
1.8.0.1


^ permalink raw reply	[flat|nested] 181+ messages in thread

end of thread, other threads:[~2013-03-26 23:48 UTC | newest]

Thread overview: 181+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-14 18:22 [PATCH v2 00/11] tty: Fix buffer work access-after-free Peter Hurley
2012-12-14 18:22 ` [PATCH v2 01/11] tty: debug buffer work race with tty free Peter Hurley
2012-12-14 18:22 ` [PATCH v2 02/11] tty: WARN if buffer work racing " Peter Hurley
2012-12-14 18:22 ` [PATCH v2 03/11] tty: Add diagnostic for halted line discipline Peter Hurley
2012-12-14 18:22 ` [PATCH v2 04/11] tty: Refactor n_tty_flush_buffer Peter Hurley
2012-12-14 18:22 ` [PATCH v2 05/11] tty: Don't flush buffer when closing ldisc Peter Hurley
2012-12-14 18:22 ` [PATCH v2 06/11] tty: Refactor wait for ldisc refs out of tty_ldisc_hangup() Peter Hurley
2012-12-14 18:22 ` [PATCH v2 07/11] tty: Remove unnecessary re-test of ldisc ref count Peter Hurley
2012-12-14 18:22 ` [PATCH v2 08/11] tty: Fix ldisc halt sequence on hangup Peter Hurley
2012-12-14 18:22 ` [PATCH v2 09/11] tty: Strengthen no-subsequent-use guarantee of tty_ldisc_halt() Peter Hurley
2012-12-14 18:22 ` [PATCH v2 10/11] tty: Remove unnecessary buffer work flush Peter Hurley
2012-12-14 18:22 ` [PATCH v2 11/11] tty: Halt both ldiscs concurrently Peter Hurley
2012-12-18 15:44 ` [PATCH v2 00/11] tty: Fix buffer work access-after-free Sasha Levin
2012-12-18 16:48   ` Peter Hurley
2012-12-18 20:44     ` Ilya Zykov
2012-12-19 20:27       ` Peter Hurley
2012-12-19 20:38     ` Sasha Levin
2012-12-20 19:03       ` Peter Hurley
2013-02-05 20:20 ` [PATCH v3 00/23] ldisc fixes Peter Hurley
2013-02-05 20:20   ` [PATCH v3 01/23] tty: Add diagnostic for halted line discipline Peter Hurley
2013-02-07 14:50     ` Jiri Slaby
2013-02-07 15:53       ` Peter Hurley
2013-02-07 15:55         ` Jiri Slaby
2013-02-05 20:20   ` [PATCH v3 02/23] n_tty: Factor packet mode status change for reuse Peter Hurley
2013-02-07 14:51     ` Jiri Slaby
2013-02-07 15:58       ` Peter Hurley
2013-02-05 20:20   ` [PATCH v3 03/23] n_tty: Don't flush buffer when closing ldisc Peter Hurley
2013-02-07 14:57     ` Jiri Slaby
2013-02-05 20:20   ` [PATCH v3 04/23] tty: Refactor wait for ldisc refs out of tty_ldisc_hangup() Peter Hurley
2013-02-07 15:11     ` Jiri Slaby
2013-02-05 20:20   ` [PATCH v3 05/23] tty: Remove unnecessary re-test of ldisc ref count Peter Hurley
2013-02-07 15:16     ` Jiri Slaby
2013-02-07 15:59       ` Peter Hurley
2013-02-07 16:02         ` Jiri Slaby
2013-02-05 20:20   ` [PATCH v3 06/23] tty: Fix ldisc halt sequence on hangup Peter Hurley
2013-02-05 20:20   ` [PATCH v3 07/23] tty: Strengthen no-subsequent-use guarantee of tty_ldisc_halt() Peter Hurley
2013-02-07 15:38     ` Jiri Slaby
2013-02-07 16:22       ` Peter Hurley
2013-02-05 20:20   ` [PATCH v3 08/23] tty: Halt both ldiscs concurrently Peter Hurley
2013-02-05 20:20   ` [PATCH v3 09/23] tty: Remove unnecessary buffer work flush Peter Hurley
2013-02-05 20:20   ` [PATCH v3 10/23] tty: Wait for SAK work before waiting for hangup work Peter Hurley
2013-02-05 20:20   ` [PATCH v3 11/23] n_tty: Correct unthrottle-with-buffer-flush comments Peter Hurley
2013-02-05 20:20   ` [PATCH v3 12/23] tty: Kick waiters _after_ the ldisc is locked Peter Hurley
2013-02-07 15:47     ` Jiri Slaby
2013-02-08  3:16       ` Peter Hurley
2013-02-05 20:20   ` [PATCH v3 13/23] n_tty: Fully initialize ldisc before restarting buffer work Peter Hurley
2013-02-05 20:20   ` [PATCH v3 14/23] tty: Don't reenable already enabled ldisc Peter Hurley
2013-02-05 20:20   ` [PATCH v3 15/23] tty: Don't restart ldisc on hangup if racing ldisc kill Peter Hurley
2013-02-05 20:20   ` [PATCH v3 16/23] tty: Make core responsible for synchronizing its work Peter Hurley
2013-02-05 21:04     ` Peter Hurley
2013-02-05 20:20   ` [PATCH v3 17/23] tty: Document lock requirements to halt ldisc Peter Hurley
2013-02-05 20:20   ` [PATCH v3 18/23] tty: Remove stale comment re: tty_ldisc_flush_works() Peter Hurley
2013-02-05 20:20   ` [PATCH v3 19/23] tty: Fix 'deferred reopen' ldisc comment Peter Hurley
2013-02-05 20:20   ` [PATCH v3 20/23] tty: Remove stale comment re: locking in tty_ldisc_release() Peter Hurley
2013-02-05 20:20   ` [PATCH v3 21/23] tty: Re-parent orphaned tty_set_ldisc() comments Peter Hurley
2013-02-05 20:20   ` [PATCH v3 22/23] tty: Bracket ldisc release with TTY_DEBUG_HANGUP messages Peter Hurley
2013-02-05 20:20   ` [PATCH v3 23/23] tty: Add ldisc hangup debug messages Peter Hurley
2013-02-20 20:02   ` [PATCH v4 00/32] ldisc patchset Peter Hurley
2013-02-21  2:33     ` Shawn Guo
2013-02-21  2:33       ` Shawn Guo
2013-02-21  7:55     ` Sebastian Andrzej Siewior
2013-02-21 13:16     ` Sasha Levin
2013-02-21 13:38       ` Peter Hurley
2013-02-22 18:37         ` Peter Hurley
2013-02-23 15:24           ` Sasha Levin
2013-02-23 16:42             ` Sasha Levin
2013-02-27  3:08               ` Peter Hurley
2013-02-23 18:43             ` Peter Hurley
2013-02-23 20:02               ` Sasha Levin
2013-03-05 17:39           ` Peter Hurley
2013-03-05 21:18     ` Sebastian Andrzej Siewior
2013-03-11 20:44     ` [PATCH v5 00/44] " Peter Hurley
2013-03-11 20:44       ` [PATCH v5 01/44] tty: Add diagnostic for halted line discipline Peter Hurley
2013-03-11 20:44       ` [PATCH v5 02/44] n_tty: Factor packet mode status change for reuse Peter Hurley
2013-03-11 20:44       ` [PATCH v5 03/44] n_tty: Don't flush buffer when closing ldisc Peter Hurley
2013-03-11 20:44       ` [PATCH v5 04/44] tty: Refactor wait for ldisc refs out of tty_ldisc_hangup() Peter Hurley
2013-03-11 20:44       ` [PATCH v5 05/44] tty: Remove unnecessary re-test of ldisc ref count Peter Hurley
2013-03-11 20:44       ` [PATCH v5 06/44] tty: Fix ldisc halt sequence on hangup Peter Hurley
2013-03-11 20:44       ` [PATCH v5 07/44] tty: Relocate tty_ldisc_halt() to avoid forward declaration Peter Hurley
2013-03-11 20:44       ` [PATCH v5 08/44] tty: Strengthen no-subsequent-use guarantee of tty_ldisc_halt() Peter Hurley
2013-03-11 20:44       ` [PATCH v5 09/44] tty: Halt both ldiscs concurrently Peter Hurley
2013-03-11 20:44       ` [PATCH v5 10/44] tty: Wait for SAK work before waiting for hangup work Peter Hurley
2013-03-11 20:44       ` [PATCH v5 11/44] n_tty: Correct unthrottle-with-buffer-flush comments Peter Hurley
2013-03-11 20:44       ` [PATCH v5 12/44] n_tty: Fully initialize ldisc before restarting buffer work Peter Hurley
2013-03-11 20:44       ` [PATCH v5 13/44] tty: Don't reenable already enabled ldisc Peter Hurley
2013-03-11 20:44       ` [PATCH v5 14/44] tty: Complete ownership transfer of flip buffers Peter Hurley
2013-03-11 20:44       ` [PATCH v5 15/44] tty: Make core responsible for synchronizing its work Peter Hurley
2013-03-11 20:44       ` [PATCH v5 16/44] tty: Fix 'deferred reopen' ldisc comment Peter Hurley
2013-03-11 20:44       ` [PATCH v5 17/44] tty: Bracket ldisc release with TTY_DEBUG_HANGUP messages Peter Hurley
2013-03-11 20:44       ` [PATCH v5 18/44] tty: Add ldisc hangup debug messages Peter Hurley
2013-03-11 20:44       ` [PATCH v5 19/44] tty: Don't protect atomic operation with mutex Peter Hurley
2013-03-11 20:44       ` [PATCH v5 20/44] tty: Separate release semantics of ldisc reference Peter Hurley
2013-03-11 20:44       ` [PATCH v5 21/44] tty: Document unsafe ldisc reference acquire Peter Hurley
2013-03-11 20:44       ` [PATCH v5 22/44] tty: Fold one-line assign function into callers Peter Hurley
2013-03-11 20:44       ` [PATCH v5 23/44] tty: Locate get/put ldisc functions together Peter Hurley
2013-03-11 20:44       ` [PATCH v5 24/44] tty: Remove redundant tty_wait_until_sent() Peter Hurley
2013-03-11 20:44       ` [PATCH v5 25/44] tty: Fix recursive deadlock in tty_perform_flush() Peter Hurley
2013-03-11 21:36         ` Peter Hurley
2013-03-11 21:36           ` Peter Hurley
2013-03-11 20:44       ` [PATCH v5 26/44] tty: Add read-recursive, writer-prioritized rw semaphore Peter Hurley
2013-03-18 23:58         ` Greg Kroah-Hartman
2013-03-19  1:01           ` Peter Hurley
2013-03-19  1:59             ` Greg Kroah-Hartman
2013-03-19 15:43               ` Peter Hurley
2013-03-26 23:47               ` Peter Hurley
2013-03-11 20:44       ` [PATCH v5 27/44] tty: Drop lock contention stat from ldsem trylocks Peter Hurley
2013-03-11 20:44       ` [PATCH v5 28/44] tty: Remove ldsem recursion support Peter Hurley
2013-03-18 23:59         ` Greg Kroah-Hartman
2013-03-19  0:01           ` Peter Hurley
2013-03-19  0:05             ` Greg Kroah-Hartman
2013-03-19  0:12               ` Peter Hurley
2013-03-11 20:44       ` [PATCH v5 29/44] tty: Add lock/unlock ldisc pair functions Peter Hurley
2013-03-11 20:44       ` [PATCH v5 30/44] tty: Replace ldisc locking with ldisc_sem Peter Hurley
2013-03-11 20:44       ` [PATCH v5 31/44] tty: Clarify ldisc variable Peter Hurley
2013-03-11 20:44       ` [PATCH v5 32/44] tty: Fix hangup race with TIOCSETD ioctl Peter Hurley
2013-03-11 20:44       ` [PATCH v5 33/44] tty: Clarify multiple-references comment in " Peter Hurley
2013-03-11 20:44       ` [PATCH v5 34/44] tty: Fix tty_ldisc_lock name collision Peter Hurley
2013-03-11 20:44       ` [PATCH v5 35/44] tty: Drop "tty is NULL" flip buffer diagnostic Peter Hurley
2013-03-11 20:44       ` [PATCH v5 36/44] tty: Inline ldsem down_failed() into down_{read,write}_failed() Peter Hurley
2013-03-11 20:44       ` [PATCH v5 37/44] tty: Drop ldsem wait type Peter Hurley
2013-03-11 20:44       ` [PATCH v5 38/44] tty: Drop wake type optimization Peter Hurley
2013-03-11 20:44       ` [PATCH v5 39/44] tty: Factor ldsem writer trylock Peter Hurley
2013-03-11 20:45       ` [PATCH v5 40/44] tty: Simplify lock taking for waiting writers Peter Hurley
2013-03-11 20:45       ` [PATCH v5 41/44] tty: Implement ldsem fast path write lock stealing Peter Hurley
2013-03-11 20:45       ` [PATCH v5 42/44] tty: Reduce and simplify ldsem atomic ops Peter Hurley
2013-03-11 20:45       ` [PATCH v5 43/44] tty: Early-out ldsem write lock stealing Peter Hurley
2013-03-11 20:45       ` [PATCH v5 44/44] tty: Early-out tardy ldsem readers Peter Hurley
2013-03-12  2:28       ` [PATCH v5 00/44] ldisc patchset Michel Lespinasse
2013-03-12 16:47         ` Peter Hurley
2013-03-13 11:36           ` Michel Lespinasse
2013-03-14  1:12             ` Peter Hurley
2013-03-14  7:25               ` Michel Lespinasse
2013-03-14 11:42                 ` Peter Hurley
2013-03-14 12:13                   ` Michel Lespinasse
2013-03-18 23:54       ` Greg Kroah-Hartman
2013-03-19 19:26       ` [PATCH 0/7] ldsem patchset Peter Hurley
2013-03-19 19:26         ` [PATCH 1/7] tty: Add timed, writer-prioritized rw semaphore Peter Hurley
2013-03-19 19:26         ` [PATCH 2/7] tty: Add lock/unlock ldisc pair functions Peter Hurley
2013-03-19 19:26         ` [PATCH 3/7] tty: Replace ldisc locking with ldisc_sem Peter Hurley
2013-03-19 19:26         ` [PATCH 4/7] tty: Clarify ldisc variable Peter Hurley
2013-03-19 19:26         ` [PATCH 5/7] tty: Fix hangup race with TIOCSETD ioctl Peter Hurley
2013-03-19 19:26         ` [PATCH 6/7] tty: Clarify multiple-references comment in " Peter Hurley
2013-03-19 19:26         ` [PATCH 7/7] tty: Fix tty_ldisc_lock name collision Peter Hurley
2013-02-20 20:02   ` [PATCH v4 01/32] tty: Add diagnostic for halted line discipline Peter Hurley
2013-02-20 20:02   ` [PATCH v4 02/32] n_tty: Factor packet mode status change for reuse Peter Hurley
2013-02-20 20:02   ` [PATCH v4 03/32] n_tty: Don't flush buffer when closing ldisc Peter Hurley
2013-02-20 20:02   ` [PATCH v4 04/32] tty: Refactor wait for ldisc refs out of tty_ldisc_hangup() Peter Hurley
2013-02-20 20:02   ` [PATCH v4 05/32] tty: Remove unnecessary re-test of ldisc ref count Peter Hurley
2013-02-20 20:02   ` [PATCH v4 06/32] tty: Fix ldisc halt sequence on hangup Peter Hurley
2013-02-20 20:02   ` [PATCH v4 07/32] tty: Relocate tty_ldisc_halt() to avoid forward declaration Peter Hurley
2013-02-20 20:02   ` [PATCH v4 08/32] tty: Strengthen no-subsequent-use guarantee of tty_ldisc_halt() Peter Hurley
2013-02-20 20:02   ` [PATCH v4 09/32] tty: Halt both ldiscs concurrently Peter Hurley
2013-02-20 20:02   ` [PATCH v4 10/32] tty: Wait for SAK work before waiting for hangup work Peter Hurley
2013-02-20 20:02   ` [PATCH v4 11/32] n_tty: Correct unthrottle-with-buffer-flush comments Peter Hurley
2013-02-20 20:02   ` [PATCH v4 12/32] n_tty: Fully initialize ldisc before restarting buffer work Peter Hurley
2013-02-20 20:03   ` [PATCH v4 13/32] tty: Don't reenable already enabled ldisc Peter Hurley
2013-02-20 20:03   ` [PATCH v4 14/32] tty: Complete ownership transfer of flip buffers Peter Hurley
2013-02-20 20:03   ` [PATCH v4 15/32] tty: Make core responsible for synchronizing its work Peter Hurley
2013-02-20 20:03   ` [PATCH v4 16/32] tty: Fix 'deferred reopen' ldisc comment Peter Hurley
2013-02-20 20:03   ` [PATCH v4 17/32] tty: Bracket ldisc release with TTY_DEBUG_HANGUP messages Peter Hurley
2013-02-20 20:03   ` [PATCH v4 18/32] tty: Add ldisc hangup debug messages Peter Hurley
2013-02-20 20:03   ` [PATCH v4 19/32] tty: Don't protect atomic operation with mutex Peter Hurley
2013-02-20 20:03   ` [PATCH v4 20/32] tty: Separate release semantics of ldisc reference Peter Hurley
2013-02-20 20:03   ` [PATCH v4 21/32] tty: Document unsafe ldisc reference acquire Peter Hurley
2013-02-20 20:03   ` [PATCH v4 22/32] tty: Fold one-line assign function into callers Peter Hurley
2013-02-20 20:03   ` [PATCH v4 23/32] tty: Locate get/put ldisc functions together Peter Hurley
2013-02-20 20:03   ` [PATCH v4 24/32] tty: Remove redundant tty_wait_until_sent() Peter Hurley
2013-02-20 20:03   ` [PATCH v4 25/32] tty: Add read-recursive, writer-prioritized rw semaphore Peter Hurley
2013-03-08 17:12     ` Peter Hurley
2013-02-20 20:03   ` [PATCH v4 26/32] tty: Add lock/unlock ldisc pair functions Peter Hurley
2013-02-20 20:03   ` [PATCH v4 27/32] tty: Replace ldisc locking with ldisc_sem Peter Hurley
2013-02-20 20:03   ` [PATCH v4 28/32] tty: Clarify ldisc variable Peter Hurley
2013-02-20 20:03   ` [PATCH v4 29/32] tty: Fix hangup race with TIOCSETD ioctl Peter Hurley
2013-02-20 20:03   ` [PATCH v4 30/32] tty: Clarify multiple-references comment in " Peter Hurley
2013-02-20 20:03   ` [PATCH v4 31/32] tty: Fix tty_ldisc_lock name collision Peter Hurley
2013-02-20 20:03   ` [PATCH v4 32/32] tty: Drop "tty is NULL" flip buffer diagnostic Peter Hurley
2013-03-05 20:50   ` [PATCH v3 00/23] ldisc fixes Sebastian Andrzej Siewior
2013-03-05 22:20     ` Peter Hurley
2013-03-05 22:39       ` Jiri Slaby
2013-03-05 23:14         ` Peter Hurley
2013-03-17 18:50       ` Sebastian Andrzej Siewior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.