Skip to content

fix(agent): reject out-of-range ports instead of narrowing them in the capture cache (#1220) - #1231

Merged
bpalermo merged 7 commits into
mainfrom
fix/1220-integer-conversion
Oct 5, 2026
Merged

bpalermo merged 7 commits into
mainfrom
fix/1220-integer-conversion

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Mechanism

CodeQL alert #4 (go/incorrect-integer-conversion, HIGH) flagged agent/internal/xds/cache/capture.go:724:

port, _ := strconv.Atoi(entry.sni)
la := proxy.UDPLoadAssignment(entry.loadAssignment, udpName, uint32(port))

Atoi yields a 64-bit int; uint32(port) wraps, so "4294967376" (2^32+80) would address port 80 and a negative value would wrap to a large one. In practice this site was guarded: captureUDPClusters only reaches it after udpClusterForLocked accepted the same entry.sni (1..65535) under the same clusterMu read lock, and entry.sni is always rendered by strconv.Itoa from a uint32 port. So it was not exploitable today, but the guard lived in another function and the unchecked conversion was one refactor away from shipping a wrapped port.

Fix

The three entry.sni parses in capture.go (udpClusterForLocked, captureUDPClusters, primaryPortOf) now go through one helper:

func entryPort(sni string) (uint32, bool) {
	p, err := strconv.ParseUint(sni, 10, 16)
	if err != nil || p == 0 {
		return 0, false
	}
	return uint32(p), true
}

An out-of-range value is rejected, never narrowed. Each caller keeps how it already handled malformed input: udpClusterForLocked returns "" and the service is skipped, primaryPortOf returns 0 ("no alias"), and captureUDPClusters skips the service with continue, which cannot happen while the guard above holds. ParseUint also rejects "+80", which Atoi accepted. That doesn't matter for Itoa output.

Sibling sweep across agent/internal/xds/... and common/: proxy/cluster.go already parses at 16 bits. server/odcds.go:286 and proxy/egress.go:348 only check that the value parses and discard it. common/procsched parses at 64 bits into uint64 and int without narrowing. None has the same shape, so nothing else changes.

Tests (entryport_test.go)

  • TestEntryPort: max valid (65535), max+1, 0, -1, -4294967216 and 4294967376 (both wrap to 80 through uint32()), 2^32+65535, empty, +80, 80, non-numeric.
  • TestPrimaryPortOf_RejectsOutOfRange: 2^32+8080 must not alias 8080.
  • TestUDPClusterForLocked_OutOfRangePortSkipped: a UDP entry's sni is corrupted to 2^32+5353, and the test asserts no udp: cluster is named.

Red: I swapped entryPort's body for the old Atoi + uint32() shape. All three tests failed: TestEntryPort/{65536,-1,-4294967216,4294967376,4295032831,+80}, TestPrimaryPortOf_RejectsOutOfRange and TestUDPClusterForLocked_OutOfRangePortSkipped.
Green: bazel test --config=race --runs_per_test=10 //agent/internal/xds/cache:cache_test: 10/10 passed. make format-check is clean, and bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation //agent/... //prober/... //common/... passes.

Fixes #1220

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 7 commits October 4, 2026 19:34
…e capture cache (#1220)

captureUDPClusters parsed the UDP entry's port with strconv.Atoi and passed
uint32(port) to UDPLoadAssignment with no bound (CodeQL alert #4,
go/incorrect-integer-conversion). The three entry.sni parses in capture.go now
share entryPort, which parses with strconv.ParseUint(s, 10, 16) and rejects 0,
so a value outside 1..65535 is skipped rather than wrapped (2^32+80 -> 80).

Fixes #1220

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit 68d0bb5 into main Oct 5, 2026
24 checks passed
@bpalermo
bpalermo deleted the fix/1220-integer-conversion branch October 5, 2026 00:03
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.

code scanning: go/incorrect-integer-conversion (high) in agent/internal/xds/cache/capture.go — alert #4 has never been triaged

1 participant