xref: /reactos/ntoskrnl/include/internal/ob.h (revision fb5d5ecd)
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 NTSTATUS
388 NTAPI
389 ObReferenceFileObjectForWrite(
390     IN HANDLE Handle,
391     IN KPROCESSOR_MODE AccessMode,
392     OUT PFILE_OBJECT *FileObject,
393     OUT POBJECT_HANDLE_INFORMATION HandleInformation
394 );
395 
396 //
397 // DOS Devices Functions
398 //
399 NTSTATUS
400 NTAPI
401 ObpCreateDeviceMap(
402     IN HANDLE DirectoryHandle
403 );
404 
405 VOID
406 NTAPI
407 ObDereferenceDeviceMap(
408     IN PEPROCESS Process
409 );
410 
411 VOID
412 FASTCALL
413 ObfDereferenceDeviceMap(
414     IN PDEVICE_MAP DeviceMap
415 );
416 
417 VOID
418 NTAPI
419 ObInheritDeviceMap(
420     IN PEPROCESS Parent,
421     IN PEPROCESS Process
422 );
423 
424 NTSTATUS
425 NTAPI
426 ObpCreateDosDevicesDirectory(
427     VOID
428 );
429 
430 //
431 // Security descriptor cache functions
432 //
433 NTSTATUS
434 NTAPI
435 ObpInitSdCache(
436     VOID
437 );
438 
439 PSECURITY_DESCRIPTOR
440 NTAPI
441 ObpReferenceSecurityDescriptor(
442     IN POBJECT_HEADER ObjectHeader
443 );
444 
445 //
446 // Object Security Routines
447 //
448 BOOLEAN
449 NTAPI
450 ObCheckObjectAccess(
451     IN PVOID Object,
452     IN OUT PACCESS_STATE AccessState,
453     IN BOOLEAN LockHeld,
454     IN KPROCESSOR_MODE AccessMode,
455     OUT PNTSTATUS ReturnedStatus
456 );
457 
458 BOOLEAN
459 NTAPI
460 ObCheckCreateObjectAccess(
461     IN PVOID Object,
462     IN ACCESS_MASK CreateAccess,
463     IN PACCESS_STATE AccessState,
464     IN PUNICODE_STRING ComponentName,
465     IN BOOLEAN LockHeld,
466     IN KPROCESSOR_MODE AccessMode,
467     OUT PNTSTATUS AccessStatus
468 );
469 
470 BOOLEAN
471 NTAPI
472 ObpCheckTraverseAccess(
473     IN PVOID Object,
474     IN ACCESS_MASK TraverseAccess,
475     IN PACCESS_STATE AccessState OPTIONAL,
476     IN BOOLEAN LockHeld,
477     IN KPROCESSOR_MODE AccessMode,
478     OUT PNTSTATUS AccessStatus
479 );
480 
481 BOOLEAN
482 NTAPI
483 ObpCheckObjectReference(
484     IN PVOID Object,
485     IN OUT PACCESS_STATE AccessState,
486     IN BOOLEAN LockHeld,
487     IN KPROCESSOR_MODE AccessMode,
488     OUT PNTSTATUS AccessStatus
489 );
490 
491 //
492 // Default Object Security Callback Routines
493 //
494 NTSTATUS
495 NTAPI
496 ObAssignObjectSecurityDescriptor(
497     IN PVOID Object,
498     IN PSECURITY_DESCRIPTOR SecurityDescriptor OPTIONAL,
499     IN POOL_TYPE PoolType
500 );
501 
502 NTSTATUS
503 NTAPI
504 ObDeassignSecurity(
505     IN OUT PSECURITY_DESCRIPTOR *SecurityDescriptor
506 );
507 
508 NTSTATUS
509 NTAPI
510 ObQuerySecurityDescriptorInfo(
511     IN PVOID Object,
512     IN PSECURITY_INFORMATION SecurityInformation,
513     OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
514     IN OUT PULONG Length,
515     IN PSECURITY_DESCRIPTOR *OutputSecurityDescriptor
516 );
517 
518 NTSTATUS
519 NTAPI
520 ObSetSecurityDescriptorInfo(
521     IN PVOID Object,
522     IN PSECURITY_INFORMATION SecurityInformation,
523     IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
524     IN OUT PSECURITY_DESCRIPTOR *OutputSecurityDescriptor,
525     IN POOL_TYPE PoolType,
526     IN PGENERIC_MAPPING GenericMapping
527 );
528 
529 //
530 // Executive Fast Referencing Functions
531 //
532 VOID
533 FASTCALL
534 ObInitializeFastReference(
535     IN PEX_FAST_REF FastRef,
536     IN PVOID Object
537 );
538 
539 PVOID
540 FASTCALL
541 ObFastReplaceObject(
542     IN PEX_FAST_REF FastRef,
543     IN PVOID Object
544 );
545 
546 PVOID
547 FASTCALL
548 ObFastReferenceObject(
549     IN PEX_FAST_REF FastRef
550 );
551 
552 PVOID
553 FASTCALL
554 ObFastReferenceObjectLocked(
555     IN PEX_FAST_REF FastRef
556 );
557 
558 VOID
559 FASTCALL
560 ObFastDereferenceObject(
561     IN PEX_FAST_REF FastRef,
562     IN PVOID Object
563 );
564 
565 //
566 // Object Create and Object Name Capture Functions
567 //
568 NTSTATUS
569 NTAPI
570 ObpCaptureObjectName(
571     IN PUNICODE_STRING CapturedName,
572     IN PUNICODE_STRING ObjectName,
573     IN KPROCESSOR_MODE AccessMode,
574     IN BOOLEAN AllocateFromLookaside
575 );
576 
577 NTSTATUS
578 NTAPI
579 ObpCaptureObjectCreateInformation(
580     IN POBJECT_ATTRIBUTES ObjectAttributes,
581     IN KPROCESSOR_MODE AccessMode,
582     IN KPROCESSOR_MODE CreatorMode,
583     IN BOOLEAN AllocateFromLookaside,
584     IN POBJECT_CREATE_INFORMATION ObjectCreateInfo,
585     OUT PUNICODE_STRING ObjectName
586 );
587 
588 //
589 // Miscellanea
590 //
591 ULONG
592 NTAPI
593 ObGetProcessHandleCount(
594     IN PEPROCESS Process
595 );
596 
597 //
598 // Global data inside the Object Manager
599 //
600 extern ULONG ObpTraceLevel;
601 extern KEVENT ObpDefaultObject;
602 extern KGUARDED_MUTEX ObpDeviceMapLock;
603 extern POBJECT_TYPE ObpTypeObjectType;
604 extern POBJECT_TYPE ObSymbolicLinkType;
605 extern POBJECT_TYPE ObpTypeObjectType;
606 extern POBJECT_DIRECTORY ObpRootDirectoryObject;
607 extern POBJECT_DIRECTORY ObpTypeDirectoryObject;
608 extern PHANDLE_TABLE ObpKernelHandleTable;
609 extern WORK_QUEUE_ITEM ObpReaperWorkItem;
610 extern volatile PVOID ObpReaperList;
611 extern GENERAL_LOOKASIDE ObpNameBufferLookasideList, ObpCreateInfoLookasideList;
612 extern BOOLEAN IoCountOperations;
613 extern ALIGNEDNAME ObpDosDevicesShortNamePrefix;
614 extern ALIGNEDNAME ObpDosDevicesShortNameRoot;
615 extern UNICODE_STRING ObpDosDevicesShortName;
616 
617 //
618 // Inlined Functions
619 //
620 #include "ob_x.h"
621