handle.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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_get_fast_sync, /* get_fast_sync */
  123. no_close_handle, /* close_handle */
  124. handle_table_destroy /* destroy */
  125. };
  126. /* dump a handle table */
  127. static void handle_table_dump( struct object *obj, int verbose )
  128. {
  129. int i;
  130. struct handle_table *table = (struct handle_table *)obj;
  131. struct handle_entry *entry;
  132. assert( obj->ops == &handle_table_ops );
  133. fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
  134. table->last, table->count, table->process );
  135. if (!verbose) return;
  136. entry = table->entries;
  137. for (i = 0; i <= table->last; i++, entry++)
  138. {
  139. if (!entry->ptr) continue;
  140. fprintf( stderr, " %04x: %p %08x ",
  141. index_to_handle(i), entry->ptr, entry->access );
  142. dump_object_name( entry->ptr );
  143. entry->ptr->ops->dump( entry->ptr, 0 );
  144. }
  145. }
  146. /* destroy a handle table */
  147. static void handle_table_destroy( struct object *obj )
  148. {
  149. int i;
  150. struct handle_table *table = (struct handle_table *)obj;
  151. struct handle_entry *entry;
  152. assert( obj->ops == &handle_table_ops );
  153. for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
  154. {
  155. struct object *obj = entry->ptr;
  156. entry->ptr = NULL;
  157. if (obj)
  158. {
  159. if (table->process)
  160. obj->ops->close_handle( obj, table->process, index_to_handle(i) );
  161. release_object_from_handle( obj );
  162. }
  163. }
  164. free( table->entries );
  165. }
  166. /* close all the process handles and free the handle table */
  167. void close_process_handles( struct process *process )
  168. {
  169. struct handle_table *table = process->handles;
  170. process->handles = NULL;
  171. if (table) release_object( table );
  172. }
  173. /* allocate a new handle table */
  174. struct handle_table *alloc_handle_table( struct process *process, int count )
  175. {
  176. struct handle_table *table;
  177. if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
  178. if (!(table = alloc_object( &handle_table_ops )))
  179. return NULL;
  180. table->process = process;
  181. table->count = count;
  182. table->last = -1;
  183. table->free = 0;
  184. if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
  185. release_object( table );
  186. return NULL;
  187. }
  188. /* grow a handle table */
  189. static int grow_handle_table( struct handle_table *table )
  190. {
  191. struct handle_entry *new_entries;
  192. int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
  193. if (count == table->count ||
  194. !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
  195. {
  196. set_error( STATUS_INSUFFICIENT_RESOURCES );
  197. return 0;
  198. }
  199. table->entries = new_entries;
  200. table->count = count;
  201. return 1;
  202. }
  203. /* allocate the first free entry in the handle table */
  204. static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
  205. {
  206. struct handle_entry *entry = table->entries + table->free;
  207. int i;
  208. for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
  209. if (i >= table->count)
  210. {
  211. if (!grow_handle_table( table )) return 0;
  212. entry = table->entries + i; /* the entries may have moved */
  213. }
  214. table->last = i;
  215. found:
  216. table->free = i + 1;
  217. entry->ptr = grab_object_for_handle( obj );
  218. entry->access = access;
  219. return index_to_handle(i);
  220. }
  221. /* allocate a handle for an object, incrementing its refcount */
  222. static obj_handle_t alloc_handle_entry( struct process *process, void *ptr,
  223. unsigned int access, unsigned int attr )
  224. {
  225. struct object *obj = ptr;
  226. assert( !(access & RESERVED_ALL) );
  227. if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
  228. if (!process->handles)
  229. {
  230. set_error( STATUS_PROCESS_IS_TERMINATING );
  231. return 0;
  232. }
  233. return alloc_entry( process->handles, obj, access );
  234. }
  235. /* allocate a handle for an object, incrementing its refcount */
  236. /* return the handle, or 0 on error */
  237. obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
  238. {
  239. struct object *obj = ptr;
  240. if (access & MAXIMUM_ALLOWED) access = GENERIC_ALL;
  241. access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
  242. return alloc_handle_entry( process, ptr, access, attr );
  243. }
  244. /* allocate a handle for an object, checking the dacl allows the process to */
  245. /* access it and incrementing its refcount */
  246. /* return the handle, or 0 on error */
  247. obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
  248. {
  249. struct object *obj = ptr;
  250. access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
  251. if (access && !check_object_access( NULL, obj, &access )) return 0;
  252. return alloc_handle_entry( process, ptr, access, attr );
  253. }
  254. /* allocate a global handle for an object, incrementing its refcount */
  255. /* return the handle, or 0 on error */
  256. static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
  257. {
  258. if (!global_table)
  259. {
  260. if (!(global_table = alloc_handle_table( NULL, 0 )))
  261. return 0;
  262. make_object_permanent( &global_table->obj );
  263. }
  264. return handle_local_to_global( alloc_entry( global_table, obj, access ));
  265. }
  266. /* allocate a global handle for an object, checking the dacl allows the */
  267. /* process to access it and incrementing its refcount and incrementing its refcount */
  268. /* return the handle, or 0 on error */
  269. static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
  270. {
  271. if (access && !check_object_access( NULL, obj, &access )) return 0;
  272. return alloc_global_handle_no_access_check( obj, access );
  273. }
  274. /* return a handle entry, or NULL if the handle is invalid */
  275. static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
  276. {
  277. struct handle_table *table = process->handles;
  278. struct handle_entry *entry;
  279. int index;
  280. if (handle_is_global(handle))
  281. {
  282. handle = handle_global_to_local(handle);
  283. table = global_table;
  284. }
  285. if (!table) return NULL;
  286. index = handle_to_index( handle );
  287. if (index < 0) return NULL;
  288. if (index > table->last) return NULL;
  289. entry = table->entries + index;
  290. if (!entry->ptr) return NULL;
  291. return entry;
  292. }
  293. /* attempt to shrink a table */
  294. static void shrink_handle_table( struct handle_table *table )
  295. {
  296. struct handle_entry *entry = table->entries + table->last;
  297. struct handle_entry *new_entries;
  298. int count = table->count;
  299. while (table->last >= 0)
  300. {
  301. if (entry->ptr) break;
  302. table->last--;
  303. entry--;
  304. }
  305. if (table->last >= count / 4) return; /* no need to shrink */
  306. if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
  307. count /= 2;
  308. if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
  309. table->count = count;
  310. table->entries = new_entries;
  311. }
  312. static void inherit_handle( struct process *parent, const obj_handle_t handle, struct handle_table *table )
  313. {
  314. struct handle_entry *dst, *src;
  315. int index;
  316. dst = table->entries;
  317. src = get_handle( parent, handle );
  318. if (!src || !(src->access & RESERVED_INHERIT)) return;
  319. index = handle_to_index( handle );
  320. if (dst[index].ptr) return;
  321. grab_object_for_handle( src->ptr );
  322. dst[index] = *src;
  323. table->last = max( table->last, index );
  324. }
  325. /* copy the handle table of the parent process */
  326. /* return 1 if OK, 0 on error */
  327. struct handle_table *copy_handle_table( struct process *process, struct process *parent,
  328. const obj_handle_t *handles, unsigned int handle_count,
  329. const obj_handle_t *std_handles )
  330. {
  331. struct handle_table *parent_table = parent->handles;
  332. struct handle_table *table;
  333. int i;
  334. assert( parent_table );
  335. assert( parent_table->obj.ops == &handle_table_ops );
  336. if (!(table = alloc_handle_table( process, parent_table->count )))
  337. return NULL;
  338. if (handles)
  339. {
  340. memset( table->entries, 0, parent_table->count * sizeof(*table->entries) );
  341. for (i = 0; i < handle_count; i++)
  342. {
  343. inherit_handle( parent, handles[i], table );
  344. }
  345. for (i = 0; i < 3; i++)
  346. {
  347. inherit_handle( parent, std_handles[i], table );
  348. }
  349. }
  350. else
  351. {
  352. if ((table->last = parent_table->last) >= 0)
  353. {
  354. struct handle_entry *ptr = table->entries;
  355. memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
  356. for (i = 0; i <= table->last; i++, ptr++)
  357. {
  358. if (!ptr->ptr) continue;
  359. if (ptr->access & RESERVED_INHERIT) grab_object_for_handle( ptr->ptr );
  360. else ptr->ptr = NULL; /* don't inherit this entry */
  361. }
  362. }
  363. }
  364. /* attempt to shrink the table */
  365. shrink_handle_table( table );
  366. return table;
  367. }
  368. /* close a handle and decrement the refcount of the associated object */
  369. unsigned int close_handle( struct process *process, obj_handle_t handle )
  370. {
  371. struct handle_table *table;
  372. struct handle_entry *entry;
  373. struct object *obj;
  374. if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
  375. if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
  376. obj = entry->ptr;
  377. if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
  378. entry->ptr = NULL;
  379. table = handle_is_global(handle) ? global_table : process->handles;
  380. if (entry < table->entries + table->free) table->free = entry - table->entries;
  381. if (entry == table->entries + table->last) shrink_handle_table( table );
  382. release_object_from_handle( obj );
  383. return STATUS_SUCCESS;
  384. }
  385. /* retrieve the object corresponding to one of the magic pseudo-handles */
  386. static inline struct object *get_magic_handle( obj_handle_t handle )
  387. {
  388. switch(handle)
  389. {
  390. case 0xfffffffa: /* current thread impersonation token pseudo-handle */
  391. return (struct object *)thread_get_impersonation_token( current );
  392. case 0xfffffffb: /* current thread token pseudo-handle */
  393. return (struct object *)current->token;
  394. case 0xfffffffc: /* current process token pseudo-handle */
  395. return (struct object *)current->process->token;
  396. case 0xfffffffe: /* current thread pseudo-handle */
  397. return &current->obj;
  398. case 0x7fffffff: /* current process pseudo-handle */
  399. case 0xffffffff: /* current process pseudo-handle */
  400. return (struct object *)current->process;
  401. default:
  402. return NULL;
  403. }
  404. }
  405. /* retrieve the object corresponding to a handle, incrementing its refcount */
  406. struct object *get_handle_obj( struct process *process, obj_handle_t handle,
  407. unsigned int access, const struct object_ops *ops )
  408. {
  409. struct handle_entry *entry;
  410. struct object *obj;
  411. if (!(obj = get_magic_handle( handle )))
  412. {
  413. if (!(entry = get_handle( process, handle )))
  414. {
  415. set_error( STATUS_INVALID_HANDLE );
  416. return NULL;
  417. }
  418. obj = entry->ptr;
  419. if (ops && (obj->ops != ops))
  420. {
  421. set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
  422. return NULL;
  423. }
  424. if ((entry->access & access) != access)
  425. {
  426. set_error( STATUS_ACCESS_DENIED );
  427. return NULL;
  428. }
  429. }
  430. else if (ops && (obj->ops != ops))
  431. {
  432. set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
  433. return NULL;
  434. }
  435. return grab_object( obj );
  436. }
  437. /* retrieve the access rights of a given handle */
  438. unsigned int get_handle_access( struct process *process, obj_handle_t handle )
  439. {
  440. struct handle_entry *entry;
  441. if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
  442. if (!(entry = get_handle( process, handle ))) return 0;
  443. return entry->access & ~RESERVED_ALL;
  444. }
  445. /* find the first inherited handle of the given type */
  446. /* this is needed for window stations and desktops (don't ask...) */
  447. obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
  448. {
  449. struct handle_table *table = process->handles;
  450. struct handle_entry *ptr;
  451. int i;
  452. if (!table) return 0;
  453. for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
  454. {
  455. if (!ptr->ptr) continue;
  456. if (ptr->ptr->ops != ops) continue;
  457. if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
  458. }
  459. return 0;
  460. }
  461. /* return number of open handles to the object in the process */
  462. unsigned int get_obj_handle_count( struct process *process, const struct object *obj )
  463. {
  464. struct handle_table *table = process->handles;
  465. struct handle_entry *ptr;
  466. unsigned int count = 0;
  467. int i;
  468. if (!table) return 0;
  469. for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
  470. if (ptr->ptr == obj) ++count;
  471. return count;
  472. }
  473. /* get/set the handle reserved flags */
  474. /* return the old flags (or -1 on error) */
  475. static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
  476. {
  477. struct handle_entry *entry;
  478. unsigned int old_access;
  479. if (get_magic_handle( handle ))
  480. {
  481. /* we can retrieve but not set info for magic handles */
  482. if (mask) set_error( STATUS_ACCESS_DENIED );
  483. return 0;
  484. }
  485. if (!(entry = get_handle( process, handle )))
  486. {
  487. set_error( STATUS_INVALID_HANDLE );
  488. return -1;
  489. }
  490. old_access = entry->access;
  491. mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
  492. flags = (flags << RESERVED_SHIFT) & mask;
  493. entry->access = (entry->access & ~mask) | flags;
  494. return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
  495. }
  496. /* duplicate a handle */
  497. obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
  498. unsigned int access, unsigned int attr, unsigned int options )
  499. {
  500. obj_handle_t res;
  501. struct handle_entry *entry;
  502. unsigned int src_access, src_flags;
  503. struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
  504. if (!obj) return 0;
  505. if ((entry = get_handle( src, src_handle )))
  506. src_access = entry->access;
  507. else /* pseudo-handle, give it full access */
  508. src_access = obj->ops->map_access( obj, GENERIC_ALL );
  509. src_flags = (src_access & RESERVED_ALL) >> RESERVED_SHIFT;
  510. src_access &= ~RESERVED_ALL;
  511. if (options & DUPLICATE_SAME_ACCESS)
  512. access = src_access;
  513. else
  514. access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
  515. /* asking for the more access rights than src_access? */
  516. if (access & ~src_access)
  517. {
  518. if ((current->token && !check_object_access( current->token, obj, &access )) ||
  519. !check_object_access( dst->token, obj, &access ))
  520. {
  521. release_object( obj );
  522. return 0;
  523. }
  524. if (options & DUPLICATE_MAKE_GLOBAL)
  525. res = alloc_global_handle( obj, access );
  526. else
  527. res = alloc_handle_no_access_check( dst, obj, access, attr );
  528. }
  529. else
  530. {
  531. if (options & DUPLICATE_MAKE_GLOBAL)
  532. res = alloc_global_handle_no_access_check( obj, access );
  533. else if ((options & DUPLICATE_CLOSE_SOURCE) && src == dst &&
  534. entry && !(entry->access & RESERVED_CLOSE_PROTECT))
  535. {
  536. if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
  537. entry->access = access;
  538. res = src_handle;
  539. }
  540. else
  541. res = alloc_handle_entry( dst, obj, access, attr );
  542. }
  543. if (res && (options & DUPLICATE_SAME_ATTRIBUTES))
  544. set_handle_flags( dst, res, ~0u, src_flags );
  545. release_object( obj );
  546. return res;
  547. }
  548. /* open a new handle to an existing object */
  549. obj_handle_t open_object( struct process *process, obj_handle_t parent, unsigned int access,
  550. const struct object_ops *ops, const struct unicode_str *name,
  551. unsigned int attributes )
  552. {
  553. obj_handle_t handle = 0;
  554. struct object *obj, *root = NULL;
  555. if (name->len >= 65534)
  556. {
  557. set_error( STATUS_OBJECT_NAME_INVALID );
  558. return 0;
  559. }
  560. if (parent)
  561. {
  562. if (name->len)
  563. root = get_directory_obj( process, parent );
  564. else /* opening the object itself can work for non-directories too */
  565. root = get_handle_obj( process, parent, 0, NULL );
  566. if (!root) return 0;
  567. }
  568. if ((obj = open_named_object( root, ops, name, attributes )))
  569. {
  570. handle = alloc_handle( process, obj, access, attributes );
  571. release_object( obj );
  572. }
  573. if (root) release_object( root );
  574. return handle;
  575. }
  576. /* return the size of the handle table of a given process */
  577. unsigned int get_handle_table_count( struct process *process )
  578. {
  579. if (!process->handles) return 0;
  580. return process->handles->count;
  581. }
  582. /* close a handle */
  583. DECL_HANDLER(close_handle)
  584. {
  585. unsigned int err = close_handle( current->process, req->handle );
  586. set_error( err );
  587. }
  588. /* set a handle information */
  589. DECL_HANDLER(set_handle_info)
  590. {
  591. reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
  592. }
  593. /* duplicate a handle */
  594. DECL_HANDLER(dup_handle)
  595. {
  596. struct process *src, *dst = NULL;
  597. reply->handle = 0;
  598. if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
  599. {
  600. if (req->options & DUPLICATE_MAKE_GLOBAL)
  601. {
  602. reply->handle = duplicate_handle( src, req->src_handle, NULL,
  603. req->access, req->attributes, req->options );
  604. }
  605. else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
  606. {
  607. reply->handle = duplicate_handle( src, req->src_handle, dst,
  608. req->access, req->attributes, req->options );
  609. release_object( dst );
  610. }
  611. /* close the handle no matter what happened */
  612. if ((req->options & DUPLICATE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
  613. close_handle( src, req->src_handle );
  614. release_object( src );
  615. }
  616. }
  617. DECL_HANDLER(get_object_info)
  618. {
  619. struct object *obj;
  620. if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
  621. reply->access = get_handle_access( current->process, req->handle );
  622. reply->ref_count = obj->refcount;
  623. reply->handle_count = obj->handle_count;
  624. release_object( obj );
  625. }
  626. DECL_HANDLER(get_object_name)
  627. {
  628. struct object *obj;
  629. WCHAR *name;
  630. if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
  631. if ((name = obj->ops->get_full_name( obj, &reply->total )))
  632. set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
  633. release_object( obj );
  634. }
  635. DECL_HANDLER(set_security_object)
  636. {
  637. data_size_t sd_size = get_req_data_size();
  638. const struct security_descriptor *sd = get_req_data();
  639. struct object *obj;
  640. unsigned int access = 0;
  641. if (!sd_is_valid( sd, sd_size ))
  642. {
  643. set_error( STATUS_ACCESS_VIOLATION );
  644. return;
  645. }
  646. if (req->security_info & OWNER_SECURITY_INFORMATION ||
  647. req->security_info & GROUP_SECURITY_INFORMATION ||
  648. req->security_info & LABEL_SECURITY_INFORMATION)
  649. access |= WRITE_OWNER;
  650. if (req->security_info & SACL_SECURITY_INFORMATION)
  651. access |= ACCESS_SYSTEM_SECURITY;
  652. if (req->security_info & DACL_SECURITY_INFORMATION)
  653. access |= WRITE_DAC;
  654. if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
  655. obj->ops->set_sd( obj, sd, req->security_info );
  656. release_object( obj );
  657. }
  658. DECL_HANDLER(get_security_object)
  659. {
  660. const struct security_descriptor *sd;
  661. struct object *obj;
  662. unsigned int access = READ_CONTROL;
  663. struct security_descriptor req_sd;
  664. int present;
  665. const struct sid *owner, *group;
  666. const struct acl *sacl, *dacl;
  667. struct acl *label_acl = NULL;
  668. if (req->security_info & SACL_SECURITY_INFORMATION)
  669. access |= ACCESS_SYSTEM_SECURITY;
  670. if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
  671. sd = obj->ops->get_sd( obj );
  672. if (sd)
  673. {
  674. req_sd.control = sd->control & ~SE_SELF_RELATIVE;
  675. owner = sd_get_owner( sd );
  676. if (req->security_info & OWNER_SECURITY_INFORMATION)
  677. req_sd.owner_len = sd->owner_len;
  678. else
  679. req_sd.owner_len = 0;
  680. group = sd_get_group( sd );
  681. if (req->security_info & GROUP_SECURITY_INFORMATION)
  682. req_sd.group_len = sd->group_len;
  683. else
  684. req_sd.group_len = 0;
  685. sacl = sd_get_sacl( sd, &present );
  686. if (req->security_info & SACL_SECURITY_INFORMATION && present)
  687. req_sd.sacl_len = sd->sacl_len;
  688. else if (req->security_info & LABEL_SECURITY_INFORMATION && present && sacl)
  689. {
  690. if (!(label_acl = extract_security_labels( sacl ))) goto done;
  691. req_sd.sacl_len = label_acl->size;
  692. sacl = label_acl;
  693. }
  694. else
  695. req_sd.sacl_len = 0;
  696. dacl = sd_get_dacl( sd, &present );
  697. if (req->security_info & DACL_SECURITY_INFORMATION && present)
  698. req_sd.dacl_len = sd->dacl_len;
  699. else
  700. req_sd.dacl_len = 0;
  701. reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
  702. req_sd.sacl_len + req_sd.dacl_len;
  703. if (reply->sd_len <= get_reply_max_size())
  704. {
  705. char *ptr = set_reply_data_size(reply->sd_len);
  706. memcpy( ptr, &req_sd, sizeof(req_sd) );
  707. ptr += sizeof(req_sd);
  708. memcpy( ptr, owner, req_sd.owner_len );
  709. ptr += req_sd.owner_len;
  710. memcpy( ptr, group, req_sd.group_len );
  711. ptr += req_sd.group_len;
  712. memcpy( ptr, sacl, req_sd.sacl_len );
  713. ptr += req_sd.sacl_len;
  714. memcpy( ptr, dacl, req_sd.dacl_len );
  715. }
  716. else
  717. set_error(STATUS_BUFFER_TOO_SMALL);
  718. }
  719. done:
  720. release_object( obj );
  721. free( label_acl );
  722. }
  723. struct enum_handle_info
  724. {
  725. unsigned int count;
  726. struct handle_info *handle;
  727. };
  728. static int enum_handles( struct process *process, void *user )
  729. {
  730. struct enum_handle_info *info = user;
  731. struct handle_table *table = process->handles;
  732. struct handle_entry *entry;
  733. struct handle_info *handle;
  734. unsigned int i;
  735. if (!table)
  736. return 0;
  737. for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
  738. {
  739. if (!entry->ptr) continue;
  740. if (!info->handle)
  741. {
  742. info->count++;
  743. continue;
  744. }
  745. assert( info->count );
  746. handle = info->handle++;
  747. handle->owner = process->id;
  748. handle->handle = index_to_handle(i);
  749. handle->access = entry->access & ~RESERVED_ALL;
  750. handle->type = entry->ptr->ops->type->index;
  751. handle->attributes = 0;
  752. if (entry->access & RESERVED_INHERIT) handle->attributes |= OBJ_INHERIT;
  753. if (entry->access & RESERVED_CLOSE_PROTECT) handle->attributes |= OBJ_PROTECT_CLOSE;
  754. info->count--;
  755. }
  756. return 0;
  757. }
  758. DECL_HANDLER(get_system_handles)
  759. {
  760. struct enum_handle_info info;
  761. struct handle_info *handle;
  762. data_size_t max_handles = get_reply_max_size() / sizeof(*handle);
  763. info.handle = NULL;
  764. info.count = 0;
  765. enum_processes( enum_handles, &info );
  766. reply->count = info.count;
  767. if (max_handles < info.count)
  768. set_error( STATUS_BUFFER_TOO_SMALL );
  769. else if ((handle = set_reply_data_size( info.count * sizeof(*handle) )))
  770. {
  771. info.handle = handle;
  772. enum_processes( enum_handles, &info );
  773. }
  774. }
  775. DECL_HANDLER(set_object_permanence)
  776. {
  777. const unsigned int access = req->permanent ? 0 : DELETE;
  778. struct object *obj;
  779. if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
  780. if (req->permanent && !obj->is_permanent)
  781. {
  782. grab_object( obj );
  783. make_object_permanent( obj );
  784. }
  785. else if (!req->permanent && obj->is_permanent)
  786. {
  787. make_object_temporary( obj );
  788. release_object( obj );
  789. }
  790. release_object( obj );
  791. }
  792. DECL_HANDLER(compare_objects)
  793. {
  794. struct object *obj1, *obj2;
  795. if (!(obj1 = get_handle_obj( current->process, req->first, 0, NULL ))) return;
  796. if (!(obj2 = get_handle_obj( current->process, req->second, 0, NULL )))
  797. {
  798. release_object( obj1 );
  799. return;
  800. }
  801. if (obj1 != obj2) set_error( STATUS_NOT_SAME_OBJECT );
  802. release_object( obj2 );
  803. release_object( obj1 );
  804. }