ReactOS  0.4.14-dev-342-gdc047f9
ObHandle.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS kernel-mode tests
3  * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE: Kernel-Mode Test Suite Object Handle test
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 #define NDEBUG
10 #include <debug.h>
11 
12 #define CheckObject(Handle, Pointers, Handles, Attrib, Access) do \
13 { \
14  PUBLIC_OBJECT_BASIC_INFORMATION ObjectInfo; \
15  Status = ZwQueryObject(Handle, ObjectBasicInformation, \
16  &ObjectInfo, sizeof ObjectInfo, NULL); \
17  ok_eq_hex(Status, STATUS_SUCCESS); \
18  ok_eq_hex(ObjectInfo.Attributes, Attrib); \
19  ok_eq_hex(ObjectInfo.GrantedAccess, Access); \
20  ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
21  ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
22 } while (0)
23 
24 #define KERNEL_HANDLE_FLAG ((ULONG_PTR)0xFFFFFFFF80000000)
25 #define IsUserHandle(h) (((ULONG_PTR)(h) & KERNEL_HANDLE_FLAG) == 0)
26 #define IsKernelHandle(h) (((ULONG_PTR)(h) & KERNEL_HANDLE_FLAG) == KERNEL_HANDLE_FLAG)
27 
29 
30 static
31 VOID
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 }
115 
116 START_TEST(ObHandle)
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
#define ZwCurrentProcess()
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:283
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
enum OPTION_FLAGS Options
Definition: stats.c:44
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:231
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 DUPLICATE_SAME_ACCESS
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)
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
_In_ HANDLE Handle
Definition: extypes.h:390
#define trace
Definition: atltest.h:70
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_INHERIT
Definition: winternl.h:225
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
Status
Definition: gdiplustypes.h:24
#define _In_
Definition: no_sal2.h:204
#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
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK DesiredAccess
Definition: create.c:4157
#define skip(...)
Definition: atltest.h:64
unsigned int ULONG
Definition: retypes.h:1
#define DIRECTORY_QUERY
Definition: nt_native.h:1254
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define ok_eq_hex(value, expected)
_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
return STATUS_SUCCESS
Definition: btrfs.c:2938
START_TEST(ObHandle)
Definition: ObHandle.c:116
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define UL
Definition: tui.h:83
POBJECT_TYPE PsProcessType
Definition: process.c:20
struct test_data Tests[]
ULONG ACCESS_MASK
Definition: nt_native.h:40
static HANDLE SystemProcessHandle
Definition: ObHandle.c:28