tty: add TIOCGPTPEER ioctl
v3 cover tty: add TIOCGPTPEER ioctl
Changes in v3:
* Defined a compat_ioctl callback for all pty ioctls, since they are all 32-bit and 64-bit compatible.
* Made TIOCGPTPEER support 32-bit userspace.
Aleksa Sarai (2):
tty: add compat_ioctl callbacks
tty: add TIOCGPTPEER ioctl
Makefile | 1 + 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 | 93 ++++++++++++++++++++++++++++++++-- fs/compat_ioctl.c | 6 --- include/uapi/asm-generic/ioctls.h | 1 + 11 files changed, 99 insertions(+), 11 deletions(-)
Signature · 1 line
2.13.0
PATCH v3 1/2 tty: add compat_ioctl callbacks
In order to avoid future diversions between fs/compat_ioctl.c and drivers/tty/pty.c, define .compat_ioctl callbacks for the relevant tty_operations structs. Since both pty_unix98_ioctl() and pty_bsd_ioctl() are compatible between 32-bit and 64-bit userspace no special translation is required.
--- Makefile | 1 + drivers/tty/pty.c | 22 ++++++++++++++++++++++ fs/compat_ioctl.c | 6 ------ 3 files changed, 23 insertions(+), 6 deletions(-)
Makefile +1 -0
@@ -401,6 +401,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ -fno-strict-aliasing -fno-common \ -Werror-implicit-function-declaration \ -Wno-format-security \ + -Wno-error=int-in-bool-context \ -std=gnu89 $(call cc-option,-fno-PIE)
drivers/tty/pty.c +22 -0
@@ -481,6 +481,16 @@ static int pty_bsd_ioctl(struct tty_struct *tty,return -ENOIOCTLCMD ; } +static long pty_bsd_compat_ioctl (struct tty_struct *tty, +unsigned int cmd,unsigned long arg) +{ +/* +* PTY ioctls don't require any special translation between 32-bit and +* 64-bit userspace, they are already compatible. +*/ +return pty_bsd_ioctl (tty, cmd, arg); +} +static int legacy_count =CONFIG_LEGACY_PTY_COUNT ;/* * not really modular, but the easiest way to keep compat with existing
@@ -502,6 +512,7 @@ static const struct tty_operations master_pty_ops_bsd = { .chars_in_buffer = pty_chars_in_buffer, .unthrottle = pty_unthrottle, .ioctl = pty_bsd_ioctl, + .compat_ioctl = pty_bsd_compat_ioctl, .cleanup = pty_cleanup, .resize = pty_resize, .remove = pty_remove
@@ -609,6 +620,16 @@ static int pty_unix98_ioctl(struct tty_struct *tty,return -ENOIOCTLCMD ; } +static long pty_unix98_compat_ioctl (struct tty_struct *tty, +unsigned int cmd,unsigned long arg) +{ +/* +* PTY ioctls don't require any special translation between 32-bit and +* 64-bit userspace, they are already compatible. +*/ +return pty_unix98_ioctl (tty, cmd, arg); +} +/** * ptm_unix98_lookup - find a pty master * @driver: ptm driver
@@ -681,6 +702,7 @@ static const struct tty_operations ptm_unix98_ops = { .chars_in_buffer = pty_chars_in_buffer, .unthrottle = pty_unthrottle, .ioctl = pty_unix98_ioctl, + .compat_ioctl = pty_unix98_compat_ioctl, .resize = pty_resize, .cleanup = pty_cleanup };
fs/compat_ioctl.c +0 -6
@@ -866,8 +866,6 @@ COMPATIBLE_IOCTL(TIOCGDEV) COMPATIBLE_IOCTL(TIOCCBRK) COMPATIBLE_IOCTL(TIOCGSID) COMPATIBLE_IOCTL(TIOCGICOUNT) -COMPATIBLE_IOCTL(TIOCGPKT) -COMPATIBLE_IOCTL(TIOCGPTLCK) COMPATIBLE_IOCTL(TIOCGEXCL) /* Little t */ COMPATIBLE_IOCTL(TIOCGETD)
@@ -883,16 +881,12 @@ COMPATIBLE_IOCTL(TIOCMGET) COMPATIBLE_IOCTL(TIOCMBIC) COMPATIBLE_IOCTL(TIOCMBIS) COMPATIBLE_IOCTL(TIOCMSET) -COMPATIBLE_IOCTL(TIOCPKT) COMPATIBLE_IOCTL(TIOCNOTTY) COMPATIBLE_IOCTL(TIOCSTI) COMPATIBLE_IOCTL(TIOCOUTQ) COMPATIBLE_IOCTL(TIOCSPGRP) COMPATIBLE_IOCTL(TIOCGPGRP) -COMPATIBLE_IOCTL(TIOCGPTN) -COMPATIBLE_IOCTL(TIOCSPTLCK) COMPATIBLE_IOCTL(TIOCSERGETLSR) -COMPATIBLE_IOCTL(TIOCSIG) #ifdef TIOCSRS485 COMPATIBLE_IOCTL(TIOCSRS485)#endif
Signature · 1 line
2.13.0
PATCH v3 2/2 tty: add TIOCGPTPEER ioctl
In reply to Aleksa SaraiWhen 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
@@ -100,6 +100,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ #define TIOCSERCONFIG 0x5453#define TIOCSERGWILD 0x5454
arch/mips/include/uapi/asm/ioctls.h +1 -0
@@ -91,6 +91,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ /* I hope the range from 0x5480 on is free ... */ #define TIOCSCTTY 0x5480/* become controlling tty */
arch/parisc/include/uapi/asm/ioctls.h +1 -0
@@ -60,6 +60,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ #define FIONCLEX 0x5450/* these numbers need to be adjusted. */ #define FIOCLEX 0x5451
arch/powerpc/include/uapi/asm/ioctls.h +1 -0
@@ -100,6 +100,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ #define TIOCSERCONFIG 0x5453#define TIOCSERGWILD 0x5454
arch/sh/include/uapi/asm/ioctls.h +1 -0
@@ -93,6 +93,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ #define TIOCSERCONFIG _IO('T', 83)/* 0x5453 */ #define TIOCSERGWILD _IOR('T', 84, int)/* 0x5454 */
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
@@ -105,6 +105,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ #define TIOCSERCONFIG _IO('T', 83)#define TIOCSERGWILD _IOR('T', 84, int)
drivers/tty/pty.c +67 -4
115 lines not on this page · raw message
include/uapi/asm-generic/ioctls.h +1 -0
@@ -77,6 +77,7 @@#define TIOCGPKT _IOR('T', 0x38, int)/* Get packet mode state */ #define TIOCGPTLCK _IOR('T', 0x39, int)/* Get Pty lock state */ #define TIOCGEXCL _IOR('T', 0x40, int)/* Get exclusive mode state */ +#define TIOCGPTPEER _IOR('T', 0x41, int)/* Safely open the slave */ #define FIONCLEX 0x5450#define FIOCLEX 0x5451
Signature · 1 line
2.13.0
Re: [PATCH v3 1/2] tty: add compat_ioctl callbacks
In reply to Aleksa SaraiMakefile KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ · line 405
Quoted 10 lines
Makefile +1
-0@@ -401,6 +401,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ -fno-strict-aliasing -fno-common \ -Werror-implicit-function-declaration \ -Wno-format-security \ + -Wno-error=int-in-bool-context \ -std=gnu89 $(call cc-option,-fno-PIE)
Ah sorry, I committed that by accident. I'll send a fixed version.
Signature · 4 lines
Aleksa Sarai Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
kbuild test robot
build botRe: [PATCH v3 1/2] tty: add compat_ioctl callbacks
In reply to Aleksa Saraiurl: https://github.com/0day-ci/linux/commits/Aleksa-Sarai/tty-add-TIOCGPTPEER-ioctl/20170603-220322 config: i386-randconfig-x018-201722 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0…
Hi Aleksa,
[auto build test WARNING on linus/master]
[also build test WARNING on v4.12-rc3 next-20170602]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Aleksa-Sarai/tty-add-TIOCGPTPEER-ioctl/20170603-220322
config: i386-randconfig-x018-201722 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 All warnings (new ones prefixed by >>):
>> cc1: warning: -malign-functions is obsolete, use -falign-functions
>> cc1: warning: -malign-jumps is obsolete, use -falign-jumps
>> cc1: warning: -malign-loops is obsolete, use -falign-loops
cc1: error: -Werror=int-in-bool-context: no option -Wint-in-bool-context
kernel/bounds.c:1:0: error: CPU you selected does not support x86-64 instruction set
/* kernel/bounds.c:1:0: error: CPU you selected does not support x86-64 instruction set kernel/bounds.c:1:0: warning: -mregparm is ignored in 64-bit mode make[2]: *** [kernel/bounds.s] Error 1
make[2]: Target '__build' not remade because of errors.
make[1]: *** [prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH v3 1/2] tty: add compat_ioctl callbacks
In reply to Aleksa Saraiurl: https://github.com/0day-ci/linux/commits/Aleksa-Sarai/tty-add-TIOCGPTPEER-ioctl/20170603-220322 config: x86_64-randconfig-x010-201722 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3)…
Hi Aleksa,
[auto build test ERROR on linus/master]
[also build test ERROR on v4.12-rc3 next-20170602]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Aleksa-Sarai/tty-add-TIOCGPTPEER-ioctl/20170603-220322
config: x86_64-randconfig-x010-201722 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 All errors (new ones prefixed by >>):
>> cc1: error: -Werror=int-in-bool-context: no option -Wint-in-bool-context
make[2]: *** [kernel/bounds.s] Error 1 make[2]: Target '__build' not remade because of errors. make[1]: *** [prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Arnd Bergmann
maintainerRe: [PATCH v3 1/2] tty: add compat_ioctl callbacks
In reply to Aleksa SaraiQuoted 18 lines · Aleksa Sarai
In order to avoid future diversions between
fs/compat_ioctl.canddrivers/tty/pty.c, define .compat_ioctl callbacks for the relevanttty_operationsstructs. Since bothpty_unix98_ioctl()andpty_bsd_ioctl()are compatible between 32-bit and 64-bit userspace no special translation is required.Signed-off-by Aleksa Sarai <[email protected]>--- Makefile | 1 + drivers/tty/pty.c | 22 ++++++++++++++++++++++ fs/compat_ioctl.c | 6 ------ 3 files changed, 23 insertions(+), 6 deletions(-)Makefile +0
-0
Makefile KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ · line 405
Quoted 5 lines
Makefile +1
-0@@ -401,6 +401,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ -fno-strict-aliasing -fno-common \ -Werror-implicit-function-declaration \ -Wno-format-security \ + -Wno-error=int-in-bool-context \ -std=gnu89 $(call cc-option,-fno-PIE)
This slipped in by accident I assume? It seems completely unrelated.
Quoted 8 lines
drivers/tty/pty.c +0
-0@@ -481,6 +481,16 @@ static int pty_bsd_ioctl(struct tty_struct *tty,return -ENOIOCTLCMD ; }
drivers/tty/pty.c static int pty_bsd_ioctl(struct tty_struct *tty, · line 493
Quoted 10 lines
drivers/tty/pty.c +10
-0+static long pty_bsd_compat_ioctl (struct tty_struct *tty, +unsigned int cmd,unsigned long arg) +{ +/* +* PTY ioctls don't require any special translation between 32-bit and +* 64-bit userspace, they are already compatible. +*/ +return pty_bsd_ioctl (tty, cmd, arg); +} +
This looks correct but unnecessary, you can simply point both function pointers to the same function:
drivers/tty/pty.c static const struct tty_operations master_pty_ops_bsd = { · line 515
Quoted 6 lines
drivers/tty/pty.c +1
-0* not really modular, but the easiest way to keep compat with existing @@ -502,6 +512,7 @@ static const struct tty_operations master_pty_ops_bsd = { .chars_in_buffer = pty_chars_in_buffer, .unthrottle = pty_unthrottle, .ioctl = pty_bsd_ioctl, + .compat_ioctl = pty_bsd_compat_ioctl,
.compat_ioctl = pty_bsd_ioctl,
The separate handler would only be required when you need any kind of special handling depending on the command.
Quoted 17 lines
fs/compat_ioctl.c +0
-2@@ -866,8 +866,6 @@ COMPATIBLE_IOCTL(TIOCGDEV) COMPATIBLE_IOCTL(TIOCCBRK) COMPATIBLE_IOCTL(TIOCGSID) COMPATIBLE_IOCTL(TIOCGICOUNT) -COMPATIBLE_IOCTL(TIOCGPKT) -COMPATIBLE_IOCTL(TIOCGPTLCK) COMPATIBLE_IOCTL(TIOCGEXCL) /* Little t */ COMPATIBLE_IOCTL(TIOCGETD)@@ -883,16 +881,12 @@ COMPATIBLE_IOCTL(TIOCMGET) COMPATIBLE_IOCTL(TIOCMBIC) COMPATIBLE_IOCTL(TIOCMBIS) COMPATIBLE_IOCTL(TIOCMSET)
fs/compat_ioctl.c COMPATIBLE_IOCTL(TIOCMGET) · line 895
Quoted 10 lines
fs/compat_ioctl.c +0
-4-COMPATIBLE_IOCTL(TIOCPKT) COMPATIBLE_IOCTL(TIOCNOTTY) COMPATIBLE_IOCTL(TIOCSTI) COMPATIBLE_IOCTL(TIOCOUTQ) COMPATIBLE_IOCTL(TIOCSPGRP) COMPATIBLE_IOCTL(TIOCGPGRP) -COMPATIBLE_IOCTL(TIOCGPTN) -COMPATIBLE_IOCTL(TIOCSPTLCK) COMPATIBLE_IOCTL(TIOCSERGETLSR) -COMPATIBLE_IOCTL(TIOCSIG)
Looks good.
Arnd
Re: [PATCH v3 1/2] tty: add compat_ioctl callbacks
Quoted 4 lines
Quoted 4 lines
Makefile +0
-0
Quoted 8 lines
Makefile KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ · line 405
Quoted 6 lines
Makefile +1
-0@@ -401,6 +401,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ -fno-strict-aliasing -fno-common \ -Werror-implicit-function-declaration \ -Wno-format-security \ + -Wno-error=int-in-bool-context \ -std=gnu89 $(call cc-option,-fno-PIE)This slipped in by accident I assume? It seems completely unrelated.
Yeah, I re-sent v4 with this removed immediately afterwards.
Quoted 9 lines
Quoted 9 lines
drivers/tty/pty.c +0
-0@@ -481,6 +481,16 @@ static int pty_bsd_ioctl(struct tty_struct *tty,return -ENOIOCTLCMD ; }
Quoted 13 lines
drivers/tty/pty.c static int pty_bsd_ioctl(struct tty_struct *tty, · line 493
Quoted 10 lines
drivers/tty/pty.c +10
-0+static long pty_bsd_compat_ioctl (struct tty_struct *tty, +unsigned int cmd,unsigned long arg) +{ +/* +* PTY ioctls don't require any special translation between 32-bit and +* 64-bit userspace, they are already compatible. +*/ +return pty_bsd_ioctl (tty, cmd, arg); +} +This looks correct but unnecessary, you can simply point both function pointers to the same function:
They have different types, since they have different return types:
int (*ioctl)(struct tty_struct *tty,
unsigned int cmd, unsigned long arg); long (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
If you like, I can change (*ioctl) to return longs as well, and then change all of the call-sites (since unlocked_ioctl also returns long).
Signature · 4 lines
Aleksa Sarai Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Arnd Bergmann
maintainerRe: [PATCH v3 1/2] tty: add compat_ioctl callbacks
Quoted 27 lines · Aleksa Sarai
Quoted 8 lines
Quoted 8 lines
drivers/tty/pty.c +0
-0@@ -481,6 +481,16 @@ static int pty_bsd_ioctl(struct tty_struct *tty,return -ENOIOCTLCMD ; }Quoted 15 lines
drivers/tty/pty.c static int pty_bsd_ioctl(struct tty_struct *tty, · line 493
Quoted 11 lines
drivers/tty/pty.c +5
-0+static long pty_bsd_compat_ioctl (struct tty_struct *tty, +unsigned int cmd,unsigned long arg) +{ +/* +* PTY ioctls don't require any special translation between 32-bit and
drivers/tty/pty.c +5
-0+* 64-bit userspace, they are already compatible. +*/ +return pty_bsd_ioctl (tty, cmd, arg); +} +This looks correct but unnecessary, you can simply point both function pointers to the same function:
They have different types, since they have different return types:
Quoted 7 lines
int (*ioctl)(struct
tty_struct*tty,unsigned int cmd, unsigned long arg); long (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg);If you like, I can change (*ioctl) to return longs as well, and then change all of the call-sites (since
unlocked_ioctlalso returns long).
Ah, my mistake. In most other data structures that have a compat_ioctl callback pointer, the prototypes are the same, and I had not realized that tty_operations is an exception.
Arnd