Repository navigation
Conversation
There was a problem hiding this comment.
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
*_lenfields until after successful allocations in multiple userauth flows. - Explicitly reset some length fields to
0when freeing/reallocating buffers. - Use local
size_tvariables (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.
There was a problem hiding this comment.
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_statetossh2_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()overwritessession->userauth_pblc_method(it sets*method = pubkey). In this code path,session->userauth_pblc_methodmay 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 callinguserauth_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
publickeydatais provided,userauth_read_blob_pubkey()allocates and returns decodedpubkeydata(base64-decoded blob). That buffer is not freed on any return path here, which leaks memory; onlytmp_publickeydatais freed. Add a conditional free forpubkeydatawhen it is nottmp_publickeydata.
return rc;
}
| SSH2_SAFEFREE(session, | ||
| session->userauth_pswd_newpw); | ||
| return ssh2_err(session, | ||
| LIBSSH2_ERROR_OUT_OF_BOUNDARY, | ||
| "Username or password too large"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_methodmay 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/resetsession->userauth_pblc_methodbefore callinguserauth_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
There was a problem hiding this comment.
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_stateback tossh2_NB_state_idle. Since this runs while in the password-change flow (ssh2_NB_state_sent1), a retry after this error can continue insent1and then fail with a misleadingLIBSSH2_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_statetossh2_NB_state_idle. This leaves the non-blocking state machine insent1withuserauth_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,
"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)
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
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
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_BOUNDARYinstead of conflating itwith
LIBSSH2_ERROR_ALLOCinuserauth_keyboard_interactive(),and
userauth_password().fix to verify method sizes before calling
strncmp()inlibssh2_userauth_publickey_sk()Follow-up to ed439a2 Add support for [email protected] and… #698
replace
strlen()calls withsizeof()for efficiency inlibssh2_userauth_publickey_sk()Follow-up to ed439a2 Add support for [email protected] and… #698
fix error message text in
userauth_keyboard_interactive().Reported by Copilot.
Bug: userauth: keep buffer lengths consistent with pointers on fail, nearby fixes #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: userauth: keep buffer lengths consistent with pointers on fail, nearby fixes #2224 (comment)
Bug: #2220 (comment)
https://github.com/libssh2/libssh2/pull/2224/files?w=1