Require AF_UNIX for bridge and management channels and reject legacy TCP restore paths before opening resources. Add per-adapter host delivery, Npcap receive injection, and checksum and segmentation handling for Ethernet and WLAN IPv4/IPv6 traffic. Validation: full CI, targeted packetbridge/app tests, two live macOS VMs over Ethernet and WLAN, and Windows Firewall allow/block/default-deny checks with SMB 3.02 negotiation. Authenticated SMB file access and physical-display suspend/resume remain unverified.
116 lines
3.0 KiB
Go
116 lines
3.0 KiB
Go
//go:build windows
|
|
|
|
package packetbridge
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/Nwflower/reims-go/internal/networkmsg"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
const sioAFUnixGetPeerPID = 0x58000100 // afunix.h: _WSAIOR(IOC_VENDOR, 256)
|
|
|
|
// unixPeerPID 通过 Windows SIO_AF_UNIX_GETPEERPID 单次系统调用查询 AF_UNIX 对端 PID。
|
|
func unixPeerPID(conn *net.UnixConn) (uint32, error) {
|
|
rc, err := conn.SyscallConn()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
var pid uint64
|
|
var returned uint32
|
|
var ioctlErr error
|
|
if err := rc.Control(func(fd uintptr) {
|
|
ioctlErr = windows.WSAIoctl(
|
|
windows.Handle(fd),
|
|
sioAFUnixGetPeerPID,
|
|
nil,
|
|
0,
|
|
(*byte)(unsafe.Pointer(&pid)),
|
|
uint32(unsafe.Sizeof(pid)),
|
|
&returned,
|
|
nil,
|
|
0,
|
|
)
|
|
}); err != nil {
|
|
return 0, err
|
|
}
|
|
if ioctlErr != nil {
|
|
return 0, ioctlErr
|
|
}
|
|
if pid == 0 || pid > uint64(^uint32(0)) {
|
|
return 0, networkmsg.Error("packet_peer", nil)
|
|
}
|
|
return uint32(pid), nil
|
|
}
|
|
|
|
// AuthorizeQEMUPeer 仅接受 AF_UNIX,并核对对端 PID、QEMU 可执行文件以及目标 VM 的 qemu.pid。
|
|
// 主桥接通道与 Guest Tools 管理网卡中继共用同一套身份鉴权。
|
|
func AuthorizeQEMUPeer(ctx context.Context, conn net.Conn, vmDir, qemuPath string) error {
|
|
uc, ok := conn.(*net.UnixConn)
|
|
if !ok {
|
|
// SLIRP 发起的回环 TCP 连接也属于 QEMU 进程,不能通过 PID 区分客体流量。
|
|
return networkmsg.Error("legacy_transport", nil)
|
|
}
|
|
if vmDir == "" || qemuPath == "" {
|
|
return networkmsg.Error("packet_peer", nil)
|
|
}
|
|
pid, err := unixPeerPID(uc)
|
|
if err != nil || pid == 0 {
|
|
return networkmsg.Error("packet_peer", err)
|
|
}
|
|
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION|windows.SYNCHRONIZE, false, pid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer windows.CloseHandle(h)
|
|
var path [32768]uint16
|
|
size := uint32(len(path))
|
|
if err := windows.QueryFullProcessImageName(h, 0, &path[0], &size); err != nil {
|
|
return err
|
|
}
|
|
actual, err := os.Stat(windows.UTF16ToString(path[:size]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
expected, err := os.Stat(qemuPath)
|
|
if err != nil || !os.SameFile(actual, expected) {
|
|
return networkmsg.Error("packet_peer", err)
|
|
}
|
|
// QEMU 可在 reims-ctl 写回运行状态前连接;只允许该 VM 的 pidfile 短暂迟到。
|
|
tick := time.NewTicker(25 * time.Millisecond)
|
|
defer tick.Stop()
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
state, err := windows.WaitForSingleObject(h, 0)
|
|
if err != nil || state != uint32(windows.WAIT_TIMEOUT) {
|
|
return networkmsg.Error("packet_peer", err)
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(vmDir, "qemu.pid"))
|
|
if err == nil {
|
|
registered, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 32)
|
|
if err == nil && registered == uint64(pid) {
|
|
return nil
|
|
}
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-tick.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func authorizePeer(ctx context.Context, conn net.Conn, cfg Config) error {
|
|
return AuthorizeQEMUPeer(ctx, conn, cfg.VMDir, cfg.QEMUPath)
|
|
}
|