token.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /*
  2. * Tokens
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. * Copyright (C) 2003 Mike McCormack
  6. * Copyright (C) 2005 Robert Shearman
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. #include "config.h"
  23. #include <assert.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <unistd.h>
  28. #include "ntstatus.h"
  29. #define WIN32_NO_STATUS
  30. #include "windef.h"
  31. #include "winternl.h"
  32. #include "handle.h"
  33. #include "thread.h"
  34. #include "process.h"
  35. #include "request.h"
  36. #include "security.h"
  37. #define MAX_SUBAUTH_COUNT 1
  38. const LUID SeIncreaseQuotaPrivilege = { 5, 0 };
  39. const LUID SeSecurityPrivilege = { 8, 0 };
  40. const LUID SeTakeOwnershipPrivilege = { 9, 0 };
  41. const LUID SeLoadDriverPrivilege = { 10, 0 };
  42. const LUID SeSystemProfilePrivilege = { 11, 0 };
  43. const LUID SeSystemtimePrivilege = { 12, 0 };
  44. const LUID SeProfileSingleProcessPrivilege = { 13, 0 };
  45. const LUID SeIncreaseBasePriorityPrivilege = { 14, 0 };
  46. const LUID SeCreatePagefilePrivilege = { 15, 0 };
  47. const LUID SeBackupPrivilege = { 17, 0 };
  48. const LUID SeRestorePrivilege = { 18, 0 };
  49. const LUID SeShutdownPrivilege = { 19, 0 };
  50. const LUID SeDebugPrivilege = { 20, 0 };
  51. const LUID SeSystemEnvironmentPrivilege = { 22, 0 };
  52. const LUID SeChangeNotifyPrivilege = { 23, 0 };
  53. const LUID SeRemoteShutdownPrivilege = { 24, 0 };
  54. const LUID SeUndockPrivilege = { 25, 0 };
  55. const LUID SeManageVolumePrivilege = { 28, 0 };
  56. const LUID SeImpersonatePrivilege = { 29, 0 };
  57. const LUID SeCreateGlobalPrivilege = { 30, 0 };
  58. #define SID_N(n) struct /* same fields as struct SID */ \
  59. { \
  60. BYTE Revision; \
  61. BYTE SubAuthorityCount; \
  62. SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
  63. DWORD SubAuthority[n]; \
  64. }
  65. static const SID world_sid = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } };
  66. static const SID local_sid = { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } };
  67. static const SID interactive_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } };
  68. static const SID anonymous_logon_sid = { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ANONYMOUS_LOGON_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 SID high_label_sid = { SID_REVISION, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY }, { SECURITY_MANDATORY_HIGH_RID } };
  72. static const SID medium_label_sid = { SID_REVISION, 1, { SECURITY_MANDATORY_LABEL_AUTHORITY }, { SECURITY_MANDATORY_MEDIUM_RID } };
  73. static const SID_N(5) local_user_sid = { SID_REVISION, 5, { SECURITY_NT_AUTHORITY }, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, 1000 } };
  74. static const SID_N(2) builtin_admins_sid = { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } };
  75. static const SID_N(2) builtin_users_sid = { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } };
  76. static const SID_N(3) builtin_logon_sid = { SID_REVISION, 3, { SECURITY_NT_AUTHORITY }, { SECURITY_LOGON_IDS_RID, 0, 0 } };
  77. static const SID_N(5) domain_users_sid = { SID_REVISION, 5, { SECURITY_NT_AUTHORITY }, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, DOMAIN_GROUP_RID_USERS } };
  78. const PSID security_world_sid = (PSID)&world_sid;
  79. static const PSID security_local_sid = (PSID)&local_sid;
  80. static const PSID security_interactive_sid = (PSID)&interactive_sid;
  81. static const PSID security_authenticated_user_sid = (PSID)&authenticated_user_sid;
  82. const PSID security_local_system_sid = (PSID)&local_system_sid;
  83. const PSID security_local_user_sid = (PSID)&local_user_sid;
  84. const PSID security_builtin_admins_sid = (PSID)&builtin_admins_sid;
  85. const PSID security_builtin_users_sid = (PSID)&builtin_users_sid;
  86. const PSID security_domain_users_sid = (PSID)&domain_users_sid;
  87. const PSID security_high_label_sid = (PSID)&high_label_sid;
  88. const PSID security_medium_label_sid = (PSID)&medium_label_sid;
  89. static luid_t prev_luid_value = { 1000, 0 };
  90. struct token
  91. {
  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 *owner; /* SID of owner (points to user or one of groups) */
  99. SID *primary_group; /* SID of user's primary group (points to one of groups) */
  100. unsigned primary; /* is this a primary or impersonation token? */
  101. ACL *default_dacl; /* the default DACL to assign to objects created by this user */
  102. TOKEN_SOURCE source; /* source of the token */
  103. int impersonation_level; /* impersonation level this token is capable of if non-primary token */
  104. TOKEN_ELEVATION_TYPE elevation; /* elevation level */
  105. const SID *integrity; /* token integrity */
  106. };
  107. struct privilege
  108. {
  109. struct list entry;
  110. LUID luid;
  111. unsigned enabled : 1; /* is the privilege currently enabled? */
  112. unsigned def : 1; /* is the privilege enabled by default? */
  113. };
  114. struct group
  115. {
  116. struct list entry;
  117. unsigned enabled : 1; /* is the sid currently enabled? */
  118. unsigned def : 1; /* is the sid enabled by default? */
  119. unsigned logon : 1; /* is this a logon sid? */
  120. unsigned mandatory: 1; /* is this sid always enabled? */
  121. unsigned owner : 1; /* can this sid be an owner of an object? */
  122. unsigned resource : 1; /* is this a domain-local group? */
  123. unsigned deny_only: 1; /* is this a sid that should be use for denying only? */
  124. SID sid;
  125. };
  126. static void token_dump( struct object *obj, int verbose );
  127. static struct object_type *token_get_type( struct object *obj );
  128. static unsigned int token_map_access( struct object *obj, unsigned int access );
  129. static void token_destroy( struct object *obj );
  130. static const struct object_ops token_ops =
  131. {
  132. sizeof(struct token), /* size */
  133. token_dump, /* dump */
  134. token_get_type, /* get_type */
  135. no_add_queue, /* add_queue */
  136. NULL, /* remove_queue */
  137. NULL, /* signaled */
  138. NULL, /* get_esync_fd */
  139. NULL, /* satisfied */
  140. no_signal, /* signal */
  141. no_get_fd, /* get_fd */
  142. token_map_access, /* map_access */
  143. default_get_sd, /* get_sd */
  144. default_set_sd, /* set_sd */
  145. no_lookup_name, /* lookup_name */
  146. no_link_name, /* link_name */
  147. NULL, /* unlink_name */
  148. no_open_file, /* open_file */
  149. no_kernel_obj_list, /* get_kernel_obj_list */
  150. no_alloc_handle, /* alloc_handle */
  151. no_close_handle, /* close_handle */
  152. token_destroy /* destroy */
  153. };
  154. static void token_dump( struct object *obj, int verbose )
  155. {
  156. struct token *token = (struct token *)obj;
  157. assert( obj->ops == &token_ops );
  158. fprintf( stderr, "Token id=%d.%u primary=%u impersonation level=%d\n", token->token_id.high_part,
  159. token->token_id.low_part, token->primary, token->impersonation_level );
  160. }
  161. static struct object_type *token_get_type( struct object *obj )
  162. {
  163. static const struct unicode_str str = { type_Token, sizeof(type_Token) };
  164. return get_object_type( &str );
  165. }
  166. static unsigned int token_map_access( struct object *obj, unsigned int access )
  167. {
  168. if (access & GENERIC_READ) access |= TOKEN_READ;
  169. if (access & GENERIC_WRITE) access |= TOKEN_WRITE;
  170. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  171. if (access & GENERIC_ALL) access |= TOKEN_ALL_ACCESS;
  172. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  173. }
  174. static SID *security_sid_alloc( const SID_IDENTIFIER_AUTHORITY *idauthority, int subauthcount, const unsigned int subauth[] )
  175. {
  176. int i;
  177. SID *sid = mem_alloc( FIELD_OFFSET(SID, SubAuthority[subauthcount]) );
  178. if (!sid) return NULL;
  179. sid->Revision = SID_REVISION;
  180. sid->SubAuthorityCount = subauthcount;
  181. sid->IdentifierAuthority = *idauthority;
  182. for (i = 0; i < subauthcount; i++)
  183. sid->SubAuthority[i] = subauth[i];
  184. return sid;
  185. }
  186. void security_set_thread_token( struct thread *thread, obj_handle_t handle )
  187. {
  188. if (!handle)
  189. {
  190. if (thread->token)
  191. release_object( thread->token );
  192. thread->token = NULL;
  193. }
  194. else
  195. {
  196. struct token *token = (struct token *)get_handle_obj( current->process,
  197. handle,
  198. TOKEN_IMPERSONATE,
  199. &token_ops );
  200. if (token)
  201. {
  202. if (thread->token)
  203. release_object( thread->token );
  204. thread->token = token;
  205. }
  206. }
  207. }
  208. const SID *security_unix_uid_to_sid( uid_t uid )
  209. {
  210. /* very simple mapping: either the current user or not the current user */
  211. if (uid == getuid())
  212. return (const SID *)&local_user_sid;
  213. else
  214. return &anonymous_logon_sid;
  215. }
  216. static int acl_is_valid( const ACL *acl, data_size_t size )
  217. {
  218. ULONG i;
  219. const ACE_HEADER *ace;
  220. if (size < sizeof(ACL))
  221. return FALSE;
  222. size = min(size, MAX_ACL_LEN);
  223. size -= sizeof(ACL);
  224. ace = (const ACE_HEADER *)(acl + 1);
  225. for (i = 0; i < acl->AceCount; i++)
  226. {
  227. const SID *sid;
  228. data_size_t sid_size;
  229. if (size < sizeof(ACE_HEADER))
  230. return FALSE;
  231. if (size < ace->AceSize)
  232. return FALSE;
  233. size -= ace->AceSize;
  234. switch (ace->AceType)
  235. {
  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);
  239. break;
  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);
  243. break;
  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);
  247. break;
  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);
  251. break;
  252. case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
  253. sid = (const SID *)&((const SYSTEM_MANDATORY_LABEL_ACE *)ace)->SidStart;
  254. sid_size = ace->AceSize - FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart);
  255. break;
  256. default:
  257. return FALSE;
  258. }
  259. if (sid_size < FIELD_OFFSET(SID, SubAuthority[0]) || sid_size < security_sid_len( sid ))
  260. return FALSE;
  261. ace = ace_next( ace );
  262. }
  263. return TRUE;
  264. }
  265. static unsigned int get_sid_count( const SID *sid, data_size_t size )
  266. {
  267. unsigned int count;
  268. for (count = 0; size >= sizeof(SID) && security_sid_len( sid ) <= size; count++)
  269. {
  270. size -= security_sid_len( sid );
  271. sid = (const SID *)((char *)sid + security_sid_len( sid ));
  272. }
  273. return count;
  274. }
  275. /* checks whether all members of a security descriptor fit inside the size
  276. * of memory specified */
  277. int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
  278. {
  279. size_t offset = sizeof(struct security_descriptor);
  280. const SID *group;
  281. const SID *owner;
  282. const ACL *sacl;
  283. const ACL *dacl;
  284. int dummy;
  285. if (size < offset)
  286. return FALSE;
  287. if ((sd->owner_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
  288. (offset + sd->owner_len > size))
  289. return FALSE;
  290. owner = sd_get_owner( sd );
  291. if (owner)
  292. {
  293. if ((sd->owner_len < sizeof(SID)) || (security_sid_len( owner ) > sd->owner_len))
  294. return FALSE;
  295. }
  296. offset += sd->owner_len;
  297. if ((sd->group_len >= FIELD_OFFSET(SID, SubAuthority[255])) ||
  298. (offset + sd->group_len > size))
  299. return FALSE;
  300. group = sd_get_group( sd );
  301. if (group)
  302. {
  303. if ((sd->group_len < sizeof(SID)) || (security_sid_len( group ) > sd->group_len))
  304. return FALSE;
  305. }
  306. offset += sd->group_len;
  307. if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
  308. return FALSE;
  309. sacl = sd_get_sacl( sd, &dummy );
  310. if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
  311. return FALSE;
  312. offset += sd->sacl_len;
  313. if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
  314. return FALSE;
  315. dacl = sd_get_dacl( sd, &dummy );
  316. if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
  317. return FALSE;
  318. offset += sd->dacl_len;
  319. return TRUE;
  320. }
  321. /* extract security labels from SACL */
  322. ACL *extract_security_labels( const ACL *sacl )
  323. {
  324. size_t size = sizeof(ACL);
  325. const ACE_HEADER *ace;
  326. ACE_HEADER *label_ace;
  327. unsigned int i, count = 0;
  328. ACL *label_acl;
  329. ace = (const ACE_HEADER *)(sacl + 1);
  330. for (i = 0; i < sacl->AceCount; i++, ace = ace_next( ace ))
  331. {
  332. if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
  333. {
  334. size += ace->AceSize;
  335. count++;
  336. }
  337. }
  338. label_acl = mem_alloc( size );
  339. if (!label_acl) return NULL;
  340. label_acl->AclRevision = sacl->AclRevision;
  341. label_acl->Sbz1 = 0;
  342. label_acl->AclSize = size;
  343. label_acl->AceCount = count;
  344. label_acl->Sbz2 = 0;
  345. label_ace = (ACE_HEADER *)(label_acl + 1);
  346. ace = (const ACE_HEADER *)(sacl + 1);
  347. for (i = 0; i < sacl->AceCount; i++, ace = ace_next( ace ))
  348. {
  349. if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
  350. {
  351. memcpy( label_ace, ace, ace->AceSize );
  352. label_ace = (ACE_HEADER *)ace_next( label_ace );
  353. }
  354. }
  355. return label_acl;
  356. }
  357. /* replace security labels in an existing SACL */
  358. ACL *replace_security_labels( const ACL *old_sacl, const ACL *new_sacl )
  359. {
  360. const ACE_HEADER *ace;
  361. ACE_HEADER *replaced_ace;
  362. size_t size = sizeof(ACL);
  363. unsigned int i, count = 0;
  364. BYTE revision = ACL_REVISION;
  365. ACL *replaced_acl;
  366. if (old_sacl)
  367. {
  368. revision = max( revision, old_sacl->AclRevision );
  369. ace = (const ACE_HEADER *)(old_sacl + 1);
  370. for (i = 0; i < old_sacl->AceCount; i++, ace = ace_next( ace ))
  371. {
  372. if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  373. size += ace->AceSize;
  374. count++;
  375. }
  376. }
  377. if (new_sacl)
  378. {
  379. revision = max( revision, new_sacl->AclRevision );
  380. ace = (const ACE_HEADER *)(new_sacl + 1);
  381. for (i = 0; i < new_sacl->AceCount; i++, ace = ace_next( ace ))
  382. {
  383. if (ace->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  384. size += ace->AceSize;
  385. count++;
  386. }
  387. }
  388. replaced_acl = mem_alloc( size );
  389. if (!replaced_acl) return NULL;
  390. replaced_acl->AclRevision = revision;
  391. replaced_acl->Sbz1 = 0;
  392. replaced_acl->AclSize = size;
  393. replaced_acl->AceCount = count;
  394. replaced_acl->Sbz2 = 0;
  395. replaced_ace = (ACE_HEADER *)(replaced_acl + 1);
  396. if (old_sacl)
  397. {
  398. ace = (const ACE_HEADER *)(old_sacl + 1);
  399. for (i = 0; i < old_sacl->AceCount; i++, ace = ace_next( ace ))
  400. {
  401. if (ace->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  402. memcpy( replaced_ace, ace, ace->AceSize );
  403. replaced_ace = (ACE_HEADER *)ace_next( replaced_ace );
  404. }
  405. }
  406. if (new_sacl)
  407. {
  408. ace = (const ACE_HEADER *)(new_sacl + 1);
  409. for (i = 0; i < new_sacl->AceCount; i++, ace = ace_next( ace ))
  410. {
  411. if (ace->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  412. memcpy( replaced_ace, ace, ace->AceSize );
  413. replaced_ace = (ACE_HEADER *)ace_next( replaced_ace );
  414. }
  415. }
  416. return replaced_acl;
  417. }
  418. /* maps from generic rights to specific rights as given by a mapping */
  419. static inline void map_generic_mask(unsigned int *mask, const GENERIC_MAPPING *mapping)
  420. {
  421. if (*mask & GENERIC_READ) *mask |= mapping->GenericRead;
  422. if (*mask & GENERIC_WRITE) *mask |= mapping->GenericWrite;
  423. if (*mask & GENERIC_EXECUTE) *mask |= mapping->GenericExecute;
  424. if (*mask & GENERIC_ALL) *mask |= mapping->GenericAll;
  425. *mask &= 0x0FFFFFFF;
  426. }
  427. static inline int is_equal_luid( const LUID *luid1, const LUID *luid2 )
  428. {
  429. return (luid1->LowPart == luid2->LowPart && luid1->HighPart == luid2->HighPart);
  430. }
  431. static inline void allocate_luid( luid_t *luid )
  432. {
  433. prev_luid_value.low_part++;
  434. *luid = prev_luid_value;
  435. }
  436. DECL_HANDLER( allocate_locally_unique_id )
  437. {
  438. allocate_luid( &reply->luid );
  439. }
  440. static inline void luid_and_attr_from_privilege( LUID_AND_ATTRIBUTES *out, const struct privilege *in)
  441. {
  442. out->Luid = in->luid;
  443. out->Attributes =
  444. (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
  445. (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
  446. }
  447. static struct privilege *privilege_add( struct token *token, const LUID *luid, int enabled )
  448. {
  449. struct privilege *privilege = mem_alloc( sizeof(*privilege) );
  450. if (privilege)
  451. {
  452. privilege->luid = *luid;
  453. privilege->def = privilege->enabled = (enabled != 0);
  454. list_add_tail( &token->privileges, &privilege->entry );
  455. }
  456. return privilege;
  457. }
  458. static inline void privilege_remove( struct privilege *privilege )
  459. {
  460. list_remove( &privilege->entry );
  461. free( privilege );
  462. }
  463. static void token_destroy( struct object *obj )
  464. {
  465. struct token* token;
  466. struct list *cursor, *cursor_next;
  467. assert( obj->ops == &token_ops );
  468. token = (struct token *)obj;
  469. free( token->user );
  470. LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
  471. {
  472. struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
  473. privilege_remove( privilege );
  474. }
  475. LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
  476. {
  477. struct group *group = LIST_ENTRY( cursor, struct group, entry );
  478. list_remove( &group->entry );
  479. free( group );
  480. }
  481. free( token->default_dacl );
  482. }
  483. /* creates a new token.
  484. * groups may be NULL if group_count is 0.
  485. * privs may be NULL if priv_count is 0.
  486. * default_dacl may be NULL, indicating that all objects created by the user
  487. * are unsecured.
  488. * modified_id may be NULL, indicating that a new modified_id luid should be
  489. * allocated.
  490. */
  491. static struct token *create_token( unsigned primary, const SID *user,
  492. const SID_AND_ATTRIBUTES *groups, unsigned int group_count,
  493. const LUID_AND_ATTRIBUTES *privs, unsigned int priv_count,
  494. const ACL *default_dacl, TOKEN_SOURCE source,
  495. const luid_t *modified_id,
  496. int impersonation_level, TOKEN_ELEVATION_TYPE elevation,
  497. const SID *integrity )
  498. {
  499. struct token *token = alloc_object( &token_ops );
  500. if (token)
  501. {
  502. unsigned int i;
  503. allocate_luid( &token->token_id );
  504. if (modified_id)
  505. token->modified_id = *modified_id;
  506. else
  507. allocate_luid( &token->modified_id );
  508. list_init( &token->privileges );
  509. list_init( &token->groups );
  510. token->primary = primary;
  511. /* primary tokens don't have impersonation levels */
  512. if (primary)
  513. token->impersonation_level = -1;
  514. else
  515. token->impersonation_level = impersonation_level;
  516. token->default_dacl = NULL;
  517. token->primary_group = NULL;
  518. token->elevation = elevation;
  519. /* copy user */
  520. token->user = memdup( user, security_sid_len( user ));
  521. if (!token->user)
  522. {
  523. release_object( token );
  524. return NULL;
  525. }
  526. /* copy groups */
  527. for (i = 0; i < group_count; i++)
  528. {
  529. size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[((const SID *)groups[i].Sid)->SubAuthorityCount] );
  530. struct group *group = mem_alloc( size );
  531. if (!group)
  532. {
  533. release_object( token );
  534. return NULL;
  535. }
  536. memcpy( &group->sid, groups[i].Sid, security_sid_len( groups[i].Sid ));
  537. group->mandatory = (groups[i].Attributes & SE_GROUP_MANDATORY) != 0;
  538. group->def = (groups[i].Attributes & SE_GROUP_ENABLED_BY_DEFAULT) != 0;
  539. group->enabled = (groups[i].Attributes & SE_GROUP_ENABLED) != 0;
  540. group->owner = (groups[i].Attributes & SE_GROUP_OWNER) != 0;
  541. group->deny_only = (groups[i].Attributes & SE_GROUP_USE_FOR_DENY_ONLY) != 0;
  542. group->logon = (groups[i].Attributes & SE_GROUP_LOGON_ID) != 0;
  543. group->resource = (groups[i].Attributes & SE_GROUP_RESOURCE) != 0;
  544. list_add_tail( &token->groups, &group->entry );
  545. /* Use first owner capable group as owner and primary group */
  546. if (!token->primary_group && group->owner)
  547. {
  548. token->owner = &group->sid;
  549. token->primary_group = &group->sid;
  550. }
  551. }
  552. /* copy privileges */
  553. for (i = 0; i < priv_count; i++)
  554. {
  555. /* note: we don't check uniqueness: the caller must make sure
  556. * privs doesn't contain any duplicate luids */
  557. if (!privilege_add( token, &privs[i].Luid,
  558. privs[i].Attributes & SE_PRIVILEGE_ENABLED ))
  559. {
  560. release_object( token );
  561. return NULL;
  562. }
  563. }
  564. if (default_dacl)
  565. {
  566. token->default_dacl = memdup( default_dacl, default_dacl->AclSize );
  567. if (!token->default_dacl)
  568. {
  569. release_object( token );
  570. return NULL;
  571. }
  572. }
  573. token->source = source;
  574. token->integrity = integrity;
  575. }
  576. return token;
  577. }
  578. static int filter_group( struct group *group, const SID *filter, unsigned int count )
  579. {
  580. unsigned int i;
  581. for (i = 0; i < count; i++)
  582. {
  583. if (security_equal_sid( &group->sid, filter )) return 1;
  584. filter = (const SID *)((char *)filter + security_sid_len( filter ));
  585. }
  586. return 0;
  587. }
  588. static int filter_privilege( struct privilege *privilege, const LUID_AND_ATTRIBUTES *filter, unsigned int count )
  589. {
  590. unsigned int i;
  591. for (i = 0; i < count; i++)
  592. {
  593. if (!memcmp( &privilege->luid, &filter[i].Luid, sizeof(LUID) ))
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. struct token *token_duplicate( struct token *src_token, unsigned primary,
  599. int impersonation_level, const struct security_descriptor *sd,
  600. const LUID_AND_ATTRIBUTES *filter_privileges, unsigned int priv_count,
  601. const SID *filter_groups, unsigned int group_count, struct token *impersonation)
  602. {
  603. const luid_t *modified_id =
  604. primary || (impersonation_level == src_token->impersonation_level) ?
  605. &src_token->modified_id : NULL;
  606. struct token *token = NULL;
  607. struct privilege *privilege;
  608. struct group *group;
  609. if (!primary &&
  610. (impersonation_level < SecurityAnonymous ||
  611. impersonation_level > SecurityDelegation ||
  612. (!src_token->primary && (impersonation_level > src_token->impersonation_level))))
  613. {
  614. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  615. return NULL;
  616. }
  617. token = create_token( primary, src_token->user, NULL, 0,
  618. NULL, 0, src_token->default_dacl,
  619. src_token->source, modified_id,
  620. impersonation_level,
  621. src_token->elevation,
  622. src_token->integrity );
  623. if (!token) return token;
  624. /* copy groups */
  625. token->primary_group = NULL;
  626. LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
  627. {
  628. size_t size = FIELD_OFFSET( struct group, sid.SubAuthority[group->sid.SubAuthorityCount] );
  629. struct group *newgroup = mem_alloc( size );
  630. if (!newgroup)
  631. {
  632. release_object( token );
  633. return NULL;
  634. }
  635. memcpy( newgroup, group, size );
  636. if (filter_group( group, filter_groups, group_count ))
  637. {
  638. newgroup->enabled = 0;
  639. newgroup->def = 0;
  640. newgroup->deny_only = 1;
  641. }
  642. list_add_tail( &token->groups, &newgroup->entry );
  643. if (src_token->primary_group == &group->sid)
  644. {
  645. token->owner = &newgroup->sid;
  646. token->primary_group = &newgroup->sid;
  647. }
  648. }
  649. assert( token->primary_group );
  650. /* copy privileges */
  651. LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
  652. {
  653. if (filter_privilege( privilege, filter_privileges, priv_count )) continue;
  654. if (!privilege_add( token, &privilege->luid, privilege->enabled ))
  655. {
  656. release_object( token );
  657. return NULL;
  658. }
  659. }
  660. if (sd) default_set_sd( &token->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
  661. DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
  662. if (!token_assign_label( token, impersonation ? (PSID)impersonation->integrity : (PSID)token->integrity ))
  663. {
  664. release_object( token );
  665. return NULL;
  666. }
  667. return token;
  668. }
  669. static ACL *create_default_dacl( const SID *user )
  670. {
  671. ACCESS_ALLOWED_ACE *aaa;
  672. ACL *default_dacl;
  673. SID *sid;
  674. size_t default_dacl_size = sizeof(ACL) +
  675. 2*(sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
  676. sizeof(local_system_sid) +
  677. security_sid_len( user );
  678. default_dacl = mem_alloc( default_dacl_size );
  679. if (!default_dacl) return NULL;
  680. default_dacl->AclRevision = ACL_REVISION;
  681. default_dacl->Sbz1 = 0;
  682. default_dacl->AclSize = default_dacl_size;
  683. default_dacl->AceCount = 2;
  684. default_dacl->Sbz2 = 0;
  685. /* GENERIC_ALL for Local System */
  686. aaa = (ACCESS_ALLOWED_ACE *)(default_dacl + 1);
  687. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  688. aaa->Header.AceFlags = 0;
  689. aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
  690. sizeof(local_system_sid);
  691. aaa->Mask = GENERIC_ALL;
  692. sid = (SID *)&aaa->SidStart;
  693. memcpy( sid, &local_system_sid, sizeof(local_system_sid) );
  694. /* GENERIC_ALL for specified user */
  695. aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
  696. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  697. aaa->Header.AceFlags = 0;
  698. aaa->Header.AceSize = (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + security_sid_len( user );
  699. aaa->Mask = GENERIC_ALL;
  700. sid = (SID *)&aaa->SidStart;
  701. memcpy( sid, user, security_sid_len( user ));
  702. return default_dacl;
  703. }
  704. struct sid_data
  705. {
  706. SID_IDENTIFIER_AUTHORITY idauth;
  707. int count;
  708. unsigned int subauth[MAX_SUBAUTH_COUNT];
  709. };
  710. static struct security_descriptor *create_security_label_sd( struct token *token, PSID label_sid )
  711. {
  712. size_t sid_len = security_sid_len( label_sid ), sacl_size, sd_size;
  713. SYSTEM_MANDATORY_LABEL_ACE *smla;
  714. struct security_descriptor *sd;
  715. ACL *sacl;
  716. sacl_size = sizeof(ACL) + FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + sid_len;
  717. sd_size = sizeof(struct security_descriptor) + sacl_size;
  718. if (!(sd = mem_alloc( sd_size )))
  719. return NULL;
  720. sd->control = SE_SACL_PRESENT;
  721. sd->owner_len = 0;
  722. sd->group_len = 0;
  723. sd->sacl_len = sacl_size;
  724. sd->dacl_len = 0;
  725. sacl = (ACL *)(sd + 1);
  726. sacl->AclRevision = ACL_REVISION;
  727. sacl->Sbz1 = 0;
  728. sacl->AclSize = sacl_size;
  729. sacl->AceCount = 1;
  730. sacl->Sbz2 = 0;
  731. smla = (SYSTEM_MANDATORY_LABEL_ACE *)(sacl + 1);
  732. smla->Header.AceType = SYSTEM_MANDATORY_LABEL_ACE_TYPE;
  733. smla->Header.AceFlags = 0;
  734. smla->Header.AceSize = FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart) + sid_len;
  735. smla->Mask = SYSTEM_MANDATORY_LABEL_NO_WRITE_UP;
  736. memcpy( &smla->SidStart, label_sid, sid_len );
  737. assert( sd_is_valid( sd, sd_size ) );
  738. return sd;
  739. }
  740. int token_assign_label( struct token *token, PSID label )
  741. {
  742. struct security_descriptor *sd;
  743. int ret = 0;
  744. if ((sd = create_security_label_sd( token, label )))
  745. {
  746. ret = set_sd_defaults_from_token( &token->obj, sd, LABEL_SECURITY_INFORMATION, token );
  747. free( sd );
  748. }
  749. return ret;
  750. }
  751. struct token *get_token_from_handle( obj_handle_t handle, unsigned int access )
  752. {
  753. return (struct token *)get_handle_obj( current->process, handle,
  754. access, &token_ops );
  755. }
  756. struct token *token_create_admin( void )
  757. {
  758. struct token *token = NULL;
  759. static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
  760. static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
  761. static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
  762. /* on Windows, this value changes every time the user logs on */
  763. static const unsigned int logon_subauth[] = { SECURITY_LOGON_IDS_RID, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
  764. PSID alias_admins_sid;
  765. PSID alias_users_sid;
  766. PSID logon_sid;
  767. const SID *user_sid = security_unix_uid_to_sid( getuid() );
  768. ACL *default_dacl = create_default_dacl( user_sid );
  769. alias_admins_sid = security_sid_alloc( &nt_authority, ARRAY_SIZE( alias_admins_subauth ),
  770. alias_admins_subauth );
  771. alias_users_sid = security_sid_alloc( &nt_authority, ARRAY_SIZE( alias_users_subauth ),
  772. alias_users_subauth );
  773. logon_sid = security_sid_alloc( &nt_authority, ARRAY_SIZE( logon_subauth ), logon_subauth );
  774. if (alias_admins_sid && alias_users_sid && logon_sid && default_dacl)
  775. {
  776. const LUID_AND_ATTRIBUTES admin_privs[] =
  777. {
  778. { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
  779. { SeSecurityPrivilege , 0 },
  780. { SeBackupPrivilege , 0 },
  781. { SeRestorePrivilege , 0 },
  782. { SeSystemtimePrivilege , 0 },
  783. { SeShutdownPrivilege , 0 },
  784. { SeRemoteShutdownPrivilege , 0 },
  785. { SeTakeOwnershipPrivilege , 0 },
  786. { SeDebugPrivilege , 0 },
  787. { SeSystemEnvironmentPrivilege , 0 },
  788. { SeSystemProfilePrivilege , 0 },
  789. { SeProfileSingleProcessPrivilege, 0 },
  790. { SeIncreaseBasePriorityPrivilege, 0 },
  791. { SeLoadDriverPrivilege , SE_PRIVILEGE_ENABLED },
  792. { SeCreatePagefilePrivilege , 0 },
  793. { SeIncreaseQuotaPrivilege , 0 },
  794. { SeUndockPrivilege , 0 },
  795. { SeManageVolumePrivilege , 0 },
  796. { SeImpersonatePrivilege , SE_PRIVILEGE_ENABLED },
  797. { SeCreateGlobalPrivilege , SE_PRIVILEGE_ENABLED },
  798. };
  799. /* note: we don't include non-builtin groups here for the user -
  800. * telling us these is the job of a client-side program */
  801. const SID_AND_ATTRIBUTES admin_groups[] =
  802. {
  803. { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  804. { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  805. { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  806. { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  807. { security_domain_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
  808. { alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
  809. { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  810. { logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
  811. };
  812. static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}};
  813. token = create_token( TRUE, user_sid, admin_groups, ARRAY_SIZE( admin_groups ),
  814. admin_privs, ARRAY_SIZE( admin_privs ), default_dacl,
  815. admin_source, NULL, -1, TokenElevationTypeFull, &high_label_sid );
  816. /* we really need a primary group */
  817. assert( token->primary_group );
  818. if (!token_assign_label( token, (PSID)token->integrity ))
  819. {
  820. release_object( token );
  821. token = NULL;
  822. }
  823. }
  824. free( logon_sid );
  825. free( alias_admins_sid );
  826. free( alias_users_sid );
  827. free( default_dacl );
  828. return token;
  829. }
  830. static struct token *token_create_limited( void )
  831. {
  832. struct token *token = NULL;
  833. static const SID_IDENTIFIER_AUTHORITY nt_authority = { SECURITY_NT_AUTHORITY };
  834. static const unsigned int alias_admins_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS };
  835. static const unsigned int alias_users_subauth[] = { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS };
  836. /* on Windows, this value changes every time the user logs on */
  837. static const unsigned int logon_subauth[] = { SECURITY_LOGON_IDS_RID, 0, 1 /* FIXME: should be randomly generated when tokens are inherited by new processes */ };
  838. PSID alias_admins_sid;
  839. PSID alias_users_sid;
  840. PSID logon_sid;
  841. const SID *user_sid = security_unix_uid_to_sid( getuid() );
  842. ACL *default_dacl = create_default_dacl( user_sid );
  843. alias_admins_sid = security_sid_alloc( &nt_authority, sizeof(alias_admins_subauth)/sizeof(alias_admins_subauth[0]),
  844. alias_admins_subauth );
  845. alias_users_sid = security_sid_alloc( &nt_authority, sizeof(alias_users_subauth)/sizeof(alias_users_subauth[0]),
  846. alias_users_subauth );
  847. logon_sid = security_sid_alloc( &nt_authority, sizeof(logon_subauth)/sizeof(logon_subauth[0]),
  848. logon_subauth );
  849. if (alias_admins_sid && alias_users_sid && logon_sid && default_dacl)
  850. {
  851. const LUID_AND_ATTRIBUTES user_privs[] =
  852. {
  853. { SeChangeNotifyPrivilege , SE_PRIVILEGE_ENABLED },
  854. { SeShutdownPrivilege , 0 },
  855. { SeUndockPrivilege , 0 },
  856. };
  857. /* note: we don't include non-builtin groups here for the user -
  858. * telling us these is the job of a client-side program */
  859. const SID_AND_ATTRIBUTES user_groups[] =
  860. {
  861. { security_world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  862. { security_local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  863. { security_interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  864. { security_authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  865. { security_domain_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
  866. { alias_admins_sid, SE_GROUP_USE_FOR_DENY_ONLY },
  867. { alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  868. { logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
  869. };
  870. static const TOKEN_SOURCE admin_source = {"SeMgr", {0, 0}};
  871. token = create_token( TRUE, user_sid, user_groups, sizeof(user_groups)/sizeof(user_groups[0]),
  872. user_privs, sizeof(user_privs)/sizeof(user_privs[0]), default_dacl,
  873. admin_source, NULL, -1, TokenElevationTypeLimited, &medium_label_sid );
  874. /* we really need a primary group */
  875. assert( token->primary_group );
  876. if (!token_assign_label( token, (PSID)token->integrity ))
  877. {
  878. release_object( token );
  879. token = NULL;
  880. }
  881. }
  882. free( logon_sid );
  883. free( alias_admins_sid );
  884. free( alias_users_sid );
  885. free( default_dacl );
  886. return token;
  887. }
  888. static struct privilege *token_find_privilege( struct token *token, const LUID *luid, int enabled_only )
  889. {
  890. struct privilege *privilege;
  891. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  892. {
  893. if (is_equal_luid( luid, &privilege->luid ))
  894. {
  895. if (enabled_only && !privilege->enabled)
  896. return NULL;
  897. return privilege;
  898. }
  899. }
  900. return NULL;
  901. }
  902. static unsigned int token_adjust_privileges( struct token *token, const LUID_AND_ATTRIBUTES *privs,
  903. unsigned int count, LUID_AND_ATTRIBUTES *mod_privs,
  904. unsigned int mod_privs_count )
  905. {
  906. unsigned int i, modified_count = 0;
  907. /* mark as modified */
  908. allocate_luid( &token->modified_id );
  909. for (i = 0; i < count; i++)
  910. {
  911. struct privilege *privilege =
  912. token_find_privilege( token, &privs[i].Luid, FALSE );
  913. if (!privilege)
  914. {
  915. set_error( STATUS_NOT_ALL_ASSIGNED );
  916. continue;
  917. }
  918. if (privs[i].Attributes & SE_PRIVILEGE_REMOVED)
  919. privilege_remove( privilege );
  920. else
  921. {
  922. /* save previous state for caller */
  923. if (mod_privs_count)
  924. {
  925. luid_and_attr_from_privilege(mod_privs, privilege);
  926. mod_privs++;
  927. mod_privs_count--;
  928. modified_count++;
  929. }
  930. if (privs[i].Attributes & SE_PRIVILEGE_ENABLED)
  931. privilege->enabled = TRUE;
  932. else
  933. privilege->enabled = FALSE;
  934. }
  935. }
  936. return modified_count;
  937. }
  938. static void token_disable_privileges( struct token *token )
  939. {
  940. struct privilege *privilege;
  941. /* mark as modified */
  942. allocate_luid( &token->modified_id );
  943. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  944. privilege->enabled = FALSE;
  945. }
  946. int token_check_privileges( struct token *token, int all_required,
  947. const LUID_AND_ATTRIBUTES *reqprivs,
  948. unsigned int count, LUID_AND_ATTRIBUTES *usedprivs)
  949. {
  950. unsigned int i, enabled_count = 0;
  951. for (i = 0; i < count; i++)
  952. {
  953. struct privilege *privilege =
  954. token_find_privilege( token, &reqprivs[i].Luid, TRUE );
  955. if (usedprivs)
  956. usedprivs[i] = reqprivs[i];
  957. if (privilege && privilege->enabled)
  958. {
  959. enabled_count++;
  960. if (usedprivs)
  961. usedprivs[i].Attributes |= SE_PRIVILEGE_USED_FOR_ACCESS;
  962. }
  963. }
  964. if (all_required)
  965. return (enabled_count == count);
  966. else
  967. return (enabled_count > 0);
  968. }
  969. int token_sid_present( struct token *token, const SID *sid, int deny )
  970. {
  971. struct group *group;
  972. if (security_equal_sid( token->user, sid )) return TRUE;
  973. LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
  974. {
  975. if (!group->enabled) continue;
  976. if (group->deny_only && !deny) continue;
  977. if (security_equal_sid( &group->sid, sid )) return TRUE;
  978. }
  979. return FALSE;
  980. }
  981. /* Checks access to a security descriptor. 'sd' must have been validated by
  982. * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
  983. * the reason. 'status' parameter will indicate if access is granted or denied.
  984. *
  985. * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
  986. */
  987. static unsigned int token_access_check( struct token *token,
  988. const struct security_descriptor *sd,
  989. unsigned int desired_access,
  990. LUID_AND_ATTRIBUTES *privs,
  991. unsigned int *priv_count,
  992. const GENERIC_MAPPING *mapping,
  993. unsigned int *granted_access,
  994. unsigned int *status )
  995. {
  996. unsigned int current_access = 0;
  997. unsigned int denied_access = 0;
  998. ULONG i;
  999. const ACL *dacl;
  1000. int dacl_present;
  1001. const ACE_HEADER *ace;
  1002. const SID *owner;
  1003. /* assume no access rights */
  1004. *granted_access = 0;
  1005. /* fail if desired_access contains generic rights */
  1006. if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
  1007. {
  1008. if (priv_count) *priv_count = 0;
  1009. return STATUS_GENERIC_NOT_MAPPED;
  1010. }
  1011. dacl = sd_get_dacl( sd, &dacl_present );
  1012. owner = sd_get_owner( sd );
  1013. if (!owner || !sd_get_group( sd ))
  1014. {
  1015. if (priv_count) *priv_count = 0;
  1016. return STATUS_INVALID_SECURITY_DESCR;
  1017. }
  1018. /* 1: Grant desired access if the object is unprotected */
  1019. if (!dacl_present || !dacl)
  1020. {
  1021. if (priv_count) *priv_count = 0;
  1022. if (desired_access & MAXIMUM_ALLOWED)
  1023. *granted_access = mapping->GenericAll;
  1024. else
  1025. *granted_access = desired_access;
  1026. return *status = STATUS_SUCCESS;
  1027. }
  1028. /* 2: Check if caller wants access to system security part. Note: access
  1029. * is only granted if specifically asked for */
  1030. if (desired_access & ACCESS_SYSTEM_SECURITY)
  1031. {
  1032. const LUID_AND_ATTRIBUTES security_priv = { SeSecurityPrivilege, 0 };
  1033. LUID_AND_ATTRIBUTES retpriv = security_priv;
  1034. if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
  1035. {
  1036. if (priv_count)
  1037. {
  1038. /* assumes that there will only be one privilege to return */
  1039. if (*priv_count >= 1)
  1040. {
  1041. *priv_count = 1;
  1042. *privs = retpriv;
  1043. }
  1044. else
  1045. {
  1046. *priv_count = 1;
  1047. return STATUS_BUFFER_TOO_SMALL;
  1048. }
  1049. }
  1050. current_access |= ACCESS_SYSTEM_SECURITY;
  1051. if (desired_access == current_access)
  1052. {
  1053. *granted_access = current_access;
  1054. return *status = STATUS_SUCCESS;
  1055. }
  1056. }
  1057. else
  1058. {
  1059. if (priv_count) *priv_count = 0;
  1060. *status = STATUS_PRIVILEGE_NOT_HELD;
  1061. return STATUS_SUCCESS;
  1062. }
  1063. }
  1064. else if (priv_count) *priv_count = 0;
  1065. /* 3: Check whether the token is the owner */
  1066. /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
  1067. * checked when a "set owner" call is made, overriding the access rights
  1068. * determined here. */
  1069. if (token_sid_present( token, owner, FALSE ))
  1070. {
  1071. current_access |= (READ_CONTROL | WRITE_DAC);
  1072. if (desired_access == current_access)
  1073. {
  1074. *granted_access = current_access;
  1075. return *status = STATUS_SUCCESS;
  1076. }
  1077. }
  1078. /* 4: Grant rights according to the DACL */
  1079. ace = (const ACE_HEADER *)(dacl + 1);
  1080. for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
  1081. {
  1082. const ACCESS_ALLOWED_ACE *aa_ace;
  1083. const ACCESS_DENIED_ACE *ad_ace;
  1084. const SID *sid;
  1085. if (ace->AceFlags & INHERIT_ONLY_ACE)
  1086. continue;
  1087. switch (ace->AceType)
  1088. {
  1089. case ACCESS_DENIED_ACE_TYPE:
  1090. ad_ace = (const ACCESS_DENIED_ACE *)ace;
  1091. sid = (const SID *)&ad_ace->SidStart;
  1092. if (token_sid_present( token, sid, TRUE ))
  1093. {
  1094. unsigned int access = ad_ace->Mask;
  1095. map_generic_mask(&access, mapping);
  1096. if (desired_access & MAXIMUM_ALLOWED)
  1097. denied_access |= access;
  1098. else
  1099. {
  1100. denied_access |= (access & ~current_access);
  1101. if (desired_access & access) goto done;
  1102. }
  1103. }
  1104. break;
  1105. case ACCESS_ALLOWED_ACE_TYPE:
  1106. aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
  1107. sid = (const SID *)&aa_ace->SidStart;
  1108. if (token_sid_present( token, sid, FALSE ))
  1109. {
  1110. unsigned int access = aa_ace->Mask;
  1111. map_generic_mask(&access, mapping);
  1112. if (desired_access & MAXIMUM_ALLOWED)
  1113. current_access |= access;
  1114. else
  1115. current_access |= (access & ~denied_access);
  1116. }
  1117. break;
  1118. }
  1119. /* don't bother carrying on checking if we've already got all of
  1120. * rights we need */
  1121. if (desired_access == *granted_access)
  1122. break;
  1123. }
  1124. done:
  1125. if (desired_access & MAXIMUM_ALLOWED)
  1126. *granted_access = current_access & ~denied_access;
  1127. else
  1128. if ((current_access & desired_access) == desired_access)
  1129. *granted_access = current_access & desired_access;
  1130. else
  1131. *granted_access = 0;
  1132. *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
  1133. return STATUS_SUCCESS;
  1134. }
  1135. const ACL *token_get_default_dacl( struct token *token )
  1136. {
  1137. return token->default_dacl;
  1138. }
  1139. const SID *token_get_user( struct token *token )
  1140. {
  1141. return token->user;
  1142. }
  1143. const SID *token_get_primary_group( struct token *token )
  1144. {
  1145. return token->primary_group;
  1146. }
  1147. int token_is_primary( struct token *token )
  1148. {
  1149. return token->primary;
  1150. }
  1151. int check_object_access(struct object *obj, unsigned int *access)
  1152. {
  1153. GENERIC_MAPPING mapping;
  1154. struct token *token = current->token ? current->token : current->process->token;
  1155. unsigned int status;
  1156. int res;
  1157. mapping.GenericAll = obj->ops->map_access( obj, GENERIC_ALL );
  1158. if (!obj->sd)
  1159. {
  1160. if (*access & MAXIMUM_ALLOWED)
  1161. *access = mapping.GenericAll;
  1162. return TRUE;
  1163. }
  1164. mapping.GenericRead = obj->ops->map_access( obj, GENERIC_READ );
  1165. mapping.GenericWrite = obj->ops->map_access( obj, GENERIC_WRITE );
  1166. mapping.GenericExecute = obj->ops->map_access( obj, GENERIC_EXECUTE );
  1167. res = token_access_check( token, obj->sd, *access, NULL, NULL,
  1168. &mapping, access, &status ) == STATUS_SUCCESS &&
  1169. status == STATUS_SUCCESS;
  1170. if (!res) set_error( STATUS_ACCESS_DENIED );
  1171. return res;
  1172. }
  1173. /* open a security token */
  1174. DECL_HANDLER(open_token)
  1175. {
  1176. if (req->flags & OPEN_TOKEN_THREAD)
  1177. {
  1178. struct thread *thread = get_thread_from_handle( req->handle, 0 );
  1179. if (thread)
  1180. {
  1181. if (thread->token)
  1182. {
  1183. if (!thread->token->primary && thread->token->impersonation_level <= SecurityAnonymous)
  1184. set_error( STATUS_CANT_OPEN_ANONYMOUS );
  1185. else
  1186. reply->token = alloc_handle( current->process, thread->token,
  1187. req->access, req->attributes );
  1188. }
  1189. else
  1190. set_error( STATUS_NO_TOKEN );
  1191. release_object( thread );
  1192. }
  1193. }
  1194. else
  1195. {
  1196. struct process *process = get_process_from_handle( req->handle, 0 );
  1197. if (process)
  1198. {
  1199. if (process->token)
  1200. reply->token = alloc_handle( current->process, process->token, req->access,
  1201. req->attributes );
  1202. else
  1203. set_error( STATUS_NO_TOKEN );
  1204. release_object( process );
  1205. }
  1206. }
  1207. }
  1208. /* adjust the privileges held by a token */
  1209. DECL_HANDLER(adjust_token_privileges)
  1210. {
  1211. struct token *token;
  1212. unsigned int access = TOKEN_ADJUST_PRIVILEGES;
  1213. if (req->get_modified_state) access |= TOKEN_QUERY;
  1214. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1215. access, &token_ops )))
  1216. {
  1217. const LUID_AND_ATTRIBUTES *privs = get_req_data();
  1218. LUID_AND_ATTRIBUTES *modified_privs = NULL;
  1219. unsigned int priv_count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
  1220. unsigned int modified_priv_count = 0;
  1221. if (req->get_modified_state && !req->disable_all)
  1222. {
  1223. unsigned int i;
  1224. /* count modified privs */
  1225. for (i = 0; i < priv_count; i++)
  1226. {
  1227. struct privilege *privilege =
  1228. token_find_privilege( token, &privs[i].Luid, FALSE );
  1229. if (privilege && req->get_modified_state)
  1230. modified_priv_count++;
  1231. }
  1232. reply->len = modified_priv_count;
  1233. modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
  1234. if (modified_priv_count)
  1235. modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
  1236. }
  1237. reply->len = modified_priv_count * sizeof(*modified_privs);
  1238. if (req->disable_all)
  1239. token_disable_privileges( token );
  1240. else
  1241. token_adjust_privileges( token, privs, priv_count, modified_privs, modified_priv_count );
  1242. release_object( token );
  1243. }
  1244. }
  1245. /* retrieves the list of privileges that may be held be the token */
  1246. DECL_HANDLER(get_token_privileges)
  1247. {
  1248. struct token *token;
  1249. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1250. TOKEN_QUERY,
  1251. &token_ops )))
  1252. {
  1253. int priv_count = 0;
  1254. LUID_AND_ATTRIBUTES *privs;
  1255. struct privilege *privilege;
  1256. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  1257. priv_count++;
  1258. reply->len = priv_count * sizeof(*privs);
  1259. if (reply->len <= get_reply_max_size())
  1260. {
  1261. privs = set_reply_data_size( priv_count * sizeof(*privs) );
  1262. if (privs)
  1263. {
  1264. int i = 0;
  1265. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  1266. {
  1267. luid_and_attr_from_privilege( &privs[i], privilege );
  1268. i++;
  1269. }
  1270. }
  1271. }
  1272. else
  1273. set_error(STATUS_BUFFER_TOO_SMALL);
  1274. release_object( token );
  1275. }
  1276. }
  1277. /* creates a duplicate of the token */
  1278. DECL_HANDLER(duplicate_token)
  1279. {
  1280. struct token *src_token;
  1281. struct unicode_str name;
  1282. const struct security_descriptor *sd;
  1283. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
  1284. if (!objattr) return;
  1285. if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
  1286. TOKEN_DUPLICATE,
  1287. &token_ops )))
  1288. {
  1289. struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level, sd, NULL, 0,
  1290. NULL, 0, thread_get_impersonation_token( current ) );
  1291. if (token)
  1292. {
  1293. unsigned int access = req->access ? req->access : get_handle_access( current->process, req->handle );
  1294. reply->new_handle = alloc_handle_no_access_check( current->process, token, access, objattr->attributes );
  1295. release_object( token );
  1296. }
  1297. release_object( src_token );
  1298. }
  1299. }
  1300. /* creates a restricted version of a token */
  1301. DECL_HANDLER(filter_token)
  1302. {
  1303. struct token *src_token;
  1304. if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
  1305. TOKEN_DUPLICATE,
  1306. &token_ops )))
  1307. {
  1308. const LUID_AND_ATTRIBUTES *filter_privileges = get_req_data();
  1309. unsigned int priv_count, group_count;
  1310. const SID *filter_groups;
  1311. struct token *token;
  1312. priv_count = min( req->privileges_size, get_req_data_size() ) / sizeof(LUID_AND_ATTRIBUTES);
  1313. filter_groups = (const SID *)((char *)filter_privileges + priv_count * sizeof(LUID_AND_ATTRIBUTES));
  1314. group_count = get_sid_count( filter_groups, get_req_data_size() - priv_count * sizeof(LUID_AND_ATTRIBUTES) );
  1315. token = token_duplicate( src_token, src_token->primary, src_token->impersonation_level, NULL,
  1316. filter_privileges, priv_count, filter_groups, group_count, thread_get_impersonation_token( current ) );
  1317. if (token)
  1318. {
  1319. unsigned int access = get_handle_access( current->process, req->handle );
  1320. reply->new_handle = alloc_handle_no_access_check( current->process, token, access, 0 );
  1321. release_object( token );
  1322. }
  1323. release_object( src_token );
  1324. }
  1325. }
  1326. /* checks the specified privileges are held by the token */
  1327. DECL_HANDLER(check_token_privileges)
  1328. {
  1329. struct token *token;
  1330. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1331. TOKEN_QUERY,
  1332. &token_ops )))
  1333. {
  1334. unsigned int count = get_req_data_size() / sizeof(LUID_AND_ATTRIBUTES);
  1335. if (!token->primary && token->impersonation_level <= SecurityAnonymous)
  1336. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  1337. else if (get_reply_max_size() >= count * sizeof(LUID_AND_ATTRIBUTES))
  1338. {
  1339. LUID_AND_ATTRIBUTES *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
  1340. reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
  1341. }
  1342. else
  1343. set_error( STATUS_BUFFER_OVERFLOW );
  1344. release_object( token );
  1345. }
  1346. }
  1347. /* checks that a user represented by a token is allowed to access an object
  1348. * represented by a security descriptor */
  1349. DECL_HANDLER(access_check)
  1350. {
  1351. data_size_t sd_size = get_req_data_size();
  1352. const struct security_descriptor *sd = get_req_data();
  1353. struct token *token;
  1354. if (!sd_is_valid( sd, sd_size ))
  1355. {
  1356. set_error( STATUS_ACCESS_VIOLATION );
  1357. return;
  1358. }
  1359. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1360. TOKEN_QUERY,
  1361. &token_ops )))
  1362. {
  1363. GENERIC_MAPPING mapping;
  1364. unsigned int status;
  1365. LUID_AND_ATTRIBUTES priv;
  1366. unsigned int priv_count = 1;
  1367. memset(&priv, 0, sizeof(priv));
  1368. /* only impersonation tokens may be used with this function */
  1369. if (token->primary)
  1370. {
  1371. set_error( STATUS_NO_IMPERSONATION_TOKEN );
  1372. release_object( token );
  1373. return;
  1374. }
  1375. /* anonymous impersonation tokens can't be used */
  1376. if (token->impersonation_level <= SecurityAnonymous)
  1377. {
  1378. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  1379. release_object( token );
  1380. return;
  1381. }
  1382. mapping.GenericRead = req->mapping_read;
  1383. mapping.GenericWrite = req->mapping_write;
  1384. mapping.GenericExecute = req->mapping_execute;
  1385. mapping.GenericAll = req->mapping_all;
  1386. status = token_access_check(
  1387. token, sd, req->desired_access, &priv, &priv_count, &mapping,
  1388. &reply->access_granted, &reply->access_status );
  1389. reply->privileges_len = priv_count*sizeof(LUID_AND_ATTRIBUTES);
  1390. if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
  1391. {
  1392. LUID_AND_ATTRIBUTES *privs = set_reply_data_size( priv_count * sizeof(*privs) );
  1393. memcpy( privs, &priv, sizeof(priv) );
  1394. }
  1395. set_error( status );
  1396. release_object( token );
  1397. }
  1398. }
  1399. /* retrieves an SID from the token */
  1400. DECL_HANDLER(get_token_sid)
  1401. {
  1402. struct token *token;
  1403. reply->sid_len = 0;
  1404. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1405. {
  1406. const SID *sid = NULL;
  1407. switch (req->which_sid)
  1408. {
  1409. case TokenUser:
  1410. assert(token->user);
  1411. sid = token->user;
  1412. break;
  1413. case TokenPrimaryGroup:
  1414. sid = token->primary_group;
  1415. break;
  1416. case TokenOwner:
  1417. sid = token->owner;
  1418. break;
  1419. case TokenLogonSid:
  1420. sid = (const SID *)&builtin_logon_sid;
  1421. break;
  1422. default:
  1423. set_error( STATUS_INVALID_PARAMETER );
  1424. break;
  1425. }
  1426. if (sid)
  1427. {
  1428. reply->sid_len = security_sid_len( sid );
  1429. if (reply->sid_len <= get_reply_max_size()) set_reply_data( sid, reply->sid_len );
  1430. else set_error( STATUS_BUFFER_TOO_SMALL );
  1431. }
  1432. release_object( token );
  1433. }
  1434. }
  1435. /* retrieves the integrity sid */
  1436. DECL_HANDLER(get_token_integrity)
  1437. {
  1438. struct token *token;
  1439. reply->sid_len = 0;
  1440. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1441. TOKEN_QUERY,
  1442. &token_ops )))
  1443. {
  1444. reply->sid_len = security_sid_len( token->integrity );
  1445. if (reply->sid_len <= get_reply_max_size())
  1446. set_reply_data( token->integrity, reply->sid_len );
  1447. else
  1448. set_error( STATUS_BUFFER_TOO_SMALL );
  1449. release_object( token );
  1450. }
  1451. }
  1452. /* retrieves the groups that the user represented by the token belongs to */
  1453. DECL_HANDLER(get_token_groups)
  1454. {
  1455. struct token *token;
  1456. reply->user_len = 0;
  1457. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1458. TOKEN_QUERY,
  1459. &token_ops )))
  1460. {
  1461. size_t size_needed = sizeof(struct token_groups);
  1462. size_t sid_size = 0;
  1463. unsigned int group_count = 0;
  1464. const struct group *group;
  1465. LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
  1466. {
  1467. group_count++;
  1468. sid_size += security_sid_len( &group->sid );
  1469. }
  1470. size_needed += sid_size;
  1471. /* attributes size */
  1472. size_needed += sizeof(unsigned int) * group_count;
  1473. /* reply buffer contains size_needed bytes formatted as:
  1474. unsigned int count;
  1475. unsigned int attrib[count];
  1476. char sid_data[];
  1477. user_len includes extra data needed for TOKEN_GROUPS representation,
  1478. required caller buffer size calculated here to avoid extra server call */
  1479. reply->user_len = FIELD_OFFSET( TOKEN_GROUPS, Groups[group_count] ) + sid_size;
  1480. if (reply->user_len <= get_reply_max_size())
  1481. {
  1482. struct token_groups *tg = set_reply_data_size( size_needed );
  1483. if (tg)
  1484. {
  1485. unsigned int *attr_ptr = (unsigned int *)(tg + 1);
  1486. SID *sid_ptr = (SID *)(attr_ptr + group_count);
  1487. tg->count = group_count;
  1488. LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
  1489. {
  1490. *attr_ptr = 0;
  1491. if (group->mandatory) *attr_ptr |= SE_GROUP_MANDATORY;
  1492. if (group->def) *attr_ptr |= SE_GROUP_ENABLED_BY_DEFAULT;
  1493. if (group->enabled) *attr_ptr |= SE_GROUP_ENABLED;
  1494. if (group->owner) *attr_ptr |= SE_GROUP_OWNER;
  1495. if (group->deny_only) *attr_ptr |= SE_GROUP_USE_FOR_DENY_ONLY;
  1496. if (group->logon) *attr_ptr |= SE_GROUP_LOGON_ID;
  1497. if (group->resource) *attr_ptr |= SE_GROUP_RESOURCE;
  1498. memcpy(sid_ptr, &group->sid, security_sid_len( &group->sid ));
  1499. sid_ptr = (SID *)((char *)sid_ptr + security_sid_len( &group->sid ));
  1500. attr_ptr++;
  1501. }
  1502. }
  1503. }
  1504. else set_error( STATUS_BUFFER_TOO_SMALL );
  1505. release_object( token );
  1506. }
  1507. }
  1508. DECL_HANDLER(get_token_impersonation_level)
  1509. {
  1510. struct token *token;
  1511. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1512. TOKEN_QUERY,
  1513. &token_ops )))
  1514. {
  1515. if (token->primary)
  1516. set_error( STATUS_INVALID_PARAMETER );
  1517. else
  1518. reply->impersonation_level = token->impersonation_level;
  1519. release_object( token );
  1520. }
  1521. }
  1522. DECL_HANDLER(get_token_statistics)
  1523. {
  1524. struct token *token;
  1525. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1526. TOKEN_QUERY,
  1527. &token_ops )))
  1528. {
  1529. reply->token_id = token->token_id;
  1530. reply->modified_id = token->modified_id;
  1531. reply->primary = token->primary;
  1532. reply->impersonation_level = token->impersonation_level;
  1533. reply->group_count = list_count( &token->groups );
  1534. reply->privilege_count = list_count( &token->privileges );
  1535. release_object( token );
  1536. }
  1537. }
  1538. DECL_HANDLER(get_token_elevation_type)
  1539. {
  1540. struct token *token;
  1541. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1542. TOKEN_QUERY,
  1543. &token_ops )))
  1544. {
  1545. reply->elevation = token->elevation;
  1546. release_object( token );
  1547. }
  1548. }
  1549. DECL_HANDLER(get_token_default_dacl)
  1550. {
  1551. struct token *token;
  1552. reply->acl_len = 0;
  1553. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1554. TOKEN_QUERY,
  1555. &token_ops )))
  1556. {
  1557. if (token->default_dacl)
  1558. reply->acl_len = token->default_dacl->AclSize;
  1559. if (reply->acl_len <= get_reply_max_size())
  1560. {
  1561. ACL *acl_reply = set_reply_data_size( reply->acl_len );
  1562. if (acl_reply)
  1563. memcpy( acl_reply, token->default_dacl, reply->acl_len );
  1564. }
  1565. else set_error( STATUS_BUFFER_TOO_SMALL );
  1566. release_object( token );
  1567. }
  1568. }
  1569. DECL_HANDLER(set_token_default_dacl)
  1570. {
  1571. struct token *token;
  1572. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1573. TOKEN_ADJUST_DEFAULT,
  1574. &token_ops )))
  1575. {
  1576. const ACL *acl = get_req_data();
  1577. unsigned int acl_size = get_req_data_size();
  1578. free( token->default_dacl );
  1579. token->default_dacl = NULL;
  1580. if (acl_size)
  1581. token->default_dacl = memdup( acl, acl_size );
  1582. release_object( token );
  1583. }
  1584. }
  1585. DECL_HANDLER(create_token)
  1586. {
  1587. struct token *token;
  1588. token = req->admin ? token_create_admin() : token_create_limited();
  1589. if (token)
  1590. {
  1591. reply->token = alloc_handle( current->process, token, TOKEN_ALL_ACCESS, 0 );
  1592. release_object( token );
  1593. }
  1594. }
  1595. /* Replaces the token of the current process */
  1596. DECL_HANDLER(replace_process_token)
  1597. {
  1598. struct token *token;
  1599. if ((token = (struct token *)get_handle_obj( current->process, req->token,
  1600. TOKEN_ASSIGN_PRIMARY,
  1601. &token_ops )))
  1602. {
  1603. replace_process_token( current->process, token );
  1604. release_object( token );
  1605. }
  1606. }