Skip to content
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

Use LoadLibraryEx to be able to find dependencies in the DLL:s directory #140

Closed
wants to merge 2 commits into from

Conversation

qpernil
Copy link

@qpernil qpernil commented Jan 31, 2020

If the DLL you point to has dependencies in the same directory it won't load with LoadLibrary, as that directory is not on the library path. LoadLibraryEx with the LOAD_WITH_ALTERED_SEARCH_PATH flags searches that directory, so it works as one would expect.

…at it look for dependencies in the directory of the DLL
@jariq
Copy link
Member

jariq commented Jan 31, 2020

Hello @qpernil and thanks for the PR!

I'll need to take a closer look at the documentation of LoadLibraryEx function and carefully consider both compatibility and security impact of this change. Did you use Pkcs11Interop with any specific PKCS#11 implementation that needed this change?

@qpernil
Copy link
Author

qpernil commented Feb 3, 2020 via email

@qpernil
Copy link
Author

qpernil commented Feb 23, 2020

Have you had a chance to look at this yet ?
Best regards
Per

@jariq
Copy link
Member

jariq commented Feb 27, 2020

Sorry not yet. I'll try this weekend.

@jariq
Copy link
Member

jariq commented Feb 28, 2020

@qpernil
Copy link
Author

qpernil commented Feb 29, 2020

Re-reading the docs it seems one should use Path.GetFullPath(fileName) before calling LoadLibraryEx as it's apparently undefined to call it with the alternate search path flag and a non-absolute path.

…ryEx with altered search path and a non-absolute path is undefined behaviour
@jariq
Copy link
Member

jariq commented Mar 1, 2020

I hope you're OK with that I slightly modified your implementation in #143 because I'm regularly using relative paths in Pkcs11Admin application. I'm going to test that code now with a few modules to confirm it does not break anything.

Could you please also test the code in LoadLibraryEx branch and confirm that it works as expected with ykcs11?

@qpernil
Copy link
Author

qpernil commented Mar 2, 2020

Wouldn't it work as I implemented it anyway ? I.e. resolving the path to absolute will give the same outcome (LoadLibraryEx will resolve the relative path anyway), just with less code ? Just thinking, I'm fine with your way as well.

An unrelated thing you may have seen already: They now support platform-independent dynamic loading of libraries directly in dotnet core. I guess you might not be able to use it because you need to support older versions, including the 'classic' dotnet.

@qpernil
Copy link
Author

qpernil commented Mar 2, 2020

I will test your version later today, thx for your feedback !

@qpernil
Copy link
Author

qpernil commented Mar 2, 2020

Closing this PR as jariq has a better solution. Thx !

@qpernil qpernil closed this Mar 2, 2020
@jariq
Copy link
Member

jariq commented Mar 2, 2020

Wouldn't it work as I implemented it anyway ? I.e. resolving the path to absolute will give the same outcome (LoadLibraryEx will resolve the relative path anyway), just with less code ?

No, it wouldn't because LoadLibrary* functions use different resolution algorithm than System.IO.Path.GetFullPath method.

In my previous comment I wrote that "I'm regularly using relative paths in Pkcs11Admin application" but now I see that relative is not correct term here. I'll try to explain with more details:

Let's assume there's c:\SoftHSM2\lib\softhsm2.dll file available on your disk and c:\SoftHSM2\lib\ is part of the PATH environment variable. With this setup you can call LoadLibrary("softhsm2") without any path and even without .dll prefix and LoadLibrary still loads c:\SoftHSM2\lib\softhsm2.dll file. This is the feature I'm using regularly with Pkcs11Admin. When library dialog pops up I just write softhsm or opensc-pkcs11 then I hit enter and correct library is loaded. I know it's not the safest method and many things can go wrong but it's very convenient and well documented behavior of LoadLibrary* functions.

I hope this better explains my reasons for an alternative implementation.

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.

None yet

2 participants