Skip to content

Commit

Permalink
Retry finding a socket's PID on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Jun 5, 2020
1 parent 6ffca6e commit 9f42e6f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 36 deletions.
47 changes: 37 additions & 10 deletions network/proc/findpid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,48 @@ import (
)

var (
// pidsByUserLock is also used for locking the socketInfo.PID on all socket.*Info structs.
pidsByUserLock sync.Mutex
pidsByUser = make(map[int][]int)
)

// FindPID returns the pid of the given uid and socket inode.
func FindPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
// FindConnectionPID returns the pid of the given socket info.
func FindConnectionPID(socketInfo *socket.ConnectionInfo) (pid int) {
pidsByUserLock.Lock()
defer pidsByUserLock.Unlock()

if socketInfo.PID != socket.UnidentifiedProcessID {
return socket.UnidentifiedProcessID
}

pid = findPID(socketInfo.UID, socketInfo.Inode)
socketInfo.PID = pid
return pid
}

// FindBindPID returns the pid of the given socket info.
func FindBindPID(socketInfo *socket.BindInfo) (pid int) {
pidsByUserLock.Lock()
defer pidsByUserLock.Unlock()

if socketInfo.PID != socket.UnidentifiedProcessID {
return socket.UnidentifiedProcessID
}

pid = findPID(socketInfo.UID, socketInfo.Inode)
socketInfo.PID = pid
return pid
}

// findPID returns the pid of the given uid and socket inode.
func findPID(uid, inode int) (pid int) { //nolint:gocognit // TODO

pidsUpdated := false

// get pids of user, update if missing
pids, ok := pidsByUser[uid]
if !ok {
// log.Trace("process: no processes of user, updating table")
// log.Trace("proc: no processes of user, updating table")
updatePids()
pidsUpdated = true
pids, ok = pidsByUser[uid]
Expand All @@ -46,7 +73,7 @@ func FindPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
}
// if we fail on the first run and have not updated, update and check the ones we haven't tried so far.
if !pidsUpdated {
// log.Trace("process: socket not found in any process of user, updating table")
// log.Trace("proc: socket not found in any process of user, updating table")
// update
updatePids()
// sort for faster search
Expand All @@ -67,7 +94,7 @@ func FindPID(uid, inode int) (pid int) { //nolint:gocognit // TODO
}

// check all other pids
// log.Trace("process: socket not found in any process of user, checking all pids")
// log.Trace("proc: socket not found in any process of user, checking all pids")
// TODO: find best order for pidsByUser for best performance
for possibleUID, pids := range pidsByUser {
if possibleUID != uid {
Expand All @@ -93,7 +120,7 @@ func findSocketFromPid(pid, inode int) bool {
link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%s", pid, entry))
if err != nil {
if !os.IsNotExist(err) {
log.Warningf("process: failed to read link /proc/%d/fd/%s: %s", pid, entry, err)
log.Warningf("proc: failed to read link /proc/%d/fd/%s: %s", pid, entry, err)
}
continue
}
Expand Down Expand Up @@ -122,12 +149,12 @@ entryLoop:

statData, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
if err != nil {
log.Warningf("process: could not stat /proc/%d: %s", pid, err)
log.Warningf("proc: could not stat /proc/%d: %s", pid, err)
continue entryLoop
}
sys, ok := statData.Sys().(*syscall.Stat_t)
if !ok {
log.Warningf("process: unable to parse /proc/%d: wrong type", pid)
log.Warningf("proc: unable to parse /proc/%d: wrong type", pid)
continue entryLoop
}

Expand All @@ -152,14 +179,14 @@ func readDirNames(dir string) (names []string) {
file, err := os.Open(dir)
if err != nil {
if !os.IsNotExist(err) {
log.Warningf("process: could not open directory %s: %s", dir, err)
log.Warningf("proc: could not open directory %s: %s", dir, err)
}
return
}
defer file.Close()
names, err = file.Readdirnames(0)
if err != nil {
log.Warningf("process: could not get entries from directory %s: %s", dir, err)
log.Warningf("proc: could not get entries from directory %s: %s", dir, err)
return []string{}
}
return
Expand Down
26 changes: 12 additions & 14 deletions network/proc/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const (
udp4ProcFile = "/proc/net/udp"
udp6ProcFile = "/proc/net/udp6"

UnfetchedProcessID = -2

tcpListenStateHex = "0A"
)

Expand Down Expand Up @@ -114,7 +112,7 @@ func getTableFromSource(stack uint8, procFile string) (connections []*socket.Con
for scanner.Scan() {
fields := strings.FieldsFunc(scanner.Text(), procDelimiter)
if len(fields) < 14 {
// log.Tracef("process: too short: %s", fields)
// log.Tracef("proc: too short: %s", fields)
continue
}

Expand All @@ -125,21 +123,21 @@ func getTableFromSource(stack uint8, procFile string) (connections []*socket.Con

localPort, err := strconv.ParseUint(fields[fieldIndexLocalPort], 16, 16)
if err != nil {
log.Warningf("process: could not parse port: %s", err)
log.Warningf("proc: could not parse port: %s", err)
continue
}

uid, err := strconv.ParseInt(fields[fieldIndexUID], 10, 32)
// log.Tracef("uid: %s", fields[fieldIndexUID])
if err != nil {
log.Warningf("process: could not parse uid %s: %s", fields[11], err)
log.Warningf("proc: could not parse uid %s: %s", fields[11], err)
continue
}

inode, err := strconv.ParseInt(fields[fieldIndexInode], 10, 32)
// log.Tracef("inode: %s", fields[fieldIndexInode])
if err != nil {
log.Warningf("process: could not parse inode %s: %s", fields[13], err)
log.Warningf("proc: could not parse inode %s: %s", fields[13], err)
continue
}

Expand All @@ -151,7 +149,7 @@ func getTableFromSource(stack uint8, procFile string) (connections []*socket.Con
IP: localIP,
Port: uint16(localPort),
},
PID: UnfetchedProcessID,
PID: socket.UnidentifiedProcessID,
UID: int(uid),
Inode: int(inode),
})
Expand All @@ -166,7 +164,7 @@ func getTableFromSource(stack uint8, procFile string) (connections []*socket.Con
IP: localIP,
Port: uint16(localPort),
},
PID: UnfetchedProcessID,
PID: socket.UnidentifiedProcessID,
UID: int(uid),
Inode: int(inode),
})
Expand All @@ -180,7 +178,7 @@ func getTableFromSource(stack uint8, procFile string) (connections []*socket.Con

remotePort, err := strconv.ParseUint(fields[fieldIndexRemotePort], 16, 16)
if err != nil {
log.Warningf("process: could not parse port: %s", err)
log.Warningf("proc: could not parse port: %s", err)
continue
}

Expand All @@ -193,7 +191,7 @@ func getTableFromSource(stack uint8, procFile string) (connections []*socket.Con
IP: remoteIP,
Port: uint16(remotePort),
},
PID: UnfetchedProcessID,
PID: socket.UnidentifiedProcessID,
UID: int(uid),
Inode: int(inode),
})
Expand All @@ -211,11 +209,11 @@ func procDelimiter(c rune) bool {
func convertIPv4(data string) net.IP {
decoded, err := hex.DecodeString(data)
if err != nil {
log.Warningf("process: could not parse IPv4 %s: %s", data, err)
log.Warningf("proc: could not parse IPv4 %s: %s", data, err)
return nil
}
if len(decoded) != 4 {
log.Warningf("process: decoded IPv4 %s has wrong length", decoded)
log.Warningf("proc: decoded IPv4 %s has wrong length", decoded)
return nil
}
ip := net.IPv4(decoded[3], decoded[2], decoded[1], decoded[0])
Expand All @@ -225,11 +223,11 @@ func convertIPv4(data string) net.IP {
func convertIPv6(data string) net.IP {
decoded, err := hex.DecodeString(data)
if err != nil {
log.Warningf("process: could not parse IPv6 %s: %s", data, err)
log.Warningf("proc: could not parse IPv6 %s: %s", data, err)
return nil
}
if len(decoded) != 16 {
log.Warningf("process: decoded IPv6 %s has wrong length", decoded)
log.Warningf("proc: decoded IPv6 %s has wrong length", decoded)
return nil
}
ip := net.IP(decoded)
Expand Down
12 changes: 6 additions & 6 deletions network/proc/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestSockets(t *testing.T) {
}
fmt.Println("\nTCP 4 connections:")
for _, connection := range connections {
pid := FindPID(connection.UID, connection.Inode)
pid := FindConnectionPID(connection)
fmt.Printf("%d: %+v\n", pid, connection)
}
fmt.Println("\nTCP 4 listeners:")
for _, listener := range listeners {
pid := FindPID(listener.UID, listener.Inode)
pid := FindBindPID(listener)
fmt.Printf("%d: %+v\n", pid, listener)
}

Expand All @@ -29,12 +29,12 @@ func TestSockets(t *testing.T) {
}
fmt.Println("\nTCP 6 connections:")
for _, connection := range connections {
pid := FindPID(connection.UID, connection.Inode)
pid := FindConnectionPID(connection)
fmt.Printf("%d: %+v\n", pid, connection)
}
fmt.Println("\nTCP 6 listeners:")
for _, listener := range listeners {
pid := FindPID(listener.UID, listener.Inode)
pid := FindBindPID(listener)
fmt.Printf("%d: %+v\n", pid, listener)
}

Expand All @@ -44,7 +44,7 @@ func TestSockets(t *testing.T) {
}
fmt.Println("\nUDP 4 binds:")
for _, bind := range binds {
pid := FindPID(bind.UID, bind.Inode)
pid := FindBindPID(bind)
fmt.Printf("%d: %+v\n", pid, bind)
}

Expand All @@ -54,7 +54,7 @@ func TestSockets(t *testing.T) {
}
fmt.Println("\nUDP 6 binds:")
for _, bind := range binds {
pid := FindPID(bind.UID, bind.Inode)
pid := FindBindPID(bind)
fmt.Printf("%d: %+v\n", pid, bind)
}
}
40 changes: 34 additions & 6 deletions network/state/system_linux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package state

import (
"time"

"github.com/safing/portmaster/network/proc"
"github.com/safing/portmaster/network/socket"
)
Expand All @@ -13,15 +15,41 @@ var (
)

func checkConnectionPID(socketInfo *socket.ConnectionInfo, connInbound bool) (pid int, inbound bool, err error) {
if socketInfo.PID == proc.UnfetchedProcessID {
socketInfo.PID = proc.FindPID(socketInfo.UID, socketInfo.Inode)
for i := 0; i <= lookupRetries; i++ {
// look for PID
pid = proc.FindConnectionPID(socketInfo)
if pid != socket.UnidentifiedProcessID {
// if we found a PID, return
break
}

// every time, except for the last iteration
if i < lookupRetries {
// we found no PID, we could have been too fast, give the kernel some time to think
// back off timer: with 3ms baseWaitTime: 3, 6, 9, 12, 15, 18, 21ms - 84ms in total
time.Sleep(time.Duration(i+1) * baseWaitTime)
}
}
return socketInfo.PID, connInbound, nil

return pid, connInbound, nil
}

func checkBindPID(socketInfo *socket.BindInfo, connInbound bool) (pid int, inbound bool, err error) {
if socketInfo.PID == proc.UnfetchedProcessID {
socketInfo.PID = proc.FindPID(socketInfo.UID, socketInfo.Inode)
for i := 0; i <= lookupRetries; i++ {
// look for PID
pid = proc.FindBindPID(socketInfo)
if pid != socket.UnidentifiedProcessID {
// if we found a PID, return
break
}

// every time, except for the last iteration
if i < lookupRetries {
// we found no PID, we could have been too fast, give the kernel some time to think
// back off timer: with 3ms baseWaitTime: 3, 6, 9, 12, 15, 18, 21ms - 84ms in total
time.Sleep(time.Duration(i+1) * baseWaitTime)
}
}
return socketInfo.PID, connInbound, nil

return pid, connInbound, nil
}

0 comments on commit 9f42e6f

Please sign in to comment.