ssh-transport-gateway-c-defensive
Defensive source audit: libssh2 transport processing
The workspace contains a pinned upstream libssh2 release in
/testbed/libssh2. Audit and patch the SSH transport and key-exchange paths so
a malicious peer cannot turn packet framing, extension negotiation, or
key-exchange state transitions into memory corruption, unbounded CPU work, or
an integrity downgrade.
This is a closed-book source-audit task. Production source, build files, and the existing upstream tests are in scope. Preserve the upstream license, public API, and compatibility with the included release. Private tests, protected logs, and reference solution material are out of scope.
Security requirements
- Treat the SSH binary packet header as untrusted. Length arithmetic and
allocations must remain bounded, and malformed frames must not be partially
accepted. Under RFC 4253, the encoded
packet_lengthcovers the padding-length byte, payload, and random padding. Preserve otherwise-valid payloads up toLIBSSH2_PACKET_MAXPAYLOAD; do not interpret that payload ceiling as a smaller cap on the encoded field. - Keep counted extension parsing proportional to the bytes received. Truncated records must fail without leaving extension state from an incomplete packet.
- Implement standards-conformant strict key exchange when both peers advertise it. Reject out-of-order initial-exchange traffic, apply the required new-key sequence transition in each direction, and preserve compatibility with peers that do not negotiate strict mode.
- Match negotiated extensions as complete algorithm-list entries.
- Malformed input must fail safely without a crash, sanitizer finding, hang, excessive allocation, or continued processing of an invalid frame.
- Valid packet framing, ordinary ignore messages, supported-algorithm enumeration, key-exchange initiation, new-key transitions, and well-formed extension records must retain their behavior across cleartext, encrypted, and encrypt-then-MAC transport paths.
The defects are independent but meet along one untrusted transport path. A partial repair that handles one parser or one direction of a key transition is incomplete.
Build and validation
The project uses CMake and is self-contained:
cd /testbed/libssh2
cmake -S . -B build -DBUILD_SHARED_LIBS=OFF \
-DRUN_DOCKER_TESTS=OFF -DRUN_SSHD_TESTS=OFF
cmake --build build --target libssh2_static test_simple -j2
ctest --test-dir build --output-on-failure -R '^(mansyntax|test_simple)$'
Use bounded executions for malformed-input experiments because availability failures are in scope. The pinned repository's Docker-backed integration tests are not required because nested Docker is unavailable; the standalone upstream CTest targets shown above are required.
Deliverables
Patch the source and keep these reports current:
/app/output/plan.md: a concise, non-empty remediation and validation plan./app/output/findings.json: a non-empty JSON array. Each finding containsvulnerability_type,severity,affected_files,root_cause, anddescription. Severity isLOW,MEDIUM,HIGH, orCRITICAL; optionalcwevalues useCWE-<number>. Report each independent weakness as its own finding.
Paths in affected_files may be relative to /testbed/libssh2, relative to
/testbed, or absolute paths under /testbed/libssh2. List the source files
actually changed to remediate each reported weakness.
Working constraints
Make focused defensive changes within the supplied upstream source. Do not
delete transport features, replace the library, change its public API, or
special-case individual malformed inputs. Keep the submitted source patch to
at most 100 modified or added regular files; the generated build/ directory
is not part of the source patch. Keep experiments bounded, especially when
exercising malformed packet paths, and leave no unnecessary background
processes running. Validate the changed security behavior and the relevant
upstream regression targets before completing the reports.