aws / aws-sdk-cpp

AWS SDK for C++
Apache License 2.0
1.98k stars 1.06k forks source link

S3Client Leaks Memory on Windows caused by BCrypt API Misuse #2918

Closed normanade closed 6 months ago

normanade commented 7 months ago

Describe the bug

Aws::S3::S3Client leaks memory on Windows, when uses windows api BCrypt. Aws::Utils::Crypto::BCryptHashImpl, which is used by the default signer Aws::Auth::AWSAuthV4Signer on windows (when AWS_LC cmake option OFF), failed to deconstruct correctly, and leaks memory by 2800KiB per 1k construction & deconstruction call pairs.

Expected Behavior

Aws::S3::S3Client should construct and deconstruct without memory leak.

Current Behavior

Per 1k call pair of Aws::S3::S3Client construction & deconstruction, the whole program leaks about 2800KiB memory. Using Visual Studio's heap analyzer, difference of heap snapshot between the beginning and the ending of the program shows that certain parts of Aws::Utils::Crypto::BCryptHashImpl stays unreleased. aws1 aws2

Reproduction Steps

// loop for 1000 times
Aws::Client::ClientConfigurationInitValues s3ConfigInitValues;
s3ConfigInitValues.shouldDisableIMDS = true;
Aws::Client::ClientConfiguration s3ClientConfig(s3ConfigInitValues);

s3ClientConfig.endpointOverride = "127.0.0.1:8443";
s3ClientConfig.verifySSL = false;
s3ClientConfig.scheme = Aws::Http::Scheme::HTTPS;
s3ClientConfig.requestTimeoutMs = 3000;

std::shared_ptr<Aws::Auth::SimpleAWSCredentialsProvider> p_s3Cred = std::make_shared<Aws::Auth::SimpleAWSCredentialsProvider>
    ("ak", "sk");
std::shared_ptr<Aws::S3::Endpoint::S3EndpointProvider> p_s3End = std::make_shared<Aws::S3::Endpoint::S3EndpointProvider>();
Aws::S3::S3Client* pClient = new Aws::S3::S3Client(p_s3Cred, p_s3End, s3ClientConfig);
delete pClient;

Possible Solution

Aws::Utils::Crypto::BCryptHashImpl & Aws::Utils::Crypto::BCryptHashContext didn't deconstruct correctly. Deconstruct all the member variables should fix the problem.

Additional Information/Context

No response

AWS CPP SDK version used

1.11.290

Compiler and Version used

Visual Studio 2022

Operating System and version

Windows 10

jmklix commented 7 months ago

Thanks for finding this and opening a PR to fix it. Reviewing and testing your PR.

DmitriyMusatkin commented 6 months ago

PR has been merged.

github-actions[bot] commented 6 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.