Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses a set of mbedTLS-backend memory-management bugs by aligning allocations/frees with the appropriate allocator and tightening cleanup of sensitive buffers, improving correctness when libssh2 is configured with custom memory callbacks.
Changes:
- Switch several mbedTLS backend allocations from
SSH2_ALLOC/SSH2_FREEtombedtls_calloc/mbedtls_free-compatible paths, and unify RSA cleanup throughssh2_rsa_free(). - Fix secure-free length mismatches for null-terminated temporary key buffers (wipe
len + 1bytes). - Correct ECDSA key-exchange cleanup in hybrid MLKEM/NISTP KEX to use
ssh2_ecdsa_free().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/mbedtls.c | Adjusts allocation/free pairs and sensitive-buffer cleanup in the mbedTLS crypto backend. |
| src/kex.c | Fixes ECDSA private-key cleanup to use the backend-appropriate free routine. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
vszakats
marked this pull request as ready for review
July 3, 2026 22:14
…tion in ssh2_ecdsa_new_private_frommemory
…private_frommemory
…private_frommemory
```
/home/runner/work/libssh2/libssh2/src/mbedtls.c:890:5: error: Null pointer passed to 1st parameter expecting 'nonnull' [clang-analyzer-core.NonNullParamChecker,-warnings-as-errors]
890 | ssh2_explicit_zero(*out_public_key_octal, plen);
| ^
/home/runner/work/libssh2/libssh2/src/misc.h:53:39: note: expanded from macro 'ssh2_explicit_zero'
53 | #define ssh2_explicit_zero(buf, size) explicit_bzero(buf, size)
| ^ ~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:863:8: note: Assuming the condition is false
863 | if(!*out_private_key)
| ^~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:863:5: note: Taking false branch
863 | if(!*out_private_key)
| ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:868:8: note: Assuming the condition is false
868 | if(mbedtls_ecdsa_genkey(*out_private_key, (mbedtls_ecp_group_id)curve,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
869 | mbedtls_ctr_drbg_random, &mbed_ctr_drbg))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:868:5: note: Taking false branch
868 | if(mbedtls_ecdsa_genkey(*out_private_key, (mbedtls_ecp_group_id)curve,
| ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:875:5: note: Assigning value
875 | *out_public_key_octal = SSH2_ALLOC(session, plen);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:876:8: note: Assuming the condition is true
876 | if(!*out_public_key_octal)
| ^~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:876:5: note: Taking true branch
876 | if(!*out_public_key_octal)
| ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:877:9: note: Control jumps to line 888
877 | goto failed;
| ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:890:5: note: Null pointer passed to 1st parameter expecting 'nonnull'
890 | ssh2_explicit_zero(*out_public_key_octal, plen);
| ^
/home/runner/work/libssh2/libssh2/src/misc.h:53:39: note: expanded from macro 'ssh2_explicit_zero'
53 | #define ssh2_explicit_zero(buf, size) explicit_bzero(buf, size)
| ^ ~~~
106 warnings generated.
```
https://github.com/libssh2/libssh2/actions/runs/28669700775/job/85029904958?pr=2196
vszakats
added a commit
that referenced
this pull request
Aug 28, 2026
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.
fix memleak on internal buffer OOM in
ssh2_rsa_new_private_frommemory().fix to use mbedtls allocator to null-terminate the local buffer and
match the free call in
ssh2_ecdsa_new_private_frommemory().drop redundant casts for
mbedtls_calloc()pointers.fix to free rsa object with
ssh2_rsa_free()on key parse error inssh2_rsa_new_private_frommemory().fix to use mbedtls alloc/free for rsa object (to match
ssh2_rsa_free()) inssh2_rsa_new_private().include terminating byte in
mbed_safe_free()calls in.ssh2_rsa_new_private_frommemory(),ssh2_pub_priv_keyfilememory(),ssh2_ecdsa_new_private_frommemory(). For consistency/clarity only,the terminating byte is normally zero.
reset
*out_public_key_octalto NULL on fail inssh2_ecdsa_create_key().fix to use mbedtls allocator in
ssh2_ecdsa_create_key(). This bufferis freed with
ssh2_ecdsa_free().fix to use libssh2 safe-free for
*out_public_key_octalon fail inssh2_ecdsa_create_key(). This buffer is freed with libssh2 free inthe codebase.
explicitly null-terminate buffers required to be so by mbedtls. Do not
rely on
mbedtls_calloc()doing the zeroing. To ensure the bug inssh2_ecdsa_new_private_frommemory()doesn't repeat.fix to use mbedtls allocator in
mbed_parse_eckey(),.mbed_parse_openssh_key(). (also stop passingsesssion *where nolonger used after this.)
fixup a type to avoid cast.
drop newlines.
TL;DR most of these could cause crashes/leaks when using a custom
allocator, some were causing leaking crypto-backend objects, some leaks
on error path,
ssh2_ecdsa_new_private_frommemory()failing randomly.Ref: #2195 (few of these reported by Copilot)