Skip to content

Commit ec5116a

Browse files
committed
Merge bitcoin#28695: net: Sanity check private keys received from SAM proxy
5cf4d26 [test] Test i2p private key constraints (Vasil Dimov) cf70a8d [net] Check i2p private key constraints (dergoegge) Pull request description: Not sanity checking can lead to crashes or worse: ``` ==1715589==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000055c2 at pc 0x5622ed66e7ad bp 0x7ffee547a2c0 sp 0x7ffee547a2b8 READ of size 2 at 0x6140000055c2 thread T0 (b-test) #0 0x5622ed66e7ac in memcpy include/bits/string_fortified.h:29:10 #1 0x5622ed66e7ac in i2p::sam::Session::MyDestination() const src/i2p.cpp:362:5 bitcoin#2 0x5622ed662e46 in i2p::sam::Session::CreateIfNotCreatedAlready() src/i2p.cpp:414:40 bitcoin#3 0x5622ed6619f2 in i2p::sam::Session::Listen(i2p::Connection&) src/i2p.cpp:143:9 ``` ACKs for top commit: maflcko: code lgtm ACK 5cf4d26 stickies-v: re-ACK 5cf4d26 vasild: ACK 5cf4d26 Tree-SHA512: 3de3bd396538fa619de67957b9c8a58011ab911f0f51097c387e730c13908278b7322aa3357051fb245a20b15bef34b0e9fadcb1eff8ad751139d2aa634c78ad
2 parents a3670b2 + 5cf4d26 commit ec5116a

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/i2p.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,26 @@ Binary Session::MyDestination() const
384384
static constexpr size_t CERT_LEN_POS = 385;
385385

386386
uint16_t cert_len;
387+
388+
if (m_private_key.size() < CERT_LEN_POS + sizeof(cert_len)) {
389+
throw std::runtime_error(strprintf("The private key is too short (%d < %d)",
390+
m_private_key.size(),
391+
CERT_LEN_POS + sizeof(cert_len)));
392+
}
393+
387394
memcpy(&cert_len, &m_private_key.at(CERT_LEN_POS), sizeof(cert_len));
388395
cert_len = be16toh(cert_len);
389396

390397
const size_t dest_len = DEST_LEN_BASE + cert_len;
391398

399+
if (dest_len > m_private_key.size()) {
400+
throw std::runtime_error(strprintf("Certificate length (%d) designates that the private key should "
401+
"be %d bytes, but it is only %d bytes",
402+
cert_len,
403+
dest_len,
404+
m_private_key.size()));
405+
}
406+
392407
return Binary{m_private_key.begin(), m_private_key.begin() + dest_len};
393408
}
394409

src/test/i2p_tests.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <test/util/logging.h>
1010
#include <test/util/net.h>
1111
#include <test/util/setup_common.h>
12+
#include <util/readwritefile.h>
1213
#include <util/threadinterrupt.h>
1314

1415
#include <boost/test/unit_test.hpp>
@@ -125,4 +126,47 @@ BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
125126
}
126127
}
127128

129+
BOOST_AUTO_TEST_CASE(damaged_private_key)
130+
{
131+
const auto CreateSockOrig = CreateSock;
132+
133+
CreateSock = [](const CService&) {
134+
return std::make_unique<StaticContentsSock>("HELLO REPLY RESULT=OK VERSION=3.1\n"
135+
"SESSION STATUS RESULT=OK DESTINATION=\n");
136+
};
137+
138+
const auto i2p_private_key_file = m_args.GetDataDirNet() / "test_i2p_private_key_damaged";
139+
140+
for (const auto& [file_contents, expected_error] : std::vector<std::tuple<std::string, std::string>>{
141+
{"", "The private key is too short (0 < 387)"},
142+
143+
{"abcd", "The private key is too short (4 < 387)"},
144+
145+
{std::string(386, '\0'), "The private key is too short (386 < 387)"},
146+
147+
{std::string(385, '\0') + '\0' + '\1',
148+
"Certificate length (1) designates that the private key should be 388 bytes, but it is only "
149+
"387 bytes"},
150+
151+
{std::string(385, '\0') + '\0' + '\5' + "abcd",
152+
"Certificate length (5) designates that the private key should be 392 bytes, but it is only "
153+
"391 bytes"}}) {
154+
BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents));
155+
156+
CThreadInterrupt interrupt;
157+
i2p::sam::Session session(i2p_private_key_file, CService{}, &interrupt);
158+
159+
{
160+
ASSERT_DEBUG_LOG("Creating persistent SAM session");
161+
ASSERT_DEBUG_LOG(expected_error);
162+
163+
i2p::Connection conn;
164+
bool proxy_error;
165+
BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error));
166+
}
167+
}
168+
169+
CreateSock = CreateSockOrig;
170+
}
171+
128172
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)