From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Mateusz Guzik <mguzik@redhat.com>
Cc: Jiri Slaby <jslaby@suse.com>,
stable@vger.kernel.org, linux-kernel@vger.kernel.org,
security@kernel.org, milos@redhat.com
Subject: Re: [PATCH] tty: plug a use-after-free in TIOCGETD ioctl
Date: Thu, 7 Jan 2016 09:11:41 -0800 [thread overview]
Message-ID: <20160107171141.GA28979@kroah.com> (raw)
In-Reply-To: <20160107162112.GD14492@mguzik>
On Thu, Jan 07, 2016 at 05:21:14PM +0100, Mateusz Guzik wrote:
> On Thu, Jan 07, 2016 at 07:33:10AM -0800, Greg Kroah-Hartman wrote:
> > On Thu, Jan 07, 2016 at 03:58:00PM +0100, Mateusz Guzik wrote:
> > > When the line discipline is being changed, the old one is freed.
> > > However, the handler for TIOCGETD would dereference it without taking
> > > any locks, in effect possibly reading freed memory.
> > >
> > > Line discipline changes are protected with tty lock. Use it on reader
> > > side as well.
> > >
> > > CVE: CVE-2016-0723
> >
> > Why a cve tag?
> >
>
> Red Hat SRT assigned a CVE and asked me to included in the commit
> message. I did a quick check how people mark such stuff and found the
> tag. I definitely don't insist on having it mentioned.
It seems odd that any random kernel bug can get assigned a CVE without
actually talking to the developers first, but whatever...
> > > Found-by: Milos Vyletel <milos@redhat.com>
> > > Signed-off-by: Mateusz Guzik <mguzik@redhat.com>
> > > ---
> > > drivers/tty/tty_io.c | 23 ++++++++++++++++++++++-
> > > 1 file changed, 22 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> > > index 892c923..1b10469 100644
> > > --- a/drivers/tty/tty_io.c
> > > +++ b/drivers/tty/tty_io.c
> > > @@ -2626,6 +2626,27 @@ static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t _
> > > }
> > >
> > > /**
> > > + * tiocgetd - get line discipline
> > > + * @tty: tty device
> > > + * @p: pointer to returned line discipline
> > > + *
> > > + * Get the line discipline associated with the tty.
> > > + *
> > > + * Locking: none
> > > + */
> > > +
> > > +static int tiocgetd(struct tty_struct *tty, int __user *p)
> > > +{
> > > + int ldisc;
> > > +
> > > + tty_lock(tty);
> > > + ldisc = tty->ldisc->ops->num;
> > > + tty_unlock(tty);
> > > +
> > > + return put_user(ldisc, p);
> >
> > Does this really protect anything? What is preventing ldisc from going
> > away right after the tty_unlock call?
>
> I guess I should have elaborated, sorry.
>
> Yes, ldisc can be freed just after tty_unlock, but it does not matter.
> There is only a need to store the number (which is done with the lock
> held) and line discipline is not touched afterwards.
You don't store the number, you store a pointer, which isn't good. If
you want to just store the number, just store the number.
> > And how are you able to trigger the tty to go away while the file is
> > still held open and this ioctl is being called?
> >
>
> It's not the tty going away, but the memory pointed to by previous value
> of tty->ldisc.
>
> tty_set_ldisc will reassign tty->ldisc to a new value, and will later
> free the old one with tty_ldisc_put.
>
> In the current code TIOCGETD is:
> return put_user(tty->ldisc->ops->num, (int __user *)p);
>
> A thread doing this ioctl can load tty->ldisc's value, but memory
> pointed to it can be freed before it loads ops's address.
But your fix doesn't solve this, you are keeping a stale pointer around
as the ldisc could have gone away. See Peter's fix for the "correct"
way to solve this.
thanks,
greg k-h
next prev parent reply other threads:[~2016-01-07 17:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-07 14:58 [PATCH] tty: plug a use-after-free in TIOCGETD ioctl Mateusz Guzik
2016-01-07 15:33 ` Greg Kroah-Hartman
2016-01-07 16:21 ` Mateusz Guzik
2016-01-07 17:11 ` Greg Kroah-Hartman [this message]
2016-01-07 16:14 ` Greg Kroah-Hartman
2016-01-07 16:38 ` Peter Hurley
2016-01-07 17:08 ` Greg Kroah-Hartman
2016-01-07 17:38 ` Peter Hurley
2016-01-07 18:21 ` Greg Kroah-Hartman
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=20160107171141.GA28979@kroah.com \
--to=gregkh@linuxfoundation.org \
--cc=jslaby@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mguzik@redhat.com \
--cc=milos@redhat.com \
--cc=security@kernel.org \
--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).