Skip to content

Commit b1a4f68

Browse files
committed
Fixed critical bug in Depot packing function
There was the bug that cause Heap corruption because of wrong DepotSize calculation (encrypted file size mismatch with original file)
1 parent b23dbd4 commit b1a4f68

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

Manager/UpdateManager/UpdateManager.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "UpdateManager.h"
2-
32
#include "Utils/httplib/httplib.h"
43

54
std::map<Host*, std::future<void>> fGetAccessGroups;
@@ -199,7 +198,7 @@ UpdateManager::Host::AddAppResponse UpdateManager::Host::AddApp(string name, str
199198
else
200199
res = cli.Get("/pipeline/v2/update/app/add/" + name + "/" + accessGroupValue, headers);
201200
reader.parse(res->body, root);
202-
if (!root.isMember("status")) {
201+
if (root.isMember("status")) {
203202
string status = root["status"].asString();
204203
if (status == "has_deleted") {
205204
return Host::AddAppResponse::HasDeleted;
@@ -539,6 +538,7 @@ void UpdateManager::Build::RemoveDepot(string name, bool onServer)
539538

540539
void UpdateManager::BuildDepot::UploadDepot()
541540
{
541+
542542
httplib::Client cli("https://" + this->Build->App->Host->Uri);
543543
string auth = this->Build->App->Host->Login + ":" + this->Build->App->Host->Password;
544544
httplib::Headers headers = {
@@ -553,11 +553,16 @@ void UpdateManager::BuildDepot::UploadDepot()
553553
else {
554554
Log("Depot " + dye::light_blue(this->Name) + " wasn't upload, error: " + dye::red(to_string(res.error())));
555555
}
556+
556557
}
557558

558559
void UpdateManager::BuildDepot::DownloadDepot(std::function<bool(uint64_t current, uint64_t total)> callback)
559560
{
560561
Log("Downloading file " + this->Name);
562+
if (this->Url == "") {
563+
this->Downloaded = true;
564+
return;
565+
}
561566

562567
httplib::Client cli("https://" + this->Build->App->Host->Uri);
563568
httplib::Result res;
@@ -888,8 +893,12 @@ bool UpdateManager::BuildDepot::PackDepot()
888893
loadedFiles.insert(loadedFiles.begin(), make_pair(jsonF, json.size()));
889894
fileShas.insert(fileShas.begin(), headerSha);
890895

896+
allSize = 0;
897+
891898
for (int i = 0; i < loadedFiles.size(); i++) {
899+
allSize += 4;
892900
string encrypted = EncryptAES(string(loadedFiles[i].first, loadedFiles[i].second), this->Key.Value, GetIV(fileShas[i], this->Key.Name));
901+
allSize += encrypted.size();
893902
free(loadedFiles[i].first);
894903
loadedFiles[i].second = encrypted.size();
895904
loadedFiles[i].first = (char*)malloc(loadedFiles[i].second);
@@ -899,6 +908,7 @@ bool UpdateManager::BuildDepot::PackDepot()
899908
}
900909
}
901910

911+
902912
string jsonString = mainJson.toStyledString();
903913
free(this->Depot);
904914
this->DepotSize = 8 + jsonString.size() + allSize;
@@ -925,7 +935,8 @@ bool UpdateManager::BuildDepot::PackDepot()
925935
offset += sizeof(unsigned int);
926936
memcpy(this->Depot + offset, loadedFiles[i].first, loadedFiles[i].second);
927937
offset += loadedFiles[i].second;
928-
//free(loadedFiles[i].first);
938+
free(loadedFiles[i].first);
939+
loadedFiles[i].first = nullptr;
929940
}
930941

931942
WriteToFile(this->FullPath, this->Depot, this->DepotSize);

Manager/UpdateManager/Utils/Encryption.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "openssl/engine.h"
66
#include "Base64.hpp"
77

8-
98
static string GetIV(string sha, string keyName) {
109
unsigned char out_key[32];
1110
memset(out_key, 0, sizeof(out_key));
@@ -20,32 +19,34 @@ static string DecryptAES(string cipherText, string keyValue, string IV) {
2019
// GetIV
2120
// E8 ? ? ? ? 48 8B 76 18
2221
int actual_size = 0, final_size = 0;
23-
EVP_CIPHER_CTX* d_ctx = EVP_CIPHER_CTX_new();
22+
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
2423
string keyIn = base64::from_base64(keyValue);
2524

2625
unsigned char* out = (unsigned char*)malloc(cipherText.size());
2726
memset(out, 0, cipherText.size());
28-
EVP_DecryptInit(d_ctx, EVP_aes_128_gcm(), 0, 0);
29-
EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_IVLEN, IV.size(), 0);
30-
EVP_DecryptInit(d_ctx, 0, (const unsigned char*)keyIn.c_str(), (const unsigned char*)IV.c_str());
31-
EVP_DecryptUpdate(d_ctx, out, &actual_size, (const unsigned char*)cipherText.c_str(), cipherText.size()); // E8 ? ? ? ? 0F B6 4E 06
32-
EVP_DecryptFinal(d_ctx, out, &final_size);
27+
EVP_DecryptInit(ctx, EVP_aes_128_gcm(), 0, 0);
28+
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, IV.size(), 0);
29+
EVP_DecryptInit(ctx, 0, (const unsigned char*)keyIn.c_str(), (const unsigned char*)IV.c_str());
30+
EVP_DecryptUpdate(ctx, out, &actual_size, (const unsigned char*)cipherText.c_str(), cipherText.size()); // E8 ? ? ? ? 0F B6 4E 06
31+
EVP_DecryptFinal(ctx, out, &final_size);
32+
EVP_CIPHER_CTX_free(ctx);
3333
return string((char*)out, actual_size);
3434
}
3535

3636
static string EncryptAES(string text, string keyValue, string IV) {
3737
int actual_size = 0, final_size = 0;
38-
EVP_CIPHER_CTX* d_ctx = EVP_CIPHER_CTX_new();
38+
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
3939
string keyIn = base64::from_base64(keyValue);
4040

4141
unsigned char* out = (unsigned char*)malloc(text.size());
4242
memset(out, 0, text.size());
4343

44-
EVP_EncryptInit(d_ctx, EVP_aes_128_gcm(), 0, 0);
45-
EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_IVLEN, IV.size(), 0);
44+
EVP_EncryptInit(ctx, EVP_aes_128_gcm(), 0, 0);
45+
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, IV.size(), 0);
4646

47-
EVP_EncryptInit(d_ctx, 0, (const unsigned char*)keyIn.c_str(), (const unsigned char*)IV.c_str());
48-
EVP_EncryptUpdate(d_ctx, out, &actual_size, (const unsigned char*)text.c_str(), text.size()); // E8 ? ? ? ? 0F B6 4E 06
49-
EVP_EncryptFinal(d_ctx, out, &final_size);
47+
EVP_EncryptInit(ctx, 0, (const unsigned char*)keyIn.c_str(), (const unsigned char*)IV.c_str());
48+
EVP_EncryptUpdate(ctx, out, &actual_size, (const unsigned char*)text.c_str(), text.size()); // E8 ? ? ? ? 0F B6 4E 06
49+
EVP_EncryptFinal(ctx, out, &final_size);
50+
EVP_CIPHER_CTX_free(ctx);
5051
return string((char*)out, actual_size);
5152
}

0 commit comments

Comments
 (0)