]> granicus.if.org Git - libexpat/commitdiff
replace loadlibrary.c and RtlGenRandom call with rand_s() on windows
authorDavid Loffredo <loffredo@steptools.com>
Wed, 7 Aug 2019 17:43:38 +0000 (19:43 +0200)
committerSebastian Pipping <sebastian@pipping.org>
Wed, 7 Aug 2019 17:43:38 +0000 (19:43 +0200)
Signed-off-by: David Loffredo <loffredo@steptools.com>
12 files changed:
expat/CMakeLists.txt
expat/lib/Makefile.am
expat/lib/expat.vcxproj
expat/lib/expat.vcxproj.filters
expat/lib/expat_static.vcxproj
expat/lib/expat_static.vcxproj.filters
expat/lib/expatw.vcxproj
expat/lib/expatw.vcxproj.filters
expat/lib/expatw_static.vcxproj
expat/lib/expatw_static.vcxproj.filters
expat/lib/loadlibrary.c [deleted file]
expat/lib/xmlparse.c

index 4b743cdf5cf1c944334e02d9e1bea8615ade2dab..e2098e0e427f67cc7e319665fa3a6ef5f6444de2 100644 (file)
@@ -175,7 +175,6 @@ if(WIN32)
 endif(WIN32)\r
 \r
 set(expat_SRCS\r
-    lib/loadlibrary.c\r
     lib/xmlparse.c\r
     lib/xmlrole.c\r
     lib/xmltok.c\r
index 534369e8af20ffb1352085bc74a7ea1b4d4bf5c4..8cb451ed464781edb203153c154687bb31865a74 100644 (file)
@@ -40,7 +40,6 @@ libexpat_la_LDFLAGS = \
     -version-info @LIBCURRENT@:@LIBREVISION@:@LIBAGE@
 
 libexpat_la_SOURCES = \
-    loadlibrary.c \
     xmlparse.c \
     xmltok.c \
     xmlrole.c
index 69cb23356e071cb9e49590fe1b7adca7faf94950..609c816f76f6292e2f766db4997408fc0942a798 100644 (file)
     <CustomBuild Include="libexpat.def" />
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c" />
     <ClCompile Include="xmlparse.c">
       <ExceptionHandling Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
       </ExceptionHandling>
index 61c52b58d882dea1c2843e0ccd9a69d13272a33b..34aea26dd1e82c45bb9ada2d988b15d64b6c53cc 100644 (file)
@@ -15,9 +15,6 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="xmlparse.c">
       <Filter>Source Files</Filter>
     </ClCompile>
index 825fc51891e961ecb8496875d9a291d98663ea7f..b09020c0573ab631942368fd12209a463bf52606 100644 (file)
     </Lib>
   </ItemDefinitionGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c" />
     <ClCompile Include="xmlparse.c" />
     <ClCompile Include="xmlrole.c" />
     <ClCompile Include="xmltok.c" />
index a2fe03e63514fafaa2aabd1271310d885cc42a8d..e6d9d80fe2ec4e5f5534da8cd1777b2e629e0dd1 100644 (file)
@@ -11,9 +11,6 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="xmlparse.c">
       <Filter>Source Files</Filter>
     </ClCompile>
index de8a0f8c1380a8708e250e5ea67f7f4d85b85590..94783ac4fe152d98f460b4a2074e9a04dcb63993 100644 (file)
     <CustomBuild Include="libexpatw.def" />
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c" />
     <ClCompile Include="xmlparse.c">
       <ExceptionHandling Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
       </ExceptionHandling>
index fb3909c9953a35ba2635683162a736b9a5576ba2..dc6e96852f6ffc9d1563ab28c3ee6203c9bc2179 100644 (file)
@@ -15,9 +15,6 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="xmlparse.c">
       <Filter>Source Files</Filter>
     </ClCompile>
index d195b1b13141b8136d2b5477778ba6921be3fb61..0f7be78c2a3e712e90369a0cf17107b7e8861e19 100644 (file)
     </Lib>
   </ItemDefinitionGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c" />
     <ClCompile Include="xmlparse.c" />
     <ClCompile Include="xmlrole.c" />
     <ClCompile Include="xmltok.c" />
index 724d9f8ce0508c28fbbc85aa091761235f006ed2..83a7c08c91cd3ffa74c1d2c3c4d42cd6fdad297b 100644 (file)
@@ -11,9 +11,6 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="loadlibrary.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="xmlparse.c">
       <Filter>Source Files</Filter>
     </ClCompile>
diff --git a/expat/lib/loadlibrary.c b/expat/lib/loadlibrary.c
deleted file mode 100644 (file)
index 6c0ec89..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/***************************************************************************
- *                                  _   _ ____  _
- *  Project                     ___| | | |  _ \| |
- *                             / __| | | | |_) | |
- *                            | (__| |_| |  _ <| |___
- *                             \___|\___/|_| \_\_____|
- *
- * Copyright (C) 2016 - 2017, Steve Holme, <steve_holme@hotmail.com>.
- * Copyright (C) 2017, Expat development team
- *
- * All rights reserved.
- * Licensed under the MIT license:
- *
- * Permission to  use, copy,  modify, and distribute  this software  for any
- * purpose with  or without fee is  hereby granted, provided that  the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
- * EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
- * MERCHANTABILITY, FITNESS FOR A  PARTICULAR PURPOSE AND NONINFRINGEMENT OF
- * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
- * CONTRACT, TORT OR  OTHERWISE, ARISING FROM, OUT OF OR  IN CONNECTION WITH
- * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Except as contained in this notice,  the name of a copyright holder shall
- * not be used in advertising or otherwise to promote the sale, use or other
- * dealings  in this  Software without  prior written  authorization of  the
- * copyright holder.
- *
- ***************************************************************************/
-
-#if defined(_WIN32)
-
-#  include <windows.h>
-#  include <tchar.h>
-
-HMODULE _Expat_LoadLibrary(LPCTSTR filename);
-
-#  if ! defined(LOAD_WITH_ALTERED_SEARCH_PATH)
-#    define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
-#  endif
-
-#  if ! defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
-#    define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
-#  endif
-
-/* We use our own typedef here since some headers might lack these */
-typedef HMODULE(APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
-
-/* See function definitions in winbase.h */
-#  ifdef UNICODE
-#    ifdef _WIN32_WCE
-#      define LOADLIBARYEX L"LoadLibraryExW"
-#    else
-#      define LOADLIBARYEX "LoadLibraryExW"
-#    endif
-#  else
-#    define LOADLIBARYEX "LoadLibraryExA"
-#  endif
-
-/*
- * _Expat_LoadLibrary()
- *
- * This is used to dynamically load DLLs using the most secure method available
- * for the version of Windows that we are running on.
- *
- * Parameters:
- *
- * filename  [in] - The filename or full path of the DLL to load. If only the
- *                  filename is passed then the DLL will be loaded from the
- *                  Windows system directory.
- *
- * Returns the handle of the module on success; otherwise NULL.
- */
-HMODULE
-_Expat_LoadLibrary(LPCTSTR filename) {
-  HMODULE hModule = NULL;
-  LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
-
-  /* Get a handle to kernel32 so we can access it's functions at runtime */
-  HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
-  if (! hKernel32)
-    return NULL; /* LCOV_EXCL_LINE */
-
-  /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
-     and above */
-  pLoadLibraryEx = (LOADLIBRARYEX_FN)GetProcAddress(hKernel32, LOADLIBARYEX);
-
-  /* Detect if there's already a path in the filename and load the library if
-     there is. Note: Both back slashes and forward slashes have been supported
-     since the earlier days of DOS at an API level although they are not
-     supported by command prompt */
-  if (_tcspbrk(filename, TEXT("\\/"))) {
-    /** !checksrc! disable BANNEDFUNC 1 **/
-    hModule = pLoadLibraryEx ? pLoadLibraryEx(filename, NULL,
-                                              LOAD_WITH_ALTERED_SEARCH_PATH)
-                             : LoadLibrary(filename);
-  }
-  /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
-     supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
-     Server 2008 R2 with this patch or natively on Windows 8 and above */
-  else if (pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
-    /* Load the DLL from the Windows system directory */
-    hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
-  } else {
-    /* Attempt to get the Windows system path */
-    UINT systemdirlen = GetSystemDirectory(NULL, 0);
-    if (systemdirlen) {
-      /* Allocate space for the full DLL path (Room for the null terminator
-         is included in systemdirlen) */
-      size_t filenamelen = _tcslen(filename);
-      TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
-      if (path && GetSystemDirectory(path, systemdirlen)) {
-        /* Calculate the full DLL path */
-        _tcscpy(path + _tcslen(path), TEXT("\\"));
-        _tcscpy(path + _tcslen(path), filename);
-
-        /* Load the DLL from the Windows system directory */
-        /** !checksrc! disable BANNEDFUNC 1 **/
-        hModule = pLoadLibraryEx ? pLoadLibraryEx(path, NULL,
-                                                  LOAD_WITH_ALTERED_SEARCH_PATH)
-                                 : LoadLibrary(path);
-      }
-      free(path);
-    }
-  }
-
-  return hModule;
-}
-
-#else /* defined(_WIN32) */
-
-/* ISO C requires a translation unit to contain at least one declaration
-   [-Wempty-translation-unit] */
-typedef int _TRANSLATION_UNIT_LOAD_LIBRARY_C_NOT_EMTPY;
-
-#endif /* defined(_WIN32) */
index 50c61a87863ebf41a2c18d68944857f6f594881f..7ce7c97363a20f49b7280a03d9ef8ba574041cff 100644 (file)
       * libbsd (arc4random_buf): HAVE_ARC4RANDOM_BUF + HAVE_LIBBSD, \
       * libbsd (arc4random): HAVE_ARC4RANDOM + HAVE_LIBBSD, \
       * Linux / BSD / macOS (/dev/urandom): XML_DEV_URANDOM \
-      * Windows (RtlGenRandom): _WIN32. \
+      * Windows (rand_s): _WIN32. \
     \
     If insist on not using any of these, bypass this error by defining \
     XML_POOR_ENTROPY; you have been warned. \
@@ -758,36 +758,6 @@ writeRandomBytes_rand_s(void *target, size_t count) {
   return 1; /* success */
 }
 
-
-typedef BOOLEAN(APIENTRY *RTLGENRANDOM_FUNC)(PVOID, ULONG);
-HMODULE _Expat_LoadLibrary(LPCTSTR filename); /* see loadlibrary.c */
-
-/* Obtain entropy on Windows XP / Windows Server 2003 and later.
- * Hint on RtlGenRandom and the following article from libsodium.
- *
- * Michael Howard: Cryptographically Secure Random number on Windows without
- * using CryptoAPI
- * https://blogs.msdn.microsoft.com/michael_howard/2005/01/14/cryptographically-secure-random-number-on-windows-without-using-cryptoapi/
- */
-static int
-writeRandomBytes_RtlGenRandom(void *target, size_t count) {
-  int success = 0; /* full count bytes written? */
-  const HMODULE advapi32 = _Expat_LoadLibrary(TEXT("ADVAPI32.DLL"));
-
-  if (advapi32) {
-    const RTLGENRANDOM_FUNC RtlGenRandom
-        = (RTLGENRANDOM_FUNC)GetProcAddress(advapi32, "SystemFunction036");
-    if (RtlGenRandom) {
-      if (RtlGenRandom((PVOID)target, (ULONG)count) == TRUE) {
-        success = 1;
-      }
-    }
-    FreeLibrary(advapi32);
-  }
-
-  return success;
-}
-
 #endif /* _WIN32 */
 
 #if ! defined(HAVE_ARC4RANDOM_BUF) && ! defined(HAVE_ARC4RANDOM)
@@ -842,8 +812,8 @@ generate_hash_secret_salt(XML_Parser parser) {
 #else
   /* Try high quality providers first .. */
 #  ifdef _WIN32
-  if (writeRandomBytes_RtlGenRandom((void *)&entropy, sizeof(entropy))) {
-    return ENTROPY_DEBUG("RtlGenRandom", entropy);
+  if (writeRandomBytes_rand_s((void *)&entropy, sizeof(entropy))) {
+    return ENTROPY_DEBUG("rand_s", entropy);
   }
 #  elif defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM)
   if (writeRandomBytes_getrandom_nonblock((void *)&entropy, sizeof(entropy))) {