dll_hijacking_protection.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * If the OS provides it, call SetDefaultDllDirectories() to prevent
  3. * DLLs from being loaded from the directory containing our own
  4. * binary, and instead only load from system32.
  5. *
  6. * This is a protection against hijacking attacks, if someone runs
  7. * PuTTY directly from their web browser's download directory having
  8. * previously been enticed into clicking on an unwise link that
  9. * downloaded a malicious DLL to the same directory under one of
  10. * various magic names that seem to be things that standard Windows
  11. * DLLs delegate to.
  12. *
  13. * It shouldn't break deliberate loading of user-provided DLLs such as
  14. * GSSAPI providers, because those are specified by their full
  15. * pathname by the user-provided configuration.
  16. */
  17. #include "putty.h"
  18. void dll_hijacking_protection(void)
  19. {
  20. static HMODULE kernel32_module;
  21. DECL_WINDOWS_FUNCTION(static, BOOL, SetDefaultDllDirectories, (DWORD));
  22. if (!kernel32_module) {
  23. kernel32_module = load_system32_dll("kernel32.dll");
  24. #if !HAVE_SETDEFAULTDLLDIRECTORIES
  25. /* For older Visual Studio, this function isn't available in
  26. * the header files to type-check */
  27. GET_WINDOWS_FUNCTION_NO_TYPECHECK(
  28. kernel32_module, SetDefaultDllDirectories);
  29. #else
  30. GET_WINDOWS_FUNCTION(kernel32_module, SetDefaultDllDirectories);
  31. #endif
  32. }
  33. if (p_SetDefaultDllDirectories) {
  34. /* LOAD_LIBRARY_SEARCH_SYSTEM32 and explicitly specified
  35. * directories only */
  36. p_SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32 |
  37. LOAD_LIBRARY_SEARCH_USER_DIRS);
  38. }
  39. }