Reformat.
[linux-2.6/linux-mips.git] / kernel / capability.c
blob518d167dbaeb339f9faf421a663534ddd955c8d6
1 /*
2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com>
7 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
8 */
10 #include <linux/mm.h>
11 #include <linux/security.h>
12 #include <asm/uaccess.h>
14 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
15 kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
18 * This global lock protects task->cap_* for all tasks including current.
19 * Locking rule: acquire this prior to tasklist_lock.
21 spinlock_t task_capability_lock = SPIN_LOCK_UNLOCKED;
24 * For sys_getproccap() and sys_setproccap(), any of the three
25 * capability set pointers may be NULL -- indicating that that set is
26 * uninteresting and/or not to be changed.
30 * sys_capget - get the capabilities of a given process.
32 asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
34 int ret = 0;
35 pid_t pid;
36 __u32 version;
37 task_t *target;
38 struct __user_cap_data_struct data;
40 if (get_user(version, &header->version))
41 return -EFAULT;
43 if (version != _LINUX_CAPABILITY_VERSION) {
44 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
45 return -EFAULT;
46 return -EINVAL;
49 if (get_user(pid, &header->pid))
50 return -EFAULT;
52 if (pid < 0)
53 return -EINVAL;
55 spin_lock(&task_capability_lock);
56 read_lock(&tasklist_lock);
58 if (pid && pid != current->pid) {
59 target = find_task_by_pid(pid);
60 if (!target) {
61 ret = -ESRCH;
62 goto out;
64 } else
65 target = current;
67 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
69 out:
70 read_unlock(&tasklist_lock);
71 spin_unlock(&task_capability_lock);
73 if (!ret && copy_to_user(dataptr, &data, sizeof data))
74 return -EFAULT;
76 return ret;
80 * cap_set_pg - set capabilities for all processes in a given process
81 * group. We call this holding task_capability_lock and tasklist_lock.
83 static inline void cap_set_pg(int pgrp, kernel_cap_t *effective,
84 kernel_cap_t *inheritable,
85 kernel_cap_t *permitted)
87 task_t *g, *target;
88 struct list_head *l;
89 struct pid *pid;
91 for_each_task_pid(pgrp, PIDTYPE_PGID, g, l, pid) {
92 target = g;
93 while_each_thread(g, target)
94 security_capset_set(target, effective, inheritable, permitted);
99 * cap_set_all - set capabilities for all processes other than init
100 * and self. We call this holding task_capability_lock and tasklist_lock.
102 static inline void cap_set_all(kernel_cap_t *effective,
103 kernel_cap_t *inheritable,
104 kernel_cap_t *permitted)
106 task_t *g, *target;
108 do_each_thread(g, target) {
109 if (target == current || target->pid == 1)
110 continue;
111 security_capset_set(target, effective, inheritable, permitted);
112 } while_each_thread(g, target);
116 * sys_capset - set capabilities for a given process, all processes, or all
117 * processes in a given process group.
119 * The restrictions on setting capabilities are specified as:
121 * [pid is for the 'target' task. 'current' is the calling task.]
123 * I: any raised capabilities must be a subset of the (old current) permitted
124 * P: any raised capabilities must be a subset of the (old current) permitted
125 * E: must be set to a subset of (new target) permitted
127 asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
129 kernel_cap_t inheritable, permitted, effective;
130 __u32 version;
131 task_t *target;
132 int ret;
133 pid_t pid;
135 if (get_user(version, &header->version))
136 return -EFAULT;
138 if (version != _LINUX_CAPABILITY_VERSION) {
139 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
140 return -EFAULT;
141 return -EINVAL;
144 if (get_user(pid, &header->pid))
145 return -EFAULT;
147 if (pid && !capable(CAP_SETPCAP))
148 return -EPERM;
150 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
151 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
152 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
153 return -EFAULT;
155 spin_lock(&task_capability_lock);
156 read_lock(&tasklist_lock);
158 if (pid > 0 && pid != current->pid) {
159 target = find_task_by_pid(pid);
160 if (!target) {
161 ret = -ESRCH;
162 goto out;
164 } else
165 target = current;
167 ret = -EPERM;
169 if (security_capset_check(target, &effective, &inheritable, &permitted))
170 goto out;
172 if (!cap_issubset(inheritable, cap_combine(target->cap_inheritable,
173 current->cap_permitted)))
174 goto out;
176 /* verify restrictions on target's new Permitted set */
177 if (!cap_issubset(permitted, cap_combine(target->cap_permitted,
178 current->cap_permitted)))
179 goto out;
181 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
182 if (!cap_issubset(effective, permitted))
183 goto out;
185 ret = 0;
187 /* having verified that the proposed changes are legal,
188 we now put them into effect. */
189 if (pid < 0) {
190 if (pid == -1) /* all procs other than current and init */
191 cap_set_all(&effective, &inheritable, &permitted);
193 else /* all procs in process group */
194 cap_set_pg(-pid, &effective, &inheritable, &permitted);
195 } else {
196 security_capset_set(target, &effective, &inheritable, &permitted);
199 out:
200 read_unlock(&tasklist_lock);
201 spin_unlock(&task_capability_lock);
203 return ret;