@@ -599,4 +599,180 @@ LsapCreateSecretSd(PSECURITY_DESCRIPTOR *SecretSd,
599
599
return Status ;
600
600
}
601
601
602
+
603
+ /**
604
+ * @brief
605
+ * Creates a security descriptor for the token
606
+ * object.
607
+ *
608
+ * @param[in] User
609
+ * A primary user to be given to the function.
610
+ * This user represents the owner that is in
611
+ * charge of this object.
612
+ *
613
+ * @param[out] TokenSd
614
+ * A pointer to an allocated security descriptor
615
+ * for the token object.
616
+ *
617
+ * @param[out] TokenSdSize
618
+ * A pointer to a returned size of the descriptor.
619
+ *
620
+ * @return
621
+ * STATUS_SUCCESS is returned if the function has
622
+ * successfully created the security descriptor.
623
+ * STATUS_INVALID_PARAMETER is returned if one of the
624
+ * parameters are not valid. STATUS_INSUFFICIENT_RESOURCES
625
+ * is returned if memory heap allocation for specific
626
+ * security buffers couldn't be done. A NTSTATUS status
627
+ * code is returned otherwise.
628
+ *
629
+ * @remarks
630
+ * Bot the local system and user are given full access rights
631
+ * for the token (they can open it, read and write into it, etc.)
632
+ * whereas admins can only read from the token. This security
633
+ * descriptor is TO NOT BE confused with the default DACL of the
634
+ * token which is another thing that serves different purpose.
635
+ */
636
+ NTSTATUS
637
+ LsapCreateTokenSd (
638
+ _In_ const TOKEN_USER * User ,
639
+ _Outptr_ PSECURITY_DESCRIPTOR * TokenSd ,
640
+ _Out_ PULONG TokenSdSize )
641
+ {
642
+ SECURITY_DESCRIPTOR AbsoluteSd ;
643
+ PSECURITY_DESCRIPTOR RelativeSd = NULL ;
644
+ ULONG RelativeSdSize = 0 ;
645
+ PSID AdministratorsSid = NULL ;
646
+ PSID LocalSystemSid = NULL ;
647
+ PACL Dacl = NULL ;
648
+ ULONG DaclSize ;
649
+ NTSTATUS Status ;
650
+
651
+ if (TokenSd == NULL || TokenSdSize == NULL )
652
+ return STATUS_INVALID_PARAMETER ;
653
+
654
+ * TokenSd = NULL ;
655
+ * TokenSdSize = 0 ;
656
+
657
+ /* Initialize the SD */
658
+ Status = RtlCreateSecurityDescriptor (& AbsoluteSd ,
659
+ SECURITY_DESCRIPTOR_REVISION );
660
+ if (!NT_SUCCESS (Status ))
661
+ return Status ;
662
+
663
+ Status = RtlAllocateAndInitializeSid (& NtAuthority ,
664
+ 1 ,
665
+ SECURITY_LOCAL_SYSTEM_RID ,
666
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
667
+ & LocalSystemSid );
668
+ if (!NT_SUCCESS (Status ))
669
+ goto done ;
670
+
671
+ Status = RtlAllocateAndInitializeSid (& NtAuthority ,
672
+ 2 ,
673
+ SECURITY_BUILTIN_DOMAIN_RID ,
674
+ DOMAIN_ALIAS_RID_ADMINS ,
675
+ 0 , 0 , 0 , 0 , 0 , 0 ,
676
+ & AdministratorsSid );
677
+ if (!NT_SUCCESS (Status ))
678
+ goto done ;
679
+
680
+ /* Allocate and initialize the DACL */
681
+ DaclSize = sizeof (ACL ) +
682
+ sizeof (ACCESS_ALLOWED_ACE ) + RtlLengthSid (LocalSystemSid ) +
683
+ sizeof (ACCESS_ALLOWED_ACE ) + RtlLengthSid (AdministratorsSid ) +
684
+ sizeof (ACCESS_ALLOWED_ACE ) + RtlLengthSid (User -> User .Sid );
685
+
686
+ Dacl = RtlAllocateHeap (RtlGetProcessHeap (),
687
+ HEAP_ZERO_MEMORY ,
688
+ DaclSize );
689
+ if (Dacl == NULL )
690
+ {
691
+ Status = STATUS_INSUFFICIENT_RESOURCES ;
692
+ goto done ;
693
+ }
694
+
695
+ Status = RtlCreateAcl (Dacl ,
696
+ DaclSize ,
697
+ ACL_REVISION );
698
+ if (!NT_SUCCESS (Status ))
699
+ goto done ;
700
+
701
+ Status = RtlAddAccessAllowedAce (Dacl ,
702
+ ACL_REVISION ,
703
+ TOKEN_ALL_ACCESS ,
704
+ LocalSystemSid );
705
+ if (!NT_SUCCESS (Status ))
706
+ goto done ;
707
+
708
+ Status = RtlAddAccessAllowedAce (Dacl ,
709
+ ACL_REVISION ,
710
+ TOKEN_READ ,
711
+ AdministratorsSid );
712
+ if (!NT_SUCCESS (Status ))
713
+ goto done ;
714
+
715
+ Status = RtlAddAccessAllowedAce (Dacl ,
716
+ ACL_REVISION ,
717
+ TOKEN_ALL_ACCESS ,
718
+ User -> User .Sid );
719
+ if (!NT_SUCCESS (Status ))
720
+ goto done ;
721
+
722
+ Status = RtlSetDaclSecurityDescriptor (& AbsoluteSd ,
723
+ TRUE,
724
+ Dacl ,
725
+ FALSE);
726
+ if (!NT_SUCCESS (Status ))
727
+ goto done ;
728
+
729
+ Status = RtlSetOwnerSecurityDescriptor (& AbsoluteSd ,
730
+ AdministratorsSid ,
731
+ FALSE);
732
+ if (!NT_SUCCESS (Status ))
733
+ goto done ;
734
+
735
+ Status = RtlAbsoluteToSelfRelativeSD (& AbsoluteSd ,
736
+ RelativeSd ,
737
+ & RelativeSdSize );
738
+ if (Status != STATUS_BUFFER_TOO_SMALL )
739
+ goto done ;
740
+
741
+ RelativeSd = RtlAllocateHeap (RtlGetProcessHeap (),
742
+ HEAP_ZERO_MEMORY ,
743
+ RelativeSdSize );
744
+ if (RelativeSd == NULL )
745
+ {
746
+ Status = STATUS_INSUFFICIENT_RESOURCES ;
747
+ goto done ;
748
+ }
749
+
750
+ Status = RtlAbsoluteToSelfRelativeSD (& AbsoluteSd ,
751
+ RelativeSd ,
752
+ & RelativeSdSize );
753
+ if (!NT_SUCCESS (Status ))
754
+ goto done ;
755
+
756
+ * TokenSd = RelativeSd ;
757
+ * TokenSdSize = RelativeSdSize ;
758
+
759
+ done :
760
+ if (Dacl != NULL )
761
+ RtlFreeHeap (RtlGetProcessHeap (), 0 , Dacl );
762
+
763
+ if (AdministratorsSid != NULL )
764
+ RtlFreeHeap (RtlGetProcessHeap (), 0 , AdministratorsSid );
765
+
766
+ if (LocalSystemSid != NULL )
767
+ RtlFreeHeap (RtlGetProcessHeap (), 0 , LocalSystemSid );
768
+
769
+ if (!NT_SUCCESS (Status ))
770
+ {
771
+ if (RelativeSd != NULL )
772
+ RtlFreeHeap (RtlGetProcessHeap (), 0 , RelativeSd );
773
+ }
774
+
775
+ return Status ;
776
+ }
777
+
602
778
/* EOF */
0 commit comments