fix: track the whole VM process tree and open hosted endpoints

- 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>
This commit is contained in:
千川白浪andClaude Opus 5.5 committed 2026-12-03 09:15:38 -07:00
1 parent 44e2174fff
commit a32cd8afad
7 files changed
+77 -19

No files matched your search

+3 -2
View File
@@ -15,6 +15,7 @@ import (
// it never chooses what the host runs for them.
const (
endpointActionDefault = ""
endpointActionOpen = "open"
endpointActionTerminal = "terminal"
endpointActionVSCode = "vscode"
endpointActionConfig = "config"
@@ -108,12 +109,12 @@ func buildEndpointLaunch(vmName string, ep driver.VMEndpoint, action string, loo
return launchSpec{}, fmt.Errorf("VS Code or Cursor CLI not found on PATH")
}
case driver.EndpointRDP:
if action == endpointActionDefault {
if action == endpointActionDefault || action == endpointActionOpen {
return launchSpec{exe: "mstsc.exe", args: []string{"/v:" + rdpTarget(ep)}}, nil
}
case driver.EndpointVMConnect:
switch action {
case endpointActionDefault:
case endpointActionDefault, endpointActionOpen:
return launchSpec{exe: "vmconnect.exe", args: []string{"localhost", ep.VM}}, nil
case endpointActionEdit:
return launchSpec{exe: "vmconnect.exe", args: []string{"localhost", ep.VM, "/edit"}}, nil
+2 -2
View File
@@ -37,9 +37,9 @@ func TestBuildEndpointLaunchTemplates(t *testing.T) {
`C:\tools\code.cmd`, []string{"--remote", "wsl+Debian"}},
{"rdp default port", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "10.0.0.5", Ready: true}, "", wt,
"mstsc.exe", []string{"/v:10.0.0.5:3389"}},
{"rdp ipv6", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "fe80::1", Port: 3390, Ready: true}, "", wt,
{"rdp ipv6", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "fe80::1", Port: 3390, Ready: true}, endpointActionOpen, wt,
"mstsc.exe", []string{"/v:[fe80::1]:3390"}},
{"vmconnect", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "Win 11", Ready: true}, "", wt,
{"vmconnect", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "Win 11", Ready: true}, endpointActionOpen, wt,
"vmconnect.exe", []string{"localhost", "Win 11"}},
{"vmconnect edit", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "Win 11", Ready: true}, endpointActionEdit, wt,
"vmconnect.exe", []string{"localhost", "Win 11", "/edit"}},
+31 -12
View File
@@ -5,21 +5,23 @@ package host
import (
"context"
"fmt"
"os/user"
"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 = accountInHyperVAdmins
inHyperVAdminGroup = func(account string) bool { return accountInLocalGroup(hyperVAdminsSID, account) }
)
// optionalFeatures names the Windows feature each action turns on.
@@ -28,10 +30,10 @@ var optionalFeatures = map[string]string{
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)
// 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
}
@@ -39,15 +41,32 @@ func hyperVAdminsGroupName() (string, error) {
return name, err
}
// accountInHyperVAdmins reads the group's current members, not the signed-in
// 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 accountInHyperVAdmins(account string) bool {
u, err := user.Lookup(account)
func accountInLocalGroup(groupSID, account string) bool {
sid, _, _, err := windows.LookupSID("", account)
if err != nil {
return false
}
ids, err := u.GroupIds()
return err == nil && slices.Contains(ids, hyperVAdminsSID)
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
@@ -83,7 +102,7 @@ func RunElevatedAction(ctx context.Context, action, targetUser string) (Elevatio
if inHyperVAdminGroup(targetUser) {
return done, nil
}
group, err := hyperVAdminsGroupName()
group, err := localGroupName(hyperVAdminsSID)
if err != nil {
return ElevationResult{Action: action}, fmt.Errorf("find the Hyper-V Administrators group: %w", err)
}
+14 -1
View File
@@ -54,6 +54,19 @@ func TestRunElevatedActionEnablesFeaturesThroughSystem32(t *testing.T) {
}
}
func TestAccountInLocalGroupReadsTheMemberList(t *testing.T) {
const administrators, guests = "S-1-5-32-544", "S-1-5-32-546"
if _, _, _, err := windows.LookupSID("", "Administrator"); err != nil {
t.Skipf("the built-in administrator is renamed here: %v", err)
}
if !accountInLocalGroup(administrators, "Administrator") {
t.Error("the built-in administrator is not seen in Administrators")
}
if accountInLocalGroup(guests, "Administrator") {
t.Error("the built-in administrator is seen in Guests")
}
}
func TestRunElevatedActionReportsFailures(t *testing.T) {
stubElevation(t, 50, nil)
if res, err := RunElevatedAction(t.Context(), ElevateHyperVEnable, ""); err == nil || res.OK || res.ExitCode != 50 {
@@ -66,7 +79,7 @@ func TestRunElevatedActionReportsFailures(t *testing.T) {
}
func TestRunElevatedActionAddsTheAccountToTheHyperVGroup(t *testing.T) {
if _, err := hyperVAdminsGroupName(); err != nil {
if _, err := localGroupName(hyperVAdminsSID); err != nil {
t.Skipf("this Windows has no Hyper-V Administrators group: %v", err)
}
const account = `DESKTOP-1\Alice Smith`
+7
View File
@@ -491,6 +491,13 @@ func (b *Broker) SpawnManagedVMProcessWithRef(ctx context.Context, pluginID stri
if jobErr != nil {
b.Audit(pluginID, "JOB_ERR", fmt.Sprintf("vm=%s pid=%d: %v (only the launcher process is tracked)", vmName, pid, jobErr))
}
if err := resumeVMProcess(pid); err != nil {
_ = cmd.Process.Kill()
_ = cmd.Wait()
job.Close()
b.Audit(pluginID, "SPAWN_ERR", fmt.Sprintf("vm=%s exe=%s err=%v", vmName, filepath.Base(exePath), err))
return 0, err
}
mp := &ManagedProcess{
PID: pid,
PluginID: pluginID,
+2
View File
@@ -18,6 +18,8 @@ func configureDetachedVMProcess(cmd *exec.Cmd) {
func configureNestedVMProcess(cmd *exec.Cmd) { configureDetachedVMProcess(cmd) }
func resumeVMProcess(int) error { return nil }
func isBreakawayDenied(error) bool { return false }
type sidecarJob struct {
+18 -2
View File
@@ -26,10 +26,12 @@ func configureSidecarCmd(cmd *exec.Cmd) {
}
}
// configureDetachedVMProcess starts the VM suspended, so it joins its job before
// it can start children; resumeVMProcess lets it run.
func configureDetachedVMProcess(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: createNoWindow | createNewProcessGroup | createBreakawayFromJob,
CreationFlags: createNoWindow | createNewProcessGroup | createBreakawayFromJob | windows.CREATE_SUSPENDED,
}
}
@@ -38,10 +40,24 @@ func configureDetachedVMProcess(cmd *exec.Cmd) {
func configureNestedVMProcess(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: createNoWindow | createNewProcessGroup,
CreationFlags: createNoWindow | createNewProcessGroup | windows.CREATE_SUSPENDED,
}
}
var procNtResumeProcess = windows.NewLazySystemDLL("ntdll.dll").NewProc("NtResumeProcess")
func resumeVMProcess(pid int) error {
proc, err := windows.OpenProcess(windows.PROCESS_SUSPEND_RESUME, false, uint32(pid))
if err != nil {
return fmt.Errorf("OpenProcess(%d): %w", pid, err)
}
defer windows.CloseHandle(proc)
if status, _, _ := procNtResumeProcess.Call(uintptr(proc)); status != 0 {
return fmt.Errorf("resume process %d: %w", pid, windows.NTStatus(status))
}
return nil
}
// isBreakawayDenied reports a failed CREATE_BREAKAWAY_FROM_JOB start.
func isBreakawayDenied(err error) bool {
return errors.Is(err, windows.ERROR_ACCESS_DENIED)