when the function finishes i get this error:
HEAP CORRUPTION DETECTED: after Normal block (#5963) at 0x000001AB99D54B60.
CRT detected that the application wrote to memory after end of heap buffer.
if (data.isEmpty())
return false;
// Generate the password
unsigned char random_byte;
RAND_bytes(&random_byte, 1);
int passwordSize = 9; // +(random_byte % 3) * 3; // Random size of 9, 12, or 15
std::vector<unsigned char> password(passwordSize);
RAND_bytes(password.data(), passwordSize);
// Generate a random salt
RAND_bytes(&random_byte, 1);
int saltSize = 9; // +(random_byte % 3) * 3; // Random size of 9, 12, or 15
std::vector<unsigned char> salt(saltSize);
RAND_bytes(salt.data(), saltSize);
int b64SaltSize = 4 * ((saltSize + 2) / 3);
std::vector<unsigned char> b64Salt(b64SaltSize);
EVP_EncodeBlock(b64Salt.data(), salt.data(), saltSize);
// SHA256 the password and salt without Base64 encoding
unsigned char keyHash[SHA256_DIGEST_LENGTH];
unsigned char ivHash[SHA256_DIGEST_LENGTH];
SHA256(password.data(), passwordSize, keyHash);
SHA256(b64Salt.data(), b64SaltSize, ivHash);
// Derive the AES key and IV by taking the first 16 bytes of the hash
unsigned char hashedKey[16];
unsigned char hashedIv[16];
std::memcpy(hashedKey, keyHash, 16);
std::memcpy(hashedIv, ivHash, 16);
OPENSSL_cleanse(keyHash, sizeof(keyHash));
OPENSSL_cleanse(ivHash, sizeof(ivHash));
std::vector<unsigned char> cipherBuf(data.size() + EVP_MAX_BLOCK_LENGTH);
const EVP_CIPHER* cipherType = EVP_aes_128_cbc();
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx)
return false;
if (EVP_EncryptInit_ex(ctx, cipherType, NULL, hashedKey, hashedIv) != 1)
{
OPENSSL_cleanse(data.data(), data.size());
data.clear();
EVP_CIPHER_CTX_free(ctx);
return false;
}
// Clean up key and IV after use
OPENSSL_cleanse(hashedKey, sizeof(hashedKey));
OPENSSL_cleanse(hashedIv, sizeof(hashedIv));
int outLength1 = 0;
if (EVP_EncryptUpdate(ctx, cipherBuf.data(), &outLength1,
reinterpret_cast<unsigned char*>(data.data()), data.size()) != 1)
{
OPENSSL_cleanse(data.data(), data.size());
data.clear();
EVP_CIPHER_CTX_free(ctx);
return false;
}
int outLength2 = 0;
if (EVP_EncryptFinal_ex(ctx, cipherBuf.data() + outLength1, &outLength2) != 1)
{
OPENSSL_cleanse(data.data(), data.size());
data.clear();
EVP_CIPHER_CTX_free(ctx);
return false;
}
EVP_CIPHER_CTX_free(ctx);
cipherBuf.resize(outLength1 + outLength2);
OPENSSL_cleanse(data.data(), data.size());
data.clear();
// Generate random string to prepend
unsigned char randomByte;
RAND_bytes(&randomByte, 1);
int prependStrSize = 4; // 4 + (randomByte % 7); // Random size between 4 and 10
std::vector<unsigned char> prependStr(prependStrSize);
RAND_bytes(prependStr.data(), prependStrSize);
data.append(prependStr);
data.append(password);
data.append(salt);
data.append(cipherBuf);
VM_S_END // C898
return true;
Assistant
}
The error message you’re encountering:
code
HEAP CORRUPTION DETECTED: after Normal block (#5963) at 0x000001AB99D54B60.
CRT detected that the application wrote to memory after end of heap buffer.
indicates that your application is writing past the end of a heap-allocated buffer. This type of error often results from buffer overflows or improper memory handling.
Identifying the Issue
Upon analyzing your weakEncrypt2 function, the primary culprit appears to be the usage of QByteArray::append with std::vector<unsigned char>. Here's why:
Incorrect append Usage:
The QByteArray::append method does not have an overload that accepts std::vector<unsigned char>. Instead, it expects data types like const char*, QByteArray, or a single char.
When you call data.append(prependStr); where prependStr is a std::vector<unsigned char>, the compiler might implicitly convert the vector to a const char* by taking its address. This can lead to undefined behavior as QByteArray::append will interpret the vector's memory as a C-string, potentially reading beyond its bounds until it hits a null terminator.
Potential Memory Corruption:
If QByteArray::append interprets the vector as a C-string without proper length specification, it may read or write beyond the allocated memory, causing heap corruption.
Solution
To fix the heap corruption, you need to ensure that you correctly append the contents of std::vector<unsigned char> to QByteArray by explicitly specifying the data pointer and size. Here's how you can modify your weakEncrypt2 function:
cpp
inline bool weakEncrypt2(QByteArray& data)
{
VM_S_START // C898
if (data.isEmpty())
return false;
// Generate the password
unsigned char random_byte;
RAND_bytes(&random_byte, 1);
int passwordSize = 9; // +(random_byte % 3) * 3; // Random size of 9, 12, or 15
std::vector<unsigned char> password(passwordSize);
RAND_bytes(password.data(), passwordSize);
// Generate a random salt
RAND_bytes(&random_byte, 1);
int saltSize = 9; // +(random_byte % 3) * 3; // Random size of 9, 12, or 15
std::vector<unsigned char> salt(saltSize);
RAND_bytes(salt.data(), saltSize);
int b64SaltSize = 4 * ((saltSize + 2) / 3);
std::vector<unsigned char> b64Salt(b64SaltSize);
EVP_EncodeBlock(b64Salt.data(), salt.data(), saltSize);
// SHA256 the password and salt without Base64 encoding
unsigned char keyHash[SHA256_DIGEST_LENGTH];
unsigned char ivHash[SHA256_DIGEST_LENGTH];
SHA256(password.data(), passwordSize, keyHash);
SHA256(b64Salt.data(), b64SaltSize, ivHash);
// Derive the AES key and IV by taking the first 16 bytes of the hash
unsigned char hashedKey[16];
unsigned char hashedIv[16];
std::memcpy(hashedKey, keyHash, 16);
std::memcpy(hashedIv, ivHash, 16);
OPENSSL_cleanse(keyHash, sizeof(keyHash));
OPENSSL_cleanse(ivHash, sizeof(ivHash));
std::vector<unsigned char> cipherBuf(data.size() + EVP_MAX_BLOCK_LENGTH);
const EVP_CIPHER* cipherType = EVP_aes_128_cbc();
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx)
return false;
if (EVP_EncryptInit_ex(ctx, cipherType, NULL, hashedKey, hashedIv) != 1)
{
OPENSSL_cleanse(data.data(), data.size());
data.clear();
EVP_CIPHER_CTX_free(ctx);
return false;
}
// Clean up key and IV after use
OPENSSL_cleanse(hashedKey, sizeof(hashedKey));
OPENSSL_cleanse(hashedIv, sizeof(hashedIv));
int outLength1 = 0;
if (EVP_EncryptUpdate(ctx, cipherBuf.data(), &outLength1,
reinterpret_cast<unsigned char*>(data.data()), data.size()) != 1)
{
OPENSSL_cleanse(data.data(), data.size());
data.clear();
EVP_CIPHER_CTX_free(ctx);
return false;
}
int outLength2 = 0;
if (EVP_EncryptFinal_ex(ctx, cipherBuf.data() + outLength1, &outLength2) != 1)
{
OPENSSL_cleanse(data.data(), data.size());
data.clear();
EVP_CIPHER_CTX_free(ctx);
return false;
}
EVP_CIPHER_CTX_free(ctx);
cipherBuf.resize(outLength1 + outLength2);
OPENSSL_cleanse(data.data(), data.size());
data.clear();
// Generate random string to prepend
unsigned char randomByte;
RAND_bytes(&randomByte, 1);
int prependStrSize = 4; // 4 + (randomByte % 7); // Random size between 4 and 10
std::vector<unsigned char> prependStr(prependStrSize);
RAND_bytes(prependStr.data(), prependStrSize);
// Correctly append data by specifying the pointer and size
data.append(reinterpret_cast<const char*>(prependStr.data()), prependStr.size());
data.append(reinterpret_cast<const char*>(password.data()), password.size());
data.append(reinterpret_cast<const char*>(salt.data()), salt.size());
data.append(reinterpret_cast<const char*>(cipherBuf.data()), cipherBuf.size());
VM_S_END // C898
return true;
}
Key Changes Explained
Appending std::vector<unsigned char> Correctly:
Instead of directly passing the std::vector to QByteArray::append, use reinterpret_cast to convert the unsigned char* to const char* and specify the exact size.
Apply the same method for appending password, salt, and cipherBuf. This ensures that each piece of data is appended correctly without causing memory access violations.
Avoid Implicit Conversions:
By explicitly specifying the data pointer and size, you avoid potential pitfalls associated with implicit type conversions, which might lead to undefined behavior or heap corruption.
Additional Recommendations
Error Handling:
Ensure that all cryptographic operations are checked for errors, and handle them appropriately to maintain the integrity of your program.
Memory Management:
Always ensure that dynamically allocated memory is properly managed to prevent leaks or corruption. Using smart pointers (e.g., std::unique_ptr) can help with this.
Code Clarity:
Consider using helper functions to handle repetitive tasks like appending data. This can make the code more readable and less error-prone.
Conclusion
The heap corruption was primarily caused by incorrect usage of QByteArray::append with std::vector<unsigned char>. By explicitly specifying the data pointer and size when appending, you can prevent memory overflows and ensure the integrity of your application's memory.
If you continue to experience issues after making these changes, consider using debugging tools like Valgrind or AddressSanitizer to further inspect memory usage and identify other potential problems.