mirror of
https://github.com/safing/portmaster.git
synced 2026-12-04 09:38:21 +00:00
090d3bfdfd
On Linux, memory usage grows without bound during bursts of new connections and does not recover for a long time afterwards, up to and including exhausting all available memory on the machine. Cause: the eBPF connection listener emits an info-only packet for every observed connect(). handlePacket() treated these like real packets and called NewIncompleteConnection() + SetFirewallHandler(), which allocates a Connection, a 100-slot packet queue and a goroutine per event, and registers the connection in the store. An info-only packet can never complete a connection: GatherConnectionInfo() returns early, dataComplete is never set, no verdict is issued and Save() is a no-op. Its only durable contribution is the PID, which lets the first real packet skip process.GetPidOfConnection() and its socket table scan. When no real packet follows - a connect() that fails, a UDP socket that never sends, or a SYN dropped under load - the connection is only reclaimed by the cleaner after DeleteIncompleteConnsAfterStartedThreshold. Under a burst, each such event therefore holds a goroutine and its allocations for that entire timeout, which is what drives the memory growth and the slow recovery. Keep only the reported PID instead, in a small cache keyed by connection ID and expired by a two-generation swap, so entries cost a fraction of a connection object and are bounded by the arrival rate over a couple of seconds, with no timers and no scanning. NewIncompleteConnection() adopts the hint when the packet carries no PID of its own, so the real-packet path and process attribution are unchanged. Also guard the info-only pull-forward in packetHandlerWorker() with !pidKnown. Info-only packets no longer start workers, so the first queued packet is now always a real packet and the branch would otherwise be entered for every new outbound connection, adding a wait for an info packet that was already consumed as a hint. Note: Windows kext v1 (old version) also reports info-only packets (WFP socket auth), so this path is active there too; kext v2 and other platforms are unaffected. https://github.com/safing/portmaster/issues/2233 https://github.com/safing/portmaster/issues/1870
201 lines
5.1 KiB
Go
201 lines
5.1 KiB
Go
package network
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/safing/portmaster/base/log"
|
|
"github.com/safing/portmaster/service/firewall/interception/dnsmonitor"
|
|
"github.com/safing/portmaster/service/mgr"
|
|
"github.com/safing/portmaster/service/netenv"
|
|
"github.com/safing/portmaster/service/network/state"
|
|
"github.com/safing/portmaster/service/profile"
|
|
"github.com/safing/portmaster/service/resolver"
|
|
)
|
|
|
|
// Events.
|
|
const (
|
|
ConnectionReattributedEvent = "connection re-attributed"
|
|
)
|
|
|
|
type Network struct {
|
|
mgr *mgr.Manager
|
|
instance instance
|
|
|
|
dnsRequestTicker *mgr.SleepyTicker
|
|
connectionCleanerTicker *mgr.SleepyTicker
|
|
pidHintTicker *mgr.SleepyTicker
|
|
|
|
EventConnectionReattributed *mgr.EventMgr[string]
|
|
}
|
|
|
|
func (n *Network) Manager() *mgr.Manager {
|
|
return n.mgr
|
|
}
|
|
|
|
func (n *Network) Start() error {
|
|
return start()
|
|
}
|
|
|
|
func (n *Network) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
func (n *Network) SetSleep(enabled bool) {
|
|
if n.dnsRequestTicker != nil {
|
|
n.dnsRequestTicker.SetSleep(enabled)
|
|
}
|
|
if n.connectionCleanerTicker != nil {
|
|
n.connectionCleanerTicker.SetSleep(enabled)
|
|
}
|
|
if n.pidHintTicker != nil {
|
|
n.pidHintTicker.SetSleep(enabled)
|
|
}
|
|
}
|
|
|
|
var defaultFirewallHandler FirewallHandler
|
|
|
|
// SetDefaultFirewallHandler sets the default firewall handler.
|
|
func SetDefaultFirewallHandler(handler FirewallHandler) {
|
|
if defaultFirewallHandler == nil {
|
|
defaultFirewallHandler = handler
|
|
}
|
|
}
|
|
|
|
func prep() error {
|
|
if netenv.IPv6Enabled() {
|
|
state.EnableTCPDualStack()
|
|
state.EnableUDPDualStack()
|
|
}
|
|
|
|
return registerAPIEndpoints()
|
|
}
|
|
|
|
func start() error {
|
|
err := registerAsDatabase()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := registerMetrics(); err != nil {
|
|
return err
|
|
}
|
|
|
|
module.mgr.Go("clean connections", connectionCleaner)
|
|
module.mgr.Go("write open dns requests", openDNSRequestWriter)
|
|
module.mgr.Go("rotate pid hints", pidHintRotator)
|
|
module.instance.Profile().EventDelete.AddCallback("re-attribute connections from deleted profile", reAttributeConnections)
|
|
|
|
return nil
|
|
}
|
|
|
|
var reAttributionLock sync.Mutex
|
|
|
|
// reAttributeConnections finds all connections of a deleted profile and re-attributes them.
|
|
// Expected event data: scoped profile ID.
|
|
func reAttributeConnections(_ *mgr.WorkerCtx, profileID string) (bool, error) {
|
|
profileSource, profileID, ok := strings.Cut(profileID, "/")
|
|
if !ok {
|
|
return false, fmt.Errorf("event data does not seem to be a scoped profile ID: %v", profileID)
|
|
}
|
|
|
|
// Hold a lock for re-attribution, to prevent simultaneous processing of the
|
|
// same connections and make logging cleaner.
|
|
reAttributionLock.Lock()
|
|
defer reAttributionLock.Unlock()
|
|
|
|
// Create tracing context.
|
|
ctx, tracer := log.AddTracer(context.Background())
|
|
defer tracer.Submit()
|
|
tracer.Infof("network: re-attributing connections from deleted profile %s/%s", profileSource, profileID)
|
|
|
|
// Count and log how many connections were re-attributed.
|
|
var reAttributed int
|
|
|
|
// Re-attribute connections.
|
|
for _, conn := range conns.clone() {
|
|
if reAttributeConnection(ctx, conn, profileID, profileSource) {
|
|
reAttributed++
|
|
tracer.Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
|
|
}
|
|
}
|
|
|
|
// Re-attribute dns connections.
|
|
for _, conn := range dnsConns.clone() {
|
|
if reAttributeConnection(ctx, conn, profileID, profileSource) {
|
|
reAttributed++
|
|
tracer.Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
|
|
}
|
|
}
|
|
|
|
tracer.Infof("filter: re-attributed %d connections", reAttributed)
|
|
return false, nil
|
|
}
|
|
|
|
func reAttributeConnection(ctx context.Context, conn *Connection, profileID, profileSource string) (reAttributed bool) {
|
|
// Lock the connection before checking anything to avoid a race condition with connection data collection.
|
|
conn.Lock()
|
|
defer conn.Unlock()
|
|
|
|
// Check if the connection has the profile we are looking for.
|
|
switch {
|
|
case !conn.DataIsComplete():
|
|
return false
|
|
case conn.ProcessContext.Profile != profileID:
|
|
return false
|
|
case conn.ProcessContext.Source != profileSource:
|
|
return false
|
|
}
|
|
|
|
// Attempt to assign new profile.
|
|
err := conn.process.RefetchProfile(ctx)
|
|
if err != nil {
|
|
log.Tracer(ctx).Warningf("network: failed to refetch profile for %s: %s", conn, err)
|
|
return false
|
|
}
|
|
|
|
// Set the new process context.
|
|
conn.ProcessContext = getProcessContext(ctx, conn.process)
|
|
conn.Save()
|
|
|
|
// Trigger event for re-attribution.
|
|
module.EventConnectionReattributed.Submit(conn.ID)
|
|
|
|
log.Tracer(ctx).Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
|
|
return true
|
|
}
|
|
|
|
var (
|
|
module *Network
|
|
shimLoaded atomic.Bool
|
|
)
|
|
|
|
// New returns a new Network module.
|
|
func New(instance instance) (*Network, error) {
|
|
if !shimLoaded.CompareAndSwap(false, true) {
|
|
return nil, errors.New("only one instance allowed")
|
|
}
|
|
m := mgr.New("Network")
|
|
module = &Network{
|
|
mgr: m,
|
|
instance: instance,
|
|
EventConnectionReattributed: mgr.NewEventMgr[string](ConnectionReattributedEvent, m),
|
|
}
|
|
|
|
if err := prep(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return module, nil
|
|
}
|
|
|
|
type instance interface {
|
|
Profile() *profile.ProfileModule
|
|
Resolver() *resolver.ResolverModule
|
|
DNSMonitor() *dnsmonitor.DNSMonitor
|
|
}
|