Skip to content
Merged
Changes from all commits
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
38 changes: 32 additions & 6 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 @@ -737,10 +744,19 @@ bgpq_read(struct bgpq_expander *b)
if (response[0] == 'A') {
char *eon, *c;
unsigned long offset = 0;
unsigned long togot = strtoul(response + 1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
unsigned long togot;
char *recvbuffer;

errno = 0;
togot = strtoul(response + 1, &eon, 10);
if (errno != 0 || togot > BGPQ_MAX_REPLY_LEN) {
sx_report(SX_ERROR, "A-code length %lu invalid "
"or too large (max %lu)\n", togot,
BGPQ_MAX_REPLY_LEN);
exit(1);
}

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

memset(recvbuffer, 0, togot + 2);
Expand Down Expand Up @@ -932,11 +948,21 @@ 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;
long togot;
char *recvbuffer;
int offset = 0;

if (!recvbuffer) {
errno = 0;
len = strtoul(response + 1, &eon, 10);
if (errno != 0 || len > BGPQ_MAX_REPLY_LEN) {
sx_report(SX_ERROR, "A-code length %lu invalid or 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