token.c 58 KB

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