server: Store the opening options in the file descriptor instead of in the individual...
[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     no_lookup_name,                  /* lookup_name */
115     no_open_file,                    /* open_file */
116     no_close_handle,                 /* close_handle */
117     handle_table_destroy             /* destroy */
118 };
119
120 /* dump a handle table */
121 static void handle_table_dump( struct object *obj, int verbose )
122 {
123     int i;
124     struct handle_table *table = (struct handle_table *)obj;
125     struct handle_entry *entry = table->entries;
126
127     assert( obj->ops == &handle_table_ops );
128
129     fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
130              table->last, table->count, table->process );
131     if (!verbose) return;
132     entry = table->entries;
133     for (i = 0; i <= table->last; i++, entry++)
134     {
135         if (!entry->ptr) continue;
136         fprintf( stderr, "    %p: %p %08x ",
137                  index_to_handle(i), entry->ptr, entry->access );
138         entry->ptr->ops->dump( entry->ptr, 0 );
139     }
140 }
141
142 /* destroy a handle table */
143 static void handle_table_destroy( struct object *obj )
144 {
145     int i;
146     struct handle_table *table = (struct handle_table *)obj;
147     struct handle_entry *entry;
148
149     assert( obj->ops == &handle_table_ops );
150
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
161     for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
162     {
163         struct object *obj = entry->ptr;
164         entry->ptr = NULL;
165         if (obj) release_object( obj );
166     }
167     free( table->entries );
168 }
169
170 /* allocate a new handle table */
171 struct handle_table *alloc_handle_table( struct process *process, int count )
172 {
173     struct handle_table *table;
174
175     if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
176     if (!(table = alloc_object( &handle_table_ops )))
177         return NULL;
178     table->process = process;
179     table->count   = count;
180     table->last    = -1;
181     table->free    = 0;
182     if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
183     release_object( table );
184     return NULL;
185 }
186
187 /* grow a handle table */
188 static int grow_handle_table( struct handle_table *table )
189 {
190     struct handle_entry *new_entries;
191     int count = table->count;
192
193     if (count >= INT_MAX / 2) return 0;
194     count *= 2;
195     if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
196     {
197         set_error( STATUS_NO_MEMORY );
198         return 0;
199     }
200     table->entries = new_entries;
201     table->count   = count;
202     return 1;
203 }
204
205 /* allocate the first free entry in the handle table */
206 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
207 {
208     struct handle_entry *entry = table->entries + table->free;
209     int i;
210
211     for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
212     if (i >= table->count)
213     {
214         if (!grow_handle_table( table )) return 0;
215         entry = table->entries + i;  /* the entries may have moved */
216     }
217     table->last = i;
218  found:
219     table->free = i + 1;
220     entry->ptr    = grab_object( obj );
221     entry->access = access;
222     return index_to_handle(i);
223 }
224
225 /* allocate a handle for an object, incrementing its refcount */
226 /* return the handle, or 0 on error */
227 static obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
228 {
229     struct object *obj = ptr;
230
231     access &= ~RESERVED_ALL;
232     if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
233     if (!process->handles)
234     {
235         set_error( STATUS_NO_MEMORY );
236         return 0;
237     }
238     return alloc_entry( process->handles, obj, access );
239 }
240
241 /* allocate a handle for an object, checking the dacl allows the process to */
242 /* access it and incrementing its refcount */
243 /* return the handle, or 0 on error */
244 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
245 {
246     struct object *obj = ptr;
247     access = obj->ops->map_access( obj, access );
248     if (access && !check_object_access( obj, &access )) return 0;
249     return alloc_handle_no_access_check( process, ptr, access, attr );
250 }
251
252 /* allocate a global handle for an object, incrementing its refcount */
253 /* return the handle, or 0 on error */
254 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
255 {
256     if (!global_table)
257     {
258         if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
259             return 0;
260         make_object_static( &global_table->obj );
261     }
262     return handle_local_to_global( alloc_entry( global_table, obj, access ));
263 }
264
265 /* allocate a global handle for an object, checking the dacl allows the */
266 /* process to access it and incrementing its refcount and incrementing its refcount */
267 /* return the handle, or 0 on error */
268 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
269 {
270     if (access && !check_object_access( obj, &access )) return 0;
271     return alloc_global_handle_no_access_check( obj, access );
272 }
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
281     if (handle_is_global(handle))
282     {
283         handle = handle_global_to_local(handle);
284         table = global_table;
285     }
286     if (!table) goto error;
287     index = handle_to_index( handle );
288     if (index < 0) goto error;
289     if (index > table->last) goto error;
290     entry = table->entries + index;
291     if (!entry->ptr) goto error;
292     return entry;
293
294  error:
295     set_error( STATUS_INVALID_HANDLE );
296     return NULL;
297 }
298
299 /* attempt to shrink a table */
300 static void shrink_handle_table( struct handle_table *table )
301 {
302     struct handle_entry *entry = table->entries + table->last;
303     struct handle_entry *new_entries;
304     int count = table->count;
305
306     while (table->last >= 0)
307     {
308         if (entry->ptr) break;
309         table->last--;
310         entry--;
311     }
312     if (table->last >= count / 4) return;  /* no need to shrink */
313     if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
314     count /= 2;
315     if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
316     table->count   = count;
317     table->entries = new_entries;
318 }
319
320 /* copy the handle table of the parent process */
321 /* return 1 if OK, 0 on error */
322 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
323 {
324     struct handle_table *parent_table = parent->handles;
325     struct handle_table *table;
326     int i;
327
328     assert( parent_table );
329     assert( parent_table->obj.ops == &handle_table_ops );
330
331     if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
332         return NULL;
333
334     if ((table->last = parent_table->last) >= 0)
335     {
336         struct handle_entry *ptr = table->entries;
337         memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
338         for (i = 0; i <= table->last; i++, ptr++)
339         {
340             if (!ptr->ptr) continue;
341             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
342             else ptr->ptr = NULL; /* don't inherit this entry */
343         }
344     }
345     /* attempt to shrink the table */
346     shrink_handle_table( table );
347     return table;
348 }
349
350 /* close a handle and decrement the refcount of the associated object */
351 /* return 1 if OK, 0 on error */
352 int close_handle( struct process *process, obj_handle_t handle )
353 {
354     struct handle_table *table;
355     struct handle_entry *entry;
356     struct object *obj;
357
358     if (!(entry = get_handle( process, handle ))) return 0;
359     if (entry->access & RESERVED_CLOSE_PROTECT)
360     {
361         set_error( STATUS_HANDLE_NOT_CLOSABLE );
362         return 0;
363     }
364     obj = entry->ptr;
365     if (!obj->ops->close_handle( obj, process, handle ))
366     {
367         set_error( STATUS_HANDLE_NOT_CLOSABLE );
368         return 0;
369     }
370     entry->ptr = NULL;
371     table = handle_is_global(handle) ? global_table : process->handles;
372     if (entry < table->entries + table->free) table->free = entry - table->entries;
373     if (entry == table->entries + table->last) shrink_handle_table( table );
374     release_object( obj );
375     return 1;
376 }
377
378 /* retrieve the object corresponding to one of the magic pseudo-handles */
379 static inline struct object *get_magic_handle( obj_handle_t handle )
380 {
381     switch((unsigned long)handle)
382     {
383         case 0xfffffffe:  /* current thread pseudo-handle */
384             return &current->obj;
385         case 0x7fffffff:  /* current process pseudo-handle */
386         case 0xffffffff:  /* current process pseudo-handle */
387             return (struct object *)current->process;
388         default:
389             return NULL;
390     }
391 }
392
393 /* retrieve the object corresponding to a handle, incrementing its refcount */
394 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
395                                unsigned int access, const struct object_ops *ops )
396 {
397     struct handle_entry *entry;
398     struct object *obj;
399
400     if (!(obj = get_magic_handle( handle )))
401     {
402         if (!(entry = get_handle( process, handle ))) return NULL;
403         if ((entry->access & access) != access)
404         {
405             set_error( STATUS_ACCESS_DENIED );
406             return NULL;
407         }
408         obj = entry->ptr;
409     }
410     if (ops && (obj->ops != ops))
411     {
412         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
413         return NULL;
414     }
415     return grab_object( obj );
416 }
417
418 /* retrieve the access rights of a given handle */
419 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
420 {
421     struct handle_entry *entry;
422
423     if (get_magic_handle( handle )) return ~RESERVED_ALL;  /* magic handles have all access rights */
424     if (!(entry = get_handle( process, handle ))) return 0;
425     return entry->access & ~RESERVED_ALL;
426 }
427
428 /* find the first inherited handle of the given type */
429 /* this is needed for window stations and desktops (don't ask...) */
430 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
431 {
432     struct handle_table *table = process->handles;
433     struct handle_entry *ptr;
434     int i;
435
436     if (!table) return 0;
437
438     for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
439     {
440         if (!ptr->ptr) continue;
441         if (ptr->ptr->ops != ops) continue;
442         if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
443     }
444     return 0;
445 }
446
447 /* get/set the handle reserved flags */
448 /* return the old flags (or -1 on error) */
449 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
450 {
451     struct handle_entry *entry;
452     unsigned int old_access;
453
454     if (get_magic_handle( handle ))
455     {
456         /* we can retrieve but not set info for magic handles */
457         if (mask) set_error( STATUS_ACCESS_DENIED );
458         return 0;
459     }
460     if (!(entry = get_handle( process, handle ))) return -1;
461     old_access = entry->access;
462     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
463     flags = (flags << RESERVED_SHIFT) & mask;
464     entry->access = (entry->access & ~mask) | flags;
465     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
466 }
467
468 /* duplicate a handle */
469 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
470                                unsigned int access, unsigned int attr, unsigned int options )
471 {
472     obj_handle_t res;
473     struct handle_entry *entry;
474     unsigned int src_access;
475     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
476
477     if (!obj) return 0;
478     if ((entry = get_handle( src, src_handle )))
479         src_access = entry->access;
480     else  /* pseudo-handle, give it full access */
481     {
482         src_access = obj->ops->map_access( obj, GENERIC_ALL );
483         clear_error();
484     }
485     src_access &= ~RESERVED_ALL;
486
487     if (options & DUP_HANDLE_SAME_ACCESS)
488         access = src_access;
489     else
490         access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
491
492     /* asking for the more access rights than src_access? */
493     if (access & ~src_access)
494     {
495         if (options & DUP_HANDLE_MAKE_GLOBAL)
496             res = alloc_global_handle( obj, access );
497         else
498             res = alloc_handle( dst, obj, access, attr );
499     }
500     else
501     {
502         if (options & DUP_HANDLE_MAKE_GLOBAL)
503             res = alloc_global_handle_no_access_check( obj, access );
504         else
505             res = alloc_handle_no_access_check( dst, obj, access, attr );
506     }
507
508     release_object( obj );
509     return res;
510 }
511
512 /* open a new handle to an existing object */
513 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
514                           const struct object_ops *ops, unsigned int access, unsigned int attr )
515 {
516     obj_handle_t handle = 0;
517     struct object *obj = find_object( namespace, name, attr );
518     if (obj)
519     {
520         if (ops && obj->ops != ops)
521             set_error( STATUS_OBJECT_TYPE_MISMATCH );
522         else
523             handle = alloc_handle( current->process, obj, access, attr );
524         release_object( obj );
525     }
526     else
527         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
528     return handle;
529 }
530
531 /* return the size of the handle table of a given process */
532 unsigned int get_handle_table_count( struct process *process )
533 {
534     if (!process->handles) return 0;
535     return process->handles->count;
536 }
537
538 /* close a handle */
539 DECL_HANDLER(close_handle)
540 {
541     close_handle( current->process, req->handle );
542 }
543
544 /* set a handle information */
545 DECL_HANDLER(set_handle_info)
546 {
547     reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
548 }
549
550 /* duplicate a handle */
551 DECL_HANDLER(dup_handle)
552 {
553     struct process *src, *dst;
554
555     reply->handle = 0;
556     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
557     {
558         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
559         {
560             reply->handle = duplicate_handle( src, req->src_handle, NULL,
561                                               req->access, req->attributes, req->options );
562         }
563         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
564         {
565             reply->handle = duplicate_handle( src, req->src_handle, dst,
566                                               req->access, req->attributes, req->options );
567             release_object( dst );
568         }
569         /* close the handle no matter what happened */
570         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
571         {
572             unsigned int err = get_error();  /* don't overwrite error from the above calls */
573             reply->closed = close_handle( src, req->src_handle );
574             set_error( err );
575         }
576         reply->self = (src == current->process);
577         release_object( src );
578     }
579 }
580
581 DECL_HANDLER(get_object_info)
582 {
583     struct object *obj;
584
585     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
586
587     reply->access = get_handle_access( current->process, req->handle );
588     reply->ref_count = obj->refcount;
589     release_object( obj );
590 }