Skip to content
Closed
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
Prev Previous commit
Next Next commit
src: use correct OOB check for IPv6 parsing
`last_piece` pointed to the end of the 8×16 bit array,
so `piece_pointer == last_piece` already means that the pointer
is not writable any longer.

Previously, this still worked most of the time but could
result in an out-of-bounds-write.

Also, rename `last_piece` to `buffer_end` to avoid this pitfall.

PR-URL: #17470
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
addaleax committed Jan 23, 2018
commit b70a4b49a11374d8d4198939ccf22a585ba1d2b8
10 changes: 5 additions & 5 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
for (unsigned n = 0; n < 8; n++)
value_.ipv6[n] = 0;
uint16_t* piece_pointer = &value_.ipv6[0];
uint16_t* last_piece = piece_pointer + 8;
uint16_t* const buffer_end = piece_pointer + 8;
uint16_t* compress_pointer = nullptr;
const char* pointer = input;
const char* end = pointer + length;
Expand All @@ -665,7 +665,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
compress_pointer = piece_pointer;
}
while (ch != kEOL) {
if (piece_pointer > last_piece)
if (piece_pointer >= buffer_end)
return;
if (ch == ':') {
if (compress_pointer != nullptr)
Expand All @@ -690,7 +690,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
return;
pointer -= len;
ch = pointer < end ? pointer[0] : kEOL;
if (piece_pointer > last_piece - 2)
if (piece_pointer > buffer_end - 2)
return;
numbers_seen = 0;
while (ch != kEOL) {
Expand Down Expand Up @@ -744,7 +744,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {

if (compress_pointer != nullptr) {
swaps = piece_pointer - compress_pointer;
piece_pointer = last_piece - 1;
piece_pointer = buffer_end - 1;
while (piece_pointer != &value_.ipv6[0] && swaps > 0) {
uint16_t temp = *piece_pointer;
uint16_t* swap_piece = compress_pointer + swaps - 1;
Expand All @@ -754,7 +754,7 @@ void URLHost::ParseIPv6Host(const char* input, size_t length) {
swaps--;
}
} else if (compress_pointer == nullptr &&
piece_pointer != last_piece) {
piece_pointer != buffer_end) {
return;
}
type_ = HostType::H_IPV6;
Expand Down