Skip to content

Commit bfe6764

Browse files
committed
inspector: fix out-of-bounds read in ws frame decoding
Signed-off-by: nashit hayyat <nashit@bugqore.com>
1 parent fe3b3b8 commit bfe6764

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎src/inspector_socket.cc‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ static ws_decode_result decode_frame_hybi17(const std::vector<char>& buffer,
360360
}
361361
size_t payload_length = static_cast<size_t>(payload_length64);
362362

363-
if (buffer.size() - kMaskingKeyWidthInBytes < payload_length)
363+
// The masking key and the payload follow the header `it` already walked
364+
// past, so they have to fit in what is left rather than in the whole buffer.
365+
size_t remaining = static_cast<size_t>(buffer.end() - it);
366+
if (remaining < kMaskingKeyWidthInBytes ||
367+
remaining - kMaskingKeyWidthInBytes < payload_length)
364368
return FRAME_INCOMPLETE;
365369

366370
std::vector<char>::const_iterator masking_key = it;

‎test/cctest/test_inspector_socket.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,24 @@ TEST_F(InspectorSocketTest, ReadsAndWritesInspectorMessage) {
409409
reinterpret_cast<uv_handle_t*>(&client_socket)));
410410
}
411411

412+
TEST_F(InspectorSocketTest, WaitsForFrameBodyToArrive) {
413+
do_write(const_cast<char*>(HANDSHAKE_REQ), sizeof(HANDSHAKE_REQ) - 1);
414+
expect_handshake();
415+
416+
// A header announcing a masked five byte payload, with the masking key and
417+
// the payload still on the wire. The frame has to be treated as incomplete
418+
// until the remaining bytes show up.
419+
const char FRAME_HEADER[] = {'\x81', '\x85'};
420+
do_write(FRAME_HEADER, sizeof(FRAME_HEADER));
421+
422+
const char FRAME_BODY[] = {'\x01', '\x02', '\x03', '\x04',
423+
'\x69', '\x67', '\x6F', '\x68', '\x6E'};
424+
do_write(FRAME_BODY, sizeof(FRAME_BODY));
425+
426+
const char CLIENT_MESSAGE[] = "hello";
427+
delegate->ExpectData(CLIENT_MESSAGE, sizeof(CLIENT_MESSAGE) - 1);
428+
}
429+
412430
TEST_F(InspectorSocketTest, BufferEdgeCases) {
413431
do_write(const_cast<char*>(HANDSHAKE_REQ), sizeof(HANDSHAKE_REQ) - 1);
414432
expect_handshake();

0 commit comments

Comments
 (0)