Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround dns kernel bug #95

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
queueID int
metricsBindAddress string
hostnameOverride string
netfilterBug1766Fix bool
)

func init() {
Expand All @@ -43,6 +44,7 @@ func init() {
flag.IntVar(&queueID, "nfqueue-id", 100, "Number of the nfqueue used")
flag.StringVar(&metricsBindAddress, "metrics-bind-address", ":9080", "The IP address and port for the metrics server to serve on")
flag.StringVar(&hostnameOverride, "hostname-override", "", "If non-empty, will be used as the name of the Node that kube-network-policies is running on. If unset, the node name is assumed to be the same as the node's hostname.")
flag.BoolVar(&netfilterBug1766Fix, "netfilter-bug-1766-fix", true, "If set, process DNS packets on the PREROUTING hooks to avoid the race condition on the conntrack subsystem, not needed for kernels 6.12+ (see https://bugzilla.netfilter.org/show_bug.cgi?id=1766)")

flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: kube-network-policies [options]\n\n")
Expand All @@ -55,7 +57,9 @@ func main() {
klog.InitFlags(nil)
flag.Parse()

klog.Infof("flags: %v", flag.Args())
flag.VisitAll(func(flag *flag.Flag) {
klog.Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})

if _, _, err := net.SplitHostPort(metricsBindAddress); err != nil {
klog.Fatalf("error parsing metrics bind address %s : %v", metricsBindAddress, err)
Expand All @@ -72,6 +76,7 @@ func main() {
FailOpen: failOpen,
QueueID: queueID,
NodeName: nodeName,
NetfilterBug1766Fix: netfilterBug1766Fix,
}
// creates the in-cluster config
config, err := rest.InClusterConfig()
Expand Down
211 changes: 147 additions & 64 deletions pkg/networkpolicy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Config struct {
BaselineAdminNetworkPolicy bool
QueueID int
NodeName string
NetfilterBug1766Fix bool
}

// NewController returns a new *Controller.
Expand Down Expand Up @@ -123,7 +124,7 @@ func newController(client clientset.Interface,
nft: nft,
queue: workqueue.NewTypedRateLimitingQueueWithConfig(
workqueue.DefaultTypedControllerRateLimiter[string](),
workqueue.TypedRateLimitingQueueConfig[string]{Name: "controllerName"},
workqueue.TypedRateLimitingQueueConfig[string]{Name: controllerName},
aojea marked this conversation as resolved.
Show resolved Hide resolved
),
}

Expand Down Expand Up @@ -681,83 +682,97 @@ func (c *Controller) syncNFTablesRules(ctx context.Context) error {
// IPVS packets follow a different path in netfilter, so we process
// everything in the POSTROUTING hook before SNAT happens.
// Ref: https://github.com/kubernetes-sigs/kube-network-policies/issues/46
for _, hook := range []knftables.BaseChainHook{knftables.PostroutingHook} {
chainName := string(hook)
tx.Add(&knftables.Chain{
Name: chainName,
Type: knftables.PtrTo(knftables.FilterType),
Hook: knftables.PtrTo(hook),
Priority: knftables.PtrTo(knftables.SNATPriority + "-5"),
})
tx.Flush(&knftables.Chain{
Name: chainName,
hook := knftables.PostroutingHook
chainName := string(hook)
tx.Add(&knftables.Chain{
Name: chainName,
Type: knftables.PtrTo(knftables.FilterType),
Hook: knftables.PtrTo(hook),
Priority: knftables.PtrTo(knftables.SNATPriority + "-5"),
})
tx.Flush(&knftables.Chain{
Name: chainName,
})

// DNS is processed by addDNSRacersWorkaroundRules()
// TODO: remove once kernel fix is on most distros
if c.config.NetfilterBug1766Fix {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: "udp dport 53 accept",
Comment: ptr.To("process DNS traffic on PREROUTING hook with network policy enforcement to avoid netfilter race condition bug"),
})
// IPv6 needs ICMP Neighbor Discovery to work
}

// IPv6 needs ICMP Neighbor Discovery to work
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"icmpv6", "type", "{", "nd-neighbor-solicit, nd-neighbor-advert", "}", "accept"),
})
// Don't process traffic generated from the root user in the Node, it can block kubelet probes
// or system daemons that depend on the internal node traffic to not be blocked.
// Ref: https://github.com/kubernetes-sigs/kube-network-policies/issues/65
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: "meta skuid 0 accept",
})
// instead of aggregating all the expresion in one rule, use two different
// rules to understand if is causing issues with UDP packets with the same
// tuple (https://github.com/kubernetes-sigs/kube-network-policies/issues/12)
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ct", "state", "established,related", "accept"),
})

action := fmt.Sprintf("queue num %d", c.config.QueueID)
if c.config.FailOpen {
action += " bypass"
}

// only if no admin network policies are used
if !c.config.AdminNetworkPolicy && !c.config.BaselineAdminNetworkPolicy {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"icmpv6", "type", "{", "nd-neighbor-solicit, nd-neighbor-advert", "}", "accept"),
"ip", "saddr", "@", podV4IPsSet, action,
),
Comment: ptr.To("process IPv4 traffic with network policy enforcement"),
})
// Don't process traffic generated from the root user in the Node, it can block kubelet probes
// or system daemons that depend on the internal node traffic to not be blocked.
// Ref: https://github.com/kubernetes-sigs/kube-network-policies/issues/65

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: "meta skuid 0 accept",
Rule: knftables.Concat(
"ip", "daddr", "@", podV4IPsSet, action,
),
Comment: ptr.To("process IPv4 traffic with network policy enforcement"),
})
// instead of aggregating all the expresion in one rule, use two different
// rules to understand if is causing issues with UDP packets with the same
// tuple (https://github.com/kubernetes-sigs/kube-network-policies/issues/12)

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ct", "state", "established,related", "accept"),
"ip6", "saddr", "@", podV6IPsSet, action,
),
Comment: ptr.To("process IPv6 traffic with network policy enforcement"),
})

action := fmt.Sprintf("queue num %d", c.config.QueueID)
if c.config.FailOpen {
action += " bypass"
}

// only if no admin network policies are used
if !c.config.AdminNetworkPolicy && !c.config.BaselineAdminNetworkPolicy {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip", "saddr", "@", podV4IPsSet, action,
),
Comment: ptr.To("process IPv4 traffic with network policy enforcement"),
})

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip", "daddr", "@", podV4IPsSet, action,
),
Comment: ptr.To("process IPv4 traffic with network policy enforcement"),
})

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip6", "saddr", "@", podV6IPsSet, action,
),
Comment: ptr.To("process IPv6 traffic with network policy enforcement"),
})
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip6", "daddr", "@", podV6IPsSet, action,
),
Comment: ptr.To("process IPv6 traffic with network policy enforcement"),
})
} else {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: action,
})
}

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip6", "daddr", "@", podV6IPsSet, action,
),
Comment: ptr.To("process IPv6 traffic with network policy enforcement"),
})
} else {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: action,
})
}
if c.config.NetfilterBug1766Fix {
c.addDNSRacersWorkaroundRules(tx)
}

if err := c.nft.Run(ctx, tx); err != nil {
Expand All @@ -767,6 +782,74 @@ func (c *Controller) syncNFTablesRules(ctx context.Context) error {
return nil
}

// To avoid a kernel bug caused by UDP DNS request racing with conntrack
// process the DNS packets only on the PREROUTING hook after DNAT happens
// so we can see the resolved destination IPs, typically the ones of the Pods
// that are used for the Kubernetes DNS Service.
// xref: https://github.com/kubernetes-sigs/kube-network-policies/issues/12
aojea marked this conversation as resolved.
Show resolved Hide resolved
// This can be removed once all kernels contain the fix in
// https://github.com/torvalds/linux/commit/8af79d3edb5fd2dce35ea0a71595b6d4f9962350
aojea marked this conversation as resolved.
Show resolved Hide resolved
// TODO: remove once kernel fix is on most distros
func (c *Controller) addDNSRacersWorkaroundRules(tx *knftables.Transaction) {
hook := knftables.PreroutingHook
chainName := string(hook)
tx.Add(&knftables.Chain{
Name: chainName,
Type: knftables.PtrTo(knftables.FilterType),
Hook: knftables.PtrTo(hook),
Priority: knftables.PtrTo(knftables.DNATPriority + "+5"),
})
tx.Flush(&knftables.Chain{
Name: chainName,
})

action := fmt.Sprintf("queue num %d", c.config.QueueID)
if c.config.FailOpen {
action += " bypass"
}

if !c.config.AdminNetworkPolicy && !c.config.BaselineAdminNetworkPolicy {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip", "saddr", "@", podV4IPsSet, "udp dport 53", action,
),
Comment: ptr.To("process IPv4 traffic destined to a DNS server with network policy enforcement"),
})

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip", "daddr", "@", podV4IPsSet, "udp dport 53", action,
),
Comment: ptr.To("process IPv4 traffic destined to a DNS server with network policy enforcement"),
})

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip6", "saddr", "@", podV6IPsSet, "udp dport 53", action,
),
Comment: ptr.To("process IPv6 traffic destined to a DNS server with network policy enforcement"),
})

tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"ip6", "daddr", "@", podV6IPsSet, "udp dport 53", action,
),
Comment: ptr.To("process IPv6 traffic destined to a DNS server with network policy enforcement"),
})
} else {
tx.Add(&knftables.Rule{
Chain: chainName,
Rule: knftables.Concat(
"udp dport 53", action,
),
})
}
}

func (c *Controller) cleanNFTablesRules() {
tx := c.nft.NewTransaction()
// Add+Delete is idempotent and won't return an error if the table doesn't already
Expand Down
Loading