diff --git a/pkg/sentry/kernel/thread_group.go b/pkg/sentry/kernel/thread_group.go index 4fce5e916..4dbf10f0c 100644 --- a/pkg/sentry/kernel/thread_group.go +++ b/pkg/sentry/kernel/thread_group.go @@ -376,7 +376,12 @@ func (tg *ThreadGroup) SetControllingTTY(tty *TTY, steal bool, isReadable bool) // "The calling process must be a session leader and not have a // controlling terminal already." - tty_ioctl(4) - if tg.processGroup.session.leader != tg || tg.tty != nil { + if tg.processGroup.session.leader != tg { + return linuxerr.EINVAL + } + if tg.tty == tty { + return nil + } else if tg.tty != nil { return linuxerr.EINVAL } diff --git a/test/syscalls/linux/pty.cc b/test/syscalls/linux/pty.cc index 132d71ac6..b7e8494c4 100644 --- a/test/syscalls/linux/pty.cc +++ b/test/syscalls/linux/pty.cc @@ -1476,6 +1476,8 @@ TEST_F(JobControlTest, SetTTY) { auto res = RunInChild([=]() { TEST_PCHECK(setsid() >= 0); TEST_PCHECK(ioctl(!replica_.get(), TIOCSCTTY, 0)); + // The second attempt setting the same terminal has to be no-op. + TEST_PCHECK(ioctl(!replica_.get(), TIOCSCTTY, 0)); }); ASSERT_NO_ERRNO(res); }