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
RevLine 
[7e3826d9]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
[174156fd]29/** @addtogroup kernel_generic
[7e3826d9]30 * @{
31 */
32/** @file
33 */
34
[6636fb19]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 *
[ce4a21a0]43 * - IPC call
[6636fb19]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 *
[d24e987]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 *
[6636fb19]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
[3f74275]78#include <cap/cap.h>
[f571ca49]79#include <abi/cap.h>
[7e3826d9]80#include <proc/task.h>
[9e87562]81#include <synch/mutex.h>
[e9d15d9]82#include <abi/errno.h>
[e7ac23d0]83#include <mm/slab.h>
[9e87562]84#include <adt/list.h>
[fc0de8c]85#include <synch/syswaitq.h>
86#include <ipc/ipcrsc.h>
87#include <ipc/ipc.h>
88#include <ipc/irq.h>
[7e3826d9]89
[10d65d70]90#include <limits.h>
[05913fe7]91#include <stdint.h>
[aafed15]92#include <stdlib.h>
[05913fe7]93
[9675296]94#define CAPS_START ((intptr_t) CAP_NIL + 1)
[fa3ed5b]95#define CAPS_LAST ((intptr_t) INT_MAX - 1)
96#define CAPS_SIZE (CAPS_LAST - CAPS_START + 1)
[05913fe7]97
[82d515e9]98static slab_cache_t *cap_cache;
[e394c196]99static slab_cache_t *kobject_cache;
[ce732e74]100
[fc0de8c]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
[05913fe7]108static size_t caps_hash(const ht_link_t *item)
[05ffb41]109{
[05913fe7]110 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
[bb97118]111 return hash_mix(cap_handle_raw(cap->handle));
[05913fe7]112}
113
[5e801dc]114static size_t caps_key_hash(const void *key)
[05913fe7]115{
[5e801dc]116 const cap_handle_t *handle = key;
[bb97118]117 return hash_mix(cap_handle_raw(*handle));
[05913fe7]118}
119
[5e801dc]120static bool caps_key_equal(const void *key, const ht_link_t *item)
[05913fe7]121{
[5e801dc]122 const cap_handle_t *handle = key;
[05913fe7]123 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
124 return *handle == cap->handle;
[05ffb41]125}
126
[fa3ed5b]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
[61eb2ce2]147static const hash_table_ops_t caps_ops = {
[05913fe7]148 .hash = caps_hash,
149 .key_hash = caps_key_hash,
[fa3ed5b]150 .key_equal = caps_key_equal,
151 .remove_callback = caps_remove_callback,
[05913fe7]152};
153
[ce732e74]154void caps_init(void)
155{
[82d515e9]156 cap_cache = slab_cache_create("cap_t", sizeof(cap_t), 0, NULL,
[ce732e74]157 NULL, 0);
[e394c196]158 kobject_cache = slab_cache_create("kobject_t", sizeof(kobject_t), 0,
159 NULL, NULL, 0);
[ce732e74]160}
161
[6636fb19]162/** Allocate the capability info structure
163 *
164 * @param task Task for which to allocate the info structure.
165 */
[b7fd2a0]166errno_t caps_task_alloc(task_t *task)
[e7ac23d0]167{
[11b285d]168 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t));
[c46bfbc]169 if (!task->cap_info)
170 return ENOMEM;
171
[fa3ed5b]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;
[e7ac23d0]185}
186
[6636fb19]187/** Initialize the capability info structure
188 *
189 * @param task Task for which to initialize the info structure.
190 */
[fa3ed5b]191errno_t caps_task_init(task_t *task)
[e7ac23d0]192{
[fa3ed5b]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 }
[9e87562]218
[48bcf49]219 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
220 list_initialize(&task->cap_info->type_list[t]);
[fa3ed5b]221
222 mutex_unlock(&task->cap_info->lock);
[e7ac23d0]223}
224
[6636fb19]225/** Deallocate the capability info structure
226 *
227 * @param task Task from which to deallocate the info structure.
228 */
[3f74275]229void caps_task_free(task_t *task)
[e7ac23d0]230{
[05913fe7]231 hash_table_destroy(&task->cap_info->caps);
[fa3ed5b]232
233 if (task->cap_info->handles)
234 ra_arena_destroy(task->cap_info->handles);
235
[9e87562]236 free(task->cap_info);
237}
238
[6636fb19]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 */
[48bcf49]250bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
[9e87562]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) {
[05913fe7]257 cap_t *cap = list_get_instance(cur, cap_t, type_link);
[9e87562]258 done = cb(cap, arg);
259 if (!done)
260 break;
261 }
262 mutex_unlock(&task->cap_info->lock);
263
264 return done;
265}
266
[05913fe7]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;
[d24e987]278 link_initialize(&cap->kobj_link);
[05913fe7]279 link_initialize(&cap->type_link);
280}
281
[6636fb19]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 */
[48bcf49]291static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
[7e3826d9]292{
[9e87562]293 assert(mutex_locked(&task->cap_info->lock));
294
[bb97118]295 if ((cap_handle_raw(handle) < CAPS_START) ||
296 (cap_handle_raw(handle) > CAPS_LAST))
[7e3826d9]297 return NULL;
[05913fe7]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)
[7e3826d9]303 return NULL;
[05913fe7]304 return cap;
305}
306
[6636fb19]307/** Allocate new capability
308 *
309 * @param task Task for which to allocate the new capability.
310 *
[09d01f2]311 * @param[out] handle New capability handle on success.
312 *
[cde999a]313 * @return An error code in case of error.
[6636fb19]314 */
[b7fd2a0]315errno_t cap_alloc(task_t *task, cap_handle_t *handle)
[7e3826d9]316{
[9e87562]317 mutex_lock(&task->cap_info->lock);
[9fc776c7]318 cap_t *cap = slab_alloc(cap_cache, FRAME_ATOMIC);
[05913fe7]319 if (!cap) {
[9fc776c7]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;
[7e3826d9]328 }
[9fc776c7]329 cap_initialize(cap, task, (cap_handle_t) hbase);
330 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
[05913fe7]331
332 cap->state = CAP_STATE_ALLOCATED;
[09d01f2]333 *handle = cap->handle;
[9e87562]334 mutex_unlock(&task->cap_info->lock);
[7e3826d9]335
[09d01f2]336 return EOK;
[7e3826d9]337}
338
[6636fb19]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 */
[48bcf49]349void
350cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
[9e87562]351{
352 mutex_lock(&task->cap_info->lock);
[fa3ed5b]353 mutex_lock(&kobj->caps_list_lock);
[48bcf49]354 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
[9e87562]355 assert(cap);
[48bcf49]356 cap->state = CAP_STATE_PUBLISHED;
357 /* Hand over kobj's reference to cap */
358 cap->kobject = kobj;
[d24e987]359 list_append(&cap->kobj_link, &kobj->caps_list);
[05913fe7]360 list_append(&cap->type_link, &task->cap_info->type_list[kobj->type]);
[d24e987]361 mutex_unlock(&kobj->caps_list_lock);
[fa3ed5b]362 mutex_unlock(&task->cap_info->lock);
[d24e987]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;
[9e87562]371}
372
[6636fb19]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.
[d24e987]384 *
385 * @return Pointer and explicit reference to the kobject that was associated
386 * with the capability.
[6636fb19]387 */
[48bcf49]388kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
389{
[c1f68b0]390 kobject_t *kobj = NULL;
[48bcf49]391
392 mutex_lock(&task->cap_info->lock);
[c1f68b0]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;
[fa3ed5b]398
399 mutex_lock(&kobj->caps_list_lock);
[d24e987]400 cap_unpublish_unsafe(cap);
401 mutex_unlock(&kobj->caps_list_lock);
[c1f68b0]402 }
403 }
[9e87562]404 mutex_unlock(&task->cap_info->lock);
405
[48bcf49]406 return kobj;
[9e87562]407}
408
[d24e987]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);
[fa3ed5b]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
[d24e987]435 cap_unpublish_unsafe(cap);
436 /* Drop the reference for the unpublished capability */
437 kobject_put(kobj);
[fa3ed5b]438
[d24e987]439 mutex_unlock(&cap->task->cap_info->lock);
440 }
[fa3ed5b]441
[d24e987]442 mutex_unlock(&kobj->caps_list_lock);
443}
444
[c1f68b0]445/** Free allocated capability
[6636fb19]446 *
[c1f68b0]447 * @param task Task in which to free the capability.
448 * @param handle Capability handle.
[6636fb19]449 */
[c1f68b0]450void cap_free(task_t *task, cap_handle_t handle)
[7e3826d9]451{
[bb97118]452 assert(cap_handle_raw(handle) >= CAPS_START);
453 assert(cap_handle_raw(handle) <= CAPS_LAST);
[7e3826d9]454
[c1f68b0]455 mutex_lock(&task->cap_info->lock);
[05913fe7]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);
[bb97118]461 ra_free(task->cap_info->handles, cap_handle_raw(handle), 1);
[9e87562]462 mutex_unlock(&task->cap_info->lock);
[7e3826d9]463}
464
[e394c196]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
[6636fb19]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 */
[fc0de8c]481void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw)
[48bcf49]482{
[96368f56]483 refcount_init(&kobj->refcnt);
[d24e987]484
485 mutex_initialize(&kobj->caps_list_lock, MUTEX_PASSIVE);
486 list_initialize(&kobj->caps_list);
487
[48bcf49]488 kobj->type = type;
489 kobj->raw = raw;
490}
491
[6636fb19]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 */
[48bcf49]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;
[96368f56]512 refcount_up(&kobj->refcnt);
[48bcf49]513 }
514 }
515 mutex_unlock(&task->cap_info->lock);
516
517 return kobj;
518}
519
[6636fb19]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{
[96368f56]526 refcount_up(&kobj->refcnt);
[6636fb19]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 */
[48bcf49]536void kobject_put(kobject_t *kobj)
537{
[96368f56]538 if (refcount_down(&kobj->refcnt)) {
[fc0de8c]539 KOBJECT_OP(kobj)->destroy(kobj->raw);
[e394c196]540 kobject_free(kobj);
[48bcf49]541 }
542}
543
[7e3826d9]544/** @}
545 */
Note: See TracBrowser for help on using the repository browser.