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 | * |
| 6 | * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com> |
| 7 | * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net> |
| 8 | */ |
| 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> |
| 15 | #include <asm/uaccess.h> |
| 16 | |
| 17 | unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */ |
| 18 | kernel_cap_t cap_bset = CAP_INIT_EFF_SET; |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /* |
| 21 | * This lock protects task->cap_* for all tasks including current. |
| 22 | * Locking rule: acquire this prior to tasklist_lock. |
| 23 | */ |
| 24 | static DEFINE_SPINLOCK(task_capability_lock); |
| 25 | |
| 26 | /* |
| 27 | * For sys_getproccap() and sys_setproccap(), any of the three |
| 28 | * capability set pointers may be NULL -- indicating that that set is |
| 29 | * uninteresting and/or not to be changed. |
| 30 | */ |
| 31 | |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 32 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * sys_capget - get the capabilities of a given process. |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 34 | * @header: pointer to struct that contains capability version and |
| 35 | * target pid data |
| 36 | * @dataptr: pointer to struct that contains the effective, permitted, |
| 37 | * and inheritable capabilities that are returned |
| 38 | * |
| 39 | * Returns 0 on success and < 0 on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
| 41 | asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr) |
| 42 | { |
| 43 | int ret = 0; |
| 44 | pid_t pid; |
| 45 | __u32 version; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 46 | struct task_struct *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct __user_cap_data_struct data; |
| 48 | |
| 49 | if (get_user(version, &header->version)) |
| 50 | return -EFAULT; |
| 51 | |
| 52 | if (version != _LINUX_CAPABILITY_VERSION) { |
| 53 | if (put_user(_LINUX_CAPABILITY_VERSION, &header->version)) |
| 54 | return -EFAULT; |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
| 58 | if (get_user(pid, &header->pid)) |
| 59 | return -EFAULT; |
| 60 | |
| 61 | if (pid < 0) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | spin_lock(&task_capability_lock); |
| 65 | read_lock(&tasklist_lock); |
| 66 | |
| 67 | if (pid && pid != current->pid) { |
| 68 | target = find_task_by_pid(pid); |
| 69 | if (!target) { |
| 70 | ret = -ESRCH; |
| 71 | goto out; |
| 72 | } |
| 73 | } else |
| 74 | target = current; |
| 75 | |
| 76 | ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted); |
| 77 | |
| 78 | out: |
| 79 | read_unlock(&tasklist_lock); |
| 80 | spin_unlock(&task_capability_lock); |
| 81 | |
| 82 | if (!ret && copy_to_user(dataptr, &data, sizeof data)) |
| 83 | return -EFAULT; |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * cap_set_pg - set capabilities for all processes in a given process |
| 90 | * group. We call this holding task_capability_lock and tasklist_lock. |
| 91 | */ |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 92 | 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] | 93 | kernel_cap_t *inheritable, |
| 94 | kernel_cap_t *permitted) |
| 95 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 96 | struct task_struct *g, *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | int ret = -EPERM; |
| 98 | int found = 0; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 99 | struct pid *pgrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 101 | pgrp = find_pid(pgrp_nr); |
| 102 | do_each_pid_task(pgrp, PIDTYPE_PGID, g) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | target = g; |
| 104 | while_each_thread(g, target) { |
| 105 | if (!security_capset_check(target, effective, |
| 106 | inheritable, |
| 107 | permitted)) { |
| 108 | security_capset_set(target, effective, |
| 109 | inheritable, |
| 110 | permitted); |
| 111 | ret = 0; |
| 112 | } |
| 113 | found = 1; |
| 114 | } |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 115 | } while_each_pid_task(pgrp, PIDTYPE_PGID, g); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | if (!found) |
| 118 | ret = 0; |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * cap_set_all - set capabilities for all processes other than init |
| 124 | * and self. We call this holding task_capability_lock and tasklist_lock. |
| 125 | */ |
| 126 | static inline int cap_set_all(kernel_cap_t *effective, |
| 127 | kernel_cap_t *inheritable, |
| 128 | kernel_cap_t *permitted) |
| 129 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 130 | struct task_struct *g, *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | int ret = -EPERM; |
| 132 | int found = 0; |
| 133 | |
| 134 | do_each_thread(g, target) { |
Sukadev Bhattiprolu | f400e19 | 2006-09-29 02:00:07 -0700 | [diff] [blame] | 135 | if (target == current || is_init(target)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | continue; |
| 137 | found = 1; |
| 138 | if (security_capset_check(target, effective, inheritable, |
| 139 | permitted)) |
| 140 | continue; |
| 141 | ret = 0; |
| 142 | security_capset_set(target, effective, inheritable, permitted); |
| 143 | } while_each_thread(g, target); |
| 144 | |
| 145 | if (!found) |
| 146 | ret = 0; |
| 147 | return ret; |
| 148 | } |
| 149 | |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 150 | /** |
| 151 | * sys_capset - set capabilities for a process or a group of processes |
| 152 | * @header: pointer to struct that contains capability version and |
| 153 | * target pid data |
| 154 | * @data: pointer to struct that contains the effective, permitted, |
| 155 | * and inheritable capabilities |
| 156 | * |
| 157 | * Set capabilities for a given process, all processes, or all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | * processes in a given process group. |
| 159 | * |
| 160 | * The restrictions on setting capabilities are specified as: |
| 161 | * |
| 162 | * [pid is for the 'target' task. 'current' is the calling task.] |
| 163 | * |
| 164 | * I: any raised capabilities must be a subset of the (old current) permitted |
| 165 | * P: any raised capabilities must be a subset of the (old current) permitted |
| 166 | * E: must be set to a subset of (new target) permitted |
Randy Dunlap | 207a7ba | 2005-07-27 11:45:10 -0700 | [diff] [blame] | 167 | * |
| 168 | * Returns 0 on success and < 0 on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | */ |
| 170 | asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data) |
| 171 | { |
| 172 | kernel_cap_t inheritable, permitted, effective; |
| 173 | __u32 version; |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 174 | struct task_struct *target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | int ret; |
| 176 | pid_t pid; |
| 177 | |
| 178 | if (get_user(version, &header->version)) |
| 179 | return -EFAULT; |
| 180 | |
| 181 | if (version != _LINUX_CAPABILITY_VERSION) { |
| 182 | if (put_user(_LINUX_CAPABILITY_VERSION, &header->version)) |
| 183 | return -EFAULT; |
| 184 | return -EINVAL; |
| 185 | } |
| 186 | |
| 187 | if (get_user(pid, &header->pid)) |
| 188 | return -EFAULT; |
| 189 | |
| 190 | if (pid && pid != current->pid && !capable(CAP_SETPCAP)) |
| 191 | return -EPERM; |
| 192 | |
| 193 | if (copy_from_user(&effective, &data->effective, sizeof(effective)) || |
| 194 | copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) || |
| 195 | copy_from_user(&permitted, &data->permitted, sizeof(permitted))) |
| 196 | return -EFAULT; |
| 197 | |
| 198 | spin_lock(&task_capability_lock); |
| 199 | read_lock(&tasklist_lock); |
| 200 | |
| 201 | if (pid > 0 && pid != current->pid) { |
| 202 | target = find_task_by_pid(pid); |
| 203 | if (!target) { |
| 204 | ret = -ESRCH; |
| 205 | goto out; |
| 206 | } |
| 207 | } else |
| 208 | target = current; |
| 209 | |
| 210 | ret = 0; |
| 211 | |
| 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); |
| 217 | |
| 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 | } |
| 228 | |
| 229 | out: |
| 230 | read_unlock(&tasklist_lock); |
| 231 | spin_unlock(&task_capability_lock); |
| 232 | |
| 233 | return ret; |
| 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); |