ReactOS  0.4.15-dev-1386-g5cb9f87
ObHandle.c File Reference
#include <kmt_test.h>
#include <debug.h>
Include dependency graph for ObHandle.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define CheckObject(Handle, Pointers, Handles, Attrib, Access)
 
#define KERNEL_HANDLE_FLAG   ((ULONG_PTR)0xFFFFFFFF80000000)
 
#define IsUserHandle(h)   (((ULONG_PTR)(h) & KERNEL_HANDLE_FLAG) == 0)
 
#define IsKernelHandle(h)   (((ULONG_PTR)(h) & KERNEL_HANDLE_FLAG) == KERNEL_HANDLE_FLAG)
 

Functions

static VOID TestDuplicate (_In_ HANDLE Handle)
 
 START_TEST (ObHandle)
 

Variables

static HANDLE SystemProcessHandle
 

Macro Definition Documentation

◆ CheckObject

#define CheckObject (   Handle,
  Pointers,
  Handles,
  Attrib,
  Access 
)
Value:
do \
{ \
PUBLIC_OBJECT_BASIC_INFORMATION ObjectInfo; \
Status = ZwQueryObject(Handle, ObjectBasicInformation, \
&ObjectInfo, sizeof ObjectInfo, NULL); \
ok_eq_hex(Status, STATUS_SUCCESS); \
ok_eq_hex(ObjectInfo.Attributes, Attrib); \
ok_eq_hex(ObjectInfo.GrantedAccess, Access); \
ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
} while (0)
Status
Definition: gdiplustypes.h:24
#define NULL
Definition: types.h:112
_In_ HANDLE Handle
Definition: extypes.h:390
#define STATUS_SUCCESS
Definition: shellext.h:65

Definition at line 12 of file ObHandle.c.

◆ IsKernelHandle

#define IsKernelHandle (   h)    (((ULONG_PTR)(h) & KERNEL_HANDLE_FLAG) == KERNEL_HANDLE_FLAG)

Definition at line 26 of file ObHandle.c.

◆ IsUserHandle

#define IsUserHandle (   h)    (((ULONG_PTR)(h) & KERNEL_HANDLE_FLAG) == 0)

Definition at line 25 of file ObHandle.c.

◆ KERNEL_HANDLE_FLAG

#define KERNEL_HANDLE_FLAG   ((ULONG_PTR)0xFFFFFFFF80000000)

Definition at line 24 of file ObHandle.c.

◆ NDEBUG

#define NDEBUG

Definition at line 9 of file ObHandle.c.

Function Documentation

◆ START_TEST()

START_TEST ( ObHandle  )

Definition at line 116 of file ObHandle.c.

117 {
120  HANDLE KernelDirectoryHandle;
121  HANDLE UserDirectoryHandle;
122 
125  NULL,
127  *PsProcessType,
128  KernelMode,
131  if (skip(NT_SUCCESS(Status), "No handle for system process\n"))
132  {
134  }
135 
137  NULL,
138  0,
139  NULL,
140  NULL);
141  Status = ZwCreateDirectoryObject(&UserDirectoryHandle,
145  if (!skip(NT_SUCCESS(Status), "No directory handle\n"))
146  {
147  ok(IsUserHandle(UserDirectoryHandle), "User handle = %p\n", UserDirectoryHandle);
148  CheckObject(UserDirectoryHandle, 2UL, 1UL, 0UL, DIRECTORY_ALL_ACCESS);
149 
150  TestDuplicate(UserDirectoryHandle);
151 
152  Status = ObCloseHandle(UserDirectoryHandle, UserMode);
154  }
155 
157  NULL,
159  NULL,
160  NULL);
161  Status = ZwCreateDirectoryObject(&KernelDirectoryHandle,
165  if (!skip(NT_SUCCESS(Status), "No directory handle\n"))
166  {
167  ok(IsKernelHandle(KernelDirectoryHandle), "Kernel handle = %p\n", KernelDirectoryHandle);
168  CheckObject(KernelDirectoryHandle, 2UL, 1UL, 0UL, DIRECTORY_ALL_ACCESS);
169 
170  TestDuplicate(KernelDirectoryHandle);
171 
172  Status = ObCloseHandle(KernelDirectoryHandle, UserMode);
174  CheckObject(KernelDirectoryHandle, 2UL, 1UL, 0UL, DIRECTORY_ALL_ACCESS);
175 
176  Status = ObCloseHandle(KernelDirectoryHandle, KernelMode);
178  }
179 
180  /* Tests for closing handles */
181  KmtStartSeh()
182  /* NtClose must accept everything */
183  DPRINT("Closing null handle (NtClose)\n");
184  Status = NtClose(NULL);
186  DPRINT("Closing null kernel handle (NtClose)\n");
187  Status = NtClose(LongToHandle(0x80000000));
189  DPRINT("Closing -1 handle (NtClose)\n");
190  Status = NtClose(LongToHandle(0x7FFFFFFF));
192  DPRINT("Closing -1 kernel handle (NtClose)\n");
193  Status = NtClose(LongToHandle(0xFFFFFFFF));
195  DPRINT("Closing 123 handle (NtClose)\n");
196  Status = NtClose(LongToHandle(123));
198  DPRINT("Closing 123 kernel handle (NtClose)\n");
199  Status = NtClose(LongToHandle(123 | 0x80000000));
201 
202  /* ObCloseHandle with UserMode accepts everything */
203  DPRINT("Closing null handle (ObCloseHandle, UserMode)\n");
206  DPRINT("Closing null kernel handle (ObCloseHandle, UserMode)\n");
207  Status = ObCloseHandle(LongToHandle(0x80000000), UserMode);
209  DPRINT("Closing -1 handle (ObCloseHandle, UserMode)\n");
210  Status = ObCloseHandle(LongToHandle(0x7FFFFFFF), UserMode);
212  DPRINT("Closing -1 kernel handle (ObCloseHandle, UserMode)\n");
213  Status = ObCloseHandle(LongToHandle(0xFFFFFFFF), UserMode);
215  DPRINT("Closing 123 handle (ObCloseHandle, UserMode)\n");
218  DPRINT("Closing 123 kernel handle (ObCloseHandle, UserMode)\n");
219  Status = ObCloseHandle(LongToHandle(123 | 0x80000000), UserMode);
221 
222  /* ZwClose only accepts 0 and -1 */
223  DPRINT("Closing null handle (ZwClose)\n");
224  Status = ZwClose(NULL);
226  DPRINT("Closing null kernel handle (ZwClose)\n");
227  Status = ZwClose(LongToHandle(0x80000000));
229  /* INVALID_KERNEL_HANDLE, 0x7FFFFFFF
230  Status = ZwClose((HANDLE)0x7FFFFFFF);*/
231  DPRINT("Closing -1 kernel handle (ZwClose)\n");
232  Status = ZwClose(LongToHandle(0xFFFFFFFF));
234  /* INVALID_KERNEL_HANDLE, 0x7B, 1, 0, 0
235  Status = ZwClose(LongToHandle(123));
236  Status = ZwClose(LongToHandle(123 | 0x80000000));*/
237 
238  /* ObCloseHandle with KernelMode accepts only 0 and -1 */
239  DPRINT("Closing null handle (ObCloseHandle, KernelMode)\n");
242  DPRINT("Closing null kernel handle (ObCloseHandle, KernelMode)\n");
245  /* INVALID_KERNEL_HANDLE, 0x7FFFFFFF, 1, 0, 0
246  Status = ObCloseHandle((HANDLE)0x7FFFFFFF, KernelMode);*/
247  DPRINT("Closing -1 kernel handle (ObCloseHandle, KernelMode)\n");
250  /* INVALID_KERNEL_HANDLE, 0x7B, 1, 0, 0
251  Status = ObCloseHandle(LongToHandle(123), KernelMode);
252  Status = ObCloseHandle(LongToHandle(123 | 0x80000000), KernelMode);*/
254 
256  {
258  }
259 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define IsUserHandle(h)
Definition: ObHandle.c:25
#define PROCESS_ALL_ACCESS
Definition: nt_native.h:1324
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:283
GLdouble n
Definition: glext.h:7729
NTSTATUS NTAPI ObOpenObjectByPointer(IN PVOID Object, IN ULONG HandleAttributes, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PHANDLE Handle)
Definition: obhandle.c:2739
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:245
static VOID TestDuplicate(_In_ HANDLE Handle)
Definition: ObHandle.c:32
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
NTSYSAPI NTSTATUS NTAPI ZwCreateDirectoryObject(_Out_ PHANDLE DirectoryHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
void DPRINT(...)
Definition: polytest.cpp:61
Status
Definition: gdiplustypes.h:24
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
int null(void)
Definition: ftp.c:1794
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define CheckObject(Handle, Pointers, Handles, Attrib, Access)
Definition: ObHandle.c:12
NTSTATUS NTAPI ObCloseHandle(IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: obhandle.c:3376
#define LongToHandle(h)
Definition: basetsd.h:82
#define KmtStartSeh()
Definition: kmt_test.h:277
#define IsKernelHandle(h)
Definition: ObHandle.c:26
#define DIRECTORY_ALL_ACCESS
Definition: nt_native.h:1259
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define skip(...)
Definition: atltest.h:64
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define ok_eq_hex(value, expected)
#define STATUS_SUCCESS
Definition: shellext.h:65
#define UL
Definition: tui.h:82
POBJECT_TYPE PsProcessType
Definition: process.c:20
static HANDLE SystemProcessHandle
Definition: ObHandle.c:28

◆ TestDuplicate()

static VOID TestDuplicate ( _In_ HANDLE  Handle)
static

Definition at line 32 of file ObHandle.c.

34 {
36  HANDLE NewHandle;
37  struct
38  {
40  ULONG RequestedAttributes;
41  ULONG Options;
43  ULONG ExpectedAttributes;
44  } Tests[] =
45  {
46  { DIRECTORY_ALL_ACCESS, 0, 0,
50  { DIRECTORY_QUERY, 0, 0,
51  DIRECTORY_QUERY, 0 },
56  /* 5 */
58  DIRECTORY_QUERY, 0 },
61  };
62  ULONG i;
63 
64  for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
65  {
66  trace("Test %lu\n", i);
67  Status = ZwDuplicateObject(ZwCurrentProcess(),
68  Handle,
70  &NewHandle,
72  Tests[i].RequestedAttributes,
73  Tests[i].Options);
75  if (!skip(NT_SUCCESS(Status), "DuplicateHandle failed\n"))
76  {
77  ok(IsUserHandle(NewHandle), "New handle = %p\n", NewHandle);
78  CheckObject(NewHandle, 3UL, 2UL, Tests[i].ExpectedAttributes, Tests[i].GrantedAccess);
80 
81  Status = ObCloseHandle(NewHandle, UserMode);
84  }
85  }
86 
87  /* If TargetProcess is the System process, we do get a kernel handle */
88  Status = ZwDuplicateObject(ZwCurrentProcess(),
89  Handle,
91  &NewHandle,
94  0);
96  if (!skip(NT_SUCCESS(Status), "DuplicateHandle failed\n"))
97  {
98  ok(IsKernelHandle(NewHandle), "New handle = %p\n", NewHandle);
99  CheckObject(NewHandle, 3UL, 2UL, 0, DIRECTORY_ALL_ACCESS);
101 
102  Status = ObCloseHandle(NewHandle, UserMode);
104  CheckObject(NewHandle, 3UL, 2UL, 0, DIRECTORY_ALL_ACCESS);
106 
107  if (IsKernelHandle(NewHandle))
108  {
109  Status = ObCloseHandle(NewHandle, KernelMode);
112  }
113  }
114 }
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2654
#define IsUserHandle(h)
Definition: ObHandle.c:25
#define ZwCurrentProcess()
LONG NTSTATUS
Definition: precomp.h:26
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:245
#define DUPLICATE_SAME_ACCESS
Status
Definition: gdiplustypes.h:24
_In_ PWDFDEVICE_INIT _In_ PWDF_REMOVE_LOCK_OPTIONS Options
Definition: wdfdevice.h:3531
#define trace
Definition: atltest.h:70
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_INHERIT
Definition: winternl.h:225
#define CheckObject(Handle, Pointers, Handles, Attrib, Access)
Definition: ObHandle.c:12
NTSTATUS NTAPI ObCloseHandle(IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: obhandle.c:3376
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define IsKernelHandle(h)
Definition: ObHandle.c:26
#define DIRECTORY_ALL_ACCESS
Definition: nt_native.h:1259
#define ok(value,...)
Definition: atltest.h:57
#define DUPLICATE_SAME_ATTRIBUTES
Definition: obtypes.h:153
#define skip(...)
Definition: atltest.h:64
_In_ HANDLE Handle
Definition: extypes.h:390
unsigned int ULONG
Definition: retypes.h:1
#define DIRECTORY_QUERY
Definition: nt_native.h:1254
#define ok_eq_hex(value, expected)
#define STATUS_SUCCESS
Definition: shellext.h:65
_In_ PSECURITY_SUBJECT_CONTEXT _In_ BOOLEAN _In_ ACCESS_MASK _In_ ACCESS_MASK _Outptr_opt_ PPRIVILEGE_SET _In_ PGENERIC_MAPPING _In_ KPROCESSOR_MODE _Out_ PACCESS_MASK GrantedAccess
Definition: sefuncs.h:13
#define UL
Definition: tui.h:82
struct test_data Tests[]
ULONG ACCESS_MASK
Definition: nt_native.h:40
static HANDLE SystemProcessHandle
Definition: ObHandle.c:28

Referenced by START_TEST().

Variable Documentation

◆ SystemProcessHandle

HANDLE SystemProcessHandle
static

Definition at line 28 of file ObHandle.c.

Referenced by START_TEST(), and TestDuplicate().