handle.c 30 KB

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