Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipv6_addr: provide fix for off-by-x error #6961

Merged
merged 2 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions sys/net/network_layer/ipv6/addr/ipv6_addr_from_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ipv6_addr_t *ipv6_addr_from_str(ipv6_addr_t *result, const char *addr)
continue;
}

if (i > sizeof(ipv6_addr_t)) {
if ((i + sizeof(uint16_t)) > sizeof(ipv6_addr_t)) {
return NULL;
}

Expand All @@ -108,7 +108,7 @@ ipv6_addr_t *ipv6_addr_from_str(ipv6_addr_t *result, const char *addr)
}

#ifdef MODULE_IPV4_ADDR
if (ch == '.' && (i <= sizeof(ipv6_addr_t)) &&
if (ch == '.' && ((i + sizeof(ipv4_addr_t)) <= sizeof(ipv6_addr_t)) &&
ipv4_addr_from_str((ipv4_addr_t *)(&(result->u8[i])),
curtok) != NULL) {
i += sizeof(ipv4_addr_t);
Expand Down
16 changes: 16 additions & 0 deletions tests/unittests/tests-ipv6_addr/tests-ipv6_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,21 @@ static void test_ipv6_addr_from_str__one_colon_start(void)
TEST_ASSERT_NULL(ipv6_addr_from_str(&result, ":ff::1"));
}

#define CANARY_DATA 0xc2, 0x8c, 0x36, 0x26, 0x24, 0x16, 0xd1, 0xd6

static void test_ipv6_addr_from_str__overflow(void)
{
uint8_t result[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
CANARY_DATA };
static const uint8_t canary_exp[] = { CANARY_DATA };
ipv6_addr_t *addr = (ipv6_addr_t *)&result[0];
uint8_t *canary = &result[sizeof(ipv6_addr_t)];

TEST_ASSERT_NULL(ipv6_addr_from_str(addr, "::A:A:A:A:A:A:A:A:A:A"));
TEST_ASSERT_EQUAL_INT(0, memcmp(canary, canary_exp, sizeof(canary_exp)));
}

static void test_ipv6_addr_from_str__three_colons(void)
{
ipv6_addr_t result;
Expand Down Expand Up @@ -1086,6 +1101,7 @@ Test *tests_ipv6_addr_tests(void)
new_TestFixture(test_ipv6_addr_to_str__success4),
new_TestFixture(test_ipv6_addr_to_str__success5),
new_TestFixture(test_ipv6_addr_from_str__one_colon_start),
new_TestFixture(test_ipv6_addr_from_str__overflow),
new_TestFixture(test_ipv6_addr_from_str__three_colons),
new_TestFixture(test_ipv6_addr_from_str__string_too_long),
new_TestFixture(test_ipv6_addr_from_str__illegal_chars),
Expand Down