Skip to content

userauth: fix crash on OOM in libssh2_userauth_publickey_sk() - #2220

Closed
vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:skoom
Closed

vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:skoom

Conversation

@vszakats

@vszakats vszakats commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

Also free the buffers if set before allocating.

Reported by GitHub Code Quality and Copilot

Follow-up to ed439a2 #698

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

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 an out-of-memory (OOM) crash in libssh2_userauth_publickey_sk() by adding allocation failure checks before using session->userauth_pblc_method.

Changes:

  • Added NULL checks after SSH2_ALLOC() for ECDSA SK key method selection.
  • Added NULL checks after SSH2_ALLOC() for ED25519 SK key method selection.

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

Comment thread src/userauth.c Resolved
Comment thread src/userauth.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 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread src/userauth.c
Comment on lines 2321 to +2327
session->userauth_pblc_method_len = strlen(ecdsa);
session->userauth_pblc_method =
SSH2_ALLOC(session, session->userauth_pblc_method_len);
if(!session->userauth_pblc_method)
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory "
"for public key method ECDSA");

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.

Most of this source follows the bad pattern prior to this patch.

Comment thread src/userauth.c Resolved
@vszakats vszakats closed this in 3e76596 Jul 5, 2026
@vszakats
vszakats deleted the skoom branch July 5, 2026 16:41
vszakats added a commit that referenced this pull request Jul 18, 2026
…y fixes

Make sure to store buffer lengths to the session structure only after
the buffer allocation was successful. To avoid leaving non-zero length
there on error cases.

Also:
- fix to fail username/password bounds check with
  `LIBSSH2_ERROR_OUT_OF_BOUNDARY` instead of conflating it with
  `LIBSSH2_ERROR_ALLOC` in `userauth_keyboard_interactive()`, and
  `userauth_password()`.

- fix to verify method sizes before calling `strncmp()` in
  `libssh2_userauth_publickey_sk()`
  Follow-up to ed439a2 #698

- replace `strlen()` calls with `sizeof()` for efficiency in
  `libssh2_userauth_publickey_sk()`
  Follow-up to ed439a2 #698

- fix error message text in `userauth_keyboard_interactive()`.
  Reported by Copilot.
  Bug: #2224 (comment)

- bail out on zero-length public key in `ssh2_userauth_publickey()`,
  to avoid zero-length allocation and imporove reported error.
  Reported by Copilot.
  Bug: #2224 (comment)

Bug: #2220 (comment)

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