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 *****************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/ps.h>
16 #include <internal/se.h>
18 #include <internal/debug.h>
20 #define TAG_SXPT TAG('S', 'X', 'P', 'T')
23 /* GLOBALS ******************************************************************/
25 PSE_EXPORTS EXPORTED SeExports = NULL;
28 /* PROTOTYPES ***************************************************************/
30 static BOOLEAN SepInitExports(VOID);
32 /* FUNCTIONS ****************************************************************/
40 if (!SepInitSecurityIDs())
51 if (!SepInitExports())
61 SepInitializeTokenImplementation();
70 OBJECT_ATTRIBUTES ObjectAttributes;
72 HANDLE DirectoryHandle;
76 /* Create '\Security' directory */
77 RtlInitUnicodeString(&Name,
79 InitializeObjectAttributes(&ObjectAttributes,
84 Status = NtCreateDirectoryObject(&DirectoryHandle,
87 if (!NT_SUCCESS(Status))
89 DPRINT1("Failed to create 'Security' directory!\n");
93 /* Create 'LSA_AUTHENTICATION_INITALIZED' event */
94 RtlInitUnicodeString(&Name,
95 L"\\LSA_AUTHENTICATION_INITALIZED");
96 InitializeObjectAttributes(&ObjectAttributes,
101 Status = NtCreateEvent(&EventHandle,
106 if (!NT_SUCCESS(Status))
108 DPRINT1("Failed to create 'Security' directory!\n");
109 NtClose(DirectoryHandle);
113 NtClose(EventHandle);
114 NtClose(DirectoryHandle);
116 /* FIXME: Create SRM port and listener thread */
125 SeExports = ExAllocatePoolWithTag(NonPagedPool,
128 if (SeExports == NULL)
131 SeExports->SeCreateTokenPrivilege = SeCreateTokenPrivilege;
132 SeExports->SeAssignPrimaryTokenPrivilege = SeAssignPrimaryTokenPrivilege;
133 SeExports->SeLockMemoryPrivilege = SeLockMemoryPrivilege;
134 SeExports->SeIncreaseQuotaPrivilege = SeIncreaseQuotaPrivilege;
135 SeExports->SeUnsolicitedInputPrivilege = SeUnsolicitedInputPrivilege;
136 SeExports->SeTcbPrivilege = SeTcbPrivilege;
137 SeExports->SeSecurityPrivilege = SeSecurityPrivilege;
138 SeExports->SeTakeOwnershipPrivilege = SeTakeOwnershipPrivilege;
139 SeExports->SeLoadDriverPrivilege = SeLoadDriverPrivilege;
140 SeExports->SeCreatePagefilePrivilege = SeCreatePagefilePrivilege;
141 SeExports->SeIncreaseBasePriorityPrivilege = SeIncreaseBasePriorityPrivilege;
142 SeExports->SeSystemProfilePrivilege = SeSystemProfilePrivilege;
143 SeExports->SeSystemtimePrivilege = SeSystemtimePrivilege;
144 SeExports->SeProfileSingleProcessPrivilege = SeProfileSingleProcessPrivilege;
145 SeExports->SeCreatePermanentPrivilege = SeCreatePermanentPrivilege;
146 SeExports->SeBackupPrivilege = SeBackupPrivilege;
147 SeExports->SeRestorePrivilege = SeRestorePrivilege;
148 SeExports->SeShutdownPrivilege = SeShutdownPrivilege;
149 SeExports->SeDebugPrivilege = SeDebugPrivilege;
150 SeExports->SeAuditPrivilege = SeAuditPrivilege;
151 SeExports->SeSystemEnvironmentPrivilege = SeSystemEnvironmentPrivilege;
152 SeExports->SeChangeNotifyPrivilege = SeChangeNotifyPrivilege;
153 SeExports->SeRemoteShutdownPrivilege = SeRemoteShutdownPrivilege;
155 SeExports->SeNullSid = SeNullSid;
156 SeExports->SeWorldSid = SeWorldSid;
157 SeExports->SeLocalSid = SeLocalSid;
158 SeExports->SeCreatorOwnerSid = SeCreatorOwnerSid;
159 SeExports->SeCreatorGroupSid = SeCreatorGroupSid;
160 SeExports->SeNtAuthoritySid = SeNtAuthoritySid;
161 SeExports->SeDialupSid = SeDialupSid;
162 SeExports->SeNetworkSid = SeNetworkSid;
163 SeExports->SeBatchSid = SeBatchSid;
164 SeExports->SeInteractiveSid = SeInteractiveSid;
165 SeExports->SeLocalSystemSid = SeLocalSystemSid;
166 SeExports->SeAliasAdminsSid = SeAliasAdminsSid;
167 SeExports->SeAliasUsersSid = SeAliasUsersSid;
168 SeExports->SeAliasGuestsSid = SeAliasGuestsSid;
169 SeExports->SeAliasPowerUsersSid = SeAliasPowerUsersSid;
170 SeExports->SeAliasAccountOpsSid = SeAliasAccountOpsSid;
171 SeExports->SeAliasSystemOpsSid = SeAliasSystemOpsSid;
172 SeExports->SeAliasPrintOpsSid = SeAliasPrintOpsSid;
173 SeExports->SeAliasBackupOpsSid = SeAliasBackupOpsSid;
179 VOID SepReferenceLogonSession(PLUID AuthenticationId)
184 VOID SepDeReferenceLogonSession(PLUID AuthenticationId)
195 NtAllocateUuids(PULARGE_INTEGER Time,
204 NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
205 IN HANDLE ClientToken,
206 IN ACCESS_MASK DesiredAccess,
207 IN PGENERIC_MAPPING GenericMapping,
208 OUT PPRIVILEGE_SET PrivilegeSet,
209 OUT PULONG ReturnLength,
210 OUT PULONG GrantedAccess,
211 OUT PBOOLEAN AccessStatus)
220 VOID STDCALL SeReleaseSubjectContext (PSECURITY_SUBJECT_CONTEXT SubjectContext)
222 ObDereferenceObject(SubjectContext->PrimaryToken);
223 if (SubjectContext->ClientToken != NULL)
225 ObDereferenceObject(SubjectContext->ClientToken);
232 VOID STDCALL SeCaptureSubjectContext (PSECURITY_SUBJECT_CONTEXT SubjectContext)
238 Process = PsGetCurrentThread()->ThreadsProcess;
240 SubjectContext->ProcessAuditId = Process;
241 SubjectContext->ClientToken =
242 PsReferenceImpersonationToken(PsGetCurrentThread(),
245 &SubjectContext->ImpersonationLevel);
246 SubjectContext->PrimaryToken = PsReferencePrimaryToken(Process);
254 SeDeassignSecurity(PSECURITY_DESCRIPTOR* SecurityDescriptor)
256 if ((*SecurityDescriptor) != NULL)
258 ExFreePool(*SecurityDescriptor);
259 (*SecurityDescriptor) = NULL;
261 return(STATUS_SUCCESS);
266 VOID SepGetDefaultsSubjectContext(PSECURITY_SUBJECT_CONTEXT SubjectContext,
270 PSID* ProcessPrimaryGroup,
275 if (SubjectContext->ClientToken != NULL)
277 Token = SubjectContext->ClientToken;
281 Token = SubjectContext->PrimaryToken;
283 *Owner = Token->UserAndGroups[Token->DefaultOwnerIndex].Sid;
284 *PrimaryGroup = Token->PrimaryGroup;
285 *DefaultDacl = Token->DefaultDacl;
286 *ProcessOwner = SubjectContext->PrimaryToken->
287 UserAndGroups[Token->DefaultOwnerIndex].Sid;
288 *ProcessPrimaryGroup = SubjectContext->PrimaryToken->PrimaryGroup;
291 NTSTATUS SepInheritAcl(PACL Acl,
292 BOOLEAN IsDirectoryObject,
298 PGENERIC_MAPPING GenericMapping)
302 return(STATUS_UNSUCCESSFUL);
304 if (Acl->AclRevision != 2 &&
305 Acl->AclRevision != 3 )
307 return(STATUS_UNSUCCESSFUL);
317 SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor,
318 PSECURITY_DESCRIPTOR ExplicitDescriptor,
319 PSECURITY_DESCRIPTOR* NewDescriptor,
320 BOOLEAN IsDirectoryObject,
321 PSECURITY_SUBJECT_CONTEXT SubjectContext,
322 PGENERIC_MAPPING GenericMapping,
326 PSECURITY_DESCRIPTOR Descriptor;
331 PSID ProcessPrimaryGroup;
334 if (ExplicitDescriptor == NULL)
336 RtlCreateSecurityDescriptor(&Descriptor, 1);
340 Descriptor = ExplicitDescriptor;
342 SeLockSubjectContext(SubjectContext);
343 SepGetDefaultsSubjectContext(SubjectContext,
348 &ProcessPrimaryGroup);
349 if (Descriptor->Control & SE_SACL_PRESENT ||
350 Descriptor->Control & SE_SACL_DEFAULTED)
352 if (ParentDescriptor == NULL)
355 if (Descriptor->Control & SE_SACL_PRESENT ||
356 Descriptor->Sacl == NULL ||)
362 Sacl = Descriptor->Sacl;
363 if (Descriptor->Control & SE_SELF_RELATIVE)
365 Sacl = (PACL)(((PVOID)Sacl) + (PVOID)Descriptor);
381 BOOLEAN SepSidInToken(PACCESS_TOKEN Token,
386 if (Token->UserAndGroupCount == 0)
391 for (i=0; i<Token->UserAndGroupCount; i++)
393 if (RtlEqualSid(Sid, Token->UserAndGroups[i].Sid))
396 (!(Token->UserAndGroups[i].Attributes & SE_GROUP_ENABLED)))
411 SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
412 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext,
413 IN BOOLEAN SubjectContextLocked,
414 IN ACCESS_MASK DesiredAccess,
415 IN ACCESS_MASK PreviouslyGrantedAccess,
416 OUT PPRIVILEGE_SET* Privileges,
417 IN PGENERIC_MAPPING GenericMapping,
418 IN KPROCESSOR_MODE AccessMode,
419 OUT PACCESS_MODE GrantedAccess,
420 OUT PNTSTATUS AccessStatus)
422 * FUNCTION: Determines whether the requested access rights can be granted
423 * to an object protected by a security descriptor and an object owner
425 * SecurityDescriptor = Security descriptor protecting the object
426 * SubjectSecurityContext = Subject's captured security context
427 * SubjectContextLocked = Indicates the user's subject context is locked
428 * DesiredAccess = Access rights the caller is trying to acquire
429 * PreviouslyGrantedAccess = Specified the access rights already granted
431 * GenericMapping = Generic mapping associated with the object
432 * AccessMode = Access mode used for the check
433 * GrantedAccess (OUT) = On return specifies the access granted
434 * AccessStatus (OUT) = Status indicating why access was denied
435 * RETURNS: If access was granted, returns TRUE
445 ACCESS_MASK CurrentAccess;
447 CurrentAccess = PreviouslyGrantedAccess;
450 * Ignore the SACL for now
456 Status = RtlGetDaclSecurityDescriptor(SecurityDescriptor,
460 if (!NT_SUCCESS(Status))
465 CurrentAce = (PACE)(Dacl + 1);
466 for (i = 0; i < Dacl->AceCount; i++)
468 Sid = (PSID)(CurrentAce + 1);
469 if (CurrentAce->Header.AceType == ACCESS_DENIED_ACE_TYPE)
471 if (SepSidInToken(SubjectSecurityContext->ClientToken, Sid))
473 *AccessStatus = STATUS_ACCESS_DENIED;
475 return(STATUS_SUCCESS);
478 if (CurrentAce->Header.AceType == ACCESS_ALLOWED_ACE_TYPE)
480 if (SepSidInToken(SubjectSecurityContext->ClientToken, Sid))
482 CurrentAccess = CurrentAccess |
483 CurrentAce->AccessMask;
487 if (!(CurrentAccess & DesiredAccess) &&
488 !((~CurrentAccess) & DesiredAccess))
490 *AccessStatus = STATUS_ACCESS_DENIED;
494 *AccessStatus = STATUS_SUCCESS;
496 *GrantedAccess = CurrentAccess;
498 return(STATUS_SUCCESS);