Skip to content

host-local: guard RangeIter.Next against NextIP returning nil - #1316

Open
ravi-arnan wants to merge 2 commits into
containernetworking:mainfrom
ravi-arnan:feat/rangeiter-nil-guard
Open

ravi-arnan wants to merge 2 commits into
containernetworking:mainfrom
ravi-arnan:feat/rangeiter-nil-guard

Conversation

@ravi-arnan

Copy link
Copy Markdown
Contributor

Fixes #1296.

Problem

RangeIter.Next called i.cur = ip.NextIP(i.cur) and returned &net.IPNet{IP: i.cur, Mask: r.Subnet.Mask} without checking if NextIP returned nil. Following #1293, NextIP returns nil upon an address family boundary overflow (e.g. 255.255.255.255 or ffff:...:ffff) in a non-canonical or programmatically constructed range set.

When i.cur became nil:

  1. Next() returned an *net.IPNet whose IP was nil. If passed to the disk store, this wrote a lease file named "<nil>".
  2. On the subsequent iteration, i.cur == nil re-triggered initialization at r.RangeStart, causing the iterator to loop indefinitely rather than terminating.

Solution

  • Check for rangeset validity and bounds.
  • If ip.NextIP(i.cur) returns nil, advance to the next range in the set (i.rangeIdx++) and reset i.cur = r.RangeStart.
  • Guard against exhausted or invalid ranges so Next() returns nil, nil when the range set has been completely traversed.

Tests

  • Added specs to plugins/ipam/host-local/backend/allocator/allocator_test.go:
    • Verified termination when NextIP returns nil at IPv4 family boundary (255.255.255.255).
    • Verified termination when NextIP returns nil at IPv6 family boundary (ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff).
    • Verified clean rollover from family boundary overflow to subsequent ranges in a multi-range set.
  • All Ginkgo specs in plugins/ipam/host-local/... pass (83 host-local specs, 53 allocator specs, 9 disk specs).
  • go vet ./plugins/ipam/host-local/... is clean.

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 Karthik-Chowdary left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@ravi-arnan

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion and detailed test scenario @Karthik-Chowdary!

I've added the regression test spec covering this in commit 89c08f1:

  • Seeds LastReservedIP at the family maximum (255.255.255.255 on /30, ffff:...:ffff on /126) with RangeEnd unset.
  • Rolls over to a second range where RangeStart == Gateway (10.0.0.1 on /29, 2001:db8::1 on /125), verifying gateway skipping (.2), boundary traversal (.3), mask/gateway mapping from the selected range, rollover back to the family maximum on the first range, and clean termination (nil, nil) on the fourth Next() call.
  • Verified across both IPv4 and IPv6; all 83 host-local specs pass cleanly, including with -race.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

host-local: RangeIter.Next assumes NextIP never returns nil

2 participants