token.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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 <sys/types.h>
  28. #include <unistd.h>
  29. #include "ntstatus.h"
  30. #define WIN32_NO_STATUS
  31. #include "windef.h"
  32. #include "winternl.h"
  33. #include "handle.h"
  34. #include "thread.h"
  35. #include "process.h"
  36. #include "request.h"
  37. #include "security.h"
  38. #define MAX_SUBAUTH_COUNT 1
  39. const struct luid SeIncreaseQuotaPrivilege = { 5, 0 };
  40. const struct luid SeTcbPrivilege = { 7, 0 };
  41. const struct luid SeSecurityPrivilege = { 8, 0 };
  42. const struct luid SeTakeOwnershipPrivilege = { 9, 0 };
  43. const struct luid SeLoadDriverPrivilege = { 10, 0 };
  44. const struct luid SeSystemProfilePrivilege = { 11, 0 };
  45. const struct luid SeSystemtimePrivilege = { 12, 0 };
  46. const struct luid SeProfileSingleProcessPrivilege = { 13, 0 };
  47. const struct luid SeIncreaseBasePriorityPrivilege = { 14, 0 };
  48. const struct luid SeCreatePagefilePrivilege = { 15, 0 };
  49. const struct luid SeBackupPrivilege = { 17, 0 };
  50. const struct luid SeRestorePrivilege = { 18, 0 };
  51. const struct luid SeShutdownPrivilege = { 19, 0 };
  52. const struct luid SeDebugPrivilege = { 20, 0 };
  53. const struct luid SeSystemEnvironmentPrivilege = { 22, 0 };
  54. const struct luid SeChangeNotifyPrivilege = { 23, 0 };
  55. const struct luid SeRemoteShutdownPrivilege = { 24, 0 };
  56. const struct luid SeUndockPrivilege = { 25, 0 };
  57. const struct luid SeManageVolumePrivilege = { 28, 0 };
  58. const struct luid SeImpersonatePrivilege = { 29, 0 };
  59. const struct luid SeCreateGlobalPrivilege = { 30, 0 };
  60. struct sid_attrs
  61. {
  62. const struct sid *sid;
  63. unsigned int attrs;
  64. };
  65. const struct sid world_sid = { SID_REVISION, 1, SECURITY_WORLD_SID_AUTHORITY, { SECURITY_WORLD_RID } };
  66. const struct sid local_system_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_LOCAL_SYSTEM_RID } };
  67. const struct sid high_label_sid = { SID_REVISION, 1, SECURITY_MANDATORY_LABEL_AUTHORITY, { SECURITY_MANDATORY_HIGH_RID } };
  68. const struct sid local_user_sid = { SID_REVISION, 5, SECURITY_NT_AUTHORITY, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, 1000 } };
  69. const struct sid builtin_admins_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } };
  70. const struct sid builtin_users_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } };
  71. const struct sid domain_users_sid = { SID_REVISION, 5, SECURITY_NT_AUTHORITY, { SECURITY_NT_NON_UNIQUE, 0, 0, 0, DOMAIN_GROUP_RID_USERS } };
  72. static const struct sid local_sid = { SID_REVISION, 1, SECURITY_LOCAL_SID_AUTHORITY, { SECURITY_LOCAL_RID } };
  73. static const struct sid interactive_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_INTERACTIVE_RID } };
  74. static const struct sid anonymous_logon_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_ANONYMOUS_LOGON_RID } };
  75. static const struct sid authenticated_user_sid = { SID_REVISION, 1, SECURITY_NT_AUTHORITY, { SECURITY_AUTHENTICATED_USER_RID } };
  76. static struct luid prev_luid_value = { 1000, 0 };
  77. static const WCHAR token_name[] = {'T','o','k','e','n'};
  78. struct type_descr token_type =
  79. {
  80. { token_name, sizeof(token_name) }, /* name */
  81. TOKEN_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
  82. { /* mapping */
  83. STANDARD_RIGHTS_READ | TOKEN_QUERY_SOURCE | TOKEN_QUERY | TOKEN_DUPLICATE,
  84. STANDARD_RIGHTS_WRITE | TOKEN_ADJUST_SESSIONID | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_GROUPS
  85. | TOKEN_ADJUST_PRIVILEGES,
  86. STANDARD_RIGHTS_EXECUTE | TOKEN_IMPERSONATE | TOKEN_ASSIGN_PRIMARY,
  87. TOKEN_ALL_ACCESS
  88. },
  89. };
  90. struct token
  91. {
  92. struct object obj; /* object header */
  93. struct luid token_id; /* system-unique id of token */
  94. struct luid 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. struct sid *user; /* SID of user this token represents */
  98. struct sid *owner; /* SID of owner (points to user or one of groups) */
  99. struct sid *primary_group; /* SID of user's primary group (points to one of groups) */
  100. unsigned int primary; /* is this a primary or impersonation token? */
  101. unsigned int session_id; /* token session id */
  102. struct acl *default_dacl; /* the default DACL to assign to objects created by this user */
  103. int impersonation_level; /* impersonation level this token is capable of if non-primary token */
  104. int elevation; /* elevation type */
  105. };
  106. struct privilege
  107. {
  108. struct list entry;
  109. struct luid luid;
  110. unsigned enabled : 1; /* is the privilege currently enabled? */
  111. unsigned def : 1; /* is the privilege enabled by default? */
  112. };
  113. struct group
  114. {
  115. struct list entry;
  116. unsigned int attrs;
  117. struct sid sid;
  118. };
  119. static void token_dump( struct object *obj, int verbose );
  120. static void token_destroy( struct object *obj );
  121. static const struct object_ops token_ops =
  122. {
  123. sizeof(struct token), /* size */
  124. &token_type, /* type */
  125. token_dump, /* dump */
  126. no_add_queue, /* add_queue */
  127. NULL, /* remove_queue */
  128. NULL, /* signaled */
  129. NULL, /* satisfied */
  130. no_signal, /* signal */
  131. no_get_fd, /* get_fd */
  132. default_map_access, /* map_access */
  133. default_get_sd, /* get_sd */
  134. default_set_sd, /* set_sd */
  135. no_get_full_name, /* get_full_name */
  136. no_lookup_name, /* lookup_name */
  137. no_link_name, /* link_name */
  138. NULL, /* unlink_name */
  139. no_open_file, /* open_file */
  140. no_kernel_obj_list, /* get_kernel_obj_list */
  141. no_close_handle, /* close_handle */
  142. token_destroy /* destroy */
  143. };
  144. static void token_dump( struct object *obj, int verbose )
  145. {
  146. struct token *token = (struct token *)obj;
  147. assert( obj->ops == &token_ops );
  148. fprintf( stderr, "Token id=%d.%u primary=%u impersonation level=%d\n", token->token_id.high_part,
  149. token->token_id.low_part, token->primary, token->impersonation_level );
  150. }
  151. void security_set_thread_token( struct thread *thread, obj_handle_t handle )
  152. {
  153. if (!handle)
  154. {
  155. if (thread->token)
  156. release_object( thread->token );
  157. thread->token = NULL;
  158. }
  159. else
  160. {
  161. struct token *token = (struct token *)get_handle_obj( current->process,
  162. handle,
  163. TOKEN_IMPERSONATE,
  164. &token_ops );
  165. if (token)
  166. {
  167. if (thread->token)
  168. release_object( thread->token );
  169. thread->token = token;
  170. }
  171. }
  172. }
  173. const struct sid *security_unix_uid_to_sid( uid_t uid )
  174. {
  175. /* very simple mapping: either the current user or not the current user */
  176. if (uid == getuid())
  177. return &local_user_sid;
  178. else
  179. return &anonymous_logon_sid;
  180. }
  181. static int acl_is_valid( const struct acl *acl, data_size_t size )
  182. {
  183. ULONG i;
  184. const struct ace *ace;
  185. if (size < sizeof(*acl)) return FALSE;
  186. size = min(size, MAX_ACL_LEN);
  187. size -= sizeof(*acl);
  188. for (i = 0, ace = ace_first( acl ); i < acl->count; i++, ace = ace_next( ace ))
  189. {
  190. if (size < sizeof(*ace) || size < ace->size) return FALSE;
  191. size -= ace->size;
  192. switch (ace->type)
  193. {
  194. case ACCESS_DENIED_ACE_TYPE:
  195. case ACCESS_ALLOWED_ACE_TYPE:
  196. case SYSTEM_AUDIT_ACE_TYPE:
  197. case SYSTEM_ALARM_ACE_TYPE:
  198. case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
  199. break;
  200. default:
  201. return FALSE;
  202. }
  203. if (!sid_valid_size( (const struct sid *)(ace + 1), ace->size - sizeof(*ace) )) return FALSE;
  204. }
  205. return TRUE;
  206. }
  207. static unsigned int get_sid_count( const struct sid *sid, data_size_t size )
  208. {
  209. unsigned int count;
  210. for (count = 0; sid_valid_size( sid, size ); count++)
  211. {
  212. size -= sid_len( sid );
  213. sid = (const struct sid *)((char *)sid + sid_len( sid ));
  214. }
  215. return count;
  216. }
  217. /* checks whether all members of a security descriptor fit inside the size
  218. * of memory specified */
  219. int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
  220. {
  221. size_t offset = sizeof(struct security_descriptor);
  222. const struct sid *group;
  223. const struct sid *owner;
  224. const struct acl *sacl;
  225. const struct acl *dacl;
  226. int dummy;
  227. if (size < offset)
  228. return FALSE;
  229. if (sd->owner_len >= offsetof(struct sid, sub_auth[255]) || offset + sd->owner_len > size) return FALSE;
  230. owner = sd_get_owner( sd );
  231. offset += sd->owner_len;
  232. if (owner && !sid_valid_size( owner, sd->owner_len )) return FALSE;
  233. if (sd->group_len >= offsetof(struct sid, sub_auth[255]) || offset + sd->group_len > size) return FALSE;
  234. group = sd_get_group( sd );
  235. offset += sd->group_len;
  236. if (group && !sid_valid_size( group, sd->group_len )) return FALSE;
  237. if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
  238. return FALSE;
  239. sacl = sd_get_sacl( sd, &dummy );
  240. if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
  241. return FALSE;
  242. offset += sd->sacl_len;
  243. if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
  244. return FALSE;
  245. dacl = sd_get_dacl( sd, &dummy );
  246. if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
  247. return FALSE;
  248. offset += sd->dacl_len;
  249. return TRUE;
  250. }
  251. /* extract security labels from SACL */
  252. struct acl *extract_security_labels( const struct acl *sacl )
  253. {
  254. size_t size = sizeof(*sacl);
  255. const struct ace *ace;
  256. struct ace *label_ace;
  257. unsigned int i, count = 0;
  258. struct acl *label_acl;
  259. for (i = 0, ace = ace_first( sacl ); i < sacl->count; i++, ace = ace_next( ace ))
  260. {
  261. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
  262. {
  263. size += ace->size;
  264. count++;
  265. }
  266. }
  267. label_acl = mem_alloc( size );
  268. if (!label_acl) return NULL;
  269. label_acl->revision = sacl->revision;
  270. label_acl->pad1 = 0;
  271. label_acl->size = size;
  272. label_acl->count = count;
  273. label_acl->pad2 = 0;
  274. label_ace = ace_first( label_acl );
  275. for (i = 0, ace = ace_first( sacl ); i < sacl->count; i++, ace = ace_next( ace ))
  276. {
  277. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
  278. {
  279. memcpy( label_ace, ace, ace->size );
  280. label_ace = ace_next( label_ace );
  281. }
  282. }
  283. return label_acl;
  284. }
  285. /* replace security labels in an existing SACL */
  286. struct acl *replace_security_labels( const struct acl *old_sacl, const struct acl *new_sacl )
  287. {
  288. const struct ace *ace;
  289. struct ace *replaced_ace;
  290. unsigned int i, count = 0;
  291. unsigned char revision = ACL_REVISION;
  292. struct acl *replaced_acl;
  293. data_size_t size = sizeof(*replaced_acl);
  294. if (old_sacl)
  295. {
  296. revision = max( revision, old_sacl->revision );
  297. for (i = 0, ace = ace_first( old_sacl ); i < old_sacl->count; i++, ace = ace_next( ace ))
  298. {
  299. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  300. size += ace->size;
  301. count++;
  302. }
  303. }
  304. if (new_sacl)
  305. {
  306. revision = max( revision, new_sacl->revision );
  307. for (i = 0, ace = ace_first( new_sacl ); i < new_sacl->count; i++, ace = ace_next( ace ))
  308. {
  309. if (ace->type != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  310. size += ace->size;
  311. count++;
  312. }
  313. }
  314. if (size > MAX_ACL_LEN)
  315. {
  316. set_error( STATUS_INVALID_ACL );
  317. return NULL;
  318. }
  319. replaced_acl = mem_alloc( size );
  320. if (!replaced_acl) return NULL;
  321. replaced_acl->revision = revision;
  322. replaced_acl->pad1 = 0;
  323. replaced_acl->size = size;
  324. replaced_acl->count = count;
  325. replaced_acl->pad2 = 0;
  326. replaced_ace = (struct ace *)(replaced_acl + 1);
  327. if (old_sacl)
  328. {
  329. for (i = 0, ace = ace_first( old_sacl ); i < old_sacl->count; i++, ace = ace_next( ace ))
  330. {
  331. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  332. memcpy( replaced_ace, ace, ace->size );
  333. replaced_ace = ace_next( replaced_ace );
  334. }
  335. }
  336. if (new_sacl)
  337. {
  338. for (i = 0, ace = ace_first( new_sacl ); i < new_sacl->count; i++, ace = ace_next( ace ))
  339. {
  340. if (ace->type != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  341. memcpy( replaced_ace, ace, ace->size );
  342. replaced_ace = ace_next( replaced_ace );
  343. }
  344. }
  345. return replaced_acl;
  346. }
  347. static inline int is_equal_luid( struct luid luid1, struct luid luid2 )
  348. {
  349. return (luid1.low_part == luid2.low_part && luid1.high_part == luid2.high_part);
  350. }
  351. static inline void allocate_luid( struct luid *luid )
  352. {
  353. prev_luid_value.low_part++;
  354. *luid = prev_luid_value;
  355. }
  356. DECL_HANDLER( allocate_locally_unique_id )
  357. {
  358. allocate_luid( &reply->luid );
  359. }
  360. static inline struct luid_attr luid_and_attr_from_privilege( const struct privilege *in )
  361. {
  362. struct luid_attr ret = { in->luid };
  363. ret.attrs = (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
  364. (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
  365. return ret;
  366. }
  367. static struct privilege *privilege_add( struct token *token, struct luid luid, int enabled )
  368. {
  369. struct privilege *privilege = mem_alloc( sizeof(*privilege) );
  370. if (privilege)
  371. {
  372. privilege->luid = luid;
  373. privilege->def = privilege->enabled = (enabled != 0);
  374. list_add_tail( &token->privileges, &privilege->entry );
  375. }
  376. return privilege;
  377. }
  378. static inline void privilege_remove( struct privilege *privilege )
  379. {
  380. list_remove( &privilege->entry );
  381. free( privilege );
  382. }
  383. static void token_destroy( struct object *obj )
  384. {
  385. struct token* token;
  386. struct list *cursor, *cursor_next;
  387. assert( obj->ops == &token_ops );
  388. token = (struct token *)obj;
  389. free( token->user );
  390. LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
  391. {
  392. struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
  393. privilege_remove( privilege );
  394. }
  395. LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
  396. {
  397. struct group *group = LIST_ENTRY( cursor, struct group, entry );
  398. list_remove( &group->entry );
  399. free( group );
  400. }
  401. free( token->default_dacl );
  402. }
  403. /* creates a new token.
  404. * groups may be NULL if group_count is 0.
  405. * privs may be NULL if priv_count is 0.
  406. * default_dacl may be NULL, indicating that all objects created by the user
  407. * are unsecured.
  408. * modified_id may be NULL, indicating that a new modified_id luid should be
  409. * allocated.
  410. */
  411. static struct token *create_token( unsigned int primary, unsigned int session_id, const struct sid *user,
  412. const struct sid_attrs *groups, unsigned int group_count,
  413. const struct luid_attr *privs, unsigned int priv_count,
  414. const struct acl *default_dacl, const struct luid *modified_id,
  415. int impersonation_level, int elevation )
  416. {
  417. struct token *token = alloc_object( &token_ops );
  418. if (token)
  419. {
  420. unsigned int i;
  421. allocate_luid( &token->token_id );
  422. if (modified_id)
  423. token->modified_id = *modified_id;
  424. else
  425. allocate_luid( &token->modified_id );
  426. list_init( &token->privileges );
  427. list_init( &token->groups );
  428. token->primary = primary;
  429. token->session_id = session_id;
  430. /* primary tokens don't have impersonation levels */
  431. if (primary)
  432. token->impersonation_level = -1;
  433. else
  434. token->impersonation_level = impersonation_level;
  435. token->default_dacl = NULL;
  436. token->primary_group = NULL;
  437. token->elevation = elevation;
  438. /* copy user */
  439. token->user = memdup( user, sid_len( user ));
  440. if (!token->user)
  441. {
  442. release_object( token );
  443. return NULL;
  444. }
  445. /* copy groups */
  446. for (i = 0; i < group_count; i++)
  447. {
  448. size_t size = offsetof( struct group, sid.sub_auth[groups[i].sid->sub_count] );
  449. struct group *group = mem_alloc( size );
  450. if (!group)
  451. {
  452. release_object( token );
  453. return NULL;
  454. }
  455. group->attrs = groups[i].attrs;
  456. copy_sid( &group->sid, groups[i].sid );
  457. list_add_tail( &token->groups, &group->entry );
  458. /* Use first owner capable group as owner and primary group */
  459. if (!token->primary_group && (group->attrs & SE_GROUP_OWNER))
  460. {
  461. token->owner = &group->sid;
  462. token->primary_group = &group->sid;
  463. }
  464. }
  465. /* copy privileges */
  466. for (i = 0; i < priv_count; i++)
  467. {
  468. /* note: we don't check uniqueness: the caller must make sure
  469. * privs doesn't contain any duplicate luids */
  470. if (!privilege_add( token, privs[i].luid, privs[i].attrs & SE_PRIVILEGE_ENABLED ))
  471. {
  472. release_object( token );
  473. return NULL;
  474. }
  475. }
  476. if (default_dacl)
  477. {
  478. token->default_dacl = memdup( default_dacl, default_dacl->size );
  479. if (!token->default_dacl)
  480. {
  481. release_object( token );
  482. return NULL;
  483. }
  484. }
  485. }
  486. return token;
  487. }
  488. static int filter_group( struct group *group, const struct sid *filter, unsigned int count )
  489. {
  490. unsigned int i;
  491. for (i = 0; i < count; i++)
  492. {
  493. if (equal_sid( &group->sid, filter )) return 1;
  494. filter = (const struct sid *)((char *)filter + sid_len( filter ));
  495. }
  496. return 0;
  497. }
  498. static int filter_privilege( struct privilege *privilege, const struct luid_attr *filter, unsigned int count )
  499. {
  500. unsigned int i;
  501. for (i = 0; i < count; i++)
  502. if (is_equal_luid( privilege->luid, filter[i].luid )) return 1;
  503. return 0;
  504. }
  505. struct token *token_duplicate( struct token *src_token, unsigned primary,
  506. int impersonation_level, const struct security_descriptor *sd,
  507. const struct luid_attr *remove_privs, unsigned int remove_priv_count,
  508. const struct sid *remove_groups, unsigned int remove_group_count)
  509. {
  510. const struct luid *modified_id =
  511. primary || (impersonation_level == src_token->impersonation_level) ?
  512. &src_token->modified_id : NULL;
  513. struct token *token = NULL;
  514. struct privilege *privilege;
  515. struct group *group;
  516. if (!primary &&
  517. (impersonation_level < SecurityAnonymous ||
  518. impersonation_level > SecurityDelegation ||
  519. (!src_token->primary && (impersonation_level > src_token->impersonation_level))))
  520. {
  521. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  522. return NULL;
  523. }
  524. token = create_token( primary, src_token->session_id, src_token->user, NULL, 0,
  525. NULL, 0, src_token->default_dacl, modified_id,
  526. impersonation_level, src_token->elevation );
  527. if (!token) return token;
  528. /* copy groups */
  529. token->primary_group = NULL;
  530. LIST_FOR_EACH_ENTRY( group, &src_token->groups, struct group, entry )
  531. {
  532. size_t size = offsetof( struct group, sid.sub_auth[group->sid.sub_count] );
  533. struct group *newgroup = mem_alloc( size );
  534. if (!newgroup)
  535. {
  536. release_object( token );
  537. return NULL;
  538. }
  539. memcpy( newgroup, group, size );
  540. if (filter_group( group, remove_groups, remove_group_count ))
  541. {
  542. newgroup->attrs &= ~(SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT);
  543. newgroup->attrs |= SE_GROUP_USE_FOR_DENY_ONLY;
  544. }
  545. list_add_tail( &token->groups, &newgroup->entry );
  546. if (src_token->primary_group == &group->sid)
  547. {
  548. token->owner = &newgroup->sid;
  549. token->primary_group = &newgroup->sid;
  550. }
  551. }
  552. assert( token->primary_group );
  553. /* copy privileges */
  554. LIST_FOR_EACH_ENTRY( privilege, &src_token->privileges, struct privilege, entry )
  555. {
  556. if (filter_privilege( privilege, remove_privs, remove_priv_count )) continue;
  557. if (!privilege_add( token, privilege->luid, privilege->enabled ))
  558. {
  559. release_object( token );
  560. return NULL;
  561. }
  562. }
  563. if (sd) default_set_sd( &token->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
  564. DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
  565. return token;
  566. }
  567. static struct acl *create_default_dacl( const struct sid *user )
  568. {
  569. struct ace *ace;
  570. struct acl *default_dacl;
  571. size_t default_dacl_size = sizeof(*default_dacl) + 2 * sizeof(*ace) +
  572. sid_len( &local_system_sid ) + sid_len( user );
  573. default_dacl = mem_alloc( default_dacl_size );
  574. if (!default_dacl) return NULL;
  575. default_dacl->revision = ACL_REVISION;
  576. default_dacl->pad1 = 0;
  577. default_dacl->size = default_dacl_size;
  578. default_dacl->count = 2;
  579. default_dacl->pad2 = 0;
  580. /* GENERIC_ALL for Local System */
  581. ace = set_ace( ace_first( default_dacl ), &local_system_sid, ACCESS_ALLOWED_ACE_TYPE, 0, GENERIC_ALL );
  582. /* GENERIC_ALL for specified user */
  583. set_ace( ace_next( ace ), user, ACCESS_ALLOWED_ACE_TYPE, 0, GENERIC_ALL );
  584. return default_dacl;
  585. }
  586. struct sid_data
  587. {
  588. SID_IDENTIFIER_AUTHORITY idauth;
  589. int count;
  590. unsigned int subauth[MAX_SUBAUTH_COUNT];
  591. };
  592. static struct security_descriptor *create_security_label_sd( struct token *token, const struct sid *label_sid )
  593. {
  594. size_t sid_size = sid_len( label_sid ), sacl_size, sd_size;
  595. struct security_descriptor *sd;
  596. struct acl *sacl;
  597. sacl_size = sizeof(*sacl) + sizeof(struct ace) + sid_size;
  598. sd_size = sizeof(struct security_descriptor) + sacl_size;
  599. if (!(sd = mem_alloc( sd_size )))
  600. return NULL;
  601. sd->control = SE_SACL_PRESENT;
  602. sd->owner_len = 0;
  603. sd->group_len = 0;
  604. sd->sacl_len = sacl_size;
  605. sd->dacl_len = 0;
  606. sacl = (struct acl *)(sd + 1);
  607. sacl->revision = ACL_REVISION;
  608. sacl->pad1 = 0;
  609. sacl->size = sacl_size;
  610. sacl->count = 1;
  611. sacl->pad2 = 0;
  612. set_ace( ace_first( sacl ), label_sid, SYSTEM_MANDATORY_LABEL_ACE_TYPE, 0,
  613. SYSTEM_MANDATORY_LABEL_NO_WRITE_UP );
  614. assert( sd_is_valid( sd, sd_size ) );
  615. return sd;
  616. }
  617. int token_assign_label( struct token *token, const struct sid *label )
  618. {
  619. struct security_descriptor *sd;
  620. int ret = 0;
  621. if ((sd = create_security_label_sd( token, label )))
  622. {
  623. ret = set_sd_defaults_from_token( &token->obj, sd, LABEL_SECURITY_INFORMATION, token );
  624. free( sd );
  625. }
  626. return ret;
  627. }
  628. struct token *get_token_obj( struct process *process, obj_handle_t handle, unsigned int access )
  629. {
  630. return (struct token *)get_handle_obj( process, handle, access, &token_ops );
  631. }
  632. struct token *token_create_admin( unsigned primary, int impersonation_level, int elevation, unsigned int session_id )
  633. {
  634. struct token *token = NULL;
  635. struct sid alias_admins_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS }};
  636. struct sid alias_users_sid = { SID_REVISION, 2, SECURITY_NT_AUTHORITY, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS }};
  637. /* on Windows, this value changes every time the user logs on */
  638. struct sid logon_sid = { SID_REVISION, 3, SECURITY_NT_AUTHORITY, { SECURITY_LOGON_IDS_RID, 0, 0 /* FIXME: should be randomly generated when tokens are inherited by new processes */ }};
  639. const struct sid *user_sid = security_unix_uid_to_sid( getuid() );
  640. struct acl *default_dacl = create_default_dacl( user_sid );
  641. const struct luid_attr admin_privs[] =
  642. {
  643. { SeChangeNotifyPrivilege, SE_PRIVILEGE_ENABLED },
  644. { SeTcbPrivilege, 0 },
  645. { SeSecurityPrivilege, 0 },
  646. { SeBackupPrivilege, 0 },
  647. { SeRestorePrivilege, 0 },
  648. { SeSystemtimePrivilege, 0 },
  649. { SeShutdownPrivilege, 0 },
  650. { SeRemoteShutdownPrivilege, 0 },
  651. { SeTakeOwnershipPrivilege, 0 },
  652. { SeDebugPrivilege, 0 },
  653. { SeSystemEnvironmentPrivilege, 0 },
  654. { SeSystemProfilePrivilege, 0 },
  655. { SeProfileSingleProcessPrivilege, 0 },
  656. { SeIncreaseBasePriorityPrivilege, 0 },
  657. { SeLoadDriverPrivilege, SE_PRIVILEGE_ENABLED },
  658. { SeCreatePagefilePrivilege, 0 },
  659. { SeIncreaseQuotaPrivilege, 0 },
  660. { SeUndockPrivilege, 0 },
  661. { SeManageVolumePrivilege, 0 },
  662. { SeImpersonatePrivilege, SE_PRIVILEGE_ENABLED },
  663. { SeCreateGlobalPrivilege, SE_PRIVILEGE_ENABLED },
  664. };
  665. /* note: we don't include non-builtin groups here for the user -
  666. * telling us these is the job of a client-side program */
  667. const struct sid_attrs admin_groups[] =
  668. {
  669. { &world_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  670. { &local_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  671. { &interactive_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  672. { &authenticated_user_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  673. { &domain_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
  674. { &alias_admins_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_OWNER },
  675. { &alias_users_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY },
  676. { &logon_sid, SE_GROUP_ENABLED|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_MANDATORY|SE_GROUP_LOGON_ID },
  677. };
  678. token = create_token( primary, session_id, user_sid, admin_groups, ARRAY_SIZE( admin_groups ),
  679. admin_privs, ARRAY_SIZE( admin_privs ), default_dacl,
  680. NULL, impersonation_level, elevation );
  681. /* we really need a primary group */
  682. assert( token->primary_group );
  683. free( default_dacl );
  684. return token;
  685. }
  686. static struct privilege *token_find_privilege( struct token *token, struct luid luid, int enabled_only )
  687. {
  688. struct privilege *privilege;
  689. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  690. {
  691. if (is_equal_luid( luid, privilege->luid ))
  692. {
  693. if (enabled_only && !privilege->enabled)
  694. return NULL;
  695. return privilege;
  696. }
  697. }
  698. return NULL;
  699. }
  700. static unsigned int token_adjust_privileges( struct token *token, const struct luid_attr *privs,
  701. unsigned int count, struct luid_attr *mod_privs,
  702. unsigned int mod_privs_count )
  703. {
  704. unsigned int i, modified_count = 0;
  705. /* mark as modified */
  706. allocate_luid( &token->modified_id );
  707. for (i = 0; i < count; i++)
  708. {
  709. struct privilege *privilege = token_find_privilege( token, privs[i].luid, FALSE );
  710. if (!privilege)
  711. {
  712. set_error( STATUS_NOT_ALL_ASSIGNED );
  713. continue;
  714. }
  715. if (privs[i].attrs & SE_PRIVILEGE_REMOVED) privilege_remove( privilege );
  716. else
  717. {
  718. /* save previous state for caller */
  719. if (mod_privs_count)
  720. {
  721. *mod_privs++ = luid_and_attr_from_privilege( privilege );
  722. mod_privs_count--;
  723. modified_count++;
  724. }
  725. privilege->enabled = !!(privs[i].attrs & SE_PRIVILEGE_ENABLED);
  726. }
  727. }
  728. return modified_count;
  729. }
  730. static void token_disable_privileges( struct token *token )
  731. {
  732. struct privilege *privilege;
  733. /* mark as modified */
  734. allocate_luid( &token->modified_id );
  735. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  736. privilege->enabled = FALSE;
  737. }
  738. int token_check_privileges( struct token *token, int all_required, const struct luid_attr *reqprivs,
  739. unsigned int count, struct luid_attr *usedprivs)
  740. {
  741. unsigned int i, enabled_count = 0;
  742. for (i = 0; i < count; i++)
  743. {
  744. struct privilege *privilege = token_find_privilege( token, reqprivs[i].luid, TRUE );
  745. if (usedprivs)
  746. usedprivs[i] = reqprivs[i];
  747. if (privilege && privilege->enabled)
  748. {
  749. enabled_count++;
  750. if (usedprivs) usedprivs[i].attrs |= SE_PRIVILEGE_USED_FOR_ACCESS;
  751. }
  752. }
  753. if (all_required)
  754. return (enabled_count == count);
  755. else
  756. return (enabled_count > 0);
  757. }
  758. int token_sid_present( struct token *token, const struct sid *sid, int deny )
  759. {
  760. struct group *group;
  761. if (equal_sid( token->user, sid )) return TRUE;
  762. LIST_FOR_EACH_ENTRY( group, &token->groups, struct group, entry )
  763. {
  764. if (!(group->attrs & SE_GROUP_ENABLED)) continue;
  765. if (!deny && (group->attrs & SE_GROUP_USE_FOR_DENY_ONLY)) continue;
  766. if (equal_sid( &group->sid, sid )) return TRUE;
  767. }
  768. return FALSE;
  769. }
  770. /* Checks access to a security descriptor. 'sd' must have been validated by
  771. * caller. It returns STATUS_SUCCESS if call succeeded or an error indicating
  772. * the reason. 'status' parameter will indicate if access is granted or denied.
  773. *
  774. * If both returned value and 'status' are STATUS_SUCCESS then access is granted.
  775. */
  776. static unsigned int token_access_check( struct token *token,
  777. const struct security_descriptor *sd,
  778. unsigned int desired_access,
  779. struct luid_attr *privs,
  780. unsigned int *priv_count,
  781. const generic_map_t *mapping,
  782. unsigned int *granted_access,
  783. unsigned int *status )
  784. {
  785. unsigned int current_access = 0;
  786. unsigned int denied_access = 0;
  787. ULONG i;
  788. const struct acl *dacl;
  789. int dacl_present;
  790. const struct ace *ace;
  791. const struct sid *owner;
  792. /* assume no access rights */
  793. *granted_access = 0;
  794. /* fail if desired_access contains generic rights */
  795. if (desired_access & (GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE|GENERIC_ALL))
  796. {
  797. if (priv_count) *priv_count = 0;
  798. return STATUS_GENERIC_NOT_MAPPED;
  799. }
  800. dacl = sd_get_dacl( sd, &dacl_present );
  801. owner = sd_get_owner( sd );
  802. if (!owner || !sd_get_group( sd ))
  803. {
  804. if (priv_count) *priv_count = 0;
  805. return STATUS_INVALID_SECURITY_DESCR;
  806. }
  807. /* 1: Grant desired access if the object is unprotected */
  808. if (!dacl_present || !dacl)
  809. {
  810. if (priv_count) *priv_count = 0;
  811. if (desired_access & MAXIMUM_ALLOWED)
  812. *granted_access = mapping->all;
  813. else
  814. *granted_access = desired_access;
  815. return *status = STATUS_SUCCESS;
  816. }
  817. /* 2: Check if caller wants access to system security part. Note: access
  818. * is only granted if specifically asked for */
  819. if (desired_access & ACCESS_SYSTEM_SECURITY)
  820. {
  821. const struct luid_attr security_priv = { SeSecurityPrivilege, 0 };
  822. struct luid_attr retpriv = security_priv;
  823. if (token_check_privileges( token, TRUE, &security_priv, 1, &retpriv ))
  824. {
  825. if (priv_count)
  826. {
  827. /* assumes that there will only be one privilege to return */
  828. if (*priv_count >= 1)
  829. {
  830. *priv_count = 1;
  831. *privs = retpriv;
  832. }
  833. else
  834. {
  835. *priv_count = 1;
  836. return STATUS_BUFFER_TOO_SMALL;
  837. }
  838. }
  839. current_access |= ACCESS_SYSTEM_SECURITY;
  840. if (desired_access == current_access)
  841. {
  842. *granted_access = current_access;
  843. return *status = STATUS_SUCCESS;
  844. }
  845. }
  846. else
  847. {
  848. if (priv_count) *priv_count = 0;
  849. *status = STATUS_PRIVILEGE_NOT_HELD;
  850. return STATUS_SUCCESS;
  851. }
  852. }
  853. else if (priv_count) *priv_count = 0;
  854. /* 3: Check whether the token is the owner */
  855. /* NOTE: SeTakeOwnershipPrivilege is not checked for here - it is instead
  856. * checked when a "set owner" call is made, overriding the access rights
  857. * determined here. */
  858. if (token_sid_present( token, owner, FALSE ))
  859. {
  860. current_access |= (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE);
  861. if (desired_access == current_access)
  862. {
  863. *granted_access = current_access;
  864. return *status = STATUS_SUCCESS;
  865. }
  866. }
  867. /* 4: Grant rights according to the DACL */
  868. for (i = 0, ace = ace_first( dacl ); i < dacl->count; i++, ace = ace_next( ace ))
  869. {
  870. const struct sid *sid = (const struct sid *)(ace + 1);
  871. if (ace->flags & INHERIT_ONLY_ACE) continue;
  872. switch (ace->type)
  873. {
  874. case ACCESS_DENIED_ACE_TYPE:
  875. if (token_sid_present( token, sid, TRUE ))
  876. {
  877. unsigned int access = map_access( ace->mask, mapping );
  878. if (desired_access & MAXIMUM_ALLOWED)
  879. denied_access |= access;
  880. else
  881. {
  882. denied_access |= (access & ~current_access);
  883. if (desired_access & access) goto done;
  884. }
  885. }
  886. break;
  887. case ACCESS_ALLOWED_ACE_TYPE:
  888. if (token_sid_present( token, sid, FALSE ))
  889. {
  890. unsigned int access = map_access( ace->mask, mapping );
  891. if (desired_access & MAXIMUM_ALLOWED)
  892. current_access |= access;
  893. else
  894. current_access |= (access & ~denied_access);
  895. }
  896. break;
  897. }
  898. /* don't bother carrying on checking if we've already got all of
  899. * rights we need */
  900. if (desired_access == *granted_access)
  901. break;
  902. }
  903. done:
  904. if (desired_access & MAXIMUM_ALLOWED)
  905. *granted_access = current_access & ~denied_access;
  906. else
  907. if ((current_access & desired_access) == desired_access)
  908. *granted_access = current_access & desired_access;
  909. else
  910. *granted_access = 0;
  911. *status = *granted_access ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
  912. return STATUS_SUCCESS;
  913. }
  914. const struct acl *token_get_default_dacl( struct token *token )
  915. {
  916. return token->default_dacl;
  917. }
  918. const struct sid *token_get_user( struct token *token )
  919. {
  920. return token->user;
  921. }
  922. const struct sid *token_get_primary_group( struct token *token )
  923. {
  924. return token->primary_group;
  925. }
  926. unsigned int token_get_session_id( struct token *token )
  927. {
  928. return token->session_id;
  929. }
  930. int check_object_access(struct token *token, struct object *obj, unsigned int *access)
  931. {
  932. generic_map_t mapping;
  933. unsigned int status;
  934. int res;
  935. if (!token)
  936. token = current->token ? current->token : current->process->token;
  937. mapping.all = obj->ops->map_access( obj, GENERIC_ALL );
  938. if (!obj->sd)
  939. {
  940. if (*access & MAXIMUM_ALLOWED) *access = mapping.all;
  941. return TRUE;
  942. }
  943. mapping.read = obj->ops->map_access( obj, GENERIC_READ );
  944. mapping.write = obj->ops->map_access( obj, GENERIC_WRITE );
  945. mapping.exec = obj->ops->map_access( obj, GENERIC_EXECUTE );
  946. res = token_access_check( token, obj->sd, *access, NULL, NULL,
  947. &mapping, access, &status ) == STATUS_SUCCESS &&
  948. status == STATUS_SUCCESS;
  949. if (!res) set_error( STATUS_ACCESS_DENIED );
  950. return res;
  951. }
  952. /* open a security token */
  953. DECL_HANDLER(open_token)
  954. {
  955. if (req->flags & OPEN_TOKEN_THREAD)
  956. {
  957. struct thread *thread = get_thread_from_handle( req->handle, 0 );
  958. if (thread)
  959. {
  960. if (thread->token)
  961. {
  962. if (!thread->token->primary && thread->token->impersonation_level <= SecurityAnonymous)
  963. set_error( STATUS_CANT_OPEN_ANONYMOUS );
  964. else
  965. reply->token = alloc_handle( current->process, thread->token,
  966. req->access, req->attributes );
  967. }
  968. else
  969. set_error( STATUS_NO_TOKEN );
  970. release_object( thread );
  971. }
  972. }
  973. else
  974. {
  975. struct process *process = get_process_from_handle( req->handle, 0 );
  976. if (process)
  977. {
  978. if (process->token)
  979. reply->token = alloc_handle( current->process, process->token, req->access,
  980. req->attributes );
  981. else
  982. set_error( STATUS_NO_TOKEN );
  983. release_object( process );
  984. }
  985. }
  986. }
  987. /* adjust the privileges held by a token */
  988. DECL_HANDLER(adjust_token_privileges)
  989. {
  990. struct token *token;
  991. unsigned int access = TOKEN_ADJUST_PRIVILEGES;
  992. if (req->get_modified_state) access |= TOKEN_QUERY;
  993. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  994. access, &token_ops )))
  995. {
  996. const struct luid_attr *privs = get_req_data();
  997. struct luid_attr *modified_privs = NULL;
  998. unsigned int priv_count = get_req_data_size() / sizeof(*privs);
  999. unsigned int modified_priv_count = 0;
  1000. if (req->get_modified_state && !req->disable_all)
  1001. {
  1002. unsigned int i;
  1003. /* count modified privs */
  1004. for (i = 0; i < priv_count; i++)
  1005. {
  1006. struct privilege *privilege = token_find_privilege( token, privs[i].luid, FALSE );
  1007. if (privilege && req->get_modified_state)
  1008. modified_priv_count++;
  1009. }
  1010. reply->len = modified_priv_count;
  1011. modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
  1012. if (modified_priv_count)
  1013. modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
  1014. }
  1015. reply->len = modified_priv_count * sizeof(*modified_privs);
  1016. if (req->disable_all)
  1017. token_disable_privileges( token );
  1018. else
  1019. token_adjust_privileges( token, privs, priv_count, modified_privs, modified_priv_count );
  1020. release_object( token );
  1021. }
  1022. }
  1023. /* retrieves the list of privileges that may be held be the token */
  1024. DECL_HANDLER(get_token_privileges)
  1025. {
  1026. struct token *token;
  1027. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1028. TOKEN_QUERY,
  1029. &token_ops )))
  1030. {
  1031. int priv_count = 0;
  1032. struct luid_attr *privs;
  1033. struct privilege *privilege;
  1034. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  1035. priv_count++;
  1036. reply->len = priv_count * sizeof(*privs);
  1037. if (reply->len <= get_reply_max_size())
  1038. {
  1039. privs = set_reply_data_size( priv_count * sizeof(*privs) );
  1040. if (privs)
  1041. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  1042. *privs++ = luid_and_attr_from_privilege( privilege );
  1043. }
  1044. else
  1045. set_error(STATUS_BUFFER_TOO_SMALL);
  1046. release_object( token );
  1047. }
  1048. }
  1049. /* creates a duplicate of the token */
  1050. DECL_HANDLER(duplicate_token)
  1051. {
  1052. struct token *src_token;
  1053. struct unicode_str name;
  1054. const struct security_descriptor *sd;
  1055. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
  1056. if (!objattr) return;
  1057. if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
  1058. TOKEN_DUPLICATE,
  1059. &token_ops )))
  1060. {
  1061. struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level, sd, NULL, 0, NULL, 0 );
  1062. if (token)
  1063. {
  1064. unsigned int access = req->access ? req->access : get_handle_access( current->process, req->handle );
  1065. reply->new_handle = alloc_handle_no_access_check( current->process, token, access, objattr->attributes );
  1066. release_object( token );
  1067. }
  1068. release_object( src_token );
  1069. }
  1070. }
  1071. /* creates a restricted version of a token */
  1072. DECL_HANDLER(filter_token)
  1073. {
  1074. struct token *src_token;
  1075. if ((src_token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_DUPLICATE, &token_ops )))
  1076. {
  1077. const struct luid_attr *filter_privileges = get_req_data();
  1078. unsigned int priv_count, group_count;
  1079. const struct sid *filter_groups;
  1080. struct token *token;
  1081. priv_count = min( req->privileges_size, get_req_data_size() ) / sizeof(struct luid_attr);
  1082. filter_groups = (const struct sid *)((char *)filter_privileges + priv_count * sizeof(struct luid_attr));
  1083. group_count = get_sid_count( filter_groups, get_req_data_size() - priv_count * sizeof(struct luid_attr) );
  1084. token = token_duplicate( src_token, src_token->primary, src_token->impersonation_level, NULL,
  1085. filter_privileges, priv_count, filter_groups, group_count );
  1086. if (token)
  1087. {
  1088. unsigned int access = get_handle_access( current->process, req->handle );
  1089. reply->new_handle = alloc_handle_no_access_check( current->process, token, access, 0 );
  1090. release_object( token );
  1091. }
  1092. release_object( src_token );
  1093. }
  1094. }
  1095. /* checks the specified privileges are held by the token */
  1096. DECL_HANDLER(check_token_privileges)
  1097. {
  1098. struct token *token;
  1099. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1100. TOKEN_QUERY,
  1101. &token_ops )))
  1102. {
  1103. unsigned int count = get_req_data_size() / sizeof(struct luid_attr);
  1104. if (!token->primary && token->impersonation_level <= SecurityAnonymous)
  1105. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  1106. else if (get_reply_max_size() >= count * sizeof(struct luid_attr))
  1107. {
  1108. struct luid_attr *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
  1109. reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
  1110. }
  1111. else
  1112. set_error( STATUS_BUFFER_OVERFLOW );
  1113. release_object( token );
  1114. }
  1115. }
  1116. /* checks that a user represented by a token is allowed to access an object
  1117. * represented by a security descriptor */
  1118. DECL_HANDLER(access_check)
  1119. {
  1120. data_size_t sd_size = get_req_data_size();
  1121. const struct security_descriptor *sd = get_req_data();
  1122. struct token *token;
  1123. if (!sd_is_valid( sd, sd_size ))
  1124. {
  1125. set_error( STATUS_ACCESS_VIOLATION );
  1126. return;
  1127. }
  1128. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1129. TOKEN_QUERY,
  1130. &token_ops )))
  1131. {
  1132. unsigned int status;
  1133. struct luid_attr priv;
  1134. unsigned int priv_count = 1;
  1135. memset(&priv, 0, sizeof(priv));
  1136. /* only impersonation tokens may be used with this function */
  1137. if (token->primary)
  1138. {
  1139. set_error( STATUS_NO_IMPERSONATION_TOKEN );
  1140. release_object( token );
  1141. return;
  1142. }
  1143. /* anonymous impersonation tokens can't be used */
  1144. if (token->impersonation_level <= SecurityAnonymous)
  1145. {
  1146. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  1147. release_object( token );
  1148. return;
  1149. }
  1150. status = token_access_check( token, sd, req->desired_access, &priv, &priv_count, &req->mapping,
  1151. &reply->access_granted, &reply->access_status );
  1152. reply->privileges_len = priv_count*sizeof(struct luid_attr);
  1153. if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
  1154. {
  1155. struct luid_attr *privs = set_reply_data_size( priv_count * sizeof(*privs) );
  1156. memcpy( privs, &priv, sizeof(priv) );
  1157. }
  1158. set_error( status );
  1159. release_object( token );
  1160. }
  1161. }
  1162. /* retrieves an SID from the token */
  1163. DECL_HANDLER(get_token_sid)
  1164. {
  1165. struct token *token;
  1166. reply->sid_len = 0;
  1167. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1168. {
  1169. const struct sid *sid = NULL;
  1170. switch (req->which_sid)
  1171. {
  1172. case TokenUser:
  1173. assert(token->user);
  1174. sid = token->user;
  1175. break;
  1176. case TokenPrimaryGroup:
  1177. sid = token->primary_group;
  1178. break;
  1179. case TokenOwner:
  1180. sid = token->owner;
  1181. break;
  1182. default:
  1183. set_error( STATUS_INVALID_PARAMETER );
  1184. break;
  1185. }
  1186. if (sid)
  1187. {
  1188. reply->sid_len = sid_len( sid );
  1189. if (reply->sid_len <= get_reply_max_size()) set_reply_data( sid, reply->sid_len );
  1190. else set_error( STATUS_BUFFER_TOO_SMALL );
  1191. }
  1192. release_object( token );
  1193. }
  1194. }
  1195. /* retrieves the groups that the user represented by the token belongs to */
  1196. DECL_HANDLER(get_token_groups)
  1197. {
  1198. struct token *token;
  1199. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1200. {
  1201. unsigned int group_count = 0;
  1202. const struct group *group;
  1203. LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
  1204. {
  1205. if (req->attr_mask && !(group->attrs & req->attr_mask)) continue;
  1206. group_count++;
  1207. reply->sid_len += sid_len( &group->sid );
  1208. }
  1209. reply->attr_len = sizeof(unsigned int) * group_count;
  1210. if (reply->attr_len + reply->sid_len <= get_reply_max_size())
  1211. {
  1212. unsigned int *attr_ptr = set_reply_data_size( reply->attr_len + reply->sid_len );
  1213. struct sid *sid = (struct sid *)(attr_ptr + group_count);
  1214. if (attr_ptr)
  1215. {
  1216. LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
  1217. {
  1218. if (req->attr_mask && !(group->attrs & req->attr_mask)) continue;
  1219. sid = copy_sid( sid, &group->sid );
  1220. *attr_ptr++ = group->attrs;
  1221. }
  1222. }
  1223. }
  1224. else set_error( STATUS_BUFFER_TOO_SMALL );
  1225. release_object( token );
  1226. }
  1227. }
  1228. DECL_HANDLER(get_token_info)
  1229. {
  1230. struct token *token;
  1231. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1232. {
  1233. reply->token_id = token->token_id;
  1234. reply->modified_id = token->modified_id;
  1235. reply->session_id = token->session_id;
  1236. reply->primary = token->primary;
  1237. reply->impersonation_level = token->impersonation_level;
  1238. reply->elevation = token->elevation;
  1239. reply->group_count = list_count( &token->groups );
  1240. reply->privilege_count = list_count( &token->privileges );
  1241. release_object( token );
  1242. }
  1243. }
  1244. DECL_HANDLER(get_token_default_dacl)
  1245. {
  1246. struct token *token;
  1247. reply->acl_len = 0;
  1248. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1249. TOKEN_QUERY,
  1250. &token_ops )))
  1251. {
  1252. if (token->default_dacl)
  1253. reply->acl_len = token->default_dacl->size;
  1254. if (reply->acl_len <= get_reply_max_size())
  1255. {
  1256. struct acl *acl_reply = set_reply_data_size( reply->acl_len );
  1257. if (acl_reply)
  1258. memcpy( acl_reply, token->default_dacl, reply->acl_len );
  1259. }
  1260. else set_error( STATUS_BUFFER_TOO_SMALL );
  1261. release_object( token );
  1262. }
  1263. }
  1264. DECL_HANDLER(set_token_default_dacl)
  1265. {
  1266. struct token *token;
  1267. const struct acl *acl = get_req_data();
  1268. unsigned int acl_size = get_req_data_size();
  1269. if (acl_size && !acl_is_valid( acl, acl_size ))
  1270. {
  1271. set_error( STATUS_INVALID_ACL );
  1272. return;
  1273. }
  1274. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1275. TOKEN_ADJUST_DEFAULT,
  1276. &token_ops )))
  1277. {
  1278. free( token->default_dacl );
  1279. token->default_dacl = NULL;
  1280. if (acl_size)
  1281. token->default_dacl = memdup( acl, acl_size );
  1282. release_object( token );
  1283. }
  1284. }
  1285. DECL_HANDLER(create_linked_token)
  1286. {
  1287. struct token *token, *linked;
  1288. int elevation;
  1289. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1290. TOKEN_QUERY, &token_ops )))
  1291. {
  1292. switch (token->elevation)
  1293. {
  1294. case TokenElevationTypeFull:
  1295. elevation = TokenElevationTypeLimited;
  1296. break;
  1297. case TokenElevationTypeLimited:
  1298. elevation = TokenElevationTypeFull;
  1299. break;
  1300. default:
  1301. release_object( token );
  1302. return;
  1303. }
  1304. if ((linked = token_create_admin( FALSE, SecurityIdentification, elevation, token->session_id )))
  1305. {
  1306. reply->linked = alloc_handle( current->process, linked, TOKEN_ALL_ACCESS, 0 );
  1307. release_object( linked );
  1308. }
  1309. release_object( token );
  1310. }
  1311. }