Skip to content

server: do not dispatch packets that failed to unmarshal - #694

Merged
puellanivis merged 1 commit into
pkg:masterfrom
likitha011:server-drop-malformed-packets
Oct 19, 2026
Merged

puellanivis merged 1 commit into
pkg:masterfrom
likitha011:server-drop-malformed-packets

Conversation

@likitha011

Copy link
Copy Markdown
Contributor

Repro: send an SSH_FXP_MKDIR to Server with the trailing attrs field cut off (path intact). The server logs the decode error and closes the connection, but the directory is still created.
Cause: on a makePacket error, Serve calls svr.conn.Close() and then a bare break, which only leaves the switch. The partially decoded packet is still queued and run by a worker with its missing fields set to zero values. A rename/symlink/hardlink/posix-rename with the second path missing runs against "", which toLocalPath turns into the working directory when WithServerWorkingDirectory is set.
Fix: break out of the receive loop, as RequestServer.serveLoop already does, so the malformed packet is dropped and Serve returns the decode error. TestServerDoesNotExecuteMalformedPacket fails without the change.

When makePacket fails with anything other than an unknown extended
request, Serve closes the connection and then uses a bare break, which
only leaves the switch. The partially decoded packet is still queued
and executed by a worker with its missing fields left at their zero
values, so a truncated SSH_FXP_MKDIR still creates the directory and a
rename/symlink/link whose second path is cut off runs against "" (the
working directory when one is configured).

Break out of the receive loop instead, as RequestServer already does,
so the malformed packet is dropped and Serve returns the decode error.
@puellanivis

Copy link
Copy Markdown
Collaborator

Should we entirely disconnect in such a situation, which is potentially recoverable, or should we reply to the client with a SSH_FX_BAD_MESSAGE and continue?

@likitha011

Copy link
Copy Markdown
Contributor Author

I'd keep the disconnect, at least for this PR. RequestServer.serveLoop already drops the connection on the same error, and OpenSSH's sftp-server calls fatal() on any parse failure, so a client that gets this far is already off the rails and the behaviour matches what it would see elsewhere.

Replying isn't quite free either: the half-decoded packet doesn't always carry a usable ID (errShortPacket in the first four bytes leaves it 0, and an unhandled type gives no packet at all), and the status would need to go through pktMgr rather than straight out of the receive loop to keep response ordering.

That said, if you'd rather keep the session alive I'm happy to switch both servers to answering SSH_FX_BAD_MESSAGE and carrying on. I just didn't want to change behaviour beyond the bug here.

@puellanivis
puellanivis merged commit 5771a0d into pkg:master Oct 19, 2026
4 checks passed
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