From 5e563eba459680ef25cd269ac33ae980a4ea8189 Mon Sep 17 00:00:00 2001 From: Machiel Sleeuwaert Date: Tue, 22 Oct 2024 22:50:15 +0200 Subject: [PATCH] Do not attempt to read data if no data is left --- openssh/agent.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/openssh/agent.go b/openssh/agent.go index 08456f8..9bd078f 100644 --- a/openssh/agent.go +++ b/openssh/agent.go @@ -66,12 +66,18 @@ func QueryAgent(pipeName string, buf []byte) (result []byte, err error) { return append(messageSizeBuf, replyCode...), nil } + // replyCode has been read + remainingMessageSize := messageSize-1 + // https://datatracker.ietf.org/doc/html/draft-miller-ssh-agent-04#section-3 - messageContents := make([]byte, messageSize-1) - _, err = conn.Read(messageContents) - if err != nil { - fmt.Printf("cannot read message contents from pipe %s: %s\n", pipeName, err.Error()) - return append(messageSizeBuf, SSH_AGENT_FAIL), nil + messageContents := make([]byte, remainingMessageSize) + + if(remainingMessageSize > 0) { + _, err = conn.Read(messageContents) + if err != nil { + fmt.Printf("cannot read message contents from pipe %s: %s\n", pipeName, err.Error()) + return append(messageSizeBuf, SSH_AGENT_FAIL), nil + } } concatResults := append(messageSizeBuf, replyCode...)