2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com>
7 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
11 #include <linux/security.h>
12 #include <asm/uaccess.h>
14 unsigned securebits
= SECUREBITS_DEFAULT
; /* systemwide security settings */
15 kernel_cap_t cap_bset
= CAP_INIT_EFF_SET
;
18 * This global lock protects task->cap_* for all tasks including current.
19 * Locking rule: acquire this prior to tasklist_lock.
21 spinlock_t task_capability_lock
= SPIN_LOCK_UNLOCKED
;
24 * For sys_getproccap() and sys_setproccap(), any of the three
25 * capability set pointers may be NULL -- indicating that that set is
26 * uninteresting and/or not to be changed.
30 * sys_capget - get the capabilities of a given process.
32 asmlinkage
long sys_capget(cap_user_header_t header
, cap_user_data_t dataptr
)
38 struct __user_cap_data_struct data
;
40 if (get_user(version
, &header
->version
))
43 if (version
!= _LINUX_CAPABILITY_VERSION
) {
44 if (put_user(_LINUX_CAPABILITY_VERSION
, &header
->version
))
49 if (get_user(pid
, &header
->pid
))
55 spin_lock(&task_capability_lock
);
56 read_lock(&tasklist_lock
);
58 if (pid
&& pid
!= current
->pid
) {
59 target
= find_task_by_pid(pid
);
67 ret
= security_capget(target
, &data
.effective
, &data
.inheritable
, &data
.permitted
);
70 read_unlock(&tasklist_lock
);
71 spin_unlock(&task_capability_lock
);
73 if (!ret
&& copy_to_user(dataptr
, &data
, sizeof data
))
80 * cap_set_pg - set capabilities for all processes in a given process
81 * group. We call this holding task_capability_lock and tasklist_lock.
83 static inline void cap_set_pg(int pgrp
, kernel_cap_t
*effective
,
84 kernel_cap_t
*inheritable
,
85 kernel_cap_t
*permitted
)
91 for_each_task_pid(pgrp
, PIDTYPE_PGID
, g
, l
, pid
) {
93 while_each_thread(g
, target
)
94 security_capset_set(target
, effective
, inheritable
, permitted
);
99 * cap_set_all - set capabilities for all processes other than init
100 * and self. We call this holding task_capability_lock and tasklist_lock.
102 static inline void cap_set_all(kernel_cap_t
*effective
,
103 kernel_cap_t
*inheritable
,
104 kernel_cap_t
*permitted
)
108 do_each_thread(g
, target
) {
109 if (target
== current
|| target
->pid
== 1)
111 security_capset_set(target
, effective
, inheritable
, permitted
);
112 } while_each_thread(g
, target
);
116 * sys_capset - set capabilities for a given process, all processes, or all
117 * processes in a given process group.
119 * The restrictions on setting capabilities are specified as:
121 * [pid is for the 'target' task. 'current' is the calling task.]
123 * I: any raised capabilities must be a subset of the (old current) permitted
124 * P: any raised capabilities must be a subset of the (old current) permitted
125 * E: must be set to a subset of (new target) permitted
127 asmlinkage
long sys_capset(cap_user_header_t header
, const cap_user_data_t data
)
129 kernel_cap_t inheritable
, permitted
, effective
;
135 if (get_user(version
, &header
->version
))
138 if (version
!= _LINUX_CAPABILITY_VERSION
) {
139 if (put_user(_LINUX_CAPABILITY_VERSION
, &header
->version
))
144 if (get_user(pid
, &header
->pid
))
147 if (pid
&& !capable(CAP_SETPCAP
))
150 if (copy_from_user(&effective
, &data
->effective
, sizeof(effective
)) ||
151 copy_from_user(&inheritable
, &data
->inheritable
, sizeof(inheritable
)) ||
152 copy_from_user(&permitted
, &data
->permitted
, sizeof(permitted
)))
155 spin_lock(&task_capability_lock
);
156 read_lock(&tasklist_lock
);
158 if (pid
> 0 && pid
!= current
->pid
) {
159 target
= find_task_by_pid(pid
);
169 if (security_capset_check(target
, &effective
, &inheritable
, &permitted
))
172 if (!cap_issubset(inheritable
, cap_combine(target
->cap_inheritable
,
173 current
->cap_permitted
)))
176 /* verify restrictions on target's new Permitted set */
177 if (!cap_issubset(permitted
, cap_combine(target
->cap_permitted
,
178 current
->cap_permitted
)))
181 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
182 if (!cap_issubset(effective
, permitted
))
187 /* having verified that the proposed changes are legal,
188 we now put them into effect. */
190 if (pid
== -1) /* all procs other than current and init */
191 cap_set_all(&effective
, &inheritable
, &permitted
);
193 else /* all procs in process group */
194 cap_set_pg(-pid
, &effective
, &inheritable
, &permitted
);
196 security_capset_set(target
, &effective
, &inheritable
, &permitted
);
200 read_unlock(&tasklist_lock
);
201 spin_unlock(&task_capability_lock
);