Skip to content

Commit a5dca20

Browse files
committed
Try to avert #1 crash from model encryption system-memory abuse by (mostly pt-BR) servers, assuming it's only allocation peaks and that it can be retried later when allocation/mem pressure is lower.
The use of decodeString with huge data payloads (or spammed requests) leads to the crash, that usually presents as KERNELBASE.dll 0x0013b702 in MTA as of writing. Servers should actually batch decode requests instead of abusing memory like this, but they turned to blame us instead.
1 parent 93d5f91 commit a5dca20

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

Shared/sdk/SharedUtil.Crypto.h

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,34 +176,48 @@ namespace SharedUtil
176176
using namespace CryptoPP;
177177
using CryptoPP::byte;
178178

179-
AutoSeededRandomPool rnd;
179+
try
180+
{
181+
AutoSeededRandomPool rnd;
180182

181-
SString result;
182-
SString sIv;
183+
SString result;
184+
SString sIv;
183185

184-
sIv.resize(AES::BLOCKSIZE);
185-
rnd.GenerateBlock((byte*)sIv.data(), sIv.size());
186+
sIv.resize(AES::BLOCKSIZE);
187+
rnd.GenerateBlock((byte*)sIv.data(), sIv.size());
186188

187-
CTR_Mode<AES>::Encryption aesEncryption;
188-
aesEncryption.SetKeyWithIV((byte*)sKey.data(), sKey.size(), (byte*)sIv.data());
189-
StringSource ss(sData, true, new StreamTransformationFilter(aesEncryption, new StringSink(result)));
189+
CTR_Mode<AES>::Encryption aesEncryption;
190+
aesEncryption.SetKeyWithIV((byte*)sKey.data(), sKey.size(), (byte*)sIv.data());
191+
StringSource ss(sData, true, new StreamTransformationFilter(aesEncryption, new StringSink(result)));
190192

191-
return {result, sIv};
193+
return {result, sIv};
194+
}
195+
catch (const std::exception&)
196+
{
197+
return {SString(), SString()};
198+
}
192199
}
193200

194201
inline SString Aes128decode(const SString& sData, const SString& sKey, SString sIv)
195202
{
196203
using namespace CryptoPP;
197204
using CryptoPP::byte;
198205

199-
sIv.resize(AES::BLOCKSIZE);
200-
SString result;
206+
try
207+
{
208+
sIv.resize(AES::BLOCKSIZE);
209+
SString result;
201210

202-
CTR_Mode<AES>::Decryption aesDecryption;
203-
aesDecryption.SetKeyWithIV((byte*)sKey.data(), sKey.size(), (byte*)sIv.data());
204-
StringSource ss(sData, true, new StreamTransformationFilter(aesDecryption, new StringSink(result)));
211+
CTR_Mode<AES>::Decryption aesDecryption;
212+
aesDecryption.SetKeyWithIV((byte*)sKey.data(), sKey.size(), (byte*)sIv.data());
213+
StringSource ss(sData, true, new StreamTransformationFilter(aesDecryption, new StringSink(result)));
205214

206-
return result;
215+
return result;
216+
}
217+
catch (const std::exception&)
218+
{
219+
return SString();
220+
}
207221
}
208222

209223
inline bool StringToZLibFormat(const std::string& format, int& outResult)

0 commit comments

Comments
 (0)