tty: add TIOCGPTPEER ioctl
Subsystems (9)
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).
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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
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/
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~