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 <asm/uaccess.h>
13 unsigned securebits
= SECUREBITS_DEFAULT
; /* systemwide security settings */
14 kernel_cap_t cap_bset
= CAP_INIT_EFF_SET
;
17 * This global lock protects task->cap_* for all tasks including current.
18 * Locking rule: acquire this prior to tasklist_lock.
20 spinlock_t task_capability_lock
= SPIN_LOCK_UNLOCKED
;
23 * For sys_getproccap() and sys_setproccap(), any of the three
24 * capability set pointers may be NULL -- indicating that that set is
25 * uninteresting and/or not to be changed.
29 * sys_capget - get the capabilities of a given process.
31 asmlinkage
long sys_capget(cap_user_header_t header
, cap_user_data_t dataptr
)
37 struct __user_cap_data_struct data
;
39 if (get_user(version
, &header
->version
))
42 if (version
!= _LINUX_CAPABILITY_VERSION
) {
43 if (put_user(_LINUX_CAPABILITY_VERSION
, &header
->version
))
48 if (get_user(pid
, &header
->pid
))
54 spin_lock(&task_capability_lock
);
55 read_lock(&tasklist_lock
);
57 target
= find_task_by_pid(pid
);
63 data
.permitted
= cap_t(target
->cap_permitted
);
64 data
.inheritable
= cap_t(target
->cap_inheritable
);
65 data
.effective
= cap_t(target
->cap_effective
);
66 ret
= security_ops
->capget(target
, &data
.effective
, &data
.inheritable
, &data
.permitted
);
69 read_unlock(&tasklist_lock
);
70 spin_unlock(&task_capability_lock
);
72 if (!ret
&& copy_to_user(dataptr
, &data
, sizeof data
))
79 * cap_set_pg - set capabilities for all processes in a given process
80 * group. We call this holding task_capability_lock and tasklist_lock.
82 static inline void cap_set_pg(int pgrp
, kernel_cap_t
*effective
,
83 kernel_cap_t
*inheritable
,
84 kernel_cap_t
*permitted
)
88 do_each_thread(g
, target
) {
89 if (target
->pgrp
!= pgrp
)
91 security_ops
->capset_set(target
, effective
, inheritable
, permitted
);
92 } while_each_thread(g
, target
);
96 * cap_set_all - set capabilities for all processes other than init
97 * and self. We call this holding task_capability_lock and tasklist_lock.
99 static inline void cap_set_all(kernel_cap_t
*effective
,
100 kernel_cap_t
*inheritable
,
101 kernel_cap_t
*permitted
)
105 do_each_thread(g
, target
) {
106 if (target
== current
|| target
->pid
== 1)
108 security_ops
->capset_set(target
, effective
, inheritable
, permitted
);
109 } while_each_thread(g
, target
);
113 * sys_capset - set capabilities for a given process, all processes, or all
114 * processes in a given process group.
116 * The restrictions on setting capabilities are specified as:
118 * [pid is for the 'target' task. 'current' is the calling task.]
120 * I: any raised capabilities must be a subset of the (old current) permitted
121 * P: any raised capabilities must be a subset of the (old current) permitted
122 * E: must be set to a subset of (new target) permitted
124 asmlinkage
long sys_capset(cap_user_header_t header
, const cap_user_data_t data
)
126 kernel_cap_t inheritable
, permitted
, effective
;
132 if (get_user(version
, &header
->version
))
135 if (version
!= _LINUX_CAPABILITY_VERSION
) {
136 if (put_user(_LINUX_CAPABILITY_VERSION
, &header
->version
))
141 if (get_user(pid
, &header
->pid
))
144 if (pid
&& !capable(CAP_SETPCAP
))
147 if (copy_from_user(&effective
, &data
->effective
, sizeof(effective
)) ||
148 copy_from_user(&inheritable
, &data
->inheritable
, sizeof(inheritable
)) ||
149 copy_from_user(&permitted
, &data
->permitted
, sizeof(permitted
)))
152 spin_lock(&task_capability_lock
);
153 read_lock(&tasklist_lock
);
155 if (pid
> 0 && pid
!= current
->pid
) {
156 target
= find_task_by_pid(pid
);
166 if (security_ops
->capset_check(target
, &effective
, &inheritable
, &permitted
))
169 if (!cap_issubset(inheritable
, cap_combine(target
->cap_inheritable
,
170 current
->cap_permitted
)))
173 /* verify restrictions on target's new Permitted set */
174 if (!cap_issubset(permitted
, cap_combine(target
->cap_permitted
,
175 current
->cap_permitted
)))
178 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
179 if (!cap_issubset(effective
, permitted
))
184 /* having verified that the proposed changes are legal,
185 we now put them into effect. */
187 if (pid
== -1) /* all procs other than current and init */
188 cap_set_all(&effective
, &inheritable
, &permitted
);
190 else /* all procs in process group */
191 cap_set_pg(-pid
, &effective
, &inheritable
, &permitted
);
193 security_ops
->capset_set(target
, &effective
, &inheritable
, &permitted
);
197 read_unlock(&tasklist_lock
);
198 spin_unlock(&task_capability_lock
);