mirror of
https://github.com/safing/portmaster.git
synced 2026-12-04 09:38:21 +00:00
13a86a43cc
Related ticket: #2104 On shutdown the NameServer module stopped its listeners with dns.Server.Shutdown, which waits without limit for all in-flight request handlers to return. A handler can be stuck in an upstream connect that the kernel never completes: a connection pended by a firewall driver cannot be cancelled. In #2104 the pending driver was our own kext, because the Interception module stops before Resolver and NameServer, so DoT dials issued in that window had nobody left to answer their verdict. The module Stop() has no time bound, so the whole shutdown stalled and the Windows service stayed in "Stopping" until reboot. - nameserver: stop listeners with ShutdownContext and a 10 second timeout. Reading from the listener socket stops immediately; only the wait for running handlers is bounded. Handlers still running afterwards are cancelled and waited for by the module manager as before. - resolver: do not dial a new upstream connection once the instance has started shutting down. The instance context is cancelled before any module stops, so this closes the window in which a dial could be pended after interception is gone. Existing pooled connections are still used. The resolver instance interface gains ShuttingDown() for this. The kext-side part (do not pend new connections after the Shutdown command) is a separate change that needs a signed driver release.
369 lines
9.3 KiB
Go
369 lines
9.3 KiB
Go
package nameserver
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/safing/portmaster/base/log"
|
|
"github.com/safing/portmaster/base/notifications"
|
|
"github.com/safing/portmaster/service/compat"
|
|
"github.com/safing/portmaster/service/firewall"
|
|
"github.com/safing/portmaster/service/mgr"
|
|
"github.com/safing/portmaster/service/netenv"
|
|
)
|
|
|
|
type NameServer struct {
|
|
mgr *mgr.Manager
|
|
instance instance
|
|
|
|
states *mgr.StateMgr
|
|
}
|
|
|
|
func (ns *NameServer) Manager() *mgr.Manager {
|
|
return ns.mgr
|
|
}
|
|
|
|
func (ns *NameServer) States() *mgr.StateMgr {
|
|
return ns.states
|
|
}
|
|
|
|
func (ns *NameServer) Start() error {
|
|
return start()
|
|
}
|
|
|
|
func (ns *NameServer) Stop() error {
|
|
return stop()
|
|
}
|
|
|
|
// listenerShutdownTimeout bounds how long stop() waits for in-flight DNS
|
|
// requests to finish. Requests that are still running afterwards are
|
|
// abandoned; the module manager cancels and waits for them separately.
|
|
const listenerShutdownTimeout = 10 * time.Second
|
|
|
|
var (
|
|
stopListeners bool
|
|
stopListener1 func(ctx context.Context) error
|
|
stopListener2 func(ctx context.Context) error
|
|
stopListenersLock sync.Mutex
|
|
|
|
eventIDConflictingService = "nameserver:conflicting-service"
|
|
eventIDListenerFailed = "nameserver:listener-failed"
|
|
)
|
|
|
|
func prep() error {
|
|
return registerConfig()
|
|
}
|
|
|
|
func start() error {
|
|
if err := registerMetrics(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Get listen addresses.
|
|
ip1, ip2, port, err := getListenAddresses(nameserverAddressConfig())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse nameserver listen address: %w", err)
|
|
}
|
|
|
|
// Tell the compat module where we are listening.
|
|
compat.SetNameserverListenIP(ip1)
|
|
|
|
// Get own hostname.
|
|
hostname, err = os.Hostname()
|
|
if err != nil {
|
|
log.Warningf("nameserver: failed to get hostname: %s", err)
|
|
}
|
|
hostname += "."
|
|
|
|
// Start listener(s).
|
|
if ip2 == nil {
|
|
// Start a single listener.
|
|
startListener(ip1, port, true)
|
|
|
|
// Set nameserver matcher in firewall to fast-track dns queries.
|
|
if ip1.Equal(net.IPv4zero) || ip1.Equal(net.IPv6zero) {
|
|
// Fast track dns queries destined for any of the local IPs.
|
|
return firewall.SetNameserverIPMatcher(func(ip net.IP) bool {
|
|
dstIsMe, err := netenv.IsMyIP(ip)
|
|
if err != nil {
|
|
log.Warningf("nameserver: failed to check if IP %s is local: %s", ip, err)
|
|
}
|
|
return dstIsMe
|
|
})
|
|
}
|
|
return firewall.SetNameserverIPMatcher(func(ip net.IP) bool {
|
|
return ip.Equal(ip1)
|
|
})
|
|
}
|
|
|
|
// Dual listener.
|
|
startListener(ip1, port, true)
|
|
startListener(ip2, port, false)
|
|
|
|
// Fast track dns queries destined for one of the listener IPs.
|
|
return firewall.SetNameserverIPMatcher(func(ip net.IP) bool {
|
|
return ip.Equal(ip1) || ip.Equal(ip2)
|
|
})
|
|
}
|
|
|
|
func startListener(ip net.IP, port uint16, first bool) {
|
|
// Start DNS server as service worker.
|
|
module.mgr.Go("dns resolver", func(ctx *mgr.WorkerCtx) error {
|
|
// Create DNS server.
|
|
dnsServer := &dns.Server{
|
|
Addr: net.JoinHostPort(
|
|
ip.String(),
|
|
strconv.Itoa(int(port)),
|
|
),
|
|
Net: "udp",
|
|
Handler: dns.HandlerFunc(handleRequestAsWorker),
|
|
UDPSize: dns.MaxMsgSize, // without this, one oversized datagram kills the listener on Windows
|
|
}
|
|
|
|
// Register stop function.
|
|
func() {
|
|
stopListenersLock.Lock()
|
|
defer stopListenersLock.Unlock()
|
|
|
|
// Check if we should stop
|
|
if stopListeners {
|
|
_ = dnsServer.Shutdown()
|
|
dnsServer = nil
|
|
return
|
|
}
|
|
|
|
// Register stop function.
|
|
if first {
|
|
stopListener1 = dnsServer.ShutdownContext
|
|
} else {
|
|
stopListener2 = dnsServer.ShutdownContext
|
|
}
|
|
}()
|
|
|
|
// Check if we should stop.
|
|
if dnsServer == nil {
|
|
return nil
|
|
}
|
|
|
|
// Resolve generic listener error, if primary listener.
|
|
if first {
|
|
module.states.Remove(eventIDListenerFailed)
|
|
}
|
|
|
|
started := false
|
|
dnsServer.NotifyStartedFunc = func() {
|
|
started = true
|
|
deleteListenErrorNotification(first)
|
|
}
|
|
|
|
// Start listening.
|
|
log.Infof("nameserver: starting to listen on %s", dnsServer.Addr)
|
|
err := dnsServer.ListenAndServe()
|
|
if err != nil {
|
|
// Stop worker without error if we are shutting down.
|
|
if module.mgr.IsDone() {
|
|
return nil
|
|
}
|
|
|
|
if !started {
|
|
// Failed to start listening. Probably some other application is already listening on the same port.
|
|
log.Warningf("nameserver: failed to start listening on %s: %s", dnsServer.Addr, err)
|
|
showListenErrorNotification(err, ip, port, first)
|
|
} else {
|
|
log.Warningf("nameserver: failed to serve on %s: %s", dnsServer.Addr, err)
|
|
}
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
func deleteListenErrorNotification(primaryListener bool) {
|
|
// Create suffix for secondary listener
|
|
var secondaryEventIDSuffix string
|
|
if !primaryListener {
|
|
secondaryEventIDSuffix = "-secondary"
|
|
}
|
|
// remove all possible notifications related to current listener (primary or secondary)
|
|
for _, eventID := range []string{eventIDConflictingService + secondaryEventIDSuffix, eventIDListenerFailed + secondaryEventIDSuffix} {
|
|
if n := notifications.Get(eventID); n != nil {
|
|
n.Delete()
|
|
}
|
|
}
|
|
}
|
|
|
|
func showListenErrorNotification(err error, ip net.IP, port uint16, primaryListener bool) {
|
|
var n *notifications.Notification
|
|
|
|
// Create suffix for secondary listener
|
|
var secondaryEventIDSuffix string
|
|
if !primaryListener {
|
|
secondaryEventIDSuffix = "-secondary"
|
|
}
|
|
|
|
// Find a conflicting service.
|
|
cfProcess := findConflictingProcess(ip, port)
|
|
if cfProcess != nil {
|
|
// Report the conflicting process.
|
|
|
|
// Build conflicting process description.
|
|
var cfDescription string
|
|
cfName, err := cfProcess.Name()
|
|
if err == nil && cfName != "" {
|
|
cfDescription = cfName
|
|
}
|
|
cfExe, err := cfProcess.Exe()
|
|
if err == nil && cfDescription != "" {
|
|
if cfDescription != "" {
|
|
cfDescription += " (" + cfExe + ")"
|
|
} else {
|
|
cfDescription = cfName
|
|
}
|
|
}
|
|
|
|
// Notify user about conflicting service.
|
|
n = notifications.Notify(¬ifications.Notification{
|
|
EventID: eventIDConflictingService + secondaryEventIDSuffix,
|
|
Type: notifications.Error,
|
|
Title: "Conflicting DNS Software",
|
|
Message: "Restart Portmaster after you have deactivated or properly configured the conflicting software: " +
|
|
cfDescription,
|
|
ShowOnSystem: true,
|
|
AvailableActions: []*notifications.Action{
|
|
{
|
|
Text: "Open Docs",
|
|
Type: notifications.ActionTypeOpenURL,
|
|
Payload: "https://docs.safing.io/portmaster/install/status/software-compatibility",
|
|
},
|
|
},
|
|
})
|
|
} else {
|
|
// If no conflict is found, report the error directly.
|
|
n = notifications.Notify(¬ifications.Notification{
|
|
EventID: eventIDListenerFailed + secondaryEventIDSuffix,
|
|
Type: notifications.Error,
|
|
Title: "Secure DNS Error",
|
|
Message: fmt.Sprintf(
|
|
"The internal DNS server failed. Restart Portmaster to try again. Error: %s",
|
|
err,
|
|
),
|
|
ShowOnSystem: true,
|
|
})
|
|
}
|
|
|
|
// Attach error to module, if primary listener.
|
|
if primaryListener {
|
|
n.SyncWithState(module.states)
|
|
}
|
|
}
|
|
|
|
func stop() error {
|
|
stopListenersLock.Lock()
|
|
defer stopListenersLock.Unlock()
|
|
|
|
// Stop listeners.
|
|
stopListeners = true
|
|
|
|
// Shutting down a dns.Server stops reading from its socket and then waits
|
|
// for all in-flight request handlers to return before closing the socket.
|
|
// A handler can be stuck in an upstream connect that the kernel never
|
|
// completes: a connection pended by a firewall driver (our own kernel
|
|
// extension after interception already stopped, see #2104, or a
|
|
// third-party one) cannot be cancelled. Without a bound, such a handler
|
|
// would block the whole shutdown forever.
|
|
ctx, cancel := context.WithTimeout(context.Background(), listenerShutdownTimeout)
|
|
defer cancel()
|
|
|
|
if stopListener1 != nil {
|
|
if err := stopListener1(ctx); err != nil {
|
|
log.Warningf("nameserver: failed to stop listener1: %s", err)
|
|
}
|
|
}
|
|
if stopListener2 != nil {
|
|
if err := stopListener2(ctx); err != nil {
|
|
log.Warningf("nameserver: failed to stop listener2: %s", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getListenAddresses(listenAddress string) (ip1, ip2 net.IP, port uint16, err error) {
|
|
// Split host and port.
|
|
ipString, portString, err := net.SplitHostPort(listenAddress)
|
|
if err != nil {
|
|
return nil, nil, 0, fmt.Errorf(
|
|
"failed to parse address %s: %w",
|
|
listenAddress,
|
|
err,
|
|
)
|
|
}
|
|
|
|
// Parse the IP address. If the want to listen on localhost, we need to
|
|
// listen separately for IPv4 and IPv6.
|
|
if ipString == "localhost" {
|
|
ip1 = net.IPv4(127, 0, 0, 17)
|
|
if netenv.IPv6Enabled() {
|
|
ip2 = net.IPv6loopback
|
|
} else {
|
|
log.Warningf("nameserver: no IPv6 stack detected, disabling IPv6 nameserver listener")
|
|
}
|
|
} else {
|
|
ip1 = net.ParseIP(ipString)
|
|
if ip1 == nil {
|
|
return nil, nil, 0, fmt.Errorf(
|
|
"failed to parse IP %s from %s",
|
|
ipString,
|
|
listenAddress,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Parse the port.
|
|
port64, err := strconv.ParseUint(portString, 10, 16)
|
|
if err != nil {
|
|
return nil, nil, 0, fmt.Errorf(
|
|
"failed to parse port %s from %s: %w",
|
|
portString,
|
|
listenAddress,
|
|
err,
|
|
)
|
|
}
|
|
|
|
return ip1, ip2, uint16(port64), nil
|
|
}
|
|
|
|
var (
|
|
module *NameServer
|
|
shimLoaded atomic.Bool
|
|
)
|
|
|
|
// New returns a new NameServer module.
|
|
func New(instance instance) (*NameServer, error) {
|
|
if !shimLoaded.CompareAndSwap(false, true) {
|
|
return nil, errors.New("only one instance allowed")
|
|
}
|
|
m := mgr.New("NameServer")
|
|
module = &NameServer{
|
|
mgr: m,
|
|
instance: instance,
|
|
|
|
states: mgr.NewStateMgr(m),
|
|
}
|
|
if err := prep(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return module, nil
|
|
}
|
|
|
|
type instance interface{}
|