TTY privilege escalation in TIOCSTI ioctl
Subsystems (2)
cover TTY privilege escalation in TIOCSTI ioctl
The TIOCSTI ioctl incorrectly uses only capable(CAP_SYS_ADMIN), which checks the current process's credentials instead of also checking the file opener's credentials stored in file->f_cred.
This creates a TOCTOU vulnerability where an unprivileged process can open a TTY fd and pass it to a privileged process via SCM_RIGHTS. The privileged process then inadvertently grants access based on its elevated privileges.
Problem Impact
==============
This vulnerability allows privilege escalation attacks where:
- Unprivileged processes can inject keystrokes into privileged terminals
- Container escape scenarios via terminal manipulation
- Bypass of intended access controls for TTY operations
Solution Approach
=================
This series addresses the vulnerability through SELinux LSM integration rather than modifying core TTY credential checking to avoid potential compatibility issues with existing userspace.
The fix adds proper current task and file credential capability validation in SELinux's selinux_file_ioctl() hook specifically for TIOCSTI operations.
Testing
=======
All patches have been validated using:
- scripts/checkpatch.pl --strict (0 errors, 0 warnings)
- Functional testing on kernel v6.16-rc2
- TOCTOU attack vector verification
- SELinux policy enforcement testing
The fd_passing_security test demonstrates the vulnerability. To verify, disable legacy TIOCSTI and run the test:
$ echo "0" | sudo tee /proc/sys/dev/tty/legacy_tiocsti $ sudo ./tools/testing/selftests/tty/tty_tiocsti_test -t fd_passing_security
Patch Overview
==============
PATCH 1/2: selftests/tty: add TIOCSTI test suite
Comprehensive test suite demonstrating the vulnerability and fix validation
PATCH 2/2: selinux: add capability checks for TIOCSTI ioctl Core security fix via SELinux LSM hook
References
==========
- tty_ioctl(4) - documents TIOCSTI ioctl and capability requirements
- commit 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled")
- Documentation/security/credentials.rst
- https://github.com/KSPP/linux/issues/156
- https://lore.kernel.org/linux-hardening/Y0m9l52AKmw6Yxi1@hostpad/
---
Abhinav Saxena (2):
selftests/tty: add TIOCSTI test suite
selinux: add capability checks for TIOCSTI ioctl
security/selinux/hooks.c | 6 + tools/testing/selftests/tty/Makefile | 6 +- tools/testing/selftests/tty/config | 1 + tools/testing/selftests/tty/tty_tiocsti_test.c | 421 +++++++++++++++++++++++++ 4 files changed, 433 insertions(+), 1 deletion(-)
---
base-commit: 5adb635077d1b4bd65b183022775a59a378a9c00
change-id: 20250618-toicsti-bug-7822b8e94a32
Best regards,
Signature · 1 line
Abhinav Saxena <[email protected]>
PATCH 1/2 selftests/tty: add TIOCSTI test suite
TIOCSTI is a TTY ioctl command that allows inserting characters into the terminal input queue, making it appear as if the user typed those characters.
Add a test suite with four tests to verify TIOCSTI behaviour in different scenarios when dev.tty.legacy_tiocsti is both enabled and disabled:
- Test TIOCSTI functionality when legacy support is enabled
- Test TIOCSTI rejection when legacy support is disabled
- Test capability requirements for TIOCSTI usage
- Test TIOCSTI security with file descriptor passing
The tests validate proper enforcement of the legacy_tiocsti sysctl introduced in commit 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled"). See tty_ioctl(4) for details on TIOCSTI behavior and security requirements.
--- tools/testing/selftests/tty/Makefile | 6 +- tools/testing/selftests/tty/config | 1 + tools/testing/selftests/tty/tty_tiocsti_test.c | 421 +++++++++++++++++++++++++ 3 files changed, 427 insertions(+), 1 deletion(-)
tools/testing/selftests/tty/Makefile +5 -1
@@ -1,5 +1,9 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS = -O2 -Wall -TEST_GEN_PROGS := tty_tstamp_update +TEST_GEN_PROGS := tty_tstamp_update tty_tiocsti_test +LDLIBS += -lcap include ../lib.mk + +# Add libcap for TIOCSTI test +$(OUTPUT)/tty_tiocsti_test: LDLIBS += -lcap
tools/testing/selftests/tty/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_LEGACY_TIOCSTI=y
tools/testing/selftests/tty/tty_tiocsti_test.c new file +421 -0
421 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/2 selinux: add capability checks for TIOCSTI ioctl
In reply to Abhinav SaxenaThe TIOCSTI ioctl currently only checks the current process's credentials, creating a TOCTOU vulnerability where an unprivileged process can open a TTY fd and pass it to a privileged process via SCM_RIGHTS.
Fix by requiring BOTH the file opener (file->f_cred) AND the current process to have CAP_SYS_ADMIN. This prevents privilege escalation while ensuring legitimate use cases continue to work.
--- security/selinux/hooks.c | 6 ++++++ 1 file changed, 6 insertions(+)
security/selinux/hooks.c +6 -0
@@ -3847,6 +3847,12 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd, CAP_OPT_NONE, true);break ; +case TIOCSTI : +if (!file_ns_capable (file, &init_user_ns,CAP_SYS_ADMIN ) || + !capable (CAP_SYS_ADMIN )) + error = -EPERM ; +break ; +case FIOCLEX :case FIONCLEX :if (!selinux_policycap_ioctl_skip_cloexec ())
Signature · 1 line
2.43.0