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
32 #define WIN32_NO_STATUS
42 #include "wine/unicode.h"
44 #define MAX_SUBAUTH_COUNT 1
46 const LUID SeIncreaseQuotaPrivilege
= { 5, 0 };
47 const LUID SeSecurityPrivilege
= { 8, 0 };
48 const LUID SeTakeOwnershipPrivilege
= { 9, 0 };
49 const LUID SeLoadDriverPrivilege
= { 10, 0 };
50 const LUID SeSystemProfilePrivilege
= { 11, 0 };
51 const LUID SeSystemtimePrivilege
= { 12, 0 };
52 const LUID SeProfileSingleProcessPrivilege
= { 13, 0 };
53 const LUID SeIncreaseBasePriorityPrivilege
= { 14, 0 };
54 const LUID SeCreatePagefilePrivilege
= { 15, 0 };
55 const LUID SeBackupPrivilege
= { 17, 0 };
56 const LUID SeRestorePrivilege
= { 18, 0 };
57 const LUID SeShutdownPrivilege
= { 19, 0 };
58 const LUID SeDebugPrivilege
= { 20, 0 };
59 const LUID SeSystemEnvironmentPrivilege
= { 22, 0 };
60 const LUID SeChangeNotifyPrivilege
= { 23, 0 };
61 const LUID SeRemoteShutdownPrivilege
= { 24, 0 };
62 const LUID SeUndockPrivilege
= { 25, 0 };
63 const LUID SeManageVolumePrivilege
= { 28, 0 };
64 const LUID SeImpersonatePrivilege
= { 29, 0 };
65 const LUID SeCreateGlobalPrivilege
= { 30, 0 };
67 static const SID world_sid
= { SID_REVISION
, 1, { SECURITY_WORLD_SID_AUTHORITY
}, { SECURITY_WORLD_RID
} };
68 static const SID local_sid
= { SID_REVISION
, 1, { SECURITY_LOCAL_SID_AUTHORITY
}, { SECURITY_LOCAL_RID
} };
69 static const SID interactive_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_INTERACTIVE_RID
} };
70 static const SID anonymous_logon_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_ANONYMOUS_LOGON_RID
} };
71 static const SID authenticated_user_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_AUTHENTICATED_USER_RID
} };
72 static const SID local_system_sid
= { SID_REVISION
, 1, { SECURITY_NT_AUTHORITY
}, { SECURITY_LOCAL_SYSTEM_RID
} };
73 static const struct /* same fields as struct SID */
76 BYTE SubAuthorityCount
;
77 SID_IDENTIFIER_AUTHORITY IdentifierAuthority
;
78 DWORD SubAuthority
[5];
79 } local_user_sid
= { SID_REVISION
, 5, { SECURITY_NT_AUTHORITY
}, { SECURITY_NT_NON_UNIQUE
, 0, 0, 0, 1000 } };
81 const PSID security_world_sid
= (PSID
)&world_sid
;
82 static const PSID security_local_sid
= (PSID
)&local_sid
;
83 static const PSID security_interactive_sid
= (PSID
)&interactive_sid
;
84 static const PSID security_authenticated_user_sid
= (PSID
)&authenticated_user_sid
;
85 const PSID security_local_system_sid
= (PSID
)&local_system_sid
;
86 const PSID security_local_user_sid
= (PSID
)&local_user_sid
;
88 static luid_t prev_luid_value
= { 1000, 0 };
92 struct object obj
; /* object header */
93 luid_t token_id
; /* system-unique id of token */
94 luid_t modified_id
; /* new id allocated every time token is modified */
95 struct list privileges
; /* privileges available to the token */
96 struct list groups
; /* groups that the user of this token belongs to (sid_and_attributes) */
97 SID
*user
; /* SID of user this token represents */
98 SID
*primary_group
; /* SID of user's primary group */
99 unsigned primary
; /* is this a primary or impersonation token? */
100 ACL
*default_dacl
; /* the default DACL to assign to objects created by this user */
101 TOKEN_SOURCE source
; /* source of the token */
102 int impersonation_level
; /* impersonation level this token is capable of if non-primary token */
109 unsigned enabled
: 1; /* is the privilege currently enabled? */
110 unsigned def
: 1; /* is the privilege enabled by default? */
116 unsigned enabled
: 1; /* is the sid currently enabled? */
117 unsigned def
: 1; /* is the sid enabled by default? */
118 unsigned logon
: 1; /* is this a logon sid? */
119 unsigned mandatory
: 1; /* is this sid always enabled? */
120 unsigned owner
: 1; /* can this sid be an owner of an object? */
121 unsigned resource
: 1; /* is this a domain-local group? */
122 unsigned deny_only
: 1; /* is this a sid that should be use for denying only? */
126 static void token_dump( struct object
*obj
, int verbose
);
127 static unsigned int token_map_access( struct object
*obj
, unsigned int access
);
128 static void token_destroy( struct object
*obj
);
130 static const struct object_ops token_ops
=
132 sizeof(struct token
), /* size */
133 token_dump
, /* dump */
134 no_get_type
, /* get_type */
135 no_add_queue
, /* add_queue */
136 NULL
, /* remove_queue */
138 NULL
, /* satisfied */
139 no_signal
, /* signal */
140 no_get_fd
, /* get_fd */
141 token_map_access
, /* map_access */
142 default_get_sd
, /* get_sd */
143 default_set_sd
, /* set_sd */
144 no_lookup_name
, /* lookup_name */
145 no_open_file
, /* open_file */
146 no_close_handle
, /* close_handle */
147 token_destroy
/* destroy */
151 static void token_dump( struct object
*obj
, int verbose
)
153 fprintf( stderr
, "Security token\n" );
154 /* FIXME: dump token members */
157 static unsigned int token_map_access( struct object
*obj
, unsigned int access
)
159 if (access
& GENERIC_READ
) access
|= TOKEN_READ
;
160 if (access
& GENERIC_WRITE
) access
|= TOKEN_WRITE
;
161 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
162 if (access
& GENERIC_ALL
) access
|= TOKEN_ALL_ACCESS
;
163 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
166 static SID
*security_sid_alloc( const SID_IDENTIFIER_AUTHORITY
*idauthority
, int subauthcount
, const unsigned int subauth
[] )
169 SID
*sid
= mem_alloc( FIELD_OFFSET(SID
, SubAuthority
[subauthcount
]) );
170 if (!sid
) return NULL
;
171 sid
->Revision
= SID_REVISION
;
172 sid
->SubAuthorityCount
= subauthcount
;
173 sid
->IdentifierAuthority
= *idauthority
;
174 for (i
= 0; i
< subauthcount
; i
++)
175 sid
->SubAuthority
[i
] = subauth
[i
];
179 void security_set_thread_token( struct thread
*thread
, obj_handle_t handle
)
184 release_object( thread
->token
);
185 thread
->token
= NULL
;
189 struct token
*token
= (struct token
*)get_handle_obj( current
->process
,
196 release_object( thread
->token
);
197 thread
->token
= token
;
202 const SID
*security_unix_uid_to_sid( uid_t uid
)
204 /* very simple mapping: either the current user or not the current user */
206 return (const SID
*)&local_user_sid
;
208 return &anonymous_logon_sid
;
211 static int acl_is_valid( const ACL
*acl
, data_size_t size
)
214 const ACE_HEADER
*ace
;
216 if (size
< sizeof(ACL
))
219 size
= min(size
, MAX_ACL_LEN
);
223 ace
= (const ACE_HEADER
*)(acl
+ 1);
224 for (i
= 0; i
< acl
->AceCount
; i
++)
227 data_size_t sid_size
;
229 if (size
< sizeof(ACE_HEADER
))
231 if (size
< ace
->AceSize
)
233 size
-= ace
->AceSize
;
234 switch (ace
->AceType
)
236 case ACCESS_DENIED_ACE_TYPE
:
237 sid
= (const SID
*)&((const ACCESS_DENIED_ACE
*)ace
)->SidStart
;
238 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
);
240 case ACCESS_ALLOWED_ACE_TYPE
:
241 sid
= (const SID
*)&((const ACCESS_ALLOWED_ACE
*)ace
)->SidStart
;
242 sid_size
= ace
->AceSize
- FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
);
244 case SYSTEM_AUDIT_ACE_TYPE
:
245 sid
= (const SID
*)&((const SYSTEM_AUDIT_ACE
*)ace
)->SidStart
;
246 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_AUDIT_ACE
, SidStart
);
248 case SYSTEM_ALARM_ACE_TYPE
:
249 sid
= (const SID
*)&((const SYSTEM_ALARM_ACE
*)ace
)->SidStart
;
250 sid_size
= ace
->AceSize
- FIELD_OFFSET(SYSTEM_ALARM_ACE
, SidStart
);
255 if (sid_size
< FIELD_OFFSET(SID
, SubAuthority
[0]) ||
256 sid_size
< FIELD_OFFSET(SID
, SubAuthority
[sid
->SubAuthorityCount
]))
258 ace
= ace_next( ace
);
263 /* checks whether all members of a security descriptor fit inside the size
264 * of memory specified */
265 int sd_is_valid( const struct security_descriptor
*sd
, data_size_t size
)
267 size_t offset
= sizeof(struct security_descriptor
);
277 if ((sd
->owner_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
278 (offset
+ sd
->owner_len
> size
))
280 owner
= sd_get_owner( sd
);
283 size_t needed_size
= FIELD_OFFSET(SID
, SubAuthority
[owner
->SubAuthorityCount
]);
284 if ((sd
->owner_len
< sizeof(SID
)) || (needed_size
> sd
->owner_len
))
287 offset
+= sd
->owner_len
;
289 if ((sd
->group_len
>= FIELD_OFFSET(SID
, SubAuthority
[255])) ||
290 (offset
+ sd
->group_len
> size
))
292 group
= sd_get_group( sd
);
295 size_t needed_size
= FIELD_OFFSET(SID
, SubAuthority
[group
->SubAuthorityCount
]);
296 if ((sd
->group_len
< sizeof(SID
)) || (needed_size
> sd
->group_len
))
299 offset
+= sd
->group_len
;
301 if ((sd
->sacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->sacl_len
> size
))
303 sacl
= sd_get_sacl( sd
, &dummy
);
304 if (sacl
&& !acl_is_valid( sacl
, sd
->sacl_len
))
306 offset
+= sd
->sacl_len
;
308 if ((sd
->dacl_len
>= MAX_ACL_LEN
) || (offset
+ sd
->dacl_len
> size
))
310 dacl
= sd_get_dacl( sd
, &dummy
);
311 if (dacl
&& !acl_is_valid( dacl
, sd
->dacl_len
))
313 offset
+= sd
->dacl_len
;
318 /* determines whether an object_attributes struct is valid in a buffer
319 * and calls set_error appropriately */
320 int objattr_is_valid( const struct object_attributes
*objattr
, data_size_t size
)
322 if ((size
< sizeof(*objattr
)) || (size
- sizeof(*objattr
) < objattr
->sd_len
) ||
323 (size
- sizeof(*objattr
) - objattr
->sd_len
< objattr
->name_len
))
325 set_error( STATUS_ACCESS_VIOLATION
);
331 const struct security_descriptor
*sd
= (const struct security_descriptor
*)(objattr
+ 1);
332 if (!sd_is_valid( sd
, objattr
->sd_len
))
334 set_error( STATUS_INVALID_SECURITY_DESCR
);
342 /* maps from generic rights to specific rights as given by a mapping */
343 static inline void map_generic_mask(unsigned int *mask
, const GENERIC_MAPPING
*mapping
)
345 if (*mask
& GENERIC_READ
) *mask
|= mapping
->GenericRead
;
346 if (*mask
& GENERIC_WRITE
) *mask
|= mapping
->GenericWrite
;
347 if (*mask
& GENERIC_EXECUTE
) *mask
|= mapping
->GenericExecute
;
348 if (*mask
& GENERIC_ALL
) *mask
|= mapping
->GenericAll
;
352 static inline int is_equal_luid( const LUID
*luid1
, const LUID
*luid2
)
354 return (luid1
->LowPart
== luid2
->LowPart
&& luid1
->HighPart
== luid2
->HighPart
);
357 static inline void allocate_luid( luid_t
*luid
)
359 prev_luid_value
.low_part
++;
360 *luid
= prev_luid_value
;
363 DECL_HANDLER( allocate_locally_unique_id
)
365 allocate_luid( &reply
->luid
);
368 static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES
*out
, const struct privilege
*in
)
370 out
->Luid
= in
->luid
;
372 (in
->enabled
? SE_PRIVILEGE_ENABLED
: 0) |
373 (in
->def
? SE_PRIVILEGE_ENABLED_BY_DEFAULT
: 0);
376 static struct privilege
*privilege_add( struct token
*token
, const LUID
*luid
, int enabled
)
378 struct privilege
*privilege
= mem_alloc( sizeof(*privilege
) );
381 privilege
->luid
= *luid
;
382 privilege
->def
= privilege
->enabled
= (enabled
!= 0);
383 list_add_tail( &token
->privileges
, &privilege
->entry
);
388 static inline void privilege_remove( struct privilege
*privilege
)
390 list_remove( &privilege
->entry
);
394 static void token_destroy( struct object
*obj
)
397 struct list
*cursor
, *cursor_next
;
399 assert( obj
->ops
== &token_ops
);
400 token
= (struct token
*)obj
;
404 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->privileges
)
406 struct privilege
*privilege
= LIST_ENTRY( cursor
, struct privilege
, entry
);
407 privilege_remove( privilege
);
410 LIST_FOR_EACH_SAFE( cursor
, cursor_next
, &token
->groups
)
412 struct group
*group
= LIST_ENTRY( cursor
, struct group
, entry
);
413 list_remove( &group
->entry
);
417 free( token
->default_dacl
);
420 /* creates a new token.
421 * groups may be NULL if group_count is 0.
422 * privs may be NULL if priv_count is 0.
423 * default_dacl may be NULL, indicating that all objects created by the user
425 * modified_id may be NULL, indicating that a new modified_id luid should be
428 static struct token
*create_token( unsigned primary
, const SID
*user
,
429 const SID_AND_ATTRIBUTES
*groups
, unsigned int group_count
,
430 const LUID_AND_ATTRIBUTES
*privs
, unsigned int priv_count
,
431 const ACL
*default_dacl
, TOKEN_SOURCE source
,
432 const luid_t
*modified_id
,
433 int impersonation_level
)
435 struct token
*token
= alloc_object( &token_ops
);
440 allocate_luid( &token
->token_id
);
442 token
->modified_id
= *modified_id
;
444 allocate_luid( &token
->modified_id
);
445 list_init( &token
->privileges
);
446 list_init( &token
->groups
);
447 token
->primary
= primary
;
448 /* primary tokens don't have impersonation levels */
450 token
->impersonation_level
= -1;
452 token
->impersonation_level
= impersonation_level
;
453 token
->default_dacl
= NULL
;
454 token
->primary_group
= NULL
;
457 token
->user
= memdup( user
, FIELD_OFFSET(SID
, SubAuthority
[user
->SubAuthorityCount
]) );
460 release_object( token
);
465 for (i
= 0; i
< group_count
; i
++)
467 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[((const SID
*)groups
[i
].Sid
)->SubAuthorityCount
] );
468 struct group
*group
= mem_alloc( size
);
472 release_object( token
);
475 memcpy( &group
->sid
, groups
[i
].Sid
, FIELD_OFFSET( SID
, SubAuthority
[((const SID
*)groups
[i
].Sid
)->SubAuthorityCount
] ) );
476 group
->enabled
= TRUE
;
478 group
->logon
= (groups
[i
].Attributes
& SE_GROUP_LOGON_ID
) ? TRUE
: FALSE
;
479 group
->mandatory
= (groups
[i
].Attributes
& SE_GROUP_MANDATORY
) ? TRUE
: FALSE
;
480 group
->owner
= groups
[i
].Attributes
& SE_GROUP_OWNER
? TRUE
: FALSE
;
481 group
->resource
= FALSE
;
482 group
->deny_only
= FALSE
;
483 list_add_tail( &token
->groups
, &group
->entry
);
484 /* Use first owner capable group as an owner */
485 if (!token
->primary_group
&& group
->owner
)
486 token
->primary_group
= &group
->sid
;
489 /* copy privileges */
490 for (i
= 0; i
< priv_count
; i
++)
492 /* note: we don't check uniqueness: the caller must make sure
493 * privs doesn't contain any duplicate luids */
494 if (!privilege_add( token
, &privs
[i
].Luid
,
495 privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
))
497 release_object( token
);
504 token
->default_dacl
= memdup( default_dacl
, default_dacl
->AclSize
);
505 if (!token
->default_dacl
)
507 release_object( token
);
512 token
->source
= source
;
517 struct token
*token_duplicate( struct token
*src_token
, unsigned primary
,
518 int impersonation_level
)
520 const luid_t
*modified_id
=
521 primary
|| (impersonation_level
== src_token
->impersonation_level
) ?
522 &src_token
->modified_id
: NULL
;
523 struct token
*token
= NULL
;
524 struct privilege
*privilege
;
528 (impersonation_level
< SecurityAnonymous
||
529 impersonation_level
> SecurityDelegation
||
530 (!src_token
->primary
&& (impersonation_level
> src_token
->impersonation_level
))))
532 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
536 token
= create_token( primary
, src_token
->user
, NULL
, 0,
537 NULL
, 0, src_token
->default_dacl
,
538 src_token
->source
, modified_id
,
539 impersonation_level
);
540 if (!token
) return token
;
543 LIST_FOR_EACH_ENTRY( group
, &src_token
->groups
, struct group
, entry
)
545 size_t size
= FIELD_OFFSET( struct group
, sid
.SubAuthority
[group
->sid
.SubAuthorityCount
] );
546 struct group
*newgroup
= mem_alloc( size
);
549 release_object( token
);
552 memcpy( newgroup
, group
, size
);
553 list_add_tail( &token
->groups
, &newgroup
->entry
);
555 token
->primary_group
= src_token
->primary_group
;
556 assert( token
->primary_group
);
558 /* copy privileges */
559 LIST_FOR_EACH_ENTRY( privilege
, &src_token
->privileges
, struct privilege
, entry
)
560 if (!privilege_add( token
, &privilege
->luid
, privilege
->enabled
))
562 release_object( token
);
569 static ACL
*create_default_dacl( const SID
*user
)
571 ACCESS_ALLOWED_ACE
*aaa
;
574 size_t default_dacl_size
= sizeof(ACL
) +
575 2*(sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
576 sizeof(local_system_sid
) +
577 FIELD_OFFSET(SID
, SubAuthority
[user
->SubAuthorityCount
]);
579 default_dacl
= mem_alloc( default_dacl_size
);
580 if (!default_dacl
) return NULL
;
582 default_dacl
->AclRevision
= ACL_REVISION
;
583 default_dacl
->Sbz1
= 0;
584 default_dacl
->AclSize
= default_dacl_size
;
585 default_dacl
->AceCount
= 2;
586 default_dacl
->Sbz2
= 0;
588 /* GENERIC_ALL for Local System */
589 aaa
= (ACCESS_ALLOWED_ACE
*)(default_dacl
+ 1);
590 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
591 aaa
->Header
.AceFlags
= 0;
592 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
593 sizeof(local_system_sid
);
594 aaa
->Mask
= GENERIC_ALL
;
595 sid
= (SID
*)&aaa
->SidStart
;
596 memcpy( sid
, &local_system_sid
, sizeof(local_system_sid
) );
598 /* GENERIC_ALL for specified user */
599 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
600 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
601 aaa
->Header
.AceFlags
= 0;
602 aaa
->Header
.AceSize
= (sizeof(ACCESS_ALLOWED_ACE
) - sizeof(DWORD
)) +
603 FIELD_OFFSET( SID
, SubAuthority
[user
->SubAuthorityCount
] );
604 aaa
->Mask
= GENERIC_ALL
;
605 sid
= (SID
*)&aaa
->SidStart
;
606 memcpy( sid
, user
, FIELD_OFFSET(SID
, SubAuthority
[user
->SubAuthorityCount
]) );
613 SID_IDENTIFIER_AUTHORITY idauth
;
615 unsigned int subauth
[MAX_SUBAUTH_COUNT
];
618 struct token
*token_create_admin( void )
620 struct token
*token
= NULL
;
621 static const SID_IDENTIFIER_AUTHORITY nt_authority
= { SECURITY_NT_AUTHORITY
};
622 static const unsigned int alias_admins_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
};
623 static const unsigned int alias_users_subauth
[] = { SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_USERS
};
624 /* on Windows, this value changes every time the user logs on */
625 static const unsigned int logon_subauth
[] = { SECURITY_LOGON_IDS_RID
, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
626 PSID alias_admins_sid
;
627 PSID alias_users_sid
;
629 /* note: should be the owner specified in the token */
630 ACL
*default_dacl
= create_default_dacl( &interactive_sid
);
632 alias_admins_sid
= security_sid_alloc( &nt_authority
, sizeof(alias_admins_subauth
)/sizeof(alias_admins_subauth
[0]),
633 alias_admins_subauth
);
634 alias_users_sid
= security_sid_alloc( &nt_authority
, sizeof(alias_users_subauth
)/sizeof(alias_users_subauth
[0]),
635 alias_users_subauth
);
636 logon_sid
= security_sid_alloc( &nt_authority
, sizeof(logon_subauth
)/sizeof(logon_subauth
[0]),
639 if (alias_admins_sid
&& alias_users_sid
&& logon_sid
&& default_dacl
)
641 const LUID_AND_ATTRIBUTES admin_privs
[] =
643 { SeChangeNotifyPrivilege
, SE_PRIVILEGE_ENABLED
},
644 { SeSecurityPrivilege
, 0 },
645 { SeBackupPrivilege
, 0 },
646 { SeRestorePrivilege
, 0 },
647 { SeSystemtimePrivilege
, 0 },
648 { SeShutdownPrivilege
, 0 },
649 { SeRemoteShutdownPrivilege
, 0 },
650 { SeTakeOwnershipPrivilege
, 0 },
651 { SeDebugPrivilege
, 0 },
652 { SeSystemEnvironmentPrivilege
, 0 },
653 { SeSystemProfilePrivilege
, 0 },
654 { SeProfileSingleProcessPrivilege
, 0 },
655 { SeIncreaseBasePriorityPrivilege
, 0 },
656 { SeLoadDriverPrivilege
, SE_PRIVILEGE_ENABLED
},
657 { SeCreatePagefilePrivilege
, 0 },
658 { SeIncreaseQuotaPrivilege
, 0 },
659 { SeUndockPrivilege
, 0 },
660 { SeManageVolumePrivilege
, 0 },
661 { SeImpersonatePrivilege
, SE_PRIVILEGE_ENABLED
},
662 { SeCreateGlobalPrivilege
, SE_PRIVILEGE_ENABLED
},
664 /* note: we don't include non-builtin groups here for the user -
665 * telling us these is the job of a client-side program */
666 const SID_AND_ATTRIBUTES admin_groups
[] =
668 { security_world_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
669 { security_local_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
670 { security_interactive_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
671 { security_authenticated_user_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
672 { alias_admins_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_OWNER
},
673 { alias_users_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
},
674 { logon_sid
, SE_GROUP_ENABLED
|SE_GROUP_ENABLED_BY_DEFAULT
|SE_GROUP_MANDATORY
|SE_GROUP_LOGON_ID
},
676 static const TOKEN_SOURCE admin_source
= {"SeMgr", {0, 0}};
677 token
= create_token( TRUE
, security_unix_uid_to_sid( getuid() ),
678 admin_groups
, sizeof(admin_groups
)/sizeof(admin_groups
[0]),
679 admin_privs
, sizeof(admin_privs
)/sizeof(admin_privs
[0]),
680 default_dacl
, admin_source
, NULL
, -1 );
681 /* we really need a primary group */
682 assert( token
->primary_group
);
686 free( alias_admins_sid
);
687 free( alias_users_sid
);
688 free( default_dacl
);
693 static struct privilege
*token_find_privilege( struct token
*token
, const LUID
*luid
, int enabled_only
)
695 struct privilege
*privilege
;
696 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
698 if (is_equal_luid( luid
, &privilege
->luid
))
700 if (enabled_only
&& !privilege
->enabled
)
708 static unsigned int token_adjust_privileges( struct token
*token
, const LUID_AND_ATTRIBUTES
*privs
,
709 unsigned int count
, LUID_AND_ATTRIBUTES
*mod_privs
,
710 unsigned int mod_privs_count
)
712 unsigned int i
, modified_count
= 0;
714 /* mark as modified */
715 allocate_luid( &token
->modified_id
);
717 for (i
= 0; i
< count
; i
++)
719 struct privilege
*privilege
=
720 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
723 set_error( STATUS_NOT_ALL_ASSIGNED
);
727 if (privs
[i
].Attributes
& SE_PRIVILEGE_REMOVED
)
728 privilege_remove( privilege
);
731 /* save previous state for caller */
734 luid_and_attr_from_privilege(mod_privs
, privilege
);
740 if (privs
[i
].Attributes
& SE_PRIVILEGE_ENABLED
)
741 privilege
->enabled
= TRUE
;
743 privilege
->enabled
= FALSE
;
746 return modified_count
;
749 static void token_disable_privileges( struct token
*token
)
751 struct privilege
*privilege
;
753 /* mark as modified */
754 allocate_luid( &token
->modified_id
);
756 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
757 privilege
->enabled
= FALSE
;
760 int token_check_privileges( struct token
*token
, int all_required
,
761 const LUID_AND_ATTRIBUTES
*reqprivs
,
762 unsigned int count
, LUID_AND_ATTRIBUTES
*usedprivs
)
764 unsigned int i
, enabled_count
= 0;
766 for (i
= 0; i
< count
; i
++)
768 struct privilege
*privilege
=
769 token_find_privilege( token
, &reqprivs
[i
].Luid
, TRUE
);
772 usedprivs
[i
] = reqprivs
[i
];
774 if (privilege
&& privilege
->enabled
)
778 usedprivs
[i
].Attributes
|= SE_PRIVILEGE_USED_FOR_ACCESS
;
783 return (enabled_count
== count
);
785 return (enabled_count
> 0);
788 int token_sid_present( struct token
*token
, const SID
*sid
, int deny
)
792 if (security_equal_sid( token
->user
, sid
)) return TRUE
;
794 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
796 if (!group
->enabled
) continue;
797 if (group
->deny_only
&& !deny
) continue;
799 if (security_equal_sid( &group
->sid
, sid
)) return TRUE
;
805 /* Checks access to a security descriptor. 'sd' must have been validated by
806 * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
807 * the reason. 'status' parameter will indicate if access is granted or denied.
809 * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
811 static unsigned int token_access_check( struct token
*token
,
812 const struct security_descriptor
*sd
,
813 unsigned int desired_access
,
814 LUID_AND_ATTRIBUTES
*privs
,
815 unsigned int *priv_count
,
816 const GENERIC_MAPPING
*mapping
,
817 unsigned int *granted_access
,
818 unsigned int *status
)
820 unsigned int current_access
= 0;
821 unsigned int denied_access
= 0;
825 const ACE_HEADER
*ace
;
828 /* assume no access rights */
831 /* fail if desired_access contains generic rights */
832 if (desired_access
& (GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
|GENERIC_ALL
))
835 return STATUS_GENERIC_NOT_MAPPED
;
838 dacl
= sd_get_dacl( sd
, &dacl_present
);
839 owner
= sd_get_owner( sd
);
840 if (!owner
|| !sd_get_group( sd
))
843 return STATUS_INVALID_SECURITY_DESCR
;
846 /* 1: Grant desired access if the object is unprotected */
847 if (!dacl_present
|| !dacl
)
850 *granted_access
= desired_access
;
851 return *status
= STATUS_SUCCESS
;
854 /* 2: Check if caller wants access to system security part. Note: access
855 * is only granted if specifically asked for */
856 if (desired_access
& ACCESS_SYSTEM_SECURITY
)
858 const LUID_AND_ATTRIBUTES security_priv
= { SeSecurityPrivilege
, 0 };
859 LUID_AND_ATTRIBUTES retpriv
= security_priv
;
860 if (token_check_privileges( token
, TRUE
, &security_priv
, 1, &retpriv
))
864 /* assumes that there will only be one privilege to return */
865 if (*priv_count
>= 1)
873 return STATUS_BUFFER_TOO_SMALL
;
876 current_access
|= ACCESS_SYSTEM_SECURITY
;
877 if (desired_access
== current_access
)
879 *granted_access
= current_access
;
880 return *status
= STATUS_SUCCESS
;
886 *status
= STATUS_PRIVILEGE_NOT_HELD
;
887 return STATUS_SUCCESS
;
890 else if (priv_count
) *priv_count
= 0;
892 /* 3: Check whether the token is the owner */
893 /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
894 * checked when a "set owner" call is made, overriding the access rights
895 * determined here. */
896 if (token_sid_present( token
, owner
, FALSE
))
898 current_access
|= (READ_CONTROL
| WRITE_DAC
);
899 if (desired_access
== current_access
)
901 *granted_access
= current_access
;
902 return *status
= STATUS_SUCCESS
;
906 /* 4: Grant rights according to the DACL */
907 ace
= (const ACE_HEADER
*)(dacl
+ 1);
908 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
910 const ACCESS_ALLOWED_ACE
*aa_ace
;
911 const ACCESS_DENIED_ACE
*ad_ace
;
914 if (ace
->AceFlags
& INHERIT_ONLY_ACE
)
917 switch (ace
->AceType
)
919 case ACCESS_DENIED_ACE_TYPE
:
920 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
921 sid
= (const SID
*)&ad_ace
->SidStart
;
922 if (token_sid_present( token
, sid
, TRUE
))
924 unsigned int access
= ad_ace
->Mask
;
925 map_generic_mask(&access
, mapping
);
926 if (desired_access
& MAXIMUM_ALLOWED
)
927 denied_access
|= access
;
930 denied_access
|= (access
& ~current_access
);
931 if (desired_access
& access
) goto done
;
935 case ACCESS_ALLOWED_ACE_TYPE
:
936 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
937 sid
= (const SID
*)&aa_ace
->SidStart
;
938 if (token_sid_present( token
, sid
, FALSE
))
940 unsigned int access
= aa_ace
->Mask
;
941 map_generic_mask(&access
, mapping
);
942 if (desired_access
& MAXIMUM_ALLOWED
)
943 current_access
|= access
;
945 current_access
|= (access
& ~denied_access
);
950 /* don't bother carrying on checking if we've already got all of
952 if (desired_access
== *granted_access
)
957 if (desired_access
& MAXIMUM_ALLOWED
)
958 *granted_access
= current_access
& ~denied_access
;
960 if ((current_access
& desired_access
) == desired_access
)
961 *granted_access
= current_access
& desired_access
;
965 *status
= *granted_access
? STATUS_SUCCESS
: STATUS_ACCESS_DENIED
;
966 return STATUS_SUCCESS
;
969 const ACL
*token_get_default_dacl( struct token
*token
)
971 return token
->default_dacl
;
974 const SID
*token_get_user( struct token
*token
)
979 const SID
*token_get_primary_group( struct token
*token
)
981 return token
->primary_group
;
984 int check_object_access(struct object
*obj
, unsigned int *access
)
986 GENERIC_MAPPING mapping
;
987 struct token
*token
= current
->token
? current
->token
: current
->process
->token
;
988 LUID_AND_ATTRIBUTES priv
;
989 unsigned int status
, priv_count
= 1;
992 mapping
.GenericAll
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
996 if (*access
& MAXIMUM_ALLOWED
)
997 *access
= mapping
.GenericAll
;
1001 mapping
.GenericRead
= obj
->ops
->map_access( obj
, GENERIC_READ
);
1002 mapping
.GenericWrite
= obj
->ops
->map_access( obj
, GENERIC_WRITE
);
1003 mapping
.GenericExecute
= obj
->ops
->map_access( obj
, GENERIC_EXECUTE
);
1005 res
= token_access_check( token
, obj
->sd
, *access
, &priv
, &priv_count
,
1006 &mapping
, access
, &status
) == STATUS_SUCCESS
&&
1007 status
== STATUS_SUCCESS
;
1009 if (!res
) set_error( STATUS_ACCESS_DENIED
);
1014 /* open a security token */
1015 DECL_HANDLER(open_token
)
1017 if (req
->flags
& OPEN_TOKEN_THREAD
)
1019 struct thread
*thread
= get_thread_from_handle( req
->handle
, 0 );
1024 if (!thread
->token
->primary
&& thread
->token
->impersonation_level
<= SecurityAnonymous
)
1025 set_error( STATUS_CANT_OPEN_ANONYMOUS
);
1027 reply
->token
= alloc_handle( current
->process
, thread
->token
,
1028 req
->access
, req
->attributes
);
1031 set_error( STATUS_NO_TOKEN
);
1032 release_object( thread
);
1037 struct process
*process
= get_process_from_handle( req
->handle
, 0 );
1041 reply
->token
= alloc_handle( current
->process
, process
->token
, req
->access
,
1044 set_error( STATUS_NO_TOKEN
);
1045 release_object( process
);
1050 /* adjust the privileges held by a token */
1051 DECL_HANDLER(adjust_token_privileges
)
1053 struct token
*token
;
1054 unsigned int access
= TOKEN_ADJUST_PRIVILEGES
;
1056 if (req
->get_modified_state
) access
|= TOKEN_QUERY
;
1058 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1059 access
, &token_ops
)))
1061 const LUID_AND_ATTRIBUTES
*privs
= get_req_data();
1062 LUID_AND_ATTRIBUTES
*modified_privs
= NULL
;
1063 unsigned int priv_count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1064 unsigned int modified_priv_count
= 0;
1066 if (req
->get_modified_state
&& !req
->disable_all
)
1069 /* count modified privs */
1070 for (i
= 0; i
< priv_count
; i
++)
1072 struct privilege
*privilege
=
1073 token_find_privilege( token
, &privs
[i
].Luid
, FALSE
);
1074 if (privilege
&& req
->get_modified_state
)
1075 modified_priv_count
++;
1077 reply
->len
= modified_priv_count
;
1078 modified_priv_count
= min( modified_priv_count
, get_reply_max_size() / sizeof(*modified_privs
) );
1079 if (modified_priv_count
)
1080 modified_privs
= set_reply_data_size( modified_priv_count
* sizeof(*modified_privs
) );
1082 reply
->len
= modified_priv_count
* sizeof(*modified_privs
);
1084 if (req
->disable_all
)
1085 token_disable_privileges( token
);
1087 modified_priv_count
= token_adjust_privileges( token
, privs
,
1088 priv_count
, modified_privs
, modified_priv_count
);
1090 release_object( token
);
1094 /* retrieves the list of privileges that may be held be the token */
1095 DECL_HANDLER(get_token_privileges
)
1097 struct token
*token
;
1099 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1104 LUID_AND_ATTRIBUTES
*privs
;
1105 struct privilege
*privilege
;
1107 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1110 reply
->len
= priv_count
* sizeof(*privs
);
1111 if (reply
->len
<= get_reply_max_size())
1113 privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1117 LIST_FOR_EACH_ENTRY( privilege
, &token
->privileges
, struct privilege
, entry
)
1119 luid_and_attr_from_privilege( &privs
[i
], privilege
);
1125 set_error(STATUS_BUFFER_TOO_SMALL
);
1127 release_object( token
);
1131 /* creates a duplicate of the token */
1132 DECL_HANDLER(duplicate_token
)
1134 struct token
*src_token
;
1136 if ((src_token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1140 struct token
*token
= token_duplicate( src_token
, req
->primary
, req
->impersonation_level
);
1143 reply
->new_handle
= alloc_handle( current
->process
, token
, req
->access
, req
->attributes
);
1144 release_object( token
);
1146 release_object( src_token
);
1150 /* checks the specified privileges are held by the token */
1151 DECL_HANDLER(check_token_privileges
)
1153 struct token
*token
;
1155 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1159 unsigned int count
= get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES
);
1161 if (!token
->primary
&& token
->impersonation_level
<= SecurityAnonymous
)
1162 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1163 else if (get_reply_max_size() >= count
* sizeof(LUID_AND_ATTRIBUTES
))
1165 LUID_AND_ATTRIBUTES
*usedprivs
= set_reply_data_size( count
* sizeof(*usedprivs
) );
1166 reply
->has_privileges
= token_check_privileges( token
, req
->all_required
, get_req_data(), count
, usedprivs
);
1169 set_error( STATUS_BUFFER_OVERFLOW
);
1170 release_object( token
);
1174 /* checks that a user represented by a token is allowed to access an object
1175 * represented by a security descriptor */
1176 DECL_HANDLER(access_check
)
1178 data_size_t sd_size
= get_req_data_size();
1179 const struct security_descriptor
*sd
= get_req_data();
1180 struct token
*token
;
1182 if (!sd_is_valid( sd
, sd_size
))
1184 set_error( STATUS_ACCESS_VIOLATION
);
1188 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1192 GENERIC_MAPPING mapping
;
1193 unsigned int status
;
1194 LUID_AND_ATTRIBUTES priv
;
1195 unsigned int priv_count
= 1;
1197 memset(&priv
, 0, sizeof(priv
));
1199 /* only impersonation tokens may be used with this function */
1202 set_error( STATUS_NO_IMPERSONATION_TOKEN
);
1203 release_object( token
);
1206 /* anonymous impersonation tokens can't be used */
1207 if (token
->impersonation_level
<= SecurityAnonymous
)
1209 set_error( STATUS_BAD_IMPERSONATION_LEVEL
);
1210 release_object( token
);
1214 mapping
.GenericRead
= req
->mapping_read
;
1215 mapping
.GenericWrite
= req
->mapping_write
;
1216 mapping
.GenericExecute
= req
->mapping_execute
;
1217 mapping
.GenericAll
= req
->mapping_all
;
1219 status
= token_access_check(
1220 token
, sd
, req
->desired_access
, &priv
, &priv_count
, &mapping
,
1221 &reply
->access_granted
, &reply
->access_status
);
1223 reply
->privileges_len
= priv_count
*sizeof(LUID_AND_ATTRIBUTES
);
1225 if ((priv_count
> 0) && (reply
->privileges_len
<= get_reply_max_size()))
1227 LUID_AND_ATTRIBUTES
*privs
= set_reply_data_size( priv_count
* sizeof(*privs
) );
1228 memcpy( privs
, &priv
, sizeof(priv
) );
1231 set_error( status
);
1232 release_object( token
);
1236 /* retrieves the SID of the user that the token represents */
1237 DECL_HANDLER(get_token_sid
)
1239 struct token
*token
;
1243 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1247 const SID
*sid
= NULL
;
1249 switch (req
->which_sid
)
1252 assert(token
->user
);
1255 case TokenPrimaryGroup
:
1256 sid
= token
->primary_group
;
1260 struct group
*group
;
1261 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, struct group
, entry
)
1272 set_error( STATUS_INVALID_PARAMETER
);
1278 reply
->sid_len
= FIELD_OFFSET(SID
, SubAuthority
[sid
->SubAuthorityCount
]);
1279 if (reply
->sid_len
<= get_reply_max_size()) set_reply_data( sid
, reply
->sid_len
);
1280 else set_error( STATUS_BUFFER_TOO_SMALL
);
1282 release_object( token
);
1286 /* retrieves the groups that the user represented by the token belongs to */
1287 DECL_HANDLER(get_token_groups
)
1289 struct token
*token
;
1291 reply
->user_len
= 0;
1293 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1297 size_t size_needed
= sizeof(struct token_groups
);
1298 unsigned int group_count
= 0;
1299 const struct group
*group
;
1301 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1304 size_needed
+= FIELD_OFFSET(SID
, SubAuthority
[group
->sid
.SubAuthorityCount
]);
1306 size_needed
+= sizeof(unsigned int) * group_count
;
1308 reply
->user_len
= size_needed
;
1310 if (size_needed
<= get_reply_max_size())
1312 struct token_groups
*tg
= set_reply_data_size( size_needed
);
1315 unsigned int *attr_ptr
= (unsigned int *)(tg
+ 1);
1316 SID
*sid_ptr
= (SID
*)(attr_ptr
+ group_count
);
1318 tg
->count
= group_count
;
1320 LIST_FOR_EACH_ENTRY( group
, &token
->groups
, const struct group
, entry
)
1324 if (group
->mandatory
) *attr_ptr
|= SE_GROUP_MANDATORY
;
1325 if (group
->def
) *attr_ptr
|= SE_GROUP_ENABLED_BY_DEFAULT
;
1326 if (group
->enabled
) *attr_ptr
|= SE_GROUP_ENABLED
;
1327 if (group
->owner
) *attr_ptr
|= SE_GROUP_OWNER
;
1328 if (group
->deny_only
) *attr_ptr
|= SE_GROUP_USE_FOR_DENY_ONLY
;
1329 if (group
->resource
) *attr_ptr
|= SE_GROUP_RESOURCE
;
1330 if (group
->logon
) *attr_ptr
|= SE_GROUP_LOGON_ID
;
1332 memcpy(sid_ptr
, &group
->sid
, FIELD_OFFSET(SID
, SubAuthority
[group
->sid
.SubAuthorityCount
]));
1334 sid_ptr
= (SID
*)((char *)sid_ptr
+ FIELD_OFFSET(SID
, SubAuthority
[group
->sid
.SubAuthorityCount
]));
1339 else set_error( STATUS_BUFFER_TOO_SMALL
);
1341 release_object( token
);
1345 DECL_HANDLER(get_token_impersonation_level
)
1347 struct token
*token
;
1349 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1354 set_error( STATUS_INVALID_PARAMETER
);
1356 reply
->impersonation_level
= token
->impersonation_level
;
1358 release_object( token
);
1362 DECL_HANDLER(get_token_statistics
)
1364 struct token
*token
;
1366 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1370 reply
->token_id
= token
->token_id
;
1371 reply
->modified_id
= token
->modified_id
;
1372 reply
->primary
= token
->primary
;
1373 reply
->impersonation_level
= token
->impersonation_level
;
1374 reply
->group_count
= list_count( &token
->groups
);
1375 reply
->privilege_count
= list_count( &token
->privileges
);
1377 release_object( token
);
1381 DECL_HANDLER(get_token_default_dacl
)
1383 struct token
*token
;
1387 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1391 if (token
->default_dacl
)
1392 reply
->acl_len
= token
->default_dacl
->AclSize
;
1394 if (reply
->acl_len
<= get_reply_max_size())
1396 ACL
*acl_reply
= set_reply_data_size( reply
->acl_len
);
1398 memcpy( acl_reply
, token
->default_dacl
, reply
->acl_len
);
1400 else set_error( STATUS_BUFFER_TOO_SMALL
);
1402 release_object( token
);
1406 DECL_HANDLER(set_token_default_dacl
)
1408 struct token
*token
;
1410 if ((token
= (struct token
*)get_handle_obj( current
->process
, req
->handle
,
1411 TOKEN_ADJUST_DEFAULT
,
1414 const ACL
*acl
= get_req_data();
1415 unsigned int acl_size
= get_req_data_size();
1417 free( token
->default_dacl
);
1418 token
->default_dacl
= NULL
;
1421 token
->default_dacl
= memdup( acl
, acl_size
);
1423 release_object( token
);