Skip to content

Commit

Permalink
Check for out-of-bounds bencoded lengths before advancing buffer pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
xercesblue committed Jun 29, 2015
1 parent bbc0b71 commit e809ea8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lazy_bdecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ namespace libtorrent
if (e)
TORRENT_FAIL_BDECODE(e);

if (start + len + 1 > end)
// remaining buffer size excluding ':'
const ptrdiff_t buff_size = end - start - 1;
if (len > buff_size)
TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);

if (len < 0)
Expand Down Expand Up @@ -216,12 +218,16 @@ namespace libtorrent
start = parse_int(start, end, ':', len, e);
if (e)
TORRENT_FAIL_BDECODE(e);
if (start + len + 1 > end)

// remaining buffer size excluding ':'
const ptrdiff_t buff_size = end - start - 1;
if (len > buff_size)
TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
if (len < 0)
TORRENT_FAIL_BDECODE(bdecode_errors::overflow);

++start;
if (start == end) TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
top->construct_string(start, int(len));
stack.pop_back();
start += len;
Expand Down

0 comments on commit e809ea8

Please sign in to comment.