Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

  1. /*++
  2. Copyright (c) 2001-2002 Microsoft Corporation
  3. Module Name:
  4. DuplicateHandleFix.cpp
  5. Abstract:
  6. DuplicateHandle was changed to always NULL the destination handle, even if
  7. errors were generated. THis shim ensures that the DestinationHandle is
  8. not modified if the duplication was not successful.
  9. History:
  10. 10/11/2001 robkenny Created.
  11. 02/20/2002 mnikkel Added check for null lpTargetHandle
  12. --*/
  13. #include "precomp.h"
  14. IMPLEMENT_SHIM_BEGIN(DuplicateHandleFix)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(DuplicateHandle)
  18. APIHOOK_ENUM_END
  19. typedef BOOL (WINAPI *_pfn_DuplicateHandle)(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions );
  20. /*++
  21. Don't allow DestinationHandle to change of DuplicateHandle generates an error.
  22. --*/
  23. BOOL
  24. APIHOOK(DuplicateHandle)(
  25. HANDLE hSourceProcessHandle, // handle to source process
  26. HANDLE hSourceHandle, // handle to duplicate
  27. HANDLE hTargetProcessHandle, // handle to target process
  28. LPHANDLE lpTargetHandle, // duplicate handle
  29. DWORD dwDesiredAccess, // requested access
  30. BOOL bInheritHandle, // handle inheritance option
  31. DWORD dwOptions // optional actions
  32. )
  33. {
  34. HANDLE origHandle = NULL;
  35. // Save the original value
  36. if (lpTargetHandle)
  37. {
  38. origHandle = *lpTargetHandle;
  39. }
  40. BOOL bSuccess = ORIGINAL_API(DuplicateHandle)(hSourceProcessHandle,
  41. hSourceHandle, hTargetProcessHandle, lpTargetHandle, dwDesiredAccess,
  42. bInheritHandle, dwOptions);
  43. if (!bSuccess && lpTargetHandle)
  44. {
  45. //
  46. // DuplicateHandle has set *lpTargetHandle to NULL, revert to it's previous value.
  47. //
  48. *lpTargetHandle = origHandle;
  49. LOGN(eDbgLevelError, "DuplicateHandle failed, reverting *lpTargetHandle to previous value");
  50. }
  51. return bSuccess;
  52. }
  53. /*++
  54. Register hooked functions
  55. --*/
  56. HOOK_BEGIN
  57. APIHOOK_ENTRY(KERNEL32.DLL, DuplicateHandle)
  58. HOOK_END
  59. IMPLEMENT_SHIM_END