tools/testing/selftests/tty/tty_tiocsti_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/tty/tty_tiocsti_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/tty/tty_tiocsti_test.c
Extension
.c
Size
17548 bytes
Lines
651
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (current_value != self->original_legacy_tiocsti_setting) {
			TH_LOG("Backup: Restoring legacy_tiocsti from %d to %d",
			       current_value,
			       self->original_legacy_tiocsti_setting);
			set_legacy_tiocsti_setting(
				_metadata,
				self->original_legacy_tiocsti_setting);
		}
	}

	if (self->has_pty) {
		if (self->pty_master_fd >= 0)
			close(self->pty_master_fd);
		if (self->pty_slave_fd >= 0)
			close(self->pty_slave_fd);
	}
}

TEST_F(tiocsti, test)
{
	int status;
	pid_t child_pid;

	if (variant->test_type == TEST_PTY_TIOCSTI_BASIC) {
		/* ===== BASIC TIOCSTI TEST ===== */
		child_pid = fork();
		ASSERT_GE(child_pid, 0);

		/* Perform the actual test in the child process */
		if (child_pid == 0)
			run_basic_tiocsti_test(_metadata, self, variant);

	} else {
		/* ===== FD PASSING SECURITY TEST ===== */
		int sockpair[2];

		ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair), 0);

		child_pid = fork();
		ASSERT_GE(child_pid, 0);

		if (child_pid == 0) {
			/* Child process - create PTY and send FD */
			close(sockpair[0]);
			run_fdpass_tiocsti_test(_metadata, variant,
						sockpair[1]);
		}

		/* Parent process - receive FD and test TIOCSTI */
		close(sockpair[1]);

		int received_fd = recv_fd_via_socket(sockpair[0]);

		ASSERT_GE(received_fd, 0);

		bool parent_has_cap = self->initial_cap_sys_admin;

		TH_LOG("=== TIOCSTI FD Passing Test Context ===");
		TH_LOG("legacy_tiocsti: %d, Parent CAP_SYS_ADMIN: %s, Child: %s",
		       variant->legacy_tiocsti, parent_has_cap ? "yes" : "no",
		       variant->requires_cap ? "kept" : "dropped");

		/* SECURITY TEST: Try TIOCSTI with FD opened by child */
		int result = test_tiocsti_injection(_metadata, received_fd);

		/* Log security concern if demonstrated */
		if (result == 0 && !variant->requires_cap) {
			TH_LOG("*** SECURITY CONCERN DEMONSTRATED ***");
			TH_LOG("Privileged parent can use TIOCSTI on FD from unprivileged child");
			TH_LOG("This shows current process credentials are used, not opener credentials");
		}

		EXPECT_EQ(result, variant->expected_success)
		{
			TH_LOG("FD passing: expected error %d, got %d",
			       variant->expected_success, result);
		}

		/* Signal child completion */
		char sync_byte = 'D';
		ssize_t bytes_written = write(sockpair[0], &sync_byte, 1);

		ASSERT_EQ(bytes_written, 1);

		close(received_fd);
		close(sockpair[0]);
	}

	/* Common child process cleanup for both test types */
	ASSERT_EQ(waitpid(child_pid, &status, 0), child_pid);

Annotation

Implementation Notes