Skip to content

Commit 5c17c86

Browse files
peterhurleygregkh
authored andcommitted
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>
1 parent 7f22f6c commit 5c17c86

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

drivers/tty/tty_io.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2658,6 +2658,28 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)
26582658
return ret;
26592659
}
26602660

2661+
/**
2662+
* tiocgetd - get line discipline
2663+
* @tty: tty device
2664+
* @p: pointer to user data
2665+
*
2666+
* Retrieves the line discipline id directly from the ldisc.
2667+
*
2668+
* Locking: waits for ldisc reference (in case the line discipline
2669+
* is changing or the tty is being hungup)
2670+
*/
2671+
2672+
static int tiocgetd(struct tty_struct *tty, int __user *p)
2673+
{
2674+
struct tty_ldisc *ld;
2675+
int ret;
2676+
2677+
ld = tty_ldisc_ref_wait(tty);
2678+
ret = put_user(ld->ops->num, p);
2679+
tty_ldisc_deref(ld);
2680+
return ret;
2681+
}
2682+
26612683
/**
26622684
* send_break - performed time break
26632685
* @tty: device to break on
@@ -2884,7 +2906,7 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28842906
case TIOCGSID:
28852907
return tiocgsid(tty, real_tty, p);
28862908
case TIOCGETD:
2887-
return put_user(tty->ldisc->ops->num, (int __user *)p);
2909+
return tiocgetd(tty, p);
28882910
case TIOCSETD:
28892911
return tiocsetd(tty, p);
28902912
case TIOCVHANGUP:

0 commit comments

Comments
 (0)