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
#include "ntrtlp.h"
00028
#include "seopaque.h"
00029
#include "sertlp.h"
00030
00031
#if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
00032
#pragma alloc_text(PAGE,RtlSelfRelativeToAbsoluteSD)
00033
#pragma alloc_text(PAGE,RtlMakeSelfRelativeSD)
00034
#pragma alloc_text(PAGE,RtlpQuerySecurityDescriptor)
00035
#pragma alloc_text(PAGE,RtlAbsoluteToSelfRelativeSD)
00036
#pragma alloc_text(PAGE,RtlSelfRelativeToAbsoluteSD2)
00037
#endif
00038
00039
00041
00042
00043
00045
00046
00047
00048
NTSTATUS
00049 RtlSelfRelativeToAbsoluteSD(
00050 IN OUT PSECURITY_DESCRIPTOR SelfRelativeSecurityDescriptor,
00051 OUT PSECURITY_DESCRIPTOR AbsoluteSecurityDescriptor,
00052 IN OUT PULONG AbsoluteSecurityDescriptorSize,
00053 IN OUT PACL Dacl,
00054 IN OUT PULONG DaclSize,
00055 IN OUT PACL Sacl,
00056 IN OUT PULONG SaclSize,
00057 IN OUT PSID Owner,
00058 IN OUT PULONG OwnerSize,
00059 IN OUT PSID PrimaryGroup,
00060 IN OUT PULONG PrimaryGroupSize
00061 )
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 {
00123 ULONG NewDaclSize;
00124 ULONG NewSaclSize;
00125 ULONG NewBodySize;
00126 ULONG NewOwnerSize;
00127 ULONG NewGroupSize;
00128
00129 PSID NewOwner;
00130 PSID NewGroup;
00131 PACL NewDacl;
00132 PACL NewSacl;
00133
00134
00135
00136
00137
00138 PISECURITY_DESCRIPTOR OutSD =
00139 AbsoluteSecurityDescriptor;
00140
00141 PISECURITY_DESCRIPTOR InSD =
00142 (PISECURITY_DESCRIPTOR)SelfRelativeSecurityDescriptor;
00143
00144
00145
RTL_PAGED_CODE();
00146
00147
if ( !RtlpAreControlBitsSet( InSD, SE_SELF_RELATIVE) ) {
00148
return( STATUS_BAD_DESCRIPTOR_FORMAT );
00149 }
00150
00151 NewBodySize =
sizeof(SECURITY_DESCRIPTOR);
00152
00153
RtlpQuerySecurityDescriptor(
00154 InSD,
00155 &NewOwner,
00156 &NewOwnerSize,
00157 &NewGroup,
00158 &NewGroupSize,
00159 &NewDacl,
00160 &NewDaclSize,
00161 &NewSacl,
00162 &NewSaclSize
00163 );
00164
00165
if ( (NewBodySize > *AbsoluteSecurityDescriptorSize) ||
00166 (NewOwnerSize > *OwnerSize ) ||
00167 (NewDaclSize > *DaclSize ) ||
00168 (NewSaclSize > *SaclSize ) ||
00169 (NewGroupSize > *PrimaryGroupSize ) ) {
00170
00171 *AbsoluteSecurityDescriptorSize =
sizeof(SECURITY_DESCRIPTOR);
00172 *PrimaryGroupSize = NewGroupSize;
00173 *OwnerSize = NewOwnerSize;
00174 *SaclSize = NewSaclSize;
00175 *DaclSize = NewDaclSize;
00176
00177
return( STATUS_BUFFER_TOO_SMALL );
00178 }
00179
00180
00181 RtlMoveMemory( OutSD,
00182 InSD,
00183
sizeof(SECURITY_DESCRIPTOR_RELATIVE) );
00184
00185 OutSD->Owner =
NULL;
00186 OutSD->Group =
NULL;
00187 OutSD->Sacl =
NULL;
00188 OutSD->Dacl =
NULL;
00189
00190 RtlpClearControlBits( OutSD, SE_SELF_RELATIVE );
00191
00192
if (NewOwner !=
NULL) {
00193 RtlMoveMemory(
Owner, NewOwner,
SeLengthSid( NewOwner ));
00194 OutSD->Owner =
Owner;
00195 }
00196
00197
if (NewGroup !=
NULL) {
00198 RtlMoveMemory( PrimaryGroup, NewGroup,
SeLengthSid( NewGroup ));
00199 OutSD->Group = PrimaryGroup;
00200 }
00201
00202
if (NewSacl !=
NULL) {
00203 RtlMoveMemory( Sacl, NewSacl, NewSacl->AclSize );
00204 OutSD->Sacl = Sacl;
00205 }
00206
00207
if (NewDacl !=
NULL) {
00208 RtlMoveMemory(
Dacl, NewDacl, NewDacl->AclSize );
00209 OutSD->Dacl =
Dacl;
00210 }
00211
00212
return( STATUS_SUCCESS );
00213 }
00214
00215
00216
00217
00218
NTSTATUS
00219 RtlMakeSelfRelativeSD(
00220 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
00221 IN OUT PSECURITY_DESCRIPTOR SelfRelativeSecurityDescriptor,
00222 IN OUT PULONG BufferLength
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 ULONG NewDaclSize;
00259 ULONG NewSaclSize;
00260 ULONG NewOwnerSize;
00261 ULONG NewGroupSize;
00262
00263 ULONG AllocationSize;
00264
00265 PSID NewOwner;
00266 PSID NewGroup;
00267 PACL NewDacl;
00268 PACL NewSacl;
00269
00270 PCHAR Field;
00271 PCHAR Base;
00272
00273
00274
00275
00276
00277
00278
00279 PISECURITY_DESCRIPTOR_RELATIVE IResultantDescriptor =
00280 (PISECURITY_DESCRIPTOR_RELATIVE)SelfRelativeSecurityDescriptor;
00281
00282 PISECURITY_DESCRIPTOR IPassedSecurityDescriptor =
00283 (PISECURITY_DESCRIPTOR)SecurityDescriptor;
00284
00285
00286
RtlpQuerySecurityDescriptor(
00287 IPassedSecurityDescriptor,
00288 &NewOwner,
00289 &NewOwnerSize,
00290 &NewGroup,
00291 &NewGroupSize,
00292 &NewDacl,
00293 &NewDaclSize,
00294 &NewSacl,
00295 &NewSaclSize
00296 );
00297
00298
RTL_PAGED_CODE();
00299
00300 AllocationSize =
sizeof(SECURITY_DESCRIPTOR_RELATIVE) +
00301 NewOwnerSize +
00302 NewGroupSize +
00303 NewDaclSize +
00304 NewSaclSize ;
00305
00306
if (AllocationSize > *BufferLength) {
00307 *BufferLength = AllocationSize;
00308
return( STATUS_BUFFER_TOO_SMALL );
00309 }
00310
00311 RtlZeroMemory( IResultantDescriptor, AllocationSize );
00312
00313 RtlCopyMemory( IResultantDescriptor,
00314 IPassedSecurityDescriptor,
00315 FIELD_OFFSET( SECURITY_DESCRIPTOR_RELATIVE,
Owner ));
00316
00317
00318 Base = (PCHAR)(IResultantDescriptor);
00319 Field = Base + (ULONG)
sizeof(SECURITY_DESCRIPTOR_RELATIVE);
00320
00321
if (NewSaclSize > 0) {
00322 RtlCopyMemory( Field, NewSacl, NewSaclSize );
00323 IResultantDescriptor->Sacl = RtlPointerToOffset(Base,Field);
00324 Field += NewSaclSize;
00325 }
else {
00326 IResultantDescriptor->Sacl = 0;
00327 }
00328
00329
00330
if (NewDaclSize > 0) {
00331 RtlCopyMemory( Field, NewDacl, NewDaclSize );
00332 IResultantDescriptor->Dacl = RtlPointerToOffset(Base,Field);
00333 Field += NewDaclSize;
00334 }
else {
00335 IResultantDescriptor->Dacl = 0;
00336 }
00337
00338
00339
00340
if (NewOwnerSize > 0) {
00341 RtlCopyMemory( Field, NewOwner, NewOwnerSize );
00342 IResultantDescriptor->Owner = RtlPointerToOffset(Base,Field);
00343 Field += NewOwnerSize;
00344 }
00345
00346
00347
if (NewGroupSize > 0) {
00348 RtlCopyMemory( Field, NewGroup, NewGroupSize );
00349 IResultantDescriptor->Group = RtlPointerToOffset(Base,Field);
00350 }
00351
00352 RtlpSetControlBits( IResultantDescriptor, SE_SELF_RELATIVE );
00353
00354
return( STATUS_SUCCESS );
00355
00356 }
00357
00358
00359
NTSTATUS
00360 RtlAbsoluteToSelfRelativeSD(
00361 IN PSECURITY_DESCRIPTOR AbsoluteSecurityDescriptor,
00362 IN OUT PSECURITY_DESCRIPTOR SelfRelativeSecurityDescriptor,
00363 IN OUT PULONG BufferLength
00364 )
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 {
00398
NTSTATUS NtStatus;
00399
00400 PISECURITY_DESCRIPTOR IAbsoluteSecurityDescriptor =
00401 (PISECURITY_DESCRIPTOR)AbsoluteSecurityDescriptor;
00402
00403
00404
RTL_PAGED_CODE();
00405
00406
00407
00408
00409
00410
00411
if ( RtlpAreControlBitsSet( IAbsoluteSecurityDescriptor, SE_SELF_RELATIVE) ) {
00412
return( STATUS_BAD_DESCRIPTOR_FORMAT );
00413 }
00414
00415 NtStatus =
RtlMakeSelfRelativeSD(
00416 AbsoluteSecurityDescriptor,
00417 SelfRelativeSecurityDescriptor,
00418 BufferLength
00419 );
00420
00421
return( NtStatus );
00422
00423 }
00424
VOID
00425 RtlpQuerySecurityDescriptor(
00426 IN PISECURITY_DESCRIPTOR SecurityDescriptor,
00427 OUT PSID *Owner,
00428 OUT PULONG OwnerSize,
00429 OUT PSID *PrimaryGroup,
00430 OUT PULONG PrimaryGroupSize,
00431 OUT PACL *Dacl,
00432 OUT PULONG DaclSize,
00433 OUT PACL *Sacl,
00434 OUT PULONG SaclSize
00435 )
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 {
00470
00471
RTL_PAGED_CODE();
00472
00473 *
Owner = RtlpOwnerAddrSecurityDescriptor( SecurityDescriptor );
00474
00475
if (*
Owner !=
NULL) {
00476 *OwnerSize = LongAlignSize(
SeLengthSid(*
Owner));
00477 }
else {
00478 *OwnerSize = 0;
00479 }
00480
00481 *
Dacl = RtlpDaclAddrSecurityDescriptor ( SecurityDescriptor );
00482
00483
if (*
Dacl !=
NULL) {
00484 *DaclSize = LongAlignSize((*Dacl)->AclSize);
00485 }
else {
00486 *DaclSize = 0;
00487 }
00488
00489 *PrimaryGroup = RtlpGroupAddrSecurityDescriptor( SecurityDescriptor );
00490
00491
if (*PrimaryGroup !=
NULL) {
00492 *PrimaryGroupSize = LongAlignSize(
SeLengthSid(*PrimaryGroup));
00493 }
else {
00494 *PrimaryGroupSize = 0;
00495 }
00496
00497 *Sacl = RtlpSaclAddrSecurityDescriptor( SecurityDescriptor );
00498
00499
if (*Sacl !=
NULL) {
00500 *SaclSize = LongAlignSize((*Sacl)->AclSize);
00501 }
else {
00502 *SaclSize = 0;
00503 }
00504
00505 }
00506
00507
00508
00509
NTSTATUS
00510 RtlSelfRelativeToAbsoluteSD2(
00511 IN OUT PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,
00512 IN OUT PULONG pBufferSize
00513 )
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557 {
00558 ULONG_PTR ptr;
00559 PSID owner;
00560 PSID group;
00561 PACL dacl;
00562 PACL sacl;
00563 ULONG daclSize;
00564 ULONG saclSize;
00565 ULONG newBodySize;
00566 ULONG ownerSize;
00567 ULONG groupSize;
00568 ULONG newBufferSize;
00569 LONG deltaSize;
00570
00571
00572
00573
00574
00575 PISECURITY_DESCRIPTOR psd = (PISECURITY_DESCRIPTOR) pSelfRelativeSecurityDescriptor;
00576 PISECURITY_DESCRIPTOR_RELATIVE psdr = (PISECURITY_DESCRIPTOR_RELATIVE)pSelfRelativeSecurityDescriptor;
00577
00578
00579
00580
00581
00582
00583
00584
C_ASSERT(
sizeof( SECURITY_DESCRIPTOR ) >=
sizeof( SECURITY_DESCRIPTOR_RELATIVE ) );
00585
C_ASSERT(
sizeof( psd->Control ) ==
sizeof( psdr->Control ) );
00586
C_ASSERT( FIELD_OFFSET( SECURITY_DESCRIPTOR, Control ) == FIELD_OFFSET( SECURITY_DESCRIPTOR_RELATIVE, Control ) );
00587
00588
RTL_PAGED_CODE();
00589
00590
00591
00592
00593
00594
if ( psd == (PISECURITY_DESCRIPTOR)0 ) {
00595
return( STATUS_INVALID_PARAMETER_1 );
00596 }
00597
if ( pBufferSize == (PULONG)0 ) {
00598
return( STATUS_INVALID_PARAMETER_2 );
00599 }
00600
00601
00602
00603
00604
00605
00606
if ( !RtlpAreControlBitsSet( psd, SE_SELF_RELATIVE) ) {
00607
return( STATUS_BAD_DESCRIPTOR_FORMAT );
00608 }
00609
00610
00611
00612
00613
00614
00615
00616
RtlpQuerySecurityDescriptor(
00617 psd,
00618 &owner,
00619 &ownerSize,
00620 &group,
00621 &groupSize,
00622 &dacl,
00623 &daclSize,
00624 &sacl,
00625 &saclSize
00626 );
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636 deltaSize =
sizeof( SECURITY_DESCRIPTOR ) -
sizeof( SECURITY_DESCRIPTOR_RELATIVE );
00637
00638
00639
00640
00641
00642
00643
00644
00645
if ( deltaSize == 0 ) {
00646
00647 RtlpClearControlBits( psd, SE_SELF_RELATIVE );
00648
00649
00650
00651
00652
00653
ASSERT(
sizeof( psd->Owner ) ==
sizeof( psdr->Owner ) );
00654
ASSERT(
sizeof( psd->Group ) ==
sizeof( psdr->Group ) );
00655
ASSERT(
sizeof( psd->Sacl ) ==
sizeof( psdr->Sacl ) );
00656
ASSERT(
sizeof( psd->Dacl ) ==
sizeof( psdr->Dacl ) );
00657
00658 psd->Owner = owner;
00659 psd->Group = group;
00660 psd->Sacl = sacl;
00661 psd->Dacl = dacl;
00662
00663
return( STATUS_SUCCESS );
00664
00665 }
00666
00667
00668
00669
00670
00671
#define ULONG_PTR_SDEND( _Adr ) ( (ULONG_PTR)(_Adr) + (ULONG_PTR)(_Adr##Size) )
00672
00673 ptr = owner > group ?
ULONG_PTR_SDEND( owner ) :
ULONG_PTR_SDEND( group );
00674 ptr = ptr > (ULONG_PTR)dacl ? ptr :
ULONG_PTR_SDEND( dacl );
00675 ptr = ptr > (ULONG_PTR)sacl ? ptr :
ULONG_PTR_SDEND( sacl );
00676
00677 newBufferSize =
sizeof( SECURITY_DESCRIPTOR );
00678
if ( ptr ) {
00679
00680
#define ULONG_ROUND_UP( x, y ) ((ULONG)(x) + ((y)-1) & ~((y)-1))
00681
00682 newBufferSize +=
ULONG_ROUND_UP( (ULONG_PTR)ptr - (ULONG_PTR)(psdr + 1),
sizeof(PVOID) );
00683 }
00684
00685
00686
00687
00688
00689
00690
if ( newBufferSize > *pBufferSize ) {
00691 *pBufferSize = newBufferSize;
00692
return( STATUS_BUFFER_TOO_SMALL );
00693 }
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
if ( ptr ) {
00705 RtlMoveMemory( (PVOID)(psd + 1), (PVOID)(psdr + 1), newBufferSize -
sizeof( SECURITY_DESCRIPTOR) );
00706 }
00707
00708
00709
00710
00711
00712 RtlpClearControlBits( psd, SE_SELF_RELATIVE );
00713
00714
00715
00716
00717
00718 psd->Owner = (PSID)( owner ? (ULONG_PTR)owner + deltaSize : 0 );
00719 psd->Group = (PSID)( group ? (ULONG_PTR)group + deltaSize : 0 );
00720 psd->Sacl = (PACL)( sacl ? (ULONG_PTR)sacl + deltaSize : 0 );
00721 psd->Dacl = (PACL)( dacl ? (ULONG_PTR)dacl + deltaSize : 0 );
00722
00723
return( STATUS_SUCCESS );
00724
00725 }
00726