ReactOS 0.4.15-dev-7674-gc0b4db1
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 \
{ \
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)
@ ObjectBasicInformation
Definition: DriverTester.h:54
#define NULL
Definition: types.h:112
ULONG Handle
Definition: gdb_input.c:15
Status
Definition: gdiplustypes.h:25
#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,
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 */
182 /* NtClose must accept everything */
183 DPRINT("Closing null handle (NtClose)\n");
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");
198 DPRINT("Closing 123 kernel handle (NtClose)\n");
199 Status = NtClose(LongToHandle(123 | 0x80000000));
201
202 /* ObCloseHandle with UserMode accepts everything */
206 DPRINT("Closing null kernel handle (ObCloseHandle, UserMode)\n");
209 DPRINT("Closing -1 handle (ObCloseHandle, UserMode)\n");
212 DPRINT("Closing -1 kernel handle (ObCloseHandle, UserMode)\n");
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");
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 */
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}
#define CheckObject(Handle, Pointers, Handles, Attrib, Access)
Definition: ObHandle.c:12
#define IsKernelHandle(h)
Definition: ObHandle.c:26
static HANDLE SystemProcessHandle
Definition: ObHandle.c:28
#define IsUserHandle(h)
Definition: ObHandle.c:25
static VOID TestDuplicate(_In_ HANDLE Handle)
Definition: ObHandle.c:32
#define ok_eq_hex(value, expected)
Definition: apitest.h:76
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
LONG NTSTATUS
Definition: precomp.h:26
int null(void)
Definition: ftp.c:1794
#define LongToHandle(h)
Definition: basetsd.h:82
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
GLdouble n
Definition: glext.h:7729
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define KmtStartSeh()
Definition: kmt_test.h:282
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:288
if(dx< 0)
Definition: linetemp.h:194
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define KernelMode
Definition: asm.h:34
#define UserMode
Definition: asm.h:35
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
NTSYSAPI NTSTATUS NTAPI ZwCreateDirectoryObject(_Out_ PHANDLE DirectoryHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
#define PROCESS_ALL_ACCESS
Definition: nt_native.h:1324
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define DIRECTORY_ALL_ACCESS
Definition: nt_native.h:1259
POBJECT_TYPE PsProcessType
Definition: process.c:20
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:245
NTSTATUS NTAPI ObCloseHandle(IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: obhandle.c:3379
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:2742
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
#define DPRINT
Definition: sndvol32.h:71
#define UL
Definition: tui.h:148

◆ 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;
43 ULONG ExpectedAttributes;
44 } Tests[] =
45 {
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}
struct test_data Tests[]
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define trace
Definition: atltest.h:70
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 OBJ_INHERIT
Definition: winternl.h:225
#define DUPLICATE_SAME_ATTRIBUTES
Definition: obtypes.h:153
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define DIRECTORY_QUERY
Definition: nt_native.h:1254
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658
_In_ PWDFDEVICE_INIT _In_ PWDF_REMOVE_LOCK_OPTIONS Options
Definition: wdfdevice.h:3534
#define DUPLICATE_SAME_ACCESS
_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:20
#define ZwCurrentProcess()

Referenced by START_TEST().

Variable Documentation

◆ SystemProcessHandle

HANDLE SystemProcessHandle
static

Definition at line 28 of file ObHandle.c.

Referenced by START_TEST(), and TestDuplicate().