Skip to content

Commit 54ebbfb

Browse files
cyphargregkh
authored andcommitted
tty: add TIOCGPTPEER ioctl
When opening the slave end of a PTY, it is not possible for userspace to safely ensure that /dev/pts/$num is actually a slave (in cases where the mount namespace in which devpts was mounted is controlled by an untrusted process). In addition, there are several unresolvable race conditions if userspace were to attempt to detect attacks through stat(2) and other similar methods [in addition it is not clear how userspace could detect attacks involving FUSE]. Resolve this by providing an interface for userpace to safely open the "peer" end of a PTY file descriptor by using the dentry cached by devpts. Since it is not possible to have an open master PTY without having its slave exposed in /dev/pts this interface is safe. This interface currently does not provide a way to get the master pty (since it is not clear whether such an interface is safe or even useful). Cc: Christian Brauner <christian.brauner@ubuntu.com> Cc: Valentin Rothberg <vrothberg@suse.com> Signed-off-by: Aleksa Sarai <asarai@suse.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5f0f187 commit 54ebbfb

9 files changed

Lines changed: 76 additions & 5 deletions

File tree

‎arch/alpha/include/uapi/asm/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
101101
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
102102
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
103+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
103104

104105
#define TIOCSERCONFIG 0x5453
105106
#define TIOCSERGWILD 0x5454

‎arch/mips/include/uapi/asm/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
9292
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
9393
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
94+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
9495

9596
/* I hope the range from 0x5480 on is free ... */
9697
#define TIOCSCTTY 0x5480 /* become controlling tty */

‎arch/parisc/include/uapi/asm/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
6161
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
6262
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
63+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
6364

6465
#define FIONCLEX 0x5450 /* these numbers need to be adjusted. */
6566
#define FIOCLEX 0x5451

‎arch/powerpc/include/uapi/asm/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
101101
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
102102
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
103+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
103104

104105
#define TIOCSERCONFIG 0x5453
105106
#define TIOCSERGWILD 0x5454

‎arch/sh/include/uapi/asm/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
9494
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
9595
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
96+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
9697

9798
#define TIOCSERCONFIG _IO('T', 83) /* 0x5453 */
9899
#define TIOCSERGWILD _IOR('T', 84, int) /* 0x5454 */

‎arch/sparc/include/uapi/asm/ioctls.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define TIOCGRS485 _IOR('T', 0x41, struct serial_rs485)
2828
#define TIOCSRS485 _IOWR('T', 0x42, struct serial_rs485)
2929

30-
/* Note that all the ioctls that are not available in Linux have a
30+
/* Note that all the ioctls that are not available in Linux have a
3131
* double underscore on the front to: a) avoid some programs to
3232
* think we support some ioctls under Linux (autoconfiguration stuff)
3333
*/
@@ -88,6 +88,7 @@
8888
#define TIOCGPTN _IOR('t', 134, unsigned int) /* Get Pty Number */
8989
#define TIOCSPTLCK _IOW('t', 135, int) /* Lock/unlock PTY */
9090
#define TIOCSIG _IOW('t', 136, int) /* Generate signal on Pty slave */
91+
#define TIOCGPTPEER _IOR('t', 137, int) /* Safely open the slave */
9192

9293
/* Little f */
9394
#define FIOCLEX _IO('f', 1)

‎arch/xtensa/include/uapi/asm/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
106106
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
107107
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
108+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
108109

109110
#define TIOCSERCONFIG _IO('T', 83)
110111
#define TIOCSERGWILD _IOR('T', 84, int)

‎drivers/tty/pty.c‎

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <linux/slab.h>
2525
#include <linux/mutex.h>
2626
#include <linux/poll.h>
27+
#include <linux/mount.h>
28+
#include <linux/file.h>
29+
#include <linux/ioctl.h>
2730

2831
#undef TTY_DEBUG_HANGUP
2932
#ifdef TTY_DEBUG_HANGUP
@@ -66,8 +69,13 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
6669
#ifdef CONFIG_UNIX98_PTYS
6770
if (tty->driver == ptm_driver) {
6871
mutex_lock(&devpts_mutex);
69-
if (tty->link->driver_data)
70-
devpts_pty_kill(tty->link->driver_data);
72+
if (tty->link->driver_data) {
73+
struct path *path = tty->link->driver_data;
74+
75+
devpts_pty_kill(path->dentry);
76+
path_put(path);
77+
kfree(path);
78+
}
7179
mutex_unlock(&devpts_mutex);
7280
}
7381
#endif
@@ -440,6 +448,48 @@ static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
440448
return retval;
441449
}
442450

451+
/**
452+
* pty_open_peer - open the peer of a pty
453+
* @tty: the peer of the pty being opened
454+
*
455+
* Open the cached dentry in tty->link, providing a safe way for userspace
456+
* to get the slave end of a pty (where they have the master fd and cannot
457+
* access or trust the mount namespace /dev/pts was mounted inside).
458+
*/
459+
static struct file *pty_open_peer(struct tty_struct *tty, int flags)
460+
{
461+
if (tty->driver->subtype != PTY_TYPE_MASTER)
462+
return ERR_PTR(-EIO);
463+
return dentry_open(tty->link->driver_data, flags, current_cred());
464+
}
465+
466+
static int pty_get_peer(struct tty_struct *tty, int flags)
467+
{
468+
int fd = -1;
469+
struct file *filp = NULL;
470+
int retval = -EINVAL;
471+
472+
fd = get_unused_fd_flags(0);
473+
if (fd < 0) {
474+
retval = fd;
475+
goto err;
476+
}
477+
478+
filp = pty_open_peer(tty, flags);
479+
if (IS_ERR(filp)) {
480+
retval = PTR_ERR(filp);
481+
goto err_put;
482+
}
483+
484+
fd_install(fd, filp);
485+
return fd;
486+
487+
err_put:
488+
put_unused_fd(fd);
489+
err:
490+
return retval;
491+
}
492+
443493
static void pty_cleanup(struct tty_struct *tty)
444494
{
445495
tty_port_put(tty->port);
@@ -613,6 +663,8 @@ static int pty_unix98_ioctl(struct tty_struct *tty,
613663
return pty_get_pktmode(tty, (int __user *)arg);
614664
case TIOCGPTN: /* Get PT Number */
615665
return put_user(tty->index, (unsigned int __user *)arg);
666+
case TIOCGPTPEER: /* Open the other end */
667+
return pty_get_peer(tty, (int) arg);
616668
case TIOCSIG: /* Send signal to other side of pty */
617669
return pty_signal(tty, (int) arg);
618670
}
@@ -740,6 +792,7 @@ static int ptmx_open(struct inode *inode, struct file *filp)
740792
{
741793
struct pts_fs_info *fsi;
742794
struct tty_struct *tty;
795+
struct path *pts_path;
743796
struct dentry *dentry;
744797
int retval;
745798
int index;
@@ -793,16 +846,26 @@ static int ptmx_open(struct inode *inode, struct file *filp)
793846
retval = PTR_ERR(dentry);
794847
goto err_release;
795848
}
796-
tty->link->driver_data = dentry;
849+
/* We need to cache a fake path for TIOCGPTPEER. */
850+
pts_path = kmalloc(sizeof(struct path), GFP_KERNEL);
851+
if (!pts_path)
852+
goto err_release;
853+
pts_path->mnt = filp->f_path.mnt;
854+
pts_path->dentry = dentry;
855+
path_get(pts_path);
856+
tty->link->driver_data = pts_path;
797857

798858
retval = ptm_driver->ops->open(tty, filp);
799859
if (retval)
800-
goto err_release;
860+
goto err_path_put;
801861

802862
tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
803863

804864
tty_unlock(tty);
805865
return 0;
866+
err_path_put:
867+
path_put(pts_path);
868+
kfree(pts_path);
806869
err_release:
807870
tty_unlock(tty);
808871
// This will also put-ref the fsi

‎include/uapi/asm-generic/ioctls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#define TIOCGPKT _IOR('T', 0x38, int) /* Get packet mode state */
7878
#define TIOCGPTLCK _IOR('T', 0x39, int) /* Get Pty lock state */
7979
#define TIOCGEXCL _IOR('T', 0x40, int) /* Get exclusive mode state */
80+
#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */
8081

8182
#define FIONCLEX 0x5450
8283
#define FIOCLEX 0x5451

0 commit comments

Comments
 (0)