Skip to content

Commit

Permalink
[service] Fix module failure when dll is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Dec 2, 2024
1 parent ef0995b commit 2a9d754
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
9 changes: 6 additions & 3 deletions service/firewall/interception/dnsmonitor/etwlink_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

type ETWSession struct {
i integration.ETWFunctions
i *integration.ETWFunctions

shutdownGuard atomic.Bool
shutdownMutex sync.Mutex
Expand All @@ -23,7 +23,10 @@ type ETWSession struct {
}

// NewSession creates new ETW event listener and initilizes it. This is a low level interface, make sure to call DestorySession when you are done using it.
func NewSession(etwInterface integration.ETWFunctions, callback func(domain string, result string)) (*ETWSession, error) {
func NewSession(etwInterface *integration.ETWFunctions, callback func(domain string, result string)) (*ETWSession, error) {
if etwInterface == nil {
return nil, fmt.Errorf("etw interface was nil")
}
etwSession := &ETWSession{
i: etwInterface,
}
Expand All @@ -47,7 +50,7 @@ func NewSession(etwInterface integration.ETWFunctions, callback func(domain stri
// Initialize session.
err := etwSession.i.InitializeSession(etwSession.state)
if err != nil {
return nil, fmt.Errorf("failed to initialzie session: %q", err)
return nil, fmt.Errorf("failed to initialize session: %q", err)
}

return etwSession, nil
Expand Down
25 changes: 19 additions & 6 deletions service/firewall/interception/dnsmonitor/eventlistener_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@ func newListener(module *DNSMonitor) (*Listener, error) {
ResolverInfo.Source = resolver.ServerSourceETW

listener := &Listener{}
var err error
// Initialize new dns event session.
listener.etw, err = NewSession(module.instance.OSIntegration().GetETWInterface(), listener.processEvent)
err := initializeSessions(module, listener)
if err != nil {
return nil, err
// Listen for event if the dll has been loaded
module.instance.OSIntegration().OnInitializedEvent.AddCallback("loader-listener", func(wc *mgr.WorkerCtx, s struct{}) (cancel bool, err error) {
err = initializeSessions(module, listener)
if err != nil {
return false, err
}
return true, nil
})
}
return listener, nil
}

// Start listening for events.
func initializeSessions(module *DNSMonitor, listener *Listener) error {
var err error
listener.etw, err = NewSession(module.instance.OSIntegration().GetETWInterface(), listener.processEvent)
if err != nil {
return err
}
// Start listener
module.mgr.Go("etw-dns-event-listener", func(w *mgr.WorkerCtx) error {
return listener.etw.StartTrace()
})

return listener, nil
return nil
}

func (l *Listener) flush() error {
Expand Down
4 changes: 2 additions & 2 deletions service/integration/etw_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type ETWFunctions struct {
stopOldSession *windows.Proc
}

func initializeETW(dll *windows.DLL) (ETWFunctions, error) {
var functions ETWFunctions
func initializeETW(dll *windows.DLL) (*ETWFunctions, error) {
functions := &ETWFunctions{}
var err error
functions.createState, err = dll.FindProc("PM_ETWCreateState")
if err != nil {
Expand Down
32 changes: 27 additions & 5 deletions service/integration/integration_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,42 @@ package integration
import (
"fmt"

"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/updates"
"golang.org/x/sys/windows"
)

type OSSpecific struct {
dll *windows.DLL
etwFunctions ETWFunctions
etwFunctions *ETWFunctions
}

// Initialize loads the dll and finds all the needed functions from it.
func (i *OSIntegration) Initialize() error {
// Try to load dll
err := i.loadDLL()
if err != nil {
log.Errorf("integration: failed to load dll: %s", err)

// 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) {
err = i.loadDLL()
if err != nil {
log.Errorf("integration: failed to load dll: %s", err)
}
return false, nil
})
}
return nil
}

func (i *OSIntegration) loadDLL() error {
// Find path to the dll.
file, err := updates.GetPlatformFile("dll/portmaster-core.dll")
if err != nil {
return err
}

// Load the DLL.
i.os.dll, err = windows.LoadDLL(file.Path())
if err != nil {
Expand All @@ -35,18 +54,21 @@ func (i *OSIntegration) Initialize() error {
return err
}

// Notify listeners
i.OnInitializedEvent.Submit(struct{}{})

return nil
}

// CleanUp releases any resourses allocated during initializaion.
// CleanUp releases any resources allocated during initialization.
func (i *OSIntegration) CleanUp() error {
if i.os.dll != nil {
return i.os.dll.Release()
}
return nil
}

// GetETWInterface return struct containing all the ETW related functions.
func (i *OSIntegration) GetETWInterface() ETWFunctions {
// GetETWInterface return struct containing all the ETW related functions, and nil if it was not loaded yet
func (i *OSIntegration) GetETWInterface() *ETWFunctions {
return i.os.etwFunctions
}
12 changes: 6 additions & 6 deletions service/integration/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

// OSIntegration module provides special integration with the OS.
type OSIntegration struct {
m *mgr.Manager
states *mgr.StateMgr
m *mgr.Manager

OnInitializedEvent *mgr.EventMgr[struct{}]

//nolint:unused
os OSSpecific
Expand All @@ -20,10 +21,9 @@ type OSIntegration struct {
func New(instance instance) (*OSIntegration, error) {
m := mgr.New("OSIntegration")
module := &OSIntegration{
m: m,
states: m.NewStateMgr(),

instance: instance,
m: m,
OnInitializedEvent: mgr.NewEventMgr[struct{}]("on-initialized", m),
instance: instance,
}

return module, nil
Expand Down

0 comments on commit 2a9d754

Please sign in to comment.