- start VM processes suspended so children cannot escape the VM's job - read Hyper-V Administrators from the member list, not the sign-in token - accept the "open" action the app sends for Remote Desktop and VMConnect Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
119 lines
3.9 KiB
Go
119 lines
3.9 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
const hyperVAdminsSID = "S-1-5-32-578"
|
|
|
|
var procNetLocalGroupGetMembers = windows.NewLazySystemDLL("netapi32.dll").NewProc("NetLocalGroupGetMembers")
|
|
|
|
// Tests replace these, so no test raises a UAC prompt.
|
|
var (
|
|
elevatedRun = runElevatedWait
|
|
inHyperVAdminGroup = func(account string) bool { return accountInLocalGroup(hyperVAdminsSID, account) }
|
|
)
|
|
|
|
// optionalFeatures names the Windows feature each action turns on.
|
|
var optionalFeatures = map[string]string{
|
|
ElevateHyperVEnable: "Microsoft-Hyper-V-All",
|
|
ElevateSandboxEnable: "Containers-DisposableClientVM",
|
|
}
|
|
|
|
// localGroupName returns the name of a built-in group, which Windows localizes;
|
|
// the well-known SID is the stable handle.
|
|
func localGroupName(groupSID string) (string, error) {
|
|
sid, err := windows.StringToSid(groupSID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
name, _, _, err := sid.LookupAccount("")
|
|
return name, err
|
|
}
|
|
|
|
// accountInLocalGroup reads the group's current members, not the signed-in
|
|
// token, so it also sees a membership that takes effect at the next sign-in.
|
|
func accountInLocalGroup(groupSID, account string) bool {
|
|
sid, _, _, err := windows.LookupSID("", account)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
group, err := localGroupName(groupSID)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
name, err := windows.UTF16PtrFromString(group)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
const maxPreferredLength = 0xFFFFFFFF
|
|
var buf *byte
|
|
var read, total uint32
|
|
// Level 0 returns an array of member SID pointers.
|
|
status, _, _ := procNetLocalGroupGetMembers.Call(0, uintptr(unsafe.Pointer(name)), 0, uintptr(unsafe.Pointer(&buf)),
|
|
maxPreferredLength, uintptr(unsafe.Pointer(&read)), uintptr(unsafe.Pointer(&total)), 0)
|
|
if status != 0 || buf == nil {
|
|
return false
|
|
}
|
|
defer windows.NetApiBufferFree(buf)
|
|
return slices.ContainsFunc(unsafe.Slice((**windows.SID)(unsafe.Pointer(buf)), read), sid.Equals)
|
|
}
|
|
|
|
// RunElevatedAction runs the fixed Windows tool behind a whitelisted action
|
|
// through a UAC prompt and waits for it. It never restarts Windows.
|
|
func RunElevatedAction(ctx context.Context, action, targetUser string) (ElevationResult, error) {
|
|
spec, ok := LookupElevationAction(action)
|
|
if !ok {
|
|
return ElevationResult{Action: action}, fmt.Errorf("unsupported elevation action %q", action)
|
|
}
|
|
sysDir, err := windows.GetSystemDirectory()
|
|
if err != nil {
|
|
return ElevationResult{Action: action}, fmt.Errorf("resolve Windows System32 directory: %w", err)
|
|
}
|
|
done := ElevationResult{OK: true, Action: action, RebootRequired: spec.NeedsReboot, LogoffRequired: spec.NeedsLogoff}
|
|
|
|
if feature, ok := optionalFeatures[action]; ok {
|
|
code, err := elevatedRun(ctx, filepath.Join(sysDir, "dism.exe"),
|
|
"/Online /Enable-Feature /FeatureName:"+feature+" /All /Quiet /NoRestart")
|
|
if err != nil {
|
|
return ElevationResult{Action: action}, err
|
|
}
|
|
if code != 0 && code != errorSuccessRebootRequired {
|
|
return ElevationResult{Action: action, ExitCode: int(code)}, fmt.Errorf("dism exited with code %d", code)
|
|
}
|
|
done.ExitCode = int(code)
|
|
return done, nil
|
|
}
|
|
|
|
targetUser = strings.TrimSpace(targetUser)
|
|
if err := ValidateElevationTargetUser(targetUser); err != nil {
|
|
return ElevationResult{Action: action}, err
|
|
}
|
|
if inHyperVAdminGroup(targetUser) {
|
|
return done, nil
|
|
}
|
|
group, err := localGroupName(hyperVAdminsSID)
|
|
if err != nil {
|
|
return ElevationResult{Action: action}, fmt.Errorf("find the Hyper-V Administrators group: %w", err)
|
|
}
|
|
code, err := elevatedRun(ctx, filepath.Join(sysDir, "net.exe"),
|
|
"localgroup "+syscall.EscapeArg(group)+" "+syscall.EscapeArg(targetUser)+" /add")
|
|
if err != nil {
|
|
return ElevationResult{Action: action}, err
|
|
}
|
|
if code != 0 && !inHyperVAdminGroup(targetUser) {
|
|
return ElevationResult{Action: action, ExitCode: int(code)}, fmt.Errorf("net localgroup exited with code %d", code)
|
|
}
|
|
return done, nil
|
|
}
|