Skip to content

Commit 2ad01f3

Browse files
committed
fix: We need to make a copy of callers ip thats passed in since its a slice. In go, when passing non pointer var that are slices, maps, or channels, it just makes a copy of the obj header and not the underlying data. Since this passed ip can be used in the fallback logic, we need to make sure we copy it so there is no bug in future.
1 parent eb34207 commit 2ad01f3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

cloud/nodeipam/ipam/cloud_allocator.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,21 @@ func getIPv6PodCIDR(ip net.IP, desiredMask int) (*net.IPNet, bool) {
367367
return nil, false
368368
}
369369

370+
// We need to make a copy so we don't mutate caller's backing array (net.IP is a slice)
371+
ipCopy := make(net.IP, len(ip))
372+
copy(ipCopy, ip)
373+
370374
// Keep first 64 bits (bytes 0..7) and set hextets 5..7 to 0, c, 0 respectively
371375
// Hextet index to byte mapping: h5->[8,9], h6->[10,11], h7->[12,13]
372-
ip[8], ip[9] = 0x00, 0x00 // :0
373-
ip[10], ip[11] = 0x00, 0x0c // :c
374-
ip[12], ip[13] = 0x00, 0x00 // :0
376+
ipCopy[8], ipCopy[9] = 0x00, 0x00 // :0
377+
ipCopy[10], ipCopy[11] = 0x00, 0x0c // :c
378+
ipCopy[12], ipCopy[13] = 0x00, 0x00 // :0
375379
// last hextet (bytes 14..15) will be zeroed by mask below
376380

377381
podMask := net.CIDRMask(desiredMask, ipv6BitLen)
378382
// Ensure the address is the network address for the desired mask
379-
ip = ip.Mask(podMask)
380-
podCIDR := &net.IPNet{IP: ip, Mask: podMask}
383+
ipCopy = ipCopy.Mask(podMask)
384+
podCIDR := &net.IPNet{IP: ipCopy, Mask: podMask}
381385

382386
return podCIDR, true
383387
}
@@ -455,6 +459,7 @@ func (c *cloudAllocator) allocateIPv6CIDR(ctx context.Context, node *v1.Node) (*
455459
// get pod cidr using stable mnemonic subprefix :0:c::/112
456460
if podCIDR, ok := getIPv6PodCIDR(ip, c.nodeCIDRMaskSizeIPv6); ok {
457461
logger.V(4).Info("Using stable IPv6 PodCIDR subprefix :0:c::/112", "ip", ip, "podCIDR", podCIDR)
462+
// Verify the /112 PodCIDR is fully contained within the base /64 range
458463
if !base.Contains(podCIDR.IP) {
459464
return nil, fmt.Errorf("stable IPv6 PodCIDR %s is not contained in base range %s", podCIDR, base)
460465
}

0 commit comments

Comments
 (0)