Skip to content

Commit a543cbc

Browse files
authored
Merge pull request #2596 from bboehmke/portmapper_ipv6
Added improved IP validation for port mapper
2 parents 535ef36 + bb5425e commit a543cbc

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

portmapper/mapper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart,
151151
}
152152

153153
containerIP, containerPort := getIPAndPort(m.container)
154-
if hostIP.To4() != nil || hostIP.To16() != nil {
154+
if pm.checkIP(hostIP) {
155155
if err := pm.AppendForwardingTableEntry(m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort); err != nil {
156156
return nil, err
157157
}
@@ -160,7 +160,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart,
160160
cleanup := func() error {
161161
// need to undo the iptables rules before we return
162162
m.userlandProxy.Stop()
163-
if hostIP.To4() != nil || hostIP.To16() != nil {
163+
if pm.checkIP(hostIP) {
164164
pm.DeleteForwardingTableEntry(m.proto, hostIP, allocatedHostPort, containerIP.String(), containerPort)
165165
if err := pm.Allocator.ReleasePort(hostIP, m.proto, allocatedHostPort); err != nil {
166166
return err

portmapper/mapper_linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ func (pm *PortMapper) forward(action iptables.Action, proto string, sourceIP net
4444
}
4545
return pm.chain.Forward(action, sourceIP, sourcePort, proto, containerIP, containerPort, pm.bridgeName)
4646
}
47+
48+
// checkIP checks if IP is valid and matching to chain version
49+
func (pm *PortMapper) checkIP(ip net.IP) bool {
50+
if pm.chain == nil || pm.chain.IPTable.Version == iptables.IPv4 {
51+
return ip.To4() != nil
52+
}
53+
return ip.To16() != nil
54+
}

portmapper/mapper_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ func (pm *PortMapper) AppendForwardingTableEntry(proto string, sourceIP net.IP,
2929
func (pm *PortMapper) DeleteForwardingTableEntry(proto string, sourceIP net.IP, sourcePort int, containerIP string, containerPort int) error {
3030
return nil
3131
}
32+
33+
// checkIP checks if IP is valid and matching to chain version
34+
func (pm *PortMapper) checkIP(ip net.IP) bool {
35+
// no IPv6 for port mapper on windows -> only IPv4 valid
36+
return ip.To4() != nil
37+
}

0 commit comments

Comments
 (0)