libssh2 icon indicating copy to clipboard operation
libssh2 copied to clipboard

We have a memory leak at src/transport.c:474

Open timelyslot opened this issue 5 years ago • 11 comments

we find libssh2-1.9.0/src/transport.c:474(p->payload = LIBSSH2_ALLOC(session, total_num);), always LIBSSH2_ALLOC without LIBSSH2_FREE it, and memory leak. 
Our program is used for SFTP transmission with 20 threads. Due to the demand, we will not close the session after uploading the file and maintain a long-time connection with the server by keeping it alive. After running for a long time, it is found that libssh2_ sftp_ open_ Ex has a memory leak, and finally locate p - > payload = libsh2_ ALLOC(session, total_ num); We want to know whether there is a wrong method or something wrong
  • OS: linux
  • libssh2 version 1.10.0

timelyslot avatar Sep 02 '21 03:09 timelyslot

There are a couple places where p->payload is released, the main one is in the function fullpacket. Can you step through your code and make sure it's being hit as expected.

willco007 avatar Sep 02 '21 03:09 willco007

Through the test, it is found that when the keepalive is configured with want_reply = 1, the memory leak will occur at the next time which _libssh2_transport_read(libssh2_sftp_open_ex) called. I guess the message from the server is not processed, while allocaing space mistakenly during the next reading. Can you tell us how to deal with this situation

timelyslot avatar Sep 03 '21 09:09 timelyslot

I see also the same memory leak. In our company we have a single sftp thread, for which we open and close sessions repeatedly.

emiliopastormira avatar Oct 13 '21 06:10 emiliopastormira

Hi, I could solve the leak as we see it with the following patch

--- a/src/session.c 2021-10-11 15:29:54.768257700 +0100
+++ b/src/session.c 2021-10-13 06:51:50.788974100 +0100
@@ -1053,7 +1053,7 @@
}



/* Free payload buffer */
- if(session->packet.total_num) {
+ if(session->packet.payload) {
    LIBSSH2_FREE(session, session->packet.payload);
  }

As I see it in closing a session, payload would need definitely to be freed, and a pointer should be NOT null, only if in use.

However, I would think that the line may need a change from LIBSSH2_ALLOC to LIBSSH2_REALLOC if that part of the code is going to be used multiple times (as my analysis indicated at first glance) I could not fully test my second suggestion, sorry.

emiliopastormira avatar Oct 14 '21 12:10 emiliopastormira

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 16 '22 19:04 stale[bot]

I encounter the same memory leak in mly company with version 1.11.0 ...

cloud4t0r avatar May 27 '24 10:05 cloud4t0r

May be fixed with https://github.com/libssh2/libssh2/pull/1412, please test it and let us know.

willco007 avatar Jun 25 '24 18:06 willco007

#1412 is not fixing this issue but can be included in the #1800 (this MR is avoiding a double-free that is not happening due to the wrong check as mentioned by @emiliopastormira)

LarsNordin-LNdata avatar Feb 13 '26 08:02 LarsNordin-LNdata

PR #1804 fails with double free, the fix is not that easy and I need to dig into how it is supposed to work.

LarsNordin-LNdata avatar Feb 18 '26 07:02 LarsNordin-LNdata

I found the problem, the _libssh2_packet_add() free the parameter data and is p->payload when called from fullpacket() in transport.c.

Adding a p->payload = NULL if not ERROR_EAGAIN solves the problem:

if(session->fullpacket_state == libssh2_NB_state_created) {
        rc = _libssh2_packet_add(session, p->payload,
                                 session->fullpacket_payload_len,
                                 session->fullpacket_macstate, seq);
        if(rc == LIBSSH2_ERROR_EAGAIN)
            return rc;
        /* p->payload is FREE:ed in packet_add. */
        p->payload = NULL; 
        if(rc) {
            session->fullpacket_state = libssh2_NB_state_idle;
            return rc;
        }
    }

I will do some more testing before adding this to PR #1804

LarsNordin-LNdata avatar Feb 18 '26 11:02 LarsNordin-LNdata

PR #2054 addresses this issue: clears p->payload after ssh2_packet_add() frees it, and guards session cleanup with a NULL check instead of total_num.

obhor avatar Jun 14 '26 15:06 obhor