90 Commits
Author SHA1 Message Date
Shailend ChandandgVisor bot b943164ed6 Make every user of a tty also refcount
A tty inode was kept alive by a single ownership chain: the stdio file
description's reference on the import-time dentry. Everything else that used
the inode referred to it uncounted: a file description opened via /dev/tty
pins only the devtmpfs dentry, and the controlling-terminal association
(ThreadGroup.tty) is a bare pointer.

PiperOrigin-RevId: 962479941
2026-08-10 18:19:56 -07:00
Shayon Mukherjee 5b002692af vfs: skip write tracking for special files on open
Linux skips the mount-level write check when opening a character device, block device, FIFO, or socket, so write-opening /dev/null on a read-only bind mount succeeds.

Mark the remaining FileDescriptions backed by special-file inodes. Also skip overlay copy-up for writable opens of special files; Linux overlayfs applies the same special_file() exception before deciding whether copy-up is required.

Add regression coverage for character devices and FIFOs through read-only bind mounts.
2026-07-28 06:47:46 -04:00
Ryan El Kochta e4f61599b0 devpts: implement TIOCGPTPEER ioctl
Provides a safer way for userspace to fetch the replica/slave end of a
pty from the master.
2026-06-16 14:58:54 -07:00
Ayush RanjanandgVisor bot fbc1d79883 Fix SIGWINCH delivery on PTY window size change (TIOCSWINSZ)
When SIGWINCH causes the window size to change, signal the replica's foreground
process group. This is similar to what Linux does.

Also implement TIOCSCTTY/TIOCNOTTY and SIGWINCH delivery on TIOCSWINSZ
for host-imported TTYs, which previously did not support controlling
terminals or window size change signals.

Add syscall tests in pty.cc and host_pty.cc to verify the behavior.

Co-authored-by: Skywalkr-dev <[email protected]>

Fixes #13317

PiperOrigin-RevId: 927482382
2026-06-05 14:29:26 -07:00
Tan Yifeng 189d02a850 pty: Send SIGHUP to foreground process group on master close
When the PTY master is closed, send SIGHUPto the foreground
process group of the session that has the slave as its controlling
terminal. This matches Linux's pty_close() -> tty_vhangup(tty->link)
behavior (drivers/tty/pty.c).

Without this, child processes spawned via PTY (e.g. by expect/telnet)
are never notified of the hangup and remain running indefinitely.

Signed-off-by: Tan Yifeng <[email protected]>
2026-04-15 11:14:29 +08:00
Tan Yifeng c141beed3b pty: Implement TCSBRK and TCFLSH
Linux PTYs accept TCSBRK as a no-op and implement TCFLSH by
flushing the input and/or output queues. gVisor returned ENOTTY,
which breaks programs such as pyserial during terminal setup.

Handle TCSBRK in the PTY ioctl paths and route TCFLSH through
lineDiscipline.tcFlush(), including packet mode flush notification
and queue wakeups, to match Linux PTY semantics.

Signed-off-by: Tan Yifeng <[email protected]>
2026-04-10 20:35:27 +08:00
Tan Yifeng d0fbf33e2a devpts: Check ISIG flag before generating signals for VINTR/VSUSP/VQUIT
Signal characters (Ctrl-C/Z/\) unconditionally generated signals
regardless of the ISIG flag. Linux only generates these signals when
L_ISIG(tty) is true (n_tty.c:n_tty_receive_char_special).

Also fix signal characters leaking into the read buffer — Linux
consumes them in n_tty_receive_signal_char() and never delivers
them to the reading process.

Signed-off-by: Tan Yifeng <[email protected]>
2026-03-25 17:55:46 +08:00
gVisor bot 8c3815adbd devpts: Correct minor issues with TCGETS2 support
- Remove stale TCGETS, etc. cases from maybeEmitUnimplementedEvent
- Avoid test runner redundantly passing hostTTYFile in certain cases

PiperOrigin-RevId: 885097262
2026-03-17 10:40:56 -07:00
Jeremy Maitin-Shepard 3069630751 devpts: Support TCGETS2, TCSETS2, TCSETSW2, TCSETSF2
glibc 2.42 uses TCGETS2 in isatty, and fails to properly detect ttys
without TCGETS2 supported.

This commit also adds support for passing additional TTY file
descriptors (fd numbers > 2) from the host, which is needed by the
new host_pty test but may also be independently useful.

In line_discipline.go, ensure that termios state is not overwritten
on CopyIn failure (e.g. EFAULT). This was reported in review. A
regression test is added.

Move kernel_termios operators to pty_util for reuse.
2026-02-25 09:53:47 -08:00
Shailend ChandandgVisor bot e428bbea51 Fix creds check in linkat(AT_EMPTY_PATH)
linkat(AT_EMPTY_PATH) needs to check for CAP_DAC_READ_SEARCH in the userns in
which the fd was opened, not the userns of the task running linkat().

To get at that userns, vfs.FileDescription now saves a pointer to the creds
at the time of its creation: plumbing these creds in accounts for much of the
diff. This is similar to how `struct file` stores creds in `f_cred` in Linux.

Moreover, since the Linux patch "vfs: relax linkat() AT_EMPTY_PATH..." [1], the
need for this cap is excused if the creds of the calling task are literally
the same as the one stored in the fd.

[1] https://github.com/torvalds/linux/commit/42bd2af5950456d

PiperOrigin-RevId: 853374474
2026-01-07 12:48:17 -08:00
Nicolas LacasseandgVisor bot 21061daa40 Move host TTYOperations to the host inode struct.
The TTYOperations for a donated host TTY were on the TTYFileDescription, but
that scopes them per-FD, when they should be scoped per-inode.

This CL moves the TTYOperations logic to the host inode struct, rather than the
TTYFileDescription. It also renames TTYOperations.Open ->
TTYOperations.TTYOpen, to avoid a naming collision with kernfs.inodeOpen.

As a result, opening a TTY (like via /dev/tty) always goes through inode.open,
thus creating a new TTYFileDescrption every time, whereas previously we
incorrectly returned the same TTYFileDescription each time.

Fixes #11644

PiperOrigin-RevId: 752856291
2025-04-29 13:20:18 -07:00
Nicolas LacasseandgVisor bot a4ec2b3270 Implement basic packet mode support for ptys.
From man TIOCPKT:
"""
In packet mode, each subsequent read(2) will return a packet that either
contains a single nonzero control byte, or has a single byte containing zero
('\0') followed by data written on the slave side of the pseudoterminal."
"""

This CL implements only the data portion of packet mode, not the control bytes,
but that seems to be enough to get xfce4-terminal to work.

PiperOrigin-RevId: 737175092
2025-03-15 09:26:56 -07:00
Nicolas LacasseandgVisor bot 14c83122c9 Clean up tty.CheckChange and call it in SetForegroundProcessGroup.
Previously, CheckChange (corresponding to Linux's tty/tty_check_change()) was
only used the host TTY implementation, not the devpts implementation.

Furthermore, ThreadGroup.SetForegroundProcessGroup() duplicated some of the
logic in CheckChange, notably sending SIGTTOU to background tasks. This means
that, for host TTYs, we could send SIGTTOU multiple times. In some
circumstances, this leads the ioctl returning ERESTARTSYS in an infinite loop.

PiperOrigin-RevId: 735934036
2025-03-11 16:46:55 -07:00
Andrei VaginandgVisor bot 03044d7bea proc: invalidate task inodes when tasks are destroyed
PiperOrigin-RevId: 705785809
2024-12-13 00:58:08 -08:00
Kevin KrakauerandgVisor bot 8440f41ea0 remove outdated bug references for PTYs
PiperOrigin-RevId: 702986132
2024-12-04 22:43:42 -08:00
Nicolas LacasseandgVisor bot 1a7df035d8 Clean up host.TTYFileOperations.
We used to track the foreground process group & session on the
TTYFileOperation, but these are already tracked in kernel.TTY.ThreadGroup.

So remove TTYFileOperations.fgProcessGroup and .session, and replace them with
a kernel.TTY.

This is analogous to how sentry-internal tty's already work.

Updates #10925

PiperOrigin-RevId: 681957240
2024-10-03 11:25:52 -07:00
Nicolas LacasseandgVisor bot 43d65c0f6c Implement /dev/tty for sentry-internal ttys.
The /dev/tty acts as a replica for the current thread group's controlling
terminal.

In a follow-up, I will make /dev/tty work for donated host ttys.

Updates #10925

PiperOrigin-RevId: 681629892
2024-10-02 16:23:54 -07:00
Jamie LiuandgVisor bot 0bc2dd9af6 kernfs: invalidate descendants of dentries failing revalidation
Adapted from cl/630063475.

This causes FUSE tests involving submounts to fail, because fuse.inode.Valid()
just returns false (fails revalidation) after the entry time expires, causing
all submounts to be unmounted; change it to perform revalidation instead, a la
Linux's fs/fuse/dir.c:fuse_dentry_revalidate(). This in turn requires that we
plumb the dentry's parent and name through kernfs.Inode.Valid().

PiperOrigin-RevId: 630476483
2024-05-03 13:13:36 -07:00
dongjinlong e6d7cba96c chore: remove repetitive words in comments
Signed-off-by: dongjinlong <[email protected]>
2024-03-26 19:57:40 +08:00
gVisor bot d2038ebb99 Merge pull request #9954 from thecodingwizard:fix-pty-eof
PiperOrigin-RevId: 609075893
2024-02-21 12:02:14 -08:00
gVisor bot 56c3e23d62 Merge pull request #9950 from thecodingwizard:master
PiperOrigin-RevId: 605691085
2024-02-09 11:47:53 -08:00
Ayush RanjanandgVisor bot 2eebbfb345 Notify master waiter when all replicas are closed.
Fixes #9951
Fixes #9333

PiperOrigin-RevId: 605462571
2024-02-08 16:41:01 -08:00
Nathan Wang 7ae15e703d Add partial support for VERASE and VWERASE in canonical mode in PTYs 2024-02-08 01:59:41 +00:00
Nathan Wang 9014dadb19 Fix EOFs when reading from empty canon->noncanon PTY
when switching from canonical to non-canonical mode, only mark inQueue as readable if there is input to be read
2024-02-02 23:50:03 +00:00
Nathan Wang d7ff07cfd7 Do not return ErrWouldBlock when writing nothing to PTY replica output
Previously, writing an empty string to the PTY replica output queue
would return linux.ErrWouldBlock. This PR fixes this behavior to
not return an error.
2024-02-01 20:38:59 +00:00
Andrei VaginandgVisor bot cf09f1a5d5 devpts: IterDirents has to check offset and return if it is out of range
panic: runtime error: slice bounds out of range [124:0]

goroutine 136 [running]:
panic({0x3ef9e0?, 0xc000848b28?})
	GOROOT/src/runtime/panic.go:1017
runtime.goPanicSliceB(0x7c, 0x0)
	GOROOT/src/runtime/panic.go:154
gvisor.dev/gvisor/pkg/sentry/fsimpl/devpts.(*rootInode).IterDirents
	pkg/sentry/fsimpl/devpts/devpts.go:342
gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs.(*GenericDirectoryFD).IterDirents
	pkg/sentry/fsimpl/kernfs/fd_impl_util.go:224

Reported-by: [email protected]
PiperOrigin-RevId: 583183528
2023-11-16 15:30:03 -08:00
Etienne PerotandgVisor bot f8117a37b4 Add a size limit to outputQueueTransformer.transform.
This applies a similar limit as `inputQueueTransformer.transform`.

The goal is to avoid large allocations of memory.

I'm not sure if this is the right thing to do, but it seems better than the
status quo of not having any limits.

PiperOrigin-RevId: 581034478
2023-11-09 14:59:32 -08:00
Andrei VaginandgVisor bot 2866c8ed2f fsimpl/devpts: handle mode, ptmxmode, uid, gid mount options
PiperOrigin-RevId: 576049946
2023-10-24 00:56:23 -07:00
gVisor bot a423565638 Merge pull request #9339 from kevinGC:tcsetsf2
PiperOrigin-RevId: 563207341
2023-09-06 13:58:08 -07:00
gVisor bot 3155f15acd Merge pull request #9336 from thundergolfer:jonathon/dev-pts-eof-handling
PiperOrigin-RevId: 563199056
2023-09-06 13:29:19 -07:00
Jonathon Belotti 7d9e612057 Fix EOF (^D) handling in devpts (partially addr #9333) 2023-09-06 01:18:18 +00:00
Kevin Krakauer dbbce69479 tty: enable TCSETSF
While there is a difference between flavors of this ioctl (`TCSETS`,
`TCSETSF`, `TCSETSW`), in practice it seems not to matter. We should let
users call `TCSETSF` and can implement those differences if necessary.
2023-09-05 09:53:50 -07:00
Nicolas LacasseandgVisor bot b9124318dc Mark some kernfs inode as Anonymous.
These inodes can never be part of a filesystem tree. They are nameless and
never have a parent.

This allows us to avoid taking a lock in kernfs.InotifyWithParent for such
anonymous inodes.

PiperOrigin-RevId: 538823227
2023-06-08 10:25:04 -07:00
gVisor bot 6194407feb [pty] Return enqueued data and then EIO if the replica is closed
PiperOrigin-RevId: 532496868
2023-05-16 10:15:01 -07:00
Etienne PerotandgVisor bot c6d19db4e4 Update unimpl.EmitUnimplementedEvent interface to add the syscall number.
This catches up the interface to the `EmitUnimplementedEvent` method signature
on `kernel.Kernel`.

Also add build-time test to verify that `kernel.Kernel` implements this
interface, in order to catch such breakages at build time in the future.

PiperOrigin-RevId: 519000411
2023-03-23 17:01:37 -07:00
Adin ScannellandgVisor bot 2911d8c3b8 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Nicolas LacasseandgVisor bot 9c40de70bc Clean up devpts code, and deduplicate the foreground process state.
We no longer store the foreground process directly in the terminal. Instead, we
get it from the terminal TTY's ThreadGroup. Added a new method:
tty.SignalForegroundProcessGroup to simplify this.

Cleaned up some things along the way:
* Terminal had a bunch of methods to get/set foreground process group and
  controlling TTY, but those methods were only usable by Ioctl, since they
  read/wrote to syscall arguments. I moved that logic to Ioctl, and deleted the
  methods from Terminal, which is now a very simple type.
* Fixed a bug in ThreadGroud.SetForegroundProcessGroup where we were
  overwriting the ID of an existing process group, rather than setting a new
  process group on the session.
* Simplified the construction of lineDiscipline type.

Reported-by: [email protected]
PiperOrigin-RevId: 512330758
2023-02-25 14:08:58 -08:00
Wim bbba316026 Implement code review remarks 2023-02-23 13:16:56 +01:00
Wim 753639abc7 Add reference to terminal in lineDiscipline 2023-02-22 22:33:37 +01:00
Wim ffc7eac5cb Support SIGQUIT 2023-02-19 20:00:26 +01:00
Wim da28fee279 Support some PTY control characters 2023-02-19 17:08:17 +01:00
Lucas ManningandgVisor bot 502a959a14 Defer kernfs openflag handling to inode implementations.
Some implementations handle more flags than others, so it doesn't
make sense to have one set of rules for all.

This change should functionally be a no-op.

PiperOrigin-RevId: 502712415
2023-01-17 16:01:40 -08:00
Ayush RanjanandgVisor bot d937df6218 Replace pkg/refs with pkg/refsvfs2.
All VFS1 only bits have been deleted.

Updates #1624

PiperOrigin-RevId: 492273183
2022-12-01 12:46:11 -08:00
Ayush RanjanandgVisor bot 1003ae8e93 Delete VFS1 completely.
- Delete pkg/sentry/fs/*.
- Move pkg/sentry/fs/fsutil out of VFS1 directory and remove VFS1 components.
- Remove remaining unused references to VFS1 from remaining codebase.
- Rename/refactor code to avoid even referencing VFS2, unless necessary.
- Rewrite VFS1-only tests to VFS2.

Updates #1624

PiperOrigin-RevId: 490064269
2022-11-21 13:57:52 -08:00
Ayush RanjanandgVisor bot ee523cfbc7 Delete VFS1 filesystem implementations.
Updates #1624

PiperOrigin-RevId: 488986080
2022-11-16 11:05:10 -08:00
Ayush RanjanandgVisor bot de25857eea Add inotify support to kernfs and anonfs.
This is consistent with Linux. Some applications recursively add
files to inotify FD. They fail if they run into kernfs files
because inotify_add_watch(2) returns EPERM in that case.

PiperOrigin-RevId: 462642204
2022-07-22 09:45:24 -07:00
Ayush RanjanandgVisor bot 2026e1f078 Reformat codebase.
PiperOrigin-RevId: 449358041
2022-05-17 17:48:35 -07:00
Kevin KrakauerandgVisor bot 87df090030 use atomicbitops, not sync/atomic, in refsvfs2
Since refsvfs2 are used in netstack, we should use atomicbitops to avoid
breaking 32-bit builds.

On 64-bit builds there is no performance difference.

PiperOrigin-RevId: 439687980
2022-04-05 15:16:01 -07:00
Fabricio VoznikaandgVisor bot 9725ff1d9b Fix epoll_ctl(2) regular files and dirs
Linux behaves differently for regular files and dirs for poll(2)/select(2)
compared to epoll_ctl(2). The latter returns EPERM for file and dirs.
I've also changed host FDs to behave like the underlying FD in regards
to epoll to keep it compatible with docker.

Fixes #7134

PiperOrigin-RevId: 429412692
2022-02-17 15:12:36 -08:00
Fabricio VoznikaandgVisor bot cecd7315bc Don't eat error from epoll_ctl EPOLL_CTL_ADD
Docker maps stdin to `/dev/null` which doesn't support epoll. Host FD
was ignoring the error and suceeding the epoll_ctl call from the
container, giving false impressing that epoll would be notified.

This required plumbing failure to all waiter.Waitable.EventRegister
callers and implementers.

Closes #6795

PiperOrigin-RevId: 414797621
2021-12-07 12:36:00 -08:00