Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions lib/network/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
mathrand "math/rand"
"net"
"os"
"strings"
"time"

Expand Down Expand Up @@ -58,11 +59,17 @@ func (m *manager) CreateAllocation(ctx context.Context, req AllocateRequest) (*N
tap := GenerateTAPName(req.InstanceID)

// 5. Create TAP device with bidirectional rate limiting
if err := m.createTAPDevice(tap, network.Bridge, network.Isolated, req.DownloadBps, req.UploadBps, req.UploadCeilBps); err != nil {
classID, err := m.createTAPDevice(ctx, tap, network.Bridge, network.Isolated, req.DownloadBps, req.UploadBps, req.UploadCeilBps)
if err != nil {
return nil, fmt.Errorf("create TAP device: %w", err)
}
m.recordTAPOperation(ctx, "create")

// Persist assigned tc class ID so removal uses the correct ID after collisions.
if classID != "" {
m.saveClassID(req.InstanceID, classID)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale classID file not cleared when rate limiting removed

Medium Severity

Both CreateAllocation and RecreateAllocation only call saveClassID when classID != "". If an instance previously had upload rate limiting (classID file saved to disk) and is later recreated without it (e.g., RecreateAllocation with uploadBps = 0), the old classid file persists with a stale value. deriveAllocation then loads this stale classID into alloc.ClassID, causing ReleaseAllocation to call deleteTAPDevice with the wrong classID — potentially deleting another VM's HTB class.

Additional Locations (1)
Fix in Cursor Fix in Web


log.InfoContext(ctx, "allocated network",
"instance_id", req.InstanceID,
"instance_name", req.InstanceName,
Expand Down Expand Up @@ -114,11 +121,17 @@ func (m *manager) RecreateAllocation(ctx context.Context, instanceID string, dow

// 3. Recreate TAP device with same name and rate limits from instance metadata
uploadCeilBps := uploadBps * int64(m.GetUploadBurstMultiplier())
if err := m.createTAPDevice(alloc.TAPDevice, network.Bridge, network.Isolated, downloadBps, uploadBps, uploadCeilBps); err != nil {
classID, err := m.createTAPDevice(ctx, alloc.TAPDevice, network.Bridge, network.Isolated, downloadBps, uploadBps, uploadCeilBps)
if err != nil {
return fmt.Errorf("create TAP device: %w", err)
}
m.recordTAPOperation(ctx, "create")

// Persist assigned tc class ID so removal uses the correct ID after collisions.
if classID != "" {
m.saveClassID(instanceID, classID)
}

log.InfoContext(ctx, "recreated network for restore",
"instance_id", instanceID,
"network", "default",
Expand All @@ -144,8 +157,8 @@ func (m *manager) ReleaseAllocation(ctx context.Context, alloc *Allocation) erro
return nil
}

// 1. Delete TAP device (best effort)
if err := m.deleteTAPDevice(alloc.TAPDevice); err != nil {
// 1. Delete TAP device (best effort), using stored class ID for correct HTB cleanup
if err := m.deleteTAPDevice(alloc.TAPDevice, alloc.ClassID); err != nil {
log.WarnContext(ctx, "failed to delete TAP device", "tap", alloc.TAPDevice, "error", err)
} else {
m.recordTAPOperation(ctx, "delete")
Expand Down Expand Up @@ -295,6 +308,23 @@ func generateMAC() (string, error) {
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]), nil
}

// saveClassID persists the tc class ID for an instance so it survives restarts.
func (m *manager) saveClassID(instanceID, classID string) {
path := m.paths.InstanceDir(instanceID)
_ = os.WriteFile(path+"/classid", []byte(classID), 0644)
}

// loadClassID loads the persisted tc class ID for an instance.
// Returns empty string if not found (backwards compat for old allocations).
func (m *manager) loadClassID(instanceID string) string {
path := m.paths.InstanceDir(instanceID)
data, err := os.ReadFile(path + "/classid")
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}

// TAPPrefix is the prefix used for hypeman TAP devices
const TAPPrefix = "hype-"

Expand Down
6 changes: 3 additions & 3 deletions lib/network/bridge_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (m *manager) setupBridgeHTB(ctx context.Context, bridgeName string, capacit

// createTAPDevice is a no-op on macOS as we use NAT networking.
// Virtualization.framework creates virtual network interfaces internally.
func (m *manager) createTAPDevice(tapName, bridgeName string, isolated bool, downloadBps, uploadBps, uploadCeilBps int64) error {
func (m *manager) createTAPDevice(ctx context.Context, tapName, bridgeName string, isolated bool, downloadBps, uploadBps, uploadCeilBps int64) (string, error) {
// On macOS with vz, network devices are created by the VMM itself
return nil
return "", nil
}

// deleteTAPDevice is a no-op on macOS as we use NAT networking.
func (m *manager) deleteTAPDevice(tapName string) error {
func (m *manager) deleteTAPDevice(tapName, classID string) error {
return nil
}

Expand Down
Loading
Loading