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 kernel_cap_t cap_bset
= CAP_INIT_EFF_SET
;
13 /* Note: never hold tasklist_lock while spinning for this one */
14 spinlock_t task_capability_lock
= SPIN_LOCK_UNLOCKED
;
17 * For sys_getproccap() and sys_setproccap(), any of the three
18 * capability set pointers may be NULL -- indicating that that set is
19 * uninteresting and/or not to be changed.
22 asmlinkage
long sys_capget(cap_user_header_t header
, cap_user_data_t dataptr
)
26 struct task_struct
*target
;
27 struct __user_cap_data_struct data
;
29 if (get_user(version
, &header
->version
))
33 if (version
!= _LINUX_CAPABILITY_VERSION
) {
34 version
= _LINUX_CAPABILITY_VERSION
;
35 if (put_user(version
, &header
->version
))
40 if (get_user(pid
, &header
->pid
))
48 spin_lock(&task_capability_lock
);
50 if (pid
&& pid
!= current
->pid
) {
51 read_lock(&tasklist_lock
);
52 target
= find_task_by_pid(pid
); /* identify target of query */
60 data
.permitted
= cap_t(target
->cap_permitted
);
61 data
.inheritable
= cap_t(target
->cap_inheritable
);
62 data
.effective
= cap_t(target
->cap_effective
);
65 if (target
!= current
)
66 read_unlock(&tasklist_lock
);
67 spin_unlock(&task_capability_lock
);
70 if (copy_to_user(dataptr
, &data
, sizeof data
))
77 /* set capabilities for all processes in a given process group */
79 static void cap_set_pg(int pgrp
,
80 kernel_cap_t
*effective
,
81 kernel_cap_t
*inheritable
,
82 kernel_cap_t
*permitted
)
84 struct task_struct
*target
;
86 /* FIXME: do we need to have a write lock here..? */
87 read_lock(&tasklist_lock
);
88 for_each_task(target
) {
89 if (target
->pgrp
!= pgrp
)
91 target
->cap_effective
= *effective
;
92 target
->cap_inheritable
= *inheritable
;
93 target
->cap_permitted
= *permitted
;
95 read_unlock(&tasklist_lock
);
98 /* set capabilities for all processes other than 1 and self */
100 static void cap_set_all(kernel_cap_t
*effective
,
101 kernel_cap_t
*inheritable
,
102 kernel_cap_t
*permitted
)
104 struct task_struct
*target
;
106 /* FIXME: do we need to have a write lock here..? */
107 read_lock(&tasklist_lock
);
108 /* ALL means everyone other than self or 'init' */
109 for_each_task(target
) {
110 if (target
== current
|| target
->pid
== 1)
112 target
->cap_effective
= *effective
;
113 target
->cap_inheritable
= *inheritable
;
114 target
->cap_permitted
= *permitted
;
116 read_unlock(&tasklist_lock
);
120 * The restrictions on setting capabilities are specified as:
122 * [pid is for the 'target' task. 'current' is the calling task.]
124 * I: any raised capabilities must be a subset of the (old current) Permitted
125 * P: any raised capabilities must be a subset of the (old current) permitted
126 * E: must be set to a subset of (new target) Permitted
129 asmlinkage
long sys_capset(cap_user_header_t header
, const cap_user_data_t data
)
131 kernel_cap_t inheritable
, permitted
, effective
;
133 struct task_struct
*target
;
136 if (get_user(version
, &header
->version
))
139 if (version
!= _LINUX_CAPABILITY_VERSION
) {
140 version
= _LINUX_CAPABILITY_VERSION
;
141 if (put_user(version
, &header
->version
))
146 if (get_user(pid
, &header
->pid
))
149 if (pid
&& !capable(CAP_SETPCAP
))
152 if (copy_from_user(&effective
, &data
->effective
, sizeof(effective
)) ||
153 copy_from_user(&inheritable
, &data
->inheritable
, sizeof(inheritable
)) ||
154 copy_from_user(&permitted
, &data
->permitted
, sizeof(permitted
)))
158 spin_lock(&task_capability_lock
);
160 if (pid
> 0 && pid
!= current
->pid
) {
161 read_lock(&tasklist_lock
);
162 target
= find_task_by_pid(pid
); /* identify target of query */
172 /* verify restrictions on target's new Inheritable set */
173 if (!cap_issubset(inheritable
,
174 cap_combine(target
->cap_inheritable
,
175 current
->cap_permitted
))) {
179 /* verify restrictions on target's new Permitted set */
180 if (!cap_issubset(permitted
,
181 cap_combine(target
->cap_permitted
,
182 current
->cap_permitted
))) {
186 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
187 if (!cap_issubset(effective
, permitted
)) {
191 /* having verified that the proposed changes are legal,
192 we now put them into effect. */
196 if (pid
== -1) /* all procs other than current and init */
197 cap_set_all(&effective
, &inheritable
, &permitted
);
199 else /* all procs in process group */
200 cap_set_pg(-pid
, &effective
, &inheritable
, &permitted
);
203 /* FIXME: do we need to have a write lock here..? */
204 target
->cap_effective
= effective
;
205 target
->cap_inheritable
= inheritable
;
206 target
->cap_permitted
= permitted
;
210 if (target
!= current
) {
211 read_unlock(&tasklist_lock
);
214 spin_unlock(&task_capability_lock
);