Skip to content

Buffer problem in ssh agent forwarding #1025

@payne1982

Description

@payne1982

AI found this bug in version 0.2.19 when I was trying to enable ssh agent forwarding. I tested the fix and it works for me now.

Bug: Buffer.skip() misuse in ChannelAgentForwarding.write() corrupts message framing

Where: ChannelAgentForwarding.java, method write(), the else branch handling unknown message types.

When it manifests: When the SSH client sends SSH2_AGENTC_EXTENSION (type 27), introduced in OpenSSH 8.x for session binding. Prior to OpenSSH 8.x this else branch was effectively dead code, so the bug went unnoticed.

Effect: One byte of the current message body is left unread in rbuf. On the next write() call it is prepended to the incoming data and interpreted as the first byte of the next message's 4-byte length field, completely corrupting its parsing (in our case producing a spurious type 115 instead of the expected type
11 SSH2_AGENTC_REQUEST_IDENTITIES, which caused agent forwarding to fail with "agent refused operation").

Root cause: Buffer.skip(n) performs index += n, advancing the write pointer, not the read pointer (s). The name "skip" is misleading for anyone expecting it to discard already-received bytes. The correct fix is to advance s directly:

- rbuf.skip(rbuf.getLength() - 1);
+ rbuf.s += rbuf.getLength();
mbuf.putByte(SSH_AGENT_FAILURE);

Note the additional - 1 in the original, which makes the off-by-one worse: even if skip() had advanced s, it would still have left one byte behind.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions