Skip to content

reject too-short PROXY v2 address length in mod_remoteip#683

Closed
arshsmith1 wants to merge 2 commits into
apache:trunkfrom
arshsmith1:remoteip-v2-len-check
Closed

reject too-short PROXY v2 address length in mod_remoteip#683
arshsmith1 wants to merge 2 commits into
apache:trunkfrom
arshsmith1:remoteip-v2-len-check

Conversation

@arshsmith1

Copy link
Copy Markdown

remoteip_process_v2_header only bounds the advertised v2 header length from above, so a peer sending a TCPv4 or TCPv6 family with a length shorter than its address block makes the code read past what was actually received from the uninitialized ctx->header buffer and adopt those bytes as the client IP used for logging and Require ip decisions. Reject the header when ntohs(hdr->v2.len) is smaller than the family address struct, matching the existing too-long check right beside it.

Comment thread modules/metadata/mod_remoteip.c Outdated
switch (hdr->v2.fam) {
case 0x11: /* TCPv4 */
if (ntohs(hdr->v2.len) < sizeof(hdr->v2.addr.ip4)) {
ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(10595)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please use APLOGNO() and let a maintainer fill in the next number as appropriate to avoid conflicts.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, both are bare APLOGNO() now and I dropped the next-number bump so the file isn't touched at all.

Your concern was well founded, by the way: trunk has moved to 10597 since I opened this, and 10596 is now taken by mod_cern_meta.c, so my hardcoded number would have collided exactly as you describe.

Comment thread modules/metadata/mod_remoteip.c Outdated
case 0x21: /* TCPv6 */
#if APR_HAVE_IPV6
if (ntohs(hdr->v2.len) < sizeof(hdr->v2.addr.ip6)) {
ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(10596)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same here, this one's bare APLOGNO() too.

@arshsmith1
arshsmith1 force-pushed the remoteip-v2-len-check branch from d1b4b70 to aa6c438 Compare July 17, 2026 14:48
@notroj

notroj commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks, sorry a couple more bits of feedback -

  1. remoteip_get_v2_len should probably be used here for consistency rather than the ntohs(...)
  2. would the size comparison not be better as a != rather than < - is there a case where it could be validly longer? I'm not familiar with the PROXY protocol but from a quick scan it looks like those records are fixed length

@arshsmith1

Copy link
Copy Markdown
Author

Both done in bfcf178. Swapped the two checks over to remoteip_get_v2_len and moved that helper above remoteip_process_v2_header so it's in scope; the log format is now APR_SIZE_T_FMT to match the return type (same idiom as the need=/rcvd= message further down).

On >= vs ==: the records are fixed length, but the v2 len field covers the address block plus any TLVs that follow it, so a longer value is legitimate. Spec section 2.2 says a receiver must use the length to skip the extra bytes it doesn't understand rather than reject them, and the block right after this already handles that (the too-long gate only rejects past sizeof(proxy_v2), and the parser just skips the remainder). So == would break any sender that appends TLVs, e.g. haproxy's PP2_TYPE_SSL or authority. The only unsafe case is len being smaller than the address struct we're about to read out of the buffer, which is what < catches.

@notroj

notroj commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Makes sense, thanks a lot.

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.

2 participants