Skip to content

system/term: Resolve the current tty once and share it - #1684

Open
3v1n0 wants to merge 3 commits into
trifectatechfoundation:mainfrom
3v1n0:tty-resolve-once
Open

3v1n0 wants to merge 3 commits into
trifectatechfoundation:mainfrom
3v1n0:tty-resolve-once

Conversation

@3v1n0

@3v1n0 3v1n0 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

The controlling tty was resolved independently each time it was needed:
for PAM_TTY, for the log entry and for the session record scope. Each
time this meant reading /proc/self/stat and possibly scanning /dev/pts
and /dev.

Resolve it once when building the sudo context instead, as sudo.ws does
in get_user_info(), and pass its device and reported name to all the
users, so that they are also guaranteed to agree on the terminal.

Also ensures that the opened device is the one we expected to.
Sadly we cannot just pass the fd as PAM_TTY as many modules require it to
be a /dev/*.

Closes: #1683


This is built to be a prerequisite of #1682 (and in fact the second commit includes it) as IMHO it would be cleaner in this order, but if you prefer to just go with the fix we can go with that one and then this can be rebased to be a refactor of that only.

@squell

squell commented Sep 29, 2026

Copy link
Copy Markdown
Member

This is built to be a prerequisite of #1682 (and in fact the second commit includes it) as IMHO it would be cleaner in this order, but if you prefer to just go with the fix we can go with that one and then this can be rebased to be a refactor of that only.

Just to clarify: you mean that if we decide to merge this, we can close #1682, right?

Comment thread src/sudo/mod.rs Outdated
Comment thread src/system/term/mod.rs Outdated
Comment thread src/system/term/mod.rs
3v1n0 added 3 commits October 12, 2026 18:40
The controlling tty was resolved independently each time it was needed:
 - To compute PAM_TTY
 - For the log entry
 - For session record scope.

Each time this meant reading /proc/self/stat and possibly scanning /dev/pts
and /dev.

Resolve it once when building the sudo context instead, as sudo.ws does
in get_user_info(), and pass its device and reported name to all the
users, so that they are also guaranteed to agree on the terminal.

Closes: trifectatechfoundation#1683
The controlling tty path is resolved before being opened for locking, so
in the meantime it may have started referring to another terminal, for
example if ours got closed and its pts index was reallocated.

Add CurrentTty::open_verified() to open the path and check through the
file descriptor that it still refers to the controlling tty device, and
use it when locking, falling back to /dev/tty otherwise.
When opening the controlling tty by path, use O_NOCTTY so that it can
never become our controlling terminal, and O_NONBLOCK to avoid blocking
on serial lines that are waiting for carrier, as we only need it for
locking.

This matches what sudo.ws does when it opens the tty by path to relabel
it in selinux.c.
@3v1n0
3v1n0 requested a review from bjorn3 October 12, 2026 16:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lookup tty to be used just once

3 participants