Skip to content

Commit

Permalink
Add str len check in config_sortlist to avoid stack overflow (#497)
Browse files Browse the repository at this point in the history
In ares_set_sortlist, it calls config_sortlist(..., sortstr) to parse
the input str and initialize a sortlist configuration.

However, ares_set_sortlist has not any checks about the validity of the input str.
It is very easy to create an arbitrary length stack overflow with the unchecked
`memcpy(ipbuf, str, q-str);` and `memcpy(ipbufpfx, str, q-str);`
statements in the config_sortlist call, which could potentially cause severe
security impact in practical programs.

This commit add necessary check for `ipbuf` and `ipbufpfx` which avoid the
potential stack overflows.

fixes #496

Fix By: @hopper-vul
  • Loading branch information
hopper-vul committed Jan 18, 2023
1 parent 1cb0c5d commit 9903253
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/ares_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
q = str;
while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
q++;
if (q-str >= 16)
return ARES_EBADSTR;
memcpy(ipbuf, str, q-str);
ipbuf[q-str] = '\0';
/* Find the prefix */
Expand All @@ -1921,6 +1923,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
const char *str2 = q+1;
while (*q && *q != ';' && !ISSPACE(*q))
q++;
if (q-str >= 32)
return ARES_EBADSTR;
memcpy(ipbufpfx, str, q-str);
ipbufpfx[q-str] = '\0';
str = str2;
Expand Down
2 changes: 2 additions & 0 deletions test/ares-test-init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ TEST_F(DefaultChannelTest, SetAddresses) {

TEST_F(DefaultChannelTest, SetSortlistFailures) {
EXPECT_EQ(ARES_ENODATA, ares_set_sortlist(nullptr, "1.2.3.4"));
EXPECT_EQ(ARES_EBADSTR, ares_set_sortlist(channel_, "111.111.111.111*/16"));
EXPECT_EQ(ARES_EBADSTR, ares_set_sortlist(channel_, "111.111.111.111/255.255.255.240*"));
EXPECT_EQ(ARES_SUCCESS, ares_set_sortlist(channel_, "xyzzy ; lwk"));
EXPECT_EQ(ARES_SUCCESS, ares_set_sortlist(channel_, "xyzzy ; 0x123"));
}
Expand Down

0 comments on commit 9903253

Please sign in to comment.