Skip to content

Bound the length in IRRd A<len> replies - #141

Merged
job merged 2 commits into
bgp:mainfrom
DicksonWu654:bound-a-reply-length
Nov 19, 2026
Merged

job merged 2 commits into
bgp:mainfrom
DicksonWu654:bound-a-reply-length

Conversation

@DicksonWu654

Copy link
Copy Markdown
Contributor

This bounds the length bgpq4 accepts from an IRRd A<len> reply before allocating the receive buffer.

  • Pipelined path: malloc(togot + 2) wraps for a length near ULONG_MAX, and the reply is then copied into a tiny buffer.
  • Non-pipelined path: togot is a signed long, so a large value goes negative and the copy runs with a huge size.

Both paths now reject a length above 256 MiB with an error before allocating. The non-pipelined path parses the length as unsigned first. 256 MiB keeps the int offset in that path safe and leaves a lot of room for big AS-SET expansions, but I picked it, so change it to whatever you think fits.

Testing: make check passes. I also replayed both reply shapes from a local fake IRR server: main aborts, this branch exits with the error, and valid replies give the same output on both.

The length announced in an "A<len>" reply comes straight from the IRR
server. In the pipelined path malloc(togot + 2) wraps for a length near
ULONG_MAX and the reply is copied into a tiny buffer. In the non-pipelined
path togot is a signed long, so a large value goes negative and the copy
runs with a huge size.

Reject lengths above 256 MiB in both paths before allocating, and parse
the non-pipelined length as unsigned before the check.
Comment thread expander.c Outdated
char *eon, *c;
long togot = strtoul(response + 1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
unsigned long len = strtoul(response + 1, &eon, 10);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Probably good to also check errno?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call, added in be82f3e for both paths: errno is cleared before strtoul and a range error is rejected along with the cap. make check still passes and both reply shapes still exit with the error.

Treat a strtoul range error as an invalid length in both paths.
@job
job merged commit 37a3adb into bgp:main Nov 19, 2026
5 checks passed
@DicksonWu654

Copy link
Copy Markdown
Contributor Author

Thanks for merging, and for the errno catch!

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