4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
30 #ifdef HAVE_VALGRIND_MEMCHECK_H
31 #include <valgrind/memcheck.h>
35 #define WIN32_NO_STATUS
47 unsigned int hash_size
; /* size of hash table */
48 struct list names
[1]; /* array of hash entry lists */
52 struct type_descr no_type
=
54 { NULL
, 0 }, /* name */
55 STANDARD_RIGHTS_REQUIRED
, /* valid_access */
58 STANDARD_RIGHTS_WRITE
,
59 STANDARD_RIGHTS_EXECUTE
,
60 STANDARD_RIGHTS_REQUIRED
65 static struct list object_list
= LIST_INIT(object_list
);
67 void dump_objects(void)
71 LIST_FOR_EACH_ENTRY( ptr
, &object_list
, struct object
, obj_list
)
73 fprintf( stderr
, "%p:%d: ", ptr
, ptr
->refcount
);
74 dump_object_name( ptr
);
75 ptr
->ops
->dump( ptr
, 1 );
79 void close_objects(void)
81 /* release the permanent objects */
87 LIST_FOR_EACH_ENTRY( obj
, &object_list
, struct object
, obj_list
)
89 if (!(found
= obj
->is_permanent
)) continue;
90 obj
->is_permanent
= 0;
91 release_object( obj
);
97 dump_objects(); /* dump any remaining objects */
100 #endif /* DEBUG_OBJECTS */
102 /*****************************************************************/
104 /* mark a block of memory as uninitialized for debugging purposes */
105 static inline void mark_block_uninitialized( void *ptr
, size_t size
)
107 memset( ptr
, 0x55, size
);
108 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
109 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
110 #elif defined(VALGRIND_MAKE_WRITABLE)
111 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
115 /* malloc replacement */
116 void *mem_alloc( size_t size
)
118 void *ptr
= malloc( size
);
119 if (ptr
) mark_block_uninitialized( ptr
, size
);
120 else set_error( STATUS_NO_MEMORY
);
124 /* duplicate a block of memory */
125 void *memdup( const void *data
, size_t len
)
127 void *ptr
= malloc( len
);
128 if (ptr
) memcpy( ptr
, data
, len
);
129 else set_error( STATUS_NO_MEMORY
);
134 /*****************************************************************/
136 void namespace_add( struct namespace *namespace, struct object_name
*ptr
)
138 unsigned int hash
= hash_strW( ptr
->name
, ptr
->len
, namespace->hash_size
);
140 list_add_head( &namespace->names
[hash
], &ptr
->entry
);
143 /* allocate a name for an object */
144 static struct object_name
*alloc_name( const struct unicode_str
*name
)
146 struct object_name
*ptr
;
148 if ((ptr
= mem_alloc( sizeof(*ptr
) + name
->len
- sizeof(ptr
->name
) )))
150 ptr
->len
= name
->len
;
152 memcpy( ptr
->name
, name
->str
, name
->len
);
157 /* get the name of an existing object */
158 const WCHAR
*get_object_name( struct object
*obj
, data_size_t
*len
)
160 struct object_name
*ptr
= obj
->name
;
161 if (!ptr
) return NULL
;
166 /* get the full path name of an existing object */
167 WCHAR
*default_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
169 static const WCHAR backslash
= '\\';
170 struct object
*ptr
= obj
;
174 while (ptr
&& ptr
->name
)
176 struct object_name
*name
= ptr
->name
;
177 len
+= name
->len
+ sizeof(WCHAR
);
180 if (!len
) return NULL
;
181 if (!(ret
= malloc( len
))) return NULL
;
184 while (obj
&& obj
->name
)
186 struct object_name
*name
= obj
->name
;
187 memcpy( ret
+ len
- name
->len
, name
->name
, name
->len
);
188 len
-= name
->len
+ sizeof(WCHAR
);
189 memcpy( ret
+ len
, &backslash
, sizeof(WCHAR
) );
195 /* allocate and initialize an object */
196 void *alloc_object( const struct object_ops
*ops
)
198 struct object
*obj
= mem_alloc( ops
->size
);
202 obj
->handle_count
= 0;
203 obj
->is_permanent
= 0;
207 list_init( &obj
->wait_queue
);
209 list_add_head( &object_list
, &obj
->obj_list
);
211 obj
->ops
->type
->obj_count
++;
212 obj
->ops
->type
->obj_max
= max( obj
->ops
->type
->obj_max
, obj
->ops
->type
->obj_count
);
218 /* free an object once it has been destroyed */
219 static void free_object( struct object
*obj
)
222 obj
->ops
->type
->obj_count
--;
224 list_remove( &obj
->obj_list
);
225 memset( obj
, 0xaa, obj
->ops
->size
);
230 /* find an object by name starting from the specified root */
231 /* if it doesn't exist, its parent is returned, and name_left contains the remaining name */
232 struct object
*lookup_named_object( struct object
*root
, const struct unicode_str
*name
,
233 unsigned int attr
, struct unicode_str
*name_left
)
235 struct object
*obj
, *parent
;
236 struct unicode_str name_tmp
= *name
, *ptr
= &name_tmp
;
240 /* if root is specified path shouldn't start with backslash */
241 if (name_tmp
.len
&& name_tmp
.str
[0] == '\\')
243 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
246 parent
= grab_object( root
);
250 if (!name_tmp
.len
|| name_tmp
.str
[0] != '\\')
252 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
255 /* skip leading backslash */
257 name_tmp
.len
-= sizeof(WCHAR
);
258 parent
= root
= get_root_directory();
261 if (!name_tmp
.len
) ptr
= NULL
; /* special case for empty path */
265 while ((obj
= parent
->ops
->lookup_name( parent
, ptr
, attr
, root
)))
267 /* move to the next element */
268 release_object ( parent
);
273 release_object( parent
);
277 if (name_left
) *name_left
= name_tmp
;
281 /* return length of first path element in name */
282 data_size_t
get_path_element( const WCHAR
*name
, data_size_t len
)
286 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++) if (name
[i
] == '\\') break;
287 return i
* sizeof(WCHAR
);
290 static struct object
*create_object( struct object
*parent
, const struct object_ops
*ops
,
291 const struct unicode_str
*name
, unsigned int attributes
,
292 const struct security_descriptor
*sd
)
295 struct object_name
*name_ptr
;
297 if (!(name_ptr
= alloc_name( name
))) return NULL
;
298 if (!(obj
= alloc_object( ops
))) goto failed
;
299 if (sd
&& !default_set_sd( obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
300 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
302 if (!obj
->ops
->link_name( obj
, name_ptr
, parent
)) goto failed
;
305 obj
->name
= name_ptr
;
309 if (obj
) free_object( obj
);
314 /* create an object as named child under the specified parent */
315 void *create_named_object( struct object
*parent
, const struct object_ops
*ops
,
316 const struct unicode_str
*name
, unsigned int attributes
,
317 const struct security_descriptor
*sd
)
319 struct object
*obj
, *new_obj
;
320 struct unicode_str new_name
;
324 if (!name
|| !name
->len
)
326 if (!(new_obj
= alloc_object( ops
))) return NULL
;
327 if (sd
&& !default_set_sd( new_obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
328 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
330 free_object( new_obj
);
336 if (!(obj
= lookup_named_object( parent
, name
, attributes
, &new_name
))) return NULL
;
340 if (attributes
& OBJ_OPENIF
&& obj
->ops
== ops
)
341 set_error( STATUS_OBJECT_NAME_EXISTS
);
344 release_object( obj
);
346 if (attributes
& OBJ_OPENIF
)
347 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
349 set_error( STATUS_OBJECT_NAME_COLLISION
);
354 new_obj
= create_object( obj
, ops
, &new_name
, attributes
, sd
);
355 release_object( obj
);
358 if (attributes
& OBJ_PERMANENT
)
360 make_object_permanent( new_obj
);
361 grab_object( new_obj
);
366 /* open a object by name under the specified parent */
367 void *open_named_object( struct object
*parent
, const struct object_ops
*ops
,
368 const struct unicode_str
*name
, unsigned int attributes
)
370 struct unicode_str name_left
;
373 if ((obj
= lookup_named_object( parent
, name
, attributes
, &name_left
)))
375 if (name_left
.len
) /* not fully parsed */
376 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
377 else if (ops
&& obj
->ops
!= ops
)
378 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
382 release_object( obj
);
387 /* recursive helper for dump_object_name */
388 static void dump_name( struct object
*obj
)
390 struct object_name
*name
= obj
->name
;
393 if (name
->parent
) dump_name( name
->parent
);
394 fputs( "\\\\", stderr
);
395 dump_strW( name
->name
, name
->len
, stderr
, "[]" );
398 /* dump the name of an object to stderr */
399 void dump_object_name( struct object
*obj
)
401 if (!obj
->name
) return;
402 fputc( '[', stderr
);
404 fputs( "] ", stderr
);
407 /* unlink a named object from its namespace, without freeing the object itself */
408 void unlink_named_object( struct object
*obj
)
410 struct object_name
*name_ptr
= obj
->name
;
412 if (!name_ptr
) return;
414 obj
->ops
->unlink_name( obj
, name_ptr
);
415 if (name_ptr
->parent
) release_object( name_ptr
->parent
);
419 /* grab an object (i.e. increment its refcount) and return the object */
420 struct object
*grab_object( void *ptr
)
422 struct object
*obj
= (struct object
*)ptr
;
423 assert( obj
->refcount
< INT_MAX
);
428 /* release an object (i.e. decrement its refcount) */
429 void release_object( void *ptr
)
431 struct object
*obj
= (struct object
*)ptr
;
432 assert( obj
->refcount
);
433 if (!--obj
->refcount
)
435 assert( !obj
->handle_count
);
436 /* if the refcount is 0, nobody can be in the wait queue */
437 assert( list_empty( &obj
->wait_queue
));
438 free_kernel_objects( obj
);
439 unlink_named_object( obj
);
440 obj
->ops
->destroy( obj
);
445 /* find an object by its name; the refcount is incremented */
446 struct object
*find_object( const struct namespace *namespace, const struct unicode_str
*name
,
447 unsigned int attributes
)
449 const struct list
*list
;
452 if (!name
|| !name
->len
) return NULL
;
454 list
= &namespace->names
[ hash_strW( name
->str
, name
->len
, namespace->hash_size
) ];
455 LIST_FOR_EACH( p
, list
)
457 const struct object_name
*ptr
= LIST_ENTRY( p
, struct object_name
, entry
);
458 if (ptr
->len
!= name
->len
) continue;
459 if (attributes
& OBJ_CASE_INSENSITIVE
)
461 if (!memicmp_strW( ptr
->name
, name
->str
, name
->len
))
462 return grab_object( ptr
->obj
);
466 if (!memcmp( ptr
->name
, name
->str
, name
->len
))
467 return grab_object( ptr
->obj
);
473 /* find an object by its index; the refcount is incremented */
474 struct object
*find_object_index( const struct namespace *namespace, unsigned int index
)
478 /* FIXME: not efficient at all */
479 for (i
= 0; i
< namespace->hash_size
; i
++)
481 const struct object_name
*ptr
;
482 LIST_FOR_EACH_ENTRY( ptr
, &namespace->names
[i
], const struct object_name
, entry
)
484 if (!index
--) return grab_object( ptr
->obj
);
487 set_error( STATUS_NO_MORE_ENTRIES
);
491 /* allocate a namespace */
492 struct namespace *create_namespace( unsigned int hash_size
)
494 struct namespace *namespace;
497 namespace = mem_alloc( sizeof(*namespace) + (hash_size
- 1) * sizeof(namespace->names
[0]) );
500 namespace->hash_size
= hash_size
;
501 for (i
= 0; i
< hash_size
; i
++) list_init( &namespace->names
[i
] );
506 /* functions for unimplemented/default object operations */
508 int no_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
510 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
514 void no_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
518 int no_signal( struct object
*obj
, unsigned int access
)
520 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
524 struct fd
*no_get_fd( struct object
*obj
)
526 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
530 unsigned int default_map_access( struct object
*obj
, unsigned int access
)
532 return map_access( access
, &obj
->ops
->type
->mapping
);
535 struct security_descriptor
*default_get_sd( struct object
*obj
)
540 int set_sd_defaults_from_token( struct object
*obj
, const struct security_descriptor
*sd
,
541 unsigned int set_info
, struct token
*token
)
543 struct security_descriptor new_sd
, *new_sd_ptr
;
545 const SID
*owner
= NULL
, *group
= NULL
;
546 const ACL
*sacl
, *dacl
;
547 ACL
*replaced_sacl
= NULL
;
550 if (!set_info
) return 1;
552 new_sd
.control
= sd
->control
& ~SE_SELF_RELATIVE
;
554 if (set_info
& OWNER_SECURITY_INFORMATION
&& sd
->owner_len
)
556 owner
= sd_get_owner( sd
);
557 new_sd
.owner_len
= sd
->owner_len
;
559 else if (obj
->sd
&& obj
->sd
->owner_len
)
561 owner
= sd_get_owner( obj
->sd
);
562 new_sd
.owner_len
= obj
->sd
->owner_len
;
566 owner
= token_get_user( token
);
567 new_sd
.owner_len
= security_sid_len( owner
);
569 else new_sd
.owner_len
= 0;
571 if (set_info
& GROUP_SECURITY_INFORMATION
&& sd
->group_len
)
573 group
= sd_get_group( sd
);
574 new_sd
.group_len
= sd
->group_len
;
576 else if (obj
->sd
&& obj
->sd
->group_len
)
578 group
= sd_get_group( obj
->sd
);
579 new_sd
.group_len
= obj
->sd
->group_len
;
583 group
= token_get_primary_group( token
);
584 new_sd
.group_len
= security_sid_len( group
);
586 else new_sd
.group_len
= 0;
588 sacl
= sd_get_sacl( sd
, &present
);
589 if (set_info
& SACL_SECURITY_INFORMATION
&& present
)
591 new_sd
.control
|= SE_SACL_PRESENT
;
592 new_sd
.sacl_len
= sd
->sacl_len
;
594 else if (set_info
& LABEL_SECURITY_INFORMATION
&& present
)
596 const ACL
*old_sacl
= NULL
;
597 if (obj
->sd
&& obj
->sd
->control
& SE_SACL_PRESENT
) old_sacl
= sd_get_sacl( obj
->sd
, &present
);
598 if (!(replaced_sacl
= replace_security_labels( old_sacl
, sacl
))) return 0;
599 new_sd
.control
|= SE_SACL_PRESENT
;
600 new_sd
.sacl_len
= replaced_sacl
->AclSize
;
601 sacl
= replaced_sacl
;
605 if (obj
->sd
) sacl
= sd_get_sacl( obj
->sd
, &present
);
607 if (obj
->sd
&& present
)
609 new_sd
.control
|= SE_SACL_PRESENT
;
610 new_sd
.sacl_len
= obj
->sd
->sacl_len
;
616 dacl
= sd_get_dacl( sd
, &present
);
617 if (set_info
& DACL_SECURITY_INFORMATION
&& present
)
619 new_sd
.control
|= SE_DACL_PRESENT
;
620 new_sd
.dacl_len
= sd
->dacl_len
;
624 if (obj
->sd
) dacl
= sd_get_dacl( obj
->sd
, &present
);
626 if (obj
->sd
&& present
)
628 new_sd
.control
|= SE_DACL_PRESENT
;
629 new_sd
.dacl_len
= obj
->sd
->dacl_len
;
633 dacl
= token_get_default_dacl( token
);
634 new_sd
.control
|= SE_DACL_PRESENT
;
635 new_sd
.dacl_len
= dacl
->AclSize
;
637 else new_sd
.dacl_len
= 0;
640 ptr
= mem_alloc( sizeof(new_sd
) + new_sd
.owner_len
+ new_sd
.group_len
+
641 new_sd
.sacl_len
+ new_sd
.dacl_len
);
644 free( replaced_sacl
);
647 new_sd_ptr
= (struct security_descriptor
*)ptr
;
649 memcpy( ptr
, &new_sd
, sizeof(new_sd
) );
650 ptr
+= sizeof(new_sd
);
651 memcpy( ptr
, owner
, new_sd
.owner_len
);
652 ptr
+= new_sd
.owner_len
;
653 memcpy( ptr
, group
, new_sd
.group_len
);
654 ptr
+= new_sd
.group_len
;
655 memcpy( ptr
, sacl
, new_sd
.sacl_len
);
656 ptr
+= new_sd
.sacl_len
;
657 memcpy( ptr
, dacl
, new_sd
.dacl_len
);
659 free( replaced_sacl
);
661 obj
->sd
= new_sd_ptr
;
665 /** Set the security descriptor using the current primary token for defaults. */
666 int default_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
667 unsigned int set_info
)
669 return set_sd_defaults_from_token( obj
, sd
, set_info
, current
->process
->token
);
672 WCHAR
*no_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
677 struct object
*no_lookup_name( struct object
*obj
, struct unicode_str
*name
,
678 unsigned int attr
, struct object
*root
)
680 if (!name
) set_error( STATUS_OBJECT_TYPE_MISMATCH
);
684 int no_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
686 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
690 void default_unlink_name( struct object
*obj
, struct object_name
*name
)
692 list_remove( &name
->entry
);
695 struct object
*no_open_file( struct object
*obj
, unsigned int access
, unsigned int sharing
,
696 unsigned int options
)
698 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
702 int no_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
704 return 1; /* ok to close */
707 void no_destroy( struct object
*obj
)