From ae8d966f5af0bba9978a1aedac64038ef65a4cc9 Mon Sep 17 00:00:00 2001 From: Kevin Krakauer Date: Thu, 11 Feb 2021 11:06:56 -0800 Subject: Assign controlling terminal when tty is opened and support NOCTTY PiperOrigin-RevId: 357015186 --- pkg/sentry/fs/tty/master.go | 4 ++-- pkg/sentry/fs/tty/replica.go | 4 ++-- pkg/sentry/fs/tty/terminal.go | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) (limited to 'pkg/sentry/fs/tty') diff --git a/pkg/sentry/fs/tty/master.go b/pkg/sentry/fs/tty/master.go index b91184b1b..1cf869b62 100644 --- a/pkg/sentry/fs/tty/master.go +++ b/pkg/sentry/fs/tty/master.go @@ -153,7 +153,7 @@ func (mf *masterFileOperations) Write(ctx context.Context, _ *fs.File, src userm } // Ioctl implements fs.FileOperations.Ioctl. -func (mf *masterFileOperations) Ioctl(ctx context.Context, _ *fs.File, io usermem.IO, args arch.SyscallArguments) (uintptr, error) { +func (mf *masterFileOperations) Ioctl(ctx context.Context, file *fs.File, io usermem.IO, args arch.SyscallArguments) (uintptr, error) { t := kernel.TaskFromContext(ctx) if t == nil { // ioctl(2) may only be called from a task goroutine. @@ -189,7 +189,7 @@ func (mf *masterFileOperations) Ioctl(ctx context.Context, _ *fs.File, io userme case linux.TIOCSCTTY: // Make the given terminal the controlling terminal of the // calling process. - return 0, mf.t.setControllingTTY(ctx, args, true /* isMaster */) + return 0, mf.t.setControllingTTY(ctx, args, true /* isMaster */, file.Flags().Read) case linux.TIOCNOTTY: // Release this process's controlling terminal. return 0, mf.t.releaseControllingTTY(ctx, args, true /* isMaster */) diff --git a/pkg/sentry/fs/tty/replica.go b/pkg/sentry/fs/tty/replica.go index 385d230fb..0e3eea3bd 100644 --- a/pkg/sentry/fs/tty/replica.go +++ b/pkg/sentry/fs/tty/replica.go @@ -138,7 +138,7 @@ func (sf *replicaFileOperations) Write(ctx context.Context, _ *fs.File, src user } // Ioctl implements fs.FileOperations.Ioctl. -func (sf *replicaFileOperations) Ioctl(ctx context.Context, _ *fs.File, io usermem.IO, args arch.SyscallArguments) (uintptr, error) { +func (sf *replicaFileOperations) Ioctl(ctx context.Context, file *fs.File, io usermem.IO, args arch.SyscallArguments) (uintptr, error) { t := kernel.TaskFromContext(ctx) if t == nil { // ioctl(2) may only be called from a task goroutine. @@ -167,7 +167,7 @@ func (sf *replicaFileOperations) Ioctl(ctx context.Context, _ *fs.File, io userm case linux.TIOCSCTTY: // Make the given terminal the controlling terminal of the // calling process. - return 0, sf.si.t.setControllingTTY(ctx, args, false /* isMaster */) + return 0, sf.si.t.setControllingTTY(ctx, args, false /* isMaster */, file.Flags().Read) case linux.TIOCNOTTY: // Release this process's controlling terminal. return 0, sf.si.t.releaseControllingTTY(ctx, args, false /* isMaster */) diff --git a/pkg/sentry/fs/tty/terminal.go b/pkg/sentry/fs/tty/terminal.go index 4f431d74d..d551267af 100644 --- a/pkg/sentry/fs/tty/terminal.go +++ b/pkg/sentry/fs/tty/terminal.go @@ -64,13 +64,14 @@ func newTerminal(ctx context.Context, d *dirInodeOperations, n uint32) *Terminal // setControllingTTY makes tm the controlling terminal of the calling thread // group. -func (tm *Terminal) setControllingTTY(ctx context.Context, args arch.SyscallArguments, isMaster bool) error { +func (tm *Terminal) setControllingTTY(ctx context.Context, args arch.SyscallArguments, isMaster bool, readable bool) error { task := kernel.TaskFromContext(ctx) if task == nil { panic("setControllingTTY must be called from a task context") } - return task.ThreadGroup().SetControllingTTY(tm.tty(isMaster), args[2].Int()) + steal := args[2].Int() == 1 + return task.ThreadGroup().SetControllingTTY(tm.tty(isMaster), steal, readable) } // releaseControllingTTY removes tm as the controlling terminal of the calling -- cgit v1.2.3