Skip to content

Commit 9442032

Browse files
committed
CDRIVER-4457 fix NULL deref on QE collection drop (#1113)
Fixes a NULL dereference when mongoc_collection_drop is called with a NULL bson_error_t for a Queryable Encryption collection
1 parent 22ae402 commit 9442032

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,13 @@ drop_with_opts_with_encryptedFields (mongoc_collection_t *collection,
10701070
mongoc_collection_t *ecocCollection = NULL;
10711071
bool ok = false;
10721072
const char *name = mongoc_collection_get_name (collection);
1073+
bson_error_t local_error = {0};
1074+
1075+
if (!error) {
1076+
/* If no error is passed, use a local error. Error codes are checked
1077+
* when collections are dropped. */
1078+
error = &local_error;
1079+
}
10731080

10741081
/* Drop ESC collection. */
10751082
escName = _mongoc_get_encryptedField_state_collection (

src/libmongoc/tests/test-mongoc-client-side-encryption.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,6 +5365,45 @@ _not_have_aws_creds_env (void *unused)
53655365
return !_have_aws_creds_env (unused);
53665366
}
53675367

5368+
// Test calling mongoc_collection_drop with a NULL bson_error_t when the state
5369+
// collections do not exist. This is a regression test for CDRIVER-4457.
5370+
static void
5371+
test_drop_qe_null_error (void *unused)
5372+
{
5373+
bson_error_t error;
5374+
mongoc_client_t *const client = test_framework_new_default_client ();
5375+
bson_t *const kmsProviders =
5376+
_make_kms_providers (false /* with aws */, true /* with local */);
5377+
bson_t *encryptedFieldsMap;
5378+
mongoc_client_t *encryptedClient;
5379+
mongoc_auto_encryption_opts_t *aeOpts;
5380+
mongoc_collection_t *coll;
5381+
5382+
BSON_UNUSED (unused);
5383+
5384+
/* Create an encryptedFieldsMap. */
5385+
encryptedFieldsMap = BCON_NEW ("db.encrypted", "{", "fields", "[", "]", "}");
5386+
encryptedClient = test_framework_new_default_client ();
5387+
aeOpts = mongoc_auto_encryption_opts_new ();
5388+
mongoc_auto_encryption_opts_set_kms_providers (aeOpts, kmsProviders);
5389+
mongoc_auto_encryption_opts_set_keyvault_namespace (
5390+
aeOpts, "keyvault", "datakeys");
5391+
mongoc_auto_encryption_opts_set_encrypted_fields_map (aeOpts,
5392+
encryptedFieldsMap);
5393+
ASSERT_OR_PRINT (
5394+
mongoc_client_enable_auto_encryption (encryptedClient, aeOpts, &error),
5395+
error);
5396+
coll = mongoc_client_get_collection (encryptedClient, "db", "encrypted");
5397+
ASSERT (mongoc_collection_drop (coll, NULL));
5398+
5399+
mongoc_collection_destroy (coll);
5400+
mongoc_auto_encryption_opts_destroy (aeOpts);
5401+
mongoc_client_destroy (encryptedClient);
5402+
bson_destroy (encryptedFieldsMap);
5403+
bson_destroy (kmsProviders);
5404+
mongoc_client_destroy (client);
5405+
}
5406+
53685407
void
53695408
test_client_side_encryption_install (TestSuite *suite)
53705409
{
@@ -5657,4 +5696,12 @@ test_client_side_encryption_install (TestSuite *suite)
56575696
test_framework_skip_if_no_client_side_encryption,
56585697
test_framework_skip_if_max_wire_version_less_than_8,
56595698
_have_aws_creds_env);
5699+
5700+
TestSuite_AddFull (suite,
5701+
"/client_side_encryption/drop_qe_null_error",
5702+
test_drop_qe_null_error,
5703+
NULL,
5704+
NULL,
5705+
test_framework_skip_if_no_client_side_encryption,
5706+
test_framework_skip_if_max_wire_version_less_than_8);
56605707
}

0 commit comments

Comments
 (0)