Skip to content

Commit 361d6fe

Browse files
authored
outbound traffic from pod should be intercepted in filter table INPUT chain (#891)
(pod's traffic that is destined to node's local ip). with out this fix even with network policy to drop all egress traffic, pod can reach host IP's. Pod's can access any service hosted in host network as well
1 parent df40aa5 commit 361d6fe

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

pkg/controllers/netpol/network_policy_controller.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -888,37 +888,23 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(version string) (map[s
888888
}
889889
}
890890

891-
// ensure there is rule in filter table and FORWARD chain to jump to pod specific firewall chain
892-
// this rule applies to the traffic getting forwarded/routed (traffic from the pod destinted
893-
// to pod on a different node)
894-
comment = "rule to jump traffic from POD name:" + pod.name + " namespace: " + pod.namespace +
895-
" to chain " + podFwChainName
896-
args = []string{"-m", "comment", "--comment", comment, "-s", pod.ip, "-j", podFwChainName}
897-
exists, err = iptablesCmdHandler.Exists("filter", "FORWARD", args...)
898-
if err != nil {
899-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
900-
}
901-
if !exists {
902-
err := iptablesCmdHandler.Insert("filter", "FORWARD", 1, args...)
891+
egressFilterChains := []string{"FORWARD", "OUTPUT", "INPUT"}
892+
for _, chain := range egressFilterChains {
893+
// ensure there is rule in filter table and FORWARD chain to jump to pod specific firewall chain
894+
// this rule applies to the traffic getting forwarded/routed (traffic from the pod destinted
895+
// to pod on a different node)
896+
comment = "rule to jump traffic from POD name:" + pod.name + " namespace: " + pod.namespace +
897+
" to chain " + podFwChainName
898+
args = []string{"-m", "comment", "--comment", comment, "-s", pod.ip, "-j", podFwChainName}
899+
exists, err = iptablesCmdHandler.Exists("filter", chain, args...)
903900
if err != nil {
904901
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
905902
}
906-
}
907-
908-
// ensure there is rule in filter table and OUTPUT chain to jump to pod specific firewall chain
909-
// this rule applies to the traffic getting proxied (traffic from the pod accessing service
910-
// resulting in traffic DNAT'ed to a pod IP)
911-
comment = "rule to jump traffic from POD name:" + pod.name + " namespace: " + pod.namespace +
912-
" to chain " + podFwChainName
913-
args = []string{"-m", "comment", "--comment", comment, "-s", pod.ip, "-j", podFwChainName}
914-
exists, err = iptablesCmdHandler.Exists("filter", "OUTPUT", args...)
915-
if err != nil {
916-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
917-
}
918-
if !exists {
919-
err := iptablesCmdHandler.Insert("filter", "OUTPUT", 1, args...)
920-
if err != nil {
921-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
903+
if !exists {
904+
err := iptablesCmdHandler.Insert("filter", chain, 1, args...)
905+
if err != nil {
906+
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
907+
}
922908
}
923909
}
924910

0 commit comments

Comments
 (0)