Skip to content

Commit fa0e5ec

Browse files
authored
GH-43946: [C++][Parquet] Guard against use of cleared decryptor/encryptor (#43947)
This is to get a clearer error rather than an obscure crash, see apache/arrow#43057 for an example. * GitHub Issue: #43946 Authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
1 parent 132f793 commit fa0e5ec

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎cpp/src/parquet/encryption/encryption_internal.cc‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class AesEncryptor::AesEncryptorImpl {
8989
}
9090

9191
private:
92+
void CheckValid() const {
93+
if (ctx_ == nullptr) {
94+
throw ParquetException("AesEncryptor was wiped out");
95+
}
96+
}
97+
9298
EVP_CIPHER_CTX* ctx_;
9399
int32_t aes_mode_;
94100
int32_t key_length_;
@@ -156,6 +162,8 @@ AesEncryptor::AesEncryptorImpl::AesEncryptorImpl(ParquetCipher::type alg_id,
156162
int32_t AesEncryptor::AesEncryptorImpl::SignedFooterEncrypt(
157163
span<const uint8_t> footer, span<const uint8_t> key, span<const uint8_t> aad,
158164
span<const uint8_t> nonce, span<uint8_t> encrypted_footer) {
165+
CheckValid();
166+
159167
if (static_cast<size_t>(key_length_) != key.size()) {
160168
std::stringstream ss;
161169
ss << "Wrong key length " << key.size() << ". Should be " << key_length_;
@@ -180,6 +188,8 @@ int32_t AesEncryptor::AesEncryptorImpl::Encrypt(span<const uint8_t> plaintext,
180188
span<const uint8_t> key,
181189
span<const uint8_t> aad,
182190
span<uint8_t> ciphertext) {
191+
CheckValid();
192+
183193
if (static_cast<size_t>(key_length_) != key.size()) {
184194
std::stringstream ss;
185195
ss << "Wrong key length " << key.size() << ". Should be " << key_length_;
@@ -413,6 +423,12 @@ class AesDecryptor::AesDecryptorImpl {
413423
}
414424

415425
private:
426+
void CheckValid() const {
427+
if (ctx_ == nullptr) {
428+
throw ParquetException("AesDecryptor was wiped out");
429+
}
430+
}
431+
416432
EVP_CIPHER_CTX* ctx_;
417433
int32_t aes_mode_;
418434
int32_t key_length_;
@@ -714,6 +730,8 @@ int32_t AesDecryptor::AesDecryptorImpl::Decrypt(span<const uint8_t> ciphertext,
714730
span<const uint8_t> key,
715731
span<const uint8_t> aad,
716732
span<uint8_t> plaintext) {
733+
CheckValid();
734+
717735
if (static_cast<size_t>(key_length_) != key.size()) {
718736
std::stringstream ss;
719737
ss << "Wrong key length " << key.size() << ". Should be " << key_length_;
@@ -806,4 +824,7 @@ void RandBytes(unsigned char* buf, size_t num) {
806824

807825
void EnsureBackendInitialized() { openssl::EnsureInitialized(); }
808826

827+
#undef ENCRYPT_INIT
828+
#undef DECRYPT_INIT
829+
809830
} // namespace parquet::encryption

0 commit comments

Comments
 (0)