2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
5 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com>
9 #include <asm/uaccess.h>
11 /* Note: never hold tasklist_lock while spinning for this one */
12 spinlock_t task_capability_lock
;
15 * For sys_getproccap() and sys_setproccap(), any of the three
16 * capability set pointers may be NULL -- indicating that that set is
17 * uninteresting and/or not to be changed.
20 asmlinkage
int sys_capget(cap_user_header_t header
, cap_user_data_t dataptr
)
24 struct task_struct
*target
;
25 struct __user_cap_data_struct data
;
27 if (get_user(version
, &header
->version
))
31 if (version
!= _LINUX_CAPABILITY_VERSION
) {
32 version
= _LINUX_CAPABILITY_VERSION
;
33 if (put_user(version
, &header
->version
))
38 if (get_user(pid
, &header
->pid
))
46 spin_lock(&task_capability_lock
);
48 if (pid
&& pid
!= current
->pid
) {
49 read_lock(&tasklist_lock
);
50 target
= find_task_by_pid(pid
); /* identify target of query */
58 data
.permitted
= cap_t(target
->cap_permitted
);
59 data
.inheritable
= cap_t(target
->cap_inheritable
);
60 data
.effective
= cap_t(target
->cap_effective
);
63 if (target
!= current
)
64 read_unlock(&tasklist_lock
);
65 spin_unlock(&task_capability_lock
);
68 if (copy_to_user(dataptr
, &data
, sizeof data
))
75 /* set capabilities for all processes in a given process group */
77 static void cap_set_pg(int pgrp
,
78 kernel_cap_t
*effective
,
79 kernel_cap_t
*inheritable
,
80 kernel_cap_t
*permitted
)
82 struct task_struct
*target
;
84 /* FIXME: do we need to have a write lock here..? */
85 read_lock(&tasklist_lock
);
86 for_each_task(target
) {
87 if (target
->pgrp
!= pgrp
)
89 target
->cap_effective
= *effective
;
90 target
->cap_inheritable
= *inheritable
;
91 target
->cap_permitted
= *permitted
;
93 read_unlock(&tasklist_lock
);
96 /* set capabilities for all processes other than 1 and self */
98 static void cap_set_all(kernel_cap_t
*effective
,
99 kernel_cap_t
*inheritable
,
100 kernel_cap_t
*permitted
)
102 struct task_struct
*target
;
104 /* FIXME: do we need to have a write lock here..? */
105 read_lock(&tasklist_lock
);
106 /* ALL means everyone other than self or 'init' */
107 for_each_task(target
) {
108 if (target
== current
|| target
->pid
== 1)
110 target
->cap_effective
= *effective
;
111 target
->cap_inheritable
= *inheritable
;
112 target
->cap_permitted
= *permitted
;
114 read_unlock(&tasklist_lock
);
118 * The restrictions on setting capabilities are specified as:
120 * [pid is for the 'target' task. 'current' is the calling task.]
122 * I: any raised capabilities must be a subset of the (old current) Permitted
123 * P: any raised capabilities must be a subset of the (old current) permitted
124 * E: must be set to a subset of (new target) Permitted
127 asmlinkage
int sys_capset(cap_user_header_t header
, const cap_user_data_t data
)
129 kernel_cap_t inheritable
, permitted
, effective
;
131 struct task_struct
*target
;
134 if (get_user(version
, &header
->version
))
137 if (version
!= _LINUX_CAPABILITY_VERSION
) {
138 version
= _LINUX_CAPABILITY_VERSION
;
139 if (put_user(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
)))
156 spin_lock(&task_capability_lock
);
158 if (pid
> 0 && pid
!= current
->pid
) {
159 read_lock(&tasklist_lock
);
160 target
= find_task_by_pid(pid
); /* identify target of query */
170 /* verify restrictions on target's new Inheritable set */
171 if (!cap_issubset(inheritable
,
172 cap_combine(target
->cap_inheritable
,
173 current
->cap_permitted
))) {
177 /* verify restrictions on target's new Permitted set */
178 if (!cap_issubset(permitted
,
179 cap_combine(target
->cap_permitted
,
180 current
->cap_permitted
))) {
184 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
185 if (!cap_issubset(effective
, permitted
)) {
189 /* having verified that the proposed changes are legal,
190 we now put them into effect. */
194 if (pid
== -1) /* all procs other than current and init */
195 cap_set_all(&effective
, &inheritable
, &permitted
);
197 else /* all procs in process group */
198 cap_set_pg(-pid
, &effective
, &inheritable
, &permitted
);
201 /* FIXME: do we need to have a write lock here..? */
202 target
->cap_effective
= effective
;
203 target
->cap_inheritable
= inheritable
;
204 target
->cap_permitted
= permitted
;
208 if (target
!= current
) {
209 read_unlock(&tasklist_lock
);
212 spin_unlock(&task_capability_lock
);