Repository navigation
host-local: guard RangeIter.Next against NextIP returning nil - #1316
ravi-arnan wants to merge 2 commits into
Conversation
Fixes containernetworking#1296. RangeIter.Next called ip.NextIP(i.cur) and returned &net.IPNet{IP: i.cur, Mask: ...} without checking if NextIP returned nil. When an address family boundary overflow occurred (e.g. 255.255.255.255 or ffff:...:ffff) in a non-canonical range, i.cur became nil, returning an invalid IPNet with nil IP and causing subsequent Next calls to restart at RangeStart and loop indefinitely. Advance to the next range in the set when NextIP returns nil, and ensure the iterator returns nil when exhausted. Signed-off-by: Ravi Arnan <raviarnankeren@gmail.com>
Karthik-Chowdary
left a comment
There was a problem hiding this comment.
Agent-generated review (Hermes / GPT-6.1 Sol), with independently executed local tests. Optional regression-test suggestion: the new boundary specs construct RangeIter directly, so they do not cover GetIter starting from LastReservedIP at the family maximum. A small Ginkgo case could seed that last-reserved address with RangeEnd deliberately unset, then roll over to a second range whose RangeStart equals its Gateway. Using different subnet masks for the two ranges would also check that the returned IPNet mask and gateway follow the selected range.
I exercised this for both families. With the second range running from .1/::1 (gateway) through .3/::3, the current patch returns .2/::2, .3/::3, then the original family maximum, and exactly the fourth Next returns nil,nil. Every result has the expected owning-range mask and gateway. The same bounded test against the parent allocator logic fails: its first result is a non-nil IPNet with IP:nil and it does not complete that cycle. This would cover the interaction between last-reserved initialization, overflow rollover, gateway skipping, and full-cycle detection rather than only overflow itself.
This is optional coverage for the noncanonical/programmatically constructed hardening scope in #1296, not a production-config reproducer or a merge blocker.
Test provenance: Go 1.26.8 linux/amd64; PR head 4d659294ac77804a8d34cb10cd7960b6f0a3b106 versus exact parent ca3e021020b07674006ccccd6f74f50c3351b87b. The isolated test uses NewFakeStore with last-reserved key 0, first-range masks /30 (IPv4) and /126 (IPv6), and second-range masks /29 and /125. Command: go test -count=1 -v -run ^TestIndependentLastReservedFamilyBoundary$ ./plugins/ipam/host-local/backend/allocator. The allocator package also passes go test -race -count=1; no daemon/network integration run is claimed.
…ll cycle Covers the interaction between LastReservedIP initialization at the address family boundary, multi-range rollover, gateway skipping, and full-cycle exhaustion detection across both IPv4 and IPv6. Signed-off-by: Ravi Arnan <raviarnankeren@gmail.com>
|
Thanks for the suggestion and detailed test scenario @Karthik-Chowdary! I've added the regression test spec covering this in commit 89c08f1:
|
Fixes #1296.
Problem
RangeIter.Nextcalledi.cur = ip.NextIP(i.cur)and returned&net.IPNet{IP: i.cur, Mask: r.Subnet.Mask}without checking ifNextIPreturnednil. Following #1293,NextIPreturnsnilupon an address family boundary overflow (e.g.255.255.255.255orffff:...:ffff) in a non-canonical or programmatically constructed range set.When
i.curbecamenil:Next()returned an*net.IPNetwhoseIPwasnil. If passed to the disk store, this wrote a lease file named"<nil>".i.cur == nilre-triggered initialization atr.RangeStart, causing the iterator to loop indefinitely rather than terminating.Solution
rangesetvalidity and bounds.ip.NextIP(i.cur)returnsnil, advance to the next range in the set (i.rangeIdx++) and reseti.cur = r.RangeStart.Next()returnsnil, nilwhen the range set has been completely traversed.Tests
plugins/ipam/host-local/backend/allocator/allocator_test.go:NextIPreturnsnilat IPv4 family boundary (255.255.255.255).NextIPreturnsnilat IPv6 family boundary (ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff).plugins/ipam/host-local/...pass (83 host-local specs, 53 allocator specs, 9 disk specs).go vet ./plugins/ipam/host-local/...is clean.