Skip to content

SSH connections leak memory #6598

@steven9724

Description

@steven9724

In _git_ssh_setup_conn, known_hosts is allocated by _git_ssh_session_create. However, known_hosts is only cleaned up in the case of error. From what I can tell, the value of known_hosts isn't stored anywhere and needs cleanup in _git_ssh_setup_conn in the case of success.

Reproduction steps

At the root of a repo with an SSH origin:

git_libgit2_init();
git_repository_open(&repo, ".");
git_remote_lookup(&remote, repo, "origin");
git_remote_connect(remote, GIT_DIRECTION_FETCH, &remoteCbs, NULL, NULL);
git_remote_disconnect(remote);
git_remote_free(remote);
git_repository_free(repo);
git_libgit2_shutdown();

Full example: libgit2-ssh-memleak.c.txt

Expected behavior

No memory leaked.

Actual behavior

==15372== 70,983 (24 direct, 70,959 indirect) bytes in 1 blocks are definitely lost in loss record 685 of 685
==15372==    at 0x4C29E83: malloc (vg_replace_malloc.c:299)
==15372==    by 0x4E56203: libssh2_knownhost_init (in /usr/lib64/libssh2.so.1.0.1)
==15372==    by 0x43D87A: _git_ssh_setup_conn (in ssh-memleak)
==15372==    by 0x438DC1: git_smart__connect (in ssh-memleak)
==15372==    by 0x41A614: git_remote_connect_ext (in ssh-memleak)
==15372==    by 0x41A857: git_remote_connect (in ssh-memleak)
==15372==    by 0x4050A8: main (in ssh-memleak)

Version of libgit2 (release number or SHA1)

1.6.4

Operating system(s) tested

Red Hat Enterprise Linux Server release 7.9 (Maipo)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions