I think it would be better to do the check just before doing the offensive operation. Consider:
void f()
{
if (x >= a.size()) {
return or throw (dont continue below)
}
a[x] = 0;
vs
void f()
{
a[x] = 0; // it is not immediately obvious if x has been checked
...
// somewhere else in the code
if (x >= a.size()) {
// make sure f() is not called
}
// but new code or a refactor could end up calling f() nevertheless
The following is shorter and I find it safer and it also avoids using magic numbers (+ unit test):
diff --git i/src/i2p.cpp w/src/i2p.cpp
index 05a5dde396..685b43ba18 100644
--- i/src/i2p.cpp
+++ w/src/i2p.cpp
@@ -381,17 +381,32 @@ Binary Session::MyDestination() const
// "They are 387 bytes plus the certificate length specified at bytes 385-386, which may be
// non-zero"
static constexpr size_t DEST_LEN_BASE = 387;
static constexpr size_t CERT_LEN_POS = 385;
uint16_t cert_len;
+
+ if (m_private_key.size() < CERT_LEN_POS + sizeof(cert_len)) {
+ throw std::runtime_error(strprintf("The private key is too short (%d < %d)",
+ m_private_key.size(),
+ CERT_LEN_POS + sizeof(cert_len)));
+ }
+
memcpy(&cert_len, &m_private_key.at(CERT_LEN_POS), sizeof(cert_len));
cert_len = be16toh(cert_len);
const size_t dest_len = DEST_LEN_BASE + cert_len;
+ if (dest_len > m_private_key.size()) {
+ throw std::runtime_error(strprintf("Certificate length (%d) designates that the private key should "
+ "be %d bytes, but it is only %d bytes",
+ cert_len,
+ dest_len,
+ m_private_key.size()));
+ }
+
return Binary{m_private_key.begin(), m_private_key.begin() + dest_len};
}
void Session::CreateIfNotCreatedAlready()
{
std::string errmsg;
diff --git i/src/test/i2p_tests.cpp w/src/test/i2p_tests.cpp
index 5b8b0e9215..d6bbe4fa8f 100644
--- i/src/test/i2p_tests.cpp
+++ w/src/test/i2p_tests.cpp
@@ -6,12 +6,13 @@
#include <i2p.h>
#include <logging.h>
#include <netaddress.h>
#include <test/util/logging.h>
#include <test/util/net.h>
#include <test/util/setup_common.h>
+#include <util/readwritefile.h>
#include <util/threadinterrupt.h>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <string>
@@ -122,7 +123,51 @@ BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
ASSERT_DEBUG_LOG("Destroying SAM session");
BOOST_REQUIRE(session.Listen(conn));
BOOST_REQUIRE(!session.Accept(conn));
}
}
+BOOST_AUTO_TEST_CASE(damaged_private_key)
+{
+ const auto CreateSockOrig = CreateSock;
+
+ CreateSock = [](const CService&) {
+ return std::make_unique<StaticContentsSock>("HELLO REPLY RESULT=OK VERSION=3.1\n"
+ "SESSION STATUS RESULT=OK DESTINATION=\n");
+ };
+
+ const auto i2p_private_key_file = gArgs.GetDataDirNet() / "test_i2p_private_key_damaged";
+
+ for (const auto& [file_contents, expected_error] : std::vector<std::tuple<std::string, std::string>>{
+ {"", "The private key is too short (0 < 387)"},
+
+ {"abcd", "The private key is too short (4 < 387)"},
+
+ {std::string(386, '\0'), "The private key is too short (386 < 387)"},
+
+ {std::string(385, '\0') + '\0' + '\1',
+ "Certificate length (1) designates that the private key should be 388 bytes, but it is only "
+ "387 bytes"},
+
+ {std::string(385, '\0') + '\0' + '\5' + "abcd",
+ "Certificate length (5) designates that the private key should be 392 bytes, but it is only "
+ "391 bytes"}}) {
+
+ BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents));
+
+ CThreadInterrupt interrupt;
+ i2p::sam::Session session(i2p_private_key_file, CService{}, &interrupt);
+
+ {
+ ASSERT_DEBUG_LOG("Creating persistent SAM session");
+ ASSERT_DEBUG_LOG(expected_error);
+
+ i2p::Connection conn;
+ bool proxy_error;
+ BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error));
+ }
+ }
+
+ CreateSock = CreateSockOrig;
+}
+
BOOST_AUTO_TEST_SUITE_END()