PHP on Windows should not rely on PATH for loading its DLL dependencies
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
There is a longstanding issue with mod_php for Apache when PHP extensions can't be loaded without adding PHP directory to PATH because they rely on some libraries which are in the root PHP directory, and the OS looks for them in the root Apache directory by default. Also, it causes an issue when a user has a few versions of PHP installed (with separate copies of Apache), one of them is in the PATH, and all the others try to load their extensions from that directory in PATH, and fail.
It is possible to resolve this issue by adding the directory of php_mod dll into the list of dll directories of current process. You should use the AddDllDirectory (supported in Windows 7+ since 2011) to specify additional DLL search path. It could be made in the DllMain of the php8apache2_4.dll like this:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlwapi.h>
BOOL APIENTRY DllMain(HMODULE hmodule, DWORD dwreason, LPVOID lpreserved)
{
static DLL_DIRECTORY_COOKIE dircookie = nullptr;
switch (dwreason)
{
case DLL_PROCESS_ATTACH:
SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
TCHAR path[MAX_PATH];
if (GetModuleFileName(hmodule, path, MAX_PATH))
{
PathRemoveFileSpec(path);
dircookie = AddDllDirectory(path);
}
break;
case DLL_PROCESS_DETACH:
if (dircookie)
{
RemoveDllDirectory(dircookie);
dircookie = nullptr;
}
break;
}
return TRUE;
}
Another option would be using AddDllDirectory during early initialization and passing LOAD_LIBRARY_SEARCH_DEFAULT_DIRS to every LoadLibrary call.
It makes sense to implement this for better security because a lot of unexpected things can be in PATH and load instead of what was expected. It will also resolve issues like #10076. This issue was already discussed in the mailing list and Christoph M. Becker agreed that it is OK to include such change.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the Windows Apache module entry point, php8apache2_4.dll, and any existing LoadLibrary calls. Review the proposed DllMain/AddDllDirectory approach and its Windows 7 support requirements, then verify extension loading with separate PHP and Apache installations. Done means dependencies load without PATH and the related #10076 failure is resolved without breaking process cleanup.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, php
- Domain
- backend, operating-systems, security
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100