diff options
Diffstat (limited to 'kernel/capability.c')
| -rw-r--r-- | kernel/capability.c | 286 |
1 files changed, 47 insertions, 239 deletions
diff --git a/kernel/capability.c b/kernel/capability.c index 33e51e78c2d8..c598d9d5be4f 100644 --- a/kernel/capability.c +++ b/kernel/capability.c | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net> | 7 | * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net> |
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| 10 | #include <linux/audit.h> | ||
| 10 | #include <linux/capability.h> | 11 | #include <linux/capability.h> |
| 11 | #include <linux/mm.h> | 12 | #include <linux/mm.h> |
| 12 | #include <linux/module.h> | 13 | #include <linux/module.h> |
| @@ -14,12 +15,7 @@ | |||
| 14 | #include <linux/syscalls.h> | 15 | #include <linux/syscalls.h> |
| 15 | #include <linux/pid_namespace.h> | 16 | #include <linux/pid_namespace.h> |
| 16 | #include <asm/uaccess.h> | 17 | #include <asm/uaccess.h> |
| 17 | 18 | #include "cred-internals.h" | |
| 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 | 19 | ||
| 24 | /* | 20 | /* |
| 25 | * Leveraged for setting/resetting capabilities | 21 | * Leveraged for setting/resetting capabilities |
| @@ -33,6 +29,17 @@ EXPORT_SYMBOL(__cap_empty_set); | |||
| 33 | EXPORT_SYMBOL(__cap_full_set); | 29 | EXPORT_SYMBOL(__cap_full_set); |
| 34 | EXPORT_SYMBOL(__cap_init_eff_set); | 30 | EXPORT_SYMBOL(__cap_init_eff_set); |
| 35 | 31 | ||
| 32 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES | ||
| 33 | int file_caps_enabled = 1; | ||
| 34 | |||
| 35 | static int __init file_caps_disable(char *str) | ||
| 36 | { | ||
| 37 | file_caps_enabled = 0; | ||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | __setup("no_file_caps", file_caps_disable); | ||
| 41 | #endif | ||
| 42 | |||
| 36 | /* | 43 | /* |
| 37 | * More recent versions of libcap are available from: | 44 | * More recent versions of libcap are available from: |
| 38 | * | 45 | * |
| @@ -115,167 +122,12 @@ static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy) | |||
| 115 | return 0; | 122 | return 0; |
| 116 | } | 123 | } |
| 117 | 124 | ||
| 118 | #ifndef CONFIG_SECURITY_FILE_CAPABILITIES | ||
| 119 | |||
| 120 | /* | 125 | /* |
| 121 | * Without filesystem capability support, we nominally support one process | 126 | * The only thing that can change the capabilities of the current |
| 122 | * setting the capabilities of another | 127 | * process is the current process. As such, we can't be in this code |
| 123 | */ | 128 | * at the same time as we are in the process of setting capabilities |
| 124 | static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, | 129 | * in this process. The net result is that we can limit our use of |
| 125 | kernel_cap_t *pIp, kernel_cap_t *pPp) | 130 | * locks to when we are reading the caps of another process. |
| 126 | { | ||
| 127 | struct task_struct *target; | ||
| 128 | int ret; | ||
| 129 | |||
| 130 | spin_lock(&task_capability_lock); | ||
| 131 | read_lock(&tasklist_lock); | ||
| 132 | |||
| 133 | if (pid && pid != task_pid_vnr(current)) { | ||
| 134 | target = find_task_by_vpid(pid); | ||
| 135 | if (!target) { | ||
| 136 | ret = -ESRCH; | ||
| 137 | goto out; | ||
| 138 | } | ||
| 139 | } else | ||
| 140 | target = current; | ||
| 141 | |||
| 142 | ret = security_capget(target, pEp, pIp, pPp); | ||
| 143 | |||
| 144 | out: | ||
| 145 | read_unlock(&tasklist_lock); | ||
| 146 | spin_unlock(&task_capability_lock); | ||
| 147 | |||
| 148 | return ret; | ||
| 149 | } | ||
| 150 | |||
| 151 | /* | ||
| 152 | * cap_set_pg - set capabilities for all processes in a given process | ||
| 153 | * group. We call this holding task_capability_lock and tasklist_lock. | ||
| 154 | */ | ||
| 155 | static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective, | ||
| 156 | kernel_cap_t *inheritable, | ||
| 157 | kernel_cap_t *permitted) | ||
| 158 | { | ||
| 159 | struct task_struct *g, *target; | ||
| 160 | int ret = -EPERM; | ||
| 161 | int found = 0; | ||
| 162 | struct pid *pgrp; | ||
| 163 | |||
| 164 | spin_lock(&task_capability_lock); | ||
| 165 | read_lock(&tasklist_lock); | ||
| 166 | |||
| 167 | pgrp = find_vpid(pgrp_nr); | ||
| 168 | do_each_pid_task(pgrp, PIDTYPE_PGID, g) { | ||
| 169 | target = g; | ||
| 170 | while_each_thread(g, target) { | ||
| 171 | if (!security_capset_check(target, effective, | ||
| 172 | inheritable, permitted)) { | ||
| 173 | security_capset_set(target, effective, | ||
| 174 | inheritable, permitted); | ||
| 175 | ret = 0; | ||
| 176 | } | ||
| 177 | found = 1; | ||
| 178 | } | ||
| 179 | } while_each_pid_task(pgrp, PIDTYPE_PGID, g); | ||
| 180 | |||
| 181 | read_unlock(&tasklist_lock); | ||
| 182 | spin_unlock(&task_capability_lock); | ||
| 183 | |||
| 184 | if (!found) | ||
| 185 | ret = 0; | ||
| 186 | return ret; | ||
| 187 | } | ||
| 188 | |||
| 189 | /* | ||
| 190 | * cap_set_all - set capabilities for all processes other than init | ||
| 191 | * and self. We call this holding task_capability_lock and tasklist_lock. | ||
| 192 | */ | ||
| 193 | static inline int cap_set_all(kernel_cap_t *effective, | ||
| 194 | kernel_cap_t *inheritable, | ||
| 195 | kernel_cap_t *permitted) | ||
| 196 | { | ||
| 197 | struct task_struct *g, *target; | ||
| 198 | int ret = -EPERM; | ||
| 199 | int found = 0; | ||
| 200 | |||
| 201 | spin_lock(&task_capability_lock); | ||
| 202 | read_lock(&tasklist_lock); | ||
| 203 | |||
| 204 | do_each_thread(g, target) { | ||
| 205 | if (target == current | ||
| 206 | || is_container_init(target->group_leader)) | ||
| 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 | read_unlock(&tasklist_lock); | ||
| 217 | spin_unlock(&task_capability_lock); | ||
| 218 | |||
| 219 | if (!found) | ||
| 220 | ret = 0; | ||
| 221 | |||
| 222 | return ret; | ||
| 223 | } | ||
| 224 | |||
| 225 | /* | ||
| 226 | * Given the target pid does not refer to the current process we | ||
| 227 | * need more elaborate support... (This support is not present when | ||
| 228 | * filesystem capabilities are configured.) | ||
| 229 | */ | ||
| 230 | static inline int do_sys_capset_other_tasks(pid_t pid, kernel_cap_t *effective, | ||
| 231 | kernel_cap_t *inheritable, | ||
| 232 | kernel_cap_t *permitted) | ||
| 233 | { | ||
| 234 | struct task_struct *target; | ||
| 235 | int ret; | ||
| 236 | |||
| 237 | if (!capable(CAP_SETPCAP)) | ||
| 238 | return -EPERM; | ||
| 239 | |||
| 240 | if (pid == -1) /* all procs other than current and init */ | ||
| 241 | return cap_set_all(effective, inheritable, permitted); | ||
| 242 | |||
| 243 | else if (pid < 0) /* all procs in process group */ | ||
| 244 | return cap_set_pg(-pid, effective, inheritable, permitted); | ||
| 245 | |||
| 246 | /* target != current */ | ||
| 247 | spin_lock(&task_capability_lock); | ||
| 248 | read_lock(&tasklist_lock); | ||
| 249 | |||
| 250 | target = find_task_by_vpid(pid); | ||
| 251 | if (!target) | ||
| 252 | ret = -ESRCH; | ||
| 253 | else { | ||
| 254 | ret = security_capset_check(target, effective, inheritable, | ||
| 255 | permitted); | ||
| 256 | |||
| 257 | /* having verified that the proposed changes are legal, | ||
| 258 | we now put them into effect. */ | ||
| 259 | if (!ret) | ||
| 260 | security_capset_set(target, effective, inheritable, | ||
| 261 | permitted); | ||
| 262 | } | ||
| 263 | |||
| 264 | read_unlock(&tasklist_lock); | ||
| 265 | spin_unlock(&task_capability_lock); | ||
| 266 | |||
| 267 | return ret; | ||
| 268 | } | ||
| 269 | |||
| 270 | #else /* ie., def CONFIG_SECURITY_FILE_CAPABILITIES */ | ||
| 271 | |||
| 272 | /* | ||
| 273 | * If we have configured with filesystem capability support, then the | ||
| 274 | * only thing that can change the capabilities of the current process | ||
| 275 | * is the current process. As such, we can't be in this code at the | ||
| 276 | * same time as we are in the process of setting capabilities in this | ||
| 277 | * process. The net result is that we can limit our use of locks to | ||
| 278 | * when we are reading the caps of another process. | ||
| 279 | */ | 131 | */ |
| 280 | static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, | 132 | static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, |
| 281 | kernel_cap_t *pIp, kernel_cap_t *pPp) | 133 | kernel_cap_t *pIp, kernel_cap_t *pPp) |
| @@ -285,7 +137,6 @@ static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, | |||
| 285 | if (pid && (pid != task_pid_vnr(current))) { | 137 | if (pid && (pid != task_pid_vnr(current))) { |
| 286 | struct task_struct *target; | 138 | struct task_struct *target; |
| 287 | 139 | ||
| 288 | spin_lock(&task_capability_lock); | ||
| 289 | read_lock(&tasklist_lock); | 140 | read_lock(&tasklist_lock); |
| 290 | 141 | ||
| 291 | target = find_task_by_vpid(pid); | 142 | target = find_task_by_vpid(pid); |
| @@ -295,50 +146,12 @@ static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, | |||
| 295 | ret = security_capget(target, pEp, pIp, pPp); | 146 | ret = security_capget(target, pEp, pIp, pPp); |
| 296 | 147 | ||
| 297 | read_unlock(&tasklist_lock); | 148 | read_unlock(&tasklist_lock); |
| 298 | spin_unlock(&task_capability_lock); | ||
| 299 | } else | 149 | } else |
| 300 | ret = security_capget(current, pEp, pIp, pPp); | 150 | ret = security_capget(current, pEp, pIp, pPp); |
| 301 | 151 | ||
| 302 | return ret; | 152 | return ret; |
| 303 | } | 153 | } |
| 304 | 154 | ||
| 305 | /* | ||
| 306 | * With filesystem capability support configured, the kernel does not | ||
| 307 | * permit the changing of capabilities in one process by another | ||
| 308 | * process. (CAP_SETPCAP has much less broad semantics when configured | ||
| 309 | * this way.) | ||
| 310 | */ | ||
| 311 | static inline int do_sys_capset_other_tasks(pid_t pid, | ||
| 312 | kernel_cap_t *effective, | ||
| 313 | kernel_cap_t *inheritable, | ||
| 314 | kernel_cap_t *permitted) | ||
| 315 | { | ||
| 316 | return -EPERM; | ||
| 317 | } | ||
| 318 | |||
| 319 | #endif /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */ | ||
| 320 | |||
| 321 | /* | ||
| 322 | * Atomically modify the effective capabilities returning the original | ||
| 323 | * value. No permission check is performed here - it is assumed that the | ||
| 324 | * caller is permitted to set the desired effective capabilities. | ||
| 325 | */ | ||
| 326 | kernel_cap_t cap_set_effective(const kernel_cap_t pE_new) | ||
| 327 | { | ||
| 328 | kernel_cap_t pE_old; | ||
| 329 | |||
| 330 | spin_lock(&task_capability_lock); | ||
| 331 | |||
| 332 | pE_old = current->cap_effective; | ||
| 333 | current->cap_effective = pE_new; | ||
| 334 | |||
| 335 | spin_unlock(&task_capability_lock); | ||
| 336 | |||
| 337 | return pE_old; | ||
| 338 | } | ||
| 339 | |||
| 340 | EXPORT_SYMBOL(cap_set_effective); | ||
| 341 | |||
| 342 | /** | 155 | /** |
| 343 | * sys_capget - get the capabilities of a given process. | 156 | * sys_capget - get the capabilities of a given process. |
| 344 | * @header: pointer to struct that contains capability version and | 157 | * @header: pointer to struct that contains capability version and |
| @@ -366,7 +179,6 @@ asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr) | |||
| 366 | return -EINVAL; | 179 | return -EINVAL; |
| 367 | 180 | ||
| 368 | ret = cap_get_target_pid(pid, &pE, &pI, &pP); | 181 | ret = cap_get_target_pid(pid, &pE, &pI, &pP); |
| 369 | |||
| 370 | if (!ret) { | 182 | if (!ret) { |
| 371 | struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S]; | 183 | struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S]; |
| 372 | unsigned i; | 184 | unsigned i; |
| @@ -412,16 +224,14 @@ asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr) | |||
| 412 | * @data: pointer to struct that contains the effective, permitted, | 224 | * @data: pointer to struct that contains the effective, permitted, |
| 413 | * and inheritable capabilities | 225 | * and inheritable capabilities |
| 414 | * | 226 | * |
| 415 | * Set capabilities for a given process, all processes, or all | 227 | * Set capabilities for the current process only. The ability to any other |
| 416 | * processes in a given process group. | 228 | * process(es) has been deprecated and removed. |
| 417 | * | 229 | * |
| 418 | * The restrictions on setting capabilities are specified as: | 230 | * The restrictions on setting capabilities are specified as: |
| 419 | * | 231 | * |
| 420 | * [pid is for the 'target' task. 'current' is the calling task.] | 232 | * I: any raised capabilities must be a subset of the old permitted |
| 421 | * | 233 | * P: any raised capabilities must be a subset of the old permitted |
| 422 | * I: any raised capabilities must be a subset of the (old current) permitted | 234 | * E: must be set to a subset of new permitted |
| 423 | * P: any raised capabilities must be a subset of the (old current) permitted | ||
| 424 | * E: must be set to a subset of (new target) permitted | ||
| 425 | * | 235 | * |
| 426 | * Returns 0 on success and < 0 on error. | 236 | * Returns 0 on success and < 0 on error. |
| 427 | */ | 237 | */ |
| @@ -430,6 +240,7 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) | |||
| 430 | struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S]; | 240 | struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S]; |
| 431 | unsigned i, tocopy; | 241 | unsigned i, tocopy; |
| 432 | kernel_cap_t inheritable, permitted, effective; | 242 | kernel_cap_t inheritable, permitted, effective; |
| 243 | struct cred *new; | ||
| 433 | int ret; | 244 | int ret; |
| 434 | pid_t pid; | 245 | pid_t pid; |
| 435 | 246 | ||
| @@ -440,10 +251,13 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) | |||
| 440 | if (get_user(pid, &header->pid)) | 251 | if (get_user(pid, &header->pid)) |
| 441 | return -EFAULT; | 252 | return -EFAULT; |
| 442 | 253 | ||
| 443 | if (copy_from_user(&kdata, data, tocopy | 254 | /* may only affect current now */ |
| 444 | * sizeof(struct __user_cap_data_struct))) { | 255 | if (pid != 0 && pid != task_pid_vnr(current)) |
| 256 | return -EPERM; | ||
| 257 | |||
| 258 | if (copy_from_user(&kdata, data, | ||
| 259 | tocopy * sizeof(struct __user_cap_data_struct))) | ||
| 445 | return -EFAULT; | 260 | return -EFAULT; |
| 446 | } | ||
| 447 | 261 | ||
| 448 | for (i = 0; i < tocopy; i++) { | 262 | for (i = 0; i < tocopy; i++) { |
| 449 | effective.cap[i] = kdata[i].effective; | 263 | effective.cap[i] = kdata[i].effective; |
| @@ -457,32 +271,21 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) | |||
| 457 | i++; | 271 | i++; |
| 458 | } | 272 | } |
| 459 | 273 | ||
| 460 | if (pid && (pid != task_pid_vnr(current))) | 274 | new = prepare_creds(); |
| 461 | ret = do_sys_capset_other_tasks(pid, &effective, &inheritable, | 275 | if (!new) |
| 462 | &permitted); | 276 | return -ENOMEM; |
| 463 | else { | ||
| 464 | /* | ||
| 465 | * This lock is required even when filesystem | ||
| 466 | * capability support is configured - it protects the | ||
| 467 | * sys_capget() call from returning incorrect data in | ||
| 468 | * the case that the targeted process is not the | ||
| 469 | * current one. | ||
| 470 | */ | ||
| 471 | spin_lock(&task_capability_lock); | ||
| 472 | 277 | ||
| 473 | ret = security_capset_check(current, &effective, &inheritable, | 278 | ret = security_capset(new, current_cred(), |
| 474 | &permitted); | 279 | &effective, &inheritable, &permitted); |
| 475 | /* | 280 | if (ret < 0) |
| 476 | * Having verified that the proposed changes are | 281 | goto error; |
| 477 | * legal, we now put them into effect. | 282 | |
| 478 | */ | 283 | audit_log_capset(pid, new, current_cred()); |
| 479 | if (!ret) | ||
| 480 | security_capset_set(current, &effective, &inheritable, | ||
| 481 | &permitted); | ||
| 482 | spin_unlock(&task_capability_lock); | ||
| 483 | } | ||
| 484 | 284 | ||
| 285 | return commit_creds(new); | ||
| 485 | 286 | ||
| 287 | error: | ||
| 288 | abort_creds(new); | ||
| 486 | return ret; | 289 | return ret; |
| 487 | } | 290 | } |
| 488 | 291 | ||
| @@ -498,6 +301,11 @@ asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) | |||
| 498 | */ | 301 | */ |
| 499 | int capable(int cap) | 302 | int capable(int cap) |
| 500 | { | 303 | { |
| 304 | if (unlikely(!cap_valid(cap))) { | ||
| 305 | printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap); | ||
| 306 | BUG(); | ||
| 307 | } | ||
| 308 | |||
| 501 | if (has_capability(current, cap)) { | 309 | if (has_capability(current, cap)) { |
| 502 | current->flags |= PF_SUPERPRIV; | 310 | current->flags |= PF_SUPERPRIV; |
| 503 | return 1; | 311 | return 1; |
