Skip to content

Commit

Permalink
os/user: make Windows user lookup treat well-known groups as valid ac…
Browse files Browse the repository at this point in the history
…counts

This change modifies account querying to consider both syscall.SidTypeUser
and syscall.SidTypeWellKnownGroup types to be valid for user accounts.

Some built-in Windows accounts such as 'NT AUTHORITY\SYSTEM' are treated by
the OS as users, but are internally classified by the OS as
syscall.SidTypeWellKnownGroup instead of syscall.SidTypeUser.

Fixes golang#49509
  • Loading branch information
dblohm7 committed Nov 17, 2022
1 parent b74aaa1 commit 7043824
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 7043824

Please sign in to comment.