Skip to content

userauth: keep buffer lengths consistent with pointers on fail, nearby fixes - #2224

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

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

Conversation

@vszakats

@vszakats vszakats commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

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:

Bug: #2220 (comment)


https://github.com/libssh2/libssh2/pull/2224/files?w=1

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 refines several userauth code paths to keep session buffer length fields consistent with their corresponding pointers when allocations fail, preventing stale-length state from persisting across retries.

Changes:

  • Delay assigning *_len fields until after successful allocations in multiple userauth flows.
  • Explicitly reset some length fields to 0 when freeing/reallocating buffers.
  • Use local size_t variables (data_len, packet_len, method_len) to avoid storing derived lengths into session state prematurely.

💡 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
Comment thread src/userauth.c Resolved
Comment thread src/userauth.c Resolved
Comment thread src/userauth.c Resolved
@vszakats vszakats changed the title userauth: keep buffer length consistent with pointers on fail userauth: keep buffer length consistent with pointers on fail, nearby fixes Jul 5, 2026
@vszakats
vszakats requested a review from Copilot July 5, 2026 18:15

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 1 comment.

Comment thread src/userauth.c Resolved
@vszakats
vszakats requested a review from Copilot July 5, 2026 19:16
@vszakats vszakats changed the title userauth: keep buffer length consistent with pointers on fail, nearby fixes userauth: keep buffer lengths consistent with pointers on fail, nearby fixes Jul 5, 2026

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 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 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 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (3)

src/userauth.c:473

  • If allocating the password-change request packet fails, the function returns without resetting session->userauth_pswd_state to ssh2_NB_state_idle. That leaves the session in an inconsistent non-idle state after a hard failure. Reset the state before returning here.
                        if(!session->userauth_pswd_data) {
                            SSH2_SAFEFREE(session,
                                          session->userauth_pswd_newpw);
                            return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
                                            "Unable to allocate memory "

src/userauth.c:2377

  • userauth_read_blob_pubkey() overwrites session->userauth_pblc_method (it sets *method = pubkey). In this code path, session->userauth_pblc_method may already point to an allocated buffer (including the cert-method buffers allocated just above), so it gets leaked when the pointer is overwritten. Free/reset the existing method buffer before calling userauth_read_blob_pubkey() (or remove the redundant cert-method allocations entirely).
        }
    }
    else
        return ssh2_err(session, LIBSSH2_ERROR_FILE,
                        "Invalid data in public and private key.");

src/userauth.c:2399

  • When publickeydata is provided, userauth_read_blob_pubkey() allocates and returns decoded pubkeydata (base64-decoded blob). That buffer is not freed on any return path here, which leaks memory; only tmp_publickeydata is freed. Add a conditional free for pubkeydata when it is not tmp_publickeydata.

    return rc;
}

Comment thread src/userauth.c
Comment on lines +457 to +461
SSH2_SAFEFREE(session,
session->userauth_pswd_newpw);
return ssh2_err(session,
LIBSSH2_ERROR_OUT_OF_BOUNDARY,
"Username or password too large");

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.

I assume the same is true for the alloc failure path, which
shared the exit point before this patch.

Maybe more, but not attempting to find and tackle those.

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 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/userauth.c:2373

  • userauth_read_blob_pubkey() always allocates a new buffer and assigns it to *method (it does not free any existing value). In this function, session->userauth_pblc_method may already point to an allocated buffer from the preceding ECDSA/ED25519 handling, so the call below can overwrite the pointer and leak that allocation. On parse failure, the pre-allocated method would also remain attached to the session without being freed. Free/reset session->userauth_pblc_method before calling userauth_read_blob_pubkey() (or remove the earlier allocation entirely).
            rc = userauth_read_blob_pubkey(session,
                                           &session->userauth_pblc_method,
                                           &session->userauth_pblc_method_len,
                                           &pubkeydata, &pubkeydata_len,
                                           (const char *)publickeydata,

keep length consisten with pointer on fail

s3

set more lens to zero on fail, split bounds err from alloc err

cleanups

check sizes before strncmp, use sizeof()

another errcode conflation and typo in prev

fixup

fix errmsg and check for zero allocation via Copilot

userauth.h reflow, avoid leaks

nl

dedupe cleanup steps

fix to set state machine to idle on bounds/alloc fails in userauth_password()

sp

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 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

src/userauth.c:462

  • The early-return path when username/password are out of bounds does not reset session->userauth_pswd_state back to ssh2_NB_state_idle. Since this runs while in the password-change flow (ssh2_NB_state_sent1), a retry after this error can continue in sent1 and then fail with a misleading LIBSSH2_ERROR_PROTO ("Unexpected packet size"). Reset the state before returning on this failure path.
                        if(username_len > MAX_INPUT_LEN ||
                           password_len > MAX_INPUT_LEN) {
                            SSH2_SAFEFREE(session,
                                          session->userauth_pswd_newpw);
                            return ssh2_err(session,

src/userauth.c:475

  • On OOM allocating the password-change request buffer, the function returns without resetting session->userauth_pswd_state to ssh2_NB_state_idle. This leaves the non-blocking state machine in sent1 with userauth_pswd_data_len == 0, so a retry can fail later with a confusing protocol error instead of restarting cleanly.
                            SSH2_ALLOC(session, data_len);
                        if(!session->userauth_pswd_data) {
                            SSH2_SAFEFREE(session,
                                          session->userauth_pswd_newpw);
                            return ssh2_err(session, LIBSSH2_ERROR_ALLOC,

@vszakats vszakats closed this in af1b501 Jul 18, 2026
@vszakats
vszakats deleted the unlen branch July 18, 2026 17:02
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 18, 2026
"On the out-of-bounds early-return path, the function returns without
resetting `session->userauth_pswd_state` back to `ssh2_NB_state_idle`.
If the caller retries after this error, the state machine can continue
in `sent1` with `userauth_pswd_data_len == 0`, leading to a misleading
follow-up `LIBSSH2_ERROR_PROTO` ("Unexpected packet size"). Reset the
state before returning on this failure path."

Reported by Copilot.
Bug: libssh2#2224 (comment)
vszakats added a commit that referenced this pull request Jul 18, 2026
Also: reset buffer length if password change callback returned NULL.

"On the out-of-bounds early-return path, the function returns without
resetting `session->userauth_pswd_state` back to `ssh2_NB_state_idle`.
If the caller retries after this error, the state machine can continue
in `sent1` with `userauth_pswd_data_len == 0`, leading to a misleading
follow-up `LIBSSH2_ERROR_PROTO` ("Unexpected packet size"). Reset the
state before returning on this failure path."

Reported by Copilot
Bug: #2224 (comment)

Closes #2344
vszakats added a commit that referenced this pull request Jul 25, 2026
This reverts an earlier change doing the opposite. `sizeof()` determines
length at build-time, while `strlen()` does the same at runtime. In case
of literals the results are identical, but the build-time option does it
with zero runtime overhead.

Also:
- fix typo in literal used for determining length. (length was correct.)
  Follow-up to bc4e619 #752
- move literals to the right-side of comparisons, where missing.

Follow-up to 88c5fd9 #2400
Follow-up to af1b501 #2224
Follow-up to 88a960a #1179

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