1 /* Common capabilities, needed by capability.o and root_plug.o
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
10 #include <linux/capability.h>
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/security.h>
16 #include <linux/file.h>
18 #include <linux/mman.h>
19 #include <linux/pagemap.h>
20 #include <linux/swap.h>
21 #include <linux/smp_lock.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/ptrace.h>
25 #include <linux/xattr.h>
26 #include <linux/hugetlb.h>
27 #include <linux/vs_base.h>
29 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
31 NETLINK_CB(skb).eff_cap = current->cap_effective;
35 EXPORT_SYMBOL(cap_netlink_send);
37 int cap_netlink_recv(struct sk_buff *skb)
39 if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
44 EXPORT_SYMBOL(cap_netlink_recv);
46 int cap_capable (struct task_struct *tsk, int cap)
48 /* Derived from include/linux/sched.h:capable. */
49 if (cap_raised(tsk->cap_effective, cap))
54 int cap_settime(struct timespec *ts, struct timezone *tz)
56 if (!capable(CAP_SYS_TIME))
61 int cap_ptrace (struct task_struct *parent, struct task_struct *child)
63 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
64 if (!cap_issubset (child->cap_permitted, current->cap_permitted) &&
65 !capable(CAP_SYS_PTRACE))
70 int cap_capget (struct task_struct *target, kernel_cap_t *effective,
71 kernel_cap_t *inheritable, kernel_cap_t *permitted)
73 /* Derived from kernel/capability.c:sys_capget. */
74 *effective = cap_t (target->cap_effective);
75 *inheritable = cap_t (target->cap_inheritable);
76 *permitted = cap_t (target->cap_permitted);
80 int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
81 kernel_cap_t *inheritable, kernel_cap_t *permitted)
83 /* Derived from kernel/capability.c:sys_capset. */
84 /* verify restrictions on target's new Inheritable set */
85 if (!cap_issubset (*inheritable,
86 cap_combine (target->cap_inheritable,
87 current->cap_permitted))) {
91 /* verify restrictions on target's new Permitted set */
92 if (!cap_issubset (*permitted,
93 cap_combine (target->cap_permitted,
94 current->cap_permitted))) {
98 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
99 if (!cap_issubset (*effective, *permitted)) {
106 void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
107 kernel_cap_t *inheritable, kernel_cap_t *permitted)
109 target->cap_effective = *effective;
110 target->cap_inheritable = *inheritable;
111 target->cap_permitted = *permitted;
114 int cap_bprm_set_security (struct linux_binprm *bprm)
116 /* Copied from fs/exec.c:prepare_binprm. */
118 /* We don't have VFS support for capabilities yet */
119 cap_clear (bprm->cap_inheritable);
120 cap_clear (bprm->cap_permitted);
121 cap_clear (bprm->cap_effective);
123 /* To support inheritance of root-permissions and suid-root
124 * executables under compatibility mode, we raise all three
125 * capability sets for the file.
127 * If only the real uid is 0, we only raise the inheritable
128 * and permitted sets of the executable file.
131 if (!issecure (SECURE_NOROOT)) {
132 if (bprm->e_uid == 0 || current->uid == 0) {
133 cap_set_full (bprm->cap_inheritable);
134 cap_set_full (bprm->cap_permitted);
136 if (bprm->e_uid == 0)
137 cap_set_full (bprm->cap_effective);
142 void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
144 /* Derived from fs/exec.c:compute_creds. */
145 kernel_cap_t new_permitted, working;
147 new_permitted = cap_intersect (bprm->cap_permitted, vx_current_bcaps());
148 working = cap_intersect (bprm->cap_inheritable,
149 current->cap_inheritable);
150 new_permitted = cap_combine (new_permitted, working);
152 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
153 !cap_issubset (new_permitted, current->cap_permitted)) {
154 current->mm->dumpable = suid_dumpable;
156 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
157 if (!capable(CAP_SETUID)) {
158 bprm->e_uid = current->uid;
159 bprm->e_gid = current->gid;
161 if (!capable (CAP_SETPCAP)) {
162 new_permitted = cap_intersect (new_permitted,
163 current->cap_permitted);
168 current->suid = current->euid = current->fsuid = bprm->e_uid;
169 current->sgid = current->egid = current->fsgid = bprm->e_gid;
171 /* For init, we want to retain the capabilities set
172 * in the init_task struct. Thus we skip the usual
173 * capability rules */
174 if (current->pid != 1) {
175 current->cap_permitted = new_permitted;
176 current->cap_effective =
177 cap_intersect (new_permitted, bprm->cap_effective);
180 /* AUD: Audit candidate if current->cap_effective is set */
182 current->keep_capabilities = 0;
185 int cap_bprm_secureexec (struct linux_binprm *bprm)
187 /* If/when this module is enhanced to incorporate capability
188 bits on files, the test below should be extended to also perform a
189 test between the old and new capability sets. For now,
190 it simply preserves the legacy decision algorithm used by
192 return (current->euid != current->uid ||
193 current->egid != current->gid);
196 int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
197 size_t size, int flags)
199 if (!strncmp(name, XATTR_SECURITY_PREFIX,
200 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
201 !capable(CAP_SYS_ADMIN))
206 int cap_inode_removexattr(struct dentry *dentry, char *name)
208 if (!strncmp(name, XATTR_SECURITY_PREFIX,
209 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
210 !capable(CAP_SYS_ADMIN))
215 /* moved from kernel/sys.c. */
217 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
218 * a process after a call to setuid, setreuid, or setresuid.
220 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
221 * {r,e,s}uid != 0, the permitted and effective capabilities are
224 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
225 * capabilities of the process are cleared.
227 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
228 * capabilities are set to the permitted capabilities.
230 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
235 * cevans - New behaviour, Oct '99
236 * A process may, via prctl(), elect to keep its capabilities when it
237 * calls setuid() and switches away from uid==0. Both permitted and
238 * effective sets will be retained.
239 * Without this change, it was impossible for a daemon to drop only some
240 * of its privilege. The call to setuid(!=0) would drop all privileges!
241 * Keeping uid 0 is not an option because uid 0 owns too many vital
243 * Thanks to Olaf Kirch and Peter Benie for spotting this.
245 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
248 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
249 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
250 !current->keep_capabilities) {
251 cap_clear (current->cap_permitted);
252 cap_clear (current->cap_effective);
254 if (old_euid == 0 && current->euid != 0) {
255 cap_clear (current->cap_effective);
257 if (old_euid != 0 && current->euid == 0) {
258 current->cap_effective = current->cap_permitted;
262 int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
269 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
270 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
271 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
276 uid_t old_fsuid = old_ruid;
278 /* Copied from kernel/sys.c:setfsuid. */
281 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
282 * if not, we might be a bit too harsh here.
285 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
286 if (old_fsuid == 0 && current->fsuid != 0) {
287 cap_t (current->cap_effective) &=
290 if (old_fsuid != 0 && current->fsuid == 0) {
291 cap_t (current->cap_effective) |=
292 (cap_t (current->cap_permitted) &
305 void cap_task_reparent_to_init (struct task_struct *p)
307 p->cap_effective = CAP_INIT_EFF_SET;
308 p->cap_inheritable = CAP_INIT_INH_SET;
309 p->cap_permitted = CAP_FULL_SET;
310 p->keep_capabilities = 0;
314 int cap_syslog (int type)
316 if ((type != 3 && type != 10) &&
317 !vx_capable(CAP_SYS_ADMIN, VXC_SYSLOG))
322 int cap_vm_enough_memory(long pages)
324 int cap_sys_admin = 0;
326 if (cap_capable(current, CAP_SYS_ADMIN) == 0)
328 return __vm_enough_memory(pages, cap_sys_admin);
331 EXPORT_SYMBOL(cap_capable);
332 EXPORT_SYMBOL(cap_settime);
333 EXPORT_SYMBOL(cap_ptrace);
334 EXPORT_SYMBOL(cap_capget);
335 EXPORT_SYMBOL(cap_capset_check);
336 EXPORT_SYMBOL(cap_capset_set);
337 EXPORT_SYMBOL(cap_bprm_set_security);
338 EXPORT_SYMBOL(cap_bprm_apply_creds);
339 EXPORT_SYMBOL(cap_bprm_secureexec);
340 EXPORT_SYMBOL(cap_inode_setxattr);
341 EXPORT_SYMBOL(cap_inode_removexattr);
342 EXPORT_SYMBOL(cap_task_post_setuid);
343 EXPORT_SYMBOL(cap_task_reparent_to_init);
344 EXPORT_SYMBOL(cap_syslog);
345 EXPORT_SYMBOL(cap_vm_enough_memory);
347 MODULE_DESCRIPTION("Standard Linux Common Capabilities Security Module");
348 MODULE_LICENSE("GPL");