From ed2338fdb94478b462f5e833883e242e9023d9c8 Mon Sep 17 00:00:00 2001 From: Vladimir Stoilov Date: Mon, 2 Dec 2024 15:15:46 +0200 Subject: [PATCH] [service] Fix error on unitilized dns monitor --- .../interception/dnsmonitor/etwlink_windows.go | 7 +++++++ .../interception/dnsmonitor/eventlistener_windows.go | 3 +++ service/integration/integration_windows.go | 12 ++++++++++++ service/network/connection.go | 6 +++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/service/firewall/interception/dnsmonitor/etwlink_windows.go b/service/firewall/interception/dnsmonitor/etwlink_windows.go index e0d48bdcf..cb9d8675f 100644 --- a/service/firewall/interception/dnsmonitor/etwlink_windows.go +++ b/service/firewall/interception/dnsmonitor/etwlink_windows.go @@ -68,6 +68,10 @@ func (l *ETWSession) IsRunning() bool { // FlushTrace flushes the trace buffer. func (l *ETWSession) FlushTrace() error { + if l.i == nil { + return fmt.Errorf("session not initialized") + } + l.shutdownMutex.Lock() defer l.shutdownMutex.Unlock() @@ -86,6 +90,9 @@ func (l *ETWSession) StopTrace() error { // DestroySession closes the session and frees the allocated memory. Listener cannot be used after this function is called. func (l *ETWSession) DestroySession() error { + if l.i == nil { + return fmt.Errorf("session not initialized") + } l.shutdownMutex.Lock() defer l.shutdownMutex.Unlock() diff --git a/service/firewall/interception/dnsmonitor/eventlistener_windows.go b/service/firewall/interception/dnsmonitor/eventlistener_windows.go index 283101365..a46e8cc6b 100644 --- a/service/firewall/interception/dnsmonitor/eventlistener_windows.go +++ b/service/firewall/interception/dnsmonitor/eventlistener_windows.go @@ -52,6 +52,9 @@ func initializeSessions(module *DNSMonitor, listener *Listener) error { } func (l *Listener) flush() error { + if l.etw == nil { + return fmt.Errorf("etw not initialized") + } return l.etw.FlushTrace() } diff --git a/service/integration/integration_windows.go b/service/integration/integration_windows.go index 0ca123fc0..b297b3638 100644 --- a/service/integration/integration_windows.go +++ b/service/integration/integration_windows.go @@ -5,6 +5,7 @@ package integration import ( "fmt" + "sync" "github.com/safing/portmaster/base/log" "github.com/safing/portmaster/service/mgr" @@ -24,14 +25,25 @@ func (i *OSIntegration) Initialize() error { if err != nil { log.Errorf("integration: failed to load dll: %s", err) + callbackLock := sync.Mutex{} // listen for event from the updater and try to load again if any. i.instance.Updates().EventResourcesUpdated.AddCallback("core-dll-loader", func(wc *mgr.WorkerCtx, s struct{}) (cancel bool, err error) { + // Make sure no multiple callas are executed at the same time. + callbackLock.Lock() + defer callbackLock.Unlock() + + // Try to load again. err = i.loadDLL() if err != nil { log.Errorf("integration: failed to load dll: %s", err) + } else { + log.Info("integration: initialize successful after updater event") } return false, nil }) + + } else { + log.Info("integration: initialize successful") } return nil } diff --git a/service/network/connection.go b/service/network/connection.go index b3dd70fcc..1c1bbf198 100644 --- a/service/network/connection.go +++ b/service/network/connection.go @@ -550,7 +550,11 @@ func (conn *Connection) GatherConnectionInfo(pkt packet.Packet) (err error) { if module.instance.Resolver().IsDisabled() && conn.shouldWaitForDomain() { // Flush the dns listener buffer and try again. for i := range 4 { - _ = module.instance.DNSMonitor().Flush() + err = module.instance.DNSMonitor().Flush() + if err != nil { + // Error flushing, dont try again. + break + } ipinfo, err = resolver.GetIPInfo(resolver.IPInfoProfileScopeGlobal, pkt.Info().RemoteIP().String()) if err == nil { log.Tracer(pkt.Ctx()).Debugf("network: found domain from dnsmonitor after %d tries", i+1)