Skip to content

Commit

Permalink
ssh/tailssh: fix Tailscale SSH to non-root tailscaled
Browse files Browse the repository at this point in the history
Fix regression from 337c779 where
tailscaled started calling Setgroups. Prior to that, SSH to a non-root
tailscaled was working.

Instead, ignore any failure calling Setgroups if the groups are
already correct.

Fixes #6888

Change-Id: I561991ddb37eaf2620759c6bcaabd36e0fb2a22d
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
  • Loading branch information
bradfitz committed Jan 6, 2023
1 parent 8047dfa commit be67b8e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion ssh/tailssh/incubator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/user"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"syscall"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/u-root/u-root/pkg/termios"
"go4.org/mem"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/exp/slices"
"golang.org/x/sys/unix"
"tailscale.com/cmd/tailscaled/childproc"
"tailscale.com/envknob"
Expand Down Expand Up @@ -727,5 +729,25 @@ func setGroups(groupIDs []int) error {
// this to work for more things than it previously did.
groupIDs = groupIDs[:16]
}
return syscall.Setgroups(groupIDs)

err := syscall.Setgroups(groupIDs)
if err != nil && os.Geteuid() != 0 && groupsMatchCurrent(groupIDs) {
// If we're not root, ignore a Setgroups failure if all groups are the same.
return nil
}
return err
}

func groupsMatchCurrent(groupIDs []int) bool {
existing, err := syscall.Getgroups()
if err != nil {
return false
}
if len(existing) != len(groupIDs) {
return false
}
groupIDs = slices.Clone(groupIDs)
sort.Ints(groupIDs)
sort.Ints(existing)
return slices.Equal(groupIDs, existing)
}

0 comments on commit be67b8e

Please sign in to comment.