source: mainline/kernel/generic/src/cap/cap.c@ 162ad53

Last change on this file since 162ad53 was 162ad53, checked in by GitHub <noreply@…>, 26 hours ago

Merge 455241b37bedd3719ed3b5b025fdf26f44fd565b into 5caad1d4a9774280b120ed9f9da51f4bb6f1f4bf

  • Property mode set to 100644
File size: 15.4 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;
99
100kobject_ops_t *kobject_ops[KOBJECT_TYPE_MAX] = {
101 [KOBJECT_TYPE_CALL] = &call_kobject_ops,
102 [KOBJECT_TYPE_IRQ] = &irq_kobject_ops,
103 [KOBJECT_TYPE_PHONE] = &phone_kobject_ops,
104 [KOBJECT_TYPE_WAITQ] = &waitq_kobject_ops
105};
106
107static size_t caps_hash(const ht_link_t *item)
108{
109 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
110 return hash_mix(cap_handle_raw(cap->handle));
111}
112
113static size_t caps_key_hash(const void *key)
114{
115 const cap_handle_t *handle = key;
116 return hash_mix(cap_handle_raw(*handle));
117}
118
119static bool caps_key_equal(const void *key, size_t hash, const ht_link_t *item)
120{
121 const cap_handle_t *handle = key;
122 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
123 return *handle == cap->handle;
124}
125
126static void caps_remove_callback(ht_link_t *item)
127{
128 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
129
130 if (cap->kobject) {
131 kobject_t *kobj = cap->kobject;
132
133 mutex_lock(&kobj->caps_list_lock);
134 cap->kobject = NULL;
135 list_remove(&cap->kobj_link);
136 mutex_unlock(&kobj->caps_list_lock);
137
138 kobject_put(kobj);
139 }
140
141 list_remove(&cap->type_link);
142
143 slab_free(cap_cache, cap);
144}
145
146static const hash_table_ops_t caps_ops = {
147 .hash = caps_hash,
148 .key_hash = caps_key_hash,
149 .key_equal = caps_key_equal,
150 .remove_callback = caps_remove_callback,
151};
152
153void caps_init(void)
154{
155 cap_cache = slab_cache_create("cap_t", sizeof(cap_t), 0, NULL,
156 NULL, 0);
157}
158
159/** Allocate the capability info structure
160 *
161 * @param task Task for which to allocate the info structure.
162 */
163errno_t caps_task_alloc(task_t *task)
164{
165 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t));
166 if (!task->cap_info)
167 return ENOMEM;
168
169 if (!hash_table_create(&task->cap_info->caps, 0, 0, &caps_ops)) {
170 free(task->cap_info);
171 return ENOMEM;
172 }
173
174 mutex_initialize(&task->cap_info->lock, MUTEX_RECURSIVE);
175
176 task->cap_info->handles = NULL;
177
178 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
179 list_initialize(&task->cap_info->type_list[t]);
180
181 return EOK;
182}
183
184/** Initialize the capability info structure
185 *
186 * @param task Task for which to initialize the info structure.
187 */
188errno_t caps_task_init(task_t *task)
189{
190 assert(task->cap_info);
191 assert(!task->cap_info->handles);
192
193 task->cap_info->handles = ra_arena_create();
194 if (!task->cap_info->handles)
195 return ENOMEM;
196
197 if (!ra_span_add(task->cap_info->handles, CAPS_START, CAPS_SIZE)) {
198 ra_arena_destroy(task->cap_info->handles);
199 return ENOMEM;
200 }
201
202 return EOK;
203}
204
205void caps_task_clear(task_t *task)
206{
207 mutex_lock(&task->cap_info->lock);
208
209 hash_table_clear(&task->cap_info->caps);
210
211 if (task->cap_info->handles) {
212 ra_arena_destroy(task->cap_info->handles);
213 task->cap_info->handles = NULL;
214 }
215
216 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
217 list_initialize(&task->cap_info->type_list[t]);
218
219 mutex_unlock(&task->cap_info->lock);
220}
221
222/** Deallocate the capability info structure
223 *
224 * @param task Task from which to deallocate the info structure.
225 */
226void caps_task_free(task_t *task)
227{
228 hash_table_destroy(&task->cap_info->caps);
229
230 if (task->cap_info->handles)
231 ra_arena_destroy(task->cap_info->handles);
232
233 free(task->cap_info);
234}
235
236/** Invoke callback function on task's capabilites of given type
237 *
238 * @param task Task where the invocation should take place.
239 * @param type Kernel object type of the task's capabilities that will be
240 * subject to the callback invocation.
241 * @param cb Callback function.
242 * @param arg Argument for the callback function.
243 *
244 * @return True if the callback was called on all matching capabilities.
245 * @return False if the callback was applied only partially.
246 */
247bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
248 bool (*cb)(cap_t *, void *), void *arg)
249{
250 bool done = true;
251
252 mutex_lock(&task->cap_info->lock);
253 list_foreach_safe(task->cap_info->type_list[type], cur, next) {
254 cap_t *cap = list_get_instance(cur, cap_t, type_link);
255 done = cb(cap, arg);
256 if (!done)
257 break;
258 }
259 mutex_unlock(&task->cap_info->lock);
260
261 return done;
262}
263
264/** Initialize capability and associate it with its handle
265 *
266 * @param cap Address of the capability.
267 * @param task Backling to the owning task.
268 * @param handle Capability handle.
269 */
270static void cap_initialize(cap_t *cap, task_t *task, cap_handle_t handle)
271{
272 cap->state = CAP_STATE_FREE;
273 cap->task = task;
274 cap->handle = handle;
275 link_initialize(&cap->kobj_link);
276 link_initialize(&cap->type_link);
277}
278
279/** Get capability using capability handle
280 *
281 * @param task Task whose capability to get.
282 * @param handle Capability handle of the desired capability.
283 * @param state State in which the capability must be.
284 *
285 * @return Address of the desired capability if it exists and its state matches.
286 * @return NULL if no such capability exists or it's in a different state.
287 */
288static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
289{
290 assert(mutex_locked(&task->cap_info->lock));
291
292 if ((cap_handle_raw(handle) < CAPS_START) ||
293 (cap_handle_raw(handle) > CAPS_LAST))
294 return NULL;
295 ht_link_t *link = hash_table_find(&task->cap_info->caps, &handle);
296 if (!link)
297 return NULL;
298 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
299 if (cap->state != state)
300 return NULL;
301 return cap;
302}
303
304/** Allocate new capability
305 *
306 * @param task Task for which to allocate the new capability.
307 *
308 * @param[out] handle New capability handle on success.
309 *
310 * @return An error code in case of error.
311 */
312errno_t cap_alloc(task_t *task, cap_handle_t *handle)
313{
314 mutex_lock(&task->cap_info->lock);
315 cap_t *cap = slab_alloc(cap_cache, FRAME_ATOMIC);
316 if (!cap) {
317 mutex_unlock(&task->cap_info->lock);
318 return ENOMEM;
319 }
320 uintptr_t hbase;
321 if (!ra_alloc(task->cap_info->handles, 1, 1, &hbase)) {
322 slab_free(cap_cache, cap);
323 mutex_unlock(&task->cap_info->lock);
324 return ENOMEM;
325 }
326 cap_initialize(cap, task, (cap_handle_t) hbase);
327 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
328
329 cap->state = CAP_STATE_ALLOCATED;
330 *handle = cap->handle;
331 mutex_unlock(&task->cap_info->lock);
332
333 return EOK;
334}
335
336/** Publish allocated capability
337 *
338 * The kernel object is moved into the capability. In other words, its reference
339 * is handed over to the capability. Once published, userspace can access and
340 * manipulate the capability.
341 *
342 * @param task Task in which to publish the capability.
343 * @param handle Capability handle.
344 * @param kobj Kernel object.
345 */
346void
347cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
348{
349 mutex_lock(&task->cap_info->lock);
350 mutex_lock(&kobj->caps_list_lock);
351 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
352 assert(cap);
353 cap->state = CAP_STATE_PUBLISHED;
354 /* Hand over kobj's reference to cap */
355 cap->kobject = kobj;
356 list_append(&cap->kobj_link, &kobj->caps_list);
357 list_append(&cap->type_link, &task->cap_info->type_list[kobj->type]);
358 mutex_unlock(&kobj->caps_list_lock);
359 mutex_unlock(&task->cap_info->lock);
360}
361
362static void cap_unpublish_unsafe(cap_t *cap)
363{
364 cap->kobject = NULL;
365 list_remove(&cap->kobj_link);
366 list_remove(&cap->type_link);
367 cap->state = CAP_STATE_ALLOCATED;
368}
369
370/** Unpublish published capability
371 *
372 * The kernel object is moved out of the capability. In other words, the
373 * capability's reference to the objects is handed over to the kernel object
374 * pointer returned by this function. Once unpublished, the capability does not
375 * refer to any kernel object anymore.
376 *
377 * @param task Task in which to unpublish the capability.
378 * @param handle Capability handle.
379 * @param type Kernel object type of the object associated with the
380 * capability.
381 *
382 * @return Pointer and explicit reference to the kobject that was associated
383 * with the capability.
384 */
385kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
386{
387 kobject_t *kobj = NULL;
388
389 mutex_lock(&task->cap_info->lock);
390 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
391 if (cap) {
392 if (cap->kobject->type == type) {
393 /* Hand over cap's reference to kobj */
394 kobj = cap->kobject;
395
396 mutex_lock(&kobj->caps_list_lock);
397 cap_unpublish_unsafe(cap);
398 mutex_unlock(&kobj->caps_list_lock);
399 }
400 }
401 mutex_unlock(&task->cap_info->lock);
402
403 return kobj;
404}
405
406/** Revoke access to kobject from all existing capabilities
407 *
408 * All published capabilities associated with the kobject are unpublished (i.e.
409 * their new state is set to CAP_STATE_ALLOCATED) and no longer point to the
410 * kobject. Kobject's reference count is decreased accordingly.
411 *
412 * Note that the caller is supposed to hold an explicit reference to the kobject
413 * so that the kobject is guaranteed to exist when this function returns.
414 *
415 * @param kobj Pointer and explicit reference to the kobject capabilities of
416 * which are about to be unpublished.
417 */
418void cap_revoke(kobject_t *kobj)
419{
420 mutex_lock(&kobj->caps_list_lock);
421
422 while (!list_empty(&kobj->caps_list)) {
423 cap_t *cap = list_get_instance(list_first(&kobj->caps_list), cap_t, kobj_link);
424
425 /* We're trying to acquire the two locks in reverse order. */
426 if (mutex_trylock(&cap->task->cap_info->lock) != EOK) {
427 mutex_unlock(&kobj->caps_list_lock);
428 mutex_lock(&kobj->caps_list_lock);
429 continue;
430 }
431
432 cap_unpublish_unsafe(cap);
433 /* Drop the reference for the unpublished capability */
434 kobject_put(kobj);
435
436 mutex_unlock(&cap->task->cap_info->lock);
437 }
438
439 mutex_unlock(&kobj->caps_list_lock);
440}
441
442/** Free allocated capability
443 *
444 * @param task Task in which to free the capability.
445 * @param handle Capability handle.
446 */
447void cap_free(task_t *task, cap_handle_t handle)
448{
449 assert(cap_handle_raw(handle) >= CAPS_START);
450 assert(cap_handle_raw(handle) <= CAPS_LAST);
451
452 mutex_lock(&task->cap_info->lock);
453 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
454
455 assert(cap);
456
457 hash_table_remove_item(&task->cap_info->caps, &cap->caps_link);
458 ra_free(task->cap_info->handles, cap_handle_raw(handle), 1);
459 mutex_unlock(&task->cap_info->lock);
460}
461
462/** Initialize kernel object
463 *
464 * @param kobj Kernel object to initialize.
465 * @param type Type of the kernel object.
466 */
467void kobject_initialize(kobject_t *kobj, kobject_type_t type)
468{
469 refcount_init(&kobj->refcnt);
470
471 mutex_initialize(&kobj->caps_list_lock, MUTEX_PASSIVE);
472 list_initialize(&kobj->caps_list);
473
474 kobj->type = type;
475}
476
477/** Get new reference to kernel object from capability
478 *
479 * @param task Task from which to get the reference.
480 * @param handle Capability handle.
481 * @param type Kernel object type of the object associated with the
482 * capability referenced by handle.
483 *
484 * @return Kernel object with incremented reference count on success.
485 * @return NULL if there is no matching capability or kernel object.
486 */
487kobject_t *
488kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
489{
490 kobject_t *kobj = NULL;
491
492 mutex_lock(&task->cap_info->lock);
493 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
494 if (cap) {
495 if (cap->kobject->type == type) {
496 kobj = cap->kobject;
497 refcount_up(&kobj->refcnt);
498 }
499 }
500 mutex_unlock(&task->cap_info->lock);
501
502 return kobj;
503}
504
505/** Record new reference
506 *
507 * @param kobj Kernel object from which the new reference is created.
508 */
509void kobject_add_ref(kobject_t *kobj)
510{
511 refcount_up(&kobj->refcnt);
512}
513
514/** Drop reference to kernel object
515 *
516 * The encapsulated object and the kobject_t wrapper are both destroyed when the
517 * last reference is dropped.
518 *
519 * @param kobj Kernel object whose reference to drop.
520 */
521void kobject_put(kobject_t *kobj)
522{
523 if (refcount_down(&kobj->refcnt)) {
524 KOBJECT_OP(kobj)->destroy(kobj);
525 }
526}
527
528/** @}
529 */
Note: See TracBrowser for help on using the repository browser.