Skip to content

Commit

Permalink
Merge pull request #1279 from safing/fix/conn-handling-history
Browse files Browse the repository at this point in the history
Fix connection handling and improve history
  • Loading branch information
dhaavi authored Aug 11, 2023
2 parents cf4df13 + 58c4b44 commit 4e41b88
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 18 deletions.
9 changes: 9 additions & 0 deletions firewall/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ func FilterResolvedDNS(
return rrCache
}

// Finalize verdict.
defer func() {
// Reset from previous filtering.
conn.Verdict.Active = network.VerdictUndecided
conn.Verdict.Worst = network.VerdictUndecided
// Update all values again.
finalizeVerdict(conn)
}()

// special grant for connectivity domains
if checkConnectivityDomain(ctx, conn, layeredProfile, nil) {
// returns true if check triggered
Expand Down
9 changes: 8 additions & 1 deletion firewall/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ type deciderFn func(context.Context, *network.Connection, *profile.LayeredProfil

var defaultDeciders = []deciderFn{
checkPortmasterConnection,
checkSelfCommunication,
// TODO: This is currently very slow.
// Find a way to improve performance using the eBPF data.
// checkSelfCommunication,
checkIfBroadcastReply,
checkConnectionType,
checkConnectionScope,
Expand Down Expand Up @@ -619,6 +621,11 @@ matchLoop:
}

func checkCustomFilterList(_ context.Context, conn *network.Connection, p *profile.LayeredProfile, _ packet.Packet) bool {
// Check if any custom list is loaded at all.
if !customlists.IsLoaded() {
return false
}

// block if the domain name appears in the custom filter list (check for subdomains if enabled)
if conn.Entity.Domain != "" {
if ok, match := customlists.LookupDomain(conn.Entity.Domain, p.FilterSubDomains()); ok {
Expand Down
19 changes: 19 additions & 0 deletions intel/customlists/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ func initFilterLists() {
domainsFilterList = make(map[string]struct{})
}

// IsLoaded returns whether a custom filter list is loaded.
func IsLoaded() bool {
filterListLock.RLock()
defer filterListLock.RUnlock()

switch {
case len(domainsFilterList) > 0:
return true
case len(ipAddressesFilterList) > 0:
return true
case len(countryCodesFilterList) > 0:
return true
case len(autonomousSystemsFilterList) > 0:
return true
default:
return false
}
}

func parseFile(filePath string) error {
// Reset all maps, previous (if any) settings will be lost.
for key := range countryCodesFilterList {
Expand Down
2 changes: 1 addition & 1 deletion netquery/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (mng *Manager) HandleFeed(ctx context.Context, feed <-chan *network.Connect

// Save to netquery database.
// Do not include internal connections in history.
if err := mng.store.Save(ctx, *model, conn.HistoryEnabled && !conn.Internal); err != nil {
if err := mng.store.Save(ctx, *model, conn.HistoryEnabled); err != nil {
log.Errorf("netquery: failed to save connection %s in sqlite database: %s", conn.ID, err)
return
}
Expand Down
7 changes: 5 additions & 2 deletions network/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func cleanConnections() (activePIDs map[int]struct{}) {
// Step 2: mark as ended
if !exists {
conn.Ended = nowUnix

// Stop the firewall handler, in case one is running.
conn.StopFirewallHandler()

// Save to database.
conn.Save()
}

Expand All @@ -93,8 +98,6 @@ func cleanConnections() (activePIDs map[int]struct{}) {
// DEBUG:
// log.Tracef("network.clean: deleted %s (ended at %s)", conn.DatabaseKey(), time.Unix(conn.Ended, 0))

// Stop the firewall handler, in case one is running.
conn.StopFirewallHandler()
// Remove connection from state.
conn.delete()
}
Expand Down
14 changes: 11 additions & 3 deletions network/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ func (conn *Connection) SetLocalIP(ip net.IP) {
conn.LocalIPScope = netutils.GetIPScope(ip)
}

// UpdateFeatures checks which connection related features may be used and sets
// the flags accordingly.
// UpdateFeatures checks which connection related features may and should be
// used and sets the flags accordingly.
// The caller must hold a lock on the connection.
func (conn *Connection) UpdateFeatures() error {
// Get user.
Expand All @@ -591,7 +591,15 @@ func (conn *Connection) UpdateFeatures() error {

// Check if history may be used and if it is enabled for this application.
conn.HistoryEnabled = false
if user.MayUse(account.FeatureHistory) {
switch {
case conn.Internal:
// Do not record internal connections, as they are of low interest in the history.
// TODO: Should we create a setting for this?
case conn.Entity.IPScope.IsLocalhost():
// Do not record localhost-only connections, as they are very low interest in the history.
// TODO: Should we create a setting for this?
case user.MayUse(account.FeatureHistory):
// Check if history may be used and is enabled.
lProfile := conn.Process().Profile()
if lProfile != nil {
conn.HistoryEnabled = lProfile.EnableHistory()
Expand Down
10 changes: 6 additions & 4 deletions profile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func registerConfiguration() error { //nolint:maintidx
err := config.Register(&config.Option{
Name: "Default Network Action",
Key: CfgOptionDefaultActionKey,
Description: `The default network action is applied when nothing else allows or blocks an outgoing connection. Incoming connections are always blocked by default.`,
Description: `The default network action is applied when nothing else allows or blocks a connection. This affects both outgoing and incoming connections. This setting is the weakest of all and is commonly overruled by Force Block settings or Rules.`,
OptType: config.OptTypeString,
DefaultValue: DefaultActionPermitValue,
Annotations: config.Annotations{
Expand Down Expand Up @@ -252,9 +252,11 @@ func registerConfiguration() error { //nolint:maintidx

// Enable History
err = config.Register(&config.Option{
Name: "Enable Network History",
Key: CfgOptionEnableHistoryKey,
Description: "Save connections in a database (on disk) in order to view and search them later. Changes might take a couple minutes to apply to all connections.",
Name: "Enable Network History",
Key: CfgOptionEnableHistoryKey,
Description: `Save connections in a database (on disk) in order to view and search them later. Changes might take a couple minutes to apply to all connections.
In order to reduce noise optimize performance, internal and device-only (localhost) connections are not saved to history.`,
OptType: config.OptTypeBool,
ReleaseLevel: config.ReleaseLevelStable,
ExpertiseLevel: config.ExpertiseLevelUser,
Expand Down
28 changes: 21 additions & 7 deletions profile/special.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ func createSpecialProfile(profileID string, path string) *Profile {
// attributed to a connection of a regular process. Otherwise, users
// would see two connection prompts for the same domain.
CfgOptionDefaultActionKey: DefaultActionPermitValue,
// Explicitly allow incoming connections.
CfgOptionBlockInboundKey: status.SecurityLevelOff,
// Disable force blockers.
CfgOptionBlockScopeInternetKey: status.SecurityLevelOff,
CfgOptionBlockScopeLANKey: status.SecurityLevelOff,
CfgOptionBlockScopeLocalKey: status.SecurityLevelOff,
CfgOptionBlockP2PKey: status.SecurityLevelOff,
CfgOptionBlockInboundKey: status.SecurityLevelOff,
// Explicitly allow localhost and answers to multicast protocols that
// are commonly used by system resolvers.
// TODO: When the Portmaster gains the ability to attribute multicast
Expand Down Expand Up @@ -233,7 +237,12 @@ func createSpecialProfile(profileID string, path string) *Profile {
Source: SourceLocal,
PresentationPath: path,
Config: map[string]interface{}{
CfgOptionDefaultActionKey: DefaultActionBlockValue,
CfgOptionDefaultActionKey: DefaultActionBlockValue,
CfgOptionBlockScopeInternetKey: status.SecurityLevelOff,
CfgOptionBlockScopeLANKey: status.SecurityLevelOff,
CfgOptionBlockScopeLocalKey: status.SecurityLevelOff,
CfgOptionBlockP2PKey: status.SecurityLevelOff,
CfgOptionBlockInboundKey: status.SecurityLevelsAll,
CfgOptionEndpointsKey: []string{
"+ Localhost",
"+ .safing.io",
Expand All @@ -248,7 +257,12 @@ func createSpecialProfile(profileID string, path string) *Profile {
Source: SourceLocal,
PresentationPath: path,
Config: map[string]interface{}{
CfgOptionDefaultActionKey: DefaultActionBlockValue,
CfgOptionDefaultActionKey: DefaultActionBlockValue,
CfgOptionBlockScopeInternetKey: status.SecurityLevelOff,
CfgOptionBlockScopeLANKey: status.SecurityLevelOff,
CfgOptionBlockScopeLocalKey: status.SecurityLevelOff,
CfgOptionBlockP2PKey: status.SecurityLevelOff,
CfgOptionBlockInboundKey: status.SecurityLevelsAll,
CfgOptionEndpointsKey: []string{
"+ Localhost",
},
Expand Down Expand Up @@ -281,11 +295,11 @@ func specialProfileNeedsReset(profile *Profile) bool {

switch profile.ID {
case SystemResolverProfileID:
return canBeUpgraded(profile, "21.10.2022")
return canBeUpgraded(profile, "12.8.2023") // FIXME: set one day after stable release date.
case PortmasterProfileID:
return canBeUpgraded(profile, "21.10.2022")
return canBeUpgraded(profile, "12.8.2023") // FIXME: set one day after stable release date.
case PortmasterAppProfileID:
return canBeUpgraded(profile, "8.9.2021")
return canBeUpgraded(profile, "12.8.2023") // FIXME: set one day after stable release date.
default:
// Not a special profile or no upgrade available yet.
return false
Expand Down

0 comments on commit 4e41b88

Please sign in to comment.