ensure connections from /proc/net/tcp{,6} get the right pid

ProcNet.Next does not allocate Connection structs, for efficiency.
Instead it always returns a *Connection pointing to the same instance.
As a result, any mutations by the caller to struct elements that
aren't actually set by ProcNet.Next, in particular Connection.Proc,
are carried across to subsequent calls.

This had hilarious consequences: connections referencing an inode
which we hadn't come across during proc walking would be associated
with the process corresponding to the last successfully looked up
inode.

The fix is to clear out the garbage left over from previous calls.

Fixes #2638.
This commit is contained in:
Matthias Radestock
2017-06-24 18:24:40 +01:00
committed by Alfonso Acosta
parent a7076e6092
commit bfd53d28e5

View File

@@ -28,6 +28,11 @@ func (c *pnConnIter) Next() *Connection {
}
if proc, ok := c.procs[n.Inode]; ok {
n.Proc = *proc
} else {
// ProcNet.Next() always returns a pointer to the same
// struct. We therefore must clear any garbage left over from
// the previous call.
n.Proc = Proc{}
}
return n
}