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
Line 
1/*
2 * Copyright (c) 2006 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 generic
30 * @{
31 */
32
33/**
34 * @file cap.c
35 * @brief Capabilities control.
36 *
37 * @see cap.h
38 */
39
40#include <security/cap.h>
41#include <proc/task.h>
42#include <synch/spinlock.h>
43#include <syscall/sysarg64.h>
44#include <syscall/copy.h>
45#include <arch.h>
46#include <errno.h>
47
48/** Set capabilities.
49 *
50 * @param task Task whose capabilities are to be changed.
51 * @param caps New set of capabilities.
52 *
53 */
54void cap_set(task_t *task, cap_t caps)
55{
56 irq_spinlock_lock(&task->lock, true);
57 task->capabilities = caps;
58 irq_spinlock_unlock(&task->lock, true);
59}
60
61/** Get capabilities.
62 *
63 * @param task Task whose capabilities are to be returned.
64 *
65 * @return Task's capabilities.
66 *
67 */
68cap_t cap_get(task_t *task)
69{
70 irq_spinlock_lock(&task->lock, true);
71 cap_t caps = task->capabilities;
72 irq_spinlock_unlock(&task->lock, true);
73
74 return caps;
75}
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.
85 *
86 */
87unative_t sys_cap_grant(sysarg64_t *uspace_taskid_arg, cap_t caps)
88{
89 if (!(cap_get(TASK) & CAP_CAP))
90 return (unative_t) EPERM;
91
92 sysarg64_t taskid_arg;
93 int rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
94 if (rc != 0)
95 return (unative_t) rc;
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);
102 return (unative_t) ENOENT;
103 }
104
105 irq_spinlock_lock(&task->lock, false);
106 task->capabilities |= caps;
107 irq_spinlock_unlock(&task->lock, false);
108
109 irq_spinlock_unlock(&tasks_lock, true);
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.
122 *
123 */
124unative_t sys_cap_revoke(sysarg64_t *uspace_taskid_arg, cap_t caps)
125{
126 sysarg64_t taskid_arg;
127 int rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
128 if (rc != 0)
129 return (unative_t) rc;
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);
136 return (unative_t) ENOENT;
137 }
138
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 */
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);
149 return (unative_t) EPERM;
150 }
151
152 task->capabilities &= ~caps;
153 irq_spinlock_unlock(&TASK->lock, false);
154
155 irq_spinlock_unlock(&tasks_lock, true);
156 return 0;
157}
158
159/** @}
160 */
Note: See TracBrowser for help on using the repository browser.