Skip to content

Verify wintun.dll signature before loading it - #22

Open
kroxiksut wants to merge 1 commit into
tun2proxy:masterfrom
kroxiksut:verify-signature-before-load
Open

kroxiksut wants to merge 1 commit into
tun2proxy:masterfrom
kroxiksut:verify-signature-before-load

Conversation

@kroxiksut

Copy link
Copy Markdown

With the verify_binary_signature feature, load_from_path found the DLL's absolute path by calling LoadLibraryW on it (the FIXME in get_dll_absolute_path), so the DLL's DllMain ran before its signature was checked. The checked file and the loaded file could also differ: the signature was verified on the resolved path, and the library was then loaded again by the original, possibly relative, name.

This change:

  • resolves the path without loading anything: an absolute path is used as is, a relative one is looked up next to the executable, then in the current directory (.dll is appended when there is no extension, as LoadLibraryW does);
  • opens the file with read-only sharing and keeps the handle until the library is loaded, so the file cannot be replaced, renamed or deleted between the check and the load;
  • verifies the signature through that handle and loads the library by the same absolute path.

It also fixes a side issue: when LoadLibraryW failed (for example, the file is not a valid DLL), GetModuleFileNameW(NULL) returned the path of the current executable, and the executable's signature was checked instead of the DLL's.

Behaviour change, only with verify_binary_signature: a bare name is no longer searched in System32 or PATH, only next to the executable and in the current directory. Happy to change the order if you prefer another one.

The public API is unchanged. Tested on Windows 10 x64:

  • cargo test --all-features, with new tests for path resolution, the file lock, rejecting an unsigned file, and loading the bundled DLL by a relative path as the examples do;
  • cargo clippy -- -D warnings with and without --all-features;
  • cargo build --examples with and without --all-features;
  • cargo fmt -- --check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant