Skip to content

scp: drain long C-response basenames past fixed buffer - #2408

Closed
SebTardif wants to merge 11 commits into
libssh2:masterfrom
SebTardif:fix/scp-recv-long-name
Closed

SebTardif wants to merge 11 commits into
libssh2:masterfrom
SebTardif:fix/scp-recv-long-name

Conversation

@SebTardif

Copy link
Copy Markdown
Contributor

Summary

libssh2_scp_recv2 fails with LIBSSH2_ERROR_SCP_PROTOCOL
("Unterminated response from SCP server") when the server SCP C line
exceeds the fixed 256-byte response buffer. Mode and size always fit early;
long basenames overflow the buffer.

Parse mode and size as soon as those fields are complete. If the buffer
fills before the trailing newline, drain the unused basename until \n
and continue with the SCP ACK. libssh2 does not use the basename.

Evidence

Fixes #1738

Copilot AI review requested due to automatic review settings July 23, 2026 14:05

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 23, 2026 14:35

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vszakats
vszakats requested a review from Copilot July 23, 2026 15:43
Comment thread src/scp.c Resolved
Comment thread src/scp.c Resolved
Comment thread tests/test_simple.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 3 out of 3 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (3)

tests/test_simple.c:154

  • These test failure messages print libssh2_int64_t using %lld, which is not portable on older MSVC. Use SSH2_INT64_T_FORMAT (defined in libssh2_priv.h) to format libssh2_int64_t consistently.
        fprintf(stderr, "scp_parse partial-name: prc=%d mode=%ld size=%lld\n",
                prc, mode, (long long)size);

tests/test_simple.c:184

  • This test failure message prints libssh2_int64_t using %lld. Use SSH2_INT64_T_FORMAT so the test program builds on older MSVC/Windows toolchains.
    }

    /* Malformed: bad mode */

src/scp.c:809

  • This trace log prints scpRecv_size (libssh2_int64_t) via a cast to long and %ld, which can truncate large file sizes and is inconsistent with other SCP logging/formatting in this file. Use SSH2_INT64_T_FORMAT and pass scpRecv_size directly.
        sb->st_mtime = session->scpRecv_mtime;
        sb->st_atime = session->scpRecv_atime;
        sb->st_size = (libssh2_struct_stat_size)session->scpRecv_size;

Comment thread tests/test_simple.c Resolved
Copilot AI review requested due to automatic review settings July 23, 2026 15:50

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 23, 2026 15:54

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SebTardif

Copy link
Copy Markdown
Contributor Author

Addressed review notes:

  • Dropped braces on single-statement if/while in ssh2_scp_parse_c_fields
  • Removed GitHub issue numbers from comments (kept in commit/Fixes)
  • Switched the long-name unit test prefix to ssh2_snprintf
  • Failure paths in the unit test now use SSH2_INT64_T_FORMAT instead of hard-coded %lld

Latest tip: 2a0d2eea

Comment thread tests/test_simple.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 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread src/scp.c Resolved
Comment thread src/scp.c Resolved
Comment thread tests/test_simple.c Resolved
Copilot AI review requested due to automatic review settings July 23, 2026 16:05

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/scp.c Resolved
Copilot AI review requested due to automatic review settings July 23, 2026 16:16

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread tests/test_simple.c Resolved
Comment thread tests/test_simple.c Resolved
Comment thread src/scp.c Resolved
Copilot AI review requested due to automatic review settings July 23, 2026 16:35

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 23, 2026 16: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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

tests/test_simple.c:176

  • prefix_len is derived from snprintf() without checking for a negative return or truncation. If snprintf() ever fails (negative) or returns a value >= sizeof(long_line), the cast to size_t can make memset(long_line + prefix_len, ...) underflow/overflow and write out of bounds. Since the prefix is a constant string, it’s safer to avoid snprintf() entirely and copy the literal with a known length.
    prefix_len = (size_t)snprintf((char *)long_line, sizeof(long_line),
                                  "C0644 99 ");
    memset(long_line + prefix_len, 'a',
           sizeof(long_line) - prefix_len);

vszakats added a commit that referenced this pull request Jul 23, 2026
To apply to all test apps. Prior to this patch it was set for fixtures only.

Ref: #2408 (comment)

Closes #2417
Comment thread tests/test_simple.c Resolved
Comment thread src/libssh2_priv.h Resolved
Comment thread tests/test_simple.c Resolved
SebTardif added 10 commits July 23, 2026 17:59
libssh2_scp_recv2 stored the server "Cmode size name\n" line in a fixed
256-byte session buffer. Mode and size always fit early, but a long
basename made the line exceed the buffer and fail with
LIBSSH2_ERROR_SCP_PROTOCOL ("Unterminated response from SCP server").

Parse mode and size as soon as those fields are complete. If the buffer
fills before the trailing newline, drain the unused basename until '\n'
and continue with the SCP ACK. Basename is not used by libssh2.

Add unit coverage in test_simple for the parser, including a full
SSH2_SCP_RESPONSE_BUFLEN buffer shaped like issue libssh2#1738.

Fixes libssh2#1738

Signed-off-by: Sebastien Tardif <[email protected]>
- Drop braces around single-statement if/while in ssh2_scp_parse_c_fields
- Remove GitHub issue numbers from comments
- Use ssh2_snprintf in the long-name unit test (MSVC-compatible)

Signed-off-by: Sebastien Tardif <[email protected]>
Avoid hard-coded %lld for libssh2_int64_t so older MSVC toolchains
print sizes correctly (matches scp.c and SSH2_INT64_T_FORMAT).

Signed-off-by: Sebastien Tardif <[email protected]>
Maintainer note: ssh2_snprintf is not available to tests without a
build overhaul. Build the long-name fixture with memcpy, and print
sizes with %lu + unsigned long cast for portable test fprintf.

Also report "Invalid response" when a full buffer parses as malformed
rather than always "Unterminated".

Signed-off-by: Sebastien Tardif <[email protected]>
Standard snprintf is available in tests (maps to _snprintf on old MSVC).
Fill the remaining basename bytes with memset.

Signed-off-by: Sebastien Tardif <[email protected]>
libssh2_setup.h only maps snprintf to _snprintf when LIBSSH2_TESTS is
set (or when not building the library). test_simple includes
libssh2_priv.h directly without that define, so VS2010 treated
snprintf as undefined under /WX.

Match session_fixture.h: define LIBSSH2_TESTS before the priv header.

Signed-off-by: Sebastien Tardif <[email protected]>
- Parse buffer as const char * (fewer casts at call sites / tests)
- Check snprintf return before filling the long-name fixture
- Single-line memset for the basename fill
- Drop local LIBSSH2_TESTS define (build sets it for all tests after libssh2#2417)

Signed-off-by: Sebastien Tardif <[email protected]>
Copilot AI review requested due to automatic review settings July 23, 2026 22:01
@SebTardif
SebTardif force-pushed the fix/scp-recv-long-name branch from 9ec3260 to 7d75c1f Compare July 23, 2026 22:01

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SebTardif

Copy link
Copy Markdown
Contributor Author

Addressed the open review notes and rebased onto current master (includes #2417 / #2418):

  • ssh2_scp_parse_c_fields now takes const char *
  • snprintf return checked before the long-name fill
  • single-line memset
  • dropped local LIBSSH2_TESTS define

Tip: 7d75c1f4

Copilot AI review requested due to automatic review settings July 23, 2026 22:56

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

@vszakats vszakats closed this in aa54fdd Jul 29, 2026
@vszakats

Copy link
Copy Markdown
Member

Thank you, merged now!

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.

scp_recv2 - cannot download file with a name longer than 246 chars

3 participants