Skip to content

Commit 72b98da

Browse files
committed
[NTOS:EX] Fix bugs in NtCreateEvent
- Validate EventType - Cleanup on failure - Add ASSERTs in KeInitializeEvent and KeInitializeTimerEx
1 parent 804fdad commit 72b98da

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

ntoskrnl/ex/event.c

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ NtCreateEvent(OUT PHANDLE EventHandle,
107107
DPRINT("NtCreateEvent(0x%p, 0x%x, 0x%p)\n",
108108
EventHandle, DesiredAccess, ObjectAttributes);
109109

110+
/* Validate the event type */
111+
if ((EventType != NotificationEvent) &&
112+
(EventType != SynchronizationEvent))
113+
{
114+
return STATUS_INVALID_PARAMETER;
115+
}
116+
110117
/* Check if we were called from user-mode */
111118
if (PreviousMode != KernelMode)
112119
{
@@ -134,40 +141,41 @@ NtCreateEvent(OUT PHANDLE EventHandle,
134141
0,
135142
0,
136143
(PVOID*)&Event);
144+
if (!NT_SUCCESS(Status))
145+
{
146+
DPRINT1("ObCreateObject failed: 0x%X\n", Status);
147+
return Status;
148+
}
137149

138-
/* Check for Success */
139-
if (NT_SUCCESS(Status))
150+
/* Initialize the Event */
151+
KeInitializeEvent(Event, EventType, InitialState);
152+
153+
/* Insert it */
154+
Status = ObInsertObject((PVOID)Event,
155+
NULL,
156+
DesiredAccess,
157+
0,
158+
NULL,
159+
&hEvent);
160+
if (!NT_SUCCESS(Status))
140161
{
141-
/* Initialize the Event */
142-
KeInitializeEvent(Event,
143-
EventType,
144-
InitialState);
145-
146-
/* Insert it */
147-
Status = ObInsertObject((PVOID)Event,
148-
NULL,
149-
DesiredAccess,
150-
0,
151-
NULL,
152-
&hEvent);
153-
154-
/* Check for success */
155-
if (NT_SUCCESS(Status))
156-
{
157-
/* Enter SEH for return */
158-
_SEH2_TRY
159-
{
160-
/* Return the handle to the caller */
161-
*EventHandle = hEvent;
162-
}
163-
_SEH2_EXCEPT(ExSystemExceptionFilter())
164-
{
165-
/* Get the exception code */
166-
Status = _SEH2_GetExceptionCode();
167-
}
168-
_SEH2_END;
169-
}
162+
DPRINT1("ObInsertObject failed: 0x%X\n", Status);
163+
/* Note: ObInsertObject dereferences Event on failure */
164+
return Status;
165+
}
166+
167+
/* Enter SEH for return */
168+
_SEH2_TRY
169+
{
170+
/* Return the handle to the caller */
171+
*EventHandle = hEvent;
172+
}
173+
_SEH2_EXCEPT(ExSystemExceptionFilter())
174+
{
175+
/* Get the exception code */
176+
Status = _SEH2_GetExceptionCode();
170177
}
178+
_SEH2_END;
171179

172180
/* Return Status */
173181
return Status;

ntoskrnl/ke/eventobj.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ KeInitializeEvent(OUT PKEVENT Event,
3737
IN BOOLEAN State)
3838
{
3939
/* Initialize the Dispatcher Header */
40-
Event->Header.Type = Type;
40+
ASSERT((Type == NotificationEvent) || (Type == SynchronizationEvent));
41+
Event->Header.Type = EventNotificationObject + Type;
4142
//Event->Header.Signalling = FALSE; // fails in kmtest
4243
Event->Header.Size = sizeof(KEVENT) / sizeof(ULONG);
4344
Event->Header.SignalState = State;

ntoskrnl/ke/timerobj.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ KeInitializeTimerEx(OUT PKTIMER Timer,
249249
"NotificationTimer" : "SynchronizationTimer");
250250

251251
/* Initialize the Dispatch Header */
252+
ASSERT((Type == NotificationTimer) || (Type == SynchronizationTimer));
252253
Timer->Header.Type = TimerNotificationObject + Type;
253254
//Timer->Header.TimerControlFlags = 0; // win does not init this field
254255
Timer->Header.Hand = sizeof(KTIMER) / sizeof(ULONG);

0 commit comments

Comments
 (0)