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