00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
#include "sep.h"
00042
#include "seopaque.h"
00043
00044
00045
#ifdef ALLOC_PRAGMA
00046
#pragma alloc_text(PAGE,SeCreateClientSecurity)
00047
#pragma alloc_text(PAGE,SeUpdateClientSecurity)
00048
#pragma alloc_text(PAGE,SeImpersonateClient)
00049
#pragma alloc_text(PAGE,SeImpersonateClientEx)
00050
#pragma alloc_text(PAGE,SeCreateClientSecurityFromSubjectContext)
00051
#endif
00052
00053
00055
00056
00057
00059
00060
00061
NTSTATUS
00062 SepCreateClientSecurity(
00063 IN PACCESS_TOKEN Token,
00064 IN PSECURITY_QUALITY_OF_SERVICE ClientSecurityQos,
00065 IN BOOLEAN ServerIsRemote,
00066 TOKEN_TYPE TokenType,
00067 BOOLEAN ThreadEffectiveOnly,
00068 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
00069 OUT
PSECURITY_CLIENT_CONTEXT ClientContext
00070 )
00071 {
00072
NTSTATUS Status = STATUS_SUCCESS;
00073 PACCESS_TOKEN DuplicateToken;
00074
00075
PAGED_CODE();
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
if (TokenType == TokenImpersonation) {
00089
00090
if ( ClientSecurityQos->ImpersonationLevel > ImpersonationLevel) {
00091
00092
PsDereferenceImpersonationToken(
Token );
00093
return STATUS_BAD_IMPERSONATION_LEVEL;
00094
00095 }
00096
00097
00098
if (
SepBadImpersonationLevel(ImpersonationLevel,ServerIsRemote)) {
00099
00100
PsDereferenceImpersonationToken(
Token );
00101
return STATUS_BAD_IMPERSONATION_LEVEL;
00102
00103 }
else {
00104
00105
00106
00107
00108
00109
00110
00111 ClientContext->DirectAccessEffectiveOnly =
00112 ( (ThreadEffectiveOnly || (ClientSecurityQos->EffectiveOnly)) ?
00113
TRUE :
FALSE );
00114 }
00115
00116 }
else {
00117
00118
00119
00120
00121
00122
00123 ClientContext->DirectAccessEffectiveOnly =
00124 ClientSecurityQos->EffectiveOnly;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
if (ClientSecurityQos->ContextTrackingMode == SECURITY_STATIC_TRACKING) {
00134
00135 ClientContext->DirectlyAccessClientToken =
FALSE;
00136
00137
Status =
SeCopyClientToken(
00138
Token,
00139 ClientSecurityQos->ImpersonationLevel,
00140
KernelMode,
00141 &DuplicateToken
00142 );
00143
00144
00145
if (
NT_SUCCESS(
Status) ) {
00146
ObDeleteCapturedInsertInfo(DuplicateToken);
00147 }
00148
00149
00150
00151
00152
if (TokenType == TokenPrimary) {
00153
PsDereferencePrimaryToken(
Token );
00154 }
else {
00155
PsDereferenceImpersonationToken(
Token );
00156 }
00157
00158
Token = DuplicateToken;
00159
00160
00161
00162
00163
00164
if (!
NT_SUCCESS(
Status)) {
00165
return Status;
00166 }
00167
00168 }
else {
00169
00170 ClientContext->DirectlyAccessClientToken =
TRUE;
00171
00172
if (ServerIsRemote) {
00173
00174
00175
00176
00177
00178
SeGetTokenControlInformation(
Token,
00179 &ClientContext->ClientTokenControl
00180 );
00181
00182 }
00183
00184 }
00185
00186
00187
00188 ClientContext->SecurityQos.Length =
00189 (ULONG)
sizeof(SECURITY_QUALITY_OF_SERVICE);
00190
00191 ClientContext->SecurityQos.ImpersonationLevel =
00192 ClientSecurityQos->ImpersonationLevel;
00193
00194 ClientContext->SecurityQos.ContextTrackingMode =
00195 ClientSecurityQos->ContextTrackingMode;
00196
00197 ClientContext->SecurityQos.EffectiveOnly =
00198 ClientSecurityQos->EffectiveOnly;
00199
00200 ClientContext->ServerIsRemote = ServerIsRemote;
00201
00202 ClientContext->ClientToken =
Token;
00203
00204
return STATUS_SUCCESS;
00205
00206 }
00207
00208
NTSTATUS
00209 SeCreateClientSecurity (
00210 IN
PETHREAD ClientThread,
00211 IN PSECURITY_QUALITY_OF_SERVICE ClientSecurityQos,
00212 IN BOOLEAN ServerIsRemote,
00213 OUT
PSECURITY_CLIENT_CONTEXT ClientContext
00214 )
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 {
00269
NTSTATUS Status = STATUS_SUCCESS;
00270 PACCESS_TOKEN
Token;
00271 TOKEN_TYPE TokenType;
00272 BOOLEAN ThreadEffectiveOnly;
00273 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
00274 PACCESS_TOKEN DuplicateToken;
00275
00276
PAGED_CODE();
00277
00278
00279
00280
00281
00282
Token =
PsReferenceEffectiveToken(
00283
ClientThread,
00284 &TokenType,
00285 &ThreadEffectiveOnly,
00286 &ImpersonationLevel
00287 );
00288
00289
00290
Status =
SepCreateClientSecurity(
00291
Token,
00292 ClientSecurityQos,
00293 ServerIsRemote,
00294 TokenType,
00295 ThreadEffectiveOnly,
00296 ImpersonationLevel,
00297 ClientContext );
00298
00299
return Status ;
00300 }
00301
00302
00303
00304
#if SAVE_FOR_PRODUCT_2
00305
00306
00307
00308
00309
NTSTATUS
00310
SeUpdateClientSecurity(
00311 IN
PETHREAD ClientThread,
00312 IN OUT
PSECURITY_CLIENT_CONTEXT ClientContext,
00313 OUT PBOOLEAN ChangesMade,
00314 OUT PBOOLEAN NewToken
00315 )
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 {
00378
NTSTATUS Status;
00379 PACCESS_TOKEN
Token;
00380 TOKEN_TYPE TokenType;
00381 BOOLEAN ThreadEffectiveOnly;
00382 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
00383 PACCESS_TOKEN DuplicateToken;
00384 TOKEN_CONTROL TokenControl;
00385
00386
PAGED_CODE();
00387
00388
if (ClientContext->SecurityQos.ContextTrackingMode ==
00389 SECURITY_STATIC_TRACKING) {
00390
00391 (*NewToken) =
FALSE;
00392 (*ChangesMade) =
FALSE;
00393
return STATUS_SUCCESS;
00394
00395 }
00396
00397
00399
00400
00401
00403
00404
00405
00406
00407
00408
00409
00410
Token =
PsReferenceEffectiveToken(
00411 ClientThread,
00412 &TokenType,
00413 &ThreadEffectiveOnly,
00414 &ImpersonationLevel
00415 );
00416
00417
00418
00419
00420
00421
00422
00423
00424
SeGetTokenControlInformation( Token, &TokenControl );
00425
00426
if (
SeSameToken( &TokenControl,
00427 &ClientContext->ClientTokenControl) ) {
00428
00429 (*NewToken =
FALSE);
00430
00431
00432
00433
00434
00435
00436
00437
if ( (TokenControl.ModifiedId.HighPart ==
00438 ClientContext->ClientTokenControl.ModifiedId.HighPart) &&
00439 (TokenControl.ModifiedId.LowPart ==
00440 ClientContext->ClientTokenControl.ModifiedId.LowPart) ) {
00441
00442
00443
00444
00445
00446
if (TokenType == TokenPrimary) {
00447
PsDereferencePrimaryToken( Token );
00448 }
else {
00449
PsDereferenceImpersonationToken( Token );
00450 }
00451
00452 (*ChangesMade) =
FALSE;
00453
return STATUS_SUCCESS;
00454
00455 }
else {
00456
00457
00458
00459
00460
00461
00462
00463
00464 (*ChangesMade) =
TRUE;
00465
if (ClientContext->DirectlyAccessClientToken) {
00466
00467
if (TokenType == TokenPrimary) {
00468
PsDereferencePrimaryToken( Token );
00469 }
else {
00470
PsDereferenceImpersonationToken( Token );
00471 }
00472
00473
00474
00475
00476
00477
00478 ClientContext->ClientTokenControl.ModifiedId =
00479 TokenControl.ModifiedId;
00480 ClientContext->DirectAccessEffectiveOnly =
00481 ( (ThreadEffectiveOnly || (ClientContext->SecurityQos.EffectiveOnly)) ?
00482
TRUE :
FALSE );
00483
00484
return STATUS_SUCCESS;
00485 }
else {
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498 }
00499 }
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 (*NewToken) =
TRUE;
00512 (*ChangesMade) =
TRUE;
00513
if (TokenType == TokenImpersonation) {
00514
if (
SepBadImpersonationLevel(ImpersonationLevel,
00515 ClientContext->ServerIsRemote)) {
00516
00517
PsDereferenceImpersonationToken( Token );
00518
return STATUS_BAD_IMPERSONATION_LEVEL;
00519 }
00520 }
00521
00522
00523
00524
00525
00526
00527
00528
00529
Status =
SeCopyClientToken(
00530 Token,
00531 ClientContext->SecurityQos.ImpersonationLevel,
00532 KernelMode,
00533 &DuplicateToken
00534 );
00535
00536
00537
00538
00539
00540
00541
if (TokenType == TokenPrimary) {
00542
PsDereferencePrimaryToken( Token );
00543 }
else {
00544
PsDereferenceImpersonationToken( Token );
00545 }
00546
00547
00548
00549
00550
00551
00552
if (!
NT_SUCCESS(Status)) {
00553
return Status;
00554 }
00555
00556
00557
00558
00559
00560
00561
Token = ClientContext->ClientToken;
00562 ClientContext->ClientToken = DuplicateToken;
00563 ClientContext->DirectlyAccessClientToken =
FALSE;
00564
00565
if (
SeTokenType( Token ) == TokenPrimary) {
00566
PsDereferencePrimaryToken( Token );
00567 }
else {
00568
PsDereferenceImpersonationToken( Token );
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
SeGetTokenControlInformation( DuplicateToken,
00578 &ClientContext->ClientTokenControl
00579 );
00580
00581
00582
return STATUS_SUCCESS;
00583
00584 }
00585
00586
00587
#endif
00588
00589
00590
00591
00592
VOID
00593 SeImpersonateClient(
00594 IN
PSECURITY_CLIENT_CONTEXT ClientContext,
00595 IN
PETHREAD ServerThread OPTIONAL
00596 )
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 {
00624
00625
PAGED_CODE();
00626
00627
#if DBG
00628
DbgPrint(
"SE: Obsolete call: SeImpersonateClient\n");
00629
#endif
00630
00631 (
VOID)
SeImpersonateClientEx( ClientContext,
ServerThread );
00632 }
00633
00634
00635
NTSTATUS
00636 SeImpersonateClientEx(
00637 IN
PSECURITY_CLIENT_CONTEXT ClientContext,
00638 IN
PETHREAD ServerThread OPTIONAL
00639 )
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666 {
00667
00668 BOOLEAN EffectiveValueToUse;
00669
PETHREAD Thread;
00670
NTSTATUS Status ;
00671
00672
PAGED_CODE();
00673
00674
if (ClientContext->DirectlyAccessClientToken) {
00675 EffectiveValueToUse = ClientContext->DirectAccessEffectiveOnly;
00676 }
else {
00677 EffectiveValueToUse = ClientContext->SecurityQos.EffectiveOnly;
00678 }
00679
00680
00681
00682
00683
00684
00685
00686
00687
if (!ARGUMENT_PRESENT(
ServerThread)) {
00688 Thread =
PsGetCurrentThread();
00689 }
else {
00690 Thread =
ServerThread;
00691 }
00692
00693
00694
00695
00696
00697
00698
00699
Status =
PsImpersonateClient( Thread,
00700 ClientContext->ClientToken,
00701
TRUE,
00702 EffectiveValueToUse,
00703 ClientContext->SecurityQos.ImpersonationLevel
00704 );
00705
00706
return Status ;
00707
00708 }
00709
00710
00711
NTSTATUS
00712 SeCreateClientSecurityFromSubjectContext (
00713 IN
PSECURITY_SUBJECT_CONTEXT SubjectContext,
00714 IN PSECURITY_QUALITY_OF_SERVICE ClientSecurityQos,
00715 IN BOOLEAN ServerIsRemote,
00716 OUT
PSECURITY_CLIENT_CONTEXT ClientContext
00717 )
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 {
00771
NTSTATUS Status = STATUS_SUCCESS;
00772 PACCESS_TOKEN
Token;
00773 TOKEN_TYPE Type;
00774 BOOLEAN ThreadEffectiveOnly;
00775 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
00776 PACCESS_TOKEN DuplicateToken;
00777
00778
PAGED_CODE();
00779
00780
Token =
SeQuerySubjectContextToken(
00781 SubjectContext
00782 );
00783
00784
ObReferenceObject(
Token );
00785
00786
if ( SubjectContext->ClientToken )
00787 {
00788 Type = TokenImpersonation ;
00789 }
00790
else
00791 {
00792 Type = TokenPrimary ;
00793 }
00794
00795
Status =
SepCreateClientSecurity(
00796
Token,
00797 ClientSecurityQos,
00798 ServerIsRemote,
00799 Type,
00800
FALSE,
00801 SubjectContext->ImpersonationLevel,
00802 ClientContext
00803 );
00804
00805
00806
return Status ;
00807 }
00808