Skip to content

Commit

Permalink
Fix #169; incompatibility when injecting multiple programs during Aut…
Browse files Browse the repository at this point in the history
…oRun

Repro:  set AutoRun regkey to inject AutoHotkey, AnsiCon, and Clink.
Supply absolute paths, and use:

`script.ahk & ansicon -p & clink.bat inject --autorun`

- inject only AHK and Clink --> all is well.
- inject only AnsiCon and Clink --> all is well.
- inject all 3 --> Clink never receives GetEnvironmentVariableW.

I don't know why Martin had changed Clink to hook kernelbase.dll APIs on
Win8 and higher, instead of always hooking kernel32.dll APIs.

Hooking of kernelbase.dll has been the root cause of several bugs now,
and hooking kernel32.dll APIs even on Win10 seems to work just fine.

This change always hooks APIs from kernel32.dll.
  • Loading branch information
chrisant996 committed Sep 23, 2021
1 parent 61a7fd9 commit 2bd63d5
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions clink/app/src/host/host_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func_SetEnvironmentVariableW_t __Real_SetEnvironmentVariableW = SetEnvironmentVa
func_WriteConsoleW_t __Real_WriteConsoleW = WriteConsoleW;
func_ReadConsoleW_t __Real_ReadConsoleW = ReadConsoleW;
func_GetEnvironmentVariableW_t __Real_GetEnvironmentVariableW = GetEnvironmentVariableW;
static const char* s_kernel_module = nullptr;
static const char s_kernel_module[] = "kernel32.dll";

//------------------------------------------------------------------------------
extern bool is_force_reload_scripts();
Expand Down Expand Up @@ -245,19 +245,6 @@ bool host_cmd::initialise()
{
hook_setter hooks;

{
// Must hook the one in kernelbase.dll if it's present because CMD links
// with kernelbase.dll on Windows 10. Otherwise hook the one in
// kernel32.dll because it doesn't exist in kernelbase.dll on Windows 7.
//
// NOTE: Now that Detours is used exclusively instead of IAT hooking,
// this might not be relevant anymore. In fact, probably the module
// name and proc name are not even needed anymore.
HMODULE hlib = GetModuleHandleA("kernelbase.dll");
bool need_kernelbase = hlib && GetProcAddress(hlib, "ReadConsoleW");
s_kernel_module = need_kernelbase ? "kernelbase.dll" : "kernel32.dll";
}

// Hook the setting of the 'prompt' environment variable so we can tag
// it and detect command entry via a write hook.
tag_prompt();
Expand Down

0 comments on commit 2bd63d5

Please sign in to comment.