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 ****************************************************************/
39 if (!SepInitSecurityIDs())
45 #endif /* LIBCAPTIVE */
52 if (!SepInitExports())
62 SepInitializeTokenImplementation();
71 OBJECT_ATTRIBUTES ObjectAttributes;
73 HANDLE DirectoryHandle;
76 #endif /* LIBCAPTIVE */
79 /* Create '\Security' directory */
80 RtlInitUnicodeString(&Name,
81 REACTOS_UCS2(L"\\Security"));
82 InitializeObjectAttributes(&ObjectAttributes,
87 Status = NtCreateDirectoryObject(&DirectoryHandle,
90 if (!NT_SUCCESS(Status))
92 DPRINT1("Failed to create 'Security' directory!\n");
97 /* Create 'LSA_AUTHENTICATION_INITALIZED' event */
98 RtlInitUnicodeString(&Name,
99 REACTOS_UCS2(L"\\LSA_AUTHENTICATION_INITALIZED"));
100 InitializeObjectAttributes(&ObjectAttributes,
105 Status = NtCreateEvent(&EventHandle,
110 if (!NT_SUCCESS(Status))
112 DPRINT1("Failed to create 'Security' directory!\n");
113 NtClose(DirectoryHandle);
117 NtClose(EventHandle);
118 NtClose(DirectoryHandle);
119 #endif /* LIBCAPTIVE */
121 /* FIXME: Create SRM port and listener thread */
130 SeExports = ExAllocatePoolWithTag(NonPagedPool,
133 if (SeExports == NULL)
136 SeExports->SeCreateTokenPrivilege = SeCreateTokenPrivilege;
137 SeExports->SeAssignPrimaryTokenPrivilege = SeAssignPrimaryTokenPrivilege;
138 SeExports->SeLockMemoryPrivilege = SeLockMemoryPrivilege;
139 SeExports->SeIncreaseQuotaPrivilege = SeIncreaseQuotaPrivilege;
140 SeExports->SeUnsolicitedInputPrivilege = SeUnsolicitedInputPrivilege;
141 SeExports->SeTcbPrivilege = SeTcbPrivilege;
142 SeExports->SeSecurityPrivilege = SeSecurityPrivilege;
143 SeExports->SeTakeOwnershipPrivilege = SeTakeOwnershipPrivilege;
144 SeExports->SeLoadDriverPrivilege = SeLoadDriverPrivilege;
145 SeExports->SeCreatePagefilePrivilege = SeCreatePagefilePrivilege;
146 SeExports->SeIncreaseBasePriorityPrivilege = SeIncreaseBasePriorityPrivilege;
147 SeExports->SeSystemProfilePrivilege = SeSystemProfilePrivilege;
148 SeExports->SeSystemtimePrivilege = SeSystemtimePrivilege;
149 SeExports->SeProfileSingleProcessPrivilege = SeProfileSingleProcessPrivilege;
150 SeExports->SeCreatePermanentPrivilege = SeCreatePermanentPrivilege;
151 SeExports->SeBackupPrivilege = SeBackupPrivilege;
152 SeExports->SeRestorePrivilege = SeRestorePrivilege;
153 SeExports->SeShutdownPrivilege = SeShutdownPrivilege;
154 SeExports->SeDebugPrivilege = SeDebugPrivilege;
155 SeExports->SeAuditPrivilege = SeAuditPrivilege;
156 SeExports->SeSystemEnvironmentPrivilege = SeSystemEnvironmentPrivilege;
157 SeExports->SeChangeNotifyPrivilege = SeChangeNotifyPrivilege;
158 SeExports->SeRemoteShutdownPrivilege = SeRemoteShutdownPrivilege;
160 SeExports->SeNullSid = SeNullSid;
161 SeExports->SeWorldSid = SeWorldSid;
162 SeExports->SeLocalSid = SeLocalSid;
163 SeExports->SeCreatorOwnerSid = SeCreatorOwnerSid;
164 SeExports->SeCreatorGroupSid = SeCreatorGroupSid;
165 SeExports->SeNtAuthoritySid = SeNtAuthoritySid;
166 SeExports->SeDialupSid = SeDialupSid;
167 SeExports->SeNetworkSid = SeNetworkSid;
168 SeExports->SeBatchSid = SeBatchSid;
169 SeExports->SeInteractiveSid = SeInteractiveSid;
170 SeExports->SeLocalSystemSid = SeLocalSystemSid;
171 SeExports->SeAliasAdminsSid = SeAliasAdminsSid;
172 SeExports->SeAliasUsersSid = SeAliasUsersSid;
173 SeExports->SeAliasGuestsSid = SeAliasGuestsSid;
174 SeExports->SeAliasPowerUsersSid = SeAliasPowerUsersSid;
175 SeExports->SeAliasAccountOpsSid = SeAliasAccountOpsSid;
176 SeExports->SeAliasSystemOpsSid = SeAliasSystemOpsSid;
177 SeExports->SeAliasPrintOpsSid = SeAliasPrintOpsSid;
178 SeExports->SeAliasBackupOpsSid = SeAliasBackupOpsSid;
185 VOID SepReferenceLogonSession(PLUID AuthenticationId)
190 VOID SepDeReferenceLogonSession(PLUID AuthenticationId)
201 NtAllocateUuids(PULARGE_INTEGER Time,
210 NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
211 IN HANDLE ClientToken,
212 IN ACCESS_MASK DesiredAccess,
213 IN PGENERIC_MAPPING GenericMapping,
214 OUT PPRIVILEGE_SET PrivilegeSet,
215 OUT PULONG ReturnLength,
216 OUT PULONG GrantedAccess,
217 OUT PBOOLEAN AccessStatus)
224 NtDeleteObjectAuditAlarm(IN PUNICODE_STRING SubsystemName,
226 IN BOOLEAN GenerateOnClose)
232 #endif /* LIBCAPTIVE */
237 VOID STDCALL SeReleaseSubjectContext (PSECURITY_SUBJECT_CONTEXT SubjectContext)
239 ObDereferenceObject(SubjectContext->PrimaryToken);
240 if (SubjectContext->ClientToken != NULL)
242 ObDereferenceObject(SubjectContext->ClientToken);
249 VOID STDCALL SeCaptureSubjectContext (PSECURITY_SUBJECT_CONTEXT SubjectContext)
255 Process = PsGetCurrentThread()->ThreadsProcess;
257 SubjectContext->ProcessAuditId = Process;
258 SubjectContext->ClientToken =
259 PsReferenceImpersonationToken(PsGetCurrentThread(),
262 &SubjectContext->ImpersonationLevel);
263 SubjectContext->PrimaryToken = PsReferencePrimaryToken(Process);
271 SeDeassignSecurity(PSECURITY_DESCRIPTOR* SecurityDescriptor)
273 if ((*SecurityDescriptor) != NULL)
275 ExFreePool(*SecurityDescriptor);
276 (*SecurityDescriptor) = NULL;
278 return(STATUS_SUCCESS);
284 VOID SepGetDefaultsSubjectContext(PSECURITY_SUBJECT_CONTEXT SubjectContext,
288 PSID* ProcessPrimaryGroup,
293 if (SubjectContext->ClientToken != NULL)
295 Token = SubjectContext->ClientToken;
299 Token = SubjectContext->PrimaryToken;
301 *Owner = Token->UserAndGroups[Token->DefaultOwnerIndex].Sid;
302 *PrimaryGroup = Token->PrimaryGroup;
303 *DefaultDacl = Token->DefaultDacl;
304 *ProcessOwner = SubjectContext->PrimaryToken->
305 UserAndGroups[Token->DefaultOwnerIndex].Sid;
306 *ProcessPrimaryGroup = SubjectContext->PrimaryToken->PrimaryGroup;
309 NTSTATUS SepInheritAcl(PACL Acl,
310 BOOLEAN IsDirectoryObject,
316 PGENERIC_MAPPING GenericMapping)
320 return(STATUS_UNSUCCESSFUL);
322 if (Acl->AclRevision != 2 &&
323 Acl->AclRevision != 3 )
325 return(STATUS_UNSUCCESSFUL);
335 SeAssignSecurity(PSECURITY_DESCRIPTOR ParentDescriptor OPTIONAL,
336 PSECURITY_DESCRIPTOR ExplicitDescriptor OPTIONAL,
337 PSECURITY_DESCRIPTOR* NewDescriptor,
338 BOOLEAN IsDirectoryObject,
339 PSECURITY_SUBJECT_CONTEXT SubjectContext,
340 PGENERIC_MAPPING GenericMapping,
344 PSECURITY_DESCRIPTOR Descriptor;
349 PSID ProcessPrimaryGroup;
352 if (ExplicitDescriptor == NULL)
354 RtlCreateSecurityDescriptor(&Descriptor, 1);
358 Descriptor = ExplicitDescriptor;
360 SeLockSubjectContext(SubjectContext);
361 SepGetDefaultsSubjectContext(SubjectContext,
366 &ProcessPrimaryGroup);
367 if (Descriptor->Control & SE_SACL_PRESENT ||
368 Descriptor->Control & SE_SACL_DEFAULTED)
370 if (ParentDescriptor == NULL)
373 if (Descriptor->Control & SE_SACL_PRESENT ||
374 Descriptor->Sacl == NULL ||)
380 Sacl = Descriptor->Sacl;
381 if (Descriptor->Control & SE_SELF_RELATIVE)
383 Sacl = (PACL)(((PVOID)Sacl) + (PVOID)Descriptor);
399 BOOLEAN SepSidInToken(PACCESS_TOKEN Token,
404 if (Token->UserAndGroupCount == 0)
409 for (i=0; i<Token->UserAndGroupCount; i++)
411 if (RtlEqualSid(Sid, Token->UserAndGroups[i].Sid))
414 (!(Token->UserAndGroups[i].Attributes & SE_GROUP_ENABLED)))
429 SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
430 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext,
431 IN BOOLEAN SubjectContextLocked,
432 IN ACCESS_MASK DesiredAccess,
433 IN ACCESS_MASK PreviouslyGrantedAccess,
434 OUT PPRIVILEGE_SET* Privileges,
435 IN PGENERIC_MAPPING GenericMapping,
436 IN KPROCESSOR_MODE AccessMode,
437 OUT PACCESS_MODE GrantedAccess,
438 OUT PNTSTATUS AccessStatus)
440 * FUNCTION: Determines whether the requested access rights can be granted
441 * to an object protected by a security descriptor and an object owner
443 * SecurityDescriptor = Security descriptor protecting the object
444 * SubjectSecurityContext = Subject's captured security context
445 * SubjectContextLocked = Indicates the user's subject context is locked
446 * DesiredAccess = Access rights the caller is trying to acquire
447 * PreviouslyGrantedAccess = Specified the access rights already granted
449 * GenericMapping = Generic mapping associated with the object
450 * AccessMode = Access mode used for the check
451 * GrantedAccess (OUT) = On return specifies the access granted
452 * AccessStatus (OUT) = Status indicating why access was denied
453 * RETURNS: If access was granted, returns TRUE
463 ACCESS_MASK CurrentAccess;
465 CurrentAccess = PreviouslyGrantedAccess;
468 * Ignore the SACL for now
474 Status = RtlGetDaclSecurityDescriptor(SecurityDescriptor,
478 if (!NT_SUCCESS(Status))
483 CurrentAce = (PACE)(Dacl + 1);
484 for (i = 0; i < Dacl->AceCount; i++)
486 Sid = (PSID)(CurrentAce + 1);
487 if (CurrentAce->Header.AceType == ACCESS_DENIED_ACE_TYPE)
489 if (SepSidInToken(SubjectSecurityContext->ClientToken, Sid))
491 *AccessStatus = STATUS_ACCESS_DENIED;
493 return(STATUS_SUCCESS);
496 if (CurrentAce->Header.AceType == ACCESS_ALLOWED_ACE_TYPE)
498 if (SepSidInToken(SubjectSecurityContext->ClientToken, Sid))
500 CurrentAccess = CurrentAccess |
501 CurrentAce->AccessMask;
505 if (!(CurrentAccess & DesiredAccess) &&
506 !((~CurrentAccess) & DesiredAccess))
508 *AccessStatus = STATUS_ACCESS_DENIED;
512 *AccessStatus = STATUS_SUCCESS;
514 *GrantedAccess = CurrentAccess;
516 return(STATUS_SUCCESS);
519 #endif /* LIBCAPTIVE */