Repository navigation
Changes for different openssl versions #54 - #66
joachimmetz wants to merge 3 commits into
Conversation
|
@sshock PTAL, note that I'm not too familiar with the code base |
|
Thanks @joachimmetz. This is much appreciated. I'll take a look when I can 😉. |
|
workflows use please add |
|
Done, also note that But it might be better that you as the maintainer make copyright related changes (given optics). Can create an issue as reminder if you want. |
|
Thanks. Hoping I can take a closer look at this PR soon. Let's discuss copyright statements separately. I was not aware that anything needed to be clarified. Can you create a discussion? |
sure, done #68 |
|
Hmm, seeing a lot of feedback from AI. I'll share feedback from Gemini, Claude and OpenAI below. |
|
From Gemini: OverviewCommit Overall, the direction is sound for OpenSSL 3.x/4.x (including High & Medium Severity Issues1. Buffer Overflow / Heap Corruption in
|
|
From Claude: Verdict: request changes. The migration is mostly mechanical, and the AES-ECB key-wrap paths and the Must fix
Should fix
What's good
Corrections to my earlier notesWith the full patch, Before mergingRun round-trip tests, old-writes/new-reads and the reverse, on OpenSSL 1.1.1 and 3.x. Cover AFF segment encryption, passphrase keys and qcow. Add a build matrix that includes LibreSSL and a no-deprecated build. If you give me the repo, I can do the grep for the removed headers and macros, |
|
From ChatGPT: I'll review PR #66 as a code-review exercise, focusing on:
The PR is titled Changes for different OpenSSL versions, targets GitHub 1. Preliminary findingsChanges requested I found several issues worth addressing before merging. Some are correctness bugs rather than merely portability concerns. The most important problems are in the new EVP-based encryption implementation, particularly error handling and the QEMU encryption changes. A. EVP encryption/decryption errors are ignoredFiles: For example, the new code in There are three problems:
This can result in corrupted or partially decrypted data being returned to the caller without an error indication. The same general problem occurs in the QEMU Recommendation: Check every EVP operation that can fail and propagate an error to the caller. In particular, don't report a successful read or write when the encryption operation failed. B. Potential integer overflow when converting |
| Area | Assessment |
|---|---|
| OpenSSL 1.0.2 compatibility | EVP context aliases appear reasonable. |
| OpenSSL 1.1.1 compatibility | Generally reasonable. |
| OpenSSL 3.x compatibility | Moving away from deprecated low-level APIs is appropriate. |
| OpenSSL 4.x compatibility | Worth testing against an actual release and its provider behavior. |
| LibreSSL | Needs a dedicated build test. |
| AES file-format compatibility | Requires regression testing against existing AFF files. |
| QEMU asynchronous I/O | Needs verification of context sharing and synchronization. |
| S3 support | Needs a dedicated build and runtime test. |
My overall opinion: The direction of the PR is good. Moving from the deprecated low-level OpenSSL interfaces to EVP is the right general approach, and some of the changes fix existing problems.
However, the migration has introduced several problems because EVP has a different operational model from the low-level AES interfaces.
The most important distinction is that AES_KEY is essentially an expanded key, whereas EVP_CIPHER_CTX is a stateful encryption/decryption context. The code needs to account for initialization, padding, update/final processing, errors, and potentially concurrent access.
I would fix the padding-order issues first, then address error propagation and context management before considering the PR ready.
Primary references:
One particularly useful next step would be to work through lib/crypto.cpp and lib/afflib.cpp line by line and establish exactly which changes are necessary to preserve the existing AFF encryption format. That would address the most consequential part of this PR before tackling the build-system changes.
- Correctness: Whether the changes work across the OpenSSL versions they intend to support.
- Compatibility: Whether they preserve compatibility with older OpenSSL versions and existing AFFLIB builds.
- Safety: Whether the changes introduce memory-management problems, cryptographic errors, undefined behavior, or other security vulnerabilities.
- Build and testing: Whether the changes compile correctly across the supported configurations, including optional S3 support.
The PR is titled Changes for different OpenSSL versions, targets master, and contains one commit (9f171f9).
GitHub
1. Preliminary findings
Changes requested
I found several issues worth addressing before merging. Some are correctness bugs rather than merely portability concerns.
The most important problems are in the new EVP-based encryption implementation, particularly error handling and the QEMU encryption changes.
A. EVP encryption/decryption errors are ignored
Files: lib/afflib.cpp, lib/qemu/block-qcow.c, lib/qemu/block-qcow2.c
For example, the new code in af_aes_decrypt() does this:
if(EVP_DecryptInit_ex(af->crypto->dkey,NULL,NULL,NULL,iv)==1){
outlen = 0;
EVP_DecryptUpdate(af->crypto->dkey,data,&outlen,data,(int)*datalen);
{
int flen = 0;
EVP_DecryptFinal_ex(af->crypto->dkey,data+outlen,&flen);
}
}
There are three problems:
- The return value from
EVP_DecryptUpdate()is ignored. - The return value from
EVP_DecryptFinal_ex()is ignored. - If
EVP_DecryptInit_ex()fails, the function continues as though decryption succeeded.
This can result in corrupted or partially decrypted data being returned to the caller without an error indication.
The same general problem occurs in the QEMU encrypt_sectors() implementations, where neither initialization nor update failures are checked.
Recommendation: Check every EVP operation that can fail and propagate an error to the caller. In particular, don't report a successful read or write when the encryption operation failed.
B. Potential integer overflow when converting size_t to int
File: lib/afflib.cpp
The new code contains:
EVP_DecryptUpdate(
af->crypto->dkey,
data,
&outlen,
data,
(int)*datalen
);
The EVP API takes an int input length, whereas AFFLIB uses size_t.
If *datalen exceeds INT_MAX, the conversion can produce an invalid length. This is particularly relevant when processing large forensic images.
The same issue exists in the encryption path:
EVP_EncryptUpdate(
af->crypto->ekey,
newdata,
&ctlen,
newdata,
(int)(datalen+pad)
);
Recommendation: Either process the input in appropriately sized chunks or explicitly reject lengths that cannot safely be represented by int. Also check the addition used to calculate the allocation size.
C. QEMU encryption contexts introduce mutable shared state
Files: lib/qemu/block-qcow.c, lib/qemu/block-qcow2.c
Previously, the QEMU code used:
AES_KEY aes_encrypt_key;
AES_KEY aes_decrypt_key;
These structures contain expanded key material and can be used without maintaining an ongoing cipher-operation state.
The PR changes them to:
EVP_CIPHER_CTX *aes_encrypt_key;
EVP_CIPHER_CTX *aes_decrypt_key;
These are mutable contexts.
The new encrypt_sectors() function repeatedly reinitializes the context with a different IV:
EVP_EncryptInit_ex(key, NULL, NULL, NULL, ivec.b);
EVP_EncryptUpdate(key, out_buf, &len, in_buf, 512);
This is important because QEMU has asynchronous I/O paths. If multiple operations can access the same context concurrently, they can interfere with one another.
The previous AES_KEY implementation did not have this particular state-sharing problem.
Recommendation: Verify the QEMU execution and locking model. If concurrent access is possible, use independent EVP contexts per operation, or otherwise serialize access.
I would consider this a significant issue until concurrency has been ruled out.
D. QEMU disables padding in the wrong order
Files: lib/qemu/block-qcow.c, lib/qemu/block-qcow2.c
This is a more serious issue than it might initially appear.
The PR does:
EVP_CIPHER_CTX_set_padding(s->aes_encrypt_key, 0);
EVP_CIPHER_CTX_set_padding(s->aes_decrypt_key, 0);
if (EVP_EncryptInit_ex(s->aes_encrypt_key, EVP_aes_128_cbc(),
NULL, keybuf, NULL) != 1 ||
EVP_DecryptInit_ex(s->aes_decrypt_key, EVP_aes_128_cbc(),
NULL, keybuf, NULL) != 1)
return -1;
OpenSSL documents that EVP_CIPHER_CTX_set_padding() should be called after initializing the cipher context.
OpenSSL Documentation+1
The correct order is:
if (EVP_EncryptInit_ex(...) != 1)
return -1;
if (EVP_CIPHER_CTX_set_padding(s->aes_encrypt_key, 0) != 1)
return -1;
And equivalently for decryption.
This matters because the QEMU code also omits EVP_EncryptFinal_ex() and EVP_DecryptFinal_ex(), relying on padding being disabled and all sector lengths being multiples of the AES block size.
If padding remains enabled, EVP_EncryptUpdate() and EVP_DecryptUpdate() can retain the final block rather than producing all 512 bytes immediately. That breaks the assumption that the operation has processed the entire sector.
I would treat this as a potentially critical data-corruption bug.
E. affinfo does not check EVP context allocation
File: tools/affinfo.cpp
The new code allocates two contexts:
EVP_MD_CTX *md5ctx = EVP_MD_CTX_new();
EVP_MD_CTX *sha1ctx = EVP_MD_CTX_new();
But it never checks whether either allocation succeeded before passing the pointers to EVP_DigestInit_ex().
For example:
if(opt_validate & VALIDATE_MD5){
const EVP_MD *md5_md = EVP_get_digestbyname("MD5");
if(!md5_md || EVP_DigestInit_ex(md5ctx,md5_md,NULL)!=1){
af_close(af);
return;
}
}
If EVP_MD_CTX_new() returns NULL, this can dereference a null context.
There is also a memory leak in the MD5 initialization failure path: neither allocated context is freed.
Recommendation: Check both allocations immediately, and free both contexts on every error path.
F. Hashing errors are not consistently propagated
Files: lib/crypto.cpp, lib/afflib_pages.cpp, tools/affcopy.cpp, tools/affinfo.cpp, tools/affconvert.cpp
The new af_MD5() and af_sha1() functions return an error code, which is good.
However, several callers ignore it.
For example:
af_MD5((const unsigned char *)data,datalen,md5_buf);
This occurs in af_update_page().
If the digest operation fails, the caller can continue and store an invalid digest.
The same general concern applies to EVP_DigestUpdate() and EVP_DigestFinal_ex() calls in the utilities.
This is especially important for forensic software: a failed integrity calculation must not silently be treated as a successful integrity calculation.
Recommendation: Propagate digest failures, and do not write or report a digest as valid unless its calculation succeeded.
G. Cross-compilation support in the OpenSSL version detection
File: m4/ax_openssl_version.m4
The new macro uses:
AC_RUN_IFELSE([AC_LANG_PROGRAM(... )],
[...],
[...])
But it does not supply the fourth argument, which is the cross-compilation action.
The comment explicitly claims:
when execution is not possible (e.g. cross-compiling), the version is reported as unknown.
However, Autoconf's AC_RUN_IFELSE requires a cross-compilation alternative if the test must work when the target executable cannot be run.
Without it, configure can fail outright during cross-compilation rather than reporting an unknown version.
Recommendation: Supply the fourth argument and explicitly set ax_openssl_version to an empty value in that case.
This is a portability issue rather than a direct security vulnerability, but it contradicts the stated behavior of the new macro.
2. Additional findings
H. The passphrase-based AES implementation has the same padding-order bug
File: lib/crypto.cpp
This is particularly important because the PR changes the implementation of AFF key storage and retrieval.
In af_save_aes_key_with_passphrase():
EVP_CIPHER_CTX *ectx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_set_padding(ectx,0);
if(EVP_EncryptInit_ex(ectx,EVP_aes_256_ecb(),NULL,
passphrase_hash,NULL)==1){
And in af_get_aes_key_from_passphrase():
EVP_CIPHER_CTX *dctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_set_padding(dctx,0);
if(EVP_DecryptInit_ex(dctx,EVP_aes_256_ecb(),NULL,
passphrase_hash,NULL)==1){
Both functions set padding before initializing the cipher.
This is especially problematic because both functions process three separate 16-byte blocks using EVP_EncryptUpdate() or EVP_DecryptUpdate(), but never call the corresponding Final function.
With standard EVP block padding enabled, the final block is retained by Update for processing during Final. Consequently, the three-block operations cannot be assumed to produce the expected three encrypted/decrypted blocks.
Recommendation: Initialize the cipher first, disable padding second, and verify every operation. Add tests that save and retrieve an AFF encryption key using a passphrase.
I would classify this as a high-priority correctness issue.
I. af_MD5() and af_sha1() don't check all EVP operations
File: lib/crypto.cpp
The new implementations check initialization and finalization, but don't check:
EVP_DigestUpdate(ctx,data,datalen);
They should check its return value as well.
Otherwise, a failed update could result in an invalid digest being returned as successful.
This is particularly important because the functions are intended to become the common replacement for the old OpenSSL hashing functions.
J. affconvert also needs better error handling
File: tools/affconvert.cpp
The new code allocates two contexts:
EVP_MD_CTX *md5ctx = EVP_MD_CTX_new();
EVP_MD_CTX *sha1ctx = EVP_MD_CTX_new();
It checks for allocation failure, which is good.
However:
- If one allocation succeeds and the other fails, the successful allocation is leaked.
- The return values of
EVP_DigestUpdate()are ignored. - The return values of
EVP_DigestFinal_ex()are ignored.
The last two issues can result in incorrect MD5/SHA1 metadata being written to the output AFF file.
K. affinfo preview change is actually an improvement
File: tools/affinfo.cpp
There is one positive change worth mentioning.
The old code used:
u_char md5[32];
MD5(data,data_len,md5);
memcpy(data,md5,32);
data_len = 32;
MD5 produces only 16 bytes. Consequently, the second 16 bytes copied into data were uninitialized stack contents.
The PR corrects this:
u_char md5[16];
af_MD5((const unsigned char *)data,data_len,md5);
memcpy(data,md5,16);
data_len = 16;
This eliminates an unintended disclosure of stack contents through the preview functionality.
That's a good fix.
3. Compatibility and design considerations
There are several other aspects I would investigate, although I don't consider them established defects from the diff alone.
| Area | Assessment |
|---|---|
| OpenSSL 1.0.2 compatibility | EVP context aliases appear reasonable. |
| OpenSSL 1.1.1 compatibility | Generally reasonable. |
| OpenSSL 3.x compatibility | Moving away from deprecated low-level APIs is appropriate. |
| OpenSSL 4.x compatibility | Worth testing against an actual release and its provider behavior. |
| LibreSSL | Needs a dedicated build test. |
| AES file-format compatibility | Requires regression testing against existing AFF files. |
| QEMU asynchronous I/O | Needs verification of context sharing and synchronization. |
| S3 support | Needs a dedicated build and runtime test. |
One additional consideration is that OpenSSL 3 uses providers for cryptographic algorithms. In particular, MD5 may not be available under every provider configuration.
The new af_MD5() implementation uses:
EVP_get_digestbyname("MD5");
If MD5 is unavailable, it returns an error. That's reasonable behavior, but callers must actually handle that error.
AES compatibility testing
I would be particularly careful about the claim that the EVP implementation produces byte-identical results to the old AES implementation.
The conversion is conceptually reasonable:
- AES-CBC remains AES-CBC.
- The key sizes are preserved.
- The IV construction is preserved.
- Padding is intended to be disabled in EVP.
- The existing AFF segment padding scheme is retained.
However, the new code also changes how the input buffer is prepared before encryption.
I would test actual ciphertext compatibility rather than relying on the comments.
4. Recommended test plan
Before merging, I would want the following tests.
| Test | Expected result |
|---|---|
| Build with OpenSSL 1.0.2 | Successful |
| Build with OpenSSL 1.1.1 | Successful |
| Build with OpenSSL 3.x | Successful |
| Build with OpenSSL 3.x, no deprecated APIs | Successful |
| Cross-compile with OpenSSL | Configure succeeds |
| AES-128 AFF encryption/decryption | Correct round trip |
| AES-192 AFF encryption/decryption | Correct round trip |
| AES-256 AFF encryption/decryption | Correct round trip |
| Passphrase-based key storage/retrieval | Correct round trip |
| Read previously encrypted AFF files | Successful |
| Write and read encrypted QEMU QCOW images | Correct data |
| QEMU concurrent I/O | No data corruption |
| MD5/SHA1 page verification | Correct results |
| S3-enabled build | Successful |
For AES, I would test input lengths of:
- 0 bytes
- 1 byte
- 15 bytes
- 16 bytes
- 17 bytes
- 511 bytes
- 512 bytes
- 513 bytes
These exercise the different padding and alignment cases.
I would also compare ciphertext generated by the old implementation and the new implementation for identical keys, IVs, and input data, wherever the old implementation's behavior is well-defined.
5. Overall assessment
I would not merge this PR yet
Here is my prioritization:
| Priority | Finding | File |
|---|---|---|
| Critical | QEMU padding initialization order | block-qcow.c, block-qcow2.c |
| Critical | Passphrase AES padding initialization order | crypto.cpp |
| High | Ignored encryption/decryption failures | afflib.cpp, QEMU files |
| High | Shared mutable QEMU cipher contexts | QEMU files |
| High | Unchecked size_t to int conversion |
afflib.cpp |
| High | Missing context allocation check | affinfo.cpp |
| Medium | Ignored digest failures | Multiple files |
| Medium | Cross-compilation configure failure | ax_openssl_version.m4 |
| Low | Context cleanup on allocation failures | affconvert.cpp |
My overall opinion: The direction of the PR is good. Moving from the deprecated low-level OpenSSL interfaces to EVP is the right general approach, and some of the changes fix existing problems.
However, the migration has introduced several problems because EVP has a different operational model from the low-level AES interfaces.
The most important distinction is that AES_KEY is essentially an expanded key, whereas EVP_CIPHER_CTX is a stateful encryption/decryption context. The code needs to account for initialization, padding, update/final processing, errors, and potentially concurrent access.
I would fix the padding-order issues first, then address error propagation and context management before considering the PR ready.
Primary references:
- [Complete PR commit diff](https://github.com/sshock/AFFLIBv3/commit/9f171f9.patch?utm_source=chatgpt.com)
- [OpenSSL EVP encryption/decryption documentation](https://docs.openssl.org/3.2/man3/EVP_EncryptInit/?utm_source=chatgpt.com)
- [OpenSSL 1.1.1 EVP documentation](https://docs.openssl.org/1.1.1/man3/EVP_EncryptInit/?utm_source=chatgpt.com)
One particularly useful next step would be to work through lib/crypto.cpp and lib/afflib.cpp line by line and establish exactly which changes are necessary to preserve the existing AFF encryption format. That would address the most consequential part of this PR before tackling the build-system changes.
|
Changes based on review feedback PTAL also CI tests are failing due to what looks like connectivity issues between GitHub and the apt repositories |
No description provided.