Skip to content

Commit 9a9f30f

Browse files
committed
Restrict access to EC2 metadata IP
1 parent 4dfa913 commit 9a9f30f

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

cmd/sdn-cni-plugin/openshift-sdn_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ func (p *cniPlugin) testCmdAdd(args *skel.CmdArgs) (types.Result, error) {
120120
}
121121

122122
var iptablesCommands = [][]string{
123+
// Block MCS
123124
{"-A", "OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "22623", "-j", "REJECT"},
124125
{"-A", "OUTPUT", "-p", "tcp", "-m", "tcp", "--dport", "22624", "-j", "REJECT"},
125126
{"-A", "FORWARD", "-p", "tcp", "-m", "tcp", "--dport", "22623", "-j", "REJECT"},
126127
{"-A", "FORWARD", "-p", "tcp", "-m", "tcp", "--dport", "22624", "-j", "REJECT"},
128+
129+
// Block cloud metadata IP
130+
{"-A", "OUTPUT", "-d", "169.254.169.254", "-j", "REJECT"},
131+
{"-A", "FORWARD", "-d", "169.254.169.254", "-j", "REJECT"},
127132
}
128133

129134
func (p *cniPlugin) CmdAdd(args *skel.CmdArgs) error {
@@ -234,7 +239,7 @@ func (p *cniPlugin) CmdAdd(args *skel.CmdArgs) error {
234239
}
235240
}
236241

237-
// HACK: block access to MCS until we can secure it properly
242+
// Block access to certain things
238243
for _, args := range iptablesCommands {
239244
out, err := exec.Command("iptables", args...).CombinedOutput()
240245
if err != nil {

pkg/network/admission/restrictedendpoints/endpoint_admission.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ type restrictedEndpointsAdmission struct {
6464

6565
authorizer authorizer.Authorizer
6666
restrictedNetworks []*net.IPNet
67-
restrictedPorts []kapi.EndpointPort
6867
}
6968

7069
var _ = initializer.WantsAuthorizer(&restrictedEndpointsAdmission{})
@@ -87,10 +86,6 @@ func NewRestrictedEndpointsAdmission(restrictedNetworks []*net.IPNet) *restricte
8786
return &restrictedEndpointsAdmission{
8887
Handler: admission.NewHandler(admission.Create, admission.Update),
8988
restrictedNetworks: restrictedNetworks,
90-
restrictedPorts: []kapi.EndpointPort{
91-
{Protocol: kapi.ProtocolTCP, Port: 22623},
92-
{Protocol: kapi.ProtocolTCP, Port: 22624},
93-
},
9489
}
9590
}
9691

@@ -105,14 +100,26 @@ func (r *restrictedEndpointsAdmission) ValidateInitialization() error {
105100
return nil
106101
}
107102

108-
func (r *restrictedEndpointsAdmission) findRestrictedIP(ep *kapi.Endpoints) error {
103+
var (
104+
defaultRestrictedPorts = []kapi.EndpointPort{
105+
// MCS ports
106+
{Protocol: kapi.ProtocolTCP, Port: 22623},
107+
{Protocol: kapi.ProtocolTCP, Port: 22624},
108+
}
109+
defaultRestrictedNetworks = []*net.IPNet{
110+
// IPv4 link-local range 169.254.0.0/16 (including cloud metadata IP)
111+
{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)},
112+
}
113+
)
114+
115+
func (r *restrictedEndpointsAdmission) findRestrictedIP(ep *kapi.Endpoints, restricted []*net.IPNet) error {
109116
for _, subset := range ep.Subsets {
110117
for _, addr := range subset.Addresses {
111118
ip := net.ParseIP(addr.IP)
112119
if ip == nil {
113120
continue
114121
}
115-
for _, net := range r.restrictedNetworks {
122+
for _, net := range restricted {
116123
if net.Contains(ip) {
117124
return fmt.Errorf("endpoint address %s is not allowed", addr.IP)
118125
}
@@ -122,10 +129,10 @@ func (r *restrictedEndpointsAdmission) findRestrictedIP(ep *kapi.Endpoints) erro
122129
return nil
123130
}
124131

125-
func (r *restrictedEndpointsAdmission) findRestrictedPort(ep *kapi.Endpoints) error {
132+
func (r *restrictedEndpointsAdmission) findRestrictedPort(ep *kapi.Endpoints, restricted []kapi.EndpointPort) error {
126133
for _, subset := range ep.Subsets {
127134
for _, port := range subset.Ports {
128-
for _, restricted := range r.restrictedPorts {
135+
for _, restricted := range restricted {
129136
if port.Protocol == restricted.Protocol && port.Port == restricted.Port {
130137
return fmt.Errorf("endpoint port %s:%d is not allowed", string(port.Protocol), port.Port)
131138
}
@@ -164,9 +171,12 @@ func (r *restrictedEndpointsAdmission) Validate(a admission.Attributes) error {
164171
return nil
165172
}
166173

167-
restrictedErr := r.findRestrictedIP(ep)
174+
restrictedErr := r.findRestrictedIP(ep, r.restrictedNetworks)
175+
if restrictedErr == nil {
176+
restrictedErr = r.findRestrictedIP(ep, defaultRestrictedNetworks)
177+
}
168178
if restrictedErr == nil {
169-
restrictedErr = r.findRestrictedPort(ep)
179+
restrictedErr = r.findRestrictedPort(ep, defaultRestrictedPorts)
170180
}
171181
if restrictedErr == nil {
172182
return nil

pkg/network/node/ovscontroller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ func (oc *ovsController) SetupOVS(clusterNetworkCIDR []string, serviceNetworkCID
160160
otx.AddFlow("table=30, priority=100, ip, nw_dst=%s, actions=goto_table:90", clusterCIDR)
161161
}
162162

163+
// Link-local traffic
164+
otx.AddFlow("table=30, priority=75, ip, nw_dst=169.254.0.0/16, actions=drop")
165+
163166
// Multicast coming from the VXLAN
164167
otx.AddFlow("table=30, priority=50, in_port=1, ip, nw_dst=224.0.0.0/4, actions=goto_table:120")
165168
// Multicast coming from local pods

0 commit comments

Comments
 (0)