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 | /* |
| 25 | * For sys_getproccap() and sys_setproccap(), any of the three |
| 26 | * capability set pointers may be NULL -- indicating that that set is |
| 27 | * uninteresting and/or not to be changed. |
| 28 | */ |
| 29 | |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 30 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * sys_capget - get the capabilities of a given process. |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 32 | * @header: pointer to struct that contains capability version and |
| 33 | * target pid data |
| 34 | * @dataptr: pointer to struct that contains the effective, permitted, |
| 35 | * and inheritable capabilities that are returned |
| 36 | * |
| 37 | * Returns 0 on success and < 0 on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr) |
| 40 | { |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 41 | int ret = 0; |
| 42 | pid_t pid; |
| 43 | __u32 version; |
| 44 | struct task_struct *target; |
| 45 | struct __user_cap_data_struct data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 47 | if (get_user(version, &header->version)) |
| 48 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 50 | if (version != _LINUX_CAPABILITY_VERSION) { |
| 51 | if (put_user(_LINUX_CAPABILITY_VERSION, &header->version)) |
| 52 | return -EFAULT; |
| 53 | return -EINVAL; |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 56 | if (get_user(pid, &header->pid)) |
| 57 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 59 | if (pid < 0) |
| 60 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 62 | spin_lock(&task_capability_lock); |
| 63 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame^] | 65 | if (pid && pid != task_pid_vnr(current)) { |
| 66 | target = find_task_by_pid_ns(pid, |
| 67 | current->nsproxy->pid_ns); |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 68 | if (!target) { |
| 69 | ret = -ESRCH; |
| 70 | goto out; |
| 71 | } |
| 72 | } else |
| 73 | target = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 75 | ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | out: |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 78 | read_unlock(&tasklist_lock); |
| 79 | spin_unlock(&task_capability_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 81 | if (!ret && copy_to_user(dataptr, &data, sizeof data)) |
| 82 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 84 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
| 88 | * cap_set_pg - set capabilities for all processes in a given process |
| 89 | * group. We call this holding task_capability_lock and tasklist_lock. |
| 90 | */ |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 91 | 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] | 92 | kernel_cap_t *inheritable, |
| 93 | kernel_cap_t *permitted) |
| 94 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 95 | struct task_struct *g, *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | int ret = -EPERM; |
| 97 | int found = 0; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 98 | struct pid *pgrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame^] | 100 | pgrp = find_pid_ns(pgrp_nr, current->nsproxy->pid_ns); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 101 | do_each_pid_task(pgrp, PIDTYPE_PGID, g) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | target = g; |
| 103 | while_each_thread(g, target) { |
| 104 | if (!security_capset_check(target, effective, |
| 105 | inheritable, |
| 106 | permitted)) { |
| 107 | security_capset_set(target, effective, |
| 108 | inheritable, |
| 109 | permitted); |
| 110 | ret = 0; |
| 111 | } |
| 112 | found = 1; |
| 113 | } |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 114 | } while_each_pid_task(pgrp, PIDTYPE_PGID, g); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | if (!found) |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 117 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * cap_set_all - set capabilities for all processes other than init |
| 123 | * and self. We call this holding task_capability_lock and tasklist_lock. |
| 124 | */ |
| 125 | static inline int cap_set_all(kernel_cap_t *effective, |
| 126 | kernel_cap_t *inheritable, |
| 127 | kernel_cap_t *permitted) |
| 128 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 129 | struct task_struct *g, *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | int ret = -EPERM; |
| 131 | int found = 0; |
| 132 | |
| 133 | do_each_thread(g, target) { |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 134 | if (target == current || is_container_init(target->group_leader)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | continue; |
| 136 | found = 1; |
| 137 | if (security_capset_check(target, effective, inheritable, |
| 138 | permitted)) |
| 139 | continue; |
| 140 | ret = 0; |
| 141 | security_capset_set(target, effective, inheritable, permitted); |
| 142 | } while_each_thread(g, target); |
| 143 | |
| 144 | if (!found) |
| 145 | ret = 0; |
| 146 | return ret; |
| 147 | } |
| 148 | |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 149 | /** |
| 150 | * sys_capset - set capabilities for a process or a group of processes |
| 151 | * @header: pointer to struct that contains capability version and |
| 152 | * target pid data |
| 153 | * @data: pointer to struct that contains the effective, permitted, |
| 154 | * and inheritable capabilities |
| 155 | * |
| 156 | * Set capabilities for a given process, all processes, or all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * processes in a given process group. |
| 158 | * |
| 159 | * The restrictions on setting capabilities are specified as: |
| 160 | * |
| 161 | * [pid is for the 'target' task. 'current' is the calling task.] |
| 162 | * |
| 163 | * I: any raised capabilities must be a subset of the (old current) permitted |
| 164 | * P: any raised capabilities must be a subset of the (old current) permitted |
| 165 | * E: must be set to a subset of (new target) permitted |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 166 | * |
| 167 | * Returns 0 on success and < 0 on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | */ |
| 169 | asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) |
| 170 | { |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 171 | kernel_cap_t inheritable, permitted, effective; |
| 172 | __u32 version; |
| 173 | struct task_struct *target; |
| 174 | int ret; |
| 175 | pid_t pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 177 | if (get_user(version, &header->version)) |
| 178 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 180 | if (version != _LINUX_CAPABILITY_VERSION) { |
| 181 | if (put_user(_LINUX_CAPABILITY_VERSION, &header->version)) |
| 182 | return -EFAULT; |
| 183 | return -EINVAL; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 186 | if (get_user(pid, &header->pid)) |
| 187 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame^] | 189 | if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP)) |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 190 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 192 | if (copy_from_user(&effective, &data->effective, sizeof(effective)) || |
| 193 | copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) || |
| 194 | copy_from_user(&permitted, &data->permitted, sizeof(permitted))) |
| 195 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 197 | spin_lock(&task_capability_lock); |
| 198 | read_lock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame^] | 200 | if (pid > 0 && pid != task_pid_vnr(current)) { |
| 201 | target = find_task_by_pid_ns(pid, |
| 202 | current->nsproxy->pid_ns); |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 203 | if (!target) { |
| 204 | ret = -ESRCH; |
| 205 | goto out; |
| 206 | } |
| 207 | } else |
| 208 | target = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 210 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 212 | /* having verified that the proposed changes are legal, |
| 213 | we now put them into effect. */ |
| 214 | if (pid < 0) { |
| 215 | if (pid == -1) /* all procs other than current and init */ |
| 216 | ret = cap_set_all(&effective, &inheritable, &permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 218 | else /* all procs in process group */ |
| 219 | ret = cap_set_pg(-pid, &effective, &inheritable, |
| 220 | &permitted); |
| 221 | } else { |
| 222 | ret = security_capset_check(target, &effective, &inheritable, |
| 223 | &permitted); |
| 224 | if (!ret) |
| 225 | security_capset_set(target, &effective, &inheritable, |
| 226 | &permitted); |
| 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | out: |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 230 | read_unlock(&tasklist_lock); |
| 231 | spin_unlock(&task_capability_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Daniel Walker | 314f70f | 2007-10-18 03:06:08 -0700 | [diff] [blame] | 233 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
Chris Wright | 12b5989 | 2006-03-25 03:07:41 -0800 | [diff] [blame] | 235 | |
| 236 | int __capable(struct task_struct *t, int cap) |
| 237 | { |
| 238 | if (security_capable(t, cap) == 0) { |
| 239 | t->flags |= PF_SUPERPRIV; |
| 240 | return 1; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
Chris Wright | 12b5989 | 2006-03-25 03:07:41 -0800 | [diff] [blame] | 244 | |
| 245 | int capable(int cap) |
| 246 | { |
| 247 | return __capable(current, cap); |
| 248 | } |
| 249 | EXPORT_SYMBOL(capable); |