-
Notifications
You must be signed in to change notification settings - Fork 4.1k
GH-49752: [C++][Gandiva] Fix potential buffer overrun in gandiva ssl function #49780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lriggs
wants to merge
7
commits into
apache:main
Choose a base branch
from
lriggs:GH-49752
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−50
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94a3bcd
gh-49752: Fix potential buffer overrun in gandiva ssl function.
lriggs 158e94d
Remove test which was not very valuable and caused linking problems o…
lriggs 3e5bc70
fix lint
lriggs 87c6b02
Cleanup
lriggs a52213d
lint
lriggs cd64760
Merge remote-tracking branch 'upstream/main' into GH-49752
lriggs a6ababa
Add check for EVP_DigestFinal_ex return value in hash function.
lriggs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,52 +21,15 @@ | |
| #include "gandiva/gdv_function_stubs.h" | ||
| #include "openssl/evp.h" | ||
|
|
||
| namespace gandiva { | ||
|
|
||
| /// Hashes a generic message using the SHA512 algorithm | ||
| GANDIVA_EXPORT | ||
| const char* gdv_sha512_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int sha512_result_length = 128; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_sha512(), | ||
| sha512_result_length, out_length); | ||
| } | ||
|
|
||
| /// Hashes a generic message using the SHA256 algorithm | ||
| GANDIVA_EXPORT | ||
| const char* gdv_sha256_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int sha256_result_length = 64; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_sha256(), | ||
| sha256_result_length, out_length); | ||
| } | ||
|
|
||
| /// Hashes a generic message using the SHA1 algorithm | ||
| GANDIVA_EXPORT | ||
| const char* gdv_sha1_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int sha1_result_length = 40; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_sha1(), | ||
| sha1_result_length, out_length); | ||
| } | ||
|
|
||
| GANDIVA_EXPORT | ||
| const char* gdv_md5_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int md5_result_length = 32; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_md5(), | ||
| md5_result_length, out_length); | ||
| } | ||
|
|
||
| namespace { | ||
| /// \brief Hashes a generic message using SHA algorithm. | ||
| /// | ||
| /// It uses the EVP API in the OpenSSL library to generate | ||
| /// the hash. The type of the hash is defined by the | ||
| /// \b hash_type \b parameter. | ||
| GANDIVA_EXPORT | ||
| const char* gdv_hash_using_openssl(int64_t context, const void* message, | ||
| size_t message_length, const EVP_MD* hash_type, | ||
| uint32_t result_buf_size, int32_t* out_length) { | ||
| static const char* gdv_hash_using_openssl(int64_t context, const void* message, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, if we use anonymous namespace, we don't need to use |
||
| size_t message_length, const EVP_MD* hash_type, | ||
| uint32_t result_buf_size, int32_t* out_length) { | ||
| EVP_MD_CTX* md_ctx = EVP_MD_CTX_new(); | ||
|
|
||
| if (md_ctx == nullptr) { | ||
|
|
@@ -100,9 +63,10 @@ const char* gdv_hash_using_openssl(int64_t context, const void* message, | |
| } | ||
|
|
||
| unsigned int result_length; | ||
| EVP_DigestFinal_ex(md_ctx, result, &result_length); | ||
| int evp_result = EVP_DigestFinal_ex(md_ctx, result, &result_length); | ||
|
|
||
| if (result_length != hash_digest_size && result_buf_size != (2 * hash_digest_size)) { | ||
| if (evp_result != evp_success_status || result_length != hash_digest_size || | ||
| result_buf_size != (2 * hash_digest_size)) { | ||
| gdv_fn_context_set_error_msg(context, | ||
| "Could not obtain the hash for the defined value"); | ||
| EVP_MD_CTX_free(md_ctx); | ||
|
|
@@ -112,8 +76,10 @@ const char* gdv_hash_using_openssl(int64_t context, const void* message, | |
| return ""; | ||
| } | ||
|
|
||
| // Allocate one extra byte beyond result_buf_size so that the null terminator | ||
| // written by the final snprintf call does not land past the end of the buffer. | ||
| auto result_buffer = | ||
| reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, result_buf_size)); | ||
| reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, result_buf_size + 1)); | ||
|
|
||
| if (result_buffer == nullptr) { | ||
| gdv_fn_context_set_error_msg(context, | ||
|
|
@@ -132,7 +98,8 @@ const char* gdv_hash_using_openssl(int64_t context, const void* message, | |
|
|
||
| unsigned char hex_number = result[j]; | ||
| result_buff_index += | ||
| snprintf(result_buffer + result_buff_index, result_buf_size, "%02x", hex_number); | ||
| snprintf(result_buffer + result_buff_index, | ||
| result_buf_size - result_buff_index + 1, "%02x", hex_number); | ||
| } | ||
|
|
||
| // Free the resources used by the EVP to avoid memory leaks | ||
|
|
@@ -142,6 +109,44 @@ const char* gdv_hash_using_openssl(int64_t context, const void* message, | |
| *out_length = result_buf_size; | ||
| return result_buffer; | ||
| } | ||
| } // namespace | ||
|
|
||
| namespace gandiva { | ||
|
|
||
| /// Hashes a generic message using the SHA512 algorithm | ||
| GANDIVA_EXPORT | ||
| const char* gdv_sha512_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int sha512_result_length = 128; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_sha512(), | ||
| sha512_result_length, out_length); | ||
| } | ||
|
|
||
| /// Hashes a generic message using the SHA256 algorithm | ||
| GANDIVA_EXPORT | ||
| const char* gdv_sha256_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int sha256_result_length = 64; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_sha256(), | ||
| sha256_result_length, out_length); | ||
| } | ||
|
|
||
| /// Hashes a generic message using the SHA1 algorithm | ||
| GANDIVA_EXPORT | ||
| const char* gdv_sha1_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int sha1_result_length = 40; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_sha1(), | ||
| sha1_result_length, out_length); | ||
| } | ||
|
|
||
| GANDIVA_EXPORT | ||
| const char* gdv_md5_hash(int64_t context, const void* message, size_t message_length, | ||
| int32_t* out_length) { | ||
| constexpr int md5_result_length = 32; | ||
| return gdv_hash_using_openssl(context, message, message_length, EVP_md5(), | ||
| md5_result_length, out_length); | ||
| } | ||
|
|
||
| GANDIVA_EXPORT | ||
| uint64_t gdv_double_to_long(double value) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.