source: mainline/kernel/generic/src/cap/cap.c@ fa3ed5b

Last change on this file since fa3ed5b was fa3ed5b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 11 months ago

Bulk clear capabilities when task terminates or is destroyed

  • Property mode set to 100644
File size: 15.8 KB
Line 
1/*
2 * Copyright (c) 2017 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_generic
30 * @{
31 */
32/** @file
33 */
34
35/*
36 * HelenOS capabilities are task-local names for references to kernel objects.
37 * Kernel objects are reference-counted wrappers for a select group of objects
38 * allocated in and by the kernel that can be made accessible to userspace in a
39 * controlled way via integer handles.
40 *
41 * A kernel object (kobject_t) encapsulates one of the following raw objects:
42 *
43 * - IPC call
44 * - IPC phone
45 * - IRQ object
46 *
47 * A capability (cap_t) is either free, allocated or published. Free
48 * capabilities can be allocated, which reserves the capability handle in the
49 * task-local capability space. Allocated capabilities can be published, which
50 * associates them with an existing kernel object. Userspace can only access
51 * published capabilities.
52 *
53 * A published capability may get unpublished, which disassociates it from the
54 * underlying kernel object and puts it back into the allocated state. An
55 * allocated capability can be freed to become available for future use.
56 *
57 * There is a 1:1 correspondence between a kernel object (kobject_t) and the
58 * actual raw object it encapsulates. A kernel object (kobject_t) may have
59 * multiple references, either implicit from one or more capabilities (cap_t),
60 * even from capabilities in different tasks, or explicit as a result of
61 * creating a new reference from a capability handle using kobject_get(), or
62 * creating a new reference from an already existing reference by
63 * kobject_add_ref() or as a result of unpublishing a capability and
64 * disassociating it from its kobject_t using cap_unpublish().
65 *
66 * A holder of an explicit reference to a kernel object may revoke access to it
67 * from all capabilities that point to it by calling cap_revoke().
68 *
69 * As kernel objects are reference-counted, they get automatically destroyed
70 * when their last reference is dropped in kobject_put(). The idea is that
71 * whenever a kernel object is inserted into some sort of a container (e.g. a
72 * list or hash table), its reference count should be incremented via
73 * kobject_get() or kobject_add_ref(). When the kernel object is removed from
74 * the container, the reference count should go down via a call to
75 * kobject_put().
76 */
77
78#include <cap/cap.h>
79#include <abi/cap.h>
80#include <proc/task.h>
81#include <synch/mutex.h>
82#include <abi/errno.h>
83#include <mm/slab.h>
84#include <adt/list.h>
85#include <synch/syswaitq.h>
86#include <ipc/ipcrsc.h>
87#include <ipc/ipc.h>
88#include <ipc/irq.h>
89
90#include <limits.h>
91#include <stdint.h>
92#include <stdlib.h>
93
94#define CAPS_START ((intptr_t) CAP_NIL + 1)
95#define CAPS_LAST ((intptr_t) INT_MAX - 1)
96#define CAPS_SIZE (CAPS_LAST - CAPS_START + 1)
97
98static slab_cache_t *cap_cache;
99static slab_cache_t *kobject_cache;
100
101kobject_ops_t *kobject_ops[KOBJECT_TYPE_MAX] = {
102 [KOBJECT_TYPE_CALL] = &call_kobject_ops,
103 [KOBJECT_TYPE_IRQ] = &irq_kobject_ops,
104 [KOBJECT_TYPE_PHONE] = &phone_kobject_ops,
105 [KOBJECT_TYPE_WAITQ] = &waitq_kobject_ops
106};
107
108static size_t caps_hash(const ht_link_t *item)
109{
110 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
111 return hash_mix(cap_handle_raw(cap->handle));
112}
113
114static size_t caps_key_hash(const void *key)
115{
116 const cap_handle_t *handle = key;
117 return hash_mix(cap_handle_raw(*handle));
118}
119
120static bool caps_key_equal(const void *key, const ht_link_t *item)
121{
122 const cap_handle_t *handle = key;
123 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
124 return *handle == cap->handle;
125}
126
127static void caps_remove_callback(ht_link_t *item)
128{
129 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
130
131 if (cap->kobject) {
132 kobject_t *kobj = cap->kobject;
133
134 mutex_lock(&kobj->caps_list_lock);
135 cap->kobject = NULL;
136 list_remove(&cap->kobj_link);
137 mutex_unlock(&kobj->caps_list_lock);
138
139 kobject_put(kobj);
140 }
141
142 list_remove(&cap->type_link);
143
144 slab_free(cap_cache, cap);
145}
146
147static const hash_table_ops_t caps_ops = {
148 .hash = caps_hash,
149 .key_hash = caps_key_hash,
150 .key_equal = caps_key_equal,
151 .remove_callback = caps_remove_callback,
152};
153
154void caps_init(void)
155{
156 cap_cache = slab_cache_create("cap_t", sizeof(cap_t), 0, NULL,
157 NULL, 0);
158 kobject_cache = slab_cache_create("kobject_t", sizeof(kobject_t), 0,
159 NULL, NULL, 0);
160}
161
162/** Allocate the capability info structure
163 *
164 * @param task Task for which to allocate the info structure.
165 */
166errno_t caps_task_alloc(task_t *task)
167{
168 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t));
169 if (!task->cap_info)
170 return ENOMEM;
171
172 if (!hash_table_create(&task->cap_info->caps, 0, 0, &caps_ops)) {
173 free(task->cap_info);
174 return ENOMEM;
175 }
176
177 mutex_initialize(&task->cap_info->lock, MUTEX_RECURSIVE);
178
179 task->cap_info->handles = NULL;
180
181 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
182 list_initialize(&task->cap_info->type_list[t]);
183
184 return EOK;
185}
186
187/** Initialize the capability info structure
188 *
189 * @param task Task for which to initialize the info structure.
190 */
191errno_t caps_task_init(task_t *task)
192{
193 assert(task->cap_info);
194 assert(!task->cap_info->handles);
195
196 task->cap_info->handles = ra_arena_create();
197 if (!task->cap_info->handles)
198 return ENOMEM;
199
200 if (!ra_span_add(task->cap_info->handles, CAPS_START, CAPS_SIZE)) {
201 ra_arena_destroy(task->cap_info->handles);
202 return ENOMEM;
203 }
204
205 return EOK;
206}
207
208void caps_task_clear(task_t *task)
209{
210 mutex_lock(&task->cap_info->lock);
211
212 hash_table_clear(&task->cap_info->caps);
213
214 if (task->cap_info->handles) {
215 ra_arena_destroy(task->cap_info->handles);
216 task->cap_info->handles = NULL;
217 }
218
219 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
220 list_initialize(&task->cap_info->type_list[t]);
221
222 mutex_unlock(&task->cap_info->lock);
223}
224
225/** Deallocate the capability info structure
226 *
227 * @param task Task from which to deallocate the info structure.
228 */
229void caps_task_free(task_t *task)
230{
231 hash_table_destroy(&task->cap_info->caps);
232
233 if (task->cap_info->handles)
234 ra_arena_destroy(task->cap_info->handles);
235
236 free(task->cap_info);
237}
238
239/** Invoke callback function on task's capabilites of given type
240 *
241 * @param task Task where the invocation should take place.
242 * @param type Kernel object type of the task's capabilities that will be
243 * subject to the callback invocation.
244 * @param cb Callback function.
245 * @param arg Argument for the callback function.
246 *
247 * @return True if the callback was called on all matching capabilities.
248 * @return False if the callback was applied only partially.
249 */
250bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
251 bool (*cb)(cap_t *, void *), void *arg)
252{
253 bool done = true;
254
255 mutex_lock(&task->cap_info->lock);
256 list_foreach_safe(task->cap_info->type_list[type], cur, next) {
257 cap_t *cap = list_get_instance(cur, cap_t, type_link);
258 done = cb(cap, arg);
259 if (!done)
260 break;
261 }
262 mutex_unlock(&task->cap_info->lock);
263
264 return done;
265}
266
267/** Initialize capability and associate it with its handle
268 *
269 * @param cap Address of the capability.
270 * @param task Backling to the owning task.
271 * @param handle Capability handle.
272 */
273static void cap_initialize(cap_t *cap, task_t *task, cap_handle_t handle)
274{
275 cap->state = CAP_STATE_FREE;
276 cap->task = task;
277 cap->handle = handle;
278 link_initialize(&cap->kobj_link);
279 link_initialize(&cap->type_link);
280}
281
282/** Get capability using capability handle
283 *
284 * @param task Task whose capability to get.
285 * @param handle Capability handle of the desired capability.
286 * @param state State in which the capability must be.
287 *
288 * @return Address of the desired capability if it exists and its state matches.
289 * @return NULL if no such capability exists or it's in a different state.
290 */
291static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
292{
293 assert(mutex_locked(&task->cap_info->lock));
294
295 if ((cap_handle_raw(handle) < CAPS_START) ||
296 (cap_handle_raw(handle) > CAPS_LAST))
297 return NULL;
298 ht_link_t *link = hash_table_find(&task->cap_info->caps, &handle);
299 if (!link)
300 return NULL;
301 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
302 if (cap->state != state)
303 return NULL;
304 return cap;
305}
306
307/** Allocate new capability
308 *
309 * @param task Task for which to allocate the new capability.
310 *
311 * @param[out] handle New capability handle on success.
312 *
313 * @return An error code in case of error.
314 */
315errno_t cap_alloc(task_t *task, cap_handle_t *handle)
316{
317 mutex_lock(&task->cap_info->lock);
318 cap_t *cap = slab_alloc(cap_cache, FRAME_ATOMIC);
319 if (!cap) {
320 mutex_unlock(&task->cap_info->lock);
321 return ENOMEM;
322 }
323 uintptr_t hbase;
324 if (!ra_alloc(task->cap_info->handles, 1, 1, &hbase)) {
325 slab_free(cap_cache, cap);
326 mutex_unlock(&task->cap_info->lock);
327 return ENOMEM;
328 }
329 cap_initialize(cap, task, (cap_handle_t) hbase);
330 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
331
332 cap->state = CAP_STATE_ALLOCATED;
333 *handle = cap->handle;
334 mutex_unlock(&task->cap_info->lock);
335
336 return EOK;
337}
338
339/** Publish allocated capability
340 *
341 * The kernel object is moved into the capability. In other words, its reference
342 * is handed over to the capability. Once published, userspace can access and
343 * manipulate the capability.
344 *
345 * @param task Task in which to publish the capability.
346 * @param handle Capability handle.
347 * @param kobj Kernel object.
348 */
349void
350cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
351{
352 mutex_lock(&task->cap_info->lock);
353 mutex_lock(&kobj->caps_list_lock);
354 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
355 assert(cap);
356 cap->state = CAP_STATE_PUBLISHED;
357 /* Hand over kobj's reference to cap */
358 cap->kobject = kobj;
359 list_append(&cap->kobj_link, &kobj->caps_list);
360 list_append(&cap->type_link, &task->cap_info->type_list[kobj->type]);
361 mutex_unlock(&kobj->caps_list_lock);
362 mutex_unlock(&task->cap_info->lock);
363}
364
365static void cap_unpublish_unsafe(cap_t *cap)
366{
367 cap->kobject = NULL;
368 list_remove(&cap->kobj_link);
369 list_remove(&cap->type_link);
370 cap->state = CAP_STATE_ALLOCATED;
371}
372
373/** Unpublish published capability
374 *
375 * The kernel object is moved out of the capability. In other words, the
376 * capability's reference to the objects is handed over to the kernel object
377 * pointer returned by this function. Once unpublished, the capability does not
378 * refer to any kernel object anymore.
379 *
380 * @param task Task in which to unpublish the capability.
381 * @param handle Capability handle.
382 * @param type Kernel object type of the object associated with the
383 * capability.
384 *
385 * @return Pointer and explicit reference to the kobject that was associated
386 * with the capability.
387 */
388kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
389{
390 kobject_t *kobj = NULL;
391
392 mutex_lock(&task->cap_info->lock);
393 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
394 if (cap) {
395 if (cap->kobject->type == type) {
396 /* Hand over cap's reference to kobj */
397 kobj = cap->kobject;
398
399 mutex_lock(&kobj->caps_list_lock);
400 cap_unpublish_unsafe(cap);
401 mutex_unlock(&kobj->caps_list_lock);
402 }
403 }
404 mutex_unlock(&task->cap_info->lock);
405
406 return kobj;
407}
408
409/** Revoke access to kobject from all existing capabilities
410 *
411 * All published capabilities associated with the kobject are unpublished (i.e.
412 * their new state is set to CAP_STATE_ALLOCATED) and no longer point to the
413 * kobject. Kobject's reference count is decreased accordingly.
414 *
415 * Note that the caller is supposed to hold an explicit reference to the kobject
416 * so that the kobject is guaranteed to exist when this function returns.
417 *
418 * @param kobj Pointer and explicit reference to the kobject capabilities of
419 * which are about to be unpublished.
420 */
421void cap_revoke(kobject_t *kobj)
422{
423 mutex_lock(&kobj->caps_list_lock);
424
425 while (!list_empty(&kobj->caps_list)) {
426 cap_t *cap = list_get_instance(list_first(&kobj->caps_list), cap_t, kobj_link);
427
428 /* We're trying to acquire the two locks in reverse order. */
429 if (mutex_trylock(&cap->task->cap_info->lock) != EOK) {
430 mutex_unlock(&kobj->caps_list_lock);
431 mutex_lock(&kobj->caps_list_lock);
432 continue;
433 }
434
435 cap_unpublish_unsafe(cap);
436 /* Drop the reference for the unpublished capability */
437 kobject_put(kobj);
438
439 mutex_unlock(&cap->task->cap_info->lock);
440 }
441
442 mutex_unlock(&kobj->caps_list_lock);
443}
444
445/** Free allocated capability
446 *
447 * @param task Task in which to free the capability.
448 * @param handle Capability handle.
449 */
450void cap_free(task_t *task, cap_handle_t handle)
451{
452 assert(cap_handle_raw(handle) >= CAPS_START);
453 assert(cap_handle_raw(handle) <= CAPS_LAST);
454
455 mutex_lock(&task->cap_info->lock);
456 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
457
458 assert(cap);
459
460 hash_table_remove_item(&task->cap_info->caps, &cap->caps_link);
461 ra_free(task->cap_info->handles, cap_handle_raw(handle), 1);
462 mutex_unlock(&task->cap_info->lock);
463}
464
465kobject_t *kobject_alloc(unsigned int flags)
466{
467 return slab_alloc(kobject_cache, flags);
468}
469
470void kobject_free(kobject_t *kobj)
471{
472 slab_free(kobject_cache, kobj);
473}
474
475/** Initialize kernel object
476 *
477 * @param kobj Kernel object to initialize.
478 * @param type Type of the kernel object.
479 * @param raw Raw pointer to the encapsulated object.
480 */
481void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw)
482{
483 refcount_init(&kobj->refcnt);
484
485 mutex_initialize(&kobj->caps_list_lock, MUTEX_PASSIVE);
486 list_initialize(&kobj->caps_list);
487
488 kobj->type = type;
489 kobj->raw = raw;
490}
491
492/** Get new reference to kernel object from capability
493 *
494 * @param task Task from which to get the reference.
495 * @param handle Capability handle.
496 * @param type Kernel object type of the object associated with the
497 * capability referenced by handle.
498 *
499 * @return Kernel object with incremented reference count on success.
500 * @return NULL if there is no matching capability or kernel object.
501 */
502kobject_t *
503kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
504{
505 kobject_t *kobj = NULL;
506
507 mutex_lock(&task->cap_info->lock);
508 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
509 if (cap) {
510 if (cap->kobject->type == type) {
511 kobj = cap->kobject;
512 refcount_up(&kobj->refcnt);
513 }
514 }
515 mutex_unlock(&task->cap_info->lock);
516
517 return kobj;
518}
519
520/** Record new reference
521 *
522 * @param kobj Kernel object from which the new reference is created.
523 */
524void kobject_add_ref(kobject_t *kobj)
525{
526 refcount_up(&kobj->refcnt);
527}
528
529/** Drop reference to kernel object
530 *
531 * The encapsulated object and the kobject_t wrapper are both destroyed when the
532 * last reference is dropped.
533 *
534 * @param kobj Kernel object whose reference to drop.
535 */
536void kobject_put(kobject_t *kobj)
537{
538 if (refcount_down(&kobj->refcnt)) {
539 KOBJECT_OP(kobj)->destroy(kobj->raw);
540 kobject_free(kobj);
541 }
542}
543
544/** @}
545 */
Note: See TracBrowser for help on using the repository browser.