From f2da1c6e4f68a355faed537491f7e791e38c9f9e Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 7 Aug 2014 23:00:01 +0200 Subject: [PATCH] Add a way to limit deserialized string lengths and use it for most strings being serialized. Rebased-From: 216e9a4 --- src/alert.h | 6 +++--- src/main.cpp | 9 +++------ src/serialize.h | 39 +++++++++++++++++++++++++++++++++++++-- src/wallet.h | 6 +++--- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/alert.h b/src/alert.h index da140be5e..296d48891 100644 --- a/src/alert.h +++ b/src/alert.h @@ -60,9 +60,9 @@ public: READWRITE(setSubVer); READWRITE(nPriority); - READWRITE(strComment); - READWRITE(strStatusBar); - READWRITE(strReserved); + READWRITE(LIMITED_STRING(strComment, 65536)); + READWRITE(LIMITED_STRING(strStatusBar, 256)); + READWRITE(LIMITED_STRING(strReserved, 256)); ) void SetNull(); diff --git a/src/main.cpp b/src/main.cpp index f6e709365..62366a12f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3755,7 +3755,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) if (!vRecv.empty()) vRecv >> addrFrom >> nNonce; if (!vRecv.empty()) { - vRecv >> pfrom->strSubVer; + vRecv >> LIMITED_STRING(pfrom->strSubVer, 256); pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer); } if (!vRecv.empty()) @@ -4361,7 +4361,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) if (fDebug) { string strMsg; unsigned char ccode; string strReason; - vRecv >> strMsg >> ccode >> strReason; + vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, 111); ostringstream ss; ss << strMsg << " code " << itostr(ccode) << ": " << strReason; @@ -4372,10 +4372,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) vRecv >> hash; ss << ": hash " << hash.ToString(); } - // Truncate to reasonable length and sanitize before printing: - string s = ss.str(); - if (s.size() > 111) s.erase(111, string::npos); - LogPrint("net", "Reject %s\n", SanitizeString(s)); + LogPrint("net", "Reject %s\n", SanitizeString(ss.str())); } } diff --git a/src/serialize.h b/src/serialize.h index 134174659..a157048d5 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -306,8 +306,9 @@ I ReadVarInt(Stream& is) } } -#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))) -#define VARINT(obj) REF(WrapVarInt(REF(obj))) +#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))) +#define VARINT(obj) REF(WrapVarInt(REF(obj))) +#define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj))) /** Wrapper for serializing arrays and POD. */ @@ -364,6 +365,40 @@ public: } }; +template +class LimitedString +{ +protected: + std::string& string; +public: + LimitedString(std::string& string) : string(string) {} + + template + void Unserialize(Stream& s, int, int=0) + { + size_t size = ReadCompactSize(s); + if (size > Limit) { + throw std::ios_base::failure("String length limit exceeded"); + } + string.resize(size); + if (size != 0) + s.read((char*)&string[0], size); + } + + template + void Serialize(Stream& s, int, int=0) const + { + WriteCompactSize(s, string.size()); + if (!string.empty()) + s.write((char*)&string[0], string.size()); + } + + unsigned int GetSerializeSize(int, int=0) const + { + return GetSizeOfCompactSize(string.size()) + string.size(); + } +}; + template CVarInt WrapVarInt(I& n) { return CVarInt(n); } diff --git a/src/wallet.h b/src/wallet.h index 3f2b9fbf1..77bbb9ac6 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -661,7 +661,7 @@ public: READWRITE(vchPrivKey); READWRITE(nTimeCreated); READWRITE(nTimeExpires); - READWRITE(strComment); + READWRITE(LIMITED_STRING(strComment, 65536)); ) }; @@ -736,7 +736,7 @@ public: // Note: strAccount is serialized as part of the key, not here. READWRITE(nCreditDebit); READWRITE(nTime); - READWRITE(strOtherAccount); + READWRITE(LIMITED_STRING(strOtherAccount, 65536)); if (!fRead) { @@ -752,7 +752,7 @@ public: } } - READWRITE(strComment); + READWRITE(LIMITED_STRING(strComment, 65536)); size_t nSepPos = strComment.find("\0", 0, 1); if (fRead)