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