syscall: if Setctty, require that Ctty be a child descriptor

Ctty was always handled as a child descriptor, but in some cases
passing a parent descriptor would also work. This depended on
unpredictable details of the implementation. Reject those cases to
avoid confusion.

Also reject setting both Setctty and Foreground, as they use Ctty
in incompatible ways. It's unlikely that any programs set both fields,
as they don't make sense together.

Fixes #29458

Change-Id: Ieba2d625711fd4b82c8e65e1feed02fd1fb25e6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/231638
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Ian Lance Taylor 2020-05-01 12:26:30 -07:00
parent 5c8715f70a
commit be08e10b3b
4 changed files with 61 additions and 1 deletions

View file

@ -213,3 +213,31 @@ func TestForeground(t *testing.T) {
signal.Reset()
}
// Test a couple of cases that SysProcAttr can't handle. Issue 29458.
func TestInvalidExec(t *testing.T) {
t.Parallel()
t.Run("SetCtty-Foreground", func(t *testing.T) {
t.Parallel()
cmd := create(t)
cmd.proc.SysProcAttr = &syscall.SysProcAttr{
Setctty: true,
Foreground: true,
Ctty: 0,
}
if err := cmd.proc.Start(); err == nil {
t.Error("expected error setting both SetCtty and Foreground")
}
})
t.Run("invalid-Ctty", func(t *testing.T) {
t.Parallel()
cmd := create(t)
cmd.proc.SysProcAttr = &syscall.SysProcAttr{
Setctty: true,
Ctty: 3,
}
if err := cmd.proc.Start(); err == nil {
t.Error("expected error with invalid Ctty value")
}
})
}