You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes: #676
Context: https://liquid.microsoft.com/Web/Object/Read/ms.security/Requirements/Microsoft.Security.SystemsADM.10039#guide
The current security guidance is that the
[`System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute`][0]
attribute should be placed either on the assembly or on `[DllImport]`
methods to control and constrain where [`LoadLibraryEx()`][1] will look
for native libraries, in particular to *prevent* looking for native
libraries within e.g. the current working directory or `%PATH%` or any
other "attacker-controlled" location.
Update `Java.Interop.dll` and `Java.Runtime.Environment.dll` so that
the [`DllImportSearchPath`][2] values `AssemblyDirectory` and
`SafeDirectories` are used:
* `AssemblyDirectory`: "include the directory that contains the
assembly itself, and search that directory first."
* `SafeDirectories`: "Include the application directory, the
`%WinDir%\System32` directory, and user directories in the DLL
search path.
Additionally, update `src/java-interop` so that instead of requiring
the use of [**dlopen**(3)][3] on Windows, the following functions are
added to support loading native libraries and resolving symbols
from those native libraries:
void* java_interop_lib_load (const char *path, unsigned int flags, char **error);
void* java_interop_lib_symbol (void* library, const char *symbol, char **error);
int java_interop_lib_close (void* library, char **error);
(Previously, xamarin-android used the [dlfcn-win32/dlfcn-win32][4]
library to implement **dlopen**(3), but
dlfcn-win32/dlfcn-win32@ef7e412d calls `LoadLibraryEx()` with
`LOAD_WITH_ALTERED_SEARCH_PATH`, which doesn't fulfill our internal
requirements.)
On Windows, `java_interop_lib_load()` will use
[`LoadLibraryEx()`][5] to load libraries from a constrained set of
directories:
* `LOAD_LIBRARY_SEARCH_APPLICATION_DIR`: "the application's
installation directory is searched for the DLL and its dependencies"
* `LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR`: "the directory that contains
the DLL is temporarily added to the beginning of the list of
directories that are searched for the DLL's dependencies."
* `LOAD_LIBRARY_SEARCH_USER_DIRS`: "directories added using the
`AddDllDirectory()` or the `SetDllDirectory()` function are searched
for the DLL and its dependencies"
In order to simplify the introduction of
`java_interop_lib_load()`, start *requiring* the presence of the
symbols `mono_thread_get_managed_id` and `mono_thread_get_name_utf8`.
These symbols have been present within Mono for ages at this point,
and requiring means we don't need to support `dlopen(NULL)` semantics.
Update the `@(ClInclude)` item group and `BuildMac` and related targets
so that we properly rebuild things when e.g. `java-interop-dlfcn.h`
changes, as would "normally" be expected.
Finally, the continued use of `MONO_API` and other macros causes
"weird" compiler issues when integrating with xamarin-android.
Replace `MONO_API`/etc. use with `JAVA_INTEROP_API`/etc. instead.
[0]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.defaultdllimportsearchpathsattribute?view=netcore-3.1
[1]: https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa?redirectedfrom=MSDN
[2]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportsearchpath?view=netcore-3.1
[3]: https://linux.die.net/man/3/dlopen
[4]: https://github.com/dlfcn-win32/dlfcn-win32
[5]: https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
0 commit comments