Files
Reims-Go/internal/host/elevate_windows.go
T
NwflowerandClaude Opus 5.5 44e2174fff fix: harden guest driver host APIs
- run elevated actions as fixed System32 tools instead of a hidden subcommand
- require a SHA-256 for plugin downloads
- keep records of secret files that could not be deleted, and never overwrite
  files the host did not create
- stop two concurrent requests from starting the same VM
- drop invalid endpoints instead of failing the whole VM list, and reject ssh
  users and IPv6 zones that could inject command-line text
- accept driver screenshots only at the requested path
- do not treat an unreadable guest phase as normal

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 09:07:46 -07:00

100 lines
3.1 KiB
Go

//go:build windows
package host
import (
"context"
"fmt"
"os/user"
"path/filepath"
"slices"
"strings"
"syscall"
"golang.org/x/sys/windows"
)
const hyperVAdminsSID = "S-1-5-32-578"
// Tests replace these, so no test raises a UAC prompt.
var (
elevatedRun = runElevatedWait
inHyperVAdminGroup = accountInHyperVAdmins
)
// optionalFeatures names the Windows feature each action turns on.
var optionalFeatures = map[string]string{
ElevateHyperVEnable: "Microsoft-Hyper-V-All",
ElevateSandboxEnable: "Containers-DisposableClientVM",
}
// hyperVAdminsGroupName returns the local name of the Hyper-V Administrators
// group, which Windows localizes; the well-known SID is the stable handle.
func hyperVAdminsGroupName() (string, error) {
sid, err := windows.StringToSid(hyperVAdminsSID)
if err != nil {
return "", err
}
name, _, _, err := sid.LookupAccount("")
return name, err
}
// accountInHyperVAdmins 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 accountInHyperVAdmins(account string) bool {
u, err := user.Lookup(account)
if err != nil {
return false
}
ids, err := u.GroupIds()
return err == nil && slices.Contains(ids, hyperVAdminsSID)
}
// 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 := hyperVAdminsGroupName()
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
}