Skip to content

Commit 5b4691b

Browse files
authored
MONGOCRYPT-825 Fix possible segmentation fault in mc_FLE2IndexedEncryptedValueV2_destroy (#1036)
1 parent 9ed7c48 commit 5b4691b

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/mc-fle2-payload-iev-v2.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ void mc_FLE2IndexedEncryptedValueV2_destroy(mc_FLE2IndexedEncryptedValueV2_t *ie
268268
_mongocrypt_buffer_cleanup(&iev->ServerEncryptedValue);
269269
_mongocrypt_buffer_cleanup(&iev->S_KeyId);
270270

271-
for (uint32_t i = 0; i < iev->edge_count; i++) {
272-
mc_FLE2TagAndEncryptedMetadataBlock_cleanup(&iev->metadata[i]);
273-
}
271+
if (iev->metadata) {
272+
for (uint32_t i = 0; i < iev->edge_count; i++) {
273+
mc_FLE2TagAndEncryptedMetadataBlock_cleanup(&iev->metadata[i]);
274+
}
274275

275-
// Metadata array is dynamically allocated
276-
bson_free(iev->metadata);
276+
// Metadata array is dynamically allocated
277+
bson_free(iev->metadata);
278+
}
277279

278280
bson_free(iev);
279281
}

test/test-mc-fle2-payload-iev-v2.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,29 @@ static void test_fle2_iev_v2(_mongocrypt_tester_t *tester) {
591591
mongocrypt_destroy(crypt);
592592
}
593593

594+
static void test_fle2_iev_v2_parse_invalid_input(_mongocrypt_tester_t *tester) {
595+
mongocrypt_status_t *status = mongocrypt_status_new();
596+
mc_FLE2IndexedEncryptedValueV2_t *iev = mc_FLE2IndexedEncryptedValueV2_new();
597+
598+
const uint32_t minValidEqualityLength = 1 + UUID_LEN + 1 + kMinServerEncryptedValueLen + kMetadataLen;
599+
_mongocrypt_buffer_t input;
600+
601+
uint8_t *data = (uint8_t *)bson_malloc0(minValidEqualityLength);
602+
603+
data[0] = MC_SUBTYPE_FLE2IndexedEqualityEncryptedValueV2;
604+
605+
_mongocrypt_buffer_from_data(&input, data, minValidEqualityLength - 1);
606+
ASSERT_FAILS_STATUS(mc_FLE2IndexedEncryptedValueV2_parse(iev, &input, status),
607+
status,
608+
"smaller than minimum length");
609+
610+
mc_FLE2IndexedEncryptedValueV2_destroy(iev);
611+
bson_free(data);
612+
_mongocrypt_buffer_cleanup(&input);
613+
mongocrypt_status_destroy(status);
614+
}
615+
594616
void _mongocrypt_tester_install_fle2_iev_v2_payloads(_mongocrypt_tester_t *tester) {
595617
INSTALL_TEST(test_fle2_iev_v2);
618+
INSTALL_TEST(test_fle2_iev_v2_parse_invalid_input);
596619
}

0 commit comments

Comments
 (0)