source: mainline/kernel/generic/src/security/cap.c@ da1bafb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since da1bafb was da1bafb, checked in by Martin Decky <martin@…>, 15 years ago

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[1077d91]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[1077d91]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
[da1bafb]29/** @addtogroup generic
[b45c443]30 * @{
31 */
32
[cf26ba9]33/**
[da1bafb]34 * @file cap.c
35 * @brief Capabilities control.
[cf26ba9]36 *
37 * @see cap.h
38 */
[da1bafb]39
[1077d91]40#include <security/cap.h>
41#include <proc/task.h>
42#include <synch/spinlock.h>
[2bb8648]43#include <syscall/sysarg64.h>
44#include <syscall/copy.h>
[1077d91]45#include <arch.h>
[2bb8648]46#include <errno.h>
[1077d91]47
48/** Set capabilities.
49 *
[da1bafb]50 * @param task Task whose capabilities are to be changed.
[1077d91]51 * @param caps New set of capabilities.
[da1bafb]52 *
[1077d91]53 */
[da1bafb]54void cap_set(task_t *task, cap_t caps)
[1077d91]55{
[da1bafb]56 irq_spinlock_lock(&task->lock, true);
57 task->capabilities = caps;
58 irq_spinlock_unlock(&task->lock, true);
[1077d91]59}
60
61/** Get capabilities.
62 *
[da1bafb]63 * @param task Task whose capabilities are to be returned.
64 *
[1077d91]65 * @return Task's capabilities.
[da1bafb]66 *
[1077d91]67 */
[da1bafb]68cap_t cap_get(task_t *task)
[1077d91]69{
[da1bafb]70 irq_spinlock_lock(&task->lock, true);
71 cap_t caps = task->capabilities;
72 irq_spinlock_unlock(&task->lock, true);
[1077d91]73
74 return caps;
75}
[2bb8648]76
77/** Grant capabilities to a task.
78 *
79 * The calling task must have the CAP_CAP capability.
80 *
81 * @param uspace_taskid_arg Userspace structure holding destination task ID.
82 * @param caps Capabilities to grant.
83 *
84 * @return Zero on success or an error code from @ref errno.h.
[da1bafb]85 *
[2bb8648]86 */
[7f1c620]87unative_t sys_cap_grant(sysarg64_t *uspace_taskid_arg, cap_t caps)
[2bb8648]88{
89 if (!(cap_get(TASK) & CAP_CAP))
[7f1c620]90 return (unative_t) EPERM;
[2bb8648]91
[da1bafb]92 sysarg64_t taskid_arg;
93 int rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
[2bb8648]94 if (rc != 0)
[7f1c620]95 return (unative_t) rc;
[da1bafb]96
97 irq_spinlock_lock(&tasks_lock, true);
98 task_t *task = task_find_by_id((task_id_t) taskid_arg.value);
99
100 if ((!task) || (!context_check(CONTEXT, task->context))) {
101 irq_spinlock_unlock(&tasks_lock, true);
[7f1c620]102 return (unative_t) ENOENT;
[2bb8648]103 }
104
[da1bafb]105 irq_spinlock_lock(&task->lock, false);
106 task->capabilities |= caps;
107 irq_spinlock_unlock(&task->lock, false);
[2569ec90]108
[da1bafb]109 irq_spinlock_unlock(&tasks_lock, true);
[2bb8648]110 return 0;
111}
112
113/** Revoke capabilities from a task.
114 *
115 * The calling task must have the CAP_CAP capability or the caller must
116 * attempt to revoke capabilities from itself.
117 *
118 * @param uspace_taskid_arg Userspace structure holding destination task ID.
119 * @param caps Capabilities to revoke.
120 *
121 * @return Zero on success or an error code from @ref errno.h.
[da1bafb]122 *
[2bb8648]123 */
[7f1c620]124unative_t sys_cap_revoke(sysarg64_t *uspace_taskid_arg, cap_t caps)
[2bb8648]125{
126 sysarg64_t taskid_arg;
[da1bafb]127 int rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
[2bb8648]128 if (rc != 0)
[7f1c620]129 return (unative_t) rc;
[da1bafb]130
131 irq_spinlock_lock(&tasks_lock, true);
132
133 task_t *task = task_find_by_id((task_id_t) taskid_arg.value);
134 if ((!task) || (!context_check(CONTEXT, task->context))) {
135 irq_spinlock_unlock(&tasks_lock, true);
[7f1c620]136 return (unative_t) ENOENT;
[2bb8648]137 }
[da1bafb]138
[2bb8648]139 /*
140 * Revoking capabilities is different from granting them in that
141 * a task can revoke capabilities from itself even if it
142 * doesn't have CAP_CAP.
143 */
[da1bafb]144 irq_spinlock_unlock(&TASK->lock, false);
145
146 if ((!(TASK->capabilities & CAP_CAP)) || (task != TASK)) {
147 irq_spinlock_unlock(&TASK->lock, false);
148 irq_spinlock_unlock(&tasks_lock, true);
[7f1c620]149 return (unative_t) EPERM;
[2bb8648]150 }
151
[da1bafb]152 task->capabilities &= ~caps;
153 irq_spinlock_unlock(&TASK->lock, false);
154
155 irq_spinlock_unlock(&tasks_lock, true);
[2bb8648]156 return 0;
157}
[b45c443]158
[06e1e95]159/** @}
[b45c443]160 */
Note: See TracBrowser for help on using the repository browser.