Source code of Windows XP (NT5)
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.

205 lines
4.8 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. WlWrap.c
  5. Abstract:
  6. This module wraps library functions, rerouting them to native
  7. implementations as available.
  8. Author:
  9. Adrian J. Oney - April 21, 2002
  10. Revision History:
  11. --*/
  12. #include "WlDef.h"
  13. #include "WlpWrap.h"
  14. #pragma hdrstop
  15. #ifdef ALLOC_PRAGMA
  16. #pragma alloc_text(PAGE, WdmlibInit)
  17. #pragma alloc_text(PAGE, WdmlibIoCreateDeviceSecure)
  18. #endif
  19. BOOLEAN WdmlibInitialized = FALSE;
  20. //
  21. // Here is a list of global variables through which we route our function calls
  22. //
  23. PFN_IO_CREATE_DEVICE_SECURE PfnIoCreateDeviceSecure = NULL;
  24. PFN_IO_VALIDATE_DEVICE_IOCONTROL_ACCESS PfnIoValidateDeviceIoControlAccess = NULL;
  25. VOID
  26. WdmlibInit(
  27. VOID
  28. )
  29. {
  30. UNICODE_STRING functionName;
  31. RtlInitUnicodeString(&functionName, L"IoCreateDeviceSecure");
  32. PfnIoCreateDeviceSecure = MmGetSystemRoutineAddress(&functionName);
  33. if (PfnIoCreateDeviceSecure == NULL) {
  34. PfnIoCreateDeviceSecure = IoDevObjCreateDeviceSecure;
  35. }
  36. RtlInitUnicodeString(&functionName, L"IoValidateDeviceIoControlAccess");
  37. PfnIoValidateDeviceIoControlAccess = MmGetSystemRoutineAddress(&functionName);
  38. WdmlibInitialized = TRUE;
  39. }
  40. NTSTATUS
  41. WdmlibIoCreateDeviceSecure(
  42. IN PDRIVER_OBJECT DriverObject,
  43. IN ULONG DeviceExtensionSize,
  44. IN PUNICODE_STRING DeviceName OPTIONAL,
  45. IN DEVICE_TYPE DeviceType,
  46. IN ULONG DeviceCharacteristics,
  47. IN BOOLEAN Exclusive,
  48. IN PCUNICODE_STRING DefaultSDDLString,
  49. IN LPCGUID DeviceClassGuid,
  50. OUT PDEVICE_OBJECT *DeviceObject
  51. )
  52. /*++
  53. Routine Description:
  54. This routine is a library wrapper for IoCreateDeviceSecure. It calls either
  55. the internal library version of IoCreateDeviceSecure, or it calls the
  56. native implementation in the Operating System.
  57. Parameters:
  58. See IoCreateDeviceSecure documentation.
  59. Return Value:
  60. See IoCreateDeviceSecure documentation.
  61. --*/
  62. {
  63. if (WdmlibInitialized == FALSE) {
  64. WdmlibInit();
  65. }
  66. return PfnIoCreateDeviceSecure(
  67. DriverObject,
  68. DeviceExtensionSize,
  69. DeviceName,
  70. DeviceType,
  71. DeviceCharacteristics,
  72. Exclusive,
  73. DefaultSDDLString,
  74. DeviceClassGuid,
  75. DeviceObject
  76. );
  77. }
  78. NTSTATUS
  79. WdmlibRtlInitUnicodeStringEx(
  80. OUT PUNICODE_STRING DestinationString,
  81. IN PCWSTR SourceString OPTIONAL
  82. )
  83. {
  84. SIZE_T Length;
  85. if (SourceString != NULL) {
  86. Length = wcslen(SourceString);
  87. //
  88. // We are actually limited to 32765 characters since we want to store a
  89. // meaningful MaximumLength also.
  90. //
  91. if (Length > (UNICODE_STRING_MAX_CHARS - 1)) {
  92. return STATUS_NAME_TOO_LONG;
  93. }
  94. Length *= sizeof(WCHAR);
  95. DestinationString->Length = (USHORT) Length;
  96. DestinationString->MaximumLength = (USHORT) (Length + sizeof(WCHAR));
  97. DestinationString->Buffer = (PWSTR) SourceString;
  98. } else {
  99. DestinationString->Length = 0;
  100. DestinationString->MaximumLength = 0;
  101. DestinationString->Buffer = NULL;
  102. }
  103. return STATUS_SUCCESS;
  104. }
  105. NTSTATUS
  106. WdmlibIoValidateDeviceIoControlAccess(
  107. IN PIRP Irp,
  108. IN ULONG RequiredAccess
  109. )
  110. /*++
  111. Routine Description:
  112. This routine validates ioctl access bits based on granted access
  113. information passed in the IRP. This routine is called by a driver to
  114. validate IOCTL access bits for IOCTLs that were originally defined as
  115. FILE_ANY_ACCESS and cannot be changed for compatibility reasons but really
  116. has to be validated for read/write access.
  117. This routine is actually a wrapper around the kernel function exported in
  118. XPSP1 and .NET server versions of Windows. This wrapper allows a driver to
  119. call this function on all versions of Windows starting with WIN2K. On
  120. Windows platforms which don't support the kernel function
  121. IoValidateDeviceIoControlAccess this wrapper reverts back to the old
  122. behaviour. This wrapper allows a driver to have the same source code and
  123. get the added benefit of the security check on newer operating systems.
  124. Arguments:
  125. IRP - IRP for the device control
  126. RequiredAccess - Is the expected access required by the driver. Should be
  127. FILE_READ_ACCESS, FILE_WRITE_ACCESS or both.
  128. Return Value:
  129. Returns NTSTATUS
  130. --*/
  131. {
  132. //
  133. // In older versions, assume access check succeeds
  134. // to retain old behaviour.
  135. //
  136. if (PfnIoValidateDeviceIoControlAccess == NULL) {
  137. return STATUS_SUCCESS;
  138. }
  139. //
  140. // If the function is present use the appropriate access check.
  141. //
  142. return (PfnIoValidateDeviceIoControlAccess(Irp, RequiredAccess));
  143. }