diff options
author | Herton R. Krzesinski <herton@redhat.com> | 2016-01-11 09:07:43 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2016-02-07 02:45:46 -0500 |
commit | 2831c89f42dcde440cfdccb9fee9f42d54bbc1ef (patch) | |
tree | cf69785b4edd19e4a59549de860b0d00f920df1a /drivers/tty | |
parent | 7dde55787b43a8f2b4021916db38d90c03a2ec64 (diff) |
pty: fix possible use after free of tty->driver_data
This change fixes a bug for a corner case where we have the the last
release from a pty master/slave coming from a previously opened /dev/tty
file. When this happens, the tty->driver_data can be stale, due to all
ptmx or pts/N files having already been closed before (and thus the inode
related to these files, which tty->driver_data points to, being already
freed/destroyed).
The fix here is to keep a reference on the opened master ptmx inode.
We maintain the inode referenced until the final pty_unix98_shutdown,
and only pass this inode to devpts_kill_index.
Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
Cc: <stable@vger.kernel.org> # 2.6.29+
Reviewed-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/tty')
-rw-r--r-- | drivers/tty/pty.c | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c index b3110040164a..3b5cde833ee5 100644 --- a/drivers/tty/pty.c +++ b/drivers/tty/pty.c | |||
@@ -681,7 +681,14 @@ static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty) | |||
681 | /* this is called once with whichever end is closed last */ | 681 | /* this is called once with whichever end is closed last */ |
682 | static void pty_unix98_shutdown(struct tty_struct *tty) | 682 | static void pty_unix98_shutdown(struct tty_struct *tty) |
683 | { | 683 | { |
684 | devpts_kill_index(tty->driver_data, tty->index); | 684 | struct inode *ptmx_inode; |
685 | |||
686 | if (tty->driver->subtype == PTY_TYPE_MASTER) | ||
687 | ptmx_inode = tty->driver_data; | ||
688 | else | ||
689 | ptmx_inode = tty->link->driver_data; | ||
690 | devpts_kill_index(ptmx_inode, tty->index); | ||
691 | iput(ptmx_inode); /* drop reference we acquired at ptmx_open */ | ||
685 | } | 692 | } |
686 | 693 | ||
687 | static const struct tty_operations ptm_unix98_ops = { | 694 | static const struct tty_operations ptm_unix98_ops = { |
@@ -773,6 +780,15 @@ static int ptmx_open(struct inode *inode, struct file *filp) | |||
773 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ | 780 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ |
774 | tty->driver_data = inode; | 781 | tty->driver_data = inode; |
775 | 782 | ||
783 | /* | ||
784 | * In the case where all references to ptmx inode are dropped and we | ||
785 | * still have /dev/tty opened pointing to the master/slave pair (ptmx | ||
786 | * is closed/released before /dev/tty), we must make sure that the inode | ||
787 | * is still valid when we call the final pty_unix98_shutdown, thus we | ||
788 | * hold an additional reference to the ptmx inode | ||
789 | */ | ||
790 | ihold(inode); | ||
791 | |||
776 | tty_add_file(tty, filp); | 792 | tty_add_file(tty, filp); |
777 | 793 | ||
778 | slave_inode = devpts_pty_new(inode, | 794 | slave_inode = devpts_pty_new(inode, |