Skip to content

kex: fix shared_secret memleak on EAGAIN - #2218

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:kexleakeagain
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:kexleakeagain

Conversation

@vszakats

@vszakats vszakats commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

"shared_secret is a local variable allocated at line 2113 within the
ssh2_NB_state_created block. When ssh2_transport_send returns
LIBSSH2_ERROR_EAGAIN here, the function returns immediately without
freeing shared_secret. On re-entry the variable is re-initialized to
NULL at line 2016, so the previous allocation is permanently leaked.
The shared_secret should be freed before returning EAGAIN, or its
lifetime should be extended beyond the local scope (e.g., stored in
exchange_state) so it can be cleaned up on re-entry or at
clean_exit."

Reported by GitHub Code Quality

Follow-up to 3ba252e #1644

@vszakats
vszakats requested a review from Copilot July 5, 2026 15:36

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

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

Fixes a memory leak in the ML-KEM/NISTP key exchange path when kex_finish() returns LIBSSH2_ERROR_EAGAIN, ensuring shared_secret is released before the function yields control in non-blocking mode.

Changes:

  • Replace the immediate return on LIBSSH2_ERROR_EAGAIN with a goto clean_free path.
  • Add a clean_free label that frees the locally allocated shared_secret before returning.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vszakats vszakats closed this in 75c5c85 Jul 5, 2026
@vszakats
vszakats deleted the kexleakeagain branch July 5, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants