[ Security ] - keep key material off disk, fix block-length parsing UB - #13
Merged
cristiancmoises merged 1 commit intoJun 16, 2026
Merged
Conversation
Security fixes:
- Stop staging keys in predictable /tmp paths. The in-memory
encrypt/decrypt path wrote the public key (and, worse, the private
key) to /tmp/zupt_{pub,priv}_<pid> just to hand a path to the
file-based init functions. That was a symlink-attack vector and it
defeated the library's own mlock protection by leaving private key
material on disk. Added zupt_hybrid_encrypt_init_mem /
zupt_hybrid_decrypt_init_mem, which parse the key straight from the
caller's buffer; the C wrapper now uses them and never touches disk.
- Create private key files with 0600 instead of the umask default.
Applied to both the C path (zupt_hybrid_keygen, via open+fdopen) and
the C++ path (KeyGenerator::saveKeyPair, via open+fchmod before the
ofstream), so a freshly generated key is never briefly world-readable.
- Fix sign-extension/UB when parsing the per-block payload length on
decrypt. Each byte promoted to int, so `byte << 24` is undefined and
sign-extends when the top bit is set, yielding a bogus huge block_len.
Cast each byte to size_t before shifting.
Cleanup:
- Remove dead, incorrect zupt_hybrid_derive_keys (it ignored the X25519
shared secret and the ml_pk argument, so it produced keys the decrypt
side could never reproduce) and the unused zupt_hybrid_decrypt_derive_keys.
Refactored the file-based init functions to share an encaps/decaps core
with the new in-memory variants, so there is no duplicated KDF logic.
Build is clean (no warnings) and all five test suites pass.
Owner
|
Thank you |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Security fixes:
Stop staging keys in predictable /tmp paths. The in-memory encrypt/decrypt path wrote the public key (and, worse, the private key) to /tmp/zupt_{pub,priv}_ just to hand a path to the file-based init functions. That was a symlink-attack vector and it defeated the library's own mlock protection by leaving private key material on disk. Added zupt_hybrid_encrypt_init_mem / zupt_hybrid_decrypt_init_mem, which parse the key straight from the caller's buffer; the C wrapper now uses them and never touches disk.
Create private key files with 0600 instead of the umask default. Applied to both the C path (zupt_hybrid_keygen, via open+fdopen) and the C++ path (KeyGenerator::saveKeyPair, via open+fchmod before the ofstream), so a freshly generated key is never briefly world-readable.
Fix sign-extension/UB when parsing the per-block payload length on decrypt. Each byte promoted to int, so
byte << 24is undefined and sign-extends when the top bit is set, yielding a bogus huge block_len. Cast each byte to size_t before shifting.Cleanup:
Refactored the file-based init functions to share an encaps/decaps core with the new in-memory variants, so there is no duplicated KDF logic. Build is clean (no warnings) and all five test suites pass.