xref: /reactos/ntoskrnl/include/internal/ob.h (revision c2c66aff)
1 /*
2 * PROJECT:         ReactOS Kernel
3 * LICENSE:         GPL - See COPYING in the top level directory
4 * FILE:            ntoskrnl/include/internal/ob.h
5 * PURPOSE:         Internal header for the Object Manager
6 * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
7 */
8 
9 //
10 // Define this if you want debugging support
11 //
12 #define _OB_DEBUG_                                      0x00
13 
14 //
15 // These define the Debug Masks Supported
16 //
17 #define OB_HANDLE_DEBUG                                 0x01
18 #define OB_NAMESPACE_DEBUG                              0x02
19 #define OB_SECURITY_DEBUG                               0x04
20 #define OB_REFERENCE_DEBUG                              0x08
21 #define OB_CALLBACK_DEBUG                               0x10
22 
23 //
24 // Debug/Tracing support
25 //
26 #if _OB_DEBUG_
27 #ifdef NEW_DEBUG_SYSTEM_IMPLEMENTED // enable when Debug Filters are implemented
28 #define OBTRACE DbgPrintEx
29 #else
30 #define OBTRACE(x, ...)                                 \
31     if (x & ObpTraceLevel) DbgPrint(__VA_ARGS__)
32 #endif
33 #else
34 #define OBTRACE(x, fmt, ...) DPRINT(fmt, ##__VA_ARGS__)
35 #endif
36 
37 //
38 // Mask to detect GENERIC_XXX access masks being used
39 //
40 #define GENERIC_ACCESS                                  \
41     (GENERIC_READ    |                                  \
42      GENERIC_WRITE   |                                  \
43      GENERIC_EXECUTE |                                  \
44      GENERIC_ALL)
45 
46 //
47 // Handle Bit Flags
48 //
49 #define OBJ_PROTECT_CLOSE                               0x01
50 //#define OBJ_INHERIT                                   0x02
51 #define OBJ_AUDIT_OBJECT_CLOSE                          0x04
52 #define OBJ_HANDLE_ATTRIBUTES                           (OBJ_PROTECT_CLOSE |\
53                                                          OBJ_INHERIT |      \
54                                                          OBJ_AUDIT_OBJECT_CLOSE)
55 
56 //
57 // Identifies a Kernel Handle
58 //
59 #ifdef _WIN64
60 #define KERNEL_HANDLE_FLAG 0xFFFFFFFF80000000ULL
61 #else
62 #define KERNEL_HANDLE_FLAG 0x80000000
63 #endif
64 #define ObpIsKernelHandle(Handle, ProcessorMode)        \
65     ((((ULONG_PTR)(Handle) & KERNEL_HANDLE_FLAG) == KERNEL_HANDLE_FLAG) && \
66      ((ProcessorMode) == KernelMode) && \
67      ((Handle) != NtCurrentProcess()) && \
68      ((Handle) != NtCurrentThread()))
69 
70 //
71 // Converts to and from a Kernel Handle to a normal handle
72 //
73 #define ObKernelHandleToHandle(Handle)                  \
74     (HANDLE)((ULONG_PTR)(Handle) & ~KERNEL_HANDLE_FLAG)
75 #define ObMarkHandleAsKernelHandle(Handle)              \
76     (HANDLE)((ULONG_PTR)(Handle) | KERNEL_HANDLE_FLAG)
77 
78 //
79 // Converts from an EXHANDLE object to a POBJECT_HEADER
80 //
81 #define ObpGetHandleObject(x)                           \
82     ((POBJECT_HEADER)((ULONG_PTR)x->Object & ~OBJ_HANDLE_ATTRIBUTES))
83 
84 //
85 // Recovers the security descriptor from a cached security descriptor header
86 //
87 #define ObpGetHeaderForSd(x) \
88     CONTAINING_RECORD((x), SECURITY_DESCRIPTOR_HEADER, SecurityDescriptor)
89 
90 //
91 // Recovers the security descriptor from a cached security descriptor list entry
92 //
93 #define ObpGetHeaderForEntry(x) \
94     CONTAINING_RECORD((x), SECURITY_DESCRIPTOR_HEADER, Link)
95 
96 //
97 // Context Structures for Ex*Handle Callbacks
98 //
99 typedef struct _OBP_SET_HANDLE_ATTRIBUTES_CONTEXT
100 {
101     KPROCESSOR_MODE PreviousMode;
102     OBJECT_HANDLE_ATTRIBUTE_INFORMATION Information;
103 } OBP_SET_HANDLE_ATTRIBUTES_CONTEXT, *POBP_SET_HANDLE_ATTRIBUTES_CONTEXT;
104 
105 typedef struct _OBP_CLOSE_HANDLE_CONTEXT
106 {
107     PHANDLE_TABLE HandleTable;
108     KPROCESSOR_MODE AccessMode;
109 } OBP_CLOSE_HANDLE_CONTEXT, *POBP_CLOSE_HANDLE_CONTEXT;
110 
111 typedef struct _OBP_FIND_HANDLE_DATA
112 {
113     POBJECT_HEADER ObjectHeader;
114     POBJECT_TYPE ObjectType;
115     POBJECT_HANDLE_INFORMATION HandleInformation;
116 } OBP_FIND_HANDLE_DATA, *POBP_FIND_HANDLE_DATA;
117 
118 //
119 // Cached Security Descriptor Header
120 //
121 typedef struct _SECURITY_DESCRIPTOR_HEADER
122 {
123     LIST_ENTRY Link;
124     ULONG RefCount;
125     ULONG FullHash;
126     QUAD SecurityDescriptor;
127 } SECURITY_DESCRIPTOR_HEADER, *PSECURITY_DESCRIPTOR_HEADER;
128 
129 //
130 // Cached Security Descriptor List
131 //
132 typedef struct _OB_SD_CACHE_LIST
133 {
134     EX_PUSH_LOCK PushLock;
135     LIST_ENTRY Head;
136 } OB_SD_CACHE_LIST, *POB_SD_CACHE_LIST;
137 
138 //
139 // Structure for quick-compare of a DOS Device path
140 //
141 typedef union
142 {
143     WCHAR Name[sizeof(ULARGE_INTEGER) / sizeof(WCHAR)];
144     ULARGE_INTEGER Alignment;
145 } ALIGNEDNAME;
146 
147 //
148 // Private Temporary Buffer for Lookup Routines
149 //
150 #define TAG_OB_TEMP_STORAGE 'tSbO'
151 typedef struct _OB_TEMP_BUFFER
152 {
153     ACCESS_STATE LocalAccessState;
154     OBJECT_CREATE_INFORMATION ObjectCreateInfo;
155     OBP_LOOKUP_CONTEXT LookupContext;
156     AUX_ACCESS_DATA AuxData;
157 } OB_TEMP_BUFFER, *POB_TEMP_BUFFER;
158 
159 //
160 // Startup and Shutdown Functions
161 //
162 BOOLEAN
163 NTAPI
164 ObInitSystem(
165     VOID
166 );
167 
168 VOID
169 NTAPI
170 ObShutdownSystem(
171     VOID
172 );
173 
174 //
175 // Directory Namespace Functions
176 //
177 BOOLEAN
178 NTAPI
179 ObpDeleteEntryDirectory(
180     IN POBP_LOOKUP_CONTEXT Context
181 );
182 
183 BOOLEAN
184 NTAPI
185 ObpInsertEntryDirectory(
186     IN POBJECT_DIRECTORY Parent,
187     IN POBP_LOOKUP_CONTEXT Context,
188     IN POBJECT_HEADER ObjectHeader
189 );
190 
191 PVOID
192 NTAPI
193 ObpLookupEntryDirectory(
194     IN POBJECT_DIRECTORY Directory,
195     IN PUNICODE_STRING Name,
196     IN ULONG Attributes,
197     IN UCHAR SearchShadow,
198     IN POBP_LOOKUP_CONTEXT Context
199 );
200 
201 //
202 // Symbolic Link Functions
203 //
204 VOID
205 NTAPI
206 ObpDeleteSymbolicLink(
207     IN PVOID ObjectBody
208 );
209 
210 NTSTATUS
211 NTAPI
212 ObpParseSymbolicLink(
213     IN PVOID ParsedObject,
214     IN PVOID ObjectType,
215     IN OUT PACCESS_STATE AccessState,
216     IN KPROCESSOR_MODE AccessMode,
217     IN ULONG Attributes,
218     IN OUT PUNICODE_STRING FullPath,
219     IN OUT PUNICODE_STRING RemainingName,
220     IN OUT PVOID Context OPTIONAL,
221     IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
222     OUT PVOID *NextObject
223 );
224 
225 VOID
226 NTAPI
227 ObpCreateSymbolicLinkName(
228     IN POBJECT_SYMBOLIC_LINK SymbolicLink
229 );
230 
231 VOID
232 NTAPI
233 ObpDeleteSymbolicLinkName(
234     IN POBJECT_SYMBOLIC_LINK SymbolicLink
235 );
236 
237 //
238 // Process/Handle Table Init/Rundown
239 //
240 NTSTATUS
241 NTAPI
242 ObInitProcess(
243     IN PEPROCESS Parent OPTIONAL,
244     IN PEPROCESS Process
245 );
246 
247 PHANDLE_TABLE
248 NTAPI
249 ObReferenceProcessHandleTable(
250     IN PEPROCESS Process
251 );
252 
253 VOID
254 NTAPI
255 ObDereferenceProcessHandleTable(
256     IN PEPROCESS Process
257 );
258 
259 VOID
260 NTAPI
261 ObKillProcess(
262     IN PEPROCESS Process
263 );
264 
265 //
266 // Object Lookup Functions
267 //
268 NTSTATUS
269 NTAPI
270 ObpLookupObjectName(
271     IN HANDLE RootHandle OPTIONAL,
272     IN OUT PUNICODE_STRING ObjectName,
273     IN ULONG Attributes,
274     IN POBJECT_TYPE ObjectType,
275     IN KPROCESSOR_MODE AccessMode,
276     IN OUT PVOID ParseContext,
277     IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
278     IN PVOID InsertObject OPTIONAL,
279     IN OUT PACCESS_STATE AccessState,
280     OUT POBP_LOOKUP_CONTEXT LookupContext,
281     OUT PVOID *FoundObject
282 );
283 
284 //
285 // Object Attribute Functions
286 //
287 BOOLEAN
288 NTAPI
289 ObpSetHandleAttributes(
290     IN OUT PHANDLE_TABLE_ENTRY HandleTableEntry,
291     IN ULONG_PTR Context
292 );
293 
294 VOID
295 NTAPI
296 ObQueryDeviceMapInformation(
297     IN PEPROCESS Process,
298     OUT PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo
299 );
300 
301 //
302 // Object Lifetime Functions
303 //
304 VOID
305 NTAPI
306 ObpDeleteObject(
307     IN PVOID Object,
308     IN BOOLEAN CalledFromWorkerThread
309 );
310 
311 LONG
312 FASTCALL
313 ObDereferenceObjectEx(
314     IN PVOID Object,
315     IN LONG Count
316 );
317 
318 LONG
319 FASTCALL
320 ObReferenceObjectEx(
321     IN PVOID Object,
322     IN LONG Count
323 );
324 
325 BOOLEAN
326 FASTCALL
327 ObReferenceObjectSafe(
328     IN PVOID Object
329 );
330 
331 VOID
332 NTAPI
333 ObpReapObject(
334     IN PVOID Unused
335 );
336 
337 VOID
338 FASTCALL
339 ObpSetPermanentObject(
340     IN PVOID ObjectBody,
341     IN BOOLEAN Permanent
342 );
343 
344 VOID
345 NTAPI
346 ObpDeleteNameCheck(
347     IN PVOID Object
348 );
349 
350 VOID
351 NTAPI
352 ObClearProcessHandleTable(
353     IN PEPROCESS Process
354 );
355 
356 NTSTATUS
357 NTAPI
358 ObDuplicateObject(
359     IN PEPROCESS SourceProcess,
360     IN HANDLE SourceHandle,
361     IN PEPROCESS TargetProcess OPTIONAL,
362     IN PHANDLE TargetHandle OPTIONAL,
363     IN ACCESS_MASK DesiredAccess,
364     IN ULONG HandleAttributes,
365     IN ULONG Options,
366     IN KPROCESSOR_MODE PreviousMode
367 );
368 
369 VOID
370 NTAPI
371 ObFreeObjectCreateInfoBuffer(
372     IN POBJECT_CREATE_INFORMATION ObjectCreateInfo
373 );
374 
375 VOID
376 NTAPI
377 ObpFreeObjectNameBuffer(
378     IN PUNICODE_STRING Name
379 );
380 
381 VOID
382 NTAPI
383 ObpDeleteObjectType(
384     IN PVOID Object
385 );
386 
387 //
388 // DOS Devices Functions
389 //
390 NTSTATUS
391 NTAPI
392 ObpCreateDeviceMap(
393     IN HANDLE DirectoryHandle
394 );
395 
396 VOID
397 NTAPI
398 ObDereferenceDeviceMap(
399     IN PEPROCESS Process
400 );
401 
402 VOID
403 FASTCALL
404 ObfDereferenceDeviceMap(
405     IN PDEVICE_MAP DeviceMap
406 );
407 
408 VOID
409 NTAPI
410 ObInheritDeviceMap(
411     IN PEPROCESS Parent,
412     IN PEPROCESS Process
413 );
414 
415 NTSTATUS
416 NTAPI
417 ObpCreateDosDevicesDirectory(
418     VOID
419 );
420 
421 //
422 // Security descriptor cache functions
423 //
424 NTSTATUS
425 NTAPI
426 ObpInitSdCache(
427     VOID
428 );
429 
430 PSECURITY_DESCRIPTOR
431 NTAPI
432 ObpReferenceSecurityDescriptor(
433     IN POBJECT_HEADER ObjectHeader
434 );
435 
436 //
437 // Object Security Routines
438 //
439 BOOLEAN
440 NTAPI
441 ObCheckObjectAccess(
442     IN PVOID Object,
443     IN OUT PACCESS_STATE AccessState,
444     IN BOOLEAN LockHeld,
445     IN KPROCESSOR_MODE AccessMode,
446     OUT PNTSTATUS ReturnedStatus
447 );
448 
449 BOOLEAN
450 NTAPI
451 ObCheckCreateObjectAccess(
452     IN PVOID Object,
453     IN ACCESS_MASK CreateAccess,
454     IN PACCESS_STATE AccessState,
455     IN PUNICODE_STRING ComponentName,
456     IN BOOLEAN LockHeld,
457     IN KPROCESSOR_MODE AccessMode,
458     OUT PNTSTATUS AccessStatus
459 );
460 
461 BOOLEAN
462 NTAPI
463 ObpCheckTraverseAccess(
464     IN PVOID Object,
465     IN ACCESS_MASK TraverseAccess,
466     IN PACCESS_STATE AccessState OPTIONAL,
467     IN BOOLEAN LockHeld,
468     IN KPROCESSOR_MODE AccessMode,
469     OUT PNTSTATUS AccessStatus
470 );
471 
472 BOOLEAN
473 NTAPI
474 ObpCheckObjectReference(
475     IN PVOID Object,
476     IN OUT PACCESS_STATE AccessState,
477     IN BOOLEAN LockHeld,
478     IN KPROCESSOR_MODE AccessMode,
479     OUT PNTSTATUS AccessStatus
480 );
481 
482 //
483 // Default Object Security Callback Routines
484 //
485 NTSTATUS
486 NTAPI
487 ObAssignObjectSecurityDescriptor(
488     IN PVOID Object,
489     IN PSECURITY_DESCRIPTOR SecurityDescriptor OPTIONAL,
490     IN POOL_TYPE PoolType
491 );
492 
493 NTSTATUS
494 NTAPI
495 ObDeassignSecurity(
496     IN OUT PSECURITY_DESCRIPTOR *SecurityDescriptor
497 );
498 
499 NTSTATUS
500 NTAPI
501 ObQuerySecurityDescriptorInfo(
502     IN PVOID Object,
503     IN PSECURITY_INFORMATION SecurityInformation,
504     OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
505     IN OUT PULONG Length,
506     IN PSECURITY_DESCRIPTOR *OutputSecurityDescriptor
507 );
508 
509 NTSTATUS
510 NTAPI
511 ObSetSecurityDescriptorInfo(
512     IN PVOID Object,
513     IN PSECURITY_INFORMATION SecurityInformation,
514     IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
515     IN OUT PSECURITY_DESCRIPTOR *OutputSecurityDescriptor,
516     IN POOL_TYPE PoolType,
517     IN PGENERIC_MAPPING GenericMapping
518 );
519 
520 //
521 // Executive Fast Referencing Functions
522 //
523 VOID
524 FASTCALL
525 ObInitializeFastReference(
526     IN PEX_FAST_REF FastRef,
527     IN PVOID Object
528 );
529 
530 PVOID
531 FASTCALL
532 ObFastReplaceObject(
533     IN PEX_FAST_REF FastRef,
534     IN PVOID Object
535 );
536 
537 PVOID
538 FASTCALL
539 ObFastReferenceObject(
540     IN PEX_FAST_REF FastRef
541 );
542 
543 PVOID
544 FASTCALL
545 ObFastReferenceObjectLocked(
546     IN PEX_FAST_REF FastRef
547 );
548 
549 VOID
550 FASTCALL
551 ObFastDereferenceObject(
552     IN PEX_FAST_REF FastRef,
553     IN PVOID Object
554 );
555 
556 //
557 // Object Create and Object Name Capture Functions
558 //
559 NTSTATUS
560 NTAPI
561 ObpCaptureObjectName(
562     IN PUNICODE_STRING CapturedName,
563     IN PUNICODE_STRING ObjectName,
564     IN KPROCESSOR_MODE AccessMode,
565     IN BOOLEAN AllocateFromLookaside
566 );
567 
568 NTSTATUS
569 NTAPI
570 ObpCaptureObjectCreateInformation(
571     IN POBJECT_ATTRIBUTES ObjectAttributes,
572     IN KPROCESSOR_MODE AccessMode,
573     IN KPROCESSOR_MODE CreatorMode,
574     IN BOOLEAN AllocateFromLookaside,
575     IN POBJECT_CREATE_INFORMATION ObjectCreateInfo,
576     OUT PUNICODE_STRING ObjectName
577 );
578 
579 //
580 // Miscellanea
581 //
582 ULONG
583 NTAPI
584 ObGetProcessHandleCount(
585     IN PEPROCESS Process
586 );
587 
588 //
589 // Global data inside the Object Manager
590 //
591 extern ULONG ObpTraceLevel;
592 extern KEVENT ObpDefaultObject;
593 extern KGUARDED_MUTEX ObpDeviceMapLock;
594 extern POBJECT_TYPE ObpTypeObjectType;
595 extern POBJECT_TYPE ObSymbolicLinkType;
596 extern POBJECT_TYPE ObpTypeObjectType;
597 extern POBJECT_DIRECTORY ObpRootDirectoryObject;
598 extern POBJECT_DIRECTORY ObpTypeDirectoryObject;
599 extern PHANDLE_TABLE ObpKernelHandleTable;
600 extern WORK_QUEUE_ITEM ObpReaperWorkItem;
601 extern volatile PVOID ObpReaperList;
602 extern GENERAL_LOOKASIDE ObpNameBufferLookasideList, ObpCreateInfoLookasideList;
603 extern BOOLEAN IoCountOperations;
604 extern ALIGNEDNAME ObpDosDevicesShortNamePrefix;
605 extern ALIGNEDNAME ObpDosDevicesShortNameRoot;
606 extern UNICODE_STRING ObpDosDevicesShortName;
607 
608 //
609 // Inlined Functions
610 //
611 #include "ob_x.h"
612