Skip to content

Commit af4ad6b

Browse files
committed
os/user: change Windows user lookup to treat accounts for well-known 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 <[email protected]>
1 parent 3fd24de commit af4ad6b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/os/user/lookup_windows.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,19 @@ func getProfilesDirectory() (string, error) {
8484
}
8585
}
8686

87+
// isValidUserAccountType returns true if acctType is a valid type for user accounts.
88+
func isValidUserAccountType(acctType uint32) bool {
89+
// Some built-in system accounts are classified as well-known groups instead of users.
90+
return acctType == syscall.SidTypeUser || acctType == syscall.SidTypeWellKnownGroup
91+
}
92+
8793
// lookupUsernameAndDomain obtains the username and domain for usid.
8894
func lookupUsernameAndDomain(usid *syscall.SID) (username, domain string, e error) {
8995
username, domain, t, e := usid.LookupAccount("")
9096
if e != nil {
9197
return "", "", e
9298
}
93-
if t != syscall.SidTypeUser {
99+
if !isValidUserAccountType(t) {
94100
return "", "", fmt.Errorf("user: should be user account type, not %d", t)
95101
}
96102
return username, domain, nil
@@ -324,7 +330,7 @@ func lookupUser(username string) (*User, error) {
324330
if e != nil {
325331
return nil, e
326332
}
327-
if t != syscall.SidTypeUser {
333+
if !isValidUserAccountType(t) {
328334
return nil, fmt.Errorf("user: should be user account type, not %d", t)
329335
}
330336
return newUserFromSid(sid)

src/os/user/lookup_windows_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package user
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func TestLookupLocalSystem(t *testing.T) {
12+
// The string representation of the SID for `NT AUTHORITY\SYSTEM`
13+
const localSystemSID = "S-1-5-18"
14+
if _, err := LookupId(localSystemSID); err != nil {
15+
t.Fatalf("LookupId(%q): %v", localSystemSID, err)
16+
}
17+
}

0 commit comments

Comments
 (0)