From 179989317c834975a69f43a583478842c87f6ff3 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 15 Jul 2022 17:00:41 +0800 Subject: [PATCH] Fix: macOS udp find process should use unspecified fallback --- component/process/process_darwin.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/component/process/process_darwin.go b/component/process/process_darwin.go index 7870b227..df6f1be8 100644 --- a/component/process/process_darwin.go +++ b/component/process/process_darwin.go @@ -44,6 +44,8 @@ func findProcessName(network string, ip net.IP, port int) (string, error) { // rup8(sizeof(xtcpcb_n)) itemSize += 208 } + + var fallbackUDPProcess string // skip the first xinpgen(24 bytes) block for i := 24; i+itemSize <= len(buf); i += itemSize { // offset of xinpcb_n and xsocket_n @@ -57,11 +59,15 @@ func findProcessName(network string, ip net.IP, port int) (string, error) { // xinpcb_n.inp_vflag flag := buf[inp+44] - var srcIP net.IP + var ( + srcIP net.IP + srcIsIPv4 bool + ) switch { case flag&0x1 > 0 && isIPv4: // ipv4 srcIP = net.IP(buf[inp+76 : inp+80]) + srcIsIPv4 = true case flag&0x2 > 0 && !isIPv4: // ipv6 srcIP = net.IP(buf[inp+64 : inp+80]) @@ -69,13 +75,20 @@ func findProcessName(network string, ip net.IP, port int) (string, error) { continue } - if !ip.Equal(srcIP) { - continue + if ip.Equal(srcIP) { + // xsocket_n.so_last_pid + pid := readNativeUint32(buf[so+68 : so+72]) + return getExecPathFromPID(pid) } - // xsocket_n.so_last_pid - pid := readNativeUint32(buf[so+68 : so+72]) - return getExecPathFromPID(pid) + // udp packet connection may be not equal with srcIP + if network == UDP && srcIP.IsUnspecified() && isIPv4 == srcIsIPv4 { + fallbackUDPProcess, _ = getExecPathFromPID(readNativeUint32(buf[so+68 : so+72])) + } + } + + if network == UDP && fallbackUDPProcess != "" { + return fallbackUDPProcess, nil } return "", ErrNotFound