2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/se/semgr.c
5 * PURPOSE: Security manager
7 * PROGRAMMERS: No programmer listed.
10 /* INCLUDES *******************************************************************/
16 /* GLOBALS ********************************************************************/
18 PSE_EXPORTS SeExports
= NULL
;
19 SE_EXPORTS SepExports
;
21 extern ULONG ExpInitializationPhase
;
22 extern ERESOURCE SepSubjectContextLock
;
24 /* PRIVATE FUNCTIONS **********************************************************/
26 static BOOLEAN INIT_FUNCTION
29 SepExports
.SeCreateTokenPrivilege
= SeCreateTokenPrivilege
;
30 SepExports
.SeAssignPrimaryTokenPrivilege
= SeAssignPrimaryTokenPrivilege
;
31 SepExports
.SeLockMemoryPrivilege
= SeLockMemoryPrivilege
;
32 SepExports
.SeIncreaseQuotaPrivilege
= SeIncreaseQuotaPrivilege
;
33 SepExports
.SeUnsolicitedInputPrivilege
= SeUnsolicitedInputPrivilege
;
34 SepExports
.SeTcbPrivilege
= SeTcbPrivilege
;
35 SepExports
.SeSecurityPrivilege
= SeSecurityPrivilege
;
36 SepExports
.SeTakeOwnershipPrivilege
= SeTakeOwnershipPrivilege
;
37 SepExports
.SeLoadDriverPrivilege
= SeLoadDriverPrivilege
;
38 SepExports
.SeCreatePagefilePrivilege
= SeCreatePagefilePrivilege
;
39 SepExports
.SeIncreaseBasePriorityPrivilege
= SeIncreaseBasePriorityPrivilege
;
40 SepExports
.SeSystemProfilePrivilege
= SeSystemProfilePrivilege
;
41 SepExports
.SeSystemtimePrivilege
= SeSystemtimePrivilege
;
42 SepExports
.SeProfileSingleProcessPrivilege
= SeProfileSingleProcessPrivilege
;
43 SepExports
.SeCreatePermanentPrivilege
= SeCreatePermanentPrivilege
;
44 SepExports
.SeBackupPrivilege
= SeBackupPrivilege
;
45 SepExports
.SeRestorePrivilege
= SeRestorePrivilege
;
46 SepExports
.SeShutdownPrivilege
= SeShutdownPrivilege
;
47 SepExports
.SeDebugPrivilege
= SeDebugPrivilege
;
48 SepExports
.SeAuditPrivilege
= SeAuditPrivilege
;
49 SepExports
.SeSystemEnvironmentPrivilege
= SeSystemEnvironmentPrivilege
;
50 SepExports
.SeChangeNotifyPrivilege
= SeChangeNotifyPrivilege
;
51 SepExports
.SeRemoteShutdownPrivilege
= SeRemoteShutdownPrivilege
;
53 SepExports
.SeNullSid
= SeNullSid
;
54 SepExports
.SeWorldSid
= SeWorldSid
;
55 SepExports
.SeLocalSid
= SeLocalSid
;
56 SepExports
.SeCreatorOwnerSid
= SeCreatorOwnerSid
;
57 SepExports
.SeCreatorGroupSid
= SeCreatorGroupSid
;
58 SepExports
.SeNtAuthoritySid
= SeNtAuthoritySid
;
59 SepExports
.SeDialupSid
= SeDialupSid
;
60 SepExports
.SeNetworkSid
= SeNetworkSid
;
61 SepExports
.SeBatchSid
= SeBatchSid
;
62 SepExports
.SeInteractiveSid
= SeInteractiveSid
;
63 SepExports
.SeLocalSystemSid
= SeLocalSystemSid
;
64 SepExports
.SeAliasAdminsSid
= SeAliasAdminsSid
;
65 SepExports
.SeAliasUsersSid
= SeAliasUsersSid
;
66 SepExports
.SeAliasGuestsSid
= SeAliasGuestsSid
;
67 SepExports
.SeAliasPowerUsersSid
= SeAliasPowerUsersSid
;
68 SepExports
.SeAliasAccountOpsSid
= SeAliasAccountOpsSid
;
69 SepExports
.SeAliasSystemOpsSid
= SeAliasSystemOpsSid
;
70 SepExports
.SeAliasPrintOpsSid
= SeAliasPrintOpsSid
;
71 SepExports
.SeAliasBackupOpsSid
= SeAliasBackupOpsSid
;
72 SepExports
.SeAuthenticatedUsersSid
= SeAuthenticatedUsersSid
;
73 SepExports
.SeRestrictedSid
= SeRestrictedSid
;
74 SepExports
.SeAnonymousLogonSid
= SeAnonymousLogonSid
;
76 SepExports
.SeUndockPrivilege
= SeUndockPrivilege
;
77 SepExports
.SeSyncAgentPrivilege
= SeSyncAgentPrivilege
;
78 SepExports
.SeEnableDelegationPrivilege
= SeEnableDelegationPrivilege
;
80 SeExports
= &SepExports
;
87 SepInitializationPhase0(VOID
)
92 if (!SepInitSecurityIDs()) return FALSE
;
93 if (!SepInitDACLs()) return FALSE
;
94 if (!SepInitSDs()) return FALSE
;
96 if (!SepInitExports()) return FALSE
;
98 /* Initialize the subject context lock */
99 ExInitializeResource(&SepSubjectContextLock
);
101 /* Initialize token objects */
102 SepInitializeTokenImplementation();
104 /* Clear impersonation info for the idle thread */
105 PsGetCurrentThread()->ImpersonationInfo
= NULL
;
106 PspClearCrossThreadFlag(PsGetCurrentThread(),
107 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
109 /* Initialize the boot token */
110 ObInitializeFastReference(&PsGetCurrentProcess()->Token
, NULL
);
111 ObInitializeFastReference(&PsGetCurrentProcess()->Token
,
112 SepCreateSystemProcessToken());
118 SepInitializationPhase1(VOID
)
123 /* Insert the system token into the tree */
124 Status
= ObInsertObject((PVOID
)(PsGetCurrentProcess()->Token
.Value
&
131 ASSERT(NT_SUCCESS(Status
));
133 /* FIXME: TODO \\ Security directory */
141 /* Check the initialization phase */
142 switch (ExpInitializationPhase
)
147 return SepInitializationPhase0();
152 return SepInitializationPhase1();
156 /* Don't know any other phase! Bugcheck! */
157 KeBugCheckEx(UNEXPECTED_INITIALIZATION_CALL
,
159 ExpInitializationPhase
,
170 OBJECT_ATTRIBUTES ObjectAttributes
;
172 HANDLE DirectoryHandle
;
176 /* Create '\Security' directory */
177 RtlInitUnicodeString(&Name
,
179 InitializeObjectAttributes(&ObjectAttributes
,
184 Status
= ZwCreateDirectoryObject(&DirectoryHandle
,
185 DIRECTORY_ALL_ACCESS
,
187 if (!NT_SUCCESS(Status
))
189 DPRINT1("Failed to create 'Security' directory!\n");
193 /* Create 'LSA_AUTHENTICATION_INITALIZED' event */
194 RtlInitUnicodeString(&Name
,
195 L
"\\LSA_AUTHENTICATION_INITALIZED");
196 InitializeObjectAttributes(&ObjectAttributes
,
201 Status
= ZwCreateEvent(&EventHandle
,
204 SynchronizationEvent
,
206 if (!NT_SUCCESS(Status
))
208 DPRINT1("Failed to create 'LSA_AUTHENTICATION_INITALIZED' event!\n");
209 NtClose(DirectoryHandle
);
213 ZwClose(EventHandle
);
214 ZwClose(DirectoryHandle
);
216 /* FIXME: Create SRM port and listener thread */
223 SeDefaultObjectMethod(IN PVOID Object
,
224 IN SECURITY_OPERATION_CODE OperationType
,
225 IN PSECURITY_INFORMATION SecurityInformation
,
226 IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor
,
227 IN OUT PULONG ReturnLength OPTIONAL
,
228 IN OUT PSECURITY_DESCRIPTOR
*OldSecurityDescriptor
,
229 IN POOL_TYPE PoolType
,
230 IN PGENERIC_MAPPING GenericMapping
)
234 /* Select the operation type */
235 switch (OperationType
)
237 /* Setting a new descriptor */
238 case SetSecurityDescriptor
:
241 ASSERT((PoolType
== PagedPool
) || (PoolType
== NonPagedPool
));
243 /* Set the information */
244 return ObSetSecurityDescriptorInfo(Object
,
247 OldSecurityDescriptor
,
251 case QuerySecurityDescriptor
:
253 /* Query the information */
254 return ObQuerySecurityDescriptorInfo(Object
,
258 OldSecurityDescriptor
);
260 case DeleteSecurityDescriptor
:
263 return ObDeassignSecurity(OldSecurityDescriptor
);
265 case AssignSecurityDescriptor
:
268 ObAssignObjectSecurityDescriptor(Object
, SecurityDescriptor
, PoolType
);
269 return STATUS_SUCCESS
;
274 KeBugCheckEx(SECURITY_SYSTEM
, 0, STATUS_INVALID_PARAMETER
, 0, 0);
277 /* Should never reach here */
279 return STATUS_SUCCESS
;
282 ULONG SidInTokenCalls
= 0;
285 SepSidInToken(PACCESS_TOKEN _Token
,
289 PTOKEN Token
= (PTOKEN
)_Token
;
294 if (!(SidInTokenCalls
% 10000)) DPRINT1("SidInToken Calls: %d\n", SidInTokenCalls
);
296 if (Token
->UserAndGroupCount
== 0)
301 for (i
=0; i
<Token
->UserAndGroupCount
; i
++)
303 if (RtlEqualSid(Sid
, Token
->UserAndGroups
[i
].Sid
))
305 if ((i
== 0)|| (Token
->UserAndGroups
[i
].Attributes
& SE_GROUP_ENABLED
))
318 SepTokenIsOwner(PACCESS_TOKEN Token
,
319 PSECURITY_DESCRIPTOR SecurityDescriptor
)
325 Status
= RtlGetOwnerSecurityDescriptor(SecurityDescriptor
,
328 if (!NT_SUCCESS(Status
))
330 DPRINT1("RtlGetOwnerSecurityDescriptor() failed (Status %lx)\n", Status
);
336 DPRINT1("Owner Sid is NULL\n");
340 return SepSidInToken(Token
, Sid
);
344 SeQuerySecurityAccessMask(IN SECURITY_INFORMATION SecurityInformation
,
345 OUT PACCESS_MASK DesiredAccess
)
349 if (SecurityInformation
& (OWNER_SECURITY_INFORMATION
|
350 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
))
352 *DesiredAccess
|= READ_CONTROL
;
354 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
356 *DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
361 SeSetSecurityAccessMask(IN SECURITY_INFORMATION SecurityInformation
,
362 OUT PACCESS_MASK DesiredAccess
)
366 if (SecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
368 *DesiredAccess
|= WRITE_OWNER
;
370 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
372 *DesiredAccess
|= WRITE_DAC
;
374 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
376 *DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
381 SepAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
382 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext
,
383 IN ACCESS_MASK DesiredAccess
,
384 IN ACCESS_MASK PreviouslyGrantedAccess
,
385 OUT PPRIVILEGE_SET
* Privileges
,
386 IN PGENERIC_MAPPING GenericMapping
,
387 IN KPROCESSOR_MODE AccessMode
,
388 OUT PACCESS_MASK GrantedAccess
,
389 OUT PNTSTATUS AccessStatus
)
391 LUID_AND_ATTRIBUTES Privilege
;
392 ACCESS_MASK RemainingAccess
;
393 ACCESS_MASK TempAccess
;
394 ACCESS_MASK TempGrantedAccess
= 0;
395 ACCESS_MASK TempDeniedAccess
= 0;
406 /* Check for no access desired */
409 /* Check if we had no previous access */
410 if (!PreviouslyGrantedAccess
)
412 /* Then there's nothing to give */
413 *AccessStatus
= STATUS_ACCESS_DENIED
;
417 /* Return the previous access only */
418 *GrantedAccess
= PreviouslyGrantedAccess
;
419 *AccessStatus
= STATUS_SUCCESS
;
424 /* Map given accesses */
425 RtlMapGenericMask(&DesiredAccess
, GenericMapping
);
426 if (PreviouslyGrantedAccess
)
427 RtlMapGenericMask(&PreviouslyGrantedAccess
, GenericMapping
);
429 /* Initialize remaining access rights */
430 RemainingAccess
= DesiredAccess
;
432 Token
= SubjectSecurityContext
->ClientToken
?
433 SubjectSecurityContext
->ClientToken
: SubjectSecurityContext
->PrimaryToken
;
435 /* Check for system security access */
436 if (RemainingAccess
& ACCESS_SYSTEM_SECURITY
)
438 Privilege
.Luid
= SeSecurityPrivilege
;
439 Privilege
.Attributes
= SE_PRIVILEGE_ENABLED
;
441 /* Fail if we do not the SeSecurityPrivilege */
442 if (!SepPrivilegeCheck(Token
,
445 PRIVILEGE_SET_ALL_NECESSARY
,
448 *AccessStatus
= STATUS_PRIVILEGE_NOT_HELD
;
452 /* Adjust access rights */
453 RemainingAccess
&= ~ACCESS_SYSTEM_SECURITY
;
454 PreviouslyGrantedAccess
|= ACCESS_SYSTEM_SECURITY
;
456 /* Succeed if there are no more rights to grant */
457 if (RemainingAccess
== 0)
459 *GrantedAccess
= PreviouslyGrantedAccess
;
460 *AccessStatus
= STATUS_SUCCESS
;
466 Status
= RtlGetDaclSecurityDescriptor(SecurityDescriptor
,
470 if (!NT_SUCCESS(Status
))
472 *AccessStatus
= Status
;
476 /* RULE 1: Grant desired access if the object is unprotected */
477 if (Present
== FALSE
|| Dacl
== NULL
)
479 if (DesiredAccess
& MAXIMUM_ALLOWED
)
481 *GrantedAccess
= GenericMapping
->GenericAll
;
482 *GrantedAccess
|= (DesiredAccess
& ~MAXIMUM_ALLOWED
);
486 *GrantedAccess
= DesiredAccess
| PreviouslyGrantedAccess
;
489 *AccessStatus
= STATUS_SUCCESS
;
493 /* RULE 2: Check token for 'take ownership' privilege */
494 if (DesiredAccess
& WRITE_OWNER
)
496 Privilege
.Luid
= SeTakeOwnershipPrivilege
;
497 Privilege
.Attributes
= SE_PRIVILEGE_ENABLED
;
499 if (SepPrivilegeCheck(Token
,
502 PRIVILEGE_SET_ALL_NECESSARY
,
505 /* Adjust access rights */
506 RemainingAccess
&= ~WRITE_OWNER
;
507 PreviouslyGrantedAccess
|= WRITE_OWNER
;
509 /* Succeed if there are no more rights to grant */
510 if (RemainingAccess
== 0)
512 *GrantedAccess
= PreviouslyGrantedAccess
;
513 *AccessStatus
= STATUS_SUCCESS
;
519 /* Deny access if the DACL is empty */
520 if (Dacl
->AceCount
== 0)
522 if (RemainingAccess
== MAXIMUM_ALLOWED
&& PreviouslyGrantedAccess
!= 0)
524 *GrantedAccess
= PreviouslyGrantedAccess
;
525 *AccessStatus
= STATUS_SUCCESS
;
531 *AccessStatus
= STATUS_ACCESS_DENIED
;
536 /* Fail if DACL is absent */
537 if (Present
== FALSE
)
540 *AccessStatus
= STATUS_ACCESS_DENIED
;
544 /* Determine the MAXIMUM_ALLOWED access rights according to the DACL */
545 if (DesiredAccess
& MAXIMUM_ALLOWED
)
547 CurrentAce
= (PACE
)(Dacl
+ 1);
548 for (i
= 0; i
< Dacl
->AceCount
; i
++)
550 Sid
= (PSID
)(CurrentAce
+ 1);
551 if (CurrentAce
->Header
.AceType
== ACCESS_DENIED_ACE_TYPE
)
553 if (SepSidInToken(Token
, Sid
))
555 /* Map access rights from the ACE */
556 TempAccess
= CurrentAce
->AccessMask
;
557 RtlMapGenericMask(&TempAccess
, GenericMapping
);
559 /* Deny access rights that have not been granted yet */
560 TempDeniedAccess
|= (TempAccess
& ~TempGrantedAccess
);
563 else if (CurrentAce
->Header
.AceType
== ACCESS_ALLOWED_ACE_TYPE
)
565 if (SepSidInToken(Token
, Sid
))
567 /* Map access rights from the ACE */
568 TempAccess
= CurrentAce
->AccessMask
;
569 RtlMapGenericMask(&TempAccess
, GenericMapping
);
571 /* Grant access rights that have not been denied yet */
572 TempGrantedAccess
|= (TempAccess
& ~TempDeniedAccess
);
577 DPRINT1("Unsupported ACE type 0x%lx\n", CurrentAce
->Header
.AceType
);
580 /* Get the next ACE */
581 CurrentAce
= (PACE
)((ULONG_PTR
)CurrentAce
+ CurrentAce
->Header
.AceSize
);
584 /* Fail if some rights have not been granted */
585 RemainingAccess
&= ~(MAXIMUM_ALLOWED
| TempGrantedAccess
);
586 if (RemainingAccess
!= 0)
589 *AccessStatus
= STATUS_ACCESS_DENIED
;
593 /* Set granted access right and access status */
594 *GrantedAccess
= TempGrantedAccess
| PreviouslyGrantedAccess
;
595 if (*GrantedAccess
!= 0)
597 *AccessStatus
= STATUS_SUCCESS
;
602 *AccessStatus
= STATUS_ACCESS_DENIED
;
607 /* RULE 4: Grant rights according to the DACL */
608 CurrentAce
= (PACE
)(Dacl
+ 1);
609 for (i
= 0; i
< Dacl
->AceCount
; i
++)
611 Sid
= (PSID
)(CurrentAce
+ 1);
612 if (CurrentAce
->Header
.AceType
== ACCESS_DENIED_ACE_TYPE
)
614 if (SepSidInToken(Token
, Sid
))
616 /* Map access rights from the ACE */
617 TempAccess
= CurrentAce
->AccessMask
;
618 RtlMapGenericMask(&TempAccess
, GenericMapping
);
620 /* Leave if a remaining right must be denied */
621 if (RemainingAccess
& TempAccess
)
625 else if (CurrentAce
->Header
.AceType
== ACCESS_ALLOWED_ACE_TYPE
)
627 if (SepSidInToken(Token
, Sid
))
629 /* Map access rights from the ACE */
630 TempAccess
= CurrentAce
->AccessMask
;
631 RtlMapGenericMask(&TempAccess
, GenericMapping
);
633 /* Remove granted rights */
634 RemainingAccess
&= ~TempAccess
;
639 DPRINT1("Unsupported ACE type 0x%lx\n", CurrentAce
->Header
.AceType
);
642 /* Get the next ACE */
643 CurrentAce
= (PACE
)((ULONG_PTR
)CurrentAce
+ CurrentAce
->Header
.AceSize
);
646 DPRINT("DesiredAccess %08lx\nPreviouslyGrantedAccess %08lx\nRemainingAccess %08lx\n",
647 DesiredAccess
, PreviouslyGrantedAccess
, RemainingAccess
);
649 /* Fail if some rights have not been granted */
650 if (RemainingAccess
!= 0)
653 *AccessStatus
= STATUS_ACCESS_DENIED
;
657 /* Set granted access rights */
658 *GrantedAccess
= DesiredAccess
| PreviouslyGrantedAccess
;
660 DPRINT("GrantedAccess %08lx\n", *GrantedAccess
);
662 /* Fail if no rights have been granted */
663 if (*GrantedAccess
== 0)
665 *AccessStatus
= STATUS_ACCESS_DENIED
;
669 *AccessStatus
= STATUS_SUCCESS
;
674 SepGetSDOwner(IN PSECURITY_DESCRIPTOR _SecurityDescriptor
)
676 PISECURITY_DESCRIPTOR SecurityDescriptor
= _SecurityDescriptor
;
679 if (SecurityDescriptor
->Control
& SE_SELF_RELATIVE
)
680 Owner
= (PSID
)((ULONG_PTR
)SecurityDescriptor
->Owner
+
681 (ULONG_PTR
)SecurityDescriptor
);
683 Owner
= (PSID
)SecurityDescriptor
->Owner
;
689 SepGetSDGroup(IN PSECURITY_DESCRIPTOR _SecurityDescriptor
)
691 PISECURITY_DESCRIPTOR SecurityDescriptor
= _SecurityDescriptor
;
694 if (SecurityDescriptor
->Control
& SE_SELF_RELATIVE
)
695 Group
= (PSID
)((ULONG_PTR
)SecurityDescriptor
->Group
+
696 (ULONG_PTR
)SecurityDescriptor
);
698 Group
= (PSID
)SecurityDescriptor
->Group
;
704 /* PUBLIC FUNCTIONS ***********************************************************/
710 SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
711 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext
,
712 IN BOOLEAN SubjectContextLocked
,
713 IN ACCESS_MASK DesiredAccess
,
714 IN ACCESS_MASK PreviouslyGrantedAccess
,
715 OUT PPRIVILEGE_SET
* Privileges
,
716 IN PGENERIC_MAPPING GenericMapping
,
717 IN KPROCESSOR_MODE AccessMode
,
718 OUT PACCESS_MASK GrantedAccess
,
719 OUT PNTSTATUS AccessStatus
)
725 /* Check if this is kernel mode */
726 if (AccessMode
== KernelMode
)
728 /* Check if kernel wants everything */
729 if (DesiredAccess
& MAXIMUM_ALLOWED
)
732 *GrantedAccess
= GenericMapping
->GenericAll
;
733 *GrantedAccess
|= (DesiredAccess
&~ MAXIMUM_ALLOWED
);
734 *GrantedAccess
|= PreviouslyGrantedAccess
;
738 /* Give the desired and previous access */
739 *GrantedAccess
= DesiredAccess
| PreviouslyGrantedAccess
;
743 *AccessStatus
= STATUS_SUCCESS
;
747 /* Check if we didn't get an SD */
748 if (!SecurityDescriptor
)
750 /* Automatic failure */
751 *AccessStatus
= STATUS_ACCESS_DENIED
;
755 /* Check for invalid impersonation */
756 if ((SubjectSecurityContext
->ClientToken
) &&
757 (SubjectSecurityContext
->ImpersonationLevel
< SecurityImpersonation
))
759 *AccessStatus
= STATUS_BAD_IMPERSONATION_LEVEL
;
763 /* Acquire the lock if needed */
764 if (!SubjectContextLocked
)
765 SeLockSubjectContext(SubjectSecurityContext
);
767 /* Check if the token is the owner and grant WRITE_DAC and READ_CONTROL rights */
768 if (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
| MAXIMUM_ALLOWED
))
770 PACCESS_TOKEN Token
= SubjectSecurityContext
->ClientToken
?
771 SubjectSecurityContext
->ClientToken
: SubjectSecurityContext
->PrimaryToken
;
773 if (SepTokenIsOwner(Token
,
776 if (DesiredAccess
& MAXIMUM_ALLOWED
)
777 PreviouslyGrantedAccess
|= (WRITE_DAC
| READ_CONTROL
);
779 PreviouslyGrantedAccess
|= (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
));
781 DesiredAccess
&= ~(WRITE_DAC
| READ_CONTROL
);
785 if (DesiredAccess
== 0)
787 *GrantedAccess
= PreviouslyGrantedAccess
;
788 *AccessStatus
= STATUS_SUCCESS
;
793 /* Call the internal function */
794 ret
= SepAccessCheck(SecurityDescriptor
,
795 SubjectSecurityContext
,
797 PreviouslyGrantedAccess
,
805 /* Release the lock if needed */
806 if (!SubjectContextLocked
)
807 SeUnlockSubjectContext(SubjectSecurityContext
);
812 /* SYSTEM CALLS ***************************************************************/
819 NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
820 IN HANDLE TokenHandle
,
821 IN ACCESS_MASK DesiredAccess
,
822 IN PGENERIC_MAPPING GenericMapping
,
823 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL
,
824 IN OUT PULONG PrivilegeSetLength
,
825 OUT PACCESS_MASK GrantedAccess
,
826 OUT PNTSTATUS AccessStatus
)
828 PSECURITY_DESCRIPTOR CapturedSecurityDescriptor
= NULL
;
829 SECURITY_SUBJECT_CONTEXT SubjectSecurityContext
;
830 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
831 ACCESS_MASK PreviouslyGrantedAccess
= 0;
836 /* Check if this is kernel mode */
837 if (PreviousMode
== KernelMode
)
839 /* Check if kernel wants everything */
840 if (DesiredAccess
& MAXIMUM_ALLOWED
)
843 *GrantedAccess
= GenericMapping
->GenericAll
;
844 *GrantedAccess
|= (DesiredAccess
&~ MAXIMUM_ALLOWED
);
848 /* Just give the desired access */
849 *GrantedAccess
= DesiredAccess
;
853 *AccessStatus
= STATUS_SUCCESS
;
854 return STATUS_SUCCESS
;
857 /* Protect probe in SEH */
860 /* Probe all pointers */
861 ProbeForRead(GenericMapping
, sizeof(GENERIC_MAPPING
), sizeof(ULONG
));
862 ProbeForRead(PrivilegeSetLength
, sizeof(ULONG
), sizeof(ULONG
));
863 ProbeForWrite(PrivilegeSet
, *PrivilegeSetLength
, sizeof(ULONG
));
864 ProbeForWrite(GrantedAccess
, sizeof(ACCESS_MASK
), sizeof(ULONG
));
865 ProbeForWrite(AccessStatus
, sizeof(NTSTATUS
), sizeof(ULONG
));
867 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
869 /* Return the exception code */
870 _SEH2_YIELD(return _SEH2_GetExceptionCode());
874 /* Check for unmapped access rights */
875 if (DesiredAccess
& (GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
))
876 return STATUS_GENERIC_NOT_MAPPED
;
878 /* Reference the token */
879 Status
= ObReferenceObjectByHandle(TokenHandle
,
885 if (!NT_SUCCESS(Status
))
887 DPRINT("Failed to reference token (Status %lx)\n", Status
);
891 /* Check token type */
892 if (Token
->TokenType
!= TokenImpersonation
)
894 DPRINT("No impersonation token\n");
895 ObDereferenceObject(Token
);
896 return STATUS_NO_IMPERSONATION_TOKEN
;
899 /* Check the impersonation level */
900 if (Token
->ImpersonationLevel
< SecurityIdentification
)
902 DPRINT("Impersonation level < SecurityIdentification\n");
903 ObDereferenceObject(Token
);
904 return STATUS_BAD_IMPERSONATION_LEVEL
;
907 /* Capture the security descriptor */
908 Status
= SeCaptureSecurityDescriptor(SecurityDescriptor
,
912 &CapturedSecurityDescriptor
);
913 if (!NT_SUCCESS(Status
))
915 DPRINT("Failed to capture the Security Descriptor\n");
916 ObDereferenceObject(Token
);
920 /* Check the captured security descriptor */
921 if (CapturedSecurityDescriptor
== NULL
)
923 DPRINT("Security Descriptor is NULL\n");
924 ObDereferenceObject(Token
);
925 return STATUS_INVALID_SECURITY_DESCR
;
928 /* Check security descriptor for valid owner and group */
929 if (SepGetSDOwner(SecurityDescriptor
) == NULL
|| // FIXME: use CapturedSecurityDescriptor
930 SepGetSDGroup(SecurityDescriptor
) == NULL
) // FIXME: use CapturedSecurityDescriptor
932 DPRINT("Security Descriptor does not have a valid group or owner\n");
933 SeReleaseSecurityDescriptor(CapturedSecurityDescriptor
,
936 ObDereferenceObject(Token
);
937 return STATUS_INVALID_SECURITY_DESCR
;
940 /* Set up the subject context, and lock it */
941 SubjectSecurityContext
.ClientToken
= Token
;
942 SubjectSecurityContext
.ImpersonationLevel
= Token
->ImpersonationLevel
;
943 SubjectSecurityContext
.PrimaryToken
= NULL
;
944 SubjectSecurityContext
.ProcessAuditId
= NULL
;
945 SeLockSubjectContext(&SubjectSecurityContext
);
947 /* Check if the token is the owner and grant WRITE_DAC and READ_CONTROL rights */
948 if (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
| MAXIMUM_ALLOWED
))
950 if (SepTokenIsOwner(Token
, SecurityDescriptor
)) // FIXME: use CapturedSecurityDescriptor
952 if (DesiredAccess
& MAXIMUM_ALLOWED
)
953 PreviouslyGrantedAccess
|= (WRITE_DAC
| READ_CONTROL
);
955 PreviouslyGrantedAccess
|= (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
));
957 DesiredAccess
&= ~(WRITE_DAC
| READ_CONTROL
);
961 if (DesiredAccess
== 0)
963 *GrantedAccess
= PreviouslyGrantedAccess
;
964 *AccessStatus
= STATUS_SUCCESS
;
968 /* Now perform the access check */
969 SepAccessCheck(SecurityDescriptor
, // FIXME: use CapturedSecurityDescriptor
970 &SubjectSecurityContext
,
972 PreviouslyGrantedAccess
,
973 &PrivilegeSet
, //FIXME
980 /* Unlock subject context */
981 SeUnlockSubjectContext(&SubjectSecurityContext
);
983 /* Release the captured security descriptor */
984 SeReleaseSecurityDescriptor(CapturedSecurityDescriptor
,
988 /* Dereference the token */
989 ObDereferenceObject(Token
);
991 /* Check succeeded */
992 return STATUS_SUCCESS
;
998 NtAccessCheckByType(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
999 IN PSID PrincipalSelfSid
,
1000 IN HANDLE ClientToken
,
1001 IN ACCESS_MASK DesiredAccess
,
1002 IN POBJECT_TYPE_LIST ObjectTypeList
,
1003 IN ULONG ObjectTypeLength
,
1004 IN PGENERIC_MAPPING GenericMapping
,
1005 IN PPRIVILEGE_SET PrivilegeSet
,
1006 IN ULONG PrivilegeSetLength
,
1007 OUT PACCESS_MASK GrantedAccess
,
1008 OUT PNTSTATUS AccessStatus
)
1011 return STATUS_NOT_IMPLEMENTED
;
1016 NtAccessCheckByTypeAndAuditAlarm(IN PUNICODE_STRING SubsystemName
,
1018 IN PUNICODE_STRING ObjectTypeName
,
1019 IN PUNICODE_STRING ObjectName
,
1020 IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1021 IN PSID PrincipalSelfSid
,
1022 IN ACCESS_MASK DesiredAccess
,
1023 IN AUDIT_EVENT_TYPE AuditType
,
1025 IN POBJECT_TYPE_LIST ObjectTypeList
,
1026 IN ULONG ObjectTypeLength
,
1027 IN PGENERIC_MAPPING GenericMapping
,
1028 IN BOOLEAN ObjectCreation
,
1029 OUT PACCESS_MASK GrantedAccess
,
1030 OUT PNTSTATUS AccessStatus
,
1031 OUT PBOOLEAN GenerateOnClose
)
1034 return STATUS_NOT_IMPLEMENTED
;
1039 NtAccessCheckByTypeResultList(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1040 IN PSID PrincipalSelfSid
,
1041 IN HANDLE ClientToken
,
1042 IN ACCESS_MASK DesiredAccess
,
1043 IN POBJECT_TYPE_LIST ObjectTypeList
,
1044 IN ULONG ObjectTypeLength
,
1045 IN PGENERIC_MAPPING GenericMapping
,
1046 IN PPRIVILEGE_SET PrivilegeSet
,
1047 IN ULONG PrivilegeSetLength
,
1048 OUT PACCESS_MASK GrantedAccess
,
1049 OUT PNTSTATUS AccessStatus
)
1052 return STATUS_NOT_IMPLEMENTED
;
1057 NtAccessCheckByTypeResultListAndAuditAlarm(IN PUNICODE_STRING SubsystemName
,
1059 IN PUNICODE_STRING ObjectTypeName
,
1060 IN PUNICODE_STRING ObjectName
,
1061 IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1062 IN PSID PrincipalSelfSid
,
1063 IN ACCESS_MASK DesiredAccess
,
1064 IN AUDIT_EVENT_TYPE AuditType
,
1066 IN POBJECT_TYPE_LIST ObjectTypeList
,
1067 IN ULONG ObjectTypeLength
,
1068 IN PGENERIC_MAPPING GenericMapping
,
1069 IN BOOLEAN ObjectCreation
,
1070 OUT PACCESS_MASK GrantedAccess
,
1071 OUT PNTSTATUS AccessStatus
,
1072 OUT PBOOLEAN GenerateOnClose
)
1075 return STATUS_NOT_IMPLEMENTED
;
1080 NtAccessCheckByTypeResultListAndAuditAlarmByHandle(IN PUNICODE_STRING SubsystemName
,
1082 IN HANDLE ClientToken
,
1083 IN PUNICODE_STRING ObjectTypeName
,
1084 IN PUNICODE_STRING ObjectName
,
1085 IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1086 IN PSID PrincipalSelfSid
,
1087 IN ACCESS_MASK DesiredAccess
,
1088 IN AUDIT_EVENT_TYPE AuditType
,
1090 IN POBJECT_TYPE_LIST ObjectTypeList
,
1091 IN ULONG ObjectTypeLength
,
1092 IN PGENERIC_MAPPING GenericMapping
,
1093 IN BOOLEAN ObjectCreation
,
1094 OUT PACCESS_MASK GrantedAccess
,
1095 OUT PNTSTATUS AccessStatus
,
1096 OUT PBOOLEAN GenerateOnClose
)
1099 return STATUS_NOT_IMPLEMENTED
;