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
;
20 ULONG SidInTokenCalls
= 0;
22 extern ULONG ExpInitializationPhase
;
23 extern ERESOURCE SepSubjectContextLock
;
25 /* PRIVATE FUNCTIONS **********************************************************/
31 SepExports
.SeCreateTokenPrivilege
= SeCreateTokenPrivilege
;
32 SepExports
.SeAssignPrimaryTokenPrivilege
= SeAssignPrimaryTokenPrivilege
;
33 SepExports
.SeLockMemoryPrivilege
= SeLockMemoryPrivilege
;
34 SepExports
.SeIncreaseQuotaPrivilege
= SeIncreaseQuotaPrivilege
;
35 SepExports
.SeUnsolicitedInputPrivilege
= SeUnsolicitedInputPrivilege
;
36 SepExports
.SeTcbPrivilege
= SeTcbPrivilege
;
37 SepExports
.SeSecurityPrivilege
= SeSecurityPrivilege
;
38 SepExports
.SeTakeOwnershipPrivilege
= SeTakeOwnershipPrivilege
;
39 SepExports
.SeLoadDriverPrivilege
= SeLoadDriverPrivilege
;
40 SepExports
.SeCreatePagefilePrivilege
= SeCreatePagefilePrivilege
;
41 SepExports
.SeIncreaseBasePriorityPrivilege
= SeIncreaseBasePriorityPrivilege
;
42 SepExports
.SeSystemProfilePrivilege
= SeSystemProfilePrivilege
;
43 SepExports
.SeSystemtimePrivilege
= SeSystemtimePrivilege
;
44 SepExports
.SeProfileSingleProcessPrivilege
= SeProfileSingleProcessPrivilege
;
45 SepExports
.SeCreatePermanentPrivilege
= SeCreatePermanentPrivilege
;
46 SepExports
.SeBackupPrivilege
= SeBackupPrivilege
;
47 SepExports
.SeRestorePrivilege
= SeRestorePrivilege
;
48 SepExports
.SeShutdownPrivilege
= SeShutdownPrivilege
;
49 SepExports
.SeDebugPrivilege
= SeDebugPrivilege
;
50 SepExports
.SeAuditPrivilege
= SeAuditPrivilege
;
51 SepExports
.SeSystemEnvironmentPrivilege
= SeSystemEnvironmentPrivilege
;
52 SepExports
.SeChangeNotifyPrivilege
= SeChangeNotifyPrivilege
;
53 SepExports
.SeRemoteShutdownPrivilege
= SeRemoteShutdownPrivilege
;
55 SepExports
.SeNullSid
= SeNullSid
;
56 SepExports
.SeWorldSid
= SeWorldSid
;
57 SepExports
.SeLocalSid
= SeLocalSid
;
58 SepExports
.SeCreatorOwnerSid
= SeCreatorOwnerSid
;
59 SepExports
.SeCreatorGroupSid
= SeCreatorGroupSid
;
60 SepExports
.SeNtAuthoritySid
= SeNtAuthoritySid
;
61 SepExports
.SeDialupSid
= SeDialupSid
;
62 SepExports
.SeNetworkSid
= SeNetworkSid
;
63 SepExports
.SeBatchSid
= SeBatchSid
;
64 SepExports
.SeInteractiveSid
= SeInteractiveSid
;
65 SepExports
.SeLocalSystemSid
= SeLocalSystemSid
;
66 SepExports
.SeAliasAdminsSid
= SeAliasAdminsSid
;
67 SepExports
.SeAliasUsersSid
= SeAliasUsersSid
;
68 SepExports
.SeAliasGuestsSid
= SeAliasGuestsSid
;
69 SepExports
.SeAliasPowerUsersSid
= SeAliasPowerUsersSid
;
70 SepExports
.SeAliasAccountOpsSid
= SeAliasAccountOpsSid
;
71 SepExports
.SeAliasSystemOpsSid
= SeAliasSystemOpsSid
;
72 SepExports
.SeAliasPrintOpsSid
= SeAliasPrintOpsSid
;
73 SepExports
.SeAliasBackupOpsSid
= SeAliasBackupOpsSid
;
74 SepExports
.SeAuthenticatedUsersSid
= SeAuthenticatedUsersSid
;
75 SepExports
.SeRestrictedSid
= SeRestrictedSid
;
76 SepExports
.SeAnonymousLogonSid
= SeAnonymousLogonSid
;
77 SepExports
.SeLocalServiceSid
= SeLocalServiceSid
;
78 SepExports
.SeNetworkServiceSid
= SeNetworkServiceSid
;
80 SepExports
.SeUndockPrivilege
= SeUndockPrivilege
;
81 SepExports
.SeSyncAgentPrivilege
= SeSyncAgentPrivilege
;
82 SepExports
.SeEnableDelegationPrivilege
= SeEnableDelegationPrivilege
;
83 SepExports
.SeManageVolumePrivilege
= SeManageVolumePrivilege
;
84 SepExports
.SeImpersonatePrivilege
= SeImpersonatePrivilege
;
85 SepExports
.SeCreateGlobalPrivilege
= SeCreateGlobalPrivilege
;
87 SeExports
= &SepExports
;
95 SepInitializationPhase0(VOID
)
100 if (!SepInitSecurityIDs()) return FALSE
;
101 if (!SepInitDACLs()) return FALSE
;
102 if (!SepInitSDs()) return FALSE
;
104 if (!SepInitExports()) return FALSE
;
106 /* Initialize the subject context lock */
107 ExInitializeResource(&SepSubjectContextLock
);
109 /* Initialize token objects */
110 SepInitializeTokenImplementation();
112 /* Initialize logon sessions */
113 if (!SeRmInitPhase0()) return FALSE
;
115 /* Clear impersonation info for the idle thread */
116 PsGetCurrentThread()->ImpersonationInfo
= NULL
;
117 PspClearCrossThreadFlag(PsGetCurrentThread(),
118 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
120 /* Initialize the boot token */
121 ObInitializeFastReference(&PsGetCurrentProcess()->Token
, NULL
);
122 ObInitializeFastReference(&PsGetCurrentProcess()->Token
,
123 SepCreateSystemProcessToken());
130 SepInitializationPhase1(VOID
)
132 OBJECT_ATTRIBUTES ObjectAttributes
;
134 HANDLE SecurityHandle
;
137 SECURITY_DESCRIPTOR SecurityDescriptor
;
143 /* Insert the system token into the tree */
144 Status
= ObInsertObject((PVOID
)(PsGetCurrentProcess()->Token
.Value
&
151 ASSERT(NT_SUCCESS(Status
));
153 /* Create a security descriptor for the directory */
154 RtlCreateSecurityDescriptor(&SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
157 DaclLength
= sizeof(ACL
) + 3 * sizeof(ACCESS_ALLOWED_ACE
) +
158 RtlLengthSid(SeLocalSystemSid
) +
159 RtlLengthSid(SeAliasAdminsSid
) +
160 RtlLengthSid(SeWorldSid
);
161 Dacl
= ExAllocatePoolWithTag(NonPagedPool
, DaclLength
, TAG_SE
);
167 Status
= RtlCreateAcl(Dacl
, DaclLength
, ACL_REVISION
);
168 ASSERT(NT_SUCCESS(Status
));
170 /* Grant full access to SYSTEM */
171 Status
= RtlAddAccessAllowedAce(Dacl
,
173 DIRECTORY_ALL_ACCESS
,
175 ASSERT(NT_SUCCESS(Status
));
177 /* Allow admins to traverse and query */
178 Status
= RtlAddAccessAllowedAce(Dacl
,
180 READ_CONTROL
| DIRECTORY_TRAVERSE
| DIRECTORY_QUERY
,
182 ASSERT(NT_SUCCESS(Status
));
184 /* Allow anyone to traverse */
185 Status
= RtlAddAccessAllowedAce(Dacl
,
189 ASSERT(NT_SUCCESS(Status
));
191 /* And link ACL and SD */
192 Status
= RtlSetDaclSecurityDescriptor(&SecurityDescriptor
, TRUE
, Dacl
, FALSE
);
193 ASSERT(NT_SUCCESS(Status
));
195 /* Create '\Security' directory */
196 RtlInitUnicodeString(&Name
, L
"\\Security");
197 InitializeObjectAttributes(&ObjectAttributes
,
199 OBJ_PERMANENT
| OBJ_CASE_INSENSITIVE
,
201 &SecurityDescriptor
);
203 Status
= ZwCreateDirectoryObject(&SecurityHandle
,
204 DIRECTORY_ALL_ACCESS
,
206 ASSERT(NT_SUCCESS(Status
));
208 /* Create 'LSA_AUTHENTICATION_INITIALIZED' event */
209 RtlInitUnicodeString(&Name
, L
"LSA_AUTHENTICATION_INITIALIZED");
210 InitializeObjectAttributes(&ObjectAttributes
,
212 OBJ_PERMANENT
| OBJ_CASE_INSENSITIVE
,
216 Status
= ZwCreateEvent(&EventHandle
,
221 ASSERT(NT_SUCCESS(Status
));
223 Status
= ZwClose(EventHandle
);
224 ASSERT(NT_SUCCESS(Status
));
226 Status
= ZwClose(SecurityHandle
);
227 ASSERT(NT_SUCCESS(Status
));
237 /* Check the initialization phase */
238 switch (ExpInitializationPhase
)
243 return SepInitializationPhase0();
248 return SepInitializationPhase1();
252 /* Don't know any other phase! Bugcheck! */
253 KeBugCheckEx(UNEXPECTED_INITIALIZATION_CALL
,
255 ExpInitializationPhase
,
264 SeDefaultObjectMethod(IN PVOID Object
,
265 IN SECURITY_OPERATION_CODE OperationType
,
266 IN PSECURITY_INFORMATION SecurityInformation
,
267 IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor
,
268 IN OUT PULONG ReturnLength OPTIONAL
,
269 IN OUT PSECURITY_DESCRIPTOR
*OldSecurityDescriptor
,
270 IN POOL_TYPE PoolType
,
271 IN PGENERIC_MAPPING GenericMapping
)
275 /* Select the operation type */
276 switch (OperationType
)
278 /* Setting a new descriptor */
279 case SetSecurityDescriptor
:
282 ASSERT((PoolType
== PagedPool
) || (PoolType
== NonPagedPool
));
284 /* Set the information */
285 return ObSetSecurityDescriptorInfo(Object
,
288 OldSecurityDescriptor
,
292 case QuerySecurityDescriptor
:
294 /* Query the information */
295 return ObQuerySecurityDescriptorInfo(Object
,
299 OldSecurityDescriptor
);
301 case DeleteSecurityDescriptor
:
304 return ObDeassignSecurity(OldSecurityDescriptor
);
306 case AssignSecurityDescriptor
:
309 ObAssignObjectSecurityDescriptor(Object
, SecurityDescriptor
, PoolType
);
310 return STATUS_SUCCESS
;
315 KeBugCheckEx(SECURITY_SYSTEM
, 0, STATUS_INVALID_PARAMETER
, 0, 0);
318 /* Should never reach here */
320 return STATUS_SUCCESS
;
325 SeQuerySecurityAccessMask(IN SECURITY_INFORMATION SecurityInformation
,
326 OUT PACCESS_MASK DesiredAccess
)
330 if (SecurityInformation
& (OWNER_SECURITY_INFORMATION
|
331 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
))
333 *DesiredAccess
|= READ_CONTROL
;
336 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
338 *DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
344 SeSetSecurityAccessMask(IN SECURITY_INFORMATION SecurityInformation
,
345 OUT PACCESS_MASK DesiredAccess
)
349 if (SecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
351 *DesiredAccess
|= WRITE_OWNER
;
354 if (SecurityInformation
& DACL_SECURITY_INFORMATION
)
356 *DesiredAccess
|= WRITE_DAC
;
359 if (SecurityInformation
& SACL_SECURITY_INFORMATION
)
361 *DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
367 SeReportSecurityEvent(
369 _In_ PUNICODE_STRING SourceName
,
370 _In_opt_ PSID UserSid
,
371 _In_ PSE_ADT_PARAMETER_ARRAY AuditParameters
)
373 SECURITY_SUBJECT_CONTEXT SubjectContext
;
374 PTOKEN EffectiveToken
;
378 /* Validate parameters */
380 (SourceName
== NULL
) ||
381 (SourceName
->Buffer
== NULL
) ||
382 (SourceName
->Length
== 0) ||
383 (AuditParameters
== NULL
) ||
384 (AuditParameters
->ParameterCount
> SE_MAX_AUDIT_PARAMETERS
- 4))
386 return STATUS_INVALID_PARAMETER
;
389 /* Validate the source name */
390 Status
= RtlValidateUnicodeString(0, SourceName
);
391 if (!NT_SUCCESS(Status
))
396 /* Check if we have a user SID */
400 if (!RtlValidSid(UserSid
))
402 return STATUS_INVALID_PARAMETER
;
405 /* Use the user SID */
410 /* No user SID, capture the security subject context */
411 SeCaptureSubjectContext(&SubjectContext
);
413 /* Extract the effective token */
414 EffectiveToken
= SubjectContext
.ClientToken
?
415 SubjectContext
.ClientToken
: SubjectContext
.PrimaryToken
;
417 /* Use the user-and-groups SID */
418 Sid
= EffectiveToken
->UserAndGroups
->Sid
;
423 /* Check if we captured the subject context */
427 SeReleaseSubjectContext(&SubjectContext
);
431 return STATUS_SUCCESS
;
438 _Inout_ PSE_ADT_PARAMETER_ARRAY AuditParameters
,
439 _In_ SE_ADT_PARAMETER_TYPE Type
,
440 _In_range_(<, SE_MAX_AUDIT_PARAMETERS
) ULONG Index
,
441 _In_reads_(_Inexpressible_("depends on SE_ADT_PARAMETER_TYPE")) PVOID Data
)
444 return STATUS_SUCCESS
;