Vulnerability: Race condition in the tty_ioctl function in drivers/tty/tty_io.c in the Linux kernel through 4.4.1 allows local users to obtain sensitive information from kernel memory or cause a denial of service (use-after-free and system crash) by making a TIOCGETD ioctl call during processing of a TIOCSETD ioctl call.
Vulnerability ID: CVE-2016-0723
CWE: CWE-362
Severity score: 7.8
Project: https://github.com/torvalds/linux
Published: 2016-02-08
File: drivers/tty/tty_io.c
Commit message: tty: Fix unsafe ldisc reference via ioctl(TIOCGETD)

ioctl(TIOCGETD) retrieves the line discipline id directly from the
ldisc because the line discipline id (c_line) in termios is untrustworthy;
userspace may have set termios via ioctl(TCSETS*) without actually
changing the line discipline via ioctl(TIOCSETD).

However, directly accessing the current ldisc via tty->ldisc is
unsafe; the ldisc ptr dereferenced may be stale if the line discipline
is changing via ioctl(TIOCSETD) or hangup.

Wait for the line discipline reference (just like read() or write())
to retrieve the "current" line discipline id.

Cc: <stable@vger.kernel.org>
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Commit author: Peter Hurley

Vulnerable version (before fix):
 static int tiocsetd(struct tty_struct *tty, int __user *p) 	return ret; }  /**  *	send_break	-	performed time break  *	@tty: device to break on long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 	case TIOCGSID: 		return tiocgsid(tty, real_tty, p); 	case TIOCGETD: 		return put_user(tty->ldisc->ops->num, (int __user *)p); 	case TIOCSETD: 		return tiocsetd(tty, p); 	case TIOCVHANGUP:

Fixed version (after fix):
 static int tiocsetd(struct tty_struct *tty, int __user *p) 	return ret; }  /**  *	tiocgetd	-	get line discipline  *	@tty: tty device  *	@p: pointer to user data  *  *	Retrieves the line discipline id directly from the ldisc.  *  *	Locking: waits for ldisc reference (in case the line discipline  *		is changing or the tty is being hungup)  */  static int tiocgetd(struct tty_struct *tty, int __user *p) { 	struct tty_ldisc *ld; 	int ret;  	ld = tty_ldisc_ref_wait(tty); 	ret = put_user(ld->ops->num, p); 	tty_ldisc_deref(ld); 	return ret; }  /**  *	send_break	-	performed time break  *	@tty: device to break on long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 	case TIOCGSID: 		return tiocgsid(tty, real_tty, p); 	case TIOCGETD: 		return tiocgetd(tty, p); 	case TIOCSETD: 		return tiocsetd(tty, p); 	case TIOCVHANGUP:

Patch:
patch: @@ -2658,6 +2658,28 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)
 	return ret;
 }
 
+/**
+ *	tiocgetd	-	get line discipline
+ *	@tty: tty device
+ *	@p: pointer to user data
+ *
+ *	Retrieves the line discipline id directly from the ldisc.
+ *
+ *	Locking: waits for ldisc reference (in case the line discipline
+ *		is changing or the tty is being hungup)
+ */
+
+static int tiocgetd(struct tty_struct *tty, int __user *p)
+{
+	struct tty_ldisc *ld;
+	int ret;
+
+	ld = tty_ldisc_ref_wait(tty);
+	ret = put_user(ld->ops->num, p);
+	tty_ldisc_deref(ld);
+	return ret;
+}
+
 /**
  *	send_break	-	performed time break
  *	@tty: device to break on
@@ -2884,7 +2906,7 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	case TIOCGSID:
 		return tiocgsid(tty, real_tty, p);
 	case TIOCGETD:
-		return put_user(tty->ldisc->ops->num, (int __user *)p);
+		return tiocgetd(tty, p);
 	case TIOCSETD:
 		return tiocsetd(tty, p);
 	case TIOCVHANGUP:
path: drivers/tty/tty_io.c