ReactOS  0.4.11-dev-195-gef016bf
win32k.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Kernel
4  * FILE: ntoskrnl/ex/win32k.c
5  * PURPOSE: Executive Win32 Object Support (Desktop/WinStation)
6  * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7  */
8 
9 #include <ntoskrnl.h>
10 #define NDEBUG
11 #include <debug.h>
12 
13 #if defined (ALLOC_PRAGMA)
14 #pragma alloc_text(INIT, ExpWin32kInit)
15 #endif
16 
18 {
21 
22 
23 /* DATA **********************************************************************/
24 
27 
29 {
34 };
35 
37 {
42 };
43 
51 
52 /* FUNCTIONS ****************************************************************/
53 
55 NTAPI
58  _In_ PKWIN32_SESSION_CALLOUT CalloutProcedure,
60 {
61  PWIN32_KERNEL_OBJECT_HEADER Win32ObjectHeader;
62  PVOID SessionEntry = NULL;
65 
66  /* The objects have a common header. And the kernel accesses it!
67  Thanks MS for this kind of retarded "design"! */
68  Win32ObjectHeader = Object;
69 
70  /* Check if we are not already in the correct session */
71  if (!PsGetCurrentProcess()->ProcessInSession ||
72  (PsGetCurrentProcessSessionId() != Win32ObjectHeader->SessionId))
73  {
74  /* Get the session from the objects session Id */
75  DPRINT("SessionId == %d\n", Win32ObjectHeader->SessionId);
76  SessionEntry = MmGetSessionById(Win32ObjectHeader->SessionId);
77  if (SessionEntry == NULL)
78  {
79  /* The requested session does not even exist! */
80  ASSERT(FALSE);
81  return STATUS_NOT_FOUND;
82  }
83 
84  /* Attach to the session */
85  Status = MmAttachSession(SessionEntry, &ApcState);
86  if (!NT_SUCCESS(Status))
87  {
88  DPRINT1("Could not attach to 0x%p, object %p, callout 0x%p\n",
89  SessionEntry,
90  Win32ObjectHeader,
91  CalloutProcedure);
92 
93  /* Cleanup and return */
94  MmQuitNextSession(SessionEntry);
95  ASSERT(FALSE);
96  return Status;
97  }
98  }
99 
100  /* Call the callout routine */
101  Status = CalloutProcedure(Parameter);
102 
103  /* Check if we have a session */
104  if (SessionEntry != NULL)
105  {
106  /* Detach from the session and quit using it */
107  MmDetachSession(SessionEntry, &ApcState);
108  MmQuitNextSession(SessionEntry);
109  }
110 
111  /* Return the callback status */
112  return Status;
113 }
114 
115 BOOLEAN
116 NTAPI
118  IN PVOID Object,
119  IN HANDLE Handle,
121 {
124 
125  Parameters.Process = Process;
126  Parameters.Object = Object;
127  Parameters.Handle = Handle;
128  Parameters.PreviousMode = AccessMode;
129 
130  Status = ExpWin32SessionCallout(Object,
132  &Parameters);
133 
134  return NT_SUCCESS(Status);
135 }
136 
137 BOOLEAN
138 NTAPI
140  IN PVOID Object,
141  IN HANDLE Handle,
143 {
146 
147  Parameters.Process = Process;
148  Parameters.Object = Object;
149  Parameters.Handle = Handle;
150  Parameters.PreviousMode = AccessMode;
151 
152  Status = ExpWin32SessionCallout(Object,
154  &Parameters);
155 
156  return NT_SUCCESS(Status);
157 }
158 
159 VOID
160 NTAPI
162 {
164 
165  /* Fill out the callback structure */
166  Parameters.Object = DeletedObject;
167 
168  ExpWin32SessionCallout(DeletedObject,
170  &Parameters);
171 }
172 
173 NTSTATUS
174 NTAPI
180  IN OUT PUNICODE_STRING CompleteName,
183  IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
184  OUT PVOID *Object)
185 {
187 
188  /* Fill out the callback structure */
189  Parameters.ParseObject = ParseObject;
190  Parameters.ObjectType = ObjectType;
191  Parameters.AccessState = AccessState;
192  Parameters.AccessMode = AccessMode;
193  Parameters.Attributes = Attributes;
194  Parameters.CompleteName = CompleteName;
195  Parameters.RemainingName = RemainingName;
196  Parameters.Context = Context;
197  Parameters.SecurityQos = SecurityQos;
198  Parameters.Object = Object;
199 
200  return ExpWin32SessionCallout(ParseObject,
202  &Parameters);
203 }
204 VOID
205 NTAPI
206 ExpDesktopDelete(PVOID DeletedObject)
207 {
209 
210  /* Fill out the callback structure */
211  Parameters.Object = DeletedObject;
212 
213  ExpWin32SessionCallout(DeletedObject,
215  &Parameters);
216 }
217 
218 NTSTATUS
219 NTAPI
222  IN PVOID ObjectBody,
224  IN ULONG HandleCount)
225 {
227 
228  Parameters.OpenReason = Reason;
229  Parameters.Process = Process;
230  Parameters.Object = ObjectBody;
231  Parameters.GrantedAccess = GrantedAccess;
232  Parameters.HandleCount = HandleCount;
233 
234  return ExpWin32SessionCallout(ObjectBody,
236  &Parameters);
237 }
238 
239 VOID
240 NTAPI
242  IN PVOID Object,
245  IN ULONG SystemHandleCount)
246 {
248 
249  Parameters.Process = Process;
250  Parameters.Object = Object;
251  Parameters.AccessMask = GrantedAccess;
253  Parameters.SystemHandleCount = SystemHandleCount;
254 
255  ExpWin32SessionCallout(Object,
257  &Parameters);
258 }
259 
260 BOOLEAN
262 NTAPI
264 {
265  OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
268  DPRINT("Creating Win32 Object Types\n");
269 
270  /* Create the window station Object Type */
271  RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
272  RtlInitUnicodeString(&Name, L"WindowStation");
273  ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
274  ObjectTypeInitializer.GenericMapping = ExpWindowStationMapping;
275  ObjectTypeInitializer.PoolType = NonPagedPool;
276  ObjectTypeInitializer.DeleteProcedure = ExpWinStaObjectDelete;
277  ObjectTypeInitializer.ParseProcedure = ExpWinStaObjectParse;
278  ObjectTypeInitializer.OkayToCloseProcedure = ExpWindowStationOkToClose;
279  ObjectTypeInitializer.SecurityRequired = TRUE;
280  ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK |
281  OBJ_PERMANENT |
283  ObjectTypeInitializer.ValidAccessMask = STANDARD_RIGHTS_REQUIRED;
284  Status = ObCreateObjectType(&Name,
285  &ObjectTypeInitializer,
286  NULL,
287  &ExWindowStationObjectType);
288  if (!NT_SUCCESS(Status)) return FALSE;
289 
290  /* Create desktop object type */
291  RtlInitUnicodeString(&Name, L"Desktop");
292  ObjectTypeInitializer.GenericMapping = ExpDesktopMapping;
293  ObjectTypeInitializer.DeleteProcedure = ExpDesktopDelete;
294  ObjectTypeInitializer.ParseProcedure = NULL;
295  ObjectTypeInitializer.OkayToCloseProcedure = ExpDesktopOkToClose;
296  ObjectTypeInitializer.OpenProcedure = ExpDesktopOpen;
297  ObjectTypeInitializer.CloseProcedure = ExpDesktopClose;
298  Status = ObCreateObjectType(&Name,
299  &ObjectTypeInitializer,
300  NULL,
301  &ExDesktopObjectType);
302  if (!NT_SUCCESS(Status)) return FALSE;
303 
304  return TRUE;
305 }
306 
307 /* EOF */
DWORD *typedef PVOID
Definition: winlogon.h:61
NTSTATUS NTAPI ExpWinStaObjectParse(IN PVOID ParseObject, IN PVOID ObjectType, IN OUT PACCESS_STATE AccessState, IN KPROCESSOR_MODE AccessMode, IN ULONG Attributes, IN OUT PUNICODE_STRING CompleteName, IN OUT PUNICODE_STRING RemainingName, IN OUT PVOID Context OPTIONAL, IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL, OUT PVOID *Object)
Definition: win32k.c:175
ObjectType
Definition: metafile.c:80
#define IN
Definition: typedefs.h:38
NTSTATUS NTAPI ObCreateObjectType(IN PUNICODE_STRING TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, IN PVOID Reserved, OUT POBJECT_TYPE *ObjectType)
Definition: oblife.c:1034
#define TRUE
Definition: types.h:120
KAPC_STATE
Definition: ketypes.h:1273
POBJECT_TYPE ExWindowStationObjectType
Definition: win32k.c:25
#define STANDARD_RIGHTS_WRITE
Definition: nt_native.h:66
OB_OPEN_REASON OpenReason
Definition: pstypes.h:1569
OB_OKAYTOCLOSE_METHOD OkayToCloseProcedure
Definition: obtypes.h:373
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
PVOID NTAPI MmGetSessionById(_In_ ULONG SessionId)
Definition: session.c:1045
#define OBJ_OPENLINK
Definition: winternl.h:230
POBJECT_TYPE ExDesktopObjectType
Definition: win32k.c:26
#define OBJ_PERMANENT
Definition: winternl.h:226
NTSTATUS NTAPI ExpWin32SessionCallout(_In_ PVOID Object, _In_ PKWIN32_SESSION_CALLOUT CalloutProcedure, _Inout_opt_ PVOID Parameter)
Definition: win32k.c:56
KPROCESSOR_MODE AccessMode
Definition: pstypes.h:1603
PKWIN32_SESSION_CALLOUT ExpWindowStationObjectOkToClose
Definition: win32k.c:46
_In_ PVOID Parameter
Definition: ldrtypes.h:239
#define STANDARD_RIGHTS_EXECUTE
Definition: nt_native.h:67
ULONG Reason
Definition: ntimage.h:533
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
VOID NTAPI ExpDesktopClose(IN PEPROCESS Process OPTIONAL, IN PVOID Object, IN ACCESS_MASK GrantedAccess, IN ULONG ProcessHandleCount, IN ULONG SystemHandleCount)
Definition: win32k.c:241
_In_ PEPROCESS _In_ KPROCESSOR_MODE AccessMode
Definition: mmfuncs.h:396
#define PsGetCurrentProcess
Definition: psfuncs.h:17
smooth NULL
Definition: ftsmooth.c:416
#define OBJ_EXCLUSIVE
Definition: winternl.h:227
void DPRINT(...)
Definition: polytest.cpp:61
OB_CLOSE_METHOD CloseProcedure
Definition: obtypes.h:368
VOID NTAPI MmQuitNextSession(_Inout_ PVOID SessionEntry)
Definition: session.c:1025
PKWIN32_SESSION_CALLOUT ExpWindowStationObjectDelete
Definition: win32k.c:45
struct _WIN32_KERNEL_OBJECT_HEADER WIN32_KERNEL_OBJECT_HEADER
_In_ HANDLE Handle
Definition: extypes.h:390
unsigned char BOOLEAN
#define STATUS_NOT_FOUND
Definition: shellext.h:55
ULONG NTAPI PsGetCurrentProcessSessionId(VOID)
Definition: process.c:1133
PKWIN32_SESSION_CALLOUT ExpDesktopObjectOpen
Definition: win32k.c:49
GENERIC_MAPPING ExpWindowStationMapping
Definition: win32k.c:28
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
OB_PARSE_METHOD ParseProcedure
Definition: obtypes.h:370
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
struct _WIN32_KERNEL_OBJECT_HEADER * PWIN32_KERNEL_OBJECT_HEADER
static IUnknown Object
Definition: main.c:512
_In_opt_ PVOID _In_opt_ PUNICODE_STRING _In_ PSECURITY_DESCRIPTOR _In_ PACCESS_STATE AccessState
Definition: sefuncs.h:414
_In_ PUNICODE_STRING Name
Definition: mrx.h:218
BOOLEAN NTAPI ExpDesktopOkToClose(IN PEPROCESS Process OPTIONAL, IN PVOID Object, IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: win32k.c:117
VOID NTAPI ExpDesktopDelete(PVOID DeletedObject)
Definition: win32k.c:206
OB_OPEN_METHOD OpenProcedure
Definition: obtypes.h:367
static const WCHAR L[]
Definition: oid.c:1087
PKWIN32_SESSION_CALLOUT ExpDesktopObjectClose
Definition: win32k.c:50
PSECURITY_QUALITY_OF_SERVICE SecurityQos
Definition: pstypes.h:1608
#define STANDARD_RIGHTS_READ
Definition: nt_native.h:65
Status
Definition: gdiplustypes.h:24
GENERIC_MAPPING ExpDesktopMapping
Definition: win32k.c:36
PKWIN32_SESSION_CALLOUT ExpDesktopObjectDelete
Definition: win32k.c:48
enum _OB_OPEN_REASON OB_OPEN_REASON
#define _In_
Definition: no_sal2.h:204
VOID NTAPI ExpWinStaObjectDelete(PVOID DeletedObject)
Definition: win32k.c:161
BOOLEAN NTAPI ExpWindowStationOkToClose(IN PEPROCESS Process OPTIONAL, IN PVOID Object, IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: win32k.c:139
DWORD *typedef HANDLE
Definition: winlogon.h:61
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
BOOLEAN INIT_FUNCTION NTAPI ExpWin32kInit(VOID)
Definition: win32k.c:263
_Out_ PKAPC_STATE ApcState
Definition: mm.h:1471
PKWIN32_SESSION_CALLOUT ExpWindowStationObjectParse
Definition: win32k.c:44
NTSTATUS NTAPI ExpDesktopOpen(IN OB_OPEN_REASON Reason, IN PEPROCESS Process OPTIONAL, IN PVOID ObjectBody, IN ACCESS_MASK GrantedAccess, IN ULONG HandleCount)
Definition: win32k.c:220
#define DPRINT1
Definition: precomp.h:8
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define OUT
Definition: typedefs.h:39
#define STANDARD_RIGHTS_REQUIRED
Definition: nt_native.h:63
struct tagContext Context
Definition: acpixf.h:1014
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
PUNICODE_STRING RemainingName
Definition: pstypes.h:1606
_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
OB_DELETE_METHOD DeleteProcedure
Definition: obtypes.h:369
_Out_ PUNICODE_STRING CompleteName
Definition: pstypes.h:1605
PKWIN32_SESSION_CALLOUT ExpDesktopObjectOkToClose
Definition: win32k.c:47
IN HDEVINFO IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL
Definition: devinst.c:44
ULONG ACCESS_MASK
Definition: nt_native.h:40
IN SCSI_ADAPTER_CONTROL_TYPE IN PVOID Parameters
Definition: srb.h:488
struct _ACPI_EFI_FILE_HANDLE CHAR16 UINT64 UINT64 Attributes
Definition: acefiex.h:335
NTSTATUS(NTAPI * PKWIN32_SESSION_CALLOUT)(_In_ PVOID Parameter)
Definition: pstypes.h:582
#define _Inout_opt_
Definition: no_sal2.h:258
#define INIT_FUNCTION
Definition: ntoskrnl.h:11
_Inout_ PFCB _Inout_ PUNICODE_STRING RemainingName
Definition: cdprocs.h:806