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>
This commit is contained in:
NwflowerandClaude Opus 5.5 committed 2026-12-03 09:07:46 -07:00
1 parent 58cf86d5eb
commit 44e2174fff
32 files changed
+431 -689

No files matched your search

+20 -4
View File
@@ -43,6 +43,8 @@ type Broker struct {
// Managed VM processes spawned on behalf of drivers (decoupled from Sidecar Job Object).
procs map[int]*ManagedProcess
procsByRef map[string]*ManagedProcess
// VMs being spawned right now, so two requests cannot both start one.
spawning map[string]bool
onNotify func(pluginID, title, body string)
onVMExit func(driverID, vmName string)
onVMState func(pluginID string, rep ReportedVMState) error
@@ -423,15 +425,29 @@ func (b *Broker) SpawnManagedVMProcessWithRef(ctx context.Context, pluginID stri
refKey := vmRefMapKey(driverID, vmName)
if driverID != "" && vmName != "" {
b.mu.Lock()
cur := b.procsByRef[refKey]
b.mu.Unlock()
if cur != nil {
busy := b.spawning[refKey]
if cur := b.procsByRef[refKey]; cur != nil && !busy {
select {
case <-cur.done:
default:
return 0, fmt.Errorf("VM %q of driver %q is already running", vmName, driverID)
busy = true
}
}
if !busy {
if b.spawning == nil {
b.spawning = map[string]bool{}
}
b.spawning[refKey] = true
}
b.mu.Unlock()
if busy {
return 0, fmt.Errorf("VM %q of driver %q is already running", vmName, driverID)
}
defer func() {
b.mu.Lock()
delete(b.spawning, refKey)
b.mu.Unlock()
}()
}
build := func(breakaway bool) *exec.Cmd {
cmd := exec.Command(exePath, args...)
+4 -1
View File
@@ -15,6 +15,9 @@ import (
const maxRPCFrameBytes = 8 << 20 // 8 MiB per JSON-RPC frame (large blobs go via file paths, §8)
// RPCMethodNotFound is the code a peer answers for a method it does not serve.
const RPCMethodNotFound = -32601
// RPCError is a JSON-RPC 2.0 error object.
type RPCError struct {
Code int `json:"code"`
@@ -246,7 +249,7 @@ func (c *RPCConn) dispatchInbound(env rpcEnvelope) {
if h == nil {
if len(env.ID) > 0 {
c.sendResponse(env.ID, nil, &RPCError{Code: -32601, Message: "method not found: " + env.Method})
c.sendResponse(env.ID, nil, &RPCError{Code: RPCMethodNotFound, Message: "method not found: " + env.Method})
}
return
}
+27
View File
@@ -142,6 +142,33 @@ func TestSpawnWithRefTracksVMAndStopsByRef(t *testing.T) {
}
}
func TestConcurrentSpawnsStartOneVM(t *testing.T) {
f := newBrokerFixture(t, "test.vmjob.plugin", "test.driver.one")
ref := plugin.VMRef{DriverID: "test.driver.one", Name: "race"}
errs := make(chan error, 4)
for range cap(errs) {
go func() {
_, err := f.broker.SpawnManagedVMProcessWithRef(context.Background(), "test.vmjob.plugin", ref, f.exe,
[]string{"-test.run=^TestHelperVM$"}, "", f.env("sleep"))
errs <- err
}()
}
started := 0
for range cap(errs) {
if err := <-errs; err == nil {
started++
} else if !strings.Contains(err.Error(), "already running") {
t.Skipf("cannot spawn a detached process in this environment: %v", err)
}
}
if started != 1 {
t.Fatalf("%d processes started for one VM", started)
}
if err := f.broker.StopManagedVMProcessByRef("test.vmjob.plugin", ref); err != nil {
t.Fatal(err)
}
}
func TestStopManagedVMProcessAllowsImmediateRespawn(t *testing.T) {
f := newBrokerFixture(t, "test.vmjob.plugin", "test.driver.one")
ref := plugin.VMRef{DriverID: "test.driver.one", Name: "fast-restart"}