Skip to content

Commit

Permalink
Merge pull request zcash#4655
Browse files Browse the repository at this point in the history
216e9a4 Add a way to limit deserialized string lengths (Pieter Wuille)
  • Loading branch information
laanwj committed Aug 18, 2014
2 parents e2e73e5 + 216e9a4 commit 21e7a56
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class CUnsignedAlert
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();
Expand Down
9 changes: 3 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3568,7 +3568,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())
Expand Down Expand Up @@ -4192,7 +4192,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;
Expand All @@ -4203,10 +4203,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()));
}
}

Expand Down
39 changes: 37 additions & 2 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,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.
*/
Expand Down Expand Up @@ -398,6 +399,40 @@ class CVarInt
}
};

template<size_t Limit>
class LimitedString
{
protected:
std::string& string;
public:
LimitedString(std::string& string) : string(string) {}

template<typename Stream>
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<typename Stream>
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<typename I>
CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }

Expand Down
6 changes: 3 additions & 3 deletions src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ class CWalletKey
READWRITE(vchPrivKey);
READWRITE(nTimeCreated);
READWRITE(nTimeExpires);
READWRITE(strComment);
READWRITE(LIMITED_STRING(strComment, 65536));
)
};

Expand Down Expand Up @@ -933,7 +933,7 @@ class CAccountingEntry
// Note: strAccount is serialized as part of the key, not here.
READWRITE(nCreditDebit);
READWRITE(nTime);
READWRITE(strOtherAccount);
READWRITE(LIMITED_STRING(strOtherAccount, 65536));

if (!fRead)
{
Expand All @@ -949,7 +949,7 @@ class CAccountingEntry
}
}

READWRITE(strComment);
READWRITE(LIMITED_STRING(strComment, 65536));

size_t nSepPos = strComment.find("\0", 0, 1);
if (fRead)
Expand Down

0 comments on commit 21e7a56

Please sign in to comment.