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
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "security.h"
40 #include "request.h"
41
42 struct handle_entry
43 {
44 struct object *ptr; /* object */
45 unsigned int access; /* access rights */
46 };
47
48 struct handle_table
49 {
50 struct object obj; /* object header */
51 struct process *process; /* process owning this table */
52 int count; /* number of allocated entries */
53 int last; /* last used entry */
54 int free; /* first entry that may be free */
55 struct handle_entry *entries; /* handle entries */
56 };
57
58 static struct handle_table *global_table;
59
60 /* reserved handle access rights */
61 #define RESERVED_SHIFT 26
62 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
63 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
64 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
65
66 #define MIN_HANDLE_ENTRIES 32
67 #define MAX_HANDLE_ENTRIES 0x00ffffff
68
69
70 /* handle to table index conversion */
71
72 /* handles are a multiple of 4 under NT; handle 0 is not used */
73 static inline obj_handle_t index_to_handle( int index )
74 {
75 return (obj_handle_t)((index + 1) << 2);
76 }
77 static inline int handle_to_index( obj_handle_t handle )
78 {
79 return (handle >> 2) - 1;
80 }
81
82 /* global handle conversion */
83
84 #define HANDLE_OBFUSCATOR 0x544a4def
85
86 static inline int handle_is_global( obj_handle_t handle)
87 {
88 return (handle ^ HANDLE_OBFUSCATOR) <= (MAX_HANDLE_ENTRIES << 2);
89 }
90 static inline obj_handle_t handle_local_to_global( obj_handle_t handle )
91 {
92 if (!handle) return 0;
93 return handle ^ HANDLE_OBFUSCATOR;
94 }
95 static inline obj_handle_t handle_global_to_local( obj_handle_t handle )
96 {
97 return handle ^ HANDLE_OBFUSCATOR;
98 }
99
100
101 static void handle_table_dump( struct object *obj, int verbose );
102 static void handle_table_destroy( struct object *obj );
103
104 static const struct object_ops handle_table_ops =
105 {
106 sizeof(struct handle_table), /* size */
107 handle_table_dump, /* dump */
108 no_get_type, /* get_type */
109 no_add_queue, /* add_queue */
110 NULL, /* remove_queue */
111 NULL, /* signaled */
112 NULL, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 no_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 handle_table_destroy /* destroy */
122 };
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
131 assert( obj->ops == &handle_table_ops );
132
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 entry->ptr->ops->dump( entry->ptr, 0 );
143 }
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
153 assert( obj->ops == &handle_table_ops );
154
155 /* first notify all objects that handles are being closed */
156 if (table->process)
157 {
158 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
159 {
160 struct object *obj = entry->ptr;
161 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
162 }
163 }
164
165 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
166 {
167 struct object *obj = entry->ptr;
168 entry->ptr = NULL;
169 if (obj) release_object( obj );
170 }
171 free( table->entries );
172 }
173
174 /* close all the process handles and free the handle table */
175 void close_process_handles( struct process *process )
176 {
177 struct handle_table *table = process->handles;
178
179 process->handles = NULL;
180 if (table) release_object( table );
181 }
182
183 /* allocate a new handle table */
184 struct handle_table *alloc_handle_table( struct process *process, int count )
185 {
186 struct handle_table *table;
187
188 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
189 if (!(table = alloc_object( &handle_table_ops )))
190 return NULL;
191 table->process = process;
192 table->count = count;
193 table->last = -1;
194 table->free = 0;
195 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
196 release_object( table );
197 return NULL;
198 }
199
200 /* grow a handle table */
201 static int grow_handle_table( struct handle_table *table )
202 {
203 struct handle_entry *new_entries;
204 int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
205
206 if (count == table->count ||
207 !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
208 {
209 set_error( STATUS_INSUFFICIENT_RESOURCES );
210 return 0;
211 }
212 table->entries = new_entries;
213 table->count = count;
214 return 1;
215 }
216
217 /* allocate the first free entry in the handle table */
218 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
219 {
220 struct handle_entry *entry = table->entries + table->free;
221 int i;
222
223 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
224 if (i >= table->count)
225 {
226 if (!grow_handle_table( table )) return 0;
227 entry = table->entries + i; /* the entries may have moved */
228 }
229 table->last = i;
230 found:
231 table->free = i + 1;
232 entry->ptr = grab_object( obj );
233 entry->access = access;
234 return index_to_handle(i);
235 }
236
237 /* allocate a handle for an object, incrementing its refcount */
238 /* return the handle, or 0 on error */
239 obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
240 {
241 struct object *obj = ptr;
242
243 access &= ~RESERVED_ALL;
244 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
245 if (!process->handles)
246 {
247 set_error( STATUS_NO_MEMORY );
248 return 0;
249 }
250 return alloc_entry( process->handles, obj, access );
251 }
252
253 /* allocate a handle for an object, checking the dacl allows the process to */
254 /* access it and incrementing its refcount */
255 /* return the handle, or 0 on error */
256 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
257 {
258 struct object *obj = ptr;
259 access = obj->ops->map_access( obj, access );
260 if (access && !check_object_access( obj, &access )) return 0;
261 return alloc_handle_no_access_check( process, ptr, access, attr );
262 }
263
264 /* allocate a global handle for an object, incrementing its refcount */
265 /* return the handle, or 0 on error */
266 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
267 {
268 if (!global_table)
269 {
270 if (!(global_table = alloc_handle_table( NULL, 0 )))
271 return 0;
272 make_object_static( &global_table->obj );
273 }
274 return handle_local_to_global( alloc_entry( global_table, obj, access ));
275 }
276
277 /* allocate a global handle for an object, checking the dacl allows the */
278 /* process to access it and incrementing its refcount and incrementing its refcount */
279 /* return the handle, or 0 on error */
280 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
281 {
282 if (access && !check_object_access( obj, &access )) return 0;
283 return alloc_global_handle_no_access_check( obj, access );
284 }
285
286 /* return a handle entry, or NULL if the handle is invalid */
287 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
288 {
289 struct handle_table *table = process->handles;
290 struct handle_entry *entry;
291 int index;
292
293 if (handle_is_global(handle))
294 {
295 handle = handle_global_to_local(handle);
296 table = global_table;
297 }
298 if (!table) return NULL;
299 index = handle_to_index( handle );
300 if (index < 0) return NULL;
301 if (index > table->last) return NULL;
302 entry = table->entries + index;
303 if (!entry->ptr) return NULL;
304 return entry;
305 }
306
307 /* attempt to shrink a table */
308 static void shrink_handle_table( struct handle_table *table )
309 {
310 struct handle_entry *entry = table->entries + table->last;
311 struct handle_entry *new_entries;
312 int count = table->count;
313
314 while (table->last >= 0)
315 {
316 if (entry->ptr) break;
317 table->last--;
318 entry--;
319 }
320 if (table->last >= count / 4) return; /* no need to shrink */
321 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
322 count /= 2;
323 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
324 table->count = count;
325 table->entries = new_entries;
326 }
327
328 /* copy the handle table of the parent process */
329 /* return 1 if OK, 0 on error */
330 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
331 {
332 struct handle_table *parent_table = parent->handles;
333 struct handle_table *table;
334 int i;
335
336 assert( parent_table );
337 assert( parent_table->obj.ops == &handle_table_ops );
338
339 if (!(table = alloc_handle_table( process, parent_table->count )))
340 return NULL;
341
342 if ((table->last = parent_table->last) >= 0)
343 {
344 struct handle_entry *ptr = table->entries;
345 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
346 for (i = 0; i <= table->last; i++, ptr++)
347 {
348 if (!ptr->ptr) continue;
349 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
350 else ptr->ptr = NULL; /* don't inherit this entry */
351 }
352 }
353 /* attempt to shrink the table */
354 shrink_handle_table( table );
355 return table;
356 }
357
358 /* close a handle and decrement the refcount of the associated object */
359 unsigned int close_handle( struct process *process, obj_handle_t handle )
360 {
361 struct handle_table *table;
362 struct handle_entry *entry;
363 struct object *obj;
364
365 if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
366 if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
367 obj = entry->ptr;
368 if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
369 entry->ptr = NULL;
370 table = handle_is_global(handle) ? global_table : process->handles;
371 if (entry < table->entries + table->free) table->free = entry - table->entries;
372 if (entry == table->entries + table->last) shrink_handle_table( table );
373 release_object( obj );
374 return STATUS_SUCCESS;
375 }
376
377 /* retrieve the object corresponding to one of the magic pseudo-handles */
378 static inline struct object *get_magic_handle( obj_handle_t handle )
379 {
380 switch(handle)
381 {
382 case 0xfffffffe: /* current thread pseudo-handle */
383 return ¤t->obj;
384 case 0x7fffffff: /* current process pseudo-handle */
385 case 0xffffffff: /* current process pseudo-handle */
386 return (struct object *)current->process;
387 default:
388 return NULL;
389 }
390 }
391
392 /* retrieve the object corresponding to a handle, incrementing its refcount */
393 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
394 unsigned int access, const struct object_ops *ops )
395 {
396 struct handle_entry *entry;
397 struct object *obj;
398
399 if (!(obj = get_magic_handle( handle )))
400 {
401 if (!(entry = get_handle( process, handle )))
402 {
403 set_error( STATUS_INVALID_HANDLE );
404 return NULL;
405 }
406 obj = entry->ptr;
407 if (ops && (obj->ops != ops))
408 {
409 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
410 return NULL;
411 }
412 if ((entry->access & access) != access)
413 {
414 set_error( STATUS_ACCESS_DENIED );
415 return NULL;
416 }
417 }
418 else if (ops && (obj->ops != ops))
419 {
420 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
421 return NULL;
422 }
423 return grab_object( obj );
424 }
425
426 /* retrieve the access rights of a given handle */
427 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
428 {
429 struct handle_entry *entry;
430
431 if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
432 if (!(entry = get_handle( process, handle ))) return 0;
433 return entry->access & ~RESERVED_ALL;
434 }
435
436 /* find the first inherited handle of the given type */
437 /* this is needed for window stations and desktops (don't ask...) */
438 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
439 {
440 struct handle_table *table = process->handles;
441 struct handle_entry *ptr;
442 int i;
443
444 if (!table) return 0;
445
446 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
447 {
448 if (!ptr->ptr) continue;
449 if (ptr->ptr->ops != ops) continue;
450 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
451 }
452 return 0;
453 }
454
455 /* enumerate handles of a given type */
456 /* this is needed for window stations and desktops */
457 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
458 unsigned int *index )
459 {
460 struct handle_table *table = process->handles;
461 unsigned int i;
462 struct handle_entry *entry;
463
464 if (!table) return 0;
465
466 for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
467 {
468 if (!entry->ptr) continue;
469 if (entry->ptr->ops != ops) continue;
470 *index = i + 1;
471 return index_to_handle(i);
472 }
473 return 0;
474 }
475
476 /* get/set the handle reserved flags */
477 /* return the old flags (or -1 on error) */
478 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
479 {
480 struct handle_entry *entry;
481 unsigned int old_access;
482
483 if (get_magic_handle( handle ))
484 {
485 /* we can retrieve but not set info for magic handles */
486 if (mask) set_error( STATUS_ACCESS_DENIED );
487 return 0;
488 }
489 if (!(entry = get_handle( process, handle )))
490 {
491 set_error( STATUS_INVALID_HANDLE );
492 return -1;
493 }
494 old_access = entry->access;
495 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
496 flags = (flags << RESERVED_SHIFT) & mask;
497 entry->access = (entry->access & ~mask) | flags;
498 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
499 }
500
501 /* duplicate a handle */
502 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
503 unsigned int access, unsigned int attr, unsigned int options )
504 {
505 obj_handle_t res;
506 struct handle_entry *entry;
507 unsigned int src_access;
508 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
509
510 if (!obj) return 0;
511 if ((entry = get_handle( src, src_handle )))
512 src_access = entry->access;
513 else /* pseudo-handle, give it full access */
514 src_access = obj->ops->map_access( obj, GENERIC_ALL );
515 src_access &= ~RESERVED_ALL;
516
517 if (options & DUP_HANDLE_SAME_ACCESS)
518 access = src_access;
519 else
520 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
521
522 /* asking for the more access rights than src_access? */
523 if (access & ~src_access)
524 {
525 if (options & DUP_HANDLE_MAKE_GLOBAL)
526 res = alloc_global_handle( obj, access );
527 else
528 res = alloc_handle( dst, obj, access, attr );
529 }
530 else
531 {
532 if (options & DUP_HANDLE_MAKE_GLOBAL)
533 res = alloc_global_handle_no_access_check( obj, access );
534 else
535 res = alloc_handle_no_access_check( dst, obj, access, attr );
536 }
537
538 release_object( obj );
539 return res;
540 }
541
542 /* open a new handle to an existing object */
543 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
544 const struct object_ops *ops, unsigned int access, unsigned int attr )
545 {
546 obj_handle_t handle = 0;
547 struct object *obj = find_object( namespace, name, attr );
548 if (obj)
549 {
550 if (ops && obj->ops != ops)
551 set_error( STATUS_OBJECT_TYPE_MISMATCH );
552 else
553 handle = alloc_handle( current->process, obj, access, attr );
554 release_object( obj );
555 }
556 else
557 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
558 return handle;
559 }
560
561 /* return the size of the handle table of a given process */
562 unsigned int get_handle_table_count( struct process *process )
563 {
564 if (!process->handles) return 0;
565 return process->handles->count;
566 }
567
568 /* close a handle */
569 DECL_HANDLER(close_handle)
570 {
571 unsigned int err = close_handle( current->process, req->handle );
572 set_error( err );
573 }
574
575 /* set a handle information */
576 DECL_HANDLER(set_handle_info)
577 {
578 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
579 }
580
581 /* duplicate a handle */
582 DECL_HANDLER(dup_handle)
583 {
584 struct process *src, *dst;
585
586 reply->handle = 0;
587 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
588 {
589 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
590 {
591 reply->handle = duplicate_handle( src, req->src_handle, NULL,
592 req->access, req->attributes, req->options );
593 }
594 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
595 {
596 reply->handle = duplicate_handle( src, req->src_handle, dst,
597 req->access, req->attributes, req->options );
598 release_object( dst );
599 }
600 /* close the handle no matter what happened */
601 if (req->options & DUP_HANDLE_CLOSE_SOURCE) reply->closed = !close_handle( src, req->src_handle );
602 reply->self = (src == current->process);
603 release_object( src );
604 }
605 }
606
607 DECL_HANDLER(get_object_info)
608 {
609 struct object *obj;
610 WCHAR *name;
611
612 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
613
614 reply->access = get_handle_access( current->process, req->handle );
615 reply->ref_count = obj->refcount;
616 if ((name = get_object_full_name( obj, &reply->total )))
617 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
618 release_object( obj );
619 }
620
621 DECL_HANDLER(set_security_object)
622 {
623 data_size_t sd_size = get_req_data_size();
624 const struct security_descriptor *sd = get_req_data();
625 struct object *obj;
626 unsigned int access = 0;
627
628 if (!sd_is_valid( sd, sd_size ))
629 {
630 set_error( STATUS_ACCESS_VIOLATION );
631 return;
632 }
633
634 if (req->security_info & OWNER_SECURITY_INFORMATION ||
635 req->security_info & GROUP_SECURITY_INFORMATION)
636 access |= WRITE_OWNER;
637 if (req->security_info & SACL_SECURITY_INFORMATION)
638 access |= ACCESS_SYSTEM_SECURITY;
639 if (req->security_info & DACL_SECURITY_INFORMATION)
640 access |= WRITE_DAC;
641
642 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
643
644 obj->ops->set_sd( obj, sd, req->security_info );
645 release_object( obj );
646 }
647
648 DECL_HANDLER(get_security_object)
649 {
650 const struct security_descriptor *sd;
651 struct object *obj;
652 unsigned int access = READ_CONTROL;
653 struct security_descriptor req_sd;
654 int present;
655 const SID *owner, *group;
656 const ACL *sacl, *dacl;
657
658 if (req->security_info & SACL_SECURITY_INFORMATION)
659 access |= ACCESS_SYSTEM_SECURITY;
660
661 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
662
663 sd = obj->ops->get_sd( obj );
664 if (sd)
665 {
666 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
667
668 owner = sd_get_owner( sd );
669 if (req->security_info & OWNER_SECURITY_INFORMATION)
670 req_sd.owner_len = sd->owner_len;
671 else
672 req_sd.owner_len = 0;
673
674 group = sd_get_group( sd );
675 if (req->security_info & GROUP_SECURITY_INFORMATION)
676 req_sd.group_len = sd->group_len;
677 else
678 req_sd.group_len = 0;
679
680 req_sd.control |= SE_SACL_PRESENT;
681 sacl = sd_get_sacl( sd, &present );
682 if (req->security_info & SACL_SECURITY_INFORMATION && present)
683 req_sd.sacl_len = sd->sacl_len;
684 else
685 req_sd.sacl_len = 0;
686
687 req_sd.control |= SE_DACL_PRESENT;
688 dacl = sd_get_dacl( sd, &present );
689 if (req->security_info & DACL_SECURITY_INFORMATION && present)
690 req_sd.dacl_len = sd->dacl_len;
691 else
692 req_sd.dacl_len = 0;
693
694 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
695 req_sd.sacl_len + req_sd.dacl_len;
696 if (reply->sd_len <= get_reply_max_size())
697 {
698 char *ptr = set_reply_data_size(reply->sd_len);
699
700 memcpy( ptr, &req_sd, sizeof(req_sd) );
701 ptr += sizeof(req_sd);
702 memcpy( ptr, owner, req_sd.owner_len );
703 ptr += req_sd.owner_len;
704 memcpy( ptr, group, req_sd.group_len );
705 ptr += req_sd.group_len;
706 memcpy( ptr, sacl, req_sd.sacl_len );
707 ptr += req_sd.sacl_len;
708 memcpy( ptr, dacl, req_sd.dacl_len );
709 }
710 else
711 set_error(STATUS_BUFFER_TOO_SMALL);
712 }
713
714 release_object( obj );
715 }
716
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.