Fix memory leak in EdDSA PrintableString handling#858
Fix memory leak in EdDSA PrintableString handling#858olszomal wants to merge 1 commit intosofthsm:mainfrom
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/lib/crypto/OSSLUtil.cpp (1)
211-229: Consider usingelse ifchain for minor efficiency.Since the curve names are mutually exclusive, using
else ifwould skip unnecessary comparisons after a match is found.♻️ Suggested refactor
- if (data_len == 12 && memcmp(data, "edwards25519", data_len) == 0) + if (data_len == 12 && memcmp(data, "edwards25519", 12) == 0) { nid = EVP_PKEY_ED25519; } - - if (data_len == 10 && memcmp(data, "curve25519", 10) == 0) + else if (data_len == 10 && memcmp(data, "curve25519", 10) == 0) { nid = EVP_PKEY_X25519; } - - if (data_len == 10 && memcmp(data, "edwards448", 10) == 0) + else if (data_len == 10 && memcmp(data, "edwards448", 10) == 0) { nid = EVP_PKEY_ED448; } - - if (data_len == 8 && memcmp(data, "curve448", 8) == 0) + else if (data_len == 8 && memcmp(data, "curve448", 8) == 0) { nid = EVP_PKEY_X448; }Note: Also changed line 211's
memcmpthird argument fromdata_lento12for consistency with the other comparisons.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/lib/crypto/OSSLUtil.cpp` around lines 211 - 229, The sequence of independent if statements comparing curve name bytes is inefficient and can cause redundant checks; change the four separate ifs that compare data/data_len and assign nid (currently checking "edwards25519", "curve25519", "edwards448", "curve448") into an else-if chain so once a match is found subsequent comparisons are skipped, and also make the first memcmp call use a fixed length 12 (instead of data_len) to match the other comparisons; update the conditions referencing data, data_len and assign EVP_PKEY_ED25519, EVP_PKEY_X25519, EVP_PKEY_ED448, EVP_PKEY_X448 accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/lib/crypto/OSSLUtil.cpp`:
- Around line 211-229: The sequence of independent if statements comparing curve
name bytes is inefficient and can cause redundant checks; change the four
separate ifs that compare data/data_len and assign nid (currently checking
"edwards25519", "curve25519", "edwards448", "curve448") into an else-if chain so
once a match is found subsequent comparisons are skipped, and also make the
first memcmp call use a fixed length 12 (instead of data_len) to match the other
comparisons; update the conditions referencing data, data_len and assign
EVP_PKEY_ED25519, EVP_PKEY_X25519, EVP_PKEY_ED448, EVP_PKEY_X448 accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 167f5af6-7c32-4e3c-952a-1aeb5443606d
📒 Files selected for processing (1)
src/lib/crypto/OSSLUtil.cpp
Fix a small memory leak in
OSSL::byteString2oid()when CKA_EC_PARAMS is decoded as ASN1_PRINTABLESTRING for EdDSA keys.Also replace
strcmp()on ASN.1 string data with length-awarememcmp()usingASN1_STRING_get0_data()andASN1_STRING_length(), as ASN.1 strings are not guaranteed to be NUL-terminated.For consistency, also free the temporary ASN1_OBJECT in the OID path.
Detected via Valgrind (ASN1_PRINTABLESTRING leak during C_SignInit with EdDSA keys).
Summary by CodeRabbit