Skip to content

Commit

Permalink
refactor: Protect array unpacking against invalid lengths.
Browse files Browse the repository at this point in the history
Each array element is at least 1 byte, so if there are fewer bytes than
array elements, the array size is invalid.
  • Loading branch information
iphydf committed Apr 5, 2022
1 parent c4beda4 commit 2fb25b9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146fb36bf3100115f913a07583c096c8dc98ab26e1220567e465b2ca86a69583 /usr/local/bin/tox-bootstrapd
95ae45707c9a19ea9c8c0a537c5defb228f8d7eca1c51c0225a3bc07a50891c6 /usr/local/bin/tox-bootstrapd
9 changes: 9 additions & 0 deletions toxcore/bin_pack_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ TEST(BinPack, BinCanHoldArbitraryData)
EXPECT_EQ(str, (std::array<uint8_t, 5>{'h', 'e', 'l', 'l', 'o'}));
}

TEST(BinPack, OversizedArrayFailsUnpack)
{
std::array<uint8_t, 1> buf = {0x91};

Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
uint32_t size;
EXPECT_FALSE(bin_unpack_array(bu.get(), &size));
}

} // namespace
2 changes: 1 addition & 1 deletion toxcore/bin_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void bin_unpack_free(Bin_Unpack *bu)

bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size)
{
return cmp_read_array(&bu->ctx, size);
return cmp_read_array(&bu->ctx, size) && *size <= bu->bytes_size;
}

bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size)
Expand Down

0 comments on commit 2fb25b9

Please sign in to comment.