Skip to content

crypto: propagate session to key alloc/free functions, switch to SSH2 allocator - #2559

Closed
vszakats wants to merge 17 commits into
libssh2:masterfrom
vszakats:session-to-new-free
Closed

vszakats wants to merge 17 commits into
libssh2:masterfrom
vszakats:session-to-new-free

Conversation

@vszakats

@vszakats vszakats commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

To shift allocation from raw or backend-specific allocators to libssh2's
own. To respect custom allocators set via libssh2_session_init_ex(),
and to reduce the chance for mismatched alloc/free calls.

  • pass session to ssh2_rsa_new() and ssh2_dsa_new() and all the
    ssh2_*_free() deallocators.
  • mbedtls: replace mbedTLS alloc/free with libssh2 alloc/free.
  • wincng: replace malloc/free with libssh2 alloc/free.
  • wincng: use libssh2 allocator in wcng_asn_decode() to sycn up with
    the above change and avoid using mixed allocators in key contexts.
  • wincng: use libssh2 allocator in wcng_asn_decode_bns(),
    wcng_publickey_from_point(), wcng_privatekey_from_point().

@vszakats vszakats changed the title crypto: propagate session to key alloc/free functions, switch to SSH2 allocators crypto: propagate session to key alloc/free functions, switch to SSH2 allocator Aug 28, 2026
@vszakats
vszakats force-pushed the session-to-new-free branch from 450bebe to eacaca1 Compare August 28, 2026 17:29
@vszakats
vszakats requested a balanced review from Copilot August 28, 2026 17:30

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

Propagates sessions through crypto key lifecycle APIs to support libssh2’s custom allocators.

Changes:

  • Adds session parameters to key constructors and destructors.
  • Migrates selected mbedTLS, Windows CNG, and OS/400 allocations.
  • Updates callers, backend macros, and documentation.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/wincng.c Migrates key-context allocation and cleanup.
src/os400qc3.h Updates RSA cleanup macro.
src/os400qc3.c Migrates crypto-context allocation.
src/openssl.h Updates destructor macros.
src/openssl.c Propagates session arguments.
src/mbedtls.c Uses session allocators for key contexts.
src/libgcrypt.h Updates destructor macros.
src/libgcrypt.c Propagates session arguments.
src/kex.c Passes sessions during ECDSA cleanup.
src/hostkey.c Updates host-key construction and destruction.
src/HACKING-CRYPTO.md Documents revised crypto interfaces.
src/crypto.h Updates shared crypto declarations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/wincng.c Resolved
Comment thread src/os400qc3.c Outdated
{
if(!ctx)
ctx = malloc(sizeof(*ctx));
ctx = SSH2_ALLOC(session, sizeof(*ctx));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Backtracking from this, destructors/allocators tangled up, plus recursive stuff.
Maybe doable if all the low-level functions got a session arg (seems overkill),
or needs untangling these.

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Suppressed comments (1)

src/wincng.c:1394

  • pbKeyObject is not always owned by the session allocator. The RSA private-key parser stores pbStructInfo here after wcng_asn_decode() allocated it with malloc() (lines 938 and 1266), so this new session deallocator call passes a foreign pointer whenever a custom allocator is configured. Allocate/rehome that decoded blob through the session allocator, or track its allocator and release it consistently.
    ssh2_zero_free(session, rsa->pbKeyObject, rsa->cbKeyObject);

Comment thread src/wincng.c Resolved
Comment thread src/mbedtls.c Resolved
@vszakats

Copy link
Copy Markdown
Member Author

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Suppressed comments (1)
src/wincng.c:1394

  • pbKeyObject is not always owned by the session allocator. The RSA private-key parser stores pbStructInfo here after wcng_asn_decode() allocated it with malloc() (lines 938 and 1266), so this new session deallocator call passes a foreign pointer whenever a custom allocator is configured. Allocate/rehome that decoded blob through the session allocator, or track its allocator and release it consistently.
    ssh2_zero_free(session, rsa->pbKeyObject, rsa->cbKeyObject);

Dang, another tangle-up.

@vszakats
vszakats force-pushed the session-to-new-free branch 2 times, most recently from 6cd768b to 33de14b Compare August 28, 2026 18:38
@vszakats
vszakats requested a balanced review from Copilot August 28, 2026 18:51

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread src/wincng.c Resolved

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@vszakats vszakats closed this in 5ef9591 Aug 28, 2026
@vszakats
vszakats deleted the session-to-new-free branch August 28, 2026 20:13
vszakats added a commit to vszakats/libssh2 that referenced this pull request Aug 28, 2026
vszakats added a commit that referenced this pull request Aug 29, 2026
…()`, `ssh2_dsa_new()`

Follow-up to 5ef9591 #2559
Follow-up to d5149ca #2548
Follow-up to b0ab005 #1207

Closes #2564
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