token.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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_get_fast_sync, /* get_fast_sync */
  142. no_close_handle, /* close_handle */
  143. token_destroy /* destroy */
  144. };
  145. static void token_dump( struct object *obj, int verbose )
  146. {
  147. struct token *token = (struct token *)obj;
  148. assert( obj->ops == &token_ops );
  149. fprintf( stderr, "Token id=%d.%u primary=%u impersonation level=%d\n", token->token_id.high_part,
  150. token->token_id.low_part, token->primary, token->impersonation_level );
  151. }
  152. void security_set_thread_token( struct thread *thread, obj_handle_t handle )
  153. {
  154. if (!handle)
  155. {
  156. if (thread->token)
  157. release_object( thread->token );
  158. thread->token = NULL;
  159. }
  160. else
  161. {
  162. struct token *token = (struct token *)get_handle_obj( current->process,
  163. handle,
  164. TOKEN_IMPERSONATE,
  165. &token_ops );
  166. if (token)
  167. {
  168. if (thread->token)
  169. release_object( thread->token );
  170. thread->token = token;
  171. }
  172. }
  173. }
  174. const struct sid *security_unix_uid_to_sid( uid_t uid )
  175. {
  176. /* very simple mapping: either the current user or not the current user */
  177. if (uid == getuid())
  178. return &local_user_sid;
  179. else
  180. return &anonymous_logon_sid;
  181. }
  182. static int acl_is_valid( const struct acl *acl, data_size_t size )
  183. {
  184. ULONG i;
  185. const struct ace *ace;
  186. if (size < sizeof(*acl)) return FALSE;
  187. size = min(size, MAX_ACL_LEN);
  188. size -= sizeof(*acl);
  189. for (i = 0, ace = ace_first( acl ); i < acl->count; i++, ace = ace_next( ace ))
  190. {
  191. if (size < sizeof(*ace) || size < ace->size) return FALSE;
  192. size -= ace->size;
  193. switch (ace->type)
  194. {
  195. case ACCESS_DENIED_ACE_TYPE:
  196. case ACCESS_ALLOWED_ACE_TYPE:
  197. case SYSTEM_AUDIT_ACE_TYPE:
  198. case SYSTEM_ALARM_ACE_TYPE:
  199. case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
  200. break;
  201. default:
  202. return FALSE;
  203. }
  204. if (!sid_valid_size( (const struct sid *)(ace + 1), ace->size - sizeof(*ace) )) return FALSE;
  205. }
  206. return TRUE;
  207. }
  208. static unsigned int get_sid_count( const struct sid *sid, data_size_t size )
  209. {
  210. unsigned int count;
  211. for (count = 0; sid_valid_size( sid, size ); count++)
  212. {
  213. size -= sid_len( sid );
  214. sid = (const struct sid *)((char *)sid + sid_len( sid ));
  215. }
  216. return count;
  217. }
  218. /* checks whether all members of a security descriptor fit inside the size
  219. * of memory specified */
  220. int sd_is_valid( const struct security_descriptor *sd, data_size_t size )
  221. {
  222. size_t offset = sizeof(struct security_descriptor);
  223. const struct sid *group;
  224. const struct sid *owner;
  225. const struct acl *sacl;
  226. const struct acl *dacl;
  227. int dummy;
  228. if (size < offset)
  229. return FALSE;
  230. if (sd->owner_len >= offsetof(struct sid, sub_auth[255]) || offset + sd->owner_len > size) return FALSE;
  231. owner = sd_get_owner( sd );
  232. offset += sd->owner_len;
  233. if (owner && !sid_valid_size( owner, sd->owner_len )) return FALSE;
  234. if (sd->group_len >= offsetof(struct sid, sub_auth[255]) || offset + sd->group_len > size) return FALSE;
  235. group = sd_get_group( sd );
  236. offset += sd->group_len;
  237. if (group && !sid_valid_size( group, sd->group_len )) return FALSE;
  238. if ((sd->sacl_len >= MAX_ACL_LEN) || (offset + sd->sacl_len > size))
  239. return FALSE;
  240. sacl = sd_get_sacl( sd, &dummy );
  241. if (sacl && !acl_is_valid( sacl, sd->sacl_len ))
  242. return FALSE;
  243. offset += sd->sacl_len;
  244. if ((sd->dacl_len >= MAX_ACL_LEN) || (offset + sd->dacl_len > size))
  245. return FALSE;
  246. dacl = sd_get_dacl( sd, &dummy );
  247. if (dacl && !acl_is_valid( dacl, sd->dacl_len ))
  248. return FALSE;
  249. offset += sd->dacl_len;
  250. return TRUE;
  251. }
  252. /* extract security labels from SACL */
  253. struct acl *extract_security_labels( const struct acl *sacl )
  254. {
  255. size_t size = sizeof(*sacl);
  256. const struct ace *ace;
  257. struct ace *label_ace;
  258. unsigned int i, count = 0;
  259. struct acl *label_acl;
  260. for (i = 0, ace = ace_first( sacl ); i < sacl->count; i++, ace = ace_next( ace ))
  261. {
  262. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
  263. {
  264. size += ace->size;
  265. count++;
  266. }
  267. }
  268. label_acl = mem_alloc( size );
  269. if (!label_acl) return NULL;
  270. label_acl->revision = sacl->revision;
  271. label_acl->pad1 = 0;
  272. label_acl->size = size;
  273. label_acl->count = count;
  274. label_acl->pad2 = 0;
  275. label_ace = ace_first( label_acl );
  276. for (i = 0, ace = ace_first( sacl ); i < sacl->count; i++, ace = ace_next( ace ))
  277. {
  278. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
  279. {
  280. memcpy( label_ace, ace, ace->size );
  281. label_ace = ace_next( label_ace );
  282. }
  283. }
  284. return label_acl;
  285. }
  286. /* replace security labels in an existing SACL */
  287. struct acl *replace_security_labels( const struct acl *old_sacl, const struct acl *new_sacl )
  288. {
  289. const struct ace *ace;
  290. struct ace *replaced_ace;
  291. unsigned int i, count = 0;
  292. unsigned char revision = ACL_REVISION;
  293. struct acl *replaced_acl;
  294. data_size_t size = sizeof(*replaced_acl);
  295. if (old_sacl)
  296. {
  297. revision = max( revision, old_sacl->revision );
  298. for (i = 0, ace = ace_first( old_sacl ); i < old_sacl->count; i++, ace = ace_next( ace ))
  299. {
  300. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  301. size += ace->size;
  302. count++;
  303. }
  304. }
  305. if (new_sacl)
  306. {
  307. revision = max( revision, new_sacl->revision );
  308. for (i = 0, ace = ace_first( new_sacl ); i < new_sacl->count; i++, ace = ace_next( ace ))
  309. {
  310. if (ace->type != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  311. size += ace->size;
  312. count++;
  313. }
  314. }
  315. if (size > MAX_ACL_LEN)
  316. {
  317. set_error( STATUS_INVALID_ACL );
  318. return NULL;
  319. }
  320. replaced_acl = mem_alloc( size );
  321. if (!replaced_acl) return NULL;
  322. replaced_acl->revision = revision;
  323. replaced_acl->pad1 = 0;
  324. replaced_acl->size = size;
  325. replaced_acl->count = count;
  326. replaced_acl->pad2 = 0;
  327. replaced_ace = (struct ace *)(replaced_acl + 1);
  328. if (old_sacl)
  329. {
  330. for (i = 0, ace = ace_first( old_sacl ); i < old_sacl->count; i++, ace = ace_next( ace ))
  331. {
  332. if (ace->type == SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  333. memcpy( replaced_ace, ace, ace->size );
  334. replaced_ace = ace_next( replaced_ace );
  335. }
  336. }
  337. if (new_sacl)
  338. {
  339. for (i = 0, ace = ace_first( new_sacl ); i < new_sacl->count; i++, ace = ace_next( ace ))
  340. {
  341. if (ace->type != SYSTEM_MANDATORY_LABEL_ACE_TYPE) continue;
  342. memcpy( replaced_ace, ace, ace->size );
  343. replaced_ace = ace_next( replaced_ace );
  344. }
  345. }
  346. return replaced_acl;
  347. }
  348. static inline int is_equal_luid( struct luid luid1, struct luid luid2 )
  349. {
  350. return (luid1.low_part == luid2.low_part && luid1.high_part == luid2.high_part);
  351. }
  352. static inline void allocate_luid( struct luid *luid )
  353. {
  354. prev_luid_value.low_part++;
  355. *luid = prev_luid_value;
  356. }
  357. DECL_HANDLER( allocate_locally_unique_id )
  358. {
  359. allocate_luid( &reply->luid );
  360. }
  361. static inline struct luid_attr luid_and_attr_from_privilege( const struct privilege *in )
  362. {
  363. struct luid_attr ret = { in->luid };
  364. ret.attrs = (in->enabled ? SE_PRIVILEGE_ENABLED : 0) |
  365. (in->def ? SE_PRIVILEGE_ENABLED_BY_DEFAULT : 0);
  366. return ret;
  367. }
  368. static struct privilege *privilege_add( struct token *token, struct luid luid, int enabled )
  369. {
  370. struct privilege *privilege = mem_alloc( sizeof(*privilege) );
  371. if (privilege)
  372. {
  373. privilege->luid = luid;
  374. privilege->def = privilege->enabled = (enabled != 0);
  375. list_add_tail( &token->privileges, &privilege->entry );
  376. }
  377. return privilege;
  378. }
  379. static inline void privilege_remove( struct privilege *privilege )
  380. {
  381. list_remove( &privilege->entry );
  382. free( privilege );
  383. }
  384. static void token_destroy( struct object *obj )
  385. {
  386. struct token* token;
  387. struct list *cursor, *cursor_next;
  388. assert( obj->ops == &token_ops );
  389. token = (struct token *)obj;
  390. free( token->user );
  391. LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->privileges )
  392. {
  393. struct privilege *privilege = LIST_ENTRY( cursor, struct privilege, entry );
  394. privilege_remove( privilege );
  395. }
  396. LIST_FOR_EACH_SAFE( cursor, cursor_next, &token->groups )
  397. {
  398. struct group *group = LIST_ENTRY( cursor, struct group, entry );
  399. list_remove( &group->entry );
  400. free( group );
  401. }
  402. free( token->default_dacl );
  403. }
  404. /* creates a new token.
  405. * groups may be NULL if group_count is 0.
  406. * privs may be NULL if priv_count is 0.
  407. * default_dacl may be NULL, indicating that all objects created by the user
  408. * are unsecured.
  409. * modified_id may be NULL, indicating that a new modified_id luid should be
  410. * allocated.
  411. */
  412. static struct token *create_token( unsigned int primary, unsigned int session_id, const struct sid *user,
  413. const struct sid_attrs *groups, unsigned int group_count,
  414. const struct luid_attr *privs, unsigned int priv_count,
  415. const struct acl *default_dacl, const struct luid *modified_id,
  416. unsigned int primary_group, int impersonation_level, int elevation )
  417. {
  418. struct token *token = alloc_object( &token_ops );
  419. if (token)
  420. {
  421. unsigned int i;
  422. allocate_luid( &token->token_id );
  423. if (modified_id)
  424. token->modified_id = *modified_id;
  425. else
  426. allocate_luid( &token->modified_id );
  427. list_init( &token->privileges );
  428. list_init( &token->groups );
  429. token->primary = primary;
  430. token->session_id = session_id;
  431. /* primary tokens don't have impersonation levels */
  432. if (primary)
  433. token->impersonation_level = -1;
  434. else
  435. token->impersonation_level = impersonation_level;
  436. token->default_dacl = NULL;
  437. token->primary_group = NULL;
  438. token->elevation = elevation;
  439. /* copy user */
  440. token->user = memdup( user, sid_len( user ));
  441. if (!token->user)
  442. {
  443. release_object( token );
  444. return NULL;
  445. }
  446. /* copy groups */
  447. for (i = 0; i < group_count; i++)
  448. {
  449. size_t size = offsetof( struct group, sid.sub_auth[groups[i].sid->sub_count] );
  450. struct group *group = mem_alloc( size );
  451. if (!group)
  452. {
  453. release_object( token );
  454. return NULL;
  455. }
  456. group->attrs = groups[i].attrs;
  457. copy_sid( &group->sid, groups[i].sid );
  458. list_add_tail( &token->groups, &group->entry );
  459. if (primary_group == i)
  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. 0, 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( &domain_users_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, 4 /* domain_users */, 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_owner( struct token *token )
  919. {
  920. return token->owner;
  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. /* create a security token */
  953. DECL_HANDLER(create_token)
  954. {
  955. struct token *token;
  956. struct object_attributes *objattr;
  957. struct sid *user;
  958. struct sid_attrs *groups;
  959. struct luid_attr *privs;
  960. struct acl *dacl = NULL;
  961. unsigned int i;
  962. data_size_t data_size, groups_size;
  963. struct acl *default_dacl = NULL;
  964. unsigned int *attrs;
  965. struct sid *sid;
  966. objattr = (struct object_attributes *)get_req_data();
  967. user = (struct sid *)get_req_data_after_objattr( objattr, &data_size );
  968. if (!user || !sid_valid_size( user, data_size ))
  969. {
  970. set_error( STATUS_INVALID_PARAMETER );
  971. return;
  972. }
  973. data_size -= sid_len( user );
  974. groups_size = req->group_count * sizeof( attrs[0] );
  975. if (data_size < groups_size)
  976. {
  977. set_error( STATUS_INVALID_PARAMETER );
  978. return;
  979. }
  980. if (req->primary_group < 0 || req->primary_group >= req->group_count)
  981. {
  982. set_error( STATUS_INVALID_PARAMETER );
  983. return;
  984. }
  985. groups = mem_alloc( req->group_count * sizeof( groups[0] ) );
  986. if (!groups) return;
  987. attrs = (unsigned int *)((char *)user + sid_len( user ));
  988. sid = (struct sid *)&attrs[req->group_count];
  989. for (i = 0; i < req->group_count; i++)
  990. {
  991. groups[i].attrs = attrs[i];
  992. groups[i].sid = sid;
  993. if (!sid_valid_size( sid, data_size - groups_size ))
  994. {
  995. free( groups );
  996. set_error( STATUS_INVALID_PARAMETER );
  997. return;
  998. }
  999. groups_size += sid_len( sid );
  1000. sid = (struct sid *)((char *)sid + sid_len( sid ));
  1001. }
  1002. data_size -= groups_size;
  1003. if (data_size < req->priv_count * sizeof( privs[0] ))
  1004. {
  1005. free( groups );
  1006. set_error( STATUS_INVALID_PARAMETER );
  1007. return;
  1008. }
  1009. privs = (struct luid_attr *)((char *)attrs + groups_size);
  1010. data_size -= req->priv_count * sizeof( privs[0] );
  1011. if (data_size)
  1012. {
  1013. dacl = (struct acl *)((char *)privs + req->priv_count * sizeof(privs[0]));
  1014. if (!acl_is_valid( dacl, data_size ))
  1015. {
  1016. free( groups );
  1017. set_error( STATUS_INVALID_PARAMETER );
  1018. return;
  1019. }
  1020. }
  1021. else
  1022. dacl = default_dacl = create_default_dacl( &domain_users_sid );
  1023. token = create_token( req->primary, default_session_id, user, groups, req->group_count,
  1024. privs, req->priv_count, dacl, NULL, req->primary_group, req->impersonation_level, 0 );
  1025. if (token)
  1026. reply->token = alloc_handle( current->process, token, req->access, objattr->attributes );
  1027. free( default_dacl );
  1028. free( groups );
  1029. }
  1030. /* open a security token */
  1031. DECL_HANDLER(open_token)
  1032. {
  1033. if (req->flags & OPEN_TOKEN_THREAD)
  1034. {
  1035. struct thread *thread = get_thread_from_handle( req->handle, 0 );
  1036. if (thread)
  1037. {
  1038. if (thread->token)
  1039. {
  1040. if (!thread->token->primary && thread->token->impersonation_level <= SecurityAnonymous)
  1041. set_error( STATUS_CANT_OPEN_ANONYMOUS );
  1042. else
  1043. reply->token = alloc_handle( current->process, thread->token,
  1044. req->access, req->attributes );
  1045. }
  1046. else
  1047. set_error( STATUS_NO_TOKEN );
  1048. release_object( thread );
  1049. }
  1050. }
  1051. else
  1052. {
  1053. struct process *process = get_process_from_handle( req->handle, 0 );
  1054. if (process)
  1055. {
  1056. if (process->token)
  1057. reply->token = alloc_handle( current->process, process->token, req->access,
  1058. req->attributes );
  1059. else
  1060. set_error( STATUS_NO_TOKEN );
  1061. release_object( process );
  1062. }
  1063. }
  1064. }
  1065. /* adjust the privileges held by a token */
  1066. DECL_HANDLER(adjust_token_privileges)
  1067. {
  1068. struct token *token;
  1069. unsigned int access = TOKEN_ADJUST_PRIVILEGES;
  1070. if (req->get_modified_state) access |= TOKEN_QUERY;
  1071. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1072. access, &token_ops )))
  1073. {
  1074. const struct luid_attr *privs = get_req_data();
  1075. struct luid_attr *modified_privs = NULL;
  1076. unsigned int priv_count = get_req_data_size() / sizeof(*privs);
  1077. unsigned int modified_priv_count = 0;
  1078. if (req->get_modified_state && !req->disable_all)
  1079. {
  1080. unsigned int i;
  1081. /* count modified privs */
  1082. for (i = 0; i < priv_count; i++)
  1083. {
  1084. struct privilege *privilege = token_find_privilege( token, privs[i].luid, FALSE );
  1085. if (privilege && req->get_modified_state)
  1086. modified_priv_count++;
  1087. }
  1088. reply->len = modified_priv_count;
  1089. modified_priv_count = min( modified_priv_count, get_reply_max_size() / sizeof(*modified_privs) );
  1090. if (modified_priv_count)
  1091. modified_privs = set_reply_data_size( modified_priv_count * sizeof(*modified_privs) );
  1092. }
  1093. reply->len = modified_priv_count * sizeof(*modified_privs);
  1094. if (req->disable_all)
  1095. token_disable_privileges( token );
  1096. else
  1097. token_adjust_privileges( token, privs, priv_count, modified_privs, modified_priv_count );
  1098. release_object( token );
  1099. }
  1100. }
  1101. /* retrieves the list of privileges that may be held be the token */
  1102. DECL_HANDLER(get_token_privileges)
  1103. {
  1104. struct token *token;
  1105. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1106. TOKEN_QUERY,
  1107. &token_ops )))
  1108. {
  1109. int priv_count = 0;
  1110. struct luid_attr *privs;
  1111. struct privilege *privilege;
  1112. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  1113. priv_count++;
  1114. reply->len = priv_count * sizeof(*privs);
  1115. if (reply->len <= get_reply_max_size())
  1116. {
  1117. privs = set_reply_data_size( priv_count * sizeof(*privs) );
  1118. if (privs)
  1119. LIST_FOR_EACH_ENTRY( privilege, &token->privileges, struct privilege, entry )
  1120. *privs++ = luid_and_attr_from_privilege( privilege );
  1121. }
  1122. else
  1123. set_error(STATUS_BUFFER_TOO_SMALL);
  1124. release_object( token );
  1125. }
  1126. }
  1127. /* creates a duplicate of the token */
  1128. DECL_HANDLER(duplicate_token)
  1129. {
  1130. struct token *src_token;
  1131. struct unicode_str name;
  1132. const struct security_descriptor *sd;
  1133. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
  1134. if (!objattr) return;
  1135. if ((src_token = (struct token *)get_handle_obj( current->process, req->handle,
  1136. TOKEN_DUPLICATE,
  1137. &token_ops )))
  1138. {
  1139. struct token *token = token_duplicate( src_token, req->primary, req->impersonation_level, sd, NULL, 0, NULL, 0 );
  1140. if (token)
  1141. {
  1142. unsigned int access = req->access ? req->access : get_handle_access( current->process, req->handle );
  1143. reply->new_handle = alloc_handle_no_access_check( current->process, token, access, objattr->attributes );
  1144. release_object( token );
  1145. }
  1146. release_object( src_token );
  1147. }
  1148. }
  1149. /* creates a restricted version of a token */
  1150. DECL_HANDLER(filter_token)
  1151. {
  1152. struct token *src_token;
  1153. if ((src_token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_DUPLICATE, &token_ops )))
  1154. {
  1155. const struct luid_attr *filter_privileges = get_req_data();
  1156. unsigned int priv_count, group_count;
  1157. const struct sid *filter_groups;
  1158. struct token *token;
  1159. priv_count = min( req->privileges_size, get_req_data_size() ) / sizeof(struct luid_attr);
  1160. filter_groups = (const struct sid *)((char *)filter_privileges + priv_count * sizeof(struct luid_attr));
  1161. group_count = get_sid_count( filter_groups, get_req_data_size() - priv_count * sizeof(struct luid_attr) );
  1162. token = token_duplicate( src_token, src_token->primary, src_token->impersonation_level, NULL,
  1163. filter_privileges, priv_count, filter_groups, group_count );
  1164. if (token)
  1165. {
  1166. unsigned int access = get_handle_access( current->process, req->handle );
  1167. reply->new_handle = alloc_handle_no_access_check( current->process, token, access, 0 );
  1168. release_object( token );
  1169. }
  1170. release_object( src_token );
  1171. }
  1172. }
  1173. /* checks the specified privileges are held by the token */
  1174. DECL_HANDLER(check_token_privileges)
  1175. {
  1176. struct token *token;
  1177. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1178. TOKEN_QUERY,
  1179. &token_ops )))
  1180. {
  1181. unsigned int count = get_req_data_size() / sizeof(struct luid_attr);
  1182. if (!token->primary && token->impersonation_level <= SecurityAnonymous)
  1183. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  1184. else if (get_reply_max_size() >= count * sizeof(struct luid_attr))
  1185. {
  1186. struct luid_attr *usedprivs = set_reply_data_size( count * sizeof(*usedprivs) );
  1187. reply->has_privileges = token_check_privileges( token, req->all_required, get_req_data(), count, usedprivs );
  1188. }
  1189. else
  1190. set_error( STATUS_BUFFER_OVERFLOW );
  1191. release_object( token );
  1192. }
  1193. }
  1194. /* checks that a user represented by a token is allowed to access an object
  1195. * represented by a security descriptor */
  1196. DECL_HANDLER(access_check)
  1197. {
  1198. data_size_t sd_size = get_req_data_size();
  1199. const struct security_descriptor *sd = get_req_data();
  1200. struct token *token;
  1201. if (!sd_is_valid( sd, sd_size ))
  1202. {
  1203. set_error( STATUS_ACCESS_VIOLATION );
  1204. return;
  1205. }
  1206. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1207. TOKEN_QUERY,
  1208. &token_ops )))
  1209. {
  1210. unsigned int status;
  1211. struct luid_attr priv;
  1212. unsigned int priv_count = 1;
  1213. memset(&priv, 0, sizeof(priv));
  1214. /* only impersonation tokens may be used with this function */
  1215. if (token->primary)
  1216. {
  1217. set_error( STATUS_NO_IMPERSONATION_TOKEN );
  1218. release_object( token );
  1219. return;
  1220. }
  1221. /* anonymous impersonation tokens can't be used */
  1222. if (token->impersonation_level <= SecurityAnonymous)
  1223. {
  1224. set_error( STATUS_BAD_IMPERSONATION_LEVEL );
  1225. release_object( token );
  1226. return;
  1227. }
  1228. status = token_access_check( token, sd, req->desired_access, &priv, &priv_count, &req->mapping,
  1229. &reply->access_granted, &reply->access_status );
  1230. reply->privileges_len = priv_count*sizeof(struct luid_attr);
  1231. if ((priv_count > 0) && (reply->privileges_len <= get_reply_max_size()))
  1232. {
  1233. struct luid_attr *privs = set_reply_data_size( priv_count * sizeof(*privs) );
  1234. memcpy( privs, &priv, sizeof(priv) );
  1235. }
  1236. set_error( status );
  1237. release_object( token );
  1238. }
  1239. }
  1240. /* retrieves an SID from the token */
  1241. DECL_HANDLER(get_token_sid)
  1242. {
  1243. struct token *token;
  1244. reply->sid_len = 0;
  1245. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1246. {
  1247. const struct sid *sid = NULL;
  1248. switch (req->which_sid)
  1249. {
  1250. case TokenUser:
  1251. assert(token->user);
  1252. sid = token->user;
  1253. break;
  1254. case TokenPrimaryGroup:
  1255. sid = token->primary_group;
  1256. break;
  1257. case TokenOwner:
  1258. sid = token->owner;
  1259. break;
  1260. default:
  1261. set_error( STATUS_INVALID_PARAMETER );
  1262. break;
  1263. }
  1264. if (sid)
  1265. {
  1266. reply->sid_len = sid_len( sid );
  1267. if (reply->sid_len <= get_reply_max_size()) set_reply_data( sid, reply->sid_len );
  1268. else set_error( STATUS_BUFFER_TOO_SMALL );
  1269. }
  1270. release_object( token );
  1271. }
  1272. }
  1273. /* retrieves the groups that the user represented by the token belongs to */
  1274. DECL_HANDLER(get_token_groups)
  1275. {
  1276. struct token *token;
  1277. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1278. {
  1279. unsigned int group_count = 0;
  1280. const struct group *group;
  1281. LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
  1282. {
  1283. if (req->attr_mask && !(group->attrs & req->attr_mask)) continue;
  1284. group_count++;
  1285. reply->sid_len += sid_len( &group->sid );
  1286. }
  1287. reply->attr_len = sizeof(unsigned int) * group_count;
  1288. if (reply->attr_len + reply->sid_len <= get_reply_max_size())
  1289. {
  1290. unsigned int *attr_ptr = set_reply_data_size( reply->attr_len + reply->sid_len );
  1291. struct sid *sid = (struct sid *)(attr_ptr + group_count);
  1292. if (attr_ptr)
  1293. {
  1294. LIST_FOR_EACH_ENTRY( group, &token->groups, const struct group, entry )
  1295. {
  1296. if (req->attr_mask && !(group->attrs & req->attr_mask)) continue;
  1297. sid = copy_sid( sid, &group->sid );
  1298. *attr_ptr++ = group->attrs;
  1299. }
  1300. }
  1301. }
  1302. else set_error( STATUS_BUFFER_TOO_SMALL );
  1303. release_object( token );
  1304. }
  1305. }
  1306. DECL_HANDLER(get_token_info)
  1307. {
  1308. struct token *token;
  1309. if ((token = (struct token *)get_handle_obj( current->process, req->handle, TOKEN_QUERY, &token_ops )))
  1310. {
  1311. reply->token_id = token->token_id;
  1312. reply->modified_id = token->modified_id;
  1313. reply->session_id = token->session_id;
  1314. reply->primary = token->primary;
  1315. reply->impersonation_level = token->impersonation_level;
  1316. reply->elevation = token->elevation;
  1317. reply->group_count = list_count( &token->groups );
  1318. reply->privilege_count = list_count( &token->privileges );
  1319. release_object( token );
  1320. }
  1321. }
  1322. DECL_HANDLER(get_token_default_dacl)
  1323. {
  1324. struct token *token;
  1325. reply->acl_len = 0;
  1326. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1327. TOKEN_QUERY,
  1328. &token_ops )))
  1329. {
  1330. if (token->default_dacl)
  1331. reply->acl_len = token->default_dacl->size;
  1332. if (reply->acl_len <= get_reply_max_size())
  1333. {
  1334. struct acl *acl_reply = set_reply_data_size( reply->acl_len );
  1335. if (acl_reply)
  1336. memcpy( acl_reply, token->default_dacl, reply->acl_len );
  1337. }
  1338. else set_error( STATUS_BUFFER_TOO_SMALL );
  1339. release_object( token );
  1340. }
  1341. }
  1342. DECL_HANDLER(set_token_default_dacl)
  1343. {
  1344. struct token *token;
  1345. const struct acl *acl = get_req_data();
  1346. unsigned int acl_size = get_req_data_size();
  1347. if (acl_size && !acl_is_valid( acl, acl_size ))
  1348. {
  1349. set_error( STATUS_INVALID_ACL );
  1350. return;
  1351. }
  1352. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1353. TOKEN_ADJUST_DEFAULT,
  1354. &token_ops )))
  1355. {
  1356. free( token->default_dacl );
  1357. token->default_dacl = NULL;
  1358. if (acl_size)
  1359. token->default_dacl = memdup( acl, acl_size );
  1360. release_object( token );
  1361. }
  1362. }
  1363. DECL_HANDLER(create_linked_token)
  1364. {
  1365. struct token *token, *linked;
  1366. int elevation;
  1367. if ((token = (struct token *)get_handle_obj( current->process, req->handle,
  1368. TOKEN_QUERY, &token_ops )))
  1369. {
  1370. switch (token->elevation)
  1371. {
  1372. case TokenElevationTypeFull:
  1373. elevation = TokenElevationTypeLimited;
  1374. break;
  1375. case TokenElevationTypeLimited:
  1376. elevation = TokenElevationTypeFull;
  1377. break;
  1378. default:
  1379. release_object( token );
  1380. return;
  1381. }
  1382. if ((linked = token_create_admin( FALSE, SecurityIdentification, elevation, token->session_id )))
  1383. {
  1384. reply->linked = alloc_handle( current->process, linked, TOKEN_ALL_ACCESS, 0 );
  1385. release_object( linked );
  1386. }
  1387. release_object( token );
  1388. }
  1389. }