blob: f02ad47320b92c9536d2c287f2bfb6a72c8a1ef2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/capability.c
3 *
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
5 *
Andrew Morgan72c2d582007-10-18 03:05:59 -07006 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
Daniel Walker314f70f2007-10-18 03:06:08 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/security.h>
14#include <linux/syscalls.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070015#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * This lock protects task->cap_* for all tasks including current.
20 * Locking rule: acquire this prior to tasklist_lock.
21 */
22static 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 Dunlap207a7ba2005-07-27 11:45:10 -070030/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * sys_capget - get the capabilities of a given process.
Randy Dunlap207a7ba2005-07-27 11:45:10 -070032 * @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 Torvalds1da177e2005-04-16 15:20:36 -070038 */
39asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
40{
Daniel Walker314f70f2007-10-18 03:06:08 -070041 int ret = 0;
42 pid_t pid;
43 __u32 version;
44 struct task_struct *target;
45 struct __user_cap_data_struct data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Daniel Walker314f70f2007-10-18 03:06:08 -070047 if (get_user(version, &header->version))
48 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Daniel Walker314f70f2007-10-18 03:06:08 -070050 if (version != _LINUX_CAPABILITY_VERSION) {
51 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
52 return -EFAULT;
53 return -EINVAL;
54 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Daniel Walker314f70f2007-10-18 03:06:08 -070056 if (get_user(pid, &header->pid))
57 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Daniel Walker314f70f2007-10-18 03:06:08 -070059 if (pid < 0)
60 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Daniel Walker314f70f2007-10-18 03:06:08 -070062 spin_lock(&task_capability_lock);
63 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Daniel Walker314f70f2007-10-18 03:06:08 -070065 if (pid && pid != current->pid) {
66 target = find_task_by_pid(pid);
67 if (!target) {
68 ret = -ESRCH;
69 goto out;
70 }
71 } else
72 target = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Daniel Walker314f70f2007-10-18 03:06:08 -070074 ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76out:
Daniel Walker314f70f2007-10-18 03:06:08 -070077 read_unlock(&tasklist_lock);
78 spin_unlock(&task_capability_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Daniel Walker314f70f2007-10-18 03:06:08 -070080 if (!ret && copy_to_user(dataptr, &data, sizeof data))
81 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Daniel Walker314f70f2007-10-18 03:06:08 -070083 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86/*
87 * cap_set_pg - set capabilities for all processes in a given process
88 * group. We call this holding task_capability_lock and tasklist_lock.
89 */
Eric W. Biederman41487c62007-02-12 00:53:01 -080090static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 kernel_cap_t *inheritable,
92 kernel_cap_t *permitted)
93{
Ingo Molnar36c8b582006-07-03 00:25:41 -070094 struct task_struct *g, *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int ret = -EPERM;
96 int found = 0;
Eric W. Biederman41487c62007-02-12 00:53:01 -080097 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Eric W. Biederman41487c62007-02-12 00:53:01 -080099 pgrp = find_pid(pgrp_nr);
100 do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 target = g;
102 while_each_thread(g, target) {
103 if (!security_capset_check(target, effective,
104 inheritable,
105 permitted)) {
106 security_capset_set(target, effective,
107 inheritable,
108 permitted);
109 ret = 0;
110 }
111 found = 1;
112 }
Eric W. Biederman41487c62007-02-12 00:53:01 -0800113 } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (!found)
Daniel Walker314f70f2007-10-18 03:06:08 -0700116 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ret;
118}
119
120/*
121 * cap_set_all - set capabilities for all processes other than init
122 * and self. We call this holding task_capability_lock and tasklist_lock.
123 */
124static inline int cap_set_all(kernel_cap_t *effective,
125 kernel_cap_t *inheritable,
126 kernel_cap_t *permitted)
127{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700128 struct task_struct *g, *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int ret = -EPERM;
130 int found = 0;
131
132 do_each_thread(g, target) {
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700133 if (target == current || is_container_init(target->group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 continue;
135 found = 1;
136 if (security_capset_check(target, effective, inheritable,
137 permitted))
138 continue;
139 ret = 0;
140 security_capset_set(target, effective, inheritable, permitted);
141 } while_each_thread(g, target);
142
143 if (!found)
144 ret = 0;
145 return ret;
146}
147
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700148/**
149 * sys_capset - set capabilities for a process or a group of processes
150 * @header: pointer to struct that contains capability version and
151 * target pid data
152 * @data: pointer to struct that contains the effective, permitted,
153 * and inheritable capabilities
154 *
155 * Set capabilities for a given process, all processes, or all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * processes in a given process group.
157 *
158 * The restrictions on setting capabilities are specified as:
159 *
160 * [pid is for the 'target' task. 'current' is the calling task.]
161 *
162 * I: any raised capabilities must be a subset of the (old current) permitted
163 * P: any raised capabilities must be a subset of the (old current) permitted
164 * E: must be set to a subset of (new target) permitted
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700165 *
166 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
169{
Daniel Walker314f70f2007-10-18 03:06:08 -0700170 kernel_cap_t inheritable, permitted, effective;
171 __u32 version;
172 struct task_struct *target;
173 int ret;
174 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Daniel Walker314f70f2007-10-18 03:06:08 -0700176 if (get_user(version, &header->version))
177 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Daniel Walker314f70f2007-10-18 03:06:08 -0700179 if (version != _LINUX_CAPABILITY_VERSION) {
180 if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
181 return -EFAULT;
182 return -EINVAL;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Daniel Walker314f70f2007-10-18 03:06:08 -0700185 if (get_user(pid, &header->pid))
186 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Daniel Walker314f70f2007-10-18 03:06:08 -0700188 if (pid && pid != current->pid && !capable(CAP_SETPCAP))
189 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Daniel Walker314f70f2007-10-18 03:06:08 -0700191 if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
192 copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
193 copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
194 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Daniel Walker314f70f2007-10-18 03:06:08 -0700196 spin_lock(&task_capability_lock);
197 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Daniel Walker314f70f2007-10-18 03:06:08 -0700199 if (pid > 0 && pid != current->pid) {
200 target = find_task_by_pid(pid);
201 if (!target) {
202 ret = -ESRCH;
203 goto out;
204 }
205 } else
206 target = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Daniel Walker314f70f2007-10-18 03:06:08 -0700208 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Daniel Walker314f70f2007-10-18 03:06:08 -0700210 /* having verified that the proposed changes are legal,
211 we now put them into effect. */
212 if (pid < 0) {
213 if (pid == -1) /* all procs other than current and init */
214 ret = cap_set_all(&effective, &inheritable, &permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Daniel Walker314f70f2007-10-18 03:06:08 -0700216 else /* all procs in process group */
217 ret = cap_set_pg(-pid, &effective, &inheritable,
218 &permitted);
219 } else {
220 ret = security_capset_check(target, &effective, &inheritable,
221 &permitted);
222 if (!ret)
223 security_capset_set(target, &effective, &inheritable,
224 &permitted);
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227out:
Daniel Walker314f70f2007-10-18 03:06:08 -0700228 read_unlock(&tasklist_lock);
229 spin_unlock(&task_capability_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Daniel Walker314f70f2007-10-18 03:06:08 -0700231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Chris Wright12b59892006-03-25 03:07:41 -0800233
234int __capable(struct task_struct *t, int cap)
235{
236 if (security_capable(t, cap) == 0) {
237 t->flags |= PF_SUPERPRIV;
238 return 1;
239 }
240 return 0;
241}
Chris Wright12b59892006-03-25 03:07:41 -0800242
243int capable(int cap)
244{
245 return __capable(current, cap);
246}
247EXPORT_SYMBOL(capable);