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
;
33 * For sys_getproccap() and sys_setproccap(), any of the three
34 * capability set pointers may be NULL -- indicating that that set is
35 * uninteresting and/or not to be changed.
39 * sys_capget - get the capabilities of a given process.
40 * @header: pointer to struct that contains capability version and
42 * @dataptr: pointer to struct that contains the effective, permitted,
43 * and inheritable capabilities that are returned
45 * Returns 0 on success and < 0 on error.
47 asmlinkage
long sys_capget(cap_user_header_t header
, cap_user_data_t dataptr
)
49 static int warned
= 0;
53 struct task_struct
*target
;
55 kernel_cap_t pE
, pI
, pP
;
57 if (get_user(version
, &header
->version
))
61 case _LINUX_CAPABILITY_VERSION_1
:
65 "warning: process `%s' gets w/ old libcap\n",
68 tocopy
= _LINUX_CAPABILITY_U32S_1
;
70 case _LINUX_CAPABILITY_VERSION_2
:
71 tocopy
= _LINUX_CAPABILITY_U32S_2
;
74 if (put_user(_LINUX_CAPABILITY_VERSION
, &header
->version
))
79 if (get_user(pid
, &header
->pid
))
85 spin_lock(&task_capability_lock
);
86 read_lock(&tasklist_lock
);
88 if (pid
&& pid
!= task_pid_vnr(current
)) {
89 target
= find_task_by_vpid(pid
);
97 ret
= security_capget(target
, &pE
, &pI
, &pP
);
100 read_unlock(&tasklist_lock
);
101 spin_unlock(&task_capability_lock
);
104 struct __user_cap_data_struct kdata
[_LINUX_CAPABILITY_U32S
];
107 for (i
= 0; i
< tocopy
; i
++) {
108 kdata
[i
].effective
= pE
.cap
[i
];
109 kdata
[i
].permitted
= pP
.cap
[i
];
110 kdata
[i
].inheritable
= pI
.cap
[i
];
112 while (i
< _LINUX_CAPABILITY_U32S
) {
113 if (pE
.cap
[i
] || pP
.cap
[i
] || pP
.cap
[i
]) {
114 /* Cannot represent w/ legacy structure */
120 if (copy_to_user(dataptr
, kdata
, tocopy
121 * sizeof(struct __user_cap_data_struct
))) {
130 * cap_set_pg - set capabilities for all processes in a given process
131 * group. We call this holding task_capability_lock and tasklist_lock.
133 static inline int cap_set_pg(int pgrp_nr
, kernel_cap_t
*effective
,
134 kernel_cap_t
*inheritable
,
135 kernel_cap_t
*permitted
)
137 struct task_struct
*g
, *target
;
142 pgrp
= find_vpid(pgrp_nr
);
143 do_each_pid_task(pgrp
, PIDTYPE_PGID
, g
) {
145 while_each_thread(g
, target
) {
146 if (!security_capset_check(target
, effective
,
149 security_capset_set(target
, effective
,
156 } while_each_pid_task(pgrp
, PIDTYPE_PGID
, g
);
164 * cap_set_all - set capabilities for all processes other than init
165 * and self. We call this holding task_capability_lock and tasklist_lock.
167 static inline int cap_set_all(kernel_cap_t
*effective
,
168 kernel_cap_t
*inheritable
,
169 kernel_cap_t
*permitted
)
171 struct task_struct
*g
, *target
;
175 do_each_thread(g
, target
) {
176 if (target
== current
|| is_container_init(target
->group_leader
))
179 if (security_capset_check(target
, effective
, inheritable
,
183 security_capset_set(target
, effective
, inheritable
, permitted
);
184 } while_each_thread(g
, target
);
192 * sys_capset - set capabilities for a process or a group of processes
193 * @header: pointer to struct that contains capability version and
195 * @data: pointer to struct that contains the effective, permitted,
196 * and inheritable capabilities
198 * Set capabilities for a given process, all processes, or all
199 * processes in a given process group.
201 * The restrictions on setting capabilities are specified as:
203 * [pid is for the 'target' task. 'current' is the calling task.]
205 * I: any raised capabilities must be a subset of the (old current) permitted
206 * P: any raised capabilities must be a subset of the (old current) permitted
207 * E: must be set to a subset of (new target) permitted
209 * Returns 0 on success and < 0 on error.
211 asmlinkage
long sys_capset(cap_user_header_t header
, const cap_user_data_t data
)
214 struct __user_cap_data_struct kdata
[_LINUX_CAPABILITY_U32S
];
216 kernel_cap_t inheritable
, permitted
, effective
;
218 struct task_struct
*target
;
222 if (get_user(version
, &header
->version
))
226 case _LINUX_CAPABILITY_VERSION_1
:
228 char name
[sizeof(current
->comm
)];
231 "warning: process `%s' sets w/ old libcap\n",
232 get_task_comm(name
, current
));
234 tocopy
= _LINUX_CAPABILITY_U32S_1
;
236 case _LINUX_CAPABILITY_VERSION_2
:
237 tocopy
= _LINUX_CAPABILITY_U32S_2
;
240 if (put_user(_LINUX_CAPABILITY_VERSION
, &header
->version
))
245 if (get_user(pid
, &header
->pid
))
248 if (pid
&& pid
!= task_pid_vnr(current
) && !capable(CAP_SETPCAP
))
251 if (copy_from_user(&kdata
, data
, tocopy
252 * sizeof(struct __user_cap_data_struct
))) {
256 for (i
= 0; i
< tocopy
; i
++) {
257 effective
.cap
[i
] = kdata
[i
].effective
;
258 permitted
.cap
[i
] = kdata
[i
].permitted
;
259 inheritable
.cap
[i
] = kdata
[i
].inheritable
;
261 while (i
< _LINUX_CAPABILITY_U32S
) {
262 effective
.cap
[i
] = 0;
263 permitted
.cap
[i
] = 0;
264 inheritable
.cap
[i
] = 0;
268 spin_lock(&task_capability_lock
);
269 read_lock(&tasklist_lock
);
271 if (pid
> 0 && pid
!= task_pid_vnr(current
)) {
272 target
= find_task_by_vpid(pid
);
282 /* having verified that the proposed changes are legal,
283 we now put them into effect. */
285 if (pid
== -1) /* all procs other than current and init */
286 ret
= cap_set_all(&effective
, &inheritable
, &permitted
);
288 else /* all procs in process group */
289 ret
= cap_set_pg(-pid
, &effective
, &inheritable
,
292 ret
= security_capset_check(target
, &effective
, &inheritable
,
295 security_capset_set(target
, &effective
, &inheritable
,
300 read_unlock(&tasklist_lock
);
301 spin_unlock(&task_capability_lock
);
306 int __capable(struct task_struct
*t
, int cap
)
308 if (security_capable(t
, cap
) == 0) {
309 t
->flags
|= PF_SUPERPRIV
;
317 return __capable(current
, cap
);
319 EXPORT_SYMBOL(capable
);