[MONGOCRYPT-610] Repeated calls of `mongocrypt_setopt_kms_providers` may leak memory Created: 14/Dec/23  Updated: 20/Dec/23  Resolved: 20/Dec/23

Status: Closed
Project: Libmongocrypt
Component/s: None
Affects Version/s: None
Fix Version/s: 1.9.0, 1.8.3

Type: Bug Priority: Trivial - P5
Reporter: Kevin Albertson Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Binding Changes: Not Needed

 Description   

Summary

Calling mongocrypt_setopt_kms_providers twice with an "aws" or "local" does not error, and results in a memory leak.

{
    mongocrypt_binary_t *bson = TEST_BSON(BSON_STR({"aws" : {"accessKeyId" : "foo", "secretAccessKey" : "bar"}}));
    mongocrypt_t *crypt = mongocrypt_new();
    ASSERT_OK(mongocrypt_setopt_kms_providers(crypt, bson), crypt);
    ASSERT_OK(mongocrypt_setopt_kms_providers(crypt, bson), crypt); // Leaks!
    // Leak is caused by overwrite to {{crypt->opts->kms_providers->aws.secret_access_key}}
    mongocrypt_destroy(crypt);
}

Calling mongocrypt_setopt_kms_providers twice with "azure", "gcp", or "kmip" results in an expected error:

// Errors if called multiple times with intersecting providers.
{
    mongocrypt_binary_t *one = TEST_BSON(BSON_STR({"azure" : {"accessToken" : "foo"}}));
    mongocrypt_binary_t *two = TEST_BSON(BSON_STR({"azure" : {"accessToken" : "bar"}}));
 
    mongocrypt_t *crypt = mongocrypt_new();
    ASSERT_OK(mongocrypt_setopt_kms_providers(crypt, one), crypt);
    ASSERT_FAILS(mongocrypt_setopt_kms_providers(crypt, two), crypt, "already set");
    mongocrypt_destroy(crypt);
}

Proposal: make configuring "aws" or "local" twice an error for consistency with other KMS providers.

Background & Motivation

I expect this to have little to no impact. I expect driver bindings are not calling mongocrypt_setopt_kms_providers more than once, since this is only needed to construct the mongocrypt_t handle.



 Comments   
Comment by Githook User [ 20/Dec/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: MONGOCRYPT-610 fix leak after repeated configuring of "aws" and "local" creds (#725)

  • add regression tests
Comment by Githook User [ 20/Dec/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: MONGOCRYPT-610 fix leak after repeated configuring of "aws" and "local" creds (#725)

  • add regression tests
Comment by Cloud GitHub Webhooks [ 20/Dec/23 ]

kevinAlbs merged a pull request (MONGOCRYPT-610 fix leak after repeated configuring of "aws" and "local" creds) into the following branch:
master: a324ecbd63faac9a042347c869c3de4bbf1727b8

Generated at Thu Feb 08 09:09:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.