Skip to content

fix: populate response TTL on macOS/BSD IPv4 (1-byte IP_RECVTTL cmsg) - #117

Merged
lance0 merged 1 commit into
masterfrom
fix/response-ttl-macos-bsd
Jul 6, 2026
Merged

lance0 merged 1 commit into
masterfrom
fix/response-ttl-macos-bsd

Conversation

@lance0

@lance0 lance0 commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Problem

The unsafe cmsg-parsing hardening added a length gate of CMSG_LEN(size_of::<c_int>()) before reading the TTL/hop-limit out of a control message. That is correct for:

  • Linux IPv4 IP_TTL — delivered as a 4-byte int
  • IPv6 hop-limit on every platform — a 4-byte int per RFC 3542

But the BSDs and macOS deliver the IPv4 IP_RECVTTL value as a single u_char. Its control message is CMSG_LEN(1), which is below the 4-byte gate, so it was skipped and response_ttl came back None. That blanks the return-path-hop / path-asymmetry estimate (record_response_ttl → estimate_return_hops) on macOS/FreeBSD/NetBSD IPv4.

The pre-hardening code happened to work only via a technically-UB 3-byte over-read of the control buffer; the hardening correctly removed the over-read but dropped the valid 1-byte case with it.

Fix

Read the value width-aware: gate on at least one data byte, read a 4-byte int when the payload is >= 4 bytes, otherwise read the single byte. The short-cmsg over-read protection is preserved.

Testing

  • New unit test builds a synthetic 1-byte and 4-byte cmsg and asserts both yield the TTL, plus that an empty payload is skipped. It exercises the platform-specific cmsg_type branches under their real cfg on the macOS and FreeBSD CI runners.
  • cargo fmt, cargo clippy --all-targets -- -D warnings, and the full test suite pass locally.

The cmsg length gate added when hardening the unsafe cmsg parser required
CMSG_LEN(size_of::<c_int>()). That is right for Linux IPv4 IP_TTL and the
IPv6 hop-limit (both arrive as a 4-byte int), but the BSDs and macOS deliver
the IPv4 IP_RECVTTL value as a single u_char. Its cmsg is CMSG_LEN(1), which
fell under the gate and was dropped, so response_ttl came back None and the
return-hop / path-asymmetry estimate derived from it went blank on those
platforms.

Read the value width-aware: require at least one data byte, read a 4-byte int
when the payload is >= 4 bytes, otherwise the single byte. This keeps the
short-cmsg over-read protection while restoring the 1-byte case.

Adds a regression test that feeds a synthetic 1-byte and 4-byte cmsg (the
1-byte case returned None before, Some after) and covers the empty-payload
skip; it runs the platform-specific cmsg_type branches under their real cfg
on the macOS and FreeBSD CI runners.
@lance0
lance0 merged commit 13f88a2 into master Jul 6, 2026
5 checks passed
@lance0
lance0 deleted the fix/response-ttl-macos-bsd branch July 6, 2026 17:15
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.

1 participant