Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions expander.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ int debug_expander = 0;
int pipelining = 1;
int expand_special_asn = 0;

/*
* Upper bound for the length announced in an IRRd "A<len>" reply. It keeps
* malloc(togot + 2) from wrapping and the int offset below from overflowing,
* while leaving plenty of room for large AS-SET expansions.
*/
#define BGPQ_MAX_REPLY_LEN (256UL * 1024UL * 1024UL)

static inline int
tentry_cmp(struct sx_tentry *a, struct sx_tentry *b)
{
Expand Down Expand Up @@ -738,9 +745,16 @@ bgpq_read(struct bgpq_expander *b)
char *eon, *c;
unsigned long offset = 0;
unsigned long togot = strtoul(response + 1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
char *recvbuffer;

if (recvbuffer == NULL)
if (togot > BGPQ_MAX_REPLY_LEN) {
sx_report(SX_ERROR, "A-code length %lu too "
"large (max %lu)\n", togot,
BGPQ_MAX_REPLY_LEN);
exit(1);
}

if ((recvbuffer = malloc(togot + 2)) == NULL)
err(1, NULL);

memset(recvbuffer, 0, togot + 2);
Expand Down Expand Up @@ -932,11 +946,19 @@ bgpq_expand_irrd(struct bgpq_expander *b,

if (response[0] == 'A') {
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.

long togot;
char *recvbuffer;
int offset = 0;

if (!recvbuffer) {
if (len > BGPQ_MAX_REPLY_LEN) {
sx_report(SX_ERROR, "A-code length %lu too large "
"(max %lu)\n", len, BGPQ_MAX_REPLY_LEN);
exit(1);
}
togot = (long)len;

if (!(recvbuffer = malloc(togot + 2))) {
sx_report(SX_FATAL, "Error allocating %lu bytes: %s\n",
togot + 2, strerror(errno));
}
Expand Down
Loading