4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2003 Mike McCormack
6 * Copyright (C) 2005 Robert Shearman
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
41 #include "wine/unicode.h"
43 #define MAX_SUBAUTH_COUNT 1
45 const LUID SeIncreaseQuotaPrivilege = { 5, 0 };
46 const LUID SeSecurityPrivilege = { 8, 0 };
47 const LUID SeTakeOwnershipPrivilege = { 9, 0 };
48 const LUID SeLoadDriverPrivilege = { 10, 0 };
49 const LUID SeSystemProfilePrivilege = { 11, 0 };
50 const LUID SeSystemtimePrivilege = { 12, 0 };
51 const LUID SeProfileSingleProcessPrivilege = { 13, 0 };
52 const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 };
53 const LUID SeCreatePagefilePrivilege = { 15, 0 };
54 const LUID SeBackupPrivilege = { 17, 0 };
55 const LUID SeRestorePrivilege = { 18, 0 };
56 const LUID SeShutdownPrivilege = { 19, 0 };
57 const LUID SeDebugPrivilege = { 20, 0 };
58 const LUID SeSystemEnvironmentPrivilege = { 22, 0 };
59 const LUID SeChangeNotifyPrivilege = { 23, 0 };
60 const LUID SeRemoteShutdownPrivilege = { 24, 0 };
61 const LUID SeUndockPrivilege = { 25, 0 };
62 const LUID SeManageVolumePrivilege = { 28, 0 };
63 const LUID SeImpersonatePrivilege = { 29, 0 };
64 const LUID SeCreateGlobalPrivilege = { 30, 0 };
66 static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
67 static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
68 static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
69 static const SID authenticated_user_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } };
70 static const SID local_system_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } };
71 static const PSID security_world_sid = (PSID)&world_sid;
72 static const PSID security_local_sid = (PSID)&local_sid;
73 const PSID security_interactive_sid = (PSID)&interactive_sid;
74 static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
75 static const PSID security_local_system_sid = (PSID)&local_system_sid;
77 static luid_t prev_luid_value = { 1000, 0 };
81 struct object obj; /* object header */
82 luid_t token_id; /* system-unique id of token */
83 luid_t modified_id; /* new id allocated every time token is modified */
84 struct list privileges; /* privileges available to the token */
85 struct list groups; /* groups that the user of this token belongs to (sid_and_attributes) */
86 SID *user; /* SID of user this token represents */
87 SID *primary_group; /* SID of user's primary group */
88 unsigned primary; /* is this a primary or impersonation token? */
89 ACL *default_dacl; /* the default DACL to assign to objects created by this user */
90 TOKEN_SOURCE source; /* source of the token */
91 SECURITY_IMPERSONATION_LEVEL impersonation_level; /* impersonation level this token is capable of if non-primary token */
98 unsigned enabled : 1; /* is the privilege currently enabled? */
99 unsigned def : 1; /* is the privilege enabled by default? */
105 unsigned enabled : 1; /* is the sid currently enabled? */
106 unsigned def : 1; /* is the sid enabled by default? */
107 unsigned logon : 1; /* is this a logon sid? */
108 unsigned mandatory: 1; /* is this sid always enabled? */
109 unsigned owner : 1; /* can this sid be an owner of an object? */
110 unsigned resource : 1; /* is this a domain-local group? */
111 unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
115 static void token_dump( struct object *obj, int verbose );
116 static unsigned int token_map_access( struct object *obj, unsigned int access );
117 static void token_destroy( struct object *obj );
119 static const struct object_ops token_ops =
121 sizeof(struct token), /* size */
122 token_dump, /* dump */
123 no_add_queue, /* add_queue */
124 NULL, /* remove_queue */
126 NULL, /* satisfied */
127 no_signal, /* signal */
128 no_get_fd, /* get_fd */
129 token_map_access, /* map_access */
130 no_lookup_name, /* lookup_name */
131 no_open_file, /* open_file */
132 no_close_handle, /* close_handle */
133 token_destroy /* destroy */
137 static void token_dump( struct object *obj, int verbose )
139 fprintf( stderr, "Security token\n" );
140 /* FIXME: dump token members */
143 static unsigned int token_map_access( struct object *obj, unsigned int access )
145 if (access & GENERIC_READ) access |= TOKEN_READ;
146 if (access & GENERIC_WRITE) access |= TOKEN_WRITE;
147 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
148 if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS;
149 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
152 static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
155 SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
156 if (!sid) return NULL;
157 sid->Revision = SID_REVISION;
158 sid->SubAuthorityCount = subauthcount;
159 sid->IdentifierAuthority = *idauthority;
160 for (i = 0; i < subauthcount; i++)
161 sid->SubAuthority[i] = subauth[i];
165 static inline int security_equal_sid( const SID *sid1, const SID *sid2 )
167 return ((sid1->SubAuthorityCount == sid2->SubAuthorityCount) &&
168 !memcmp( sid1, sid2, FIELD_OFFSET(SID, SubAuthority[sid1->SubAuthorityCount]) ));
171 void security_set_thread_token( struct thread *thread, obj_handle_t handle )
176 release_object( thread->token );
177 thread->token = NULL;
181 struct token *token = (struct token *)get_handle_obj( current->process,
188 release_object( thread->token );
189 thread->token = token;
194 static const ACE_HEADER *ace_next( const ACE_HEADER *ace )
196 return (const ACE_HEADER *)((const char *)ace + ace->AceSize);
199 static int acl_is_valid( const ACL *acl, data_size_t size )
202 const ACE_HEADER *ace;
204 if (size < sizeof(ACL))
207 size = min(size, MAX_ACL_LEN);
211 ace = (const ACE_HEADER *)(acl + 1);
212 for (i = 0; i < acl->AceCount; i++)
215 data_size_t sid_size;
217 if (size < sizeof(ACE_HEADER))
219 if (size < ace->AceSize)
221 size -= ace->AceSize;
222 switch (ace->AceType)
224 case ACCESS_DENIED_ACE_TYPE:
225 sid = (const SID *)&((const ACCESS_DENIED_ACE *)ace)->SidStart;
226 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart);
228 case ACCESS_ALLOWED_ACE_TYPE:
229 sid = (const SID *)&((const ACCESS_ALLOWED_ACE *)ace)->SidStart;
230 sid_size = ace->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart);
232 case SYSTEM_AUDIT_ACE_TYPE:
233 sid = (const SID *)&((const SYSTEM_AUDIT_ACE *)ace)->SidStart;
234 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart);
236 case SYSTEM_ALARM_ACE_TYPE:
237 sid = (const SID *)&((const SYSTEM_ALARM_ACE *)ace)->SidStart;
238 sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart);
243 if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) ||
244 sid_size < FIELD_OFFSET(SID, SubAuthority[sid->SubAuthorityCount]))
246 ace = ace_next( ace );
251 /* checks whether all members of a security descriptor fit inside the size
252 * of memory specified */
253 int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
255 size_t offset = sizeof(struct security_descriptor);
265 if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
266 (offset + sd->owner_len > size))
268 owner = sd_get_owner( sd );
271 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
272 if ((sd->owner_len < sizeof(SID)) || (needed_size > sd->owner_len))
275 offset += sd->owner_len;
277 if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
278 (offset + sd->group_len > size))
280 group = sd_get_group( sd );
283 size_t needed_size = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
284 if ((sd->group_len < sizeof(SID)) || (needed_size > sd->group_len))
287 offset += sd->group_len;
289 if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
291 sacl = sd_get_sacl( sd, &dummy );
292 if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
294 offset += sd->sacl_len;
296 if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
298 dacl = sd_get_dacl( sd, &dummy );
299 if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
301 offset += sd->dacl_len;
306 /* maps from generic rights to specific rights as given by a mapping */
307 static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
309 if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
310 if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
311 if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
312 if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
316 static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
318 return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
321 static inline void allocate_luid( luid_t *luid )
323 prev_luid_value.low_part++;
324 *luid = prev_luid_value;
327 DECL_HANDLER( allocate_locally_unique_id )
329 allocate_luid( &reply->luid );
332 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
334 out->Luid = in->luid;
336 (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
337 (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
340 static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
342 struct privilege *privilege = mem_alloc( sizeof(*privilege) );
345 privilege->luid = *luid;
346 privilege->def = privilege->enabled = (enabled != 0);
347 list_add_tail( &token->privileges, &privilege->entry );
352 static inline void privilege_remove( struct privilege *privilege )
354 list_remove( &privilege->entry );
358 static void token_destroy( struct object *obj )
361 struct list *cursor, *cursor_next;
363 assert( obj->ops == &token_ops );
364 token = (struct token *)obj;
368 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
370 struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
371 privilege_remove( privilege );
374 LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
376 struct group *group = LIST_ENTRY( cursor, struct group, entry );
377 list_remove( &group->entry );
381 free( token->default_dacl );
384 /* creates a new token.
385 * groups may be NULL if group_count is 0.
386 * privs may be NULL if priv_count is 0.
387 * default_dacl may be NULL, indicating that all objects created by the user
389 * modified_id may be NULL, indicating that a new modified_id luid should be
392 static struct token *create_token( unsigned primary, const SID *user,
393 const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
394 const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
395 const ACL *default_dacl, TOKEN_SOURCE source,
396 const luid_t *modified_id,
397 SECURITY_IMPERSONATION_LEVEL impersonation_level )
399 struct token *token = alloc_object( &token_ops );
404 allocate_luid( &token->token_id );
406 token->modified_id = *modified_id;
408 allocate_luid( &token->modified_id );
409 list_init( &token->privileges );
410 list_init( &token->groups );
411 token->primary = primary;
412 /* primary tokens don't have impersonation levels */
414 token->impersonation_level = -1;
416 token->impersonation_level = impersonation_level;
417 token->default_dacl = NULL;
418 token->primary_group = NULL;
421 token->user = memdup( user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
424 release_object( token );
429 for (i = 0; i < group_count; i++)
431 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
432 struct group *group = mem_alloc( size );
436 release_object( token );
439 memcpy( &group->sid, groups[i].Sid, FIELD_OFFSET( SID, SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] ) );
440 group->enabled = TRUE;
442 group->logon = FALSE;
443 group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) ? TRUE : FALSE;
444 group->owner = groups[i].Attributes & SE_GROUP_OWNER ? TRUE : FALSE;
445 group->resource = FALSE;
446 group->deny_only = FALSE;
447 list_add_tail( &token->groups, &group->entry );
448 /* Use first owner capable group as an owner */
449 if (!token->primary_group && group->owner)
450 token->primary_group = &group->sid;
453 /* copy privileges */
454 for (i = 0; i < priv_count; i++)
456 /* note: we don't check uniqueness: the caller must make sure
457 * privs doesn't contain any duplicate luids */
458 if (!privilege_add( token, &privs[i].Luid,
459 privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
461 release_object( token );
468 token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
469 if (!token->default_dacl)
471 release_object( token );
476 token->source = source;
481 struct token *token_duplicate( struct token *src_token, unsigned primary,
482 SECURITY_IMPERSONATION_LEVEL impersonation_level )
484 const luid_t *modified_id =
485 primary || (impersonation_level == src_token->impersonation_level) ?
486 &src_token->modified_id : NULL;
487 struct token *token = NULL;
488 struct privilege *privilege;
491 if ((impersonation_level < SecurityAnonymous) ||
492 (impersonation_level > SecurityDelegation))
494 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
498 if (primary || (impersonation_level <= src_token->impersonation_level))
499 token = create_token( primary, src_token->user, NULL, 0,
500 NULL, 0, src_token->default_dacl,
501 src_token->source, modified_id,
502 impersonation_level );
503 else set_error( STATUS_BAD_IMPERSONATION_LEVEL );
505 if (!token) return token;
508 LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
510 size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[group->sid.SubAuthorityCount] );
511 struct group *newgroup = mem_alloc( size );
514 release_object( token );
517 memcpy( newgroup, group, size );
518 list_add_tail( &token->groups, &newgroup->entry );
520 token->primary_group = src_token->primary_group;
521 assert( token->primary_group );
523 /* copy privileges */
524 LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
525 if (!privilege_add( token, &privilege->luid, privilege->enabled ))
527 release_object( token );
534 static ACL *create_default_dacl( const SID *user )
536 ACCESS_ALLOWED_ACE *aaa;
539 size_t default_dacl_size = sizeof(ACL) +
540 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
541 sizeof(local_system_sid) +
542 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
544 default_dacl = mem_alloc( default_dacl_size );
545 if (!default_dacl) return NULL;
547 default_dacl->AclRevision = ACL_REVISION;
548 default_dacl->Sbz1 = 0;
549 default_dacl->AclSize = default_dacl_size;
550 default_dacl->AceCount = 2;
551 default_dacl->Sbz2 = 0;
553 /* GENERIC_ALL for Local System */
554 aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
555 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
556 aaa->Header.AceFlags = 0;
557 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
558 sizeof(local_system_sid);
559 aaa->Mask = GENERIC_ALL;
560 sid = (SID *)&aaa->SidStart;
561 memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
563 /* GENERIC_ALL for specified user */
564 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
565 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
566 aaa->Header.AceFlags = 0;
567 aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
568 FIELD_OFFSET( SID, SubAuthority[user->SubAuthorityCount] );
569 aaa->Mask = GENERIC_ALL;
570 sid = (SID *)&aaa->SidStart;
571 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
578 SID_IDENTIFIER_AUTHORITY idauth;
580 unsigned int subauth[MAX_SUBAUTH_COUNT];
583 struct token *token_create_admin( void )
585 struct token *token = NULL;
586 static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
587 static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
588 static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
589 /* on Windows, this value changes every time the user logs on */
590 static const unsigned int logon_subauth[] = { SECURITY_LOGON_IDS_RID, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
591 PSID alias_admins_sid;
592 PSID alias_users_sid;
594 /* note: should be the owner specified in the token */
595 ACL *default_dacl = create_default_dacl( &interactive_sid );
597 alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
598 alias_admins_subauth );
599 alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
600 alias_users_subauth );
601 logon_sid = security_sid_alloc( &nt_authority, sizeof(logon_subauth)/sizeof(logon_subauth[0]),
604 if (alias_admins_sid && alias_users_sid && logon_sid && default_dacl)
606 const LUID_AND_ATTRIBUTES admin_privs[] =
608 { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
609 { SeSecurityPrivilege , 0 },
610 { SeBackupPrivilege , 0 },
611 { SeRestorePrivilege , 0 },
612 { SeSystemtimePrivilege , 0 },
613 { SeShutdownPrivilege , 0 },
614 { SeRemoteShutdownPrivilege , 0 },
615 { SeTakeOwnershipPrivilege , 0 },
616 { SeDebugPrivilege , 0 },
617 { SeSystemEnvironmentPrivilege , 0 },
618 { SeSystemProfilePrivilege , 0 },
619 { SeProfileSingleProcessPrivilege, 0 },
620 { SeIncreaseBasePriorityPrivilege, 0 },
621 { SeLoadDriverPrivilege , SE_PRIVILEGE_ENABLED },
622 { SeCreatePagefilePrivilege , 0 },
623 { SeIncreaseQuotaPrivilege , 0 },
624 { SeUndockPrivilege , 0 },
625 { SeManageVolumePrivilege , 0 },
626 { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
627 { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
629 /* note: we don't include non-builtin groups here for the user -
630 * telling us these is the job of a client-side program */
631 const SID_AND_ATTRIBUTES admin_groups[] =
633 { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
634 { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
635 { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
636 { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
637 { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
638 { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
639 { logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
641 static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}};
642 /* note: we just set the user sid to be the interactive builtin sid -
643 * we should really translate the UNIX user id to a sid */
644 token = create_token( TRUE, &interactive_sid,
645 admin_groups, sizeof(admin_groups)/sizeof(admin_groups[0]),
646 admin_privs, sizeof(admin_privs)/sizeof(admin_privs[0]),
647 default_dacl, admin_source, NULL, -1 );
648 /* we really need a primary group */
649 assert( token->primary_group );
653 free( alias_admins_sid );
654 free( alias_users_sid );
655 free( default_dacl );
660 static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
662 struct privilege *privilege;
663 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
665 if (is_equal_luid( luid, &privilege->luid ))
667 if (enabled_only && !privilege->enabled)
675 static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
676 unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
677 unsigned int mod_privs_count )
679 unsigned int i, modified_count = 0;
681 /* mark as modified */
682 allocate_luid( &token->modified_id );
684 for (i = 0; i < count; i++)
686 struct privilege *privilege =
687 token_find_privilege( token, &privs[i].Luid, FALSE );
690 set_error( STATUS_NOT_ALL_ASSIGNED );
694 if (privs[i].Attributes & SE_PRIVILEGE_REMOVE)
695 privilege_remove( privilege );
698 /* save previous state for caller */
701 luid_and_attr_from_privilege(mod_privs, privilege);
707 if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
708 privilege->enabled = TRUE;
710 privilege->enabled = FALSE;
713 return modified_count;
716 static void token_disable_privileges( struct token *token )
718 struct privilege *privilege;
720 /* mark as modified */
721 allocate_luid( &token->modified_id );
723 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
724 privilege->enabled = FALSE;
727 int token_check_privileges( struct token *token, int all_required,
728 const LUID_AND_ATTRIBUTES *reqprivs,
729 unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
731 unsigned int i, enabled_count = 0;
733 for (i = 0; i < count; i++)
735 struct privilege *privilege =
736 token_find_privilege( token, &reqprivs[i].Luid, TRUE );
739 usedprivs[i] = reqprivs[i];
741 if (privilege && privilege->enabled)
745 usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
750 return (enabled_count == count);
752 return (enabled_count > 0);
755 static int token_sid_present( struct token *token, const SID *sid, int deny )
759 if (security_equal_sid( token->user, sid )) return TRUE;
761 LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
763 if (!group->enabled) continue;
764 if (group->deny_only && !deny) continue;
766 if (security_equal_sid( &group->sid, sid )) return TRUE;
772 /* Checks access to a security descriptor. 'sd' must have been validated by
773 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
774 * the reason. 'status' parameter will indicate if access is granted or denied.
776 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
778 static unsigned int token_access_check( struct token *token,
779 const struct security_descriptor *sd,
780 unsigned int desired_access,
781 LUID_AND_ATTRIBUTES *privs,
782 unsigned int *priv_count,
783 const GENERIC_MAPPING *mapping,
784 unsigned int *granted_access,
785 unsigned int *status )
787 unsigned int current_access = 0;
788 unsigned int denied_access = 0;
792 const ACE_HEADER *ace;
795 /* assume no access rights */
798 /* fail if desired_access contains generic rights */
799 if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
802 return STATUS_GENERIC_NOT_MAPPED;
805 dacl = sd_get_dacl( sd, &dacl_present );
806 owner = sd_get_owner( sd );
807 if (!owner || !sd_get_group( sd ))
810 return STATUS_INVALID_SECURITY_DESCR;
813 /* 1: Grant desired access if the object is unprotected */
817 *granted_access = desired_access;
818 return *status = STATUS_SUCCESS;
823 *status = STATUS_ACCESS_DENIED;
824 return STATUS_SUCCESS;
827 /* 2: Check if caller wants access to system security part. Note: access
828 * is only granted if specifically asked for */
829 if (desired_access & ACCESS_SYSTEM_SECURITY)
831 const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
832 LUID_AND_ATTRIBUTES retpriv = security_priv;
833 if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
837 /* assumes that there will only be one privilege to return */
838 if (*priv_count >= 1)
846 return STATUS_BUFFER_TOO_SMALL;
849 current_access |= ACCESS_SYSTEM_SECURITY;
850 if (desired_access == current_access)
852 *granted_access = current_access;
853 return *status = STATUS_SUCCESS;
859 *status = STATUS_PRIVILEGE_NOT_HELD;
860 return STATUS_SUCCESS;
863 else if (priv_count) *priv_count = 0;
865 /* 3: Check whether the token is the owner */
866 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
867 * checked when a "set owner" call is made, overriding the access rights
868 * determined here. */
869 if (token_sid_present( token, owner, FALSE ))
871 current_access |= (READ_CONTROL | WRITE_DAC);
872 if (desired_access == current_access)
874 *granted_access = current_access;
875 return *status = STATUS_SUCCESS;
879 /* 4: Grant rights according to the DACL */
880 ace = (const ACE_HEADER *)(dacl + 1);
881 for (i = 0; i < dacl->AceCount; i++)
883 const ACCESS_ALLOWED_ACE *aa_ace;
884 const ACCESS_DENIED_ACE *ad_ace;
886 switch (ace->AceType)
888 case ACCESS_DENIED_ACE_TYPE:
889 ad_ace = (const ACCESS_DENIED_ACE *)ace;
890 sid = (const SID *)&ad_ace->SidStart;
891 if (token_sid_present( token, sid, TRUE ))
893 unsigned int access = ad_ace->Mask;
894 map_generic_mask(&access, mapping);
895 if (desired_access & MAXIMUM_ALLOWED)
896 denied_access |= access;
899 denied_access |= (access & ~current_access);
900 if (desired_access & access) goto done;
904 case ACCESS_ALLOWED_ACE_TYPE:
905 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
906 sid = (const SID *)&aa_ace->SidStart;
907 if (token_sid_present( token, sid, FALSE ))
909 unsigned int access = aa_ace->Mask;
910 map_generic_mask(&access, mapping);
911 if (desired_access & MAXIMUM_ALLOWED)
912 current_access |= access;
914 current_access |= (access & ~denied_access);
919 /* don't bother carrying on checking if we've already got all of
921 if (desired_access == *granted_access)
924 ace = ace_next( ace );
928 if (desired_access & MAXIMUM_ALLOWED)
929 *granted_access = current_access & ~denied_access;
931 if ((current_access & desired_access) == desired_access)
932 *granted_access = current_access & desired_access;
936 *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
937 return STATUS_SUCCESS;
940 const ACL *token_get_default_dacl( struct token *token )
942 return token->default_dacl;
945 const SID *token_get_user( struct token *token )
950 const SID *token_get_primary_group( struct token *token )
952 return token->primary_group;
955 int check_object_access(struct object *obj, unsigned int *access)
957 GENERIC_MAPPING mapping;
958 struct token *token = current->token ? current->token : current->process->token;
959 LUID_AND_ATTRIBUTES priv;
960 unsigned int status, priv_count = 1;
963 mapping.GenericAll = obj->ops->map_access( obj, GENERIC_ALL );
967 if (*access & MAXIMUM_ALLOWED)
968 *access = mapping.GenericAll;
972 mapping.GenericRead = obj->ops->map_access( obj, GENERIC_READ );
973 mapping.GenericWrite = obj->ops->map_access( obj, GENERIC_WRITE );
974 mapping.GenericExecute = obj->ops->map_access( obj, GENERIC_EXECUTE );
976 res = token_access_check( token, obj->sd, *access, &priv, &priv_count,
977 &mapping, access, &status ) == STATUS_SUCCESS &&
978 status == STATUS_SUCCESS;
980 if (!res) set_error( STATUS_ACCESS_DENIED );
985 /* open a security token */
986 DECL_HANDLER(open_token)
988 if (req->flags & OPEN_TOKEN_THREAD)
990 struct thread *thread = get_thread_from_handle( req->handle, 0 );
995 if (thread->token->impersonation_level <= SecurityAnonymous)
996 set_error( STATUS_CANT_OPEN_ANONYMOUS );
998 reply->token = alloc_handle( current->process, thread->token,
999 req->access, req->attributes );
1002 set_error( STATUS_NO_TOKEN );
1003 release_object( thread );
1008 struct process *process = get_process_from_handle( req->handle, 0 );
1012 reply->token = alloc_handle( current->process, process->token, req->access,
1015 set_error( STATUS_NO_TOKEN );
1016 release_object( process );
1021 /* adjust the privileges held by a token */
1022 DECL_HANDLER(adjust_token_privileges)
1024 struct token *token;
1025 unsigned int access = TOKEN_ADJUST_PRIVILEGES;
1027 if (req->get_modified_state) access |= TOKEN_QUERY;
1029 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1030 access, &token_ops )))
1032 const LUID_AND_ATTRIBUTES *privs = get_req_data();
1033 LUID_AND_ATTRIBUTES *modified_privs = NULL;
1034 unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1035 unsigned int modified_priv_count = 0;
1037 if (req->get_modified_state && !req->disable_all)
1040 /* count modified privs */
1041 for (i = 0; i < priv_count; i++)
1043 struct privilege *privilege =
1044 token_find_privilege( token, &privs[i].Luid, FALSE );
1045 if (privilege && req->get_modified_state)
1046 modified_priv_count++;
1048 reply->len = modified_priv_count;
1049 modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
1050 if (modified_priv_count)
1051 modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
1053 reply->len = modified_priv_count * sizeof(*modified_privs);
1055 if (req->disable_all)
1056 token_disable_privileges( token );
1058 modified_priv_count = token_adjust_privileges( token, privs,
1059 priv_count, modified_privs, modified_priv_count );
1061 release_object( token );
1065 /* retrieves the list of privileges that may be held be the token */
1066 DECL_HANDLER(get_token_privileges)
1068 struct token *token;
1070 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1075 LUID_AND_ATTRIBUTES *privs;
1076 struct privilege *privilege;
1078 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1081 reply->len = priv_count * sizeof(*privs);
1082 if (reply->len <= get_reply_max_size())
1084 privs = set_reply_data_size( priv_count * sizeof(*privs) );
1088 LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
1090 luid_and_attr_from_privilege( &privs[i], privilege );
1096 set_error(STATUS_BUFFER_TOO_SMALL);
1098 release_object( token );
1102 /* creates a duplicate of the token */
1103 DECL_HANDLER(duplicate_token)
1105 struct token *src_token;
1107 if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
1111 struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level );
1114 reply->new_handle = alloc_handle( current->process, token, req->access, req->attributes);
1115 release_object( token );
1117 release_object( src_token );
1121 /* checks the specified privileges are held by the token */
1122 DECL_HANDLER(check_token_privileges)
1124 struct token *token;
1126 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1130 unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
1132 if (!token->primary && token->impersonation_level <= SecurityAnonymous)
1133 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1134 else if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
1136 LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
1137 reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
1140 set_error( STATUS_BUFFER_OVERFLOW );
1141 release_object( token );
1145 /* checks that a user represented by a token is allowed to access an object
1146 * represented by a security descriptor */
1147 DECL_HANDLER(access_check)
1149 data_size_t sd_size = get_req_data_size();
1150 const struct security_descriptor *sd = get_req_data();
1151 struct token *token;
1153 if (!sd_is_valid( sd, sd_size ))
1155 set_error( STATUS_ACCESS_VIOLATION );
1159 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1163 GENERIC_MAPPING mapping;
1164 unsigned int status;
1165 LUID_AND_ATTRIBUTES priv;
1166 unsigned int priv_count = 1;
1168 memset(&priv, 0, sizeof(priv));
1170 /* only impersonation tokens may be used with this function */
1173 set_error( STATUS_NO_IMPERSONATION_TOKEN );
1174 release_object( token );
1177 /* anonymous impersonation tokens can't be used */
1178 if (token->impersonation_level <= SecurityAnonymous)
1180 set_error( STATUS_BAD_IMPERSONATION_LEVEL );
1181 release_object( token );
1185 mapping.GenericRead = req->mapping_read;
1186 mapping.GenericWrite = req->mapping_write;
1187 mapping.GenericExecute = req->mapping_execute;
1188 mapping.GenericAll = req->mapping_all;
1190 status = token_access_check(
1191 token, sd, req->desired_access, &priv, &priv_count, &mapping,
1192 &reply->access_granted, &reply->access_status );
1194 reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
1196 if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
1198 LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
1199 memcpy( privs, &priv, sizeof(priv) );
1202 set_error( status );
1203 release_object( token );
1207 /* retrieves the SID of the user that the token represents */
1208 DECL_HANDLER(get_token_user)
1210 struct token *token;
1212 reply->user_len = 0;
1214 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1218 const SID *user = token->user;
1220 reply->user_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
1221 if (reply->user_len <= get_reply_max_size())
1223 SID *user_reply = set_reply_data_size( reply->user_len );
1225 memcpy( user_reply, user, reply->user_len );
1227 else set_error( STATUS_BUFFER_TOO_SMALL );
1229 release_object( token );
1233 /* retrieves the groups that the user represented by the token belongs to */
1234 DECL_HANDLER(get_token_groups)
1236 struct token *token;
1238 reply->user_len = 0;
1240 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1244 size_t size_needed = sizeof(struct token_groups);
1245 unsigned int group_count = 0;
1246 const struct group *group;
1248 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1251 size_needed += FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]);
1253 size_needed += sizeof(unsigned int) * group_count;
1255 reply->user_len = size_needed;
1257 if (size_needed <= get_reply_max_size())
1259 struct token_groups *tg = set_reply_data_size( size_needed );
1262 unsigned int *attr_ptr = (unsigned int *)(tg + 1);
1263 SID *sid_ptr = (SID *)(attr_ptr + group_count);
1265 tg->count = group_count;
1267 LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
1271 if (group->mandatory) *attr_ptr |= SE_GROUP_MANDATORY;
1272 if (group->def) *attr_ptr |= SE_GROUP_ENABLED_BY_DEFAULT;
1273 if (group->enabled) *attr_ptr |= SE_GROUP_ENABLED;
1274 if (group->owner) *attr_ptr |= SE_GROUP_OWNER;
1275 if (group->deny_only) *attr_ptr |= SE_GROUP_USE_FOR_DENY_ONLY;
1276 if (group->resource) *attr_ptr |= SE_GROUP_RESOURCE;
1278 memcpy(sid_ptr, &group->sid, FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]));
1280 sid_ptr = (SID *)((char *)sid_ptr + FIELD_OFFSET(SID, SubAuthority[group->sid.SubAuthorityCount]));
1285 else set_error( STATUS_BUFFER_TOO_SMALL );
1287 release_object( token );
1291 DECL_HANDLER(get_token_impersonation_level)
1293 struct token *token;
1295 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1300 set_error( STATUS_INVALID_PARAMETER );
1302 reply->impersonation_level = token->impersonation_level;
1304 release_object( token );
1308 DECL_HANDLER(get_token_statistics)
1310 struct token *token;
1312 if ((token = (struct token *)get_handle_obj( current->process, req->handle,
1316 reply->token_id = token->token_id;
1317 reply->modified_id = token->modified_id;
1318 reply->primary = token->primary;
1319 reply->impersonation_level = token->impersonation_level;
1320 reply->group_count = list_count( &token->groups );
1321 reply->privilege_count = list_count( &token->privileges );
1323 release_object( token );