Skip to content

userauth: fix memory leaks in libssh2_userauth_publickey_sk - #2530

Closed
abdul-khaliq-khalid wants to merge 1 commit into
libssh2:masterfrom
abdul-khaliq-khalid:userauth-sk-leaks
Closed

abdul-khaliq-khalid wants to merge 1 commit into
libssh2:masterfrom
abdul-khaliq-khalid:userauth-sk-leaks

Conversation

@abdul-khaliq-khalid

@abdul-khaliq-khalid abdul-khaliq-khalid commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

libssh2_userauth_publickey_sk() gets the public key from the sk private key via ssh2_sk_pubkey(), and on success that helper returns sk_info.application and sk_info.key_handle as fresh allocations. The cleanup frees application but never key_handle, so every successful security-key authentication leaks it. When a caller passes both a private blob and its own publickeydata, the pubkeydata buffer that userauth_read_pubkey() allocates is also lost, since only tmp_publickeydata is released.

Free key_handle next to application, and free pubkeydata only when it is not the same pointer as tmp_publickeydata so the aliased branch still frees once. Ownership stays with the function that received the buffers, matching how libssh2_userauth_publickey_fromfile() and _frommemory() already free their pubkeydata after ssh2_userauth_publickey() returns.

Follow-up to ed439a2 #698

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

Fixes memory leaks after security-key public-key authentication.

Changes:

  • Frees separately allocated public-key data without double-freeing aliases.
  • Frees the security-key handle during cleanup.

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

@vszakats vszakats closed this in e6e8839 Aug 19, 2026
@vszakats

Copy link
Copy Markdown
Member

Nice catch, thanks!

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.

3 participants