-
Notifications
You must be signed in to change notification settings - Fork 64
[java-interop, Java.Interop] Securely load native libs #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
radekdoulik
merged 2 commits into
dotnet:master
from
jonpryor:jonp-loadlib-search-order
Aug 25, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| [assembly: DefaultDllImportSearchPathsAttribute (DllImportSearchPath.SafeDirectories | DllImportSearchPath.AssemblyDirectory)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| #include "java-interop.h" | ||
| #include "java-interop-dlfcn.h" | ||
| #include "java-interop-util.h" | ||
|
|
||
| #ifdef WINDOWS | ||
| #include <libloaderapi.h> | ||
| #include <winerror.h> | ||
| #include <wtypes.h> | ||
| #include <winbase.h> | ||
| #else | ||
| #include <dlfcn.h> | ||
| #include <string.h> | ||
| #endif | ||
|
|
||
| namespace microsoft::java_interop { | ||
|
|
||
| static char * | ||
| _get_last_dlerror () | ||
| { | ||
| #ifdef WINDOWS | ||
|
|
||
| DWORD error = GetLastError (); | ||
| if (error == ERROR_SUCCESS /* 0 */) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| wchar_t *buf = nullptr; | ||
|
|
||
| DWORD size = FormatMessageW ( | ||
| /* dwFlags */ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | ||
| /* lpSource */ NULL, | ||
| /* dwMessageId */ error, | ||
| /* dwLanguageId */ MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
| /* lpBuffer */ (LPWSTR) &buf, | ||
| /* nSize */ 0, | ||
| /* Arguments */ NULL | ||
| ); | ||
| if (size == 0) | ||
| return nullptr; | ||
|
|
||
| char *message = utf16_to_utf8 (buf); | ||
| LocalFree (buf); | ||
|
|
||
| return message; | ||
|
|
||
| #else // ndef WINDOWS | ||
|
|
||
| return java_interop_strdup (dlerror ()); | ||
|
|
||
| #endif // ndef WINDOWS | ||
| } | ||
|
|
||
| static void | ||
| _free_error (char **error) | ||
| { | ||
| if (error == nullptr) | ||
| return; | ||
| java_interop_free (*error); | ||
| *error = nullptr; | ||
| } | ||
|
|
||
| static void | ||
| _set_error (char **error, const char *message) | ||
| { | ||
| if (error == nullptr) | ||
| return; | ||
| *error = java_interop_strdup (message); | ||
| } | ||
|
|
||
| static void | ||
| _set_error_to_last_error (char **error) | ||
| { | ||
| if (error == nullptr) | ||
| return; | ||
| *error = _get_last_dlerror (); | ||
| } | ||
|
|
||
| void* | ||
| java_interop_lib_load (const char *path, [[maybe_unused]] unsigned int flags, char **error) | ||
| { | ||
| _free_error (error); | ||
| if (path == nullptr) { | ||
| _set_error (error, "path=nullptr is not supported"); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void *handle = nullptr; | ||
|
|
||
| #ifdef WINDOWS | ||
|
|
||
| wchar_t *wpath = utf8_to_utf16 (path); | ||
| if (wpath == nullptr) { | ||
| _set_error (error, "could not convert path to UTF-16"); | ||
| return nullptr; | ||
| } | ||
| HMODULE module = LoadLibraryExW ( | ||
| /* lpLibFileName */ wpath, | ||
| /* hFile */ nullptr, | ||
| /* dwFlags */ LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_USER_DIRS | ||
| ); | ||
| java_interop_free (wpath); | ||
|
|
||
| handle = reinterpret_cast<void*>(module); | ||
|
|
||
| #else // ndef WINDOWS | ||
|
|
||
| int mode = 0; | ||
| if ((flags & JAVA_INTEROP_LIB_LOAD_GLOBALLY) == JAVA_INTEROP_LIB_LOAD_GLOBALLY) { | ||
| mode = RTLD_GLOBAL; | ||
| } | ||
| if ((flags & JAVA_INTEROP_LIB_LOAD_LOCALLY) == JAVA_INTEROP_LIB_LOAD_LOCALLY) { | ||
| mode = RTLD_LOCAL; | ||
| } | ||
|
|
||
| if (mode == 0) { | ||
| mode = RTLD_LOCAL; | ||
| } | ||
| mode |= RTLD_NOW; | ||
|
|
||
| handle = dlopen (path, mode); | ||
|
|
||
| #endif // ndef WINDOWS | ||
|
|
||
| if (handle == nullptr) { | ||
| _set_error_to_last_error (error); | ||
| } | ||
|
|
||
| return handle; | ||
| } | ||
|
|
||
| void* | ||
| java_interop_lib_symbol (void *library, const char *symbol, char **error) | ||
| { | ||
| _free_error (error); | ||
|
|
||
| if (library == nullptr) { | ||
| _set_error (error, "library=nullptr"); | ||
| return nullptr; | ||
| } | ||
| if (symbol == nullptr) { | ||
| _set_error (error, "symbol=nullptr"); | ||
| return nullptr; | ||
| } | ||
|
|
||
| void *address = nullptr; | ||
|
|
||
| #ifdef WINDOWS | ||
|
|
||
| HMODULE module = reinterpret_cast<HMODULE>(library); | ||
| FARPROC a = GetProcAddress (module, symbol); | ||
| address = reinterpret_cast<void*>(a); | ||
|
|
||
| #else // ndef WINDOWS | ||
|
|
||
| address = dlsym (library, symbol); | ||
|
|
||
| #endif // ndef WINDOWS | ||
|
|
||
| if (address == nullptr) { | ||
| _set_error_to_last_error (error); | ||
| } | ||
|
|
||
| return address; | ||
| } | ||
|
|
||
| int | ||
| java_interop_lib_close (void* library, char **error) | ||
| { | ||
| _free_error (error); | ||
| if (library == nullptr) { | ||
| _set_error (error, "library=nullptr"); | ||
| return JAVA_INTEROP_LIB_INVALID_PARAM; | ||
| } | ||
|
|
||
| int r = 0; | ||
|
|
||
| #ifdef WINDOWS | ||
| HMODULE h = reinterpret_cast<HMODULE>(library); | ||
| BOOL v = FreeLibrary (h); | ||
| if (!v) { | ||
| r = JAVA_INTEROP_LIB_CLOSE_FAILED; | ||
| } | ||
| #else // ndef WINDOWS | ||
| r = dlclose (library); | ||
| if (r != 0) { | ||
| r = JAVA_INTEROP_LIB_CLOSE_FAILED; | ||
| } | ||
| #endif // ndef WINDOWS | ||
|
|
||
| if (r != 0) { | ||
| _set_error_to_last_error (error); | ||
| } | ||
|
|
||
| return r; | ||
| } | ||
|
|
||
| } // namespace microsoft::java_interop | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #ifndef INC_JAVA_INTEROP_DLFCN_H | ||
| #define INC_JAVA_INTEROP_DLFCN_H | ||
|
|
||
| #include "java-interop.h" | ||
|
|
||
| namespace microsoft::java_interop { | ||
|
|
||
| // Possible flags values for java_interop_lib_load | ||
| constexpr unsigned int JAVA_INTEROP_LIB_LOAD_GLOBALLY = (1 << 0); | ||
| constexpr unsigned int JAVA_INTEROP_LIB_LOAD_LOCALLY = (1 << 1); | ||
|
|
||
|
|
||
| // Possible error codes from java_interop_lib_close | ||
| constexpr int JAVA_INTEROP_LIB_FAILED = -1000; | ||
| constexpr int JAVA_INTEROP_LIB_CLOSE_FAILED = JAVA_INTEROP_LIB_FAILED-1; | ||
| constexpr int JAVA_INTEROP_LIB_INVALID_PARAM = JAVA_INTEROP_LIB_FAILED-2; | ||
|
|
||
| JAVA_INTEROP_BEGIN_DECLS | ||
|
|
||
| JAVA_INTEROP_API void* java_interop_lib_load (const char *path, unsigned int flags, char **error); | ||
| JAVA_INTEROP_API void* java_interop_lib_symbol (void* library, const char *symbol, char **error); | ||
| JAVA_INTEROP_API int java_interop_lib_close (void* library, char **error); | ||
|
|
||
| JAVA_INTEROP_END_DECLS | ||
|
|
||
| } | ||
|
|
||
| #endif /* INC_JAVA_INTEROP_DLFCN_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.