Skip to content

Conversation

samuel40791765
Copy link
Contributor

Issues:

Addresses CryptoAlg-3279

Description of changes:

OpenSSL's PKCS7 structure has historically been non-opaque. This causes issues like projects depending on the PKCS7 internals to be of a certain format. OpenSSL's PKCS7 struct has a field called other, which various projects internally have found to have a dependence on. The usage spans multiple projects and seeing that OpenSSL 3.x still supports the field, this isn't going away anytime soon.

examples:

Call-outs:

N/A

Testing:

New tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@samuel40791765 samuel40791765 requested a review from a team as a code owner August 6, 2025 01:20
@codecov-commenter
Copy link

codecov-commenter commented Aug 6, 2025

Codecov Report

❌ Patch coverage is 80.76923% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.71%. Comparing base (04875db) to head (ca6003b).

Files with missing lines Patch % Lines
crypto/pkcs7/pkcs7_test.cc 80.76% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2603      +/-   ##
==========================================
- Coverage   78.72%   78.71%   -0.02%     
==========================================
  Files         645      645              
  Lines      111086   111109      +23     
  Branches    15690    15688       -2     
==========================================
+ Hits        87453    87456       +3     
- Misses      22941    22961      +20     
  Partials      692      692              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@justsmth justsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also seed our "pkcs7 verify" fuzz test with some example inputs that use the new "other" field?

Copy link
Contributor Author

@samuel40791765 samuel40791765 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also seed our "pkcs7 verify" fuzz test with some example inputs that use the new "other" field?

I'll try seeing if I can put this in the same PR.

@@ -25,7 +25,7 @@ ASN1_ADB(PKCS7) = {
ADB_ENTRY(
NID_pkcs7_encrypted,
ASN1_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT,
0))} ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
0))} ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, &p7default_tt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we diverge from what OpenSSL does here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the key to the memory leak I had to resolve. The last field in ASN1_ADB_END points to null_tt within the ASN1_ADB_st structure.

const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */

type is the selector field for the ASN1 structure. default_tt returns the designated default type for the selector field, while null_tt returns the type to return if the selector field is NULL.

I was testing against a circumstance where only d.other had been designated a value, but where type is NULL. Interestingly enough, this causes a memory leak once PKCS7 goes out of scope in AWS-LC. I pinned the reason to us returning NULL for the selector field (since type hasn't been defined) and our ASN1 macros think that nothing has to be freed here. Said ASN1 code is used below:

This doesn't happen in OpenSSL for some reason. type seems to already have a value assigned and cases default to default_tt to free all related memory. I don't necessarily think this is the "correct behavior" though, it's a bit sketchy since we haven't really initialized the type field. My assumption is that the different behavior might have something to do with us calling OpenSSL_zalloc in more places and most undefined fields are actually NULL.

Changing null_tt to a proper default value allows us to properly free values in d.other, even if type has no value assigned to it (like OpenSSL). There won't be double free issues, mainly because OpenSSL_free already does NULL check for us. I added a test called TEST(PKCS7Test, OtherFieldMemoryLeak) mainly to test this behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pinned the reason to us returning NULL for the selector field (since type hasn't been defined) and our ASN1 macros think that nothing has to be freed here.

that makes sense.

This doesn't happen in OpenSSL for some reason.

perhaps it does, and there isn't similar valgrind CI?

anyway, changes look good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran a mini test with OpenSSL locally with valgrind and verified that there was no memory leakage with OpenSSL. I dove deeper and found that reason being type having a non-NULL value and ASN1 cases default to default_tt to free the memory because of that. In AWS-LC, we default to null_tt instead since uninitialized memory is all NULL.

@samuel40791765 samuel40791765 merged commit f4f1f53 into aws:main Aug 15, 2025
194 of 202 checks passed
@samuel40791765 samuel40791765 deleted the pkcs7-other branch August 15, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants