Skip to content

Commit

Permalink
Fix out-of-bounds read in torrent parsing
Browse files Browse the repository at this point in the history
Fixes #3591
  • Loading branch information
guidovranken committed Aug 6, 2022
1 parent 1f5c650 commit 58821db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 12 additions & 4 deletions libtransmission/torrent-metainfo.cc
Expand Up @@ -455,10 +455,18 @@ struct MetainfoHandler final : public transmission::benc::BasicHandler<MaxBencDe
}
else if (pathIs(InfoKey, PiecesKey))
{
auto const n = std::size(value) / sizeof(tr_sha1_digest_t);
tm_.pieces_.resize(n);
std::copy_n(std::data(value), std::size(value), reinterpret_cast<char*>(std::data(tm_.pieces_)));
tm_.pieces_offset_ = context.tokenSpan().first;
if (std::size(value) % sizeof(tr_sha1_digest_t) == 0)
{
auto const n = std::size(value) / sizeof(tr_sha1_digest_t);
tm_.pieces_.resize(n);
std::copy_n(std::data(value), std::size(value), reinterpret_cast<char*>(std::data(tm_.pieces_)));
tm_.pieces_offset_ = context.tokenSpan().first;
}
else
{
tr_error_set(context.error, EINVAL, fmt::format("invalid piece size: {}", std::size(value)));
unhandled = true;
}
}
else if (pathStartsWith(PieceLayersKey))
{
Expand Down
7 changes: 7 additions & 0 deletions tests/libtransmission/torrent-metainfo-test.cc
Expand Up @@ -255,5 +255,12 @@ TEST_F(TorrentMetainfoTest, GetRightStyleWebseedString)
EXPECT_EQ("http://www.webseed-one.com/"sv, tm.webseed(0));
}

// Test for https://github.com/transmission/transmission/issues/3591
TEST_F(TorrentMetainfoTest, parseBencOOBWrite)
{
auto tm = tr_torrent_metainfo{};
EXPECT_FALSE(tm.parseBenc(tr_base64_decode("ZGg0OmluZm9kNjpwaWVjZXMzOkFpzQ==")));
}

} // namespace test
} // namespace libtransmission

0 comments on commit 58821db

Please sign in to comment.