Skip to contentSkip to conversation

tty: add TIOCGPTPEER ioctl

PATCH Superseded Aleksa Sarai 3 participants 4 messages Jun 1, 2017

Subsystems (9)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 <[email protected]>
Cc Valentin Rothberg <[email protected]>
---
 arch/alpha/include/uapi/asm/ioctls.h   |  1 +
 arch/mips/include/uapi/asm/ioctls.h    |  1 +
 arch/parisc/include/uapi/asm/ioctls.h  |  1 +
 arch/powerpc/include/uapi/asm/ioctls.h |  1 +
 arch/sh/include/uapi/asm/ioctls.h      |  1 +
 arch/sparc/include/uapi/asm/ioctls.h   |  3 +-
 arch/xtensa/include/uapi/asm/ioctls.h  |  1 +
 drivers/tty/pty.c                      | 71 ++++++++++++++++++++++++++++++++--
 include/uapi/asm-generic/ioctls.h      |  1 +
 9 files changed, 76 insertions(+), 5 deletions(-)
arch/alpha/include/uapi/asm/ioctls.h +1 -0
index f30c94ae1bdb..f6ec120fdccd 100644
@@ -94,6 +94,7 @@
 #define TIOCSRS485	_IOWR('T', 0x2F, struct serial_rs485)
 #define TIOCGPTN	_IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T',0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TIOCSIG		_IOW('T',0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP	0x5437
arch/mips/include/uapi/asm/ioctls.h +1 -0
index 740219c2c894..6a44a4d82a6f 100644
@@ -85,6 +85,7 @@
 #define TIOCSRS485	_IOWR('T', 0x2F, struct serial_rs485)
 #define TIOCGPTN	_IOR('T', 0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T', 0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T', 0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TIOCSIG		_IOW('T', 0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP	0x5437
arch/parisc/include/uapi/asm/ioctls.h +1 -0
index b6572f051b67..b89a7029a83f 100644
@@ -54,6 +54,7 @@
 #define TIOCSRS485	_IOWR('T', 0x2F, struct serial_rs485)
 #define TIOCGPTN	_IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T',0x32, int)  /* Get primary device node of /dev/console */
 #define TIOCSIG		_IOW('T',0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP	0x5437
arch/powerpc/include/uapi/asm/ioctls.h +1 -0
index 49a25796a61a..28ccc176e403 100644
@@ -94,6 +94,7 @@
 #define TIOCSRS485	0x542f
 #define TIOCGPTN	_IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T',0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TIOCSIG		_IOW('T',0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP	0x5437
arch/sh/include/uapi/asm/ioctls.h +1 -0
index c9903e56ccf4..c155812f75db 100644
@@ -87,6 +87,7 @@
 #define TIOCSRS485	_IOWR('T', 47, struct serial_rs485)
 #define TIOCGPTN	_IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T',0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TIOCSIG		_IOW('T',0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP	_IO('T', 0x37)
arch/sparc/include/uapi/asm/ioctls.h +2 -1
index 06b3f6c3bb9a..6d27398632ea 100644
@@ -27,7 +27,7 @@
 #define TIOCGRS485	_IOR('T', 0x41, struct serial_rs485)
 #define TIOCSRS485	_IOWR('T', 0x42, struct serial_rs485)
 
-/* Note that all the ioctls that are not available in Linux have a 
+/* Note that all the ioctls that are not available in Linux have a
  * double underscore on the front to: a) avoid some programs to
  * think we support some ioctls under Linux (autoconfiguration stuff)
  */
@@ -88,6 +88,7 @@
 #define TIOCGPTN	_IOR('t', 134, unsigned int) /* Get Pty Number */
 #define TIOCSPTLCK	_IOW('t', 135, int) /* Lock/unlock PTY */
 #define TIOCSIG		_IOW('t', 136, int) /* Generate signal on Pty slave */
+#define TIOCGPTPEER	_IOR('t', 137, int) /* Safely open the slave */
 
 /* Little f */
 #define FIOCLEX		_IO('f', 1)
arch/xtensa/include/uapi/asm/ioctls.h +1 -0
index 518954e74e6d..71f0a979647e 100644
@@ -99,6 +99,7 @@
 #define TIOCSRS485	_IOWR('T', 47, struct serial_rs485)
 #define TIOCGPTN	_IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T',0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TIOCSIG		_IOW('T',0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP	_IO('T', 0x37)
drivers/tty/pty.c +67 -4

115 lines not on this page · raw message

include/uapi/asm-generic/ioctls.h +1 -0
index 143dacbb7d9a..e836dc677612 100644
@@ -67,6 +67,7 @@
 #endif
 #define TIOCGPTN	_IOR('T', 0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK	_IOW('T', 0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER	_IOR('T', 0x41, int) /* Safely open the slave */
 #define TIOCGDEV	_IOR('T', 0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TCGETX		0x5432 /* SYS5 TCGETX compatibility */
 #define TCSETX		0x5433
Signature · 1 line
2.13.0
#1 lore raw

Hi Aleksa,

arch/alpha/include/uapi/asm/ioctls.h line 97

Quoted 7 lines · Aleksa Sarai
arch/alpha/include/uapi/asm/ioctls.h +1 -0
@@ -94,6 +94,7 @@
 #define TIOCSRS485     _IOWR('T', 0x2F, struct serial_rs485)
 #define TIOCGPTN       _IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
 #define TIOCSPTLCK     _IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER    _IOR('T', 0x41, int) /* Safely open the slave */

Shouldn't the list of definitions be kept sorted, by hex number? (everywhere)

arch/alpha/include/uapi/asm/ioctls.h line 100

Quoted 3 lines
arch/alpha/include/uapi/asm/ioctls.h +0 -0
 #define TIOCGDEV       _IOR('T',0x32, unsigned int) /* Get primary device node of /dev/console */
 #define TIOCSIG                _IOW('T',0x36, int)  /* Generate signal on Pty slave */
 #define TIOCVHANGUP    0x5437

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 10 lines

arch/alpha/include/uapi/asm/ioctls.h line 97

Quoted 7 lines
arch/alpha/include/uapi/asm/ioctls.h +1 -0
@@ -94,6 +94,7 @@
  #define TIOCSRS485     _IOWR('T', 0x2F, struct serial_rs485)
  #define TIOCGPTN       _IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
  #define TIOCSPTLCK     _IOW('T',0x31, int)  /* Lock/unlock Pty */
+#define TIOCGPTPEER    _IOR('T', 0x41, int) /* Safely open the slave */

Shouldn't the list of definitions be kept sorted, by hex number? (everywhere)

Probably. The reason I put it here is because it logically is very similar to TIOCGPTN and TIOSPTLCK, but I can move it if the hex order is more important for maintainence.

Signature · 4 lines
Aleksa Sarai
Software Engineer (Containers)
SUSE Linux GmbH
https://www.cyphar.com/
#3 lore raw
Quoted 12 lines · Aleksa Sarai
Quoted 11 lines
Quoted 8 lines
arch/alpha/include/uapi/asm/ioctls.h +0 -0
@@ -94,6 +94,7 @@
  #define TIOCSRS485     _IOWR('T', 0x2F, struct serial_rs485)
  #define TIOCGPTN       _IOR('T',0x30, unsigned int) /* Get Pty Number (of 

pty-mux device) */
#define TIOCSPTLCK _IOW('T',0x31, int) /* Lock/unlock Pty */
+#define TIOCGPTPEER _IOR('T', 0x41, int) /* Safely open the slave */

Shouldn't the list of definitions be kept sorted, by hex number? (everywhere)

Quoted 2 lines

Probably. The reason I put it here is because it logically is very similar to TIOCGPTN and TIOSPTLCK, but I can move it if the hex order is more important for maintainence.

Yes please.

r~