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 #include <sys/types.h>
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
36 #define WIN32_NO_STATUS
48 unsigned int hash_size
; /* size of hash table */
49 struct list names
[1]; /* array of hash entry lists */
53 struct type_descr no_type
=
55 { NULL
, 0 }, /* name */
56 STANDARD_RIGHTS_REQUIRED
, /* valid_access */
59 STANDARD_RIGHTS_WRITE
,
60 STANDARD_RIGHTS_EXECUTE
,
61 STANDARD_RIGHTS_REQUIRED
66 static struct list object_list
= LIST_INIT(object_list
);
68 void dump_objects(void)
72 LIST_FOR_EACH_ENTRY( ptr
, &object_list
, struct object
, obj_list
)
74 fprintf( stderr
, "%p:%d: ", ptr
, ptr
->refcount
);
75 dump_object_name( ptr
);
76 ptr
->ops
->dump( ptr
, 1 );
80 void close_objects(void)
82 /* release the permanent objects */
88 LIST_FOR_EACH_ENTRY( obj
, &object_list
, struct object
, obj_list
)
90 if (!(found
= obj
->is_permanent
)) continue;
91 obj
->is_permanent
= 0;
92 release_object( obj
);
98 dump_objects(); /* dump any remaining objects */
101 #endif /* DEBUG_OBJECTS */
103 /*****************************************************************/
105 /* mark a block of memory as uninitialized for debugging purposes */
106 static inline void mark_block_uninitialized( void *ptr
, size_t size
)
108 memset( ptr
, 0x55, size
);
109 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
110 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
111 #elif defined(VALGRIND_MAKE_WRITABLE)
112 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
116 /* malloc replacement */
117 void *mem_alloc( size_t size
)
119 void *ptr
= malloc( size
);
120 if (ptr
) mark_block_uninitialized( ptr
, size
);
121 else set_error( STATUS_NO_MEMORY
);
125 /* duplicate a block of memory */
126 void *memdup( const void *data
, size_t len
)
128 void *ptr
= malloc( len
);
129 if (ptr
) memcpy( ptr
, data
, len
);
130 else set_error( STATUS_NO_MEMORY
);
135 /*****************************************************************/
137 void namespace_add( struct namespace *namespace, struct object_name
*ptr
)
139 unsigned int hash
= hash_strW( ptr
->name
, ptr
->len
, namespace->hash_size
);
141 list_add_head( &namespace->names
[hash
], &ptr
->entry
);
144 /* allocate a name for an object */
145 static struct object_name
*alloc_name( const struct unicode_str
*name
)
147 struct object_name
*ptr
;
149 if ((ptr
= mem_alloc( sizeof(*ptr
) + name
->len
- sizeof(ptr
->name
) )))
151 ptr
->len
= name
->len
;
153 memcpy( ptr
->name
, name
->str
, name
->len
);
158 /* get the name of an existing object */
159 const WCHAR
*get_object_name( struct object
*obj
, data_size_t
*len
)
161 struct object_name
*ptr
= obj
->name
;
162 if (!ptr
) return NULL
;
167 /* get the full path name of an existing object */
168 WCHAR
*default_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
170 static const WCHAR backslash
= '\\';
171 struct object
*ptr
= obj
;
175 while (ptr
&& ptr
->name
)
177 struct object_name
*name
= ptr
->name
;
178 len
+= name
->len
+ sizeof(WCHAR
);
181 if (!len
) return NULL
;
182 if (!(ret
= malloc( len
))) return NULL
;
185 while (obj
&& obj
->name
)
187 struct object_name
*name
= obj
->name
;
188 memcpy( ret
+ len
- name
->len
, name
->name
, name
->len
);
189 len
-= name
->len
+ sizeof(WCHAR
);
190 memcpy( ret
+ len
, &backslash
, sizeof(WCHAR
) );
196 /* allocate and initialize an object */
197 void *alloc_object( const struct object_ops
*ops
)
199 struct object
*obj
= mem_alloc( ops
->size
);
203 obj
->handle_count
= 0;
204 obj
->is_permanent
= 0;
208 list_init( &obj
->wait_queue
);
210 list_add_head( &object_list
, &obj
->obj_list
);
212 obj
->ops
->type
->obj_count
++;
213 obj
->ops
->type
->obj_max
= max( obj
->ops
->type
->obj_max
, obj
->ops
->type
->obj_count
);
219 /* free an object once it has been destroyed */
220 static void free_object( struct object
*obj
)
223 obj
->ops
->type
->obj_count
--;
225 list_remove( &obj
->obj_list
);
226 memset( obj
, 0xaa, obj
->ops
->size
);
231 /* find an object by name starting from the specified root */
232 /* if it doesn't exist, its parent is returned, and name_left contains the remaining name */
233 struct object
*lookup_named_object( struct object
*root
, const struct unicode_str
*name
,
234 unsigned int attr
, struct unicode_str
*name_left
)
236 struct object
*obj
, *parent
;
237 struct unicode_str name_tmp
= *name
, *ptr
= &name_tmp
;
241 /* if root is specified path shouldn't start with backslash */
242 if (name_tmp
.len
&& name_tmp
.str
[0] == '\\')
244 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
247 parent
= grab_object( root
);
251 if (!name_tmp
.len
|| name_tmp
.str
[0] != '\\')
253 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
256 /* skip leading backslash */
258 name_tmp
.len
-= sizeof(WCHAR
);
259 parent
= root
= get_root_directory();
262 if (!name_tmp
.len
) ptr
= NULL
; /* special case for empty path */
266 while ((obj
= parent
->ops
->lookup_name( parent
, ptr
, attr
, root
)))
268 /* move to the next element */
269 release_object ( parent
);
274 release_object( parent
);
278 if (name_left
) *name_left
= name_tmp
;
282 /* return length of first path element in name */
283 data_size_t
get_path_element( const WCHAR
*name
, data_size_t len
)
287 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++) if (name
[i
] == '\\') break;
288 return i
* sizeof(WCHAR
);
291 static struct object
*create_object( struct object
*parent
, const struct object_ops
*ops
,
292 const struct unicode_str
*name
, unsigned int attributes
,
293 const struct security_descriptor
*sd
)
296 struct object_name
*name_ptr
;
298 if (!(name_ptr
= alloc_name( name
))) return NULL
;
299 if (!(obj
= alloc_object( ops
))) goto failed
;
300 if (sd
&& !default_set_sd( obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
301 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
303 if (!obj
->ops
->link_name( obj
, name_ptr
, parent
)) goto failed
;
306 obj
->name
= name_ptr
;
310 if (obj
) free_object( obj
);
315 /* create an object as named child under the specified parent */
316 void *create_named_object( struct object
*parent
, const struct object_ops
*ops
,
317 const struct unicode_str
*name
, unsigned int attributes
,
318 const struct security_descriptor
*sd
)
320 struct object
*obj
, *new_obj
;
321 struct unicode_str new_name
;
325 if (!name
|| !name
->len
)
327 if (!(new_obj
= alloc_object( ops
))) return NULL
;
328 if (sd
&& !default_set_sd( new_obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
329 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
331 free_object( new_obj
);
337 if (!(obj
= lookup_named_object( parent
, name
, attributes
, &new_name
))) return NULL
;
341 if (attributes
& OBJ_OPENIF
&& obj
->ops
== ops
)
342 set_error( STATUS_OBJECT_NAME_EXISTS
);
345 release_object( obj
);
347 if (attributes
& OBJ_OPENIF
)
348 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
350 set_error( STATUS_OBJECT_NAME_COLLISION
);
355 new_obj
= create_object( obj
, ops
, &new_name
, attributes
, sd
);
356 release_object( obj
);
359 if (attributes
& OBJ_PERMANENT
)
361 make_object_permanent( new_obj
);
362 grab_object( new_obj
);
367 /* open a object by name under the specified parent */
368 void *open_named_object( struct object
*parent
, const struct object_ops
*ops
,
369 const struct unicode_str
*name
, unsigned int attributes
)
371 struct unicode_str name_left
;
374 if ((obj
= lookup_named_object( parent
, name
, attributes
, &name_left
)))
376 if (name_left
.len
) /* not fully parsed */
377 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
378 else if (ops
&& obj
->ops
!= ops
)
379 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
383 release_object( obj
);
388 /* recursive helper for dump_object_name */
389 static void dump_name( struct object
*obj
)
391 struct object_name
*name
= obj
->name
;
394 if (name
->parent
) dump_name( name
->parent
);
395 fputs( "\\\\", stderr
);
396 dump_strW( name
->name
, name
->len
, stderr
, "[]" );
399 /* dump the name of an object to stderr */
400 void dump_object_name( struct object
*obj
)
402 if (!obj
->name
) return;
403 fputc( '[', stderr
);
405 fputs( "] ", stderr
);
408 /* unlink a named object from its namespace, without freeing the object itself */
409 void unlink_named_object( struct object
*obj
)
411 struct object_name
*name_ptr
= obj
->name
;
413 if (!name_ptr
) return;
415 obj
->ops
->unlink_name( obj
, name_ptr
);
416 if (name_ptr
->parent
) release_object( name_ptr
->parent
);
420 /* grab an object (i.e. increment its refcount) and return the object */
421 struct object
*grab_object( void *ptr
)
423 struct object
*obj
= (struct object
*)ptr
;
424 assert( obj
->refcount
< INT_MAX
);
429 /* release an object (i.e. decrement its refcount) */
430 void release_object( void *ptr
)
432 struct object
*obj
= (struct object
*)ptr
;
433 assert( obj
->refcount
);
434 if (!--obj
->refcount
)
436 assert( !obj
->handle_count
);
437 /* if the refcount is 0, nobody can be in the wait queue */
438 assert( list_empty( &obj
->wait_queue
));
439 free_kernel_objects( obj
);
440 unlink_named_object( obj
);
441 obj
->ops
->destroy( obj
);
446 /* find an object by its name; the refcount is incremented */
447 struct object
*find_object( const struct namespace *namespace, const struct unicode_str
*name
,
448 unsigned int attributes
)
450 const struct list
*list
;
453 if (!name
|| !name
->len
) return NULL
;
455 list
= &namespace->names
[ hash_strW( name
->str
, name
->len
, namespace->hash_size
) ];
456 LIST_FOR_EACH( p
, list
)
458 const struct object_name
*ptr
= LIST_ENTRY( p
, struct object_name
, entry
);
459 if (ptr
->len
!= name
->len
) continue;
460 if (attributes
& OBJ_CASE_INSENSITIVE
)
462 if (!memicmp_strW( ptr
->name
, name
->str
, name
->len
))
463 return grab_object( ptr
->obj
);
467 if (!memcmp( ptr
->name
, name
->str
, name
->len
))
468 return grab_object( ptr
->obj
);
474 /* find an object by its index; the refcount is incremented */
475 struct object
*find_object_index( const struct namespace *namespace, unsigned int index
)
479 /* FIXME: not efficient at all */
480 for (i
= 0; i
< namespace->hash_size
; i
++)
482 const struct object_name
*ptr
;
483 LIST_FOR_EACH_ENTRY( ptr
, &namespace->names
[i
], const struct object_name
, entry
)
485 if (!index
--) return grab_object( ptr
->obj
);
488 set_error( STATUS_NO_MORE_ENTRIES
);
492 /* allocate a namespace */
493 struct namespace *create_namespace( unsigned int hash_size
)
495 struct namespace *namespace;
498 namespace = mem_alloc( sizeof(*namespace) + (hash_size
- 1) * sizeof(namespace->names
[0]) );
501 namespace->hash_size
= hash_size
;
502 for (i
= 0; i
< hash_size
; i
++) list_init( &namespace->names
[i
] );
507 /* functions for unimplemented/default object operations */
509 int no_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
511 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
515 void no_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
519 int no_signal( struct object
*obj
, unsigned int access
)
521 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
525 struct fd
*no_get_fd( struct object
*obj
)
527 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
531 unsigned int default_map_access( struct object
*obj
, unsigned int access
)
533 return map_access( access
, &obj
->ops
->type
->mapping
);
536 struct security_descriptor
*default_get_sd( struct object
*obj
)
541 int set_sd_defaults_from_token( struct object
*obj
, const struct security_descriptor
*sd
,
542 unsigned int set_info
, struct token
*token
)
544 struct security_descriptor new_sd
, *new_sd_ptr
;
546 const struct sid
*owner
= NULL
, *group
= NULL
;
547 const struct acl
*sacl
, *dacl
;
548 struct acl
*replaced_sacl
= NULL
;
551 if (!set_info
) return 1;
553 new_sd
.control
= sd
->control
& ~SE_SELF_RELATIVE
;
555 if (set_info
& OWNER_SECURITY_INFORMATION
&& sd
->owner_len
)
557 owner
= sd_get_owner( sd
);
558 new_sd
.owner_len
= sd
->owner_len
;
560 else if (obj
->sd
&& obj
->sd
->owner_len
)
562 owner
= sd_get_owner( obj
->sd
);
563 new_sd
.owner_len
= obj
->sd
->owner_len
;
567 owner
= token_get_user( token
);
568 new_sd
.owner_len
= sid_len( owner
);
570 else new_sd
.owner_len
= 0;
572 if (set_info
& GROUP_SECURITY_INFORMATION
&& sd
->group_len
)
574 group
= sd_get_group( sd
);
575 new_sd
.group_len
= sd
->group_len
;
577 else if (obj
->sd
&& obj
->sd
->group_len
)
579 group
= sd_get_group( obj
->sd
);
580 new_sd
.group_len
= obj
->sd
->group_len
;
584 group
= token_get_primary_group( token
);
585 new_sd
.group_len
= sid_len( group
);
587 else new_sd
.group_len
= 0;
589 sacl
= sd_get_sacl( sd
, &present
);
590 if (set_info
& SACL_SECURITY_INFORMATION
&& present
)
592 new_sd
.control
|= SE_SACL_PRESENT
;
593 new_sd
.sacl_len
= sd
->sacl_len
;
595 else if (set_info
& LABEL_SECURITY_INFORMATION
&& present
)
597 const struct acl
*old_sacl
= NULL
;
598 if (obj
->sd
&& obj
->sd
->control
& SE_SACL_PRESENT
) old_sacl
= sd_get_sacl( obj
->sd
, &present
);
599 if (!(replaced_sacl
= replace_security_labels( old_sacl
, sacl
))) return 0;
600 new_sd
.control
|= SE_SACL_PRESENT
;
601 new_sd
.sacl_len
= replaced_sacl
->size
;
602 sacl
= replaced_sacl
;
606 if (obj
->sd
) sacl
= sd_get_sacl( obj
->sd
, &present
);
608 if (obj
->sd
&& present
)
610 new_sd
.control
|= SE_SACL_PRESENT
;
611 new_sd
.sacl_len
= obj
->sd
->sacl_len
;
617 dacl
= sd_get_dacl( sd
, &present
);
618 if (set_info
& DACL_SECURITY_INFORMATION
&& present
)
620 new_sd
.control
|= SE_DACL_PRESENT
;
621 new_sd
.dacl_len
= sd
->dacl_len
;
625 if (obj
->sd
) dacl
= sd_get_dacl( obj
->sd
, &present
);
627 if (obj
->sd
&& present
)
629 new_sd
.control
|= SE_DACL_PRESENT
;
630 new_sd
.dacl_len
= obj
->sd
->dacl_len
;
634 dacl
= token_get_default_dacl( token
);
635 new_sd
.control
|= SE_DACL_PRESENT
;
636 new_sd
.dacl_len
= dacl
->size
;
638 else new_sd
.dacl_len
= 0;
641 ptr
= mem_alloc( sizeof(new_sd
) + new_sd
.owner_len
+ new_sd
.group_len
+
642 new_sd
.sacl_len
+ new_sd
.dacl_len
);
645 free( replaced_sacl
);
648 new_sd_ptr
= (struct security_descriptor
*)ptr
;
650 memcpy( ptr
, &new_sd
, sizeof(new_sd
) );
651 ptr
+= sizeof(new_sd
);
652 memcpy( ptr
, owner
, new_sd
.owner_len
);
653 ptr
+= new_sd
.owner_len
;
654 memcpy( ptr
, group
, new_sd
.group_len
);
655 ptr
+= new_sd
.group_len
;
656 memcpy( ptr
, sacl
, new_sd
.sacl_len
);
657 ptr
+= new_sd
.sacl_len
;
658 memcpy( ptr
, dacl
, new_sd
.dacl_len
);
660 free( replaced_sacl
);
662 obj
->sd
= new_sd_ptr
;
666 /** Set the security descriptor using the current primary token for defaults. */
667 int default_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
668 unsigned int set_info
)
670 return set_sd_defaults_from_token( obj
, sd
, set_info
, current
->process
->token
);
673 WCHAR
*no_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
678 struct object
*no_lookup_name( struct object
*obj
, struct unicode_str
*name
,
679 unsigned int attr
, struct object
*root
)
681 if (!name
) set_error( STATUS_OBJECT_TYPE_MISMATCH
);
685 int no_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
687 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
691 void default_unlink_name( struct object
*obj
, struct object_name
*name
)
693 list_remove( &name
->entry
);
696 struct object
*no_open_file( struct object
*obj
, unsigned int access
, unsigned int sharing
,
697 unsigned int options
)
699 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
703 int no_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
705 return 1; /* ok to close */
708 void no_destroy( struct object
*obj
)