Changeset View
Changeset View
Standalone View
Standalone View
secmem/secmem.c
| Show All 25 Lines | |||||
| #include <errno.h> | #include <errno.h> | ||||
| #include <stdarg.h> | #include <stdarg.h> | ||||
| #include <unistd.h> | #include <unistd.h> | ||||
| #if defined(HAVE_MLOCK) || defined(HAVE_MMAP) | #if defined(HAVE_MLOCK) || defined(HAVE_MMAP) | ||||
| # include <sys/mman.h> | # include <sys/mman.h> | ||||
| # include <sys/types.h> | # include <sys/types.h> | ||||
| # include <fcntl.h> | # include <fcntl.h> | ||||
| #endif | #endif | ||||
| #if HAVE_W32_SYSTEM | |||||
| #include <windows.h> | |||||
| #endif | |||||
| #include <string.h> | #include <string.h> | ||||
| #include "secmem.h" | #include "secmem.h" | ||||
| #ifdef ORIGINAL_GPG_VERSION | #ifdef ORIGINAL_GPG_VERSION | ||||
| #include "types.h" | #include "types.h" | ||||
| #include "util.h" | #include "util.h" | ||||
| #else /* ORIGINAL_GPG_VERSION */ | #else /* ORIGINAL_GPG_VERSION */ | ||||
| ▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | |||||
| static void *pool; | static void *pool; | ||||
| static volatile int pool_okay; /* may be checked in an atexit function */ | static volatile int pool_okay; /* may be checked in an atexit function */ | ||||
| #if HAVE_MMAP | #if HAVE_MMAP | ||||
| static int pool_is_mmapped; | static int pool_is_mmapped; | ||||
| #endif | #endif | ||||
| #if HAVE_W32_SYSTEM | |||||
| static int pool_is_virtualalloc; | |||||
| #endif | |||||
| static size_t poolsize; /* allocated length */ | static size_t poolsize; /* allocated length */ | ||||
| static size_t poollen; /* used length */ | static size_t poollen; /* used length */ | ||||
| static MEMBLOCK *unused_blocks; | static MEMBLOCK *unused_blocks; | ||||
| static unsigned max_alloced; | static unsigned max_alloced; | ||||
| static unsigned cur_alloced; | static unsigned cur_alloced; | ||||
| static unsigned max_blocks; | static unsigned max_blocks; | ||||
| static unsigned cur_blocks; | static unsigned cur_blocks; | ||||
| static int disable_secmem; | static int disable_secmem; | ||||
| Show All 40 Lines | #endif | ||||
| if( errno != EPERM | if( errno != EPERM | ||||
| #ifdef EAGAIN /* OpenBSD returns this */ | #ifdef EAGAIN /* OpenBSD returns this */ | ||||
| && errno != EAGAIN | && errno != EAGAIN | ||||
| #endif | #endif | ||||
| ) | ) | ||||
| log_error("can't lock memory: %s\n", strerror(errno)); | log_error("can't lock memory: %s\n", strerror(errno)); | ||||
| show_warning = 1; | show_warning = 1; | ||||
| } | } | ||||
| #elif HAVE_W32_SYSTEM | |||||
| if( !VirtualLock( p, n ) ) { | |||||
| log_error("can't lock memory: VirtualLock failed, (error %lu)\n", | |||||
| GetLastError()); | |||||
| show_warning = 1; | |||||
| } | |||||
| #else | #else | ||||
| (void)p; | (void)p; | ||||
| (void)n; | (void)n; | ||||
| log_info("Please note that you don't have secure memory on this system\n"); | log_info("Please note that you don't have secure memory on this system\n"); | ||||
| #endif | #endif | ||||
| } | } | ||||
| static void | static void | ||||
| init_pool( size_t n) | init_pool( size_t n) | ||||
| { | { | ||||
| #if HAVE_MMAP | #if HAVE_MMAP | ||||
| size_t pgsize; | size_t pgsize; | ||||
| #elif HAVE_W32_SYSTEM | |||||
| size_t pgsize; | |||||
| SYSTEM_INFO si; | |||||
| #endif | #endif | ||||
| poolsize = n; | poolsize = n; | ||||
| if( disable_secmem ) | if( disable_secmem ) | ||||
| log_bug("secure memory is disabled"); | log_bug("secure memory is disabled"); | ||||
| #if HAVE_MMAP | #if HAVE_MMAP | ||||
| Show All 25 Lines | # endif | ||||
| if( pool == (void*)-1 ) | if( pool == (void*)-1 ) | ||||
| log_info("can't mmap pool of %u bytes: %s - using malloc\n", | log_info("can't mmap pool of %u bytes: %s - using malloc\n", | ||||
| (unsigned)poolsize, strerror(errno)); | (unsigned)poolsize, strerror(errno)); | ||||
| else { | else { | ||||
| pool_is_mmapped = 1; | pool_is_mmapped = 1; | ||||
| pool_okay = 1; | pool_okay = 1; | ||||
| } | } | ||||
| #elif HAVE_W32_SYSTEM | |||||
| GetSystemInfo(&si); | |||||
| pgsize = si.dwPageSize; | |||||
| poolsize = (poolsize + pgsize -1 ) & ~(pgsize - 1); | |||||
| pool = VirtualAlloc(NULL, poolsize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | |||||
| if( !pool ) { | |||||
| log_info("can't VirtualAlloc memory pool of %u bytes: %lu - using malloc\n", | |||||
| (unsigned)poolsize, GetLastError()); | |||||
| } | |||||
| else { | |||||
| pool_is_virtualalloc = 1; | |||||
| pool_okay = 1; | |||||
| } | |||||
| #endif | #endif | ||||
| if( !pool_okay ) { | if( !pool_okay ) { | ||||
| pool = malloc( poolsize ); | pool = malloc( poolsize ); | ||||
| if( !pool ) | if( !pool ) | ||||
| log_fatal("can't allocate memory pool of %u bytes\n", | log_fatal("can't allocate memory pool of %u bytes\n", | ||||
| (unsigned)poolsize); | (unsigned)poolsize); | ||||
| else | else | ||||
| pool_okay = 1; | pool_okay = 1; | ||||
| ▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | return; | ||||
| wipememory2( pool, 0xff, poolsize); | wipememory2( pool, 0xff, poolsize); | ||||
| wipememory2( pool, 0xaa, poolsize); | wipememory2( pool, 0xaa, poolsize); | ||||
| wipememory2( pool, 0x55, poolsize); | wipememory2( pool, 0x55, poolsize); | ||||
| wipememory2( pool, 0x00, poolsize); | wipememory2( pool, 0x00, poolsize); | ||||
| #if HAVE_MMAP | #if HAVE_MMAP | ||||
| if( pool_is_mmapped ) | if( pool_is_mmapped ) | ||||
| munmap( pool, poolsize ); | munmap( pool, poolsize ); | ||||
| #elif HAVE_W32_SYSTEM | |||||
| if( pool_is_virtualalloc ) { | |||||
| VirtualUnlock( pool, poolsize ); | |||||
| VirtualFree( pool, 0, MEM_RELEASE ); | |||||
| } | |||||
| #endif | #endif | ||||
| pool = NULL; | pool = NULL; | ||||
| pool_okay = 0; | pool_okay = 0; | ||||
| poolsize=0; | poolsize=0; | ||||
| poollen=0; | poollen=0; | ||||
| unused_blocks=NULL; | unused_blocks=NULL; | ||||
| } | } | ||||
| Show All 18 Lines | |||||