Skip to contentSkip to conversation

selftests/tty: add TIOCSTI test suite

PATCH Superseded Abhinav Saxena 2 participants 3 messages Sep 3, 2025 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 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. This functionality has behavior that varies based on system configuration and process credentials.

The dev.tty.legacy_tiocsti sysctl introduced in commit 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled") controls TIOCSTI usage. When disabled, TIOCSTI requires CAP_SYS_ADMIN capability.

The current implementation checks the current process's credentials via capable(CAP_SYS_ADMIN), but does not validate against the file opener's credentials stored in file->f_cred. This creates different behavior when file descriptors are passed between processes via SCM_RIGHTS.

Add a test suite with 16 test variants using fixture variants to verify TIOCSTI behavior when dev.tty.legacy_tiocsti is enabled/disabled:

- Basic TIOCSTI tests (8 variants): Direct testing with different capability and controlling terminal combinations
- FD passing tests (8 variants): Test behavior when file descriptors are passed between processes with different capabilities

The FD passing tests document this behavior - some tests show different results than expected based on file opener credentials, demonstrating that TIOCSTI uses current process credentials rather than file opener credentials.

The tests validate proper enforcement of the legacy_tiocsti sysctl. Test implementation uses openpty(3) with TIOCSCTTY for isolated PTY environments. See tty_ioctl(4) for details on TIOCSTI behavior and security requirements.

---
Changes in v4:
- Moved skip conditions and sysctl setup from TEST_F to FIXTURE_SETUP (Kees Cook)
- Fixed fclose() error handling in set_legacy_tiocsti_setting (Kees Cook)
- Extracted run_basic_tiocsti_test() and run_fdpass_tiocsti_test functions (Kees Cook)
- Removed redundant sysctl restore logic from TEST_F (Kees Cook)
- Simplified FIXTURE_TEARDOWN (Kees Cook)
- Replace drop_to_nobody() to drop_all_privs() which should be more portable (Justin Stitt)
- Link to v3: https://lore.kernel.org/r/[email protected]

Add selftests for TIOCSTI ioctl

To run all tests:

$ sudo ./tools/testing/selftests/tty/tty_tiocsti_test

Test Results:
- PASSED: 13/16 tests
- Different behavior: 3/16 tests (documenting credential checking behavior)

All tests validated using:
- scripts/checkpatch.pl --strict (clean output)
- Functional testing on kernel v6.16-rc2

Changes in v3:
- Replaced all printf() calls with TH_LOG() for proper test logging (Kees Cook)
- Added struct __test_metadata parameter to helper functions
- Moved common legacy_tiocsti availability check to FIXTURE_SETUP()
- Implemented sysctl modification/restoration in FIXTURE_SETUP/TEARDOWN
- Used openpty() with TIOCSCTTY for reliable PTY testing environment
- Fixed child/parent synchronization in FD passing tests
- Replaced manual _exit(1) handling with proper ASSERT statements
- Switched // comments to /* */ format throughout
- Expanded to 16 test variants using fixture variants
- Enhanced error handling and test reliability
- Link to v2: https://lore.kernel.org/r/[email protected]
- Link to v1: https://lore.kernel.org/r/[email protected]

References:
- tty_ioctl(4) - documents TIOCSTI ioctl and capability requirements
- openpty(3) - pseudo-terminal creation and management
- 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/
- drivers/tty/Kconfig
- Documentation/driver-api/tty/

---
 tools/testing/selftests/tty/Makefile           |   6 +-
 tools/testing/selftests/tty/config             |   1 +
 tools/testing/selftests/tty/tty_tiocsti_test.c | 650 +++++++++++++++++++++++++
 3 files changed, 656 insertions(+), 1 deletion(-)
tools/testing/selftests/tty/Makefile +5 -1
index 50d7027b2ae3fb495dd1c0684363fa8f426be42c..7f6fbe5a0cd5663310e334d9d068b21dab9136ec 100644
@@ -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
new file mode 100644
index 0000000000000000000000000000000000000000..c6373aba66366c82435bb26c019eb360eb6310eb
@@ -0,0 +1 @@
+CONFIG_LEGACY_TIOCSTI=y
tools/testing/selftests/tty/tty_tiocsti_test.c new file +650 -0

650 lines not on this page · raw message

---
base-commit: e6b9dce0aeeb91dfc0974ab87f02454e24566182
change-id: 20250618-toicsti-bug-7822b8e94a32

Best regards,

Signature · 1 line
Abhinav Saxena <[email protected]>
#1 lore raw
Quoted 4 lines · Abhinav Saxena

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. This functionality has behavior that varies based on system configuration and process credentials.

<snip>

Any specific reason you didn't cc: the tty/serial maintainers and the linux-serial list for this?

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 6 lines · Greg KH
Quoted 4 lines · Abhinav Saxena

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. This functionality has behavior that varies based on system configuration and process credentials.

Quoted 8 lines

<snip>

Any specific reason you didn’t cc: the tty/serial maintainers and the linux-serial list for this?

thanks,

greg k-h

My apologies for the oversight. I used b4 to collect to/cc addresses, and didn’t confirm them before sending the patch. I guess since this is just a selftest, it did not include tty/serial maintainers.

I’ll resend the series.

-Abhinav