Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/capability.c |
| 3 | * |
| 4 | * Copyright (C) 1997 Andrew Main <zefram@fysh.org> |
| 5 | * |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 6 | * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net> |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/syscalls.h> |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 15 | #include <linux/pid_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
| 19 | * This lock protects task->cap_* for all tasks including current. |
| 20 | * Locking rule: acquire this prior to tasklist_lock. |
| 21 | */ |
| 22 | static DEFINE_SPINLOCK(task_capability_lock); |
| 23 | |
| 24 | /* |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 25 | * Leveraged for setting/resetting capabilities |
| 26 | */ |
| 27 | |
| 28 | const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET; |
| 29 | const kernel_cap_t __cap_full_set = CAP_FULL_SET; |
| 30 | const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET; |
| 31 | |
| 32 | EXPORT_SYMBOL(__cap_empty_set); |
| 33 | EXPORT_SYMBOL(__cap_full_set); |
| 34 | EXPORT_SYMBOL(__cap_init_eff_set); |
| 35 | |
| 36 | /* |
| 37 | * More recent versions of libcap are available from: |
| 38 | * |
| 39 | * http://www.kernel.org/pub/linux/libs/security/linux-privs/ |
| 40 | */ |
| 41 | |
| 42 | static void warn_legacy_capability_use(void) |
| 43 | { |
| 44 | static int warned; |
| 45 | if (!warned) { |
| 46 | char name[sizeof(current->comm)]; |
| 47 | |
| 48 | printk(KERN_INFO "warning: `%s' uses 32-bit capabilities" |
| 49 | " (legacy support in use)\n", |
| 50 | get_task_comm(name, current)); |
| 51 | warned = 1; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * For sys_getproccap() and sys_setproccap(), any of the three |
| 57 | * capability set pointers may be NULL -- indicating that that set is |
| 58 | * uninteresting and/or not to be changed. |
| 59 | */ |
| 60 | |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 61 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * sys_capget - get the capabilities of a given process. |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 63 | * @header: pointer to struct that contains capability version and |
| 64 | * target pid data |
| 65 | * @dataptr: pointer to struct that contains the effective, permitted, |
| 66 | * and inheritable capabilities that are returned |
| 67 | * |
| 68 | * Returns 0 on success and < 0 on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | */ |
| 70 | asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr) |
| 71 | { |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 72 | int ret = 0; |
| 73 | pid_t pid; |
| 74 | __u32 version; |
| 75 | struct task_struct *target; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 76 | unsigned tocopy; |
| 77 | kernel_cap_t pE, pI, pP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 79 | if (get_user(version, &header->version)) |
| 80 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 82 | switch (version) { |
| 83 | case _LINUX_CAPABILITY_VERSION_1: |
| 84 | warn_legacy_capability_use(); |
| 85 | tocopy = _LINUX_CAPABILITY_U32S_1; |
| 86 | break; |
| 87 | case _LINUX_CAPABILITY_VERSION_2: |
| 88 | tocopy = _LINUX_CAPABILITY_U32S_2; |
| 89 | break; |
| 90 | default: |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 91 | if (put_user(_LINUX_CAPABILITY_VERSION, &header->version)) |
| 92 | return -EFAULT; |
| 93 | return -EINVAL; |
| 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 96 | if (get_user(pid, &header->pid)) |
| 97 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 99 | if (pid < 0) |
| 100 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 102 | spin_lock(&task_capability_lock); |
| 103 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 105 | if (pid && pid != task_pid_vnr(current)) { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 106 | target = find_task_by_vpid(pid); |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 107 | if (!target) { |
| 108 | ret = -ESRCH; |
| 109 | goto out; |
| 110 | } |
| 111 | } else |
| 112 | target = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 114 | ret = security_capget(target, &pE, &pI, &pP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | out: |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 117 | read_unlock(&tasklist_lock); |
| 118 | spin_unlock(&task_capability_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 120 | if (!ret) { |
| 121 | struct __user_cap_data_struct kdata[_LINUX_CAPABILITY_U32S]; |
| 122 | unsigned i; |
| 123 | |
| 124 | for (i = 0; i < tocopy; i++) { |
| 125 | kdata[i].effective = pE.cap[i]; |
| 126 | kdata[i].permitted = pP.cap[i]; |
| 127 | kdata[i].inheritable = pI.cap[i]; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Note, in the case, tocopy < _LINUX_CAPABILITY_U32S, |
| 132 | * we silently drop the upper capabilities here. This |
| 133 | * has the effect of making older libcap |
| 134 | * implementations implicitly drop upper capability |
| 135 | * bits when they perform a: capget/modify/capset |
| 136 | * sequence. |
| 137 | * |
| 138 | * This behavior is considered fail-safe |
| 139 | * behavior. Upgrading the application to a newer |
| 140 | * version of libcap will enable access to the newer |
| 141 | * capabilities. |
| 142 | * |
| 143 | * An alternative would be to return an error here |
| 144 | * (-ERANGE), but that causes legacy applications to |
| 145 | * unexpectidly fail; the capget/modify/capset aborts |
| 146 | * before modification is attempted and the application |
| 147 | * fails. |
| 148 | */ |
| 149 | |
| 150 | if (copy_to_user(dataptr, kdata, tocopy |
| 151 | * sizeof(struct __user_cap_data_struct))) { |
| 152 | return -EFAULT; |
| 153 | } |
| 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 156 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * cap_set_pg - set capabilities for all processes in a given process |
| 161 | * group. We call this holding task_capability_lock and tasklist_lock. |
| 162 | */ |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 163 | static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | kernel_cap_t *inheritable, |
| 165 | kernel_cap_t *permitted) |
| 166 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 167 | struct task_struct *g, *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | int ret = -EPERM; |
| 169 | int found = 0; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 170 | struct pid *pgrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Pavel Emelyanov | 8990571 | 2007-10-18 23:40:19 -0700 | [diff] [blame] | 172 | pgrp = find_vpid(pgrp_nr); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 173 | do_each_pid_task(pgrp, PIDTYPE_PGID, g) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | target = g; |
| 175 | while_each_thread(g, target) { |
| 176 | if (!security_capset_check(target, effective, |
| 177 | inheritable, |
| 178 | permitted)) { |
| 179 | security_capset_set(target, effective, |
| 180 | inheritable, |
| 181 | permitted); |
| 182 | ret = 0; |
| 183 | } |
| 184 | found = 1; |
| 185 | } |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 186 | } while_each_pid_task(pgrp, PIDTYPE_PGID, g); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | if (!found) |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 189 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * cap_set_all - set capabilities for all processes other than init |
| 195 | * and self. We call this holding task_capability_lock and tasklist_lock. |
| 196 | */ |
| 197 | static inline int cap_set_all(kernel_cap_t *effective, |
| 198 | kernel_cap_t *inheritable, |
| 199 | kernel_cap_t *permitted) |
| 200 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 201 | struct task_struct *g, *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | int ret = -EPERM; |
| 203 | int found = 0; |
| 204 | |
| 205 | do_each_thread(g, target) { |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 206 | if (target == current || is_container_init(target->group_leader)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | continue; |
| 208 | found = 1; |
| 209 | if (security_capset_check(target, effective, inheritable, |
| 210 | permitted)) |
| 211 | continue; |
| 212 | ret = 0; |
| 213 | security_capset_set(target, effective, inheritable, permitted); |
| 214 | } while_each_thread(g, target); |
| 215 | |
| 216 | if (!found) |
| 217 | ret = 0; |
| 218 | return ret; |
| 219 | } |
| 220 | |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 221 | /** |
| 222 | * sys_capset - set capabilities for a process or a group of processes |
| 223 | * @header: pointer to struct that contains capability version and |
| 224 | * target pid data |
| 225 | * @data: pointer to struct that contains the effective, permitted, |
| 226 | * and inheritable capabilities |
| 227 | * |
| 228 | * Set capabilities for a given process, all processes, or all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | * processes in a given process group. |
| 230 | * |
| 231 | * The restrictions on setting capabilities are specified as: |
| 232 | * |
| 233 | * [pid is for the 'target' task. 'current' is the calling task.] |
| 234 | * |
| 235 | * I: any raised capabilities must be a subset of the (old current) permitted |
| 236 | * P: any raised capabilities must be a subset of the (old current) permitted |
| 237 | * E: must be set to a subset of (new target) permitted |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 238 | * |
| 239 | * Returns 0 on success and < 0 on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
| 241 | asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) |
| 242 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 243 | struct __user_cap_data_struct kdata[_LINUX_CAPABILITY_U32S]; |
| 244 | unsigned i, tocopy; |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 245 | kernel_cap_t inheritable, permitted, effective; |
| 246 | __u32 version; |
| 247 | struct task_struct *target; |
| 248 | int ret; |
| 249 | pid_t pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 251 | if (get_user(version, &header->version)) |
| 252 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 254 | switch (version) { |
| 255 | case _LINUX_CAPABILITY_VERSION_1: |
| 256 | warn_legacy_capability_use(); |
| 257 | tocopy = _LINUX_CAPABILITY_U32S_1; |
| 258 | break; |
| 259 | case _LINUX_CAPABILITY_VERSION_2: |
| 260 | tocopy = _LINUX_CAPABILITY_U32S_2; |
| 261 | break; |
| 262 | default: |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 263 | if (put_user(_LINUX_CAPABILITY_VERSION, &header->version)) |
| 264 | return -EFAULT; |
| 265 | return -EINVAL; |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 268 | if (get_user(pid, &header->pid)) |
| 269 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 271 | if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP)) |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 272 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 274 | if (copy_from_user(&kdata, data, tocopy |
| 275 | * sizeof(struct __user_cap_data_struct))) { |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 276 | return -EFAULT; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | for (i = 0; i < tocopy; i++) { |
| 280 | effective.cap[i] = kdata[i].effective; |
| 281 | permitted.cap[i] = kdata[i].permitted; |
| 282 | inheritable.cap[i] = kdata[i].inheritable; |
| 283 | } |
| 284 | while (i < _LINUX_CAPABILITY_U32S) { |
| 285 | effective.cap[i] = 0; |
| 286 | permitted.cap[i] = 0; |
| 287 | inheritable.cap[i] = 0; |
| 288 | i++; |
| 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 291 | spin_lock(&task_capability_lock); |
| 292 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 294 | if (pid > 0 && pid != task_pid_vnr(current)) { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 295 | target = find_task_by_vpid(pid); |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 296 | if (!target) { |
| 297 | ret = -ESRCH; |
| 298 | goto out; |
| 299 | } |
| 300 | } else |
| 301 | target = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 303 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 305 | /* having verified that the proposed changes are legal, |
| 306 | we now put them into effect. */ |
| 307 | if (pid < 0) { |
| 308 | if (pid == -1) /* all procs other than current and init */ |
| 309 | ret = cap_set_all(&effective, &inheritable, &permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 311 | else /* all procs in process group */ |
| 312 | ret = cap_set_pg(-pid, &effective, &inheritable, |
| 313 | &permitted); |
| 314 | } else { |
| 315 | ret = security_capset_check(target, &effective, &inheritable, |
| 316 | &permitted); |
| 317 | if (!ret) |
| 318 | security_capset_set(target, &effective, &inheritable, |
| 319 | &permitted); |
| 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | out: |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 323 | read_unlock(&tasklist_lock); |
| 324 | spin_unlock(&task_capability_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 326 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
Chris Wright | 12b5989 | 2006-03-25 03:07:41 -0800 | [diff] [blame] | 328 | |
| 329 | int __capable(struct task_struct *t, int cap) |
| 330 | { |
| 331 | if (security_capable(t, cap) == 0) { |
| 332 | t->flags |= PF_SUPERPRIV; |
| 333 | return 1; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
Chris Wright | 12b5989 | 2006-03-25 03:07:41 -0800 | [diff] [blame] | 337 | |
| 338 | int capable(int cap) |
| 339 | { |
| 340 | return __capable(current, cap); |
| 341 | } |
| 342 | EXPORT_SYMBOL(capable); |