From c84b9a0c7ca77a44d0d296ceb3bd25f2dc7a8417 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Sat, 24 Feb 2024 10:15:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20handle=20case=20where=20ipv4=20o?= =?UTF-8?q?r=20ipv6=20is=20disabled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- providers/os/resources/port.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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