Skip to content

Commit 3c8b92f

Browse files
committed
Verify socket ownership before treating a hypervisor PID as live
A bare liveness probe treats any process that reused a stored hypervisor PID as the owning VMM. Require the PID to own the instance's hypervisor socket on Linux before reporting it alive.
1 parent 7421fd6 commit 3c8b92f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build linux
2+
3+
package instances
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestHypervisorProcessExistsRejectsLivePIDWithoutSocketOwnership(t *testing.T) {
14+
t.Parallel()
15+
16+
assert.False(t, HypervisorProcessExists(os.Getpid(), filepath.Join(t.TempDir(), "missing.sock")))
17+
}

lib/instances/query.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,21 @@ func refreshHypervisorPID(stored *StoredMetadata, state State) {
587587
}
588588
}
589589

590+
// HypervisorProcessExists reports whether pid owns the instance's hypervisor socket.
591+
func HypervisorProcessExists(pid int, socketPath string) bool {
592+
if !ProcessExists(pid) {
593+
return false
594+
}
595+
if runtime.GOOS != "linux" {
596+
return true
597+
}
598+
if socketPath == "" {
599+
return false
600+
}
601+
resolvedPID, err := hypervisor.ResolveProcessPID(socketPath)
602+
return err == nil && resolvedPID == pid
603+
}
604+
590605
// ProcessExists reports whether pid belongs to a live, non-zombie process.
591606
func ProcessExists(pid int) bool {
592607
if pid <= 0 {

0 commit comments

Comments
 (0)