Skip to content

libssh2 uses memset to zero memory #120

Closed

Description

@Keno

As noted in #106, libssh2 uses memset to zero memory. Compiler are free to (and happily do) optimize those calls away. In order to be sure that the memory is actually zero'd, memset_s/SecureZeroMemory (if available), or perhaps a simple loop with store through a volatile pointer should be used.

Activity

  1. mback2k commented on Aug 14, 2016

    @mback2k
    Member

    The WinCNG backend already provides a method to securely free memory:
    https://github.com/libssh2/libssh2/blob/master/src/wincng.c#L311
    Maybe such a method could be made available by all crypto backends for general use across libssh2?

  2. willco007 commented on Apr 27, 2018

    @willco007
    Member

    I've added _libssh2_explicit_zero() to do just this included in pull request #248. It uses SecureZeroMemory or memset_s followed by the volatile pointer if neither is available.

  3. willco007 commented on Apr 27, 2018

    @willco007
    Member

    I should note, existing code needs to be updated to use the new API once the pull request is approved. Edit: scrubbed code for memset using 0, made required changes.

  4. added a commit that references this issue on Aug 2, 2018
  5. mback2k commented on Aug 26, 2018

    @mback2k
    Member

    @willco007 Was the existing code in the WinCNG backend updated? I am getting the unsupported warning with WinCNG, even though it did implement this functionality first.

    And I am also getting this error while building with classic MinGW:

    pem.c: In function '_libssh2_openssh_pem_parse_data':
    pem.c:614:25: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
             out_buf->data = malloc(decrypted.len);
                             ^~~~~~
    pem.c:614:25: warning: incompatible implicit declaration of built-in function 'malloc'
    pem.c:614:25: note: include '<stdlib.h>' or provide a declaration of 'malloc'
    

    Error source: https://libssh2build.uxnr.de/admin/builders/libssh2_wincng_mingw_x86/builds/1715/steps/compile/logs/stdio

  6. willco007 commented on Aug 27, 2018

    @willco007
    Member

    @mback2k I've updated the call in pem.c to use LIBSSH2_CALLOC() instead of malloc directly which should fix your warning there.

    The new zeroing memory is done in common code such has kex.c and pem.c in locations where libssh2 was using memset 0 which is a no-op on some platforms. I did not change the way specific backends work. My suggestion would be to soft deprecate the clear memory build flag and backends always zero sensitive memory, if needed, using the new API _libssh2_explicit_zero(). Hope that helps clarify things a bit.

  7. mback2k commented on Aug 31, 2018

    @mback2k
    Member

    @willco007 Thanks for your fix and explanation. Unfortunately SecureZeroMemory is not declared in legacy MinGW, so you would either have to check for it being there and disable the feature if not or test if adding a fallback declaration in the header could help, see here.

  8. willco007 commented on Aug 31, 2018

    @willco007
    Member

    I am testing for it in _libssh2_explicit_zero(), using the define HAVE_DECL_SECUREZEROMEMORY; assuming it works correctly.

  9. mback2k commented on Sep 2, 2018

    @mback2k
    Member

    Thanks for the information, I pushed some commits to fix compatibility with legacy MinGW / Windows XP.

  10. added a commit that references this issue on Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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