1 /* $Id: semgr.c,v 1.40 2004/08/15 16:39:12 chorns Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * PURPOSE: Security manager
6 * FILE: kernel/se/semgr.c
9 * 26/07/98: Added stubs for security functions
12 /* INCLUDES *****************************************************************/
16 #include <internal/debug.h>
18 #define TAG_SXPT TAG('S', 'X', 'P', 'T')
21 /* GLOBALS ******************************************************************/
23 PSE_EXPORTS EXPORTED SeExports
= NULL
;
26 /* PROTOTYPES ***************************************************************/
28 static BOOLEAN
SepInitExports(VOID
);
30 /* FUNCTIONS ****************************************************************/
38 if (!SepInitSecurityIDs())
49 if (!SepInitExports())
59 SepInitializeTokenImplementation();
68 OBJECT_ATTRIBUTES ObjectAttributes
;
70 HANDLE DirectoryHandle
;
74 /* Create '\Security' directory */
75 RtlInitUnicodeString(&Name
,
77 InitializeObjectAttributes(&ObjectAttributes
,
82 Status
= NtCreateDirectoryObject(&DirectoryHandle
,
85 if (!NT_SUCCESS(Status
))
87 DPRINT1("Failed to create 'Security' directory!\n");
91 /* Create 'LSA_AUTHENTICATION_INITALIZED' event */
92 RtlInitUnicodeString(&Name
,
93 L
"\\LSA_AUTHENTICATION_INITALIZED");
94 InitializeObjectAttributes(&ObjectAttributes
,
99 Status
= NtCreateEvent(&EventHandle
,
104 if (!NT_SUCCESS(Status
))
106 DPRINT1("Failed to create 'LSA_AUTHENTICATION_INITALIZED' event!\n");
107 NtClose(DirectoryHandle
);
111 NtClose(EventHandle
);
112 NtClose(DirectoryHandle
);
114 /* FIXME: Create SRM port and listener thread */
120 static BOOLEAN INIT_FUNCTION
123 SeExports
= ExAllocatePoolWithTag(NonPagedPool
,
126 if (SeExports
== NULL
)
129 SeExports
->SeCreateTokenPrivilege
= SeCreateTokenPrivilege
;
130 SeExports
->SeAssignPrimaryTokenPrivilege
= SeAssignPrimaryTokenPrivilege
;
131 SeExports
->SeLockMemoryPrivilege
= SeLockMemoryPrivilege
;
132 SeExports
->SeIncreaseQuotaPrivilege
= SeIncreaseQuotaPrivilege
;
133 SeExports
->SeUnsolicitedInputPrivilege
= SeUnsolicitedInputPrivilege
;
134 SeExports
->SeTcbPrivilege
= SeTcbPrivilege
;
135 SeExports
->SeSecurityPrivilege
= SeSecurityPrivilege
;
136 SeExports
->SeTakeOwnershipPrivilege
= SeTakeOwnershipPrivilege
;
137 SeExports
->SeLoadDriverPrivilege
= SeLoadDriverPrivilege
;
138 SeExports
->SeCreatePagefilePrivilege
= SeCreatePagefilePrivilege
;
139 SeExports
->SeIncreaseBasePriorityPrivilege
= SeIncreaseBasePriorityPrivilege
;
140 SeExports
->SeSystemProfilePrivilege
= SeSystemProfilePrivilege
;
141 SeExports
->SeSystemtimePrivilege
= SeSystemtimePrivilege
;
142 SeExports
->SeProfileSingleProcessPrivilege
= SeProfileSingleProcessPrivilege
;
143 SeExports
->SeCreatePermanentPrivilege
= SeCreatePermanentPrivilege
;
144 SeExports
->SeBackupPrivilege
= SeBackupPrivilege
;
145 SeExports
->SeRestorePrivilege
= SeRestorePrivilege
;
146 SeExports
->SeShutdownPrivilege
= SeShutdownPrivilege
;
147 SeExports
->SeDebugPrivilege
= SeDebugPrivilege
;
148 SeExports
->SeAuditPrivilege
= SeAuditPrivilege
;
149 SeExports
->SeSystemEnvironmentPrivilege
= SeSystemEnvironmentPrivilege
;
150 SeExports
->SeChangeNotifyPrivilege
= SeChangeNotifyPrivilege
;
151 SeExports
->SeRemoteShutdownPrivilege
= SeRemoteShutdownPrivilege
;
153 SeExports
->SeNullSid
= SeNullSid
;
154 SeExports
->SeWorldSid
= SeWorldSid
;
155 SeExports
->SeLocalSid
= SeLocalSid
;
156 SeExports
->SeCreatorOwnerSid
= SeCreatorOwnerSid
;
157 SeExports
->SeCreatorGroupSid
= SeCreatorGroupSid
;
158 SeExports
->SeNtAuthoritySid
= SeNtAuthoritySid
;
159 SeExports
->SeDialupSid
= SeDialupSid
;
160 SeExports
->SeNetworkSid
= SeNetworkSid
;
161 SeExports
->SeBatchSid
= SeBatchSid
;
162 SeExports
->SeInteractiveSid
= SeInteractiveSid
;
163 SeExports
->SeLocalSystemSid
= SeLocalSystemSid
;
164 SeExports
->SeAliasAdminsSid
= SeAliasAdminsSid
;
165 SeExports
->SeAliasUsersSid
= SeAliasUsersSid
;
166 SeExports
->SeAliasGuestsSid
= SeAliasGuestsSid
;
167 SeExports
->SeAliasPowerUsersSid
= SeAliasPowerUsersSid
;
168 SeExports
->SeAliasAccountOpsSid
= SeAliasAccountOpsSid
;
169 SeExports
->SeAliasSystemOpsSid
= SeAliasSystemOpsSid
;
170 SeExports
->SeAliasPrintOpsSid
= SeAliasPrintOpsSid
;
171 SeExports
->SeAliasBackupOpsSid
= SeAliasBackupOpsSid
;
177 VOID
SepReferenceLogonSession(PLUID AuthenticationId
)
182 VOID
SepDeReferenceLogonSession(PLUID AuthenticationId
)
193 NtAllocateUuids(PULARGE_INTEGER Time
,
198 return(STATUS_NOT_IMPLEMENTED
);
206 SeCaptureSubjectContext(OUT PSECURITY_SUBJECT_CONTEXT SubjectContext
)
210 BOOLEAN EffectiveOnly
;
212 Thread
= PsGetCurrentThread();
215 SubjectContext
->ProcessAuditId
= 0;
216 SubjectContext
->PrimaryToken
= NULL
;
217 SubjectContext
->ClientToken
= NULL
;
218 SubjectContext
->ImpersonationLevel
= 0;
222 SubjectContext
->ProcessAuditId
= Thread
->ThreadsProcess
;
223 SubjectContext
->ClientToken
=
224 PsReferenceImpersonationToken(Thread
,
227 &SubjectContext
->ImpersonationLevel
);
228 SubjectContext
->PrimaryToken
= PsReferencePrimaryToken(Thread
->ThreadsProcess
);
237 SeLockSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext
)
247 SeReleaseSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext
)
249 if (SubjectContext
->PrimaryToken
!= NULL
)
251 ObDereferenceObject(SubjectContext
->PrimaryToken
);
254 if (SubjectContext
->ClientToken
!= NULL
)
256 ObDereferenceObject(SubjectContext
->ClientToken
);
265 SeUnlockSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext
)
275 SeDeassignSecurity(PSECURITY_DESCRIPTOR
*SecurityDescriptor
)
277 if (*SecurityDescriptor
!= NULL
)
279 ExFreePool(*SecurityDescriptor
);
280 *SecurityDescriptor
= NULL
;
283 return STATUS_SUCCESS
;
292 IN PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL
,
293 IN PSECURITY_DESCRIPTOR ExplicitDescriptor OPTIONAL
,
294 OUT PSECURITY_DESCRIPTOR
*NewDescriptor
,
295 IN GUID
*ObjectType OPTIONAL
,
296 IN BOOLEAN IsDirectoryObject
,
297 IN ULONG AutoInheritFlags
,
298 IN PSECURITY_SUBJECT_CONTEXT SubjectContext
,
299 IN PGENERIC_MAPPING GenericMapping
,
300 IN POOL_TYPE PoolType
304 return STATUS_NOT_IMPLEMENTED
;
308 * FUNCTION: Creates a security descriptor for a new object.
311 * ExplicitDescriptor =
313 * IsDirectoryObject =
322 SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL
,
323 PSECURITY_DESCRIPTOR ExplicitDescriptor OPTIONAL
,
324 PSECURITY_DESCRIPTOR
*NewDescriptor
,
325 BOOLEAN IsDirectoryObject
,
326 PSECURITY_SUBJECT_CONTEXT SubjectContext
,
327 PGENERIC_MAPPING GenericMapping
,
330 PSECURITY_DESCRIPTOR Descriptor
;
332 ULONG OwnerLength
= 0;
333 ULONG GroupLength
= 0;
334 ULONG DaclLength
= 0;
335 ULONG SaclLength
= 0;
344 /* FIXME: Lock subject context */
346 if (SubjectContext
->ClientToken
!= NULL
)
348 Token
= SubjectContext
->ClientToken
;
352 Token
= SubjectContext
->PrimaryToken
;
356 /* Inherit the Owner SID */
357 if (ExplicitDescriptor
!= NULL
&& ExplicitDescriptor
->Owner
!= NULL
)
359 DPRINT("Use explicit owner sid!\n");
360 Owner
= ExplicitDescriptor
->Owner
;
361 if (ExplicitDescriptor
->Control
& SE_SELF_RELATIVE
)
363 Owner
= (PSID
)(((ULONG_PTR
)Owner
) + (ULONG_PTR
)ExplicitDescriptor
);
370 DPRINT("Use token owner sid!\n");
371 Owner
= Token
->UserAndGroups
[Token
->DefaultOwnerIndex
].Sid
;
375 DPRINT("Use default owner sid!\n");
376 Owner
= SeLocalSystemSid
;
379 Control
|= SE_OWNER_DEFAULTED
;
382 OwnerLength
= ROUND_UP(RtlLengthSid(Owner
), 4);
385 /* Inherit the Group SID */
386 if (ExplicitDescriptor
!= NULL
&& ExplicitDescriptor
->Group
!= NULL
)
388 DPRINT("Use explicit group sid!\n");
389 Group
= ExplicitDescriptor
->Group
;
390 if (ExplicitDescriptor
->Control
& SE_SELF_RELATIVE
)
392 Group
= (PSID
)(((ULONG_PTR
)Group
) + (ULONG_PTR
)ExplicitDescriptor
);
399 DPRINT("Use token group sid!\n");
400 Group
= Token
->PrimaryGroup
;
404 DPRINT("Use default group sid!\n");
405 Group
= SeLocalSystemSid
;
408 Control
|= SE_OWNER_DEFAULTED
;
411 GroupLength
= ROUND_UP(RtlLengthSid(Group
), 4);
414 /* Inherit the DACL */
415 if (ExplicitDescriptor
!= NULL
&&
416 (ExplicitDescriptor
->Control
& SE_DACL_PRESENT
) &&
417 !(ExplicitDescriptor
->Control
& SE_DACL_DEFAULTED
))
419 DPRINT("Use explicit DACL!\n");
420 Dacl
= ExplicitDescriptor
->Dacl
;
421 if (Dacl
!= NULL
&& (ExplicitDescriptor
->Control
& SE_SELF_RELATIVE
))
423 Dacl
= (PACL
)(((ULONG_PTR
)Dacl
) + (ULONG_PTR
)ExplicitDescriptor
);
426 Control
|= SE_DACL_PRESENT
;
428 else if (ParentDescriptor
!= NULL
&&
429 (ParentDescriptor
->Control
& SE_DACL_PRESENT
))
431 DPRINT("Use parent DACL!\n");
433 Dacl
= ParentDescriptor
->Dacl
;
434 if (Dacl
!= NULL
&& (ParentDescriptor
->Control
& SE_SELF_RELATIVE
))
436 Dacl
= (PACL
)(((ULONG_PTR
)Dacl
) + (ULONG_PTR
)ParentDescriptor
);
438 Control
|= (SE_DACL_PRESENT
& SE_DACL_DEFAULTED
);
440 else if (Token
!= NULL
&& Token
->DefaultDacl
!= NULL
)
442 DPRINT("Use token default DACL!\n");
444 Dacl
= Token
->DefaultDacl
;
445 Control
|= (SE_DACL_PRESENT
& SE_DACL_DEFAULTED
);
449 DPRINT("Use NULL DACL!\n");
451 Control
|= (SE_DACL_PRESENT
& SE_DACL_DEFAULTED
);
454 DaclLength
= (Dacl
!= NULL
) ? ROUND_UP(Dacl
->AclSize
, 4) : 0;
457 /* Inherit the SACL */
458 if (ExplicitDescriptor
!= NULL
&&
459 (ExplicitDescriptor
->Control
& SE_SACL_PRESENT
) &&
460 !(ExplicitDescriptor
->Control
& SE_SACL_DEFAULTED
))
462 DPRINT("Use explicit SACL!\n");
463 Sacl
= ExplicitDescriptor
->Sacl
;
464 if (Sacl
!= NULL
&& (ExplicitDescriptor
->Control
& SE_SELF_RELATIVE
))
466 Sacl
= (PACL
)(((ULONG_PTR
)Sacl
) + (ULONG_PTR
)ExplicitDescriptor
);
469 Control
|= SE_SACL_PRESENT
;
471 else if (ParentDescriptor
!= NULL
&&
472 (ParentDescriptor
->Control
& SE_SACL_PRESENT
))
474 DPRINT("Use parent SACL!\n");
476 Sacl
= ParentDescriptor
->Sacl
;
477 if (Sacl
!= NULL
&& (ParentDescriptor
->Control
& SE_SELF_RELATIVE
))
479 Sacl
= (PACL
)(((ULONG_PTR
)Sacl
) + (ULONG_PTR
)ParentDescriptor
);
481 Control
|= (SE_SACL_PRESENT
& SE_SACL_DEFAULTED
);
484 SaclLength
= (Sacl
!= NULL
) ? ROUND_UP(Sacl
->AclSize
, 4) : 0;
487 /* Allocate and initialize the new security descriptor */
488 Length
= sizeof(SECURITY_DESCRIPTOR
) + OwnerLength
+ GroupLength
+ DaclLength
+ SaclLength
;
490 Descriptor
= ExAllocatePool(NonPagedPool
,
492 if (Descriptor
== NULL
)
494 DPRINT1("ExAlloctePool() failed\n");
495 /* FIXME: Unlock subject context */
496 return STATUS_INSUFFICIENT_RESOURCES
;
499 RtlCreateSecurityDescriptor(Descriptor
,
500 SECURITY_DESCRIPTOR_REVISION
);
502 Descriptor
->Control
= Control
| SE_SELF_RELATIVE
;
504 Current
= (ULONG
)Descriptor
+ sizeof(SECURITY_DESCRIPTOR
);
508 RtlCopyMemory((PVOID
)Current
,
511 Descriptor
->Sacl
= (PACL
)((ULONG
)Current
- (ULONG
)Descriptor
);
512 Current
+= SaclLength
;
517 RtlCopyMemory((PVOID
)Current
,
520 Descriptor
->Dacl
= (PACL
)((ULONG
)Current
- (ULONG
)Descriptor
);
521 Current
+= DaclLength
;
524 if (OwnerLength
!= 0)
526 RtlCopyMemory((PVOID
)Current
,
529 Descriptor
->Owner
= (PSID
)((ULONG
)Current
- (ULONG
)Descriptor
);
530 Current
+= OwnerLength
;
533 if (GroupLength
!= 0)
535 memmove((PVOID
)Current
,
538 Descriptor
->Group
= (PSID
)((ULONG
)Current
- (ULONG
)Descriptor
);
541 /* FIXME: Unlock subject context */
543 *NewDescriptor
= Descriptor
;
545 return STATUS_SUCCESS
;
550 SepSidInToken(PACCESS_TOKEN Token
,
555 if (Token
->UserAndGroupCount
== 0)
560 for (i
=0; i
<Token
->UserAndGroupCount
; i
++)
562 if (RtlEqualSid(Sid
, Token
->UserAndGroups
[i
].Sid
))
564 if (Token
->UserAndGroups
[i
].Attributes
& SE_GROUP_ENABLED
)
578 * FUNCTION: Determines whether the requested access rights can be granted
579 * to an object protected by a security descriptor and an object owner
581 * SecurityDescriptor = Security descriptor protecting the object
582 * SubjectSecurityContext = Subject's captured security context
583 * SubjectContextLocked = Indicates the user's subject context is locked
584 * DesiredAccess = Access rights the caller is trying to acquire
585 * PreviouslyGrantedAccess = Specified the access rights already granted
587 * GenericMapping = Generic mapping associated with the object
588 * AccessMode = Access mode used for the check
589 * GrantedAccess (OUT) = On return specifies the access granted
590 * AccessStatus (OUT) = Status indicating why access was denied
591 * RETURNS: If access was granted, returns TRUE
596 SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
597 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext
,
598 IN BOOLEAN SubjectContextLocked
,
599 IN ACCESS_MASK DesiredAccess
,
600 IN ACCESS_MASK PreviouslyGrantedAccess
,
601 OUT PPRIVILEGE_SET
* Privileges
,
602 IN PGENERIC_MAPPING GenericMapping
,
603 IN KPROCESSOR_MODE AccessMode
,
604 OUT PACCESS_MASK GrantedAccess
,
605 OUT PNTSTATUS AccessStatus
)
607 LUID_AND_ATTRIBUTES Privilege
;
608 ACCESS_MASK CurrentAccess
;
618 CurrentAccess
= PreviouslyGrantedAccess
;
620 Token
= SubjectSecurityContext
->ClientToken
?
621 SubjectSecurityContext
->ClientToken
: SubjectSecurityContext
->PrimaryToken
;
624 Status
= RtlGetDaclSecurityDescriptor(SecurityDescriptor
,
628 if (!NT_SUCCESS(Status
))
630 *AccessStatus
= Status
;
634 /* RULE 1: Grant desired access if the object is unprotected */
637 *GrantedAccess
= DesiredAccess
;
638 *AccessStatus
= STATUS_SUCCESS
;
642 CurrentAccess
= PreviouslyGrantedAccess
;
644 /* RULE 2: Check token for 'take ownership' privilege */
645 Privilege
.Luid
= SeTakeOwnershipPrivilege
;
646 Privilege
.Attributes
= SE_PRIVILEGE_ENABLED
;
648 if (SepPrivilegeCheck(Token
,
651 PRIVILEGE_SET_ALL_NECESSARY
,
654 CurrentAccess
|= WRITE_OWNER
;
655 if (DesiredAccess
== CurrentAccess
)
657 *GrantedAccess
= CurrentAccess
;
658 *AccessStatus
= STATUS_SUCCESS
;
663 /* RULE 3: Check whether the token is the owner */
664 Status
= RtlGetOwnerSecurityDescriptor(SecurityDescriptor
,
667 if (!NT_SUCCESS(Status
))
669 DPRINT1("RtlGetOwnerSecurityDescriptor() failed (Status %lx)\n", Status
);
670 *AccessStatus
= Status
;
674 if (SepSidInToken(Token
, Sid
))
676 CurrentAccess
|= (READ_CONTROL
| WRITE_DAC
);
677 if (DesiredAccess
== CurrentAccess
)
679 *GrantedAccess
= CurrentAccess
;
680 *AccessStatus
= STATUS_SUCCESS
;
685 /* RULE 4: Grant rights according to the DACL */
686 CurrentAce
= (PACE
)(Dacl
+ 1);
687 for (i
= 0; i
< Dacl
->AceCount
; i
++)
689 Sid
= (PSID
)(CurrentAce
+ 1);
690 if (CurrentAce
->Header
.AceType
== ACCESS_DENIED_ACE_TYPE
)
692 if (SepSidInToken(Token
, Sid
))
695 *AccessStatus
= STATUS_ACCESS_DENIED
;
700 if (CurrentAce
->Header
.AceType
== ACCESS_ALLOWED_ACE_TYPE
)
702 if (SepSidInToken(Token
, Sid
))
704 CurrentAccess
|= CurrentAce
->AccessMask
;
709 DPRINT("CurrentAccess %08lx\n DesiredAccess %08lx\n",
710 CurrentAccess
, DesiredAccess
);
712 *GrantedAccess
= CurrentAccess
& DesiredAccess
;
715 (*GrantedAccess
== DesiredAccess
) ? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
722 NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
723 IN HANDLE TokenHandle
,
724 IN ACCESS_MASK DesiredAccess
,
725 IN PGENERIC_MAPPING GenericMapping
,
726 OUT PPRIVILEGE_SET PrivilegeSet
,
727 OUT PULONG ReturnLength
,
728 OUT PACCESS_MASK GrantedAccess
,
729 OUT PNTSTATUS AccessStatus
)
731 SECURITY_SUBJECT_CONTEXT SubjectSecurityContext
;
732 KPROCESSOR_MODE PreviousMode
;
736 DPRINT("NtAccessCheck() called\n");
738 PreviousMode
= KeGetPreviousMode();
739 if (PreviousMode
== KernelMode
)
741 *GrantedAccess
= DesiredAccess
;
742 *AccessStatus
= STATUS_SUCCESS
;
743 return STATUS_SUCCESS
;
746 Status
= ObReferenceObjectByHandle(TokenHandle
,
752 if (!NT_SUCCESS(Status
))
754 DPRINT1("Failed to reference token (Status %lx)\n", Status
);
758 /* Check token type */
759 if (Token
->TokenType
!= TokenImpersonation
)
761 DPRINT1("No impersonation token\n");
762 ObDereferenceObject(Token
);
763 return STATUS_ACCESS_VIOLATION
;
766 /* Check impersonation level */
767 if (Token
->ImpersonationLevel
< SecurityAnonymous
)
769 DPRINT1("Invalid impersonation level\n");
770 ObDereferenceObject(Token
);
771 return STATUS_ACCESS_VIOLATION
;
774 RtlZeroMemory(&SubjectSecurityContext
,
775 sizeof(SECURITY_SUBJECT_CONTEXT
));
776 SubjectSecurityContext
.ClientToken
= Token
;
777 SubjectSecurityContext
.ImpersonationLevel
= Token
->ImpersonationLevel
;
779 /* FIXME: Lock subject context */
781 if (!SeAccessCheck(SecurityDescriptor
,
782 &SubjectSecurityContext
,
792 Status
= *AccessStatus
;
796 Status
= STATUS_ACCESS_DENIED
;
799 /* FIXME: Unlock subject context */
801 ObDereferenceObject(Token
);
803 DPRINT("NtAccessCheck() done\n");