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