-
Notifications
You must be signed in to change notification settings - Fork 55
CHORE: load bundled Windows driver and auth DLLs from package-local directories #735
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
Open
Gaurav Sharma (bewithgaurav)
wants to merge
7
commits into
main
Choose a base branch
from
bewithgaurav/win-dll-search-hardening
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9fdc87
CHORE: load bundled Windows driver and auth DLLs from package-local d…
bewithgaurav de227c3
CHORE: apply Black formatting to Windows DLL search test
bewithgaurav a4a7346
CHORE: guard the Windows loader against reverting to unhardened LoadL…
bewithgaurav 6ead192
CHORE: simplify Windows loader hardening to LoadLibraryExW flags only
bewithgaurav c3430f1
Merge branch 'main' into bewithgaurav/win-dll-search-hardening
bewithgaurav 7c4ac71
CHORE: bind the loader test's flag checks to each LoadLibraryExW call
bewithgaurav 9e4e3df
Merge remote-tracking branch 'origin/bewithgaurav/win-dll-search-hard…
bewithgaurav 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,59 @@ | ||
| """ | ||
| Regression guard for the Windows package-local DLL load path. | ||
|
|
||
| The vendored ODBC driver (``msodbcsql18.dll``) and Entra auth DLL | ||
| (``mssql-auth.dll``) must be loaded with a constrained search path -- | ||
| ``LoadLibraryExW`` with ``LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | | ||
| LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR`` -- rather than the legacy ``LoadLibraryW`` | ||
| search order, which also consults the current working directory and ``%PATH%`` | ||
| when resolving those DLLs' dependencies. | ||
|
|
||
| This is a source-contract test on purpose. The restriction only manifests at | ||
| DLL-resolution time on Windows, which cannot be observed without dropping a | ||
| file on disk; a success-path "does it still load" check passes on the | ||
| unhardened code too (any host with ``msvcp140.dll`` in System32), so it guards | ||
| nothing. Asserting the loader keeps using the constrained API is the | ||
| deterministic, platform-independent way to fail if the hardening is reverted. | ||
| """ | ||
|
|
||
| import re | ||
| from pathlib import Path | ||
|
|
||
| _LOADER_SRC = Path(__file__).resolve().parents[1] / "mssql_python" / "pybind" / "ddbc_bindings.cpp" | ||
|
|
||
|
|
||
| def _code_without_comments(text): | ||
| # Drop // line comments so prose that mentions LoadLibraryW is not matched. | ||
| return "\n".join(re.sub(r"//.*", "", line) for line in text.splitlines()) | ||
|
|
||
|
|
||
| def test_loader_source_present(): | ||
| assert _LOADER_SRC.is_file(), f"loader source not found at {_LOADER_SRC}" | ||
|
|
||
|
|
||
| def test_no_unhardened_loadlibrary_call(): | ||
| code = _code_without_comments(_LOADER_SRC.read_text(encoding="utf-8")) | ||
| # A bare LoadLibraryW(...) call resolves dependencies via the legacy search | ||
| # order, which includes the current directory and %PATH%. | ||
| assert re.search(r"\bLoadLibraryW\s*\(", code) is None, ( | ||
| "ddbc_bindings.cpp contains a bare LoadLibraryW call; the vendored " | ||
| "driver and auth DLLs must be loaded with LoadLibraryExW and the " | ||
| "constrained search flags instead." | ||
| ) | ||
|
|
||
|
|
||
| def test_driver_and_auth_loads_use_constrained_search(): | ||
| code = _code_without_comments(_LOADER_SRC.read_text(encoding="utf-8")) | ||
| # Match each LoadLibraryExW( ... ); call and require BOTH flags inside that | ||
| # call's own argument list -- not merely somewhere in the file (the flag | ||
| # names also appear in the #define block, so a file-wide substring check | ||
| # would still pass if a call's flags were replaced with 0). | ||
| calls = re.findall(r"LoadLibraryExW\s*\(.*?\)\s*;", code, re.DOTALL) | ||
| assert len(calls) >= 2, "expected LoadLibraryExW for both the driver and the auth DLL loads" | ||
| for call in calls: | ||
| assert "LOAD_LIBRARY_SEARCH_DEFAULT_DIRS" in call, ( | ||
| "a LoadLibraryExW call is missing LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: " + call | ||
| ) | ||
| assert "LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR" in call, ( | ||
| "a LoadLibraryExW call is missing LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR: " + call | ||
| ) | ||
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.