handle.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Server-side handle management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <limits.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28. #include "ntstatus.h"
  29. #define WIN32_NO_STATUS
  30. #include "windef.h"
  31. #include "winternl.h"
  32. #include "handle.h"
  33. #include "process.h"
  34. #include "thread.h"
  35. #include "security.h"
  36. #include "request.h"
  37. struct handle_entry
  38. {
  39. struct object *ptr; /* object */
  40. unsigned int access; /* access rights */
  41. };
  42. struct handle_table
  43. {
  44. struct object obj; /* object header */
  45. struct process *process; /* process owning this table */
  46. int count; /* number of allocated entries */
  47. int last; /* last used entry */
  48. int free; /* first entry that may be free */
  49. struct handle_entry *entries; /* handle entries */
  50. };
  51. static struct handle_table *global_table;
  52. /* reserved handle access rights */
  53. #define RESERVED_SHIFT 26
  54. #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
  55. #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
  56. #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
  57. #define MIN_HANDLE_ENTRIES 32
  58. #define MAX_HANDLE_ENTRIES 0x00ffffff
  59. /* handle to table index conversion */
  60. /* handles are a multiple of 4 under NT; handle 0 is not used */
  61. static inline obj_handle_t index_to_handle( int index )
  62. {
  63. return (obj_handle_t)((index + 1) << 2);
  64. }
  65. static inline int handle_to_index( obj_handle_t handle )
  66. {
  67. return (handle >> 2) - 1;
  68. }
  69. /* global handle conversion */
  70. #define HANDLE_OBFUSCATOR 0x544a4def
  71. static inline int handle_is_global( obj_handle_t handle)
  72. {
  73. return (handle ^ HANDLE_OBFUSCATOR) <= (MAX_HANDLE_ENTRIES << 2);
  74. }
  75. static inline obj_handle_t handle_local_to_global( obj_handle_t handle )
  76. {
  77. if (!handle) return 0;
  78. return handle ^ HANDLE_OBFUSCATOR;
  79. }
  80. static inline obj_handle_t handle_global_to_local( obj_handle_t handle )
  81. {
  82. return handle ^ HANDLE_OBFUSCATOR;
  83. }
  84. /* grab an object and increment its handle count */
  85. static struct object *grab_object_for_handle( struct object *obj )
  86. {
  87. obj->handle_count++;
  88. obj->ops->type->handle_count++;
  89. obj->ops->type->handle_max = max( obj->ops->type->handle_max, obj->ops->type->handle_count );
  90. return grab_object( obj );
  91. }
  92. /* release an object and decrement its handle count */
  93. static void release_object_from_handle( struct object *obj )
  94. {
  95. assert( obj->handle_count );
  96. obj->ops->type->handle_count--;
  97. obj->handle_count--;
  98. release_object( obj );
  99. }
  100. static void handle_table_dump( struct object *obj, int verbose );
  101. static void handle_table_destroy( struct object *obj );
  102. static const struct object_ops handle_table_ops =
  103. {
  104. sizeof(struct handle_table), /* size */
  105. &no_type, /* type */
  106. handle_table_dump, /* dump */
  107. no_add_queue, /* add_queue */
  108. NULL, /* remove_queue */
  109. NULL, /* signaled */
  110. NULL, /* satisfied */
  111. no_signal, /* signal */
  112. no_get_fd, /* get_fd */
  113. default_map_access, /* map_access */
  114. default_get_sd, /* get_sd */
  115. default_set_sd, /* set_sd */
  116. no_get_full_name, /* get_full_name */
  117. no_lookup_name, /* lookup_name */
  118. no_link_name, /* link_name */
  119. NULL, /* unlink_name */
  120. no_open_file, /* open_file */
  121. no_kernel_obj_list, /* get_kernel_obj_list */
  122. no_close_handle, /* close_handle */
  123. handle_table_destroy /* destroy */
  124. };
  125. /* dump a handle table */
  126. static void handle_table_dump( struct object *obj, int verbose )
  127. {
  128. int i;
  129. struct handle_table *table = (struct handle_table *)obj;
  130. struct handle_entry *entry;
  131. assert( obj->ops == &handle_table_ops );
  132. fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
  133. table->last, table->count, table->process );
  134. if (!verbose) return;
  135. entry = table->entries;
  136. for (i = 0; i <= table->last; i++, entry++)
  137. {
  138. if (!entry->ptr) continue;
  139. fprintf( stderr, " %04x: %p %08x ",
  140. index_to_handle(i), entry->ptr, entry->access );
  141. dump_object_name( entry->ptr );
  142. entry->ptr->ops->dump( entry->ptr, 0 );
  143. }
  144. }
  145. /* destroy a handle table */
  146. static void handle_table_destroy( struct object *obj )
  147. {
  148. int i;
  149. struct handle_table *table = (struct handle_table *)obj;
  150. struct handle_entry *entry;
  151. assert( obj->ops == &handle_table_ops );
  152. for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
  153. {
  154. struct object *obj = entry->ptr;
  155. entry->ptr = NULL;
  156. if (obj)
  157. {
  158. if (table->process)
  159. obj->ops->close_handle( obj, table->process, index_to_handle(i) );
  160. release_object_from_handle( obj );
  161. }
  162. }
  163. free( table->entries );
  164. }
  165. /* close all the process handles and free the handle table */
  166. void close_process_handles( struct process *process )
  167. {
  168. struct handle_table *table = process->handles;
  169. process->handles = NULL;
  170. if (table) release_object( table );
  171. }
  172. /* allocate a new handle table */
  173. struct handle_table *alloc_handle_table( struct process *process, int count )
  174. {
  175. struct handle_table *table;
  176. if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
  177. if (!(table = alloc_object( &handle_table_ops )))
  178. return NULL;
  179. table->process = process;
  180. table->count = count;
  181. table->last = -1;
  182. table->free = 0;
  183. if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
  184. release_object( table );
  185. return NULL;
  186. }
  187. /* grow a handle table */
  188. static int grow_handle_table( struct handle_table *table )
  189. {
  190. struct handle_entry *new_entries;
  191. int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
  192. if (count == table->count ||
  193. !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
  194. {
  195. set_error( STATUS_INSUFFICIENT_RESOURCES );
  196. return 0;
  197. }
  198. table->entries = new_entries;
  199. table->count = count;
  200. return 1;
  201. }
  202. /* allocate the first free entry in the handle table */
  203. static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
  204. {
  205. struct handle_entry *entry = table->entries + table->free;
  206. int i;
  207. for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
  208. if (i >= table->count)
  209. {
  210. if (!grow_handle_table( table )) return 0;
  211. entry = table->entries + i; /* the entries may have moved */
  212. }
  213. table->last = i;
  214. found:
  215. table->free = i + 1;
  216. entry->ptr = grab_object_for_handle( obj );
  217. entry->access = access;
  218. return index_to_handle(i);
  219. }
  220. /* allocate a handle for an object, incrementing its refcount */
  221. static obj_handle_t alloc_handle_entry( struct process *process, void *ptr,
  222. unsigned int access, unsigned int attr )
  223. {
  224. struct object *obj = ptr;
  225. assert( !(access & RESERVED_ALL) );
  226. if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
  227. if (!process->handles)
  228. {
  229. set_error( STATUS_PROCESS_IS_TERMINATING );
  230. return 0;
  231. }
  232. return alloc_entry( process->handles, obj, access );
  233. }
  234. /* allocate a handle for an object, incrementing its refcount */
  235. /* return the handle, or 0 on error */
  236. obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
  237. {
  238. struct object *obj = ptr;
  239. if (access & MAXIMUM_ALLOWED) access = GENERIC_ALL;
  240. access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
  241. return alloc_handle_entry( process, ptr, access, attr );
  242. }
  243. /* allocate a handle for an object, checking the dacl allows the process to */
  244. /* access it and incrementing its refcount */
  245. /* return the handle, or 0 on error */
  246. obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
  247. {
  248. struct object *obj = ptr;
  249. access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
  250. if (access && !check_object_access( NULL, obj, &access )) return 0;
  251. return alloc_handle_entry( process, ptr, access, attr );
  252. }
  253. /* allocate a global handle for an object, incrementing its refcount */
  254. /* return the handle, or 0 on error */
  255. static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
  256. {
  257. if (!global_table)
  258. {
  259. if (!(global_table = alloc_handle_table( NULL, 0 )))
  260. return 0;
  261. make_object_permanent( &global_table->obj );
  262. }
  263. return handle_local_to_global( alloc_entry( global_table, obj, access ));
  264. }
  265. /* allocate a global handle for an object, checking the dacl allows the */
  266. /* process to access it and incrementing its refcount and incrementing its refcount */
  267. /* return the handle, or 0 on error */
  268. static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
  269. {
  270. if (access && !check_object_access( NULL, obj, &access )) return 0;
  271. return alloc_global_handle_no_access_check( obj, access );
  272. }
  273. /* return a handle entry, or NULL if the handle is invalid */
  274. static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
  275. {
  276. struct handle_table *table = process->handles;
  277. struct handle_entry *entry;
  278. int index;
  279. if (handle_is_global(handle))
  280. {
  281. handle = handle_global_to_local(handle);
  282. table = global_table;
  283. }
  284. if (!table) return NULL;
  285. index = handle_to_index( handle );
  286. if (index < 0) return NULL;
  287. if (index > table->last) return NULL;
  288. entry = table->entries + index;
  289. if (!entry->ptr) return NULL;
  290. return entry;
  291. }
  292. /* attempt to shrink a table */
  293. static void shrink_handle_table( struct handle_table *table )
  294. {
  295. struct handle_entry *entry = table->entries + table->last;
  296. struct handle_entry *new_entries;
  297. int count = table->count;
  298. while (table->last >= 0)
  299. {
  300. if (entry->ptr) break;
  301. table->last--;
  302. entry--;
  303. }
  304. if (table->last >= count / 4) return; /* no need to shrink */
  305. if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
  306. count /= 2;
  307. if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
  308. table->count = count;
  309. table->entries = new_entries;
  310. }
  311. static void inherit_handle( struct process *parent, const obj_handle_t handle, struct handle_table *table )
  312. {
  313. struct handle_entry *dst, *src;
  314. int index;
  315. dst = table->entries;
  316. src = get_handle( parent, handle );
  317. if (!src || !(src->access & RESERVED_INHERIT)) return;
  318. index = handle_to_index( handle );
  319. if (dst[index].ptr) return;
  320. grab_object_for_handle( src->ptr );
  321. dst[index] = *src;
  322. table->last = max( table->last, index );
  323. }
  324. /* copy the handle table of the parent process */
  325. /* return 1 if OK, 0 on error */
  326. struct handle_table *copy_handle_table( struct process *process, struct process *parent,
  327. const obj_handle_t *handles, unsigned int handle_count,
  328. const obj_handle_t *std_handles )
  329. {
  330. struct handle_table *parent_table = parent->handles;
  331. struct handle_table *table;
  332. int i;
  333. assert( parent_table );
  334. assert( parent_table->obj.ops == &handle_table_ops );
  335. if (!(table = alloc_handle_table( process, parent_table->count )))
  336. return NULL;
  337. if (handles)
  338. {
  339. memset( table->entries, 0, parent_table->count * sizeof(*table->entries) );
  340. for (i = 0; i < handle_count; i++)
  341. {
  342. inherit_handle( parent, handles[i], table );
  343. }
  344. for (i = 0; i < 3; i++)
  345. {
  346. inherit_handle( parent, std_handles[i], table );
  347. }
  348. }
  349. else
  350. {
  351. if ((table->last = parent_table->last) >= 0)
  352. {
  353. struct handle_entry *ptr = table->entries;
  354. memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
  355. for (i = 0; i <= table->last; i++, ptr++)
  356. {
  357. if (!ptr->ptr) continue;
  358. if (ptr->access & RESERVED_INHERIT) grab_object_for_handle( ptr->ptr );
  359. else ptr->ptr = NULL; /* don't inherit this entry */
  360. }
  361. }
  362. }
  363. /* attempt to shrink the table */
  364. shrink_handle_table( table );
  365. return table;
  366. }
  367. /* close a handle and decrement the refcount of the associated object */
  368. unsigned int close_handle( struct process *process, obj_handle_t handle )
  369. {
  370. struct handle_table *table;
  371. struct handle_entry *entry;
  372. struct object *obj;
  373. if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
  374. if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
  375. obj = entry->ptr;
  376. if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
  377. entry->ptr = NULL;
  378. table = handle_is_global(handle) ? global_table : process->handles;
  379. if (entry < table->entries + table->free) table->free = entry - table->entries;
  380. if (entry == table->entries + table->last) shrink_handle_table( table );
  381. release_object_from_handle( obj );
  382. return STATUS_SUCCESS;
  383. }
  384. /* retrieve the object corresponding to one of the magic pseudo-handles */
  385. static inline struct object *get_magic_handle( obj_handle_t handle )
  386. {
  387. switch(handle)
  388. {
  389. case 0xfffffffa: /* current thread impersonation token pseudo-handle */
  390. return (struct object *)thread_get_impersonation_token( current );
  391. case 0xfffffffb: /* current thread token pseudo-handle */
  392. return (struct object *)current->token;
  393. case 0xfffffffc: /* current process token pseudo-handle */
  394. return (struct object *)current->process->token;
  395. case 0xfffffffe: /* current thread pseudo-handle */
  396. return &current->obj;
  397. case 0x7fffffff: /* current process pseudo-handle */
  398. case 0xffffffff: /* current process pseudo-handle */
  399. return (struct object *)current->process;
  400. default:
  401. return NULL;
  402. }
  403. }
  404. /* retrieve the object corresponding to a handle, incrementing its refcount */
  405. struct object *get_handle_obj( struct process *process, obj_handle_t handle,
  406. unsigned int access, const struct object_ops *ops )
  407. {
  408. struct handle_entry *entry;
  409. struct object *obj;
  410. if (!(obj = get_magic_handle( handle )))
  411. {
  412. if (!(entry = get_handle( process, handle )))
  413. {
  414. set_error( STATUS_INVALID_HANDLE );
  415. return NULL;
  416. }
  417. obj = entry->ptr;
  418. if (ops && (obj->ops != ops))
  419. {
  420. set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
  421. return NULL;
  422. }
  423. if ((entry->access & access) != access)
  424. {
  425. set_error( STATUS_ACCESS_DENIED );
  426. return NULL;
  427. }
  428. }
  429. else if (ops && (obj->ops != ops))
  430. {
  431. set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
  432. return NULL;
  433. }
  434. return grab_object( obj );
  435. }
  436. /* retrieve the access rights of a given handle */
  437. unsigned int get_handle_access( struct process *process, obj_handle_t handle )
  438. {
  439. struct handle_entry *entry;
  440. if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
  441. if (!(entry = get_handle( process, handle ))) return 0;
  442. return entry->access & ~RESERVED_ALL;
  443. }
  444. /* find the first inherited handle of the given type */
  445. /* this is needed for window stations and desktops (don't ask...) */
  446. obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
  447. {
  448. struct handle_table *table = process->handles;
  449. struct handle_entry *ptr;
  450. int i;
  451. if (!table) return 0;
  452. for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
  453. {
  454. if (!ptr->ptr) continue;
  455. if (ptr->ptr->ops != ops) continue;
  456. if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
  457. }
  458. return 0;
  459. }
  460. /* get/set the handle reserved flags */
  461. /* return the old flags (or -1 on error) */
  462. static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
  463. {
  464. struct handle_entry *entry;
  465. unsigned int old_access;
  466. if (get_magic_handle( handle ))
  467. {
  468. /* we can retrieve but not set info for magic handles */
  469. if (mask) set_error( STATUS_ACCESS_DENIED );
  470. return 0;
  471. }
  472. if (!(entry = get_handle( process, handle )))
  473. {
  474. set_error( STATUS_INVALID_HANDLE );
  475. return -1;
  476. }
  477. old_access = entry->access;
  478. mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
  479. flags = (flags << RESERVED_SHIFT) & mask;
  480. entry->access = (entry->access & ~mask) | flags;
  481. return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
  482. }
  483. /* duplicate a handle */
  484. obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
  485. unsigned int access, unsigned int attr, unsigned int options )
  486. {
  487. obj_handle_t res;
  488. struct handle_entry *entry;
  489. unsigned int src_access;
  490. struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
  491. if (!obj) return 0;
  492. if ((entry = get_handle( src, src_handle )))
  493. src_access = entry->access;
  494. else /* pseudo-handle, give it full access */
  495. src_access = obj->ops->map_access( obj, GENERIC_ALL );
  496. src_access &= ~RESERVED_ALL;
  497. if (options & DUPLICATE_SAME_ACCESS)
  498. access = src_access;
  499. else
  500. access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
  501. /* asking for the more access rights than src_access? */
  502. if (access & ~src_access)
  503. {
  504. if ((current->token && !check_object_access( current->token, obj, &access )) ||
  505. !check_object_access( dst->token, obj, &access ))
  506. {
  507. release_object( obj );
  508. return 0;
  509. }
  510. if (options & DUPLICATE_MAKE_GLOBAL)
  511. res = alloc_global_handle( obj, access );
  512. else
  513. res = alloc_handle_no_access_check( dst, obj, access, attr );
  514. }
  515. else
  516. {
  517. if (options & DUPLICATE_MAKE_GLOBAL)
  518. res = alloc_global_handle_no_access_check( obj, access );
  519. else if ((options & DUPLICATE_CLOSE_SOURCE) && src == dst &&
  520. entry && !(entry->access & RESERVED_CLOSE_PROTECT))
  521. {
  522. if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
  523. entry->access = access;
  524. res = src_handle;
  525. }
  526. else
  527. res = alloc_handle_entry( dst, obj, access, attr );
  528. }
  529. release_object( obj );
  530. return res;
  531. }
  532. /* open a new handle to an existing object */
  533. obj_handle_t open_object( struct process *process, obj_handle_t parent, unsigned int access,
  534. const struct object_ops *ops, const struct unicode_str *name,
  535. unsigned int attributes )
  536. {
  537. obj_handle_t handle = 0;
  538. struct object *obj, *root = NULL;
  539. if (name->len >= 65534)
  540. {
  541. set_error( STATUS_OBJECT_NAME_INVALID );
  542. return 0;
  543. }
  544. if (parent)
  545. {
  546. if (name->len)
  547. root = get_directory_obj( process, parent );
  548. else /* opening the object itself can work for non-directories too */
  549. root = get_handle_obj( process, parent, 0, NULL );
  550. if (!root) return 0;
  551. }
  552. if ((obj = open_named_object( root, ops, name, attributes )))
  553. {
  554. handle = alloc_handle( process, obj, access, attributes );
  555. release_object( obj );
  556. }
  557. if (root) release_object( root );
  558. return handle;
  559. }
  560. /* return the size of the handle table of a given process */
  561. unsigned int get_handle_table_count( struct process *process )
  562. {
  563. if (!process->handles) return 0;
  564. return process->handles->count;
  565. }
  566. /* close a handle */
  567. DECL_HANDLER(close_handle)
  568. {
  569. unsigned int err = close_handle( current->process, req->handle );
  570. set_error( err );
  571. }
  572. /* set a handle information */
  573. DECL_HANDLER(set_handle_info)
  574. {
  575. reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
  576. }
  577. /* duplicate a handle */
  578. DECL_HANDLER(dup_handle)
  579. {
  580. struct process *src, *dst = NULL;
  581. reply->handle = 0;
  582. if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
  583. {
  584. if (req->options & DUPLICATE_MAKE_GLOBAL)
  585. {
  586. reply->handle = duplicate_handle( src, req->src_handle, NULL,
  587. req->access, req->attributes, req->options );
  588. }
  589. else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
  590. {
  591. reply->handle = duplicate_handle( src, req->src_handle, dst,
  592. req->access, req->attributes, req->options );
  593. release_object( dst );
  594. }
  595. /* close the handle no matter what happened */
  596. if ((req->options & DUPLICATE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
  597. close_handle( src, req->src_handle );
  598. release_object( src );
  599. }
  600. }
  601. DECL_HANDLER(get_object_info)
  602. {
  603. struct object *obj;
  604. if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
  605. reply->access = get_handle_access( current->process, req->handle );
  606. reply->ref_count = obj->refcount;
  607. reply->handle_count = obj->handle_count;
  608. release_object( obj );
  609. }
  610. DECL_HANDLER(get_object_name)
  611. {
  612. struct object *obj;
  613. WCHAR *name;
  614. if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
  615. if ((name = obj->ops->get_full_name( obj, &reply->total )))
  616. set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
  617. release_object( obj );
  618. }
  619. DECL_HANDLER(set_security_object)
  620. {
  621. data_size_t sd_size = get_req_data_size();
  622. const struct security_descriptor *sd = get_req_data();
  623. struct object *obj;
  624. unsigned int access = 0;
  625. if (!sd_is_valid( sd, sd_size ))
  626. {
  627. set_error( STATUS_ACCESS_VIOLATION );
  628. return;
  629. }
  630. if (req->security_info & OWNER_SECURITY_INFORMATION ||
  631. req->security_info & GROUP_SECURITY_INFORMATION ||
  632. req->security_info & LABEL_SECURITY_INFORMATION)
  633. access |= WRITE_OWNER;
  634. if (req->security_info & SACL_SECURITY_INFORMATION)
  635. access |= ACCESS_SYSTEM_SECURITY;
  636. if (req->security_info & DACL_SECURITY_INFORMATION)
  637. access |= WRITE_DAC;
  638. if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
  639. obj->ops->set_sd( obj, sd, req->security_info );
  640. release_object( obj );
  641. }
  642. DECL_HANDLER(get_security_object)
  643. {
  644. const struct security_descriptor *sd;
  645. struct object *obj;
  646. unsigned int access = READ_CONTROL;
  647. struct security_descriptor req_sd;
  648. int present;
  649. const SID *owner, *group;
  650. const ACL *sacl, *dacl;
  651. ACL *label_acl = NULL;
  652. if (req->security_info & SACL_SECURITY_INFORMATION)
  653. access |= ACCESS_SYSTEM_SECURITY;
  654. if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
  655. sd = obj->ops->get_sd( obj );
  656. if (sd)
  657. {
  658. req_sd.control = sd->control & ~SE_SELF_RELATIVE;
  659. owner = sd_get_owner( sd );
  660. if (req->security_info & OWNER_SECURITY_INFORMATION)
  661. req_sd.owner_len = sd->owner_len;
  662. else
  663. req_sd.owner_len = 0;
  664. group = sd_get_group( sd );
  665. if (req->security_info & GROUP_SECURITY_INFORMATION)
  666. req_sd.group_len = sd->group_len;
  667. else
  668. req_sd.group_len = 0;
  669. sacl = sd_get_sacl( sd, &present );
  670. if (req->security_info & SACL_SECURITY_INFORMATION && present)
  671. req_sd.sacl_len = sd->sacl_len;
  672. else if (req->security_info & LABEL_SECURITY_INFORMATION && present && sacl)
  673. {
  674. if (!(label_acl = extract_security_labels( sacl ))) goto done;
  675. req_sd.sacl_len = label_acl->AclSize;
  676. sacl = label_acl;
  677. }
  678. else
  679. req_sd.sacl_len = 0;
  680. dacl = sd_get_dacl( sd, &present );
  681. if (req->security_info & DACL_SECURITY_INFORMATION && present)
  682. req_sd.dacl_len = sd->dacl_len;
  683. else
  684. req_sd.dacl_len = 0;
  685. reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
  686. req_sd.sacl_len + req_sd.dacl_len;
  687. if (reply->sd_len <= get_reply_max_size())
  688. {
  689. char *ptr = set_reply_data_size(reply->sd_len);
  690. memcpy( ptr, &req_sd, sizeof(req_sd) );
  691. ptr += sizeof(req_sd);
  692. memcpy( ptr, owner, req_sd.owner_len );
  693. ptr += req_sd.owner_len;
  694. memcpy( ptr, group, req_sd.group_len );
  695. ptr += req_sd.group_len;
  696. memcpy( ptr, sacl, req_sd.sacl_len );
  697. ptr += req_sd.sacl_len;
  698. memcpy( ptr, dacl, req_sd.dacl_len );
  699. }
  700. else
  701. set_error(STATUS_BUFFER_TOO_SMALL);
  702. }
  703. done:
  704. release_object( obj );
  705. free( label_acl );
  706. }
  707. struct enum_handle_info
  708. {
  709. unsigned int count;
  710. struct handle_info *handle;
  711. };
  712. static int enum_handles( struct process *process, void *user )
  713. {
  714. struct enum_handle_info *info = user;
  715. struct handle_table *table = process->handles;
  716. struct handle_entry *entry;
  717. struct handle_info *handle;
  718. unsigned int i;
  719. if (!table)
  720. return 0;
  721. for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
  722. {
  723. if (!entry->ptr) continue;
  724. if (!info->handle)
  725. {
  726. info->count++;
  727. continue;
  728. }
  729. assert( info->count );
  730. handle = info->handle++;
  731. handle->owner = process->id;
  732. handle->handle = index_to_handle(i);
  733. handle->access = entry->access & ~RESERVED_ALL;
  734. handle->type = entry->ptr->ops->type->index;
  735. handle->attributes = 0;
  736. if (entry->access & RESERVED_INHERIT) handle->attributes |= OBJ_INHERIT;
  737. if (entry->access & RESERVED_CLOSE_PROTECT) handle->attributes |= OBJ_PROTECT_CLOSE;
  738. info->count--;
  739. }
  740. return 0;
  741. }
  742. DECL_HANDLER(get_system_handles)
  743. {
  744. struct enum_handle_info info;
  745. struct handle_info *handle;
  746. data_size_t max_handles = get_reply_max_size() / sizeof(*handle);
  747. info.handle = NULL;
  748. info.count = 0;
  749. enum_processes( enum_handles, &info );
  750. reply->count = info.count;
  751. if (max_handles < info.count)
  752. set_error( STATUS_BUFFER_TOO_SMALL );
  753. else if ((handle = set_reply_data_size( info.count * sizeof(*handle) )))
  754. {
  755. info.handle = handle;
  756. enum_processes( enum_handles, &info );
  757. }
  758. }
  759. DECL_HANDLER(make_temporary)
  760. {
  761. struct object *obj;
  762. if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
  763. if (obj->is_permanent)
  764. {
  765. make_object_temporary( obj );
  766. release_object( obj );
  767. }
  768. release_object( obj );
  769. }
  770. DECL_HANDLER(compare_objects)
  771. {
  772. struct object *obj1, *obj2;
  773. if (!(obj1 = get_handle_obj( current->process, req->first, 0, NULL ))) return;
  774. if (!(obj2 = get_handle_obj( current->process, req->second, 0, NULL )))
  775. {
  776. release_object( obj1 );
  777. return;
  778. }
  779. if (obj1 != obj2) set_error( STATUS_NOT_SAME_OBJECT );
  780. release_object( obj2 );
  781. release_object( obj1 );
  782. }