blob: aa5120e59b460eccb6986edb228a3d16ce04d27f [file] [log] [blame]
James Morris3e1c2512009-10-20 13:48:33 +09001/* Common capabilities, needed by capability.o.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Eric Paris3fc689e2008-11-11 21:48:18 +110011#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/security.h>
16#include <linux/file.h>
17#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/pagemap.h>
20#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/netlink.h>
23#include <linux/ptrace.h>
24#include <linux/xattr.h>
25#include <linux/hugetlb.h>
Serge E. Hallynb5376772007-10-16 23:31:36 -070026#include <linux/mount.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070027#include <linux/sched.h>
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -070028#include <linux/prctl.h>
29#include <linux/securebits.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070030#include <linux/user_namespace.h>
Al Viro40401532012-02-13 03:58:52 +000031#include <linux/binfmts.h>
Jonghwan Choi51b79be2012-04-18 17:23:04 -040032#include <linux/personality.h>
Andrew Morgan72c2d582007-10-18 03:05:59 -070033
Chia-chi Yeh04320132009-06-19 07:15:05 +080034#ifdef CONFIG_ANDROID_PARANOID_NETWORK
35#include <linux/android_aid.h>
36#endif
37
Serge E. Hallynb5f22a52009-04-02 18:47:14 -050038/*
39 * If a non-root user executes a setuid-root binary in
40 * !secure(SECURE_NOROOT) mode, then we raise capabilities.
41 * However if fE is also set, then the intent is for only
42 * the file capabilities to be applied, and the setuid-root
43 * bit is left on either to change the uid (plausible) or
44 * to get full privilege on a kernel without file capabilities
45 * support. So in that case we do not raise capabilities.
46 *
47 * Warn if that happens, once per boot.
48 */
David Howellsd7627462010-08-17 23:52:56 +010049static void warn_setuid_and_fcaps_mixed(const char *fname)
Serge E. Hallynb5f22a52009-04-02 18:47:14 -050050{
51 static int warned;
52 if (!warned) {
53 printk(KERN_INFO "warning: `%s' has both setuid-root and"
54 " effective capabilities. Therefore not raising all"
55 " capabilities.\n", fname);
56 warned = 1;
57 }
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
61{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63}
64
David Howells1d045982008-11-14 10:39:24 +110065/**
66 * cap_capable - Determine whether a task has a particular effective capability
David Howells3699c532009-01-06 22:27:01 +000067 * @cred: The credentials to use
Serge E. Hallyn34867402011-03-23 16:43:17 -070068 * @ns: The user namespace in which we need the capability
David Howells1d045982008-11-14 10:39:24 +110069 * @cap: The capability to check for
70 * @audit: Whether to write an audit message or not
71 *
72 * Determine whether the nominated task has the specified capability amongst
73 * its effective set, returning 0 if it does, -ve if it does not.
74 *
David Howells3699c532009-01-06 22:27:01 +000075 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
76 * and has_capability() functions. That is, it has the reverse semantics:
77 * cap_has_capability() returns 0 when a task has a capability, but the
78 * kernel's capable() and has_capability() returns 1 for this case.
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -080079 */
Eric Paris6a9de492012-01-03 12:25:14 -050080int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
81 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Tushar Beherae9a85c72012-03-26 16:54:15 +053083#ifdef CONFIG_ANDROID_PARANOID_NETWORK
Chia-chi Yeh04320132009-06-19 07:15:05 +080084 if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
85 return 0;
86 if (cap == CAP_NET_ADMIN && in_egroup_p(AID_NET_ADMIN))
87 return 0;
Tushar Beherae9a85c72012-03-26 16:54:15 +053088#endif
Chia-chi Yeh04320132009-06-19 07:15:05 +080089
Serge E. Hallyn34867402011-03-23 16:43:17 -070090 for (;;) {
91 /* The creator of the user namespace has all caps. */
92 if (targ_ns != &init_user_ns && targ_ns->creator == cred->user)
93 return 0;
94
95 /* Do we have the necessary capabilities? */
96 if (targ_ns == cred->user->user_ns)
97 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
98
99 /* Have we tried all of the parent namespaces? */
100 if (targ_ns == &init_user_ns)
101 return -EPERM;
102
103 /*
104 *If you have a capability in a parent user ns, then you have
105 * it over all children user namespaces as well.
106 */
107 targ_ns = targ_ns->creator->user_ns;
108 }
109
110 /* We never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
David Howells1d045982008-11-14 10:39:24 +1100113/**
114 * cap_settime - Determine whether the current process may set the system clock
115 * @ts: The time to set
116 * @tz: The timezone to set
117 *
118 * Determine whether the current process may set the system clock and timezone
119 * information, returning 0 if permission granted, -ve if denied.
120 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000121int cap_settime(const struct timespec *ts, const struct timezone *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 if (!capable(CAP_SYS_TIME))
124 return -EPERM;
125 return 0;
126}
127
David Howells1d045982008-11-14 10:39:24 +1100128/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000129 * cap_ptrace_access_check - Determine whether the current process may access
David Howells1d045982008-11-14 10:39:24 +1100130 * another
131 * @child: The process to be accessed
132 * @mode: The mode of attachment.
133 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700134 * If we are in the same or an ancestor user_ns and have all the target
135 * task's capabilities, then ptrace access is allowed.
136 * If we have the ptrace capability to the target user_ns, then ptrace
137 * access is allowed.
138 * Else denied.
139 *
David Howells1d045982008-11-14 10:39:24 +1100140 * Determine whether a process may access another, returning 0 if permission
141 * granted, -ve if denied.
142 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000143int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
David Howellsc69e8d92008-11-14 10:39:19 +1100145 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700146 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100147
148 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700149 cred = current_cred();
150 child_cred = __task_cred(child);
151 if (cred->user->user_ns == child_cred->user->user_ns &&
152 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
153 goto out;
154 if (ns_capable(child_cred->user->user_ns, CAP_SYS_PTRACE))
155 goto out;
156 ret = -EPERM;
157out:
David Howellsc69e8d92008-11-14 10:39:19 +1100158 rcu_read_unlock();
159 return ret;
David Howells5cd9c582008-08-14 11:37:28 +0100160}
161
David Howells1d045982008-11-14 10:39:24 +1100162/**
163 * cap_ptrace_traceme - Determine whether another process may trace the current
164 * @parent: The task proposed to be the tracer
165 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700166 * If parent is in the same or an ancestor user_ns and has all current's
167 * capabilities, then ptrace access is allowed.
168 * If parent has the ptrace capability to current's user_ns, then ptrace
169 * access is allowed.
170 * Else denied.
171 *
David Howells1d045982008-11-14 10:39:24 +1100172 * Determine whether the nominated task is permitted to trace the current
173 * process, returning 0 if permission is granted, -ve if denied.
174 */
David Howells5cd9c582008-08-14 11:37:28 +0100175int cap_ptrace_traceme(struct task_struct *parent)
176{
David Howellsc69e8d92008-11-14 10:39:19 +1100177 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700178 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100179
180 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700181 cred = __task_cred(parent);
182 child_cred = current_cred();
183 if (cred->user->user_ns == child_cred->user->user_ns &&
184 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
185 goto out;
186 if (has_ns_capability(parent, child_cred->user->user_ns, CAP_SYS_PTRACE))
187 goto out;
188 ret = -EPERM;
189out:
David Howellsc69e8d92008-11-14 10:39:19 +1100190 rcu_read_unlock();
191 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
David Howells1d045982008-11-14 10:39:24 +1100194/**
195 * cap_capget - Retrieve a task's capability sets
196 * @target: The task from which to retrieve the capability sets
197 * @effective: The place to record the effective set
198 * @inheritable: The place to record the inheritable set
199 * @permitted: The place to record the permitted set
200 *
201 * This function retrieves the capabilities of the nominated task and returns
202 * them to the caller.
203 */
204int cap_capget(struct task_struct *target, kernel_cap_t *effective,
205 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
David Howellsc69e8d92008-11-14 10:39:19 +1100207 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100210 rcu_read_lock();
211 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100212 *effective = cred->cap_effective;
213 *inheritable = cred->cap_inheritable;
214 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100215 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
David Howells1d045982008-11-14 10:39:24 +1100219/*
220 * Determine whether the inheritable capabilities are limited to the old
221 * permitted set. Returns 1 if they are limited, 0 if they are not.
222 */
Andrew Morgan72c2d582007-10-18 03:05:59 -0700223static inline int cap_inh_is_capped(void)
224{
David Howells1d045982008-11-14 10:39:24 +1100225
226 /* they are so limited unless the current task has the CAP_SETPCAP
227 * capability
Andrew Morgan72c2d582007-10-18 03:05:59 -0700228 */
Eric Paris6a9de492012-01-03 12:25:14 -0500229 if (cap_capable(current_cred(), current_cred()->user->user_ns,
230 CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
David Howells1d045982008-11-14 10:39:24 +1100231 return 0;
David Howells1d045982008-11-14 10:39:24 +1100232 return 1;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700233}
234
David Howells1d045982008-11-14 10:39:24 +1100235/**
236 * cap_capset - Validate and apply proposed changes to current's capabilities
237 * @new: The proposed new credentials; alterations should be made here
238 * @old: The current task's current credentials
239 * @effective: A pointer to the proposed new effective capabilities set
240 * @inheritable: A pointer to the proposed new inheritable capabilities set
241 * @permitted: A pointer to the proposed new permitted capabilities set
242 *
243 * This function validates and applies a proposed mass change to the current
244 * process's capability sets. The changes are made to the proposed new
245 * credentials, and assuming no error, will be committed by the caller of LSM.
246 */
David Howellsd84f4f92008-11-14 10:39:23 +1100247int cap_capset(struct cred *new,
248 const struct cred *old,
249 const kernel_cap_t *effective,
250 const kernel_cap_t *inheritable,
251 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
David Howellsd84f4f92008-11-14 10:39:23 +1100253 if (cap_inh_is_capped() &&
254 !cap_issubset(*inheritable,
255 cap_combine(old->cap_inheritable,
256 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700257 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100259
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800260 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100261 cap_combine(old->cap_inheritable,
262 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800263 /* no new pI capabilities outside bounding set */
264 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100267 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100271 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Howellsd84f4f92008-11-14 10:39:23 +1100274 new->cap_effective = *effective;
275 new->cap_inheritable = *inheritable;
276 new->cap_permitted = *permitted;
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700277
278 /*
279 * Mask off ambient bits that are no longer both permitted and
280 * inheritable.
281 */
282 new->cap_ambient = cap_intersect(new->cap_ambient,
283 cap_intersect(*permitted,
284 *inheritable));
285 if (WARN_ON(!cap_ambient_invariant_ok(new)))
286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288}
289
David Howells1d045982008-11-14 10:39:24 +1100290/*
291 * Clear proposed capability sets for execve().
292 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700293static inline void bprm_clear_caps(struct linux_binprm *bprm)
294{
David Howellsa6f76f22008-11-14 10:39:24 +1100295 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700296 bprm->cap_effective = false;
297}
298
David Howells1d045982008-11-14 10:39:24 +1100299/**
300 * cap_inode_need_killpriv - Determine if inode change affects privileges
301 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
302 *
303 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
304 * affects the security markings on that inode, and if it is, should
305 * inode_killpriv() be invoked or the change rejected?
306 *
307 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
308 * -ve to deny the change.
309 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700310int cap_inode_need_killpriv(struct dentry *dentry)
311{
312 struct inode *inode = dentry->d_inode;
313 int error;
314
Al Viroacfa4382008-12-04 10:06:33 -0500315 if (!inode->i_op->getxattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700316 return 0;
317
318 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
319 if (error <= 0)
320 return 0;
321 return 1;
322}
323
David Howells1d045982008-11-14 10:39:24 +1100324/**
325 * cap_inode_killpriv - Erase the security markings on an inode
326 * @dentry: The inode/dentry to alter
327 *
328 * Erase the privilege-enhancing security markings on an inode.
329 *
330 * Returns 0 if successful, -ve on error.
331 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700332int cap_inode_killpriv(struct dentry *dentry)
333{
334 struct inode *inode = dentry->d_inode;
335
Al Viroacfa4382008-12-04 10:06:33 -0500336 if (!inode->i_op->removexattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700337 return 0;
338
339 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
340}
341
David Howells1d045982008-11-14 10:39:24 +1100342/*
343 * Calculate the new process capability sets from the capability sets attached
344 * to a file.
345 */
Eric Parisc0b00442008-11-11 21:48:10 +1100346static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100347 struct linux_binprm *bprm,
Zhi Li4d49f672011-08-11 13:27:50 +0800348 bool *effective,
349 bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700350{
David Howellsa6f76f22008-11-14 10:39:24 +1100351 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100352 unsigned i;
353 int ret = 0;
354
355 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100356 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100357
Zhi Li4d49f672011-08-11 13:27:50 +0800358 if (caps->magic_etc & VFS_CAP_REVISION_MASK)
359 *has_cap = true;
360
Eric Parisc0b00442008-11-11 21:48:10 +1100361 CAP_FOR_EACH_U32(i) {
362 __u32 permitted = caps->permitted.cap[i];
363 __u32 inheritable = caps->inheritable.cap[i];
364
365 /*
366 * pP' = (X & fP) | (pI & fI)
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700367 * The addition of pA' is handled later.
Eric Parisc0b00442008-11-11 21:48:10 +1100368 */
David Howellsa6f76f22008-11-14 10:39:24 +1100369 new->cap_permitted.cap[i] =
370 (new->cap_bset.cap[i] & permitted) |
371 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100372
David Howellsa6f76f22008-11-14 10:39:24 +1100373 if (permitted & ~new->cap_permitted.cap[i])
374 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100375 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100376 }
377
378 /*
379 * For legacy apps, with no internal support for recognizing they
380 * do not have enough capabilities, we return an error if they are
381 * missing some "forced" (aka file-permitted) capabilities.
382 */
David Howellsa6f76f22008-11-14 10:39:24 +1100383 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100384}
385
David Howells1d045982008-11-14 10:39:24 +1100386/*
387 * Extract the on-exec-apply capability sets for an executable file.
388 */
Eric Parisc0b00442008-11-11 21:48:10 +1100389int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
390{
391 struct inode *inode = dentry->d_inode;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700392 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800393 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100394 int size;
395 struct vfs_cap_data caps;
396
397 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
398
Al Viroacfa4382008-12-04 10:06:33 -0500399 if (!inode || !inode->i_op->getxattr)
Eric Parisc0b00442008-11-11 21:48:10 +1100400 return -ENODATA;
401
402 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
403 XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100404 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100405 /* no data, that's ok */
406 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100407 if (size < 0)
408 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700409
Andrew Morgane338d262008-02-04 22:29:42 -0800410 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700411 return -EINVAL;
412
Eric Parisc0b00442008-11-11 21:48:10 +1100413 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700414
David Howellsa6f76f22008-11-14 10:39:24 +1100415 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800416 case VFS_CAP_REVISION_1:
417 if (size != XATTR_CAPS_SZ_1)
418 return -EINVAL;
419 tocopy = VFS_CAP_U32_1;
420 break;
421 case VFS_CAP_REVISION_2:
422 if (size != XATTR_CAPS_SZ_2)
423 return -EINVAL;
424 tocopy = VFS_CAP_U32_2;
425 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700426 default:
427 return -EINVAL;
428 }
Andrew Morgane338d262008-02-04 22:29:42 -0800429
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700430 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100431 if (i >= tocopy)
432 break;
433 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
434 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800435 }
David Howellsa6f76f22008-11-14 10:39:24 +1100436
Eric Parisc0b00442008-11-11 21:48:10 +1100437 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700438}
439
David Howells1d045982008-11-14 10:39:24 +1100440/*
441 * Attempt to get the on-exec apply capability sets for an executable file from
442 * its xattrs and, if present, apply them to the proposed credentials being
443 * constructed by execve().
444 */
Zhi Li4d49f672011-08-11 13:27:50 +0800445static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700446{
447 struct dentry *dentry;
448 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100449 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700450
Serge Hallyn3318a382008-10-30 11:52:23 -0500451 bprm_clear_caps(bprm);
452
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600453 if (!file_caps_enabled)
454 return 0;
455
Serge Hallyn3318a382008-10-30 11:52:23 -0500456 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700457 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700458
459 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700460
Eric Parisc0b00442008-11-11 21:48:10 +1100461 rc = get_vfs_caps_from_disk(dentry, &vcaps);
462 if (rc < 0) {
463 if (rc == -EINVAL)
464 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
465 __func__, rc, bprm->filename);
466 else if (rc == -ENODATA)
467 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700468 goto out;
469 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700470
Zhi Li4d49f672011-08-11 13:27:50 +0800471 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100472 if (rc == -EINVAL)
473 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
474 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700475
476out:
477 dput(dentry);
478 if (rc)
479 bprm_clear_caps(bprm);
480
481 return rc;
482}
483
David Howells1d045982008-11-14 10:39:24 +1100484/**
485 * cap_bprm_set_creds - Set up the proposed credentials for execve().
486 * @bprm: The execution parameters, including the proposed creds
487 *
488 * Set up the proposed credentials for a new execution context being
489 * constructed by execve(). The proposed creds in @bprm->cred is altered,
490 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100491 */
492int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
David Howellsa6f76f22008-11-14 10:39:24 +1100494 const struct cred *old = current_cred();
495 struct cred *new = bprm->cred;
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700496 bool effective, has_cap = false, is_setid;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700497 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700499 if (WARN_ON(!cap_ambient_invariant_ok(old)))
500 return -EPERM;
501
David Howellsa6f76f22008-11-14 10:39:24 +1100502 effective = false;
Zhi Li4d49f672011-08-11 13:27:50 +0800503 ret = get_file_caps(bprm, &effective, &has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100504 if (ret < 0)
505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700507 if (!issecure(SECURE_NOROOT)) {
508 /*
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500509 * If the legacy file capability is set, then don't set privs
510 * for a setuid root binary run by a non-root user. Do set it
511 * for a root user just to cause least surprise to an admin.
512 */
Zhi Li4d49f672011-08-11 13:27:50 +0800513 if (has_cap && new->uid != 0 && new->euid == 0) {
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500514 warn_setuid_and_fcaps_mixed(bprm->filename);
515 goto skip;
516 }
517 /*
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700518 * To support inheritance of root-permissions and suid-root
519 * executables under compatibility mode, we override the
520 * capability sets for the file.
521 *
David Howellsa6f76f22008-11-14 10:39:24 +1100522 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700523 */
David Howellsa6f76f22008-11-14 10:39:24 +1100524 if (new->euid == 0 || new->uid == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700525 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100526 new->cap_permitted = cap_combine(old->cap_bset,
527 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
David Howellsa6f76f22008-11-14 10:39:24 +1100529 if (new->euid == 0)
530 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500532skip:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700533
Eric Parisd52fc5d2012-04-17 16:26:54 -0400534 /* if we have fs caps, clear dangerous personality flags */
535 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
536 bprm->per_clear |= PER_CLEAR_ON_SETID;
537
538
David Howellsa6f76f22008-11-14 10:39:24 +1100539 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
Andy Lutomirski397a85e2012-01-30 08:17:26 -0800540 * credentials unless they have the appropriate permit.
541 *
542 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
David Howellsa6f76f22008-11-14 10:39:24 +1100543 */
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700544 is_setid = new->euid != old->uid || new->egid != old->gid;
545
546 if ((is_setid ||
David Howellsa6f76f22008-11-14 10:39:24 +1100547 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
548 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
549 /* downgrade; they get no more than they had, and maybe less */
Andy Lutomirski397a85e2012-01-30 08:17:26 -0800550 if (!capable(CAP_SETUID) ||
551 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100552 new->euid = new->uid;
553 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Serge E. Hallynb3a222e2009-11-23 16:21:30 -0600555 new->cap_permitted = cap_intersect(new->cap_permitted,
556 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
David Howellsa6f76f22008-11-14 10:39:24 +1100559 new->suid = new->fsuid = new->euid;
560 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700562 /* File caps or setid cancels ambient. */
563 if (has_cap || is_setid)
564 cap_clear(new->cap_ambient);
565
566 /*
567 * Now that we've computed pA', update pP' to give:
568 * pP' = (X & fP) | (pI & fI) | pA'
569 */
570 new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
571
572 /*
573 * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
574 * this is the same as pE' = (fE ? pP' : 0) | pA'.
575 */
Eric Paris4bf2ea72011-04-01 17:08:28 -0400576 if (effective)
577 new->cap_effective = new->cap_permitted;
578 else
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700579 new->cap_effective = new->cap_ambient;
580
581 if (WARN_ON(!cap_ambient_invariant_ok(new)))
582 return -EPERM;
583
David Howellsa6f76f22008-11-14 10:39:24 +1100584 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Eric Paris3fc689e2008-11-11 21:48:18 +1100586 /*
587 * Audit candidate if current->cap_effective is set
588 *
589 * We do not bother to audit if 3 things are true:
590 * 1) cap_effective has all caps
591 * 2) we are root
592 * 3) root is supposed to have all caps (SECURE_NOROOT)
593 * Since this is just a normal root execing a process.
594 *
595 * Number 1 above might fail if you don't have a full bset, but I think
596 * that is interesting information to audit.
597 */
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700598 if (!cap_issubset(new->cap_effective, new->cap_ambient)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100599 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100600 new->euid != 0 || new->uid != 0 ||
601 issecure(SECURE_NOROOT)) {
602 ret = audit_log_bprm_fcaps(bprm, new, old);
603 if (ret < 0)
604 return ret;
605 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
David Howellsd84f4f92008-11-14 10:39:23 +1100608 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700609
610 if (WARN_ON(!cap_ambient_invariant_ok(new)))
611 return -EPERM;
612
David Howellsa6f76f22008-11-14 10:39:24 +1100613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
David Howells1d045982008-11-14 10:39:24 +1100616/**
617 * cap_bprm_secureexec - Determine whether a secure execution is required
618 * @bprm: The execution parameters
619 *
620 * Determine whether a secure execution is required, return 1 if it is, and 0
621 * if it is not.
622 *
623 * The credentials have been committed by this point, and so are no longer
624 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100625 */
626int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
David Howellsc69e8d92008-11-14 10:39:19 +1100628 const struct cred *cred = current_cred();
David Howellsb6dff3e2008-11-14 10:39:16 +1100629
630 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700631 if (bprm->cap_effective)
632 return 1;
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700633 if (!cap_issubset(cred->cap_permitted, cred->cap_ambient))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700634 return 1;
635 }
636
David Howellsb6dff3e2008-11-14 10:39:16 +1100637 return (cred->euid != cred->uid ||
638 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
David Howells1d045982008-11-14 10:39:24 +1100641/**
642 * cap_inode_setxattr - Determine whether an xattr may be altered
643 * @dentry: The inode/dentry being altered
644 * @name: The name of the xattr to be changed
645 * @value: The value that the xattr will be changed to
646 * @size: The size of value
647 * @flags: The replacement flag
648 *
649 * Determine whether an xattr may be altered or set on an inode, returning 0 if
650 * permission is granted, -ve if denied.
651 *
652 * This is used to make sure security xattrs don't get updated or set by those
653 * who aren't privileged to do so.
654 */
David Howells8f0cfa52008-04-29 00:59:41 -0700655int cap_inode_setxattr(struct dentry *dentry, const char *name,
656 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700658 if (!strcmp(name, XATTR_NAME_CAPS)) {
659 if (!capable(CAP_SETFCAP))
660 return -EPERM;
661 return 0;
David Howells1d045982008-11-14 10:39:24 +1100662 }
663
664 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700665 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 !capable(CAP_SYS_ADMIN))
667 return -EPERM;
668 return 0;
669}
670
David Howells1d045982008-11-14 10:39:24 +1100671/**
672 * cap_inode_removexattr - Determine whether an xattr may be removed
673 * @dentry: The inode/dentry being altered
674 * @name: The name of the xattr to be changed
675 *
676 * Determine whether an xattr may be removed from an inode, returning 0 if
677 * permission is granted, -ve if denied.
678 *
679 * This is used to make sure security xattrs don't get removed by those who
680 * aren't privileged to remove them.
681 */
David Howells8f0cfa52008-04-29 00:59:41 -0700682int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700684 if (!strcmp(name, XATTR_NAME_CAPS)) {
685 if (!capable(CAP_SETFCAP))
686 return -EPERM;
687 return 0;
David Howells1d045982008-11-14 10:39:24 +1100688 }
689
690 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700691 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 !capable(CAP_SYS_ADMIN))
693 return -EPERM;
694 return 0;
695}
696
David Howellsa6f76f22008-11-14 10:39:24 +1100697/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
699 * a process after a call to setuid, setreuid, or setresuid.
700 *
701 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
702 * {r,e,s}uid != 0, the permitted and effective capabilities are
703 * cleared.
704 *
705 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
706 * capabilities of the process are cleared.
707 *
708 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
709 * capabilities are set to the permitted capabilities.
710 *
David Howellsa6f76f22008-11-14 10:39:24 +1100711 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * never happen.
713 *
David Howellsa6f76f22008-11-14 10:39:24 +1100714 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 *
716 * cevans - New behaviour, Oct '99
717 * A process may, via prctl(), elect to keep its capabilities when it
718 * calls setuid() and switches away from uid==0. Both permitted and
719 * effective sets will be retained.
720 * Without this change, it was impossible for a daemon to drop only some
721 * of its privilege. The call to setuid(!=0) would drop all privileges!
722 * Keeping uid 0 is not an option because uid 0 owns too many vital
723 * files..
724 * Thanks to Olaf Kirch and Peter Benie for spotting this.
725 */
David Howellsd84f4f92008-11-14 10:39:23 +1100726static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
David Howellsd84f4f92008-11-14 10:39:23 +1100728 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700729 (new->uid != 0 && new->euid != 0 && new->suid != 0)) {
730 if (!issecure(SECURE_KEEP_CAPS)) {
731 cap_clear(new->cap_permitted);
732 cap_clear(new->cap_effective);
733 }
734 /*
735 * Pre-ambient programs expect setresuid to nonroot followed
736 * by exec to drop capabilities. We should make sure that
737 * this remains the case.
738 */
739 cap_clear(new->cap_ambient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
David Howellsd84f4f92008-11-14 10:39:23 +1100741 if (old->euid == 0 && new->euid != 0)
742 cap_clear(new->cap_effective);
743 if (old->euid != 0 && new->euid == 0)
744 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
David Howells1d045982008-11-14 10:39:24 +1100747/**
748 * cap_task_fix_setuid - Fix up the results of setuid() call
749 * @new: The proposed credentials
750 * @old: The current task's current credentials
751 * @flags: Indications of what has changed
752 *
753 * Fix up the results of setuid() call before the credential changes are
754 * actually applied, returning 0 to grant the changes, -ve to deny them.
755 */
David Howellsd84f4f92008-11-14 10:39:23 +1100756int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 switch (flags) {
759 case LSM_SETID_RE:
760 case LSM_SETID_ID:
761 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100762 /* juggle the capabilities to follow [RES]UID changes unless
763 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100764 if (!issecure(SECURE_NO_SETUID_FIXUP))
765 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
David Howells1d045982008-11-14 10:39:24 +1100768 case LSM_SETID_FS:
769 /* juggle the capabilties to follow FSUID changes, unless
770 * otherwise suppressed
771 *
David Howellsd84f4f92008-11-14 10:39:23 +1100772 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
773 * if not, we might be a bit too harsh here.
774 */
775 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
David Howells1d045982008-11-14 10:39:24 +1100776 if (old->fsuid == 0 && new->fsuid != 0)
David Howellsd84f4f92008-11-14 10:39:23 +1100777 new->cap_effective =
778 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100779
780 if (old->fsuid != 0 && new->fsuid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +1100781 new->cap_effective =
782 cap_raise_fs_set(new->cap_effective,
783 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
David Howellsd84f4f92008-11-14 10:39:23 +1100785 break;
David Howells1d045982008-11-14 10:39:24 +1100786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 default:
788 return -EINVAL;
789 }
790
791 return 0;
792}
793
Serge E. Hallynb5376772007-10-16 23:31:36 -0700794/*
795 * Rationale: code calling task_setscheduler, task_setioprio, and
796 * task_setnice, assumes that
797 * . if capable(cap_sys_nice), then those actions should be allowed
798 * . if not capable(cap_sys_nice), but acting on your own processes,
799 * then those actions should be allowed
800 * This is insufficient now since you can call code without suid, but
801 * yet with increased caps.
802 * So we check for increased caps on the target process.
803 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400804static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700805{
David Howellsc69e8d92008-11-14 10:39:19 +1100806 int is_subset;
807
808 rcu_read_lock();
809 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
810 current_cred()->cap_permitted);
811 rcu_read_unlock();
812
813 if (!is_subset && !capable(CAP_SYS_NICE))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700814 return -EPERM;
815 return 0;
816}
817
David Howells1d045982008-11-14 10:39:24 +1100818/**
819 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
820 * @p: The task to affect
David Howells1d045982008-11-14 10:39:24 +1100821 *
822 * Detemine if the requested scheduler policy change is permitted for the
823 * specified task, returning 0 if permission is granted, -ve if denied.
824 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +0900825int cap_task_setscheduler(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700826{
827 return cap_safe_nice(p);
828}
829
David Howells1d045982008-11-14 10:39:24 +1100830/**
831 * cap_task_ioprio - Detemine if I/O priority change is permitted
832 * @p: The task to affect
833 * @ioprio: The I/O priority to set
834 *
835 * Detemine if the requested I/O priority change is permitted for the specified
836 * task, returning 0 if permission is granted, -ve if denied.
837 */
838int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700839{
840 return cap_safe_nice(p);
841}
842
David Howells1d045982008-11-14 10:39:24 +1100843/**
844 * cap_task_ioprio - Detemine if task priority change is permitted
845 * @p: The task to affect
846 * @nice: The nice value to set
847 *
848 * Detemine if the requested task priority change is permitted for the
849 * specified task, returning 0 if permission is granted, -ve if denied.
850 */
851int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700852{
853 return cap_safe_nice(p);
854}
855
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800856/*
David Howells1d045982008-11-14 10:39:24 +1100857 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
858 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800859 */
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900860static int cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800861{
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900862 struct cred *new;
863
864 if (!ns_capable(current_user_ns(), CAP_SETPCAP))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800865 return -EPERM;
866 if (!cap_valid(cap))
867 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100868
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900869 new = prepare_creds();
870 if (!new)
871 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100872 cap_lower(new->cap_bset, cap);
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900873 return commit_creds(new);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800874}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700875
David Howells1d045982008-11-14 10:39:24 +1100876/**
877 * cap_task_prctl - Implement process control functions for this security module
878 * @option: The process control function requested
879 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
880 *
881 * Allow process control functions (sys_prctl()) to alter capabilities; may
882 * also deny access to other functions not otherwise implemented here.
883 *
884 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
885 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
886 * modules will consider performing the function.
887 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700888int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100889 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700890{
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900891 const struct cred *old = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +1100892 struct cred *new;
David Howellsd84f4f92008-11-14 10:39:23 +1100893
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700894 switch (option) {
895 case PR_CAPBSET_READ:
896 if (!cap_valid(arg2))
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900897 return -EINVAL;
898 return !!cap_raised(old->cap_bset, arg2);
David Howellsd84f4f92008-11-14 10:39:23 +1100899
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700900 case PR_CAPBSET_DROP:
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900901 return cap_prctl_drop(arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700902
903 /*
904 * The next four prctl's remain to assist with transitioning a
905 * system from legacy UID=0 based privilege (when filesystem
906 * capabilities are not in use) to a system using filesystem
907 * capabilities only - as the POSIX.1e draft intended.
908 *
909 * Note:
910 *
911 * PR_SET_SECUREBITS =
912 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
913 * | issecure_mask(SECURE_NOROOT)
914 * | issecure_mask(SECURE_NOROOT_LOCKED)
915 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
916 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
917 *
918 * will ensure that the current process and all of its
919 * children will be locked into a pure
920 * capability-based-privilege environment.
921 */
922 case PR_SET_SECUREBITS:
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900923 if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
924 & (old->securebits ^ arg2)) /*[1]*/
925 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
David Howellsd84f4f92008-11-14 10:39:23 +1100926 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris6a9de492012-01-03 12:25:14 -0500927 || (cap_capable(current_cred(),
Serge E. Hallyn34867402011-03-23 16:43:17 -0700928 current_cred()->user->user_ns, CAP_SETPCAP,
David Howells3699c532009-01-06 22:27:01 +0000929 SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700930 /*
931 * [1] no changing of bits that are locked
932 * [2] no unlocking of locks
933 * [3] no setting of unsupported bits
934 * [4] doing anything requires privilege (go read about
935 * the "sendmail capabilities bug")
936 */
David Howellsd84f4f92008-11-14 10:39:23 +1100937 )
938 /* cannot change a locked bit */
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900939 return -EPERM;
940
941 new = prepare_creds();
942 if (!new)
943 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100944 new->securebits = arg2;
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900945 return commit_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100946
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700947 case PR_GET_SECUREBITS:
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900948 return old->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700949
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700950 case PR_GET_KEEPCAPS:
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900951 return !!issecure(SECURE_KEEP_CAPS);
David Howellsd84f4f92008-11-14 10:39:23 +1100952
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700953 case PR_SET_KEEPCAPS:
954 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900955 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100956 if (issecure(SECURE_KEEP_CAPS_LOCKED))
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900957 return -EPERM;
958
959 new = prepare_creds();
960 if (!new)
961 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100962 if (arg2)
963 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700964 else
David Howellsd84f4f92008-11-14 10:39:23 +1100965 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Tetsuo Handa281fafc2014-07-22 21:20:01 +0900966 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700967
Andy Lutomirskid10c6e12015-09-04 15:42:45 -0700968 case PR_CAP_AMBIENT:
969 if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
970 if (arg3 | arg4 | arg5)
971 return -EINVAL;
972
973 new = prepare_creds();
974 if (!new)
975 return -ENOMEM;
976 cap_clear(new->cap_ambient);
977 return commit_creds(new);
978 }
979
980 if (((!cap_valid(arg3)) | arg4 | arg5))
981 return -EINVAL;
982
983 if (arg2 == PR_CAP_AMBIENT_IS_SET) {
984 return !!cap_raised(current_cred()->cap_ambient, arg3);
985 } else if (arg2 != PR_CAP_AMBIENT_RAISE &&
986 arg2 != PR_CAP_AMBIENT_LOWER) {
987 return -EINVAL;
988 } else {
989 if (arg2 == PR_CAP_AMBIENT_RAISE &&
990 (!cap_raised(current_cred()->cap_permitted, arg3) ||
991 !cap_raised(current_cred()->cap_inheritable,
992 arg3)))
993 return -EPERM;
994
995 new = prepare_creds();
996 if (!new)
997 return -ENOMEM;
998 if (arg2 == PR_CAP_AMBIENT_RAISE)
999 cap_raise(new->cap_ambient, arg3);
1000 else
1001 cap_lower(new->cap_ambient, arg3);
1002 return commit_creds(new);
1003 }
1004
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001005 default:
1006 /* No functionality available - continue with default */
Tetsuo Handa281fafc2014-07-22 21:20:01 +09001007 return -ENOSYS;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
David Howells1d045982008-11-14 10:39:24 +11001011/**
David Howells1d045982008-11-14 10:39:24 +11001012 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
1013 * @mm: The VM space in which the new mapping is to be made
1014 * @pages: The size of the mapping
1015 *
1016 * Determine whether the allocation of a new virtual mapping by the current
1017 * task is permitted, returning 0 if permission is granted, -ve if not.
1018 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001019int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 int cap_sys_admin = 0;
1022
Eric Paris6a9de492012-01-03 12:25:14 -05001023 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00001024 SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -07001026 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
Eric Paris7c738752009-07-31 12:53:58 -04001028
1029/*
1030 * cap_file_mmap - check if able to map given addr
1031 * @file: unused
1032 * @reqprot: unused
1033 * @prot: unused
1034 * @flags: unused
1035 * @addr: address attempting to be mapped
1036 * @addr_only: unused
1037 *
wzt.wzt@gmail.com6f262d82010-04-19 09:16:17 +08001038 * If the process is attempting to map memory below dac_mmap_min_addr they need
Eric Paris7c738752009-07-31 12:53:58 -04001039 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
1040 * capability security module. Returns 0 if this mapping should be allowed
1041 * -EPERM if not.
1042 */
1043int cap_file_mmap(struct file *file, unsigned long reqprot,
1044 unsigned long prot, unsigned long flags,
1045 unsigned long addr, unsigned long addr_only)
1046{
1047 int ret = 0;
1048
Eric Parisa2551df2009-07-31 12:54:11 -04001049 if (addr < dac_mmap_min_addr) {
Eric Paris6a9de492012-01-03 12:25:14 -05001050 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
Eric Paris7c738752009-07-31 12:53:58 -04001051 SECURITY_CAP_AUDIT);
1052 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1053 if (ret == 0)
1054 current->flags |= PF_SUPERPRIV;
1055 }
1056 return ret;
1057}