Skip to content

pty: add GetPtyFromFile as safer GetPty #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025

Conversation

cyphar
Copy link
Contributor

@cyphar cyphar commented May 19, 2025

Security-conscious callers may wish to have more control over opening
/dev/ptmx, and so GetPty is not suitable for them. Previously it was not
possible for such users to open /dev/ptmx and then use this package to
create new ptys and manage the console.

On Linux, the intended usage would be for a daemon to get an O_PATH
handle to /dev/ptmx that can then be re-opened (through procfs) to get a
new handle to /dev/ptmx. I suspect on FreeBSD you could use O_EMPTY_PATH
to accomplish something similar.

Signed-off-by: Aleksa Sarai [email protected]

cyphar added 3 commits May 19, 2025 13:57
v1.1.0 was released in 2022, and since then there have been some project
renames that cause the old version to no longer work, breaking CI:

  go: github.com/kunalkushwaha/ltag@latest: github.com/kunalkushwaha/[email protected]: parsing go.mod:
  	module declares its path as: github.com/containerd/ltag
  	        but was required as: github.com/kunalkushwaha/ltag

Signed-off-by: Aleksa Sarai <[email protected]>
In a future patch we will split GetPty() so that users can pass a File
as the already-opened pty master, but in order for us to be able to use
the same File interface as our other external APIs we need to migrate
our internal handlers to use File rather than *os.File.

This change has no user-visible impact, as the underlying types used are
identical.

Signed-off-by: Aleksa Sarai <[email protected]>
Security-conscious callers may wish to have more control over opening
/dev/ptmx, and so GetPty is not suitable for them. Previously it was not
possible for such users to open /dev/ptmx and then use this package to
create new ptys and manage the console.

On Linux, the intended usage would be for a daemon to get an O_PATH
handle to /dev/ptmx that can then be re-opened (through procfs) to get a
new handle to /dev/ptmx. I suspect on FreeBSD you could use O_EMPTY_PATH
to accomplish something similar.

Signed-off-by: Aleksa Sarai <[email protected]>
@cyphar cyphar force-pushed the newpty-from-file branch from 13f3a19 to c79e45e Compare May 19, 2025 03:57
@@ -31,7 +30,7 @@ const (

// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
// unlockpt should be called before opening the slave side of a pty.
func unlockpt(f *os.File) error {
func unlockpt(f File) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we can rather drop the File interface and just use *os.File, assuming that it is the only implementation of the interface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm it was introduced due to the GC stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is an remarkably common issue to run into with *os.File. It's a shame there isn't a properly supported way of doing this other than hacking around with runtime.SetFinalizer which is (imho) incredibly dodgy.

To be fair, even Rust only really solved this issue in 1.63 with OwnedFd and BorrowedFd...

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will merge and release

@AkihiroSuda AkihiroSuda merged commit c8d9621 into containerd:main May 20, 2025
1 check passed
@cyphar cyphar deleted the newpty-from-file branch May 20, 2025 06:36
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.

2 participants