diff --git a/providers/os/resources/port.go b/providers/os/resources/port.go index ad8c486e01..e92ffad07e 100644 --- a/providers/os/resources/port.go +++ b/providers/os/resources/port.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "net/netip" + "os" "regexp" "strconv" "strings" @@ -242,13 +243,16 @@ func (p *mqlPorts) processesBySocket() (map[int64]*mqlProcess, error) { return processes.BySocketID, err } -// See: -// - socket/address parsing: https://wiki.christophchamp.com/index.php?title=Unix_sockets +// parseProcNet parses the proc filesystem +// See socket/address parsing: https://wiki.christophchamp.com/index.php?title=Unix_sockets func (p *mqlPorts) parseProcNet(path string, protocol string, users map[int64]*mqlUser) ([]interface{}, error) { conn := p.MqlRuntime.Connection.(shared.Connection) fs := conn.FileSystem() stat, err := fs.Stat(path) - if err != nil { + // if the file does not exist, we just return nil since no ports are open then + if errors.Is(err, os.ErrNotExist) { + return nil, nil + } else if err != nil { return nil, errors.New("cannot access stat for " + path) } if stat.IsDir() { @@ -379,6 +383,8 @@ func (p *mqlPorts) listLinux() ([]interface{}, error) { return nil, err } + // check if kernel supports /proc/net/tcp4 + var ports []interface{} tcpPorts, err := p.parseProcNet("/proc/net/tcp", "tcp4", users) if err != nil { @@ -392,6 +398,8 @@ func (p *mqlPorts) listLinux() ([]interface{}, error) { } ports = append(ports, udpPorts...) + // check if kernel supports /proc/net/tcp6 + tcpPortsV6, err := p.parseProcNet("/proc/net/tcp6", "tcp6", users) if err != nil { return nil, err