-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix ESIGN memcpy null pointer and clang22 fortify warning (#1338) #1340
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
base: master
Are you sure you want to change the base?
Conversation
) - Use seed.data() instead of pointer arithmetic for static analyzer - Add CRYPTOPP_ASSERT to verify bounds explicitly - Guard memcpy against null pointer when seedParam.size() == 0 Resolves UBSan violation and clang22 -O3 fortify warning. Fixes weidai11#1338
|
Thanks for submitting this upstream! I'm glad the fix I provided in #1338 resolved your issue. |
|
This PR essentially does not fix the issue, because zero count in |
|
Hi, thanks for taking the time to look at this. Just to clarify what I was trying to address: the warning I’m looking at is std::memcpy(seed + 4, seedParam.begin(), seedParam.size());The diagnostic shows a bound of In my fork (cryptopp-modern) I changed this to keep the behaviour the seed.resize(seedParam.size() + 4);
CRYPTOPP_ASSERT(seed.size() >= seedParam.size() + 4);
std::memcpy(seed.data() + 4, seedParam.begin(), seedParam.size());Using I’m not claiming this fixes any broader issues around For reference, the fork and change are here: |
|
@irwir Hello, and thank you for taking the time to review my PR! First, the PR description itself is a bit short because I tried to follow the contribution guidelines from the official Crypto++ website (https://www.cryptopp.com/ ). For that reason, I also sent an email to the mailing list with the full details. Here is the complete content:Issue Summary
Environment
Exact Error Message SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior esign.cpp:118:25 Stack Trace Root Cause Minimal Reproduction A focused test exercising this path is available here: The test validates 17 seed lengths (including 0) across 3 modulus sizes for comprehensive edge case coverage. Evidence Commit demonstrating the issue:
Commit with UBSan fix:
Complete fix (UBSan + Issue #1338):
Pull Request #1340 The fix applies three changes to esign.cpp:117-122: seed.resize(seedParam.size() + 4);
// Help static analyzer verify bounds (Issue #1338)
CRYPTOPP_ASSERT(seed.size() >= seedParam.size() + 4);
// Guard against null pointer when size is 0 (UBSan)
if (seedParam.size() > 0)
std::memcpy(seed.data() + 4, seedParam.begin(), seedParam.size());Key changes:
This approach is inspired by the cryptopp-modern fork fix (commit 79ecd1f0) with additional UBSan protection. Additional Context (referencing @Coralesoft explanation) I think @Coralesoft already described the fortify/Clang side of this very clearly in his earlier comment, so I’ll just briefly summarize and connect it to this PR. In short, the warning we’re seeing is raised on the std::memcpy(seed + 4, seedParam.begin(), seedParam.size());As @Coralesoft pointed out, the static analysis appears to lose track of the actual buffer bounds once pointer arithmetic (
This pattern is exactly what I adopted here for the fortify/Clang diagnostic: it doesn’t change the semantics of On top of that, my PR adds the |
There was
Seem like this check should have been done much earlier; and maybe exception should have been thrown instead of returning random(?) values without a proper seed. Possibly the root cause should be fixed somewhere in |
|
@irwir, thanks for the thorough review. You’ve raised valid technical points: Re: pointer arithmetic, you’re correct that Re: if (seedParam.size() > seed.ELEMS_MAX - 4)
throw InvalidArgument(...);The assert is there as supplementary debug-time validation. Re: Proposal, my view is that @scc-tw's PR still has value on its own by:
I’d suggest we treat this as a targeted fix and track the Thoughts? |
|
@irwir Thanks a lot for your detailed review comments and for taking the time to write them up. To be honest, the original wording in this PR description partly came from autocomplete / GPT. The only part I carefully reviewed myself was the email-style explanation I pasted into the PR body. I was trying to follow the project’s contribution guidelines more rigorously and treated the short PR summary as relatively trivial, since the patch itself is small and (in my view) quite safe. I agree with you that the way I phrased it was not ideal and could be misleading, and I appreciate you pointing that out. Many thanks also to @Coralesoft for the clearer explanation—I’ve updated the wording in this PR to follow that summary, because it matches what I actually had in mind much better. I also agree that Please let me know if the current patch and the discussion so far address your concerns, or if there’s anything else you’d like me to clarify or adjust. I’m happy to provide more details or refine the change further. |
|
@irwir Thank you again for your thoughtful comments. I agree that the To properly address the |
Exactly the opposite is true.
Nothing new. The changes should fix diagnosed issues, not just hide. |
|
@noloader Hi, could you take a look at this when you have a moment? This PR fixes:
with the There’s been some discussion above about whether this should stay a narrow, call-site fix vs. addressing broader |
InvertibleESIGNFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶m)Fixes #1338