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 if (!(CurrentAce
->Header
.AceFlags
& INHERIT_ONLY_ACE
))
552 Sid
= (PSID
)(CurrentAce
+ 1);
553 if (CurrentAce
->Header
.AceType
== ACCESS_DENIED_ACE_TYPE
)
555 if (SepSidInToken(Token
, Sid
))
557 /* Map access rights from the ACE */
558 TempAccess
= CurrentAce
->AccessMask
;
559 RtlMapGenericMask(&TempAccess
, GenericMapping
);
561 /* Deny access rights that have not been granted yet */
562 TempDeniedAccess
|= (TempAccess
& ~TempGrantedAccess
);
565 else if (CurrentAce
->Header
.AceType
== ACCESS_ALLOWED_ACE_TYPE
)
567 if (SepSidInToken(Token
, Sid
))
569 /* Map access rights from the ACE */
570 TempAccess
= CurrentAce
->AccessMask
;
571 RtlMapGenericMask(&TempAccess
, GenericMapping
);
573 /* Grant access rights that have not been denied yet */
574 TempGrantedAccess
|= (TempAccess
& ~TempDeniedAccess
);
579 DPRINT1("Unsupported ACE type 0x%lx\n", CurrentAce
->Header
.AceType
);
583 /* Get the next ACE */
584 CurrentAce
= (PACE
)((ULONG_PTR
)CurrentAce
+ CurrentAce
->Header
.AceSize
);
587 /* Fail if some rights have not been granted */
588 RemainingAccess
&= ~(MAXIMUM_ALLOWED
| TempGrantedAccess
);
589 if (RemainingAccess
!= 0)
592 *AccessStatus
= STATUS_ACCESS_DENIED
;
596 /* Set granted access right and access status */
597 *GrantedAccess
= TempGrantedAccess
| PreviouslyGrantedAccess
;
598 if (*GrantedAccess
!= 0)
600 *AccessStatus
= STATUS_SUCCESS
;
605 *AccessStatus
= STATUS_ACCESS_DENIED
;
610 /* RULE 4: Grant rights according to the DACL */
611 CurrentAce
= (PACE
)(Dacl
+ 1);
612 for (i
= 0; i
< Dacl
->AceCount
; i
++)
614 if (!(CurrentAce
->Header
.AceFlags
& INHERIT_ONLY_ACE
))
616 Sid
= (PSID
)(CurrentAce
+ 1);
617 if (CurrentAce
->Header
.AceType
== ACCESS_DENIED_ACE_TYPE
)
619 if (SepSidInToken(Token
, Sid
))
621 /* Map access rights from the ACE */
622 TempAccess
= CurrentAce
->AccessMask
;
623 RtlMapGenericMask(&TempAccess
, GenericMapping
);
625 /* Leave if a remaining right must be denied */
626 if (RemainingAccess
& TempAccess
)
630 else if (CurrentAce
->Header
.AceType
== ACCESS_ALLOWED_ACE_TYPE
)
632 if (SepSidInToken(Token
, Sid
))
634 /* Map access rights from the ACE */
635 TempAccess
= CurrentAce
->AccessMask
;
636 RtlMapGenericMask(&TempAccess
, GenericMapping
);
638 /* Remove granted rights */
639 RemainingAccess
&= ~TempAccess
;
644 DPRINT1("Unsupported ACE type 0x%lx\n", CurrentAce
->Header
.AceType
);
648 /* Get the next ACE */
649 CurrentAce
= (PACE
)((ULONG_PTR
)CurrentAce
+ CurrentAce
->Header
.AceSize
);
652 DPRINT("DesiredAccess %08lx\nPreviouslyGrantedAccess %08lx\nRemainingAccess %08lx\n",
653 DesiredAccess
, PreviouslyGrantedAccess
, RemainingAccess
);
655 /* Fail if some rights have not been granted */
656 if (RemainingAccess
!= 0)
659 *AccessStatus
= STATUS_ACCESS_DENIED
;
663 /* Set granted access rights */
664 *GrantedAccess
= DesiredAccess
| PreviouslyGrantedAccess
;
666 DPRINT("GrantedAccess %08lx\n", *GrantedAccess
);
668 /* Fail if no rights have been granted */
669 if (*GrantedAccess
== 0)
671 *AccessStatus
= STATUS_ACCESS_DENIED
;
675 *AccessStatus
= STATUS_SUCCESS
;
680 SepGetSDOwner(IN PSECURITY_DESCRIPTOR _SecurityDescriptor
)
682 PISECURITY_DESCRIPTOR SecurityDescriptor
= _SecurityDescriptor
;
685 if (SecurityDescriptor
->Control
& SE_SELF_RELATIVE
)
686 Owner
= (PSID
)((ULONG_PTR
)SecurityDescriptor
->Owner
+
687 (ULONG_PTR
)SecurityDescriptor
);
689 Owner
= (PSID
)SecurityDescriptor
->Owner
;
695 SepGetSDGroup(IN PSECURITY_DESCRIPTOR _SecurityDescriptor
)
697 PISECURITY_DESCRIPTOR SecurityDescriptor
= _SecurityDescriptor
;
700 if (SecurityDescriptor
->Control
& SE_SELF_RELATIVE
)
701 Group
= (PSID
)((ULONG_PTR
)SecurityDescriptor
->Group
+
702 (ULONG_PTR
)SecurityDescriptor
);
704 Group
= (PSID
)SecurityDescriptor
->Group
;
710 /* PUBLIC FUNCTIONS ***********************************************************/
716 SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
717 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext
,
718 IN BOOLEAN SubjectContextLocked
,
719 IN ACCESS_MASK DesiredAccess
,
720 IN ACCESS_MASK PreviouslyGrantedAccess
,
721 OUT PPRIVILEGE_SET
* Privileges
,
722 IN PGENERIC_MAPPING GenericMapping
,
723 IN KPROCESSOR_MODE AccessMode
,
724 OUT PACCESS_MASK GrantedAccess
,
725 OUT PNTSTATUS AccessStatus
)
731 /* Check if this is kernel mode */
732 if (AccessMode
== KernelMode
)
734 /* Check if kernel wants everything */
735 if (DesiredAccess
& MAXIMUM_ALLOWED
)
738 *GrantedAccess
= GenericMapping
->GenericAll
;
739 *GrantedAccess
|= (DesiredAccess
&~ MAXIMUM_ALLOWED
);
740 *GrantedAccess
|= PreviouslyGrantedAccess
;
744 /* Give the desired and previous access */
745 *GrantedAccess
= DesiredAccess
| PreviouslyGrantedAccess
;
749 *AccessStatus
= STATUS_SUCCESS
;
753 /* Check if we didn't get an SD */
754 if (!SecurityDescriptor
)
756 /* Automatic failure */
757 *AccessStatus
= STATUS_ACCESS_DENIED
;
761 /* Check for invalid impersonation */
762 if ((SubjectSecurityContext
->ClientToken
) &&
763 (SubjectSecurityContext
->ImpersonationLevel
< SecurityImpersonation
))
765 *AccessStatus
= STATUS_BAD_IMPERSONATION_LEVEL
;
769 /* Acquire the lock if needed */
770 if (!SubjectContextLocked
)
771 SeLockSubjectContext(SubjectSecurityContext
);
773 /* Check if the token is the owner and grant WRITE_DAC and READ_CONTROL rights */
774 if (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
| MAXIMUM_ALLOWED
))
776 PACCESS_TOKEN Token
= SubjectSecurityContext
->ClientToken
?
777 SubjectSecurityContext
->ClientToken
: SubjectSecurityContext
->PrimaryToken
;
779 if (SepTokenIsOwner(Token
,
782 if (DesiredAccess
& MAXIMUM_ALLOWED
)
783 PreviouslyGrantedAccess
|= (WRITE_DAC
| READ_CONTROL
);
785 PreviouslyGrantedAccess
|= (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
));
787 DesiredAccess
&= ~(WRITE_DAC
| READ_CONTROL
);
791 if (DesiredAccess
== 0)
793 *GrantedAccess
= PreviouslyGrantedAccess
;
794 *AccessStatus
= STATUS_SUCCESS
;
799 /* Call the internal function */
800 ret
= SepAccessCheck(SecurityDescriptor
,
801 SubjectSecurityContext
,
803 PreviouslyGrantedAccess
,
811 /* Release the lock if needed */
812 if (!SubjectContextLocked
)
813 SeUnlockSubjectContext(SubjectSecurityContext
);
818 /* SYSTEM CALLS ***************************************************************/
825 NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
826 IN HANDLE TokenHandle
,
827 IN ACCESS_MASK DesiredAccess
,
828 IN PGENERIC_MAPPING GenericMapping
,
829 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL
,
830 IN OUT PULONG PrivilegeSetLength
,
831 OUT PACCESS_MASK GrantedAccess
,
832 OUT PNTSTATUS AccessStatus
)
834 PSECURITY_DESCRIPTOR CapturedSecurityDescriptor
= NULL
;
835 SECURITY_SUBJECT_CONTEXT SubjectSecurityContext
;
836 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
837 ACCESS_MASK PreviouslyGrantedAccess
= 0;
842 /* Check if this is kernel mode */
843 if (PreviousMode
== KernelMode
)
845 /* Check if kernel wants everything */
846 if (DesiredAccess
& MAXIMUM_ALLOWED
)
849 *GrantedAccess
= GenericMapping
->GenericAll
;
850 *GrantedAccess
|= (DesiredAccess
&~ MAXIMUM_ALLOWED
);
854 /* Just give the desired access */
855 *GrantedAccess
= DesiredAccess
;
859 *AccessStatus
= STATUS_SUCCESS
;
860 return STATUS_SUCCESS
;
863 /* Protect probe in SEH */
866 /* Probe all pointers */
867 ProbeForRead(GenericMapping
, sizeof(GENERIC_MAPPING
), sizeof(ULONG
));
868 ProbeForRead(PrivilegeSetLength
, sizeof(ULONG
), sizeof(ULONG
));
869 ProbeForWrite(PrivilegeSet
, *PrivilegeSetLength
, sizeof(ULONG
));
870 ProbeForWrite(GrantedAccess
, sizeof(ACCESS_MASK
), sizeof(ULONG
));
871 ProbeForWrite(AccessStatus
, sizeof(NTSTATUS
), sizeof(ULONG
));
873 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
875 /* Return the exception code */
876 _SEH2_YIELD(return _SEH2_GetExceptionCode());
880 /* Check for unmapped access rights */
881 if (DesiredAccess
& (GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
))
882 return STATUS_GENERIC_NOT_MAPPED
;
884 /* Reference the token */
885 Status
= ObReferenceObjectByHandle(TokenHandle
,
891 if (!NT_SUCCESS(Status
))
893 DPRINT("Failed to reference token (Status %lx)\n", Status
);
897 /* Check token type */
898 if (Token
->TokenType
!= TokenImpersonation
)
900 DPRINT("No impersonation token\n");
901 ObDereferenceObject(Token
);
902 return STATUS_NO_IMPERSONATION_TOKEN
;
905 /* Check the impersonation level */
906 if (Token
->ImpersonationLevel
< SecurityIdentification
)
908 DPRINT("Impersonation level < SecurityIdentification\n");
909 ObDereferenceObject(Token
);
910 return STATUS_BAD_IMPERSONATION_LEVEL
;
913 /* Capture the security descriptor */
914 Status
= SeCaptureSecurityDescriptor(SecurityDescriptor
,
918 &CapturedSecurityDescriptor
);
919 if (!NT_SUCCESS(Status
))
921 DPRINT("Failed to capture the Security Descriptor\n");
922 ObDereferenceObject(Token
);
926 /* Check the captured security descriptor */
927 if (CapturedSecurityDescriptor
== NULL
)
929 DPRINT("Security Descriptor is NULL\n");
930 ObDereferenceObject(Token
);
931 return STATUS_INVALID_SECURITY_DESCR
;
934 /* Check security descriptor for valid owner and group */
935 if (SepGetSDOwner(SecurityDescriptor
) == NULL
|| // FIXME: use CapturedSecurityDescriptor
936 SepGetSDGroup(SecurityDescriptor
) == NULL
) // FIXME: use CapturedSecurityDescriptor
938 DPRINT("Security Descriptor does not have a valid group or owner\n");
939 SeReleaseSecurityDescriptor(CapturedSecurityDescriptor
,
942 ObDereferenceObject(Token
);
943 return STATUS_INVALID_SECURITY_DESCR
;
946 /* Set up the subject context, and lock it */
947 SubjectSecurityContext
.ClientToken
= Token
;
948 SubjectSecurityContext
.ImpersonationLevel
= Token
->ImpersonationLevel
;
949 SubjectSecurityContext
.PrimaryToken
= NULL
;
950 SubjectSecurityContext
.ProcessAuditId
= NULL
;
951 SeLockSubjectContext(&SubjectSecurityContext
);
953 /* Check if the token is the owner and grant WRITE_DAC and READ_CONTROL rights */
954 if (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
| MAXIMUM_ALLOWED
))
956 if (SepTokenIsOwner(Token
, SecurityDescriptor
)) // FIXME: use CapturedSecurityDescriptor
958 if (DesiredAccess
& MAXIMUM_ALLOWED
)
959 PreviouslyGrantedAccess
|= (WRITE_DAC
| READ_CONTROL
);
961 PreviouslyGrantedAccess
|= (DesiredAccess
& (WRITE_DAC
| READ_CONTROL
));
963 DesiredAccess
&= ~(WRITE_DAC
| READ_CONTROL
);
967 if (DesiredAccess
== 0)
969 *GrantedAccess
= PreviouslyGrantedAccess
;
970 *AccessStatus
= STATUS_SUCCESS
;
974 /* Now perform the access check */
975 SepAccessCheck(SecurityDescriptor
, // FIXME: use CapturedSecurityDescriptor
976 &SubjectSecurityContext
,
978 PreviouslyGrantedAccess
,
979 &PrivilegeSet
, //FIXME
986 /* Unlock subject context */
987 SeUnlockSubjectContext(&SubjectSecurityContext
);
989 /* Release the captured security descriptor */
990 SeReleaseSecurityDescriptor(CapturedSecurityDescriptor
,
994 /* Dereference the token */
995 ObDereferenceObject(Token
);
997 /* Check succeeded */
998 return STATUS_SUCCESS
;
1004 NtAccessCheckByType(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1005 IN PSID PrincipalSelfSid
,
1006 IN HANDLE ClientToken
,
1007 IN ACCESS_MASK DesiredAccess
,
1008 IN POBJECT_TYPE_LIST ObjectTypeList
,
1009 IN ULONG ObjectTypeLength
,
1010 IN PGENERIC_MAPPING GenericMapping
,
1011 IN PPRIVILEGE_SET PrivilegeSet
,
1012 IN ULONG PrivilegeSetLength
,
1013 OUT PACCESS_MASK GrantedAccess
,
1014 OUT PNTSTATUS AccessStatus
)
1017 return STATUS_NOT_IMPLEMENTED
;
1022 NtAccessCheckByTypeAndAuditAlarm(IN PUNICODE_STRING SubsystemName
,
1024 IN PUNICODE_STRING ObjectTypeName
,
1025 IN PUNICODE_STRING ObjectName
,
1026 IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1027 IN PSID PrincipalSelfSid
,
1028 IN ACCESS_MASK DesiredAccess
,
1029 IN AUDIT_EVENT_TYPE AuditType
,
1031 IN POBJECT_TYPE_LIST ObjectTypeList
,
1032 IN ULONG ObjectTypeLength
,
1033 IN PGENERIC_MAPPING GenericMapping
,
1034 IN BOOLEAN ObjectCreation
,
1035 OUT PACCESS_MASK GrantedAccess
,
1036 OUT PNTSTATUS AccessStatus
,
1037 OUT PBOOLEAN GenerateOnClose
)
1040 return STATUS_NOT_IMPLEMENTED
;
1045 NtAccessCheckByTypeResultList(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1046 IN PSID PrincipalSelfSid
,
1047 IN HANDLE ClientToken
,
1048 IN ACCESS_MASK DesiredAccess
,
1049 IN POBJECT_TYPE_LIST ObjectTypeList
,
1050 IN ULONG ObjectTypeLength
,
1051 IN PGENERIC_MAPPING GenericMapping
,
1052 IN PPRIVILEGE_SET PrivilegeSet
,
1053 IN ULONG PrivilegeSetLength
,
1054 OUT PACCESS_MASK GrantedAccess
,
1055 OUT PNTSTATUS AccessStatus
)
1058 return STATUS_NOT_IMPLEMENTED
;
1063 NtAccessCheckByTypeResultListAndAuditAlarm(IN PUNICODE_STRING SubsystemName
,
1065 IN PUNICODE_STRING ObjectTypeName
,
1066 IN PUNICODE_STRING ObjectName
,
1067 IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1068 IN PSID PrincipalSelfSid
,
1069 IN ACCESS_MASK DesiredAccess
,
1070 IN AUDIT_EVENT_TYPE AuditType
,
1072 IN POBJECT_TYPE_LIST ObjectTypeList
,
1073 IN ULONG ObjectTypeLength
,
1074 IN PGENERIC_MAPPING GenericMapping
,
1075 IN BOOLEAN ObjectCreation
,
1076 OUT PACCESS_MASK GrantedAccess
,
1077 OUT PNTSTATUS AccessStatus
,
1078 OUT PBOOLEAN GenerateOnClose
)
1081 return STATUS_NOT_IMPLEMENTED
;
1086 NtAccessCheckByTypeResultListAndAuditAlarmByHandle(IN PUNICODE_STRING SubsystemName
,
1088 IN HANDLE ClientToken
,
1089 IN PUNICODE_STRING ObjectTypeName
,
1090 IN PUNICODE_STRING ObjectName
,
1091 IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
1092 IN PSID PrincipalSelfSid
,
1093 IN ACCESS_MASK DesiredAccess
,
1094 IN AUDIT_EVENT_TYPE AuditType
,
1096 IN POBJECT_TYPE_LIST ObjectTypeList
,
1097 IN ULONG ObjectTypeLength
,
1098 IN PGENERIC_MAPPING GenericMapping
,
1099 IN BOOLEAN ObjectCreation
,
1100 OUT PACCESS_MASK GrantedAccess
,
1101 OUT PNTSTATUS AccessStatus
,
1102 OUT PBOOLEAN GenerateOnClose
)
1105 return STATUS_NOT_IMPLEMENTED
;