[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / se / semgr.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/se/semgr.c
5 * PURPOSE: Security manager
6 *
7 * PROGRAMMERS: No programmer listed.
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 /* GLOBALS ********************************************************************/
17
18 PSE_EXPORTS SeExports = NULL;
19 SE_EXPORTS SepExports;
20
21 extern ULONG ExpInitializationPhase;
22 extern ERESOURCE SepSubjectContextLock;
23
24 /* PRIVATE FUNCTIONS **********************************************************/
25
26 static BOOLEAN INIT_FUNCTION
27 SepInitExports(VOID)
28 {
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;
52
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;
75
76 SepExports.SeUndockPrivilege = SeUndockPrivilege;
77 SepExports.SeSyncAgentPrivilege = SeSyncAgentPrivilege;
78 SepExports.SeEnableDelegationPrivilege = SeEnableDelegationPrivilege;
79
80 SeExports = &SepExports;
81 return TRUE;
82 }
83
84
85 BOOLEAN
86 NTAPI
87 SepInitializationPhase0(VOID)
88 {
89 PAGED_CODE();
90
91 ExpInitLuid();
92 if (!SepInitSecurityIDs()) return FALSE;
93 if (!SepInitDACLs()) return FALSE;
94 if (!SepInitSDs()) return FALSE;
95 SepInitPrivileges();
96 if (!SepInitExports()) return FALSE;
97
98 /* Initialize the subject context lock */
99 ExInitializeResource(&SepSubjectContextLock);
100
101 /* Initialize token objects */
102 SepInitializeTokenImplementation();
103
104 /* Clear impersonation info for the idle thread */
105 PsGetCurrentThread()->ImpersonationInfo = NULL;
106 PspClearCrossThreadFlag(PsGetCurrentThread(),
107 CT_ACTIVE_IMPERSONATION_INFO_BIT);
108
109 /* Initialize the boot token */
110 ObInitializeFastReference(&PsGetCurrentProcess()->Token, NULL);
111 ObInitializeFastReference(&PsGetCurrentProcess()->Token,
112 SepCreateSystemProcessToken());
113 return TRUE;
114 }
115
116 BOOLEAN
117 NTAPI
118 SepInitializationPhase1(VOID)
119 {
120 NTSTATUS Status;
121 PAGED_CODE();
122
123 /* Insert the system token into the tree */
124 Status = ObInsertObject((PVOID)(PsGetCurrentProcess()->Token.Value &
125 ~MAX_FAST_REFS),
126 NULL,
127 0,
128 0,
129 NULL,
130 NULL);
131 ASSERT(NT_SUCCESS(Status));
132
133 /* FIXME: TODO \\ Security directory */
134 return TRUE;
135 }
136
137 BOOLEAN
138 NTAPI
139 SeInitSystem(VOID)
140 {
141 /* Check the initialization phase */
142 switch (ExpInitializationPhase)
143 {
144 case 0:
145
146 /* Do Phase 0 */
147 return SepInitializationPhase0();
148
149 case 1:
150
151 /* Do Phase 1 */
152 return SepInitializationPhase1();
153
154 default:
155
156 /* Don't know any other phase! Bugcheck! */
157 KeBugCheckEx(UNEXPECTED_INITIALIZATION_CALL,
158 0,
159 ExpInitializationPhase,
160 0,
161 0);
162 return FALSE;
163 }
164 }
165
166 BOOLEAN
167 NTAPI
168 SeInitSRM(VOID)
169 {
170 OBJECT_ATTRIBUTES ObjectAttributes;
171 UNICODE_STRING Name;
172 HANDLE DirectoryHandle;
173 HANDLE EventHandle;
174 NTSTATUS Status;
175
176 /* Create '\Security' directory */
177 RtlInitUnicodeString(&Name,
178 L"\\Security");
179 InitializeObjectAttributes(&ObjectAttributes,
180 &Name,
181 OBJ_PERMANENT,
182 0,
183 NULL);
184 Status = ZwCreateDirectoryObject(&DirectoryHandle,
185 DIRECTORY_ALL_ACCESS,
186 &ObjectAttributes);
187 if (!NT_SUCCESS(Status))
188 {
189 DPRINT1("Failed to create 'Security' directory!\n");
190 return FALSE;
191 }
192
193 /* Create 'LSA_AUTHENTICATION_INITALIZED' event */
194 RtlInitUnicodeString(&Name,
195 L"\\LSA_AUTHENTICATION_INITALIZED");
196 InitializeObjectAttributes(&ObjectAttributes,
197 &Name,
198 OBJ_PERMANENT,
199 DirectoryHandle,
200 SePublicDefaultSd);
201 Status = ZwCreateEvent(&EventHandle,
202 EVENT_ALL_ACCESS,
203 &ObjectAttributes,
204 SynchronizationEvent,
205 FALSE);
206 if (!NT_SUCCESS(Status))
207 {
208 DPRINT1("Failed to create 'LSA_AUTHENTICATION_INITALIZED' event!\n");
209 NtClose(DirectoryHandle);
210 return FALSE;
211 }
212
213 ZwClose(EventHandle);
214 ZwClose(DirectoryHandle);
215
216 /* FIXME: Create SRM port and listener thread */
217
218 return TRUE;
219 }
220
221 NTSTATUS
222 NTAPI
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)
231 {
232 PAGED_CODE();
233
234 /* Select the operation type */
235 switch (OperationType)
236 {
237 /* Setting a new descriptor */
238 case SetSecurityDescriptor:
239
240 /* Sanity check */
241 ASSERT((PoolType == PagedPool) || (PoolType == NonPagedPool));
242
243 /* Set the information */
244 return ObSetSecurityDescriptorInfo(Object,
245 SecurityInformation,
246 SecurityDescriptor,
247 OldSecurityDescriptor,
248 PoolType,
249 GenericMapping);
250
251 case QuerySecurityDescriptor:
252
253 /* Query the information */
254 return ObQuerySecurityDescriptorInfo(Object,
255 SecurityInformation,
256 SecurityDescriptor,
257 ReturnLength,
258 OldSecurityDescriptor);
259
260 case DeleteSecurityDescriptor:
261
262 /* De-assign it */
263 return ObDeassignSecurity(OldSecurityDescriptor);
264
265 case AssignSecurityDescriptor:
266
267 /* Assign it */
268 ObAssignObjectSecurityDescriptor(Object, SecurityDescriptor, PoolType);
269 return STATUS_SUCCESS;
270
271 default:
272
273 /* Bug check */
274 KeBugCheckEx(SECURITY_SYSTEM, 0, STATUS_INVALID_PARAMETER, 0, 0);
275 }
276
277 /* Should never reach here */
278 ASSERT(FALSE);
279 return STATUS_SUCCESS;
280 }
281
282 ULONG SidInTokenCalls = 0;
283
284 static BOOLEAN
285 SepSidInToken(PACCESS_TOKEN _Token,
286 PSID Sid)
287 {
288 ULONG i;
289 PTOKEN Token = (PTOKEN)_Token;
290
291 PAGED_CODE();
292
293 SidInTokenCalls++;
294 if (!(SidInTokenCalls % 10000)) DPRINT1("SidInToken Calls: %d\n", SidInTokenCalls);
295
296 if (Token->UserAndGroupCount == 0)
297 {
298 return FALSE;
299 }
300
301 for (i=0; i<Token->UserAndGroupCount; i++)
302 {
303 if (RtlEqualSid(Sid, Token->UserAndGroups[i].Sid))
304 {
305 if ((i == 0)|| (Token->UserAndGroups[i].Attributes & SE_GROUP_ENABLED))
306 {
307 return TRUE;
308 }
309
310 return FALSE;
311 }
312 }
313
314 return FALSE;
315 }
316
317
318 VOID NTAPI
319 SeQuerySecurityAccessMask(IN SECURITY_INFORMATION SecurityInformation,
320 OUT PACCESS_MASK DesiredAccess)
321 {
322 *DesiredAccess = 0;
323
324 if (SecurityInformation & (OWNER_SECURITY_INFORMATION |
325 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION))
326 {
327 *DesiredAccess |= READ_CONTROL;
328 }
329 if (SecurityInformation & SACL_SECURITY_INFORMATION)
330 {
331 *DesiredAccess |= ACCESS_SYSTEM_SECURITY;
332 }
333 }
334
335 VOID NTAPI
336 SeSetSecurityAccessMask(IN SECURITY_INFORMATION SecurityInformation,
337 OUT PACCESS_MASK DesiredAccess)
338 {
339 *DesiredAccess = 0;
340
341 if (SecurityInformation & (OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION))
342 {
343 *DesiredAccess |= WRITE_OWNER;
344 }
345 if (SecurityInformation & DACL_SECURITY_INFORMATION)
346 {
347 *DesiredAccess |= WRITE_DAC;
348 }
349 if (SecurityInformation & SACL_SECURITY_INFORMATION)
350 {
351 *DesiredAccess |= ACCESS_SYSTEM_SECURITY;
352 }
353 }
354
355 BOOLEAN NTAPI
356 SepAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
357 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext,
358 IN BOOLEAN SubjectContextLocked,
359 IN ACCESS_MASK DesiredAccess,
360 IN ACCESS_MASK PreviouslyGrantedAccess,
361 OUT PPRIVILEGE_SET* Privileges,
362 IN PGENERIC_MAPPING GenericMapping,
363 IN KPROCESSOR_MODE AccessMode,
364 OUT PACCESS_MASK GrantedAccess,
365 OUT PNTSTATUS AccessStatus,
366 SECURITY_IMPERSONATION_LEVEL LowestImpersonationLevel)
367 {
368 LUID_AND_ATTRIBUTES Privilege;
369 ACCESS_MASK CurrentAccess, AccessMask;
370 PACCESS_TOKEN Token;
371 ULONG i;
372 PACL Dacl;
373 BOOLEAN Present;
374 BOOLEAN Defaulted;
375 PACE CurrentAce;
376 PSID Sid;
377 NTSTATUS Status;
378 PAGED_CODE();
379
380 /* Check if we didn't get an SD */
381 if (!SecurityDescriptor)
382 {
383 /* Automatic failure */
384 *AccessStatus = STATUS_ACCESS_DENIED;
385 return FALSE;
386 }
387
388 /* Check for invalid impersonation */
389 if ((SubjectSecurityContext->ClientToken) &&
390 (SubjectSecurityContext->ImpersonationLevel < LowestImpersonationLevel))
391 {
392 *AccessStatus = STATUS_BAD_IMPERSONATION_LEVEL;
393 return FALSE;
394 }
395
396 /* Check for no access desired */
397 if (!DesiredAccess)
398 {
399 /* Check if we had no previous access */
400 if (!PreviouslyGrantedAccess)
401 {
402 /* Then there's nothing to give */
403 *AccessStatus = STATUS_ACCESS_DENIED;
404 return FALSE;
405 }
406
407 /* Return the previous access only */
408 *GrantedAccess = PreviouslyGrantedAccess;
409 *AccessStatus = STATUS_SUCCESS;
410 *Privileges = NULL;
411 return TRUE;
412 }
413
414 /* Acquire the lock if needed */
415 if (!SubjectContextLocked) SeLockSubjectContext(SubjectSecurityContext);
416
417 /* Map given accesses */
418 RtlMapGenericMask(&DesiredAccess, GenericMapping);
419 if (PreviouslyGrantedAccess)
420 RtlMapGenericMask(&PreviouslyGrantedAccess, GenericMapping);
421
422
423
424 CurrentAccess = PreviouslyGrantedAccess;
425
426
427
428 Token = SubjectSecurityContext->ClientToken ?
429 SubjectSecurityContext->ClientToken : SubjectSecurityContext->PrimaryToken;
430
431 /* Get the DACL */
432 Status = RtlGetDaclSecurityDescriptor(SecurityDescriptor,
433 &Present,
434 &Dacl,
435 &Defaulted);
436 if (!NT_SUCCESS(Status))
437 {
438 if (SubjectContextLocked == FALSE)
439 {
440 SeUnlockSubjectContext(SubjectSecurityContext);
441 }
442
443 *AccessStatus = Status;
444 return FALSE;
445 }
446
447 /* RULE 1: Grant desired access if the object is unprotected */
448 if (Present == FALSE || Dacl == NULL)
449 {
450 if (SubjectContextLocked == FALSE)
451 {
452 SeUnlockSubjectContext(SubjectSecurityContext);
453 }
454
455 if (DesiredAccess & MAXIMUM_ALLOWED)
456 {
457 *GrantedAccess = GenericMapping->GenericAll;
458 *GrantedAccess |= (DesiredAccess & ~MAXIMUM_ALLOWED);
459 }
460 else
461 {
462 *GrantedAccess = DesiredAccess | PreviouslyGrantedAccess;
463 }
464
465 *AccessStatus = STATUS_SUCCESS;
466 return TRUE;
467 }
468
469 CurrentAccess = PreviouslyGrantedAccess;
470
471 /* RULE 2: Check token for 'take ownership' privilege */
472 Privilege.Luid = SeTakeOwnershipPrivilege;
473 Privilege.Attributes = SE_PRIVILEGE_ENABLED;
474
475 if (SepPrivilegeCheck(Token,
476 &Privilege,
477 1,
478 PRIVILEGE_SET_ALL_NECESSARY,
479 AccessMode))
480 {
481 CurrentAccess |= WRITE_OWNER;
482 if ((DesiredAccess & ~VALID_INHERIT_FLAGS) ==
483 (CurrentAccess & ~VALID_INHERIT_FLAGS))
484 {
485 if (SubjectContextLocked == FALSE)
486 {
487 SeUnlockSubjectContext(SubjectSecurityContext);
488 }
489
490 *GrantedAccess = CurrentAccess;
491 *AccessStatus = STATUS_SUCCESS;
492 return TRUE;
493 }
494 }
495
496 /* RULE 3: Check whether the token is the owner */
497 Status = RtlGetOwnerSecurityDescriptor(SecurityDescriptor,
498 &Sid,
499 &Defaulted);
500 if (!NT_SUCCESS(Status))
501 {
502 DPRINT1("RtlGetOwnerSecurityDescriptor() failed (Status %lx)\n", Status);
503 if (SubjectContextLocked == FALSE)
504 {
505 SeUnlockSubjectContext(SubjectSecurityContext);
506 }
507
508 *AccessStatus = Status;
509 return FALSE;
510 }
511
512 if (Sid && SepSidInToken(Token, Sid))
513 {
514 CurrentAccess |= (READ_CONTROL | WRITE_DAC);
515 if ((DesiredAccess & ~VALID_INHERIT_FLAGS) ==
516 (CurrentAccess & ~VALID_INHERIT_FLAGS))
517 {
518 if (SubjectContextLocked == FALSE)
519 {
520 SeUnlockSubjectContext(SubjectSecurityContext);
521 }
522
523 *GrantedAccess = CurrentAccess;
524 *AccessStatus = STATUS_SUCCESS;
525 return TRUE;
526 }
527 }
528
529 /* Fail if DACL is absent */
530 if (Present == FALSE)
531 {
532 if (SubjectContextLocked == FALSE)
533 {
534 SeUnlockSubjectContext(SubjectSecurityContext);
535 }
536
537 *GrantedAccess = 0;
538 *AccessStatus = STATUS_ACCESS_DENIED;
539 return FALSE;
540 }
541
542 /* RULE 4: Grant rights according to the DACL */
543 CurrentAce = (PACE)(Dacl + 1);
544 for (i = 0; i < Dacl->AceCount; i++)
545 {
546 Sid = (PSID)(CurrentAce + 1);
547 if (CurrentAce->Header.AceType == ACCESS_DENIED_ACE_TYPE)
548 {
549 if (SepSidInToken(Token, Sid))
550 {
551 if (SubjectContextLocked == FALSE)
552 {
553 SeUnlockSubjectContext(SubjectSecurityContext);
554 }
555
556 *GrantedAccess = 0;
557 *AccessStatus = STATUS_ACCESS_DENIED;
558 return FALSE;
559 }
560 }
561
562 else if (CurrentAce->Header.AceType == ACCESS_ALLOWED_ACE_TYPE)
563 {
564 if (SepSidInToken(Token, Sid))
565 {
566 AccessMask = CurrentAce->AccessMask;
567 RtlMapGenericMask(&AccessMask, GenericMapping);
568 CurrentAccess |= AccessMask;
569 }
570 }
571 else
572 {
573 DPRINT1("Unknown Ace type 0x%lx\n", CurrentAce->Header.AceType);
574 }
575 CurrentAce = (PACE)((ULONG_PTR)CurrentAce + CurrentAce->Header.AceSize);
576 }
577
578 if (SubjectContextLocked == FALSE)
579 {
580 SeUnlockSubjectContext(SubjectSecurityContext);
581 }
582
583 DPRINT("CurrentAccess %08lx\n DesiredAccess %08lx\n",
584 CurrentAccess, DesiredAccess);
585
586 *GrantedAccess = CurrentAccess & DesiredAccess;
587
588 if (DesiredAccess & MAXIMUM_ALLOWED)
589 {
590 *GrantedAccess = CurrentAccess;
591 *AccessStatus = STATUS_SUCCESS;
592 return TRUE;
593 }
594 else if ((*GrantedAccess & ~VALID_INHERIT_FLAGS) ==
595 (DesiredAccess & ~VALID_INHERIT_FLAGS))
596 {
597 *AccessStatus = STATUS_SUCCESS;
598 return TRUE;
599 }
600 else
601 {
602 DPRINT1("HACK: Should deny access for caller: granted 0x%lx, desired 0x%lx (generic mapping %p).\n",
603 *GrantedAccess, DesiredAccess, GenericMapping);
604 //*AccessStatus = STATUS_ACCESS_DENIED;
605 //return FALSE;
606 *AccessStatus = STATUS_SUCCESS;
607 return TRUE;
608 }
609 }
610
611 static PSID
612 SepGetSDOwner(IN PSECURITY_DESCRIPTOR _SecurityDescriptor)
613 {
614 PISECURITY_DESCRIPTOR SecurityDescriptor = _SecurityDescriptor;
615 PSID Owner;
616
617 if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
618 Owner = (PSID)((ULONG_PTR)SecurityDescriptor->Owner +
619 (ULONG_PTR)SecurityDescriptor);
620 else
621 Owner = (PSID)SecurityDescriptor->Owner;
622
623 return Owner;
624 }
625
626 static PSID
627 SepGetSDGroup(IN PSECURITY_DESCRIPTOR _SecurityDescriptor)
628 {
629 PISECURITY_DESCRIPTOR SecurityDescriptor = _SecurityDescriptor;
630 PSID Group;
631
632 if (SecurityDescriptor->Control & SE_SELF_RELATIVE)
633 Group = (PSID)((ULONG_PTR)SecurityDescriptor->Group +
634 (ULONG_PTR)SecurityDescriptor);
635 else
636 Group = (PSID)SecurityDescriptor->Group;
637
638 return Group;
639 }
640
641
642 /* PUBLIC FUNCTIONS ***********************************************************/
643
644 /*
645 * @implemented
646 */
647 BOOLEAN NTAPI
648 SeAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
649 IN PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext,
650 IN BOOLEAN SubjectContextLocked,
651 IN ACCESS_MASK DesiredAccess,
652 IN ACCESS_MASK PreviouslyGrantedAccess,
653 OUT PPRIVILEGE_SET* Privileges,
654 IN PGENERIC_MAPPING GenericMapping,
655 IN KPROCESSOR_MODE AccessMode,
656 OUT PACCESS_MASK GrantedAccess,
657 OUT PNTSTATUS AccessStatus)
658 {
659 PAGED_CODE();
660
661 /* Check if this is kernel mode */
662 if (AccessMode == KernelMode)
663 {
664 /* Check if kernel wants everything */
665 if (DesiredAccess & MAXIMUM_ALLOWED)
666 {
667 /* Give it */
668 *GrantedAccess = GenericMapping->GenericAll;
669 *GrantedAccess |= (DesiredAccess &~ MAXIMUM_ALLOWED);
670 *GrantedAccess |= PreviouslyGrantedAccess;
671 }
672 else
673 {
674 /* Give the desired and previous access */
675 *GrantedAccess = DesiredAccess | PreviouslyGrantedAccess;
676 }
677
678 /* Success */
679 *AccessStatus = STATUS_SUCCESS;
680 return TRUE;
681 }
682
683 /* Call the internal function */
684 return SepAccessCheck(SecurityDescriptor,
685 SubjectSecurityContext,
686 SubjectContextLocked,
687 DesiredAccess,
688 PreviouslyGrantedAccess,
689 Privileges,
690 GenericMapping,
691 AccessMode,
692 GrantedAccess,
693 AccessStatus,
694 SecurityImpersonation);
695 }
696
697 /* SYSTEM CALLS ***************************************************************/
698
699 /*
700 * @implemented
701 */
702 NTSTATUS
703 NTAPI
704 NtAccessCheck(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
705 IN HANDLE TokenHandle,
706 IN ACCESS_MASK DesiredAccess,
707 IN PGENERIC_MAPPING GenericMapping,
708 OUT PPRIVILEGE_SET PrivilegeSet OPTIONAL,
709 IN OUT PULONG PrivilegeSetLength,
710 OUT PACCESS_MASK GrantedAccess,
711 OUT PNTSTATUS AccessStatus)
712 {
713 SECURITY_SUBJECT_CONTEXT SubjectSecurityContext;
714 KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
715 PTOKEN Token;
716 NTSTATUS Status;
717 PAGED_CODE();
718
719 /* Check if this is kernel mode */
720 if (PreviousMode == KernelMode)
721 {
722 /* Check if kernel wants everything */
723 if (DesiredAccess & MAXIMUM_ALLOWED)
724 {
725 /* Give it */
726 *GrantedAccess = GenericMapping->GenericAll;
727 *GrantedAccess |= (DesiredAccess &~ MAXIMUM_ALLOWED);
728 }
729 else
730 {
731 /* Just give the desired access */
732 *GrantedAccess = DesiredAccess;
733 }
734
735 /* Success */
736 *AccessStatus = STATUS_SUCCESS;
737 return STATUS_SUCCESS;
738 }
739
740 /* Protect probe in SEH */
741 _SEH2_TRY
742 {
743 /* Probe all pointers */
744 ProbeForRead(GenericMapping, sizeof(GENERIC_MAPPING), sizeof(ULONG));
745 ProbeForRead(PrivilegeSetLength, sizeof(ULONG), sizeof(ULONG));
746 ProbeForWrite(PrivilegeSet, *PrivilegeSetLength, sizeof(ULONG));
747 ProbeForWrite(GrantedAccess, sizeof(ACCESS_MASK), sizeof(ULONG));
748 ProbeForWrite(AccessStatus, sizeof(NTSTATUS), sizeof(ULONG));
749 }
750 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
751 {
752 /* Return the exception code */
753 _SEH2_YIELD(return _SEH2_GetExceptionCode());
754 }
755 _SEH2_END;
756
757 /* Check for unmapped access rights */
758 if (DesiredAccess & (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL))
759 return STATUS_GENERIC_NOT_MAPPED;
760
761 /* Reference the token */
762 Status = ObReferenceObjectByHandle(TokenHandle,
763 TOKEN_QUERY,
764 SepTokenObjectType,
765 PreviousMode,
766 (PVOID*)&Token,
767 NULL);
768 if (!NT_SUCCESS(Status))
769 {
770 DPRINT("Failed to reference token (Status %lx)\n", Status);
771 return Status;
772 }
773
774 /* Check token type */
775 if (Token->TokenType != TokenImpersonation)
776 {
777 DPRINT("No impersonation token\n");
778 ObDereferenceObject(Token);
779 return STATUS_NO_IMPERSONATION_TOKEN;
780 }
781
782 /* Check the impersonation level */
783 if (Token->ImpersonationLevel < SecurityIdentification)
784 {
785 DPRINT("Impersonation level < SecurityIdentification\n");
786 ObDereferenceObject(Token);
787 return STATUS_BAD_IMPERSONATION_LEVEL;
788 }
789
790 /* Check security descriptor for valid owner and group */
791 if (SepGetSDOwner(SecurityDescriptor)== NULL ||
792 SepGetSDGroup(SecurityDescriptor) == NULL)
793 {
794 DPRINT("Security Descriptor does not have a valid group or owner\n");
795 ObDereferenceObject(Token);
796 return STATUS_INVALID_SECURITY_DESCR;
797 }
798
799 /* Set up the subject context, and lock it */
800 SubjectSecurityContext.ClientToken = Token;
801 SubjectSecurityContext.ImpersonationLevel = Token->ImpersonationLevel;
802 SubjectSecurityContext.PrimaryToken = NULL;
803 SubjectSecurityContext.ProcessAuditId = NULL;
804 SeLockSubjectContext(&SubjectSecurityContext);
805
806 /* Now perform the access check */
807 SepAccessCheck(SecurityDescriptor,
808 &SubjectSecurityContext,
809 TRUE,
810 DesiredAccess,
811 0,
812 &PrivilegeSet, //FIXME
813 GenericMapping,
814 PreviousMode,
815 GrantedAccess,
816 AccessStatus,
817 SecurityIdentification);
818
819 /* Unlock subject context */
820 SeUnlockSubjectContext(&SubjectSecurityContext);
821
822 /* Dereference the token */
823 ObDereferenceObject(Token);
824
825 /* Check succeeded */
826 return STATUS_SUCCESS;
827 }
828
829
830 NTSTATUS
831 NTAPI
832 NtAccessCheckByType(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
833 IN PSID PrincipalSelfSid,
834 IN HANDLE ClientToken,
835 IN ACCESS_MASK DesiredAccess,
836 IN POBJECT_TYPE_LIST ObjectTypeList,
837 IN ULONG ObjectTypeLength,
838 IN PGENERIC_MAPPING GenericMapping,
839 IN PPRIVILEGE_SET PrivilegeSet,
840 IN ULONG PrivilegeSetLength,
841 OUT PACCESS_MASK GrantedAccess,
842 OUT PNTSTATUS AccessStatus)
843 {
844 UNIMPLEMENTED;
845 return STATUS_NOT_IMPLEMENTED;
846 }
847
848 NTSTATUS
849 NTAPI
850 NtAccessCheckByTypeAndAuditAlarm(IN PUNICODE_STRING SubsystemName,
851 IN HANDLE HandleId,
852 IN PUNICODE_STRING ObjectTypeName,
853 IN PUNICODE_STRING ObjectName,
854 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
855 IN PSID PrincipalSelfSid,
856 IN ACCESS_MASK DesiredAccess,
857 IN AUDIT_EVENT_TYPE AuditType,
858 IN ULONG Flags,
859 IN POBJECT_TYPE_LIST ObjectTypeList,
860 IN ULONG ObjectTypeLength,
861 IN PGENERIC_MAPPING GenericMapping,
862 IN BOOLEAN ObjectCreation,
863 OUT PACCESS_MASK GrantedAccess,
864 OUT PNTSTATUS AccessStatus,
865 OUT PBOOLEAN GenerateOnClose)
866 {
867 UNIMPLEMENTED;
868 return STATUS_NOT_IMPLEMENTED;
869 }
870
871 NTSTATUS
872 NTAPI
873 NtAccessCheckByTypeResultList(IN PSECURITY_DESCRIPTOR SecurityDescriptor,
874 IN PSID PrincipalSelfSid,
875 IN HANDLE ClientToken,
876 IN ACCESS_MASK DesiredAccess,
877 IN POBJECT_TYPE_LIST ObjectTypeList,
878 IN ULONG ObjectTypeLength,
879 IN PGENERIC_MAPPING GenericMapping,
880 IN PPRIVILEGE_SET PrivilegeSet,
881 IN ULONG PrivilegeSetLength,
882 OUT PACCESS_MASK GrantedAccess,
883 OUT PNTSTATUS AccessStatus)
884 {
885 UNIMPLEMENTED;
886 return STATUS_NOT_IMPLEMENTED;
887 }
888
889 NTSTATUS
890 NTAPI
891 NtAccessCheckByTypeResultListAndAuditAlarm(IN PUNICODE_STRING SubsystemName,
892 IN HANDLE HandleId,
893 IN PUNICODE_STRING ObjectTypeName,
894 IN PUNICODE_STRING ObjectName,
895 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
896 IN PSID PrincipalSelfSid,
897 IN ACCESS_MASK DesiredAccess,
898 IN AUDIT_EVENT_TYPE AuditType,
899 IN ULONG Flags,
900 IN POBJECT_TYPE_LIST ObjectTypeList,
901 IN ULONG ObjectTypeLength,
902 IN PGENERIC_MAPPING GenericMapping,
903 IN BOOLEAN ObjectCreation,
904 OUT PACCESS_MASK GrantedAccess,
905 OUT PNTSTATUS AccessStatus,
906 OUT PBOOLEAN GenerateOnClose)
907 {
908 UNIMPLEMENTED;
909 return STATUS_NOT_IMPLEMENTED;
910 }
911
912 NTSTATUS
913 NTAPI
914 NtAccessCheckByTypeResultListAndAuditAlarmByHandle(IN PUNICODE_STRING SubsystemName,
915 IN HANDLE HandleId,
916 IN HANDLE ClientToken,
917 IN PUNICODE_STRING ObjectTypeName,
918 IN PUNICODE_STRING ObjectName,
919 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
920 IN PSID PrincipalSelfSid,
921 IN ACCESS_MASK DesiredAccess,
922 IN AUDIT_EVENT_TYPE AuditType,
923 IN ULONG Flags,
924 IN POBJECT_TYPE_LIST ObjectTypeList,
925 IN ULONG ObjectTypeLength,
926 IN PGENERIC_MAPPING GenericMapping,
927 IN BOOLEAN ObjectCreation,
928 OUT PACCESS_MASK GrantedAccess,
929 OUT PNTSTATUS AccessStatus,
930 OUT PBOOLEAN GenerateOnClose)
931 {
932 UNIMPLEMENTED;
933 return STATUS_NOT_IMPLEMENTED;
934 }
935
936 /* EOF */