Skip to content

Commit

Permalink
os/user: change Windows user lookup to treat accounts for well-known …
Browse files Browse the repository at this point in the history
…groups as valid.

Some built-in Windows accounts such as `NT AUTHORITY\SYSTEM` are considered to
be users, but are classified by the OS as syscall.SidTypeWellKnownGroup, not as
syscall.SidTypeUser.

This change modifies account querying to consider both types to be valid.

Fixes golang#49509

Signed-off-by: Aaron Klotz <aaron@tailscale.com>
  • Loading branch information
dblohm7 committed Oct 27, 2022
1 parent 3fd24de commit af4ad6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/os/user/lookup_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ func getProfilesDirectory() (string, error) {
}
}

// isValidUserAccountType returns true if acctType is a valid type for user accounts.
func isValidUserAccountType(acctType uint32) bool {
// Some built-in system accounts are classified as well-known groups instead of users.
return acctType == syscall.SidTypeUser || acctType == syscall.SidTypeWellKnownGroup
}

// lookupUsernameAndDomain obtains the username and domain for usid.
func lookupUsernameAndDomain(usid *syscall.SID) (username, domain string, e error) {
username, domain, t, e := usid.LookupAccount("")
if e != nil {
return "", "", e
}
if t != syscall.SidTypeUser {
if !isValidUserAccountType(t) {
return "", "", fmt.Errorf("user: should be user account type, not %d", t)
}
return username, domain, nil
Expand Down Expand Up @@ -324,7 +330,7 @@ func lookupUser(username string) (*User, error) {
if e != nil {
return nil, e
}
if t != syscall.SidTypeUser {
if !isValidUserAccountType(t) {
return nil, fmt.Errorf("user: should be user account type, not %d", t)
}
return newUserFromSid(sid)
Expand Down
17 changes: 17 additions & 0 deletions src/os/user/lookup_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package user

import (
"testing"
)

func TestLookupLocalSystem(t *testing.T) {
// The string representation of the SID for `NT AUTHORITY\SYSTEM`
const localSystemSID = "S-1-5-18"
if _, err := LookupId(localSystemSID); err != nil {
t.Fatalf("LookupId(%q): %v", localSystemSID, err)
}
}

0 comments on commit af4ad6b

Please sign in to comment.