Skip to content

Commit c6172e3

Browse files
committed
add win32 logic for mmap and munmap
1 parent 3fd5c9f commit c6172e3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sshkey.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#include "includes.h"
2929

3030
#include <sys/types.h>
31+
#ifndef WINDOWS
3132
#include <sys/mman.h>
33+
#endif
3234
#include <netinet/in.h>
3335

3436
#ifdef WITH_OPENSSL
@@ -67,6 +69,7 @@
6769
#endif
6870
#ifdef WINDOWS
6971
#include <lmcons.h>
72+
#include <Windows.h>
7073
#endif
7174

7275
#include "openbsd-compat/openssl-compat.h"
@@ -774,12 +777,20 @@ sshkey_prekey_alloc(u_char **prekeyp, size_t len)
774777
u_char *prekey;
775778

776779
*prekeyp = NULL;
780+
#ifdef WINDOWS
781+
prekey = VirtualAlloc(NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
782+
if (prekey == NULL) {
783+
return SSH_ERR_SYSTEM_ERROR;
784+
}
785+
VirtualLock(prekey, len);
786+
#else
777787
if ((prekey = mmap(NULL, len, PROT_READ|PROT_WRITE,
778788
MAP_ANON|MAP_PRIVATE|PREKEY_MMAP_FLAG, -1, 0)) == MAP_FAILED)
779789
return SSH_ERR_SYSTEM_ERROR;
780790
#if defined(MADV_DONTDUMP) && !defined(MAP_CONCEAL) && !defined(MAP_NOCORE)
781791
(void)madvise(prekey, len, MADV_DONTDUMP);
782792
#endif
793+
#endif /* WINDOWS */
783794
*prekeyp = prekey;
784795
return 0;
785796
}
@@ -789,7 +800,13 @@ sshkey_prekey_free(void *prekey, size_t len)
789800
{
790801
if (prekey == NULL)
791802
return;
803+
#ifdef WINDOWS
804+
SecureZeroMemory(prekey, len);
805+
VirtualUnlock(prekey, len);
806+
VirtualFree(prekey, 0, MEM_RELEASE);
807+
#else
792808
munmap(prekey, len);
809+
#endif /* WINDOWS */
793810
}
794811

795812
static void

0 commit comments

Comments
 (0)