From d0a0210bd42e63f39eaf4ccf983697fcd4422982 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Thu, 23 Jul 2026 20:11:00 +0200 Subject: [PATCH] agent: fix potential infinite loop while receiving data from SSH agent If the local SSH agent terminates while we are receiving data from it. Refs: https://learn.microsoft.com/windows/win32/api/fileapi/nf-fileapi-readfile#pipes https://linux.die.net/man/2/recv https://www.man7.org/linux/man-pages/man2/recv.2.html Reported-by: afldl on github Closes #2414 --- src/agent.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/agent.c b/src/agent.c index ce4ce16e..3a15183e 100644 --- a/src/agent.c +++ b/src/agent.c @@ -425,11 +425,14 @@ cleanup: int rc; \ \ while(*(total) < (length)) { \ - if(!(agent)->pending_io) \ + if(!(agent)->pending_io) { \ ret = (func)((agent)->pipe, (char *)(buffer) + *(total), \ (DWORD)((length) - *(total)), \ &bytes_transferred, \ &(agent)->overlapped); \ + if(ret && !bytes_transferred) \ + return LIBSSH2_ERROR_SOCKET_DISCONNECT; \ + } \ else \ ret = GetOverlappedResult((agent)->pipe, \ &(agent)->overlapped, \ @@ -619,6 +622,8 @@ static int agent_connect_unix(LIBSSH2_AGENT *agent) flags, abstract); \ if(rc < 0) \ return rc; \ + else if(!rc) \ + return LIBSSH2_ERROR_SOCKET_DISCONNECT; \ \ finished += rc; \ } \