Skip to content

mbedtls: fix mismatched alloc/free, leaks, leaks on OOM, missing null-terminator - #2196

Closed
vszakats wants to merge 18 commits into
libssh2:masterfrom
vszakats:mbedissues
Closed

vszakats wants to merge 18 commits into
libssh2:masterfrom
vszakats:mbedissues

Conversation

@vszakats

@vszakats vszakats commented Jul 3, 2026 •

Copy link
Copy Markdown
Member
  • 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 in
    ssh2_rsa_new_private_frommemory().

  • fix to use mbedtls alloc/free for rsa object (to match
    ssh2_rsa_free()) in ssh2_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_octal to NULL on fail in
    ssh2_ecdsa_create_key().

  • fix to use mbedtls allocator in ssh2_ecdsa_create_key(). This buffer
    is freed with ssh2_ecdsa_free().

  • fix to use libssh2 safe-free for *out_public_key_octal on fail in
    ssh2_ecdsa_create_key(). This buffer is freed with libssh2 free in
    the codebase.

  • explicitly null-terminate buffers required to be so by mbedtls. Do not
    rely on mbedtls_calloc() doing the zeroing. To ensure the bug in
    ssh2_ecdsa_new_private_frommemory() doesn't repeat.

  • fix to use mbedtls allocator in mbed_parse_eckey(),.
    mbed_parse_openssh_key(). (also stop passing sesssion * where no
    longer 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)


@vszakats
vszakats marked this pull request as draft July 3, 2026 15:25
@vszakats
vszakats requested a review from Copilot July 3, 2026 15:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_FREE to mbedtls_calloc/mbedtls_free-compatible paths, and unify RSA cleanup through ssh2_rsa_free().
  • Fix secure-free length mismatches for null-terminated temporary key buffers (wipe len + 1 bytes).
  • 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.

Comment thread src/mbedtls.c Resolved
Comment thread src/mbedtls.c Resolved
@vszakats
vszakats marked this pull request as ready for review July 3, 2026 22:14
@vszakats vszakats changed the title mbedtls: fix a list of bugs mbedtls: fix mismatched alloc/free, leak, leaks on OOM, missing null-terminator Jul 3, 2026
@vszakats vszakats changed the title mbedtls: fix mismatched alloc/free, leak, leaks on OOM, missing null-terminator mbedtls: fix mismatched alloc/free, leaks, leaks on OOM, missing null-terminator Jul 3, 2026
vszakats added a commit that referenced this pull request Jul 4, 2026
Replace `SSH2_FREE()` with `ssh2_ecdsa_free()` in
`kex_method_mlkem_nistp_cleanup()`. This buffer is allocated with the
crypto backend's allocator, and may also require backend-specific
cleanup beforehand.

Follow-up to 3ba252e #1644
Cherry-picked from #2196

Closes #2200
vszakats added 18 commits July 4, 2026 11:59
```
/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 vszakats closed this in f7fa81c Jul 5, 2026
@vszakats
vszakats deleted the mbedissues branch July 5, 2026 00:48
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.

2 participants