-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNtUtils.Objects.pas
More file actions
778 lines (646 loc) · 22.6 KB
/
NtUtils.Objects.pas
File metadata and controls
778 lines (646 loc) · 22.6 KB
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
unit NtUtils.Objects;
{
This modules provides functions for operations with handles that are common
for all types of kernel objects.
}
interface
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntobapi, NtUtils, DelphiApi.Reflection;
type
TObjectTypeInfo = record
TypeName: String;
[Aggregate] Native: TObjectTypeInformation;
end;
// Close a kernel handle
function NtxClose(hObject: THandle): TNtxStatus;
type
TAutoKernelObjectHelper = class helper for Auto
// Capture ownership of a kernel handle
class function CaptureHandle(hObject: THandle): IHandle; static;
// Capture ownership of a kernel handle in another process
class function CaptureRemoteHandle(
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
hObject: THandle
): IHandle; static;
end;
// Capture ownership of a kernel handle and validate it's within valid range
function NtxCaptureHandle(
out hxObject: IHandle;
hObject: THandle
): TNtxStatus;
// Capture ownership of a kernel handle in another process and validate range
function NtxCaptureRemoteHandle(
out hxObject: IHandle;
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
hObject: THandle
): TNtxStatus;
// ------------------------------ Duplication ------------------------------ //
// Note: all handle duplication routines here support MAXIMUM_ALLOWED
// Duplicate a handle within the current process
function NtxDuplicateHandleLocal(
const hxSource: IHandle;
out hxNewHandle: IHandle;
DesiredAccess: TAccessMask;
HandleAttributes: TObjectAttributesFlags;
Options: TDuplicateOptions;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Duplicate a handle in-place within the current process
function NtxReopenHandle(
var hxHandle: IHandle;
DesiredAccess: TAccessMask;
HandleAttributes: TObjectAttributesFlags = 0;
Options: TDuplicateOptions = DUPLICATE_SAME_ATTRIBUTES;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Duplicate a handle between two processes
function NtxDuplicateHandle(
[Access(PROCESS_DUP_HANDLE)] const hxSourceProcess: IHandle;
hSourceHandle: THandle;
[Access(PROCESS_DUP_HANDLE)] const hxTargetProcess: IHandle;
out hTargetHandle: THandle;
DesiredAccess: TAccessMask;
HandleAttributes: TObjectAttributesFlags;
Options: TDuplicateOptions;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Check if a handle grants an access mask and reopen it if necessary
function NtxEnsureAccessHandle(
var hxHandle: IHandle;
DesiredAccess: TAccessMask;
HandleAttributes: TObjectAttributesFlags = 0;
Options: TDuplicateOptions = DUPLICATE_SAME_ATTRIBUTES;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Retrieve a handle from a process
function NtxDuplicateHandleFrom(
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
hRemoteHandle: THandle;
out hxLocalHandle: IHandle;
DesiredAccess: TAccessMask = 0;
HandleAttributes: TObjectAttributesFlags = 0;
Options: TDuplicateOptions = DUPLICATE_SAME_ACCESS;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Send a handle to a process
function NtxDuplicateHandleTo(
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
const hxLocalHandle: IHandle;
out hRemoteHandle: THandle;
DesiredAccess: TAccessMask = 0;
HandleAttributes: TObjectAttributesFlags = 0;
Options: TDuplicateOptions = DUPLICATE_SAME_ACCESS;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Send a handle to a process and then automatically close it later
function NtxDuplicateHandleToAuto(
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
const hxLocalHandle: IHandle;
out hxRemoteHandle: IHandle;
DesiredAccess: TAccessMask = 0;
HandleAttributes: TObjectAttributesFlags = 0;
Options: TDuplicateOptions = DUPLICATE_SAME_ACCESS;
[opt] AccessMaskType: Pointer = nil
): TNtxStatus;
// Close a handle in a process
function NtxCloseRemoteHandle(
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
hRemoteHandle: THandle
): TNtxStatus;
// Close a handle in a process and ensure it's closed
function NtxCloseRemoteHandleWithCheck(
[Access(PROCESS_DUP_HANDLE)] const hxProcess: IHandle;
hRemoteHandle: THandle
): TNtxStatus;
// ------------------------------ Information ------------------------------ //
type
NtxObject = class abstract
// Query fixed-size information
class function Query<T>(
[Access(0)] const hxObject: IHandle;
InfoClass: TObjectInformationClass;
out Buffer: T
): TNtxStatus; static;
end;
// Query variable-length object information
function NtxQueryObject(
[Access(0)] const hxObject: IHandle;
InfoClass: TObjectInformationClass;
out xMemory: IMemory;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
// Query name of an object
function NtxQueryNameObject(
[Access(0)] const hxObject: IHandle;
out Name: String
): TNtxStatus;
// Query object type information
function NtxQueryTypeObject(
[Access(0)] const hxObject: IHandle;
out Info: TObjectTypeInfo
): TNtxStatus;
// Set flags for a handle
function NtxSetFlagsHandle(
[Access(0)] const hxObject: IHandle;
Inherit: Boolean;
ProtectFromClose: Boolean
): TNtxStatus;
// ------------------------------- Security -------------------------------- //
// Query security descriptor of a kernel object
function NtxQuerySecurityObject(
[Access(OBJECT_READ_SECURITY)] const hxObject: IHandle;
Info: TSecurityInformation;
out SD: ISecurityDescriptor
): TNtxStatus;
// Set security descriptor on a kernel object
function NtxSetSecurityObject(
[Access(OBJECT_WRITE_SECURITY)] const hxObject: IHandle;
Info: TSecurityInformation;
[in] SD: PSecurityDescriptor
): TNtxStatus;
// ----------------------------- Access masks ------------------------------ //
type
TObjectOpener = reference to function (
[out] out hxObject: IHandle;
[in] AccessMask: TAccessMask
): TNtxStatus;
// Determine what is the maximum access that we can get to an object
function RtlxComputeMaximumAccess(
out MaximumAccess: TAccessMask;
ObjectOpener: TObjectOpener;
IsKernelObject: Boolean;
FullAccessMask: TAccessMask;
[opt] ReadOnlyAccessMask: TAccessMask
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntpsapi, Ntapi.ntpebteb, Ntapi.ntdbg, Ntapi.ntseapi,
Ntapi.ntrtl, DelphiUtils.AutoObjects, NtUtils.Objects.Compare;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{$WARN SYMBOL_PLATFORM OFF}
type
TAutoHandle = class (TCustomAutoHandle)
destructor Destroy; override;
end;
TAutoRemoteHandle = class (TCustomAutoHandle)
FProcess: IHandle;
destructor Destroy; override;
constructor Capture(const hxProcess: IHandle; hObject: THandle);
end;
destructor TAutoHandle.Destroy;
begin
if (FHandle <> 0) and not FDiscardOwnership then
NtxClose(FHandle);
inherited;
end;
constructor TAutoRemoteHandle.Capture;
begin
inherited Capture(hObject);
FProcess := hxProcess;
end;
destructor TAutoRemoteHandle.Destroy;
begin
if Assigned(FProcess) and (FHandle <> 0) and not FDiscardOwnership then
NtxCloseRemoteHandle(FProcess, FHandle);
inherited;
end;
class function TAutoKernelObjectHelper.CaptureHandle;
begin
Result := TAutoHandle.Capture(hObject);
end;
class function TAutoKernelObjectHelper.CaptureRemoteHandle;
begin
Result := TAutoRemoteHandle.Capture(hxProcess, hObject);
end;
function NtxClose;
var
Flags: TObjectHandleFlagInformation;
begin
Flags.Inherit := False;
Flags.ProtectFromClose := False;
// Clear handle protection
NtSetInformationObject(hObject, ObjectHandleFlagInformation, @Flags,
SizeOf(Flags));
Result.Location := 'NtClose';
Result.Status := NtClose(hObject);
end;
function NtxCaptureHandle;
begin
if (hObject > 0) and (hObject <= MAX_HANDLE) then
begin
hxObject := Auto.CaptureHandle(hObject);
Exit(NtxSuccess);
end;
Result.Location := 'NtxCaptureHandle';
Result.Status := STATUS_INVALID_HANDLE;
end;
function NtxCaptureRemoteHandle;
begin
if (hObject > 0) and (hObject <= MAX_HANDLE) and Assigned(hxProcess) then
begin
hxObject := Auto.CaptureRemoteHandle(hxProcess, hObject);
Exit(NtxSuccess);
end;
Result.Location := 'NtxCaptureHandle';
Result.Status := STATUS_INVALID_HANDLE;
end;
procedure RtlxpTryDuplicateAccess(
const hxSource: IHandle;
var AccumulatedAccess: TAccessMask;
AccessToTry: TAccessMask
);
var
BasicInfo: TObjectBasicInformation;
hTemp: THandle;
hxTemp: IHandle;
begin
// Already tested?
if HasAll(AccumulatedAccess, AccessToTry) then
Exit;
// Try to duplicate the handle for the requested access
if not NT_SUCCESS(NtDuplicateObject(NtCurrentProcess, HandleOrDefault(
hxSource), NtCurrentProcess, hTemp, AccessToTry, 0, 0)) then
Exit;
hxTemp := Auto.CaptureHandle(hTemp);
// Record which rights we successfully got, in case they were filtered
if NtxObject.Query(hxTemp, ObjectBasicInformation, BasicInfo).IsSuccess then
AccumulatedAccess := AccumulatedAccess or BasicInfo.GrantedAccess;
end;
function NtxDuplicateHandleLocal;
var
Basic: TObjectBasicInformation;
ObjectType: TObjectTypeInfo;
Status: NTSTATUS;
hObject: THandle;
AccumulatedAccess: TAccessMask;
Bit: Integer;
begin
if BitTest(DesiredAccess and MAXIMUM_ALLOWED) and
not BitTest(Options and DUPLICATE_SAME_ACCESS) then
begin
// NtDuplicateObject does not support MAXIMUM_ALLOWED (it returns zero
// access instead). We need to probe access masks to calculate how much
// access we can return. Keep in mind that the caller can also combine
// MAXIMUM_ALLOWED with other access rights which we must grant to succeed.
// Determine the maximum possible access for handles of this type
Result := NtxQueryTypeObject(hxSource, ObjectType);
if not Result.IsSuccess then
Exit;
// Try full access first. If the desired access includes system security,
// don't forget to include it as well.
Status := NtDuplicateObject(NtCurrentProcess, HandleOrDefault(hxSource),
NtCurrentProcess, hObject, (ObjectType.Native.ValidAccessMask or
DesiredAccess) and not MAXIMUM_ALLOWED, HandleAttributes,
Options and not DUPLICATE_CLOSE_SOURCE);
if NT_SUCCESS(Status) then
begin
// Correct guess
hxNewHandle := Auto.CaptureHandle(hObject);
// Close the source if necessary
if BitTest(Options and DUPLICATE_CLOSE_SOURCE) then
NtDuplicateObject(NtCurrentProcess, HandleOrDefault(hxSource), 0,
THandle(nil^), 0, 0, DUPLICATE_CLOSE_SOURCE);
Exit(NtxSuccess);
end;
// Always include the required bits
AccumulatedAccess := DesiredAccess;
// Try all existing access at once
if NtxObject.Query(hxSource, ObjectBasicInformation, Basic).IsSuccess then
RtlxpTryDuplicateAccess(hxSource, AccumulatedAccess, Basic.GrantedAccess);
// Try read and execute groups in bulk
RtlxpTryDuplicateAccess(hxSource, AccumulatedAccess,
ObjectType.Native.GenericMapping.GenericRead);
RtlxpTryDuplicateAccess(hxSource, AccumulatedAccess,
ObjectType.Native.GenericMapping.GenericExecute);
// Try each optional valid access mask bit
for Bit := 0 to 24 do
RtlxpTryDuplicateAccess(hxSource, AccumulatedAccess,
ObjectType.Native.ValidAccessMask and (1 shl Bit));
// Replace maximum allowed with the calculated value
DesiredAccess := AccumulatedAccess and not MAXIMUM_ALLOWED;
end;
// Finally, try all collected access at once
Result.Location := 'NtDuplicateObject';
if not BitTest(Options and DUPLICATE_SAME_ACCESS) then
begin
Result.LastCall.OpensForAccess(DesiredAccess);
if Assigned(AccessMaskType) then
Result.LastCall.AccessMaskType := AccessMaskType;
end;
Result.Status := NtDuplicateObject(NtCurrentProcess, HandleOrDefault(
hxSource), NtCurrentProcess, hObject, DesiredAccess, HandleAttributes,
Options);
if Result.IsSuccess then
hxNewHandle := Auto.CaptureHandle(hObject);
end;
function NtxReopenHandle;
var
hxSourceHandle: IHandle;
begin
hxSourceHandle := hxHandle;
Result := NtxDuplicateHandleLocal(hxSourceHandle, hxHandle, DesiredAccess,
HandleAttributes, Options, AccessMaskType);
end;
function NtxDuplicateHandle;
var
hxLocalHandle: IHandle;
hLocalHandle: THandle;
ReopenOptions: TDuplicateOptions;
begin
Result.Location := 'NtDuplicateObject';
if not BitTest(Options and DUPLICATE_SAME_ACCESS) then
begin
Result.LastCall.OpensForAccess(DesiredAccess);
if Assigned(AccessMaskType) then
Result.LastCall.AccessMaskType := AccessMaskType;
end;
if (HandleOrDefault(hxSourceProcess) <> NtCurrentProcess) or
(HandleOrDefault(hxTargetProcess) <> NtCurrentProcess) then
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_DUP_HANDLE);
// NtDuplicateObject does not support MAXIMUM_ALLOWED (it returns zero access
// instead), so we make a local handle copy and probe access masks on it to
// calculate how much access we can return. Keep in mind that the caller can
// combine MAXIMUM_ALLOWED with other access rights which we must grant to
// succeed.
if BitTest(DesiredAccess and MAXIMUM_ALLOWED) and
not BitTest(Options and DUPLICATE_SAME_ACCESS) then
begin
if HandleOrDefault(hxSourceProcess) <> NtCurrentProcess then
begin
// Make a local handle copy to use for probing rights
Result.Status := NtDuplicateObject(HandleOrDefault(hxSourceProcess),
hSourceHandle, NtCurrentProcess, hLocalHandle, 0, 0,
DUPLICATE_SAME_ACCESS or (Options and (DUPLICATE_SAME_ATTRIBUTES or
DUPLICATE_CLOSE_SOURCE)));
if not Result.IsSuccess then
Exit;
// We own the local copy now
hxLocalHandle := Auto.CaptureHandle(hLocalHandle);
end
else
hxLocalHandle := Auto.RefHandle(hSourceHandle);
if HandleOrDefault(hxTargetProcess) <> NtCurrentProcess then
ReopenOptions := Options and DUPLICATE_SAME_ATTRIBUTES
else
ReopenOptions := Options;
// Obtain the maximum allowed access on the handle
Result := NtxReopenHandle(hxLocalHandle, DesiredAccess, HandleAttributes,
ReopenOptions, AccessMaskType);
if not Result.IsSuccess then
Exit;
if HandleOrDefault(hxTargetProcess) <> NtCurrentProcess then
begin
// Send the handle to the target
Result.Location := 'NtDuplicateObject';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_DUP_HANDLE);
Result.Status := NtDuplicateObject(NtCurrentProcess, hxLocalHandle.Handle,
HandleOrDefault(hxTargetProcess), hTargetHandle, 0, HandleAttributes,
DUPLICATE_SAME_ACCESS or (Options and
(DUPLICATE_SAME_ATTRIBUTES or DUPLICATE_NO_RIGHTS_UPGRADE)));
end
else
begin
// Transfer local ownerhip over the handle with the expanded access
hTargetHandle := hxLocalHandle.Handle;
hxLocalHandle.DiscardOwnership;
end;
end
else
begin
// Forward simple request to the system
Result.Status := NtDuplicateObject(HandleOrDefault(hxSourceProcess),
hSourceHandle, HandleOrDefault(hxTargetProcess), hTargetHandle,
DesiredAccess, HandleAttributes, Options);
end;
end;
function NtxEnsureAccessHandle;
var
Info: TObjectBasicInformation;
ObjectType: TObjectTypeInfo;
begin
// Expand generic rights
if HasAny(DesiredAccess and (GENERIC_RIGHTS_ALL or MAXIMUM_ALLOWED)) then
begin
Result := NtxQueryTypeObject(hxHandle, ObjectType);
if not Result.IsSuccess then
Exit;
RtlMapGenericMask(DesiredAccess, ObjectType.Native.GenericMapping);
end;
// Determine existing access
Result := NtxObject.Query(hxHandle, ObjectBasicInformation, Info);
if not Result.IsSuccess then
Exit;
// Full access satisfies MAXIMUM_ALLOWED
if BitTest(DesiredAccess and MAXIMUM_ALLOWED) and HasAll(Info.GrantedAccess,
ObjectType.Native.ValidAccessMask or DesiredAccess and not MAXIMUM_ALLOWED)
then
Exit;
// Check fixed granted access
if HasAll(Info.GrantedAccess, DesiredAccess) then
Exit;
// Reopen
Result := NtxReopenHandle(hxHandle, DesiredAccess, HandleAttributes,
Options, AccessMaskType);
end;
function NtxDuplicateHandleFrom;
var
hLocalHandle: THandle;
begin
Result := NtxDuplicateHandle(hxProcess, hRemoteHandle, NtxCurrentProcess,
hLocalHandle, DesiredAccess, HandleAttributes, Options, AccessMaskType);
if Result.IsSuccess then
hxLocalHandle := Auto.CaptureHandle(hLocalHandle);
end;
function NtxDuplicateHandleTo;
begin
Result := NtxDuplicateHandle(NtxCurrentProcess, HandleOrDefault(
hxLocalHandle), hxProcess, hRemoteHandle, DesiredAccess, HandleAttributes,
Options, AccessMaskType);
end;
function NtxDuplicateHandleToAuto;
var
hRemoteHandle: THandle;
begin
Result := NtxDuplicateHandle(NtxCurrentProcess, HandleOrDefault(
hxLocalHandle), hxProcess, hRemoteHandle, DesiredAccess, HandleAttributes,
Options, AccessMaskType);
if Result.IsSuccess then
hxRemoteHandle := Auto.CaptureRemoteHandle(hxProcess, hRemoteHandle);
end;
function NtxCloseRemoteHandle;
begin
Result.Location := 'NtDuplicateObject';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_DUP_HANDLE);
Result.Status := NtDuplicateObject(HandleOrDefault(hxProcess), hRemoteHandle,
0, THandle(nil^), 0, 0, DUPLICATE_CLOSE_SOURCE);
end;
function NtxCloseRemoteHandleWithCheck;
var
hxLocalCopyA, hxLocalCopyB: IHandle;
Equal: Boolean;
begin
// A DUPLICATE_CLOSE_SOURCE request might succeed without actually closing
// the handle. It happens when the handle is protected via a flag
// (OBJ_PROTECT_CLOSE) or a kernel callback (such as actively used window
// station and desktop handles). To see if it happens, we check if the
// handle is still there.
// Make a local copy and close the remote handle
Result := NtxDuplicateHandleFrom(hxProcess, hRemoteHandle, hxLocalCopyA, 0, 0,
DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE);
if not Result.IsSuccess then
Exit;
// Try to copy the handle again. This time, without closing (in case the slot
// was reused)
Result := NtxDuplicateHandleFrom(hxProcess, hRemoteHandle, hxLocalCopyB, 0, 0,
DUPLICATE_SAME_ACCESS);
if Result.IsSuccess then
begin
// If the second copy succeeded, either the first one failed to close the
// handle or the slot got reused between calls. Verify what happened by
// comparing the objects.
Result := NtxCompareObjects(Equal, hxLocalCopyA, hxLocalCopyB);
if not Result.IsSuccess then
Exit;
if not Equal then
begin
Result.Location := 'NtxCloseRemoteHandleWithCheck';
Result.Status := STATUS_HANDLE_NOT_CLOSABLE;
end;
end
else if Result.Status = STATUS_INVALID_HANDLE then
Result := NtxSuccess; // No handle anymore
end;
class function NtxObject.Query<T>;
begin
Result.Location := 'NtQueryObject';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.Status := NtQueryObject(HandleOrDefault(hxObject), InfoClass, @Buffer,
SizeOf(Buffer), nil);
end;
function NtxQueryObject;
var
Required: Cardinal;
begin
Result.Location := 'NtQueryObject';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
xMemory := Auto.AllocateDynamic(InitialBuffer);
repeat
Required := 0;
Result.Status := NtQueryObject(HandleOrDefault(hxObject), InfoClass,
xMemory.Data, xMemory.Size, @Required);
until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod);
end;
function NtxQueryNameObject;
var
xMemory: IMemory<PNtUnicodeString>;
begin
Result := NtxQueryObject(hxObject, ObjectNameInformation, IMemory(xMemory));
if Result.IsSuccess then
Name := xMemory.Data.ToString;
end;
function NtxQueryTypeObject;
var
xMemory: IMemory<PObjectTypeInformation>;
begin
Result := NtxQueryObject(hxObject, ObjectTypeInformation, IMemory(xMemory),
SizeOf(TObjectTypeInformation));
if not Result.IsSuccess then
Exit;
Info.TypeName := xMemory.Data.TypeName.ToString;
Info.Native := xMemory.Data^;
Info.Native.TypeName.Buffer := PWideChar(Info.TypeName);
end;
function NtxSetFlagsHandle;
var
Info: TObjectHandleFlagInformation;
begin
Info.Inherit := Inherit;
Info.ProtectFromClose := ProtectFromClose;
Result.Location := 'NtSetInformationObject';
Result.LastCall.UsesInfoClass(ObjectHandleFlagInformation, icSet);
Result.Status := NtSetInformationObject(HandleOrDefault(hxObject),
ObjectHandleFlagInformation, @Info, SizeOf(Info));
end;
function NtxQuerySecurityObject;
var
Buffer: IMemory absolute SD;
Required: Cardinal;
begin
Result.Location := 'NtQuerySecurityObject';
Result.LastCall.Expects(SecurityReadAccess(Info));
Buffer := Auto.AllocateDynamic(0);
repeat
Required := 0;
Result.Status := NtQuerySecurityObject(HandleOrDefault(hxObject), Info,
Buffer.Data, Buffer.Size, Required);
until not NtxExpandBufferEx(Result, Buffer, Required, nil);
end;
function NtxSetSecurityObject;
begin
Result.Location := 'NtSetSecurityObject';
Result.LastCall.Expects(SecurityWriteAccess(Info));
Result.Status := NtSetSecurityObject(HandleOrDefault(hxObject), Info, SD);
end;
{ Access masks }
function RtlxpRecordAccess(
const hxObject: IHandle;
var MaximumAccess: TAccessMask;
var BitsToTest: TAccessMask
): TNtxStatus;
var
Info: TObjectBasicInformation;
begin
Result := NtxObject.Query(hxObject, ObjectBasicInformation, Info);
if Result.IsSuccess then
begin
MaximumAccess := MaximumAccess or Info.GrantedAccess;
BitsToTest := BitsToTest and not Info.GrantedAccess;
end;
end;
function RtlxComputeMaximumAccess;
var
hxObject: IHandle;
BitsToTest: TAccessMask;
i: Byte;
begin
MaximumAccess := 0;
BitsToTest := FullAccessMask;
// Try MAXIMUM_ALLOWED on kernel objects because we can query the results
if IsKernelObject and ObjectOpener(hxObject, MAXIMUM_ALLOWED).IsSuccess then
Result := RtlxpRecordAccess(hxObject, MaximumAccess, BitsToTest);
// Try all read-only rights at once
if HasAny(BitsToTest and ReadOnlyAccessMask) and
ObjectOpener(hxObject, BitsToTest and ReadOnlyAccessMask).IsSuccess then
begin
// Test for externally-filtered rights when possible
if IsKernelObject then
Result := RtlxpRecordAccess(hxObject, MaximumAccess, BitsToTest)
else
MaximumAccess := MaximumAccess or (BitsToTest and ReadOnlyAccessMask);
BitsToTest := BitsToTest and not ReadOnlyAccessMask;
end;
// Test bits one-by-one
for i := 0 to 31 do
begin
if not BitTest(BitsToTest and (1 shl i)) then
Continue;
Result := ObjectOpener(hxObject, 1 shl i);
if not Result.IsSuccess then
Continue;
if IsKernelObject then
Result := RtlxpRecordAccess(hxObject, MaximumAccess, BitsToTest)
else
begin
// Believe that success indicates granted (and not filtered) access
// because we cannot verify it in general case
MaximumAccess := MaximumAccess or (1 shl i);
BitsToTest := BitsToTest and not (1 shl i);
end;
end;
if MaximumAccess <> 0 then
Result := NtxSuccess;
end;
end.