2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
7 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
10 #include <linux/capability.h>
12 #include <linux/module.h>
13 #include <linux/security.h>
14 #include <linux/syscalls.h>
15 #include <linux/pid_namespace.h>
16 #include <asm/uaccess.h>
19 * This lock protects task->cap_* for all tasks including current.
20 * Locking rule: acquire this prior to tasklist_lock.
22 static DEFINE_SPINLOCK(task_capability_lock
);
25 * Leveraged for setting/resetting capabilities
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
;
32 EXPORT_SYMBOL(__cap_empty_set
);
33 EXPORT_SYMBOL(__cap_full_set
);
34 EXPORT_SYMBOL(__cap_init_eff_set
);
37 * More recent versions of libcap are available from:
39 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
42 static void warn_legacy_capability_use(void)
46 char name
[sizeof(current
->comm
)];
48 printk(KERN_INFO
"warning: `%s' uses 32-bit capabilities"
49 " (legacy support in use)\n",
50 get_task_comm(name
, current
));
56 * Version 2 capabilities worked fine, but the linux/capability.h file
57 * that accompanied their introduction encouraged their use without
58 * the necessary user-space source code changes. As such, we have
59 * created a version 3 with equivalent functionality to version 2, but
60 * with a header change to protect legacy source code from using
61 * version 2 when it wanted to use version 1. If your system has code
62 * that trips the following warning, it is using version 2 specific
63 * capabilities and may be doing so insecurely.
65 * The remedy is to either upgrade your version of libcap (to 2.10+,
66 * if the application is linked against it), or recompile your
67 * application with modern kernel headers and this warning will go
71 static void warn_deprecated_v2(void)
76 char name
[sizeof(current
->comm
)];
78 printk(KERN_INFO
"warning: `%s' uses deprecated v2"
79 " capabilities in a way that may be insecure.\n",
80 get_task_comm(name
, current
));
86 * Version check. Return the number of u32s in each capability flag
87 * array, or a negative value on error.
89 static int cap_validate_magic(cap_user_header_t header
, unsigned *tocopy
)
93 if (get_user(version
, &header
->version
))
97 case _LINUX_CAPABILITY_VERSION_1
:
98 warn_legacy_capability_use();
99 *tocopy
= _LINUX_CAPABILITY_U32S_1
;
101 case _LINUX_CAPABILITY_VERSION_2
:
102 warn_deprecated_v2();
104 * fall through - v3 is otherwise equivalent to v2.
106 case _LINUX_CAPABILITY_VERSION_3
:
107 *tocopy
= _LINUX_CAPABILITY_U32S_3
;
110 if (put_user((u32
)_KERNEL_CAPABILITY_VERSION
, &header
->version
))
119 * For sys_getproccap() and sys_setproccap(), any of the three
120 * capability set pointers may be NULL -- indicating that that set is
121 * uninteresting and/or not to be changed.
125 * sys_capget - get the capabilities of a given process.
126 * @header: pointer to struct that contains capability version and
128 * @dataptr: pointer to struct that contains the effective, permitted,
129 * and inheritable capabilities that are returned
131 * Returns 0 on success and < 0 on error.
133 asmlinkage
long sys_capget(cap_user_header_t header
, cap_user_data_t dataptr
)
137 struct task_struct
*target
;
139 kernel_cap_t pE
, pI
, pP
;
141 ret
= cap_validate_magic(header
, &tocopy
);
145 if (get_user(pid
, &header
->pid
))
151 spin_lock(&task_capability_lock
);
152 read_lock(&tasklist_lock
);
154 if (pid
&& pid
!= task_pid_vnr(current
)) {
155 target
= find_task_by_vpid(pid
);
163 ret
= security_capget(target
, &pE
, &pI
, &pP
);
166 read_unlock(&tasklist_lock
);
167 spin_unlock(&task_capability_lock
);
170 struct __user_cap_data_struct kdata
[_KERNEL_CAPABILITY_U32S
];
173 for (i
= 0; i
< tocopy
; i
++) {
174 kdata
[i
].effective
= pE
.cap
[i
];
175 kdata
[i
].permitted
= pP
.cap
[i
];
176 kdata
[i
].inheritable
= pI
.cap
[i
];
180 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
181 * we silently drop the upper capabilities here. This
182 * has the effect of making older libcap
183 * implementations implicitly drop upper capability
184 * bits when they perform a: capget/modify/capset
187 * This behavior is considered fail-safe
188 * behavior. Upgrading the application to a newer
189 * version of libcap will enable access to the newer
192 * An alternative would be to return an error here
193 * (-ERANGE), but that causes legacy applications to
194 * unexpectidly fail; the capget/modify/capset aborts
195 * before modification is attempted and the application
199 if (copy_to_user(dataptr
, kdata
, tocopy
200 * sizeof(struct __user_cap_data_struct
))) {
209 * cap_set_pg - set capabilities for all processes in a given process
210 * group. We call this holding task_capability_lock and tasklist_lock.
212 static inline int cap_set_pg(int pgrp_nr
, kernel_cap_t
*effective
,
213 kernel_cap_t
*inheritable
,
214 kernel_cap_t
*permitted
)
216 struct task_struct
*g
, *target
;
221 pgrp
= find_vpid(pgrp_nr
);
222 do_each_pid_task(pgrp
, PIDTYPE_PGID
, g
) {
224 while_each_thread(g
, target
) {
225 if (!security_capset_check(target
, effective
,
228 security_capset_set(target
, effective
,
235 } while_each_pid_task(pgrp
, PIDTYPE_PGID
, g
);
243 * cap_set_all - set capabilities for all processes other than init
244 * and self. We call this holding task_capability_lock and tasklist_lock.
246 static inline int cap_set_all(kernel_cap_t
*effective
,
247 kernel_cap_t
*inheritable
,
248 kernel_cap_t
*permitted
)
250 struct task_struct
*g
, *target
;
254 do_each_thread(g
, target
) {
255 if (target
== current
|| is_container_init(target
->group_leader
))
258 if (security_capset_check(target
, effective
, inheritable
,
262 security_capset_set(target
, effective
, inheritable
, permitted
);
263 } while_each_thread(g
, target
);
271 * sys_capset - set capabilities for a process or a group of processes
272 * @header: pointer to struct that contains capability version and
274 * @data: pointer to struct that contains the effective, permitted,
275 * and inheritable capabilities
277 * Set capabilities for a given process, all processes, or all
278 * processes in a given process group.
280 * The restrictions on setting capabilities are specified as:
282 * [pid is for the 'target' task. 'current' is the calling task.]
284 * I: any raised capabilities must be a subset of the (old current) permitted
285 * P: any raised capabilities must be a subset of the (old current) permitted
286 * E: must be set to a subset of (new target) permitted
288 * Returns 0 on success and < 0 on error.
290 asmlinkage
long sys_capset(cap_user_header_t header
, const cap_user_data_t data
)
292 struct __user_cap_data_struct kdata
[_KERNEL_CAPABILITY_U32S
];
294 kernel_cap_t inheritable
, permitted
, effective
;
295 struct task_struct
*target
;
299 ret
= cap_validate_magic(header
, &tocopy
);
303 if (get_user(pid
, &header
->pid
))
306 if (pid
&& pid
!= task_pid_vnr(current
) && !capable(CAP_SETPCAP
))
309 if (copy_from_user(&kdata
, data
, tocopy
310 * sizeof(struct __user_cap_data_struct
))) {
314 for (i
= 0; i
< tocopy
; i
++) {
315 effective
.cap
[i
] = kdata
[i
].effective
;
316 permitted
.cap
[i
] = kdata
[i
].permitted
;
317 inheritable
.cap
[i
] = kdata
[i
].inheritable
;
319 while (i
< _KERNEL_CAPABILITY_U32S
) {
320 effective
.cap
[i
] = 0;
321 permitted
.cap
[i
] = 0;
322 inheritable
.cap
[i
] = 0;
326 spin_lock(&task_capability_lock
);
327 read_lock(&tasklist_lock
);
329 if (pid
> 0 && pid
!= task_pid_vnr(current
)) {
330 target
= find_task_by_vpid(pid
);
340 /* having verified that the proposed changes are legal,
341 we now put them into effect. */
343 if (pid
== -1) /* all procs other than current and init */
344 ret
= cap_set_all(&effective
, &inheritable
, &permitted
);
346 else /* all procs in process group */
347 ret
= cap_set_pg(-pid
, &effective
, &inheritable
,
350 ret
= security_capset_check(target
, &effective
, &inheritable
,
353 security_capset_set(target
, &effective
, &inheritable
,
358 read_unlock(&tasklist_lock
);
359 spin_unlock(&task_capability_lock
);
364 int __capable(struct task_struct
*t
, int cap
)
366 if (security_capable(t
, cap
) == 0) {
367 t
->flags
|= PF_SUPERPRIV
;
375 return __capable(current
, cap
);
377 EXPORT_SYMBOL(capable
);