-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathobclose.c
544 lines (377 loc) · 13.4 KB
/
obclose.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
You may only use this code if you agree to the terms of the Windows Research Kernel Source Code License agreement (see License.txt).
If you do not agree to the terms, do not use the code.
Module Name:
obclose.c
Abstract:
Object close system service
--*/
#include "obp.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE,NtMakeTemporaryObject)
#pragma alloc_text(PAGE,NtClose)
#pragma alloc_text(PAGE,ObMakeTemporaryObject)
#pragma alloc_text(PAGE,ObpCloseHandleTableEntry)
#pragma alloc_text(PAGE,ObCloseHandle)
#pragma alloc_text(PAGE,ObpCloseHandle)
#endif
//
// Indicates if auditing is enabled so we have to close down the object
// audit alarm
//
extern BOOLEAN SepAdtAuditingEnabled;
NTSTATUS
ObpCloseHandleTableEntry (
IN PHANDLE_TABLE ObjectTable,
IN PHANDLE_TABLE_ENTRY ObjectTableEntry,
IN HANDLE Handle,
IN KPROCESSOR_MODE PreviousMode,
IN BOOLEAN Rundown
)
/*++
Routine Description:
This function is used to close a handle table entry
Arguments:
ObjectTableEntry - Supplies the entry being closed. It must be locked
PreviousMode - Mode of caller
Rundown - Called as part of process rundown, ignore protected handles in this mode
Return Value:
NTSTATUS.
--*/
{
POBJECT_HEADER ObjectHeader;
POBJECT_TYPE ObjectType;
PVOID Object;
ULONG CapturedGrantedAccess;
ULONG CapturedAttributes;
#if DBG
KIRQL SaveIrql;
#endif // DBG
//
// From the object table entry we can grab a pointer to the object
// header, get its type and its body
//
ObjectHeader = (POBJECT_HEADER)(((ULONG_PTR)(ObjectTableEntry->Object)) & ~OBJ_HANDLE_ATTRIBUTES);
ObjectType = ObjectHeader->Type;
Object = &ObjectHeader->Body;
//
// If the object type specifies an okay to close procedure then we
// need to invoke that callback. If the callback doesn't want us to
// close handle then unlock the object table and return the error
// to our caller
//
if (ObjectType->TypeInfo.OkayToCloseProcedure != NULL) {
ObpBeginTypeSpecificCallOut( SaveIrql );
if (!(*ObjectType->TypeInfo.OkayToCloseProcedure)( PsGetCurrentProcess(),
Object,
Handle,
PreviousMode )) {
ObpEndTypeSpecificCallOut( SaveIrql, "NtClose", ObjectType, Object );
ExUnlockHandleTableEntry( ObjectTable, ObjectTableEntry );
return STATUS_HANDLE_NOT_CLOSABLE;
}
ObpEndTypeSpecificCallOut( SaveIrql, "NtClose", ObjectType, Object );
}
CapturedAttributes = ObpGetHandleAttributes(ObjectTableEntry);
//
// If the previous mode was user and the handle is protected from
// being closed, then we'll either raise or return an error depending
// on the global flags and debugger port situation.
//
if ((CapturedAttributes & OBJ_PROTECT_CLOSE) != 0 && Rundown == FALSE) {
if (PreviousMode != KernelMode) {
ExUnlockHandleTableEntry( ObjectTable, ObjectTableEntry );
if (!KeIsAttachedProcess() &&
((NtGlobalFlag & FLG_ENABLE_CLOSE_EXCEPTIONS) ||
(PsGetCurrentProcess()->DebugPort != NULL) ||
(ObjectTable->DebugInfo != NULL))) {
//
// Raise and exception in user mode
//
return KeRaiseUserException(STATUS_HANDLE_NOT_CLOSABLE);
} else {
return STATUS_HANDLE_NOT_CLOSABLE;
}
} else {
KeBugCheckEx(INVALID_KERNEL_HANDLE, (ULONG_PTR)Handle, 0, 0, 0);
}
}
//
// Get the granted access for the handle
//
#if i386
if (NtGlobalFlag & FLG_KERNEL_STACK_TRACE_DB) {
CapturedGrantedAccess = ObpTranslateGrantedAccessIndex( ObjectTableEntry->GrantedAccessIndex );
} else {
CapturedGrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
}
#else
CapturedGrantedAccess = ObpDecodeGrantedAccess(ObjectTableEntry->GrantedAccess);
#endif // i386
//
// Now remove the handle from the handle table
//
ExDestroyHandle( ObjectTable,
Handle,
ObjectTableEntry );
//
// perform any auditing required
//
//
// Extract the value of the GenerateOnClose bit stored
// after object open auditing is performed. This value
// was stored by a call to ObSetGenerateOnClosed.
//
if (CapturedAttributes & OBJ_AUDIT_OBJECT_CLOSE) {
if ( SepAdtAuditingEnabled ) {
SeCloseObjectAuditAlarm( Object,
(HANDLE)((ULONG_PTR)Handle & ~OBJ_HANDLE_TAGBITS), // Mask off the tagbits defined for OB objects.
TRUE );
}
}
//
// Since we took the handle away we need to decrement the objects
// handle count, and remove a reference
//
ObpDecrementHandleCount( PsGetCurrentProcess(),
ObjectHeader,
ObjectType,
CapturedGrantedAccess );
ObDereferenceObject( Object );
//
// And return to our caller
//
return STATUS_SUCCESS;
}
NTSTATUS
ObpCloseHandle (
IN HANDLE Handle,
IN KPROCESSOR_MODE PreviousMode
)
/*++
Routine Description:
This function is used to close access to the specified handle with the given mode
Arguments:
Handle - Supplies the handle being closed
PreviousMode - Processor mode to be used in the handle access checks.
CanNotRaise - We are not allowed to do a user mode raise.
Return Value:
An appropriate status value
--*/
{
PHANDLE_TABLE ObjectTable;
PHANDLE_TABLE_ENTRY ObjectTableEntry;
NTSTATUS Status;
BOOLEAN AttachedToProcess = FALSE;
KAPC_STATE ApcState;
PETHREAD CurrentThread;
PEPROCESS CurrentProcess;
ObpValidateIrql( "NtClose" );
CurrentThread = PsGetCurrentThread ();
CurrentProcess = PsGetCurrentProcessByThread (CurrentThread);
//
// For the current process we will grab its handle/object table and
// translate the handle to its corresponding table entry. If the
// call is successful it also lock down the handle table. But first
// check for a kernel handle and attach and use that table if so.
//
if (IsKernelHandle( Handle, PreviousMode )) {
Handle = DecodeKernelHandle( Handle );
ObjectTable = ObpKernelHandleTable;
//
// Go to the system process if we have to
//
if (CurrentProcess != PsInitialSystemProcess) {
KeStackAttachProcess (&PsInitialSystemProcess->Pcb, &ApcState);
AttachedToProcess = TRUE;
}
} else {
ObjectTable = CurrentProcess->ObjectTable;
}
//
// Protect ourselves from being interrupted while we hold a handle table
// entry lock
//
KeEnterCriticalRegionThread(&CurrentThread->Tcb);
ObjectTableEntry = ExMapHandleToPointer( ObjectTable,
Handle );
//
// Check that the specified handle is legitimate otherwise we can
// assume the caller just passed in some bogus handle value
//
if (ObjectTableEntry != NULL) {
Status = ObpCloseHandleTableEntry (ObjectTable, ObjectTableEntry, Handle, PreviousMode, FALSE);
KeLeaveCriticalRegionThread(&CurrentThread->Tcb);
//
// If we are attached to the system process then detach
//
if (AttachedToProcess) {
KeUnstackDetachProcess(&ApcState);
AttachedToProcess = FALSE;
}
} else {
KeLeaveCriticalRegionThread(&CurrentThread->Tcb);
//
// At this point the input handle did not translate to a valid
// object table entry
//
//
// If we are attached to the system process then return
// back to our caller
//
if (AttachedToProcess) {
KeUnstackDetachProcess(&ApcState);
AttachedToProcess = FALSE;
}
//
// Now if the handle is not null and it does not represent the
// current thread or process then if we're user mode we either raise
// or return an error
//
if ((Handle != NULL) &&
(Handle != NtCurrentThread()) &&
(Handle != NtCurrentProcess())) {
if (PreviousMode != KernelMode) {
if ((NtGlobalFlag & FLG_ENABLE_CLOSE_EXCEPTIONS) ||
(CurrentProcess->DebugPort != NULL) ||
(ObjectTable->DebugInfo != NULL)) {
if (!KeIsAttachedProcess()) {
return KeRaiseUserException (STATUS_INVALID_HANDLE);
} else {
return STATUS_INVALID_HANDLE;
}
}
} else {
//
// bugcheck here if kernel debugger is enabled and if kernel mode code is
// closing a bogus handle and process is not exiting. Ignore
// if no PEB as this occurs if process is killed before
// really starting.
//
if ((!PsIsThreadTerminating(CurrentThread)) &&
(CurrentProcess->Peb != NULL)) {
if (KdDebuggerEnabled) {
KeBugCheckEx(INVALID_KERNEL_HANDLE, (ULONG_PTR)Handle, 1, 0, 0);
}
}
}
}
Status = STATUS_INVALID_HANDLE;
}
return Status;
}
NTSTATUS
ObCloseHandle (
__in HANDLE Handle,
__in KPROCESSOR_MODE PreviousMode
)
/*++
Routine Description:
This function is used to close access to the specified handle with the given mode
Arguments:
Handle - Supplies the handle being closed
PreviousMode - Processor mode to be used in the handle access checks.
Return Value:
An appropriate status value
--*/
{
return ObpCloseHandle (Handle,
PreviousMode);
}
NTSTATUS
NtClose (
__in HANDLE Handle
)
/*++
Routine Description:
This function is used to close access to the specified handle
Arguments:
Handle - Supplies the handle being closed
Return Value:
An appropriate status value
--*/
{
return ObpCloseHandle (Handle,
KeGetPreviousMode ());
}
NTSTATUS
NtMakeTemporaryObject (
__in HANDLE Handle
)
/*++
Routine Description:
This routine makes the specified object non permanent.
Arguments:
Handle - Supplies a handle to the object being modified
Return Value:
An appropriate status value.
--*/
{
KPROCESSOR_MODE PreviousMode;
NTSTATUS Status;
PVOID Object;
OBJECT_HANDLE_INFORMATION HandleInformation;
PAGED_CODE();
//
// Get previous processor mode and probe output argument if necessary.
//
PreviousMode = KeGetPreviousMode();
Status = ObReferenceObjectByHandle( Handle,
DELETE,
(POBJECT_TYPE)NULL,
PreviousMode,
&Object,
&HandleInformation );
if (!NT_SUCCESS( Status )) {
return( Status );
}
//
// Make the object temporary. Note that the object should still
// have a name and directory entry because its handle count is not
// zero
//
ObMakeTemporaryObject( Object );
//
// Check if we need to generate a delete object audit/alarm
//
if (HandleInformation.HandleAttributes & OBJ_AUDIT_OBJECT_CLOSE) {
SeDeleteObjectAuditAlarm( Object,
Handle );
}
ObDereferenceObject( Object );
return( Status );
}
VOID
ObMakeTemporaryObject (
__in PVOID Object
)
/*++
Routine Description:
This routine removes the name of the object from its parent
directory. The object is only removed if it has a non zero
handle count and a name. Otherwise the object is simply
made non permanent
Arguments:
Object - Supplies the object being modified
Return Value:
None.
--*/
{
POBJECT_HEADER ObjectHeader;
POBJECT_TYPE ObjectType;
PAGED_CODE();
ObjectHeader = OBJECT_TO_OBJECT_HEADER( Object );
ObjectType = ObjectHeader->Type;
//
// Other bits are set in this flags field by the handle database code. Synchronize with that.
//
ObpLockObject( ObjectHeader );
ObjectHeader->Flags &= ~OB_FLAG_PERMANENT_OBJECT;
ObpUnlockObject( ObjectHeader );
//
// Now delete the object name if no more handles are present.
//
ObpDeleteNameCheck( Object );
return;
}