mirror of
https://github.com/golang/sys.git
synced 2026-02-08 11:46:04 +03:00
This CL fixes unsafe casts to slices that are missing length or capacity.
Running tests with -d=checkptr enabled may panic on casting unsafe.Pointer to a static array of large predefined length, that is most likely much bigger than the size of the actual array in memory. Checkptr check is not satisfied if slicing operator misses length and capacity arguments `(*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:]`, or when there is no slicing at all `(*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))`.
To find all potential cases I used `grep -nr ")(unsafe.Pointer(" ./windows`, then filtered out safe casts when object size is always static and known at compile time.
To reproduce the issue run tests with checkptr enabled `go test -a -gcflags=all=-d=checkptr ./windows/...`.
Updates golang/go#34972
Fixes golang/go#38355
Change-Id: I9dd2084b4f9fb7618cdb140fb2f38b56b6d6cc04
GitHub-Last-Rev: 73288ad18a
GitHub-Pull-Request: golang/sys#65
Reviewed-on: https://go-review.googlesource.com/c/sys/+/225418
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
// Copyright 2012 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.
|
|
|
|
// +build windows
|
|
|
|
package svc
|
|
|
|
import (
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func allocSid(subAuth0 uint32) (*windows.SID, error) {
|
|
var sid *windows.SID
|
|
err := windows.AllocateAndInitializeSid(&windows.SECURITY_NT_AUTHORITY,
|
|
1, subAuth0, 0, 0, 0, 0, 0, 0, 0, &sid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return sid, nil
|
|
}
|
|
|
|
// IsAnInteractiveSession determines if calling process is running interactively.
|
|
// It queries the process token for membership in the Interactive group.
|
|
// http://stackoverflow.com/questions/2668851/how-do-i-detect-that-my-application-is-running-as-service-or-in-an-interactive-s
|
|
func IsAnInteractiveSession() (bool, error) {
|
|
interSid, err := allocSid(windows.SECURITY_INTERACTIVE_RID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer windows.FreeSid(interSid)
|
|
|
|
serviceSid, err := allocSid(windows.SECURITY_SERVICE_RID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer windows.FreeSid(serviceSid)
|
|
|
|
t, err := windows.OpenCurrentProcessToken()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer t.Close()
|
|
|
|
gs, err := t.GetTokenGroups()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
for _, g := range gs.AllGroups() {
|
|
if windows.EqualSid(g.Sid, interSid) {
|
|
return true, nil
|
|
}
|
|
if windows.EqualSid(g.Sid, serviceSid) {
|
|
return false, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|