blob: a8e4aacf0b0c0e9426038e67918db4b1b22a8d19 [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>
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -070015#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 Yeh198bc8c2009-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
David Howells1d045982008-11-14 10:39:24 +110060/**
61 * cap_capable - Determine whether a task has a particular effective capability
David Howells3699c532009-01-06 22:27:01 +000062 * @cred: The credentials to use
Serge E. Hallyn34867402011-03-23 16:43:17 -070063 * @ns: The user namespace in which we need the capability
David Howells1d045982008-11-14 10:39:24 +110064 * @cap: The capability to check for
65 * @audit: Whether to write an audit message or not
66 *
67 * Determine whether the nominated task has the specified capability amongst
68 * its effective set, returning 0 if it does, -ve if it does not.
69 *
David Howells3699c532009-01-06 22:27:01 +000070 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
71 * and has_capability() functions. That is, it has the reverse semantics:
72 * cap_has_capability() returns 0 when a task has a capability, but the
73 * kernel's capable() and has_capability() returns 1 for this case.
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -080074 */
Eric Paris6a9de492012-01-03 12:25:14 -050075int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
76 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080078 struct user_namespace *ns = targ_ns;
Serge E. Hallyn34867402011-03-23 16:43:17 -070079
Tushar Behera94cec892012-03-26 16:54:15 +053080#ifdef CONFIG_ANDROID_PARANOID_NETWORK
Chia-chi Yeh198bc8c2009-06-19 07:15:05 +080081 if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
82 return 0;
83 if (cap == CAP_NET_ADMIN && in_egroup_p(AID_NET_ADMIN))
84 return 0;
Tushar Behera94cec892012-03-26 16:54:15 +053085#endif
Chia-chi Yeh198bc8c2009-06-19 07:15:05 +080086
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080087 /* See if cred has the capability in the target user namespace
88 * by examining the target user namespace and all of the target
89 * user namespace's parents.
90 */
91 for (;;) {
Serge E. Hallyn34867402011-03-23 16:43:17 -070092 /* Do we have the necessary capabilities? */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080093 if (ns == cred->user_ns)
Serge E. Hallyn34867402011-03-23 16:43:17 -070094 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
95
96 /* Have we tried all of the parent namespaces? */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080097 if (ns == &init_user_ns)
Serge E. Hallyn34867402011-03-23 16:43:17 -070098 return -EPERM;
99
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800100 /*
101 * The owner of the user namespace in the parent of the
102 * user namespace has all caps.
103 */
104 if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
105 return 0;
106
Serge E. Hallyn34867402011-03-23 16:43:17 -0700107 /*
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800108 * If you have a capability in a parent user ns, then you have
Serge E. Hallyn34867402011-03-23 16:43:17 -0700109 * it over all children user namespaces as well.
110 */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800111 ns = ns->parent;
Serge E. Hallyn34867402011-03-23 16:43:17 -0700112 }
113
114 /* We never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
David Howells1d045982008-11-14 10:39:24 +1100117/**
118 * cap_settime - Determine whether the current process may set the system clock
119 * @ts: The time to set
120 * @tz: The timezone to set
121 *
122 * Determine whether the current process may set the system clock and timezone
123 * information, returning 0 if permission granted, -ve if denied.
124 */
Baolin Wang457db292016-04-08 14:02:11 +0800125int cap_settime(const struct timespec64 *ts, const struct timezone *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 if (!capable(CAP_SYS_TIME))
128 return -EPERM;
129 return 0;
130}
131
David Howells1d045982008-11-14 10:39:24 +1100132/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000133 * cap_ptrace_access_check - Determine whether the current process may access
David Howells1d045982008-11-14 10:39:24 +1100134 * another
135 * @child: The process to be accessed
136 * @mode: The mode of attachment.
137 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700138 * If we are in the same or an ancestor user_ns and have all the target
139 * task's capabilities, then ptrace access is allowed.
140 * If we have the ptrace capability to the target user_ns, then ptrace
141 * access is allowed.
142 * Else denied.
143 *
David Howells1d045982008-11-14 10:39:24 +1100144 * Determine whether a process may access another, returning 0 if permission
145 * granted, -ve if denied.
146 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000147int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
David Howellsc69e8d92008-11-14 10:39:19 +1100149 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700150 const struct cred *cred, *child_cred;
Jann Horncaaee622016-01-20 15:00:04 -0800151 const kernel_cap_t *caller_caps;
David Howellsc69e8d92008-11-14 10:39:19 +1100152
153 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700154 cred = current_cred();
155 child_cred = __task_cred(child);
Jann Horncaaee622016-01-20 15:00:04 -0800156 if (mode & PTRACE_MODE_FSCREDS)
157 caller_caps = &cred->cap_effective;
158 else
159 caller_caps = &cred->cap_permitted;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800160 if (cred->user_ns == child_cred->user_ns &&
Jann Horncaaee622016-01-20 15:00:04 -0800161 cap_issubset(child_cred->cap_permitted, *caller_caps))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700162 goto out;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800163 if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700164 goto out;
165 ret = -EPERM;
166out:
David Howellsc69e8d92008-11-14 10:39:19 +1100167 rcu_read_unlock();
168 return ret;
David Howells5cd9c582008-08-14 11:37:28 +0100169}
170
David Howells1d045982008-11-14 10:39:24 +1100171/**
172 * cap_ptrace_traceme - Determine whether another process may trace the current
173 * @parent: The task proposed to be the tracer
174 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700175 * If parent is in the same or an ancestor user_ns and has all current's
176 * capabilities, then ptrace access is allowed.
177 * If parent has the ptrace capability to current's user_ns, then ptrace
178 * access is allowed.
179 * Else denied.
180 *
David Howells1d045982008-11-14 10:39:24 +1100181 * Determine whether the nominated task is permitted to trace the current
182 * process, returning 0 if permission is granted, -ve if denied.
183 */
David Howells5cd9c582008-08-14 11:37:28 +0100184int cap_ptrace_traceme(struct task_struct *parent)
185{
David Howellsc69e8d92008-11-14 10:39:19 +1100186 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700187 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100188
189 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700190 cred = __task_cred(parent);
191 child_cred = current_cred();
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800192 if (cred->user_ns == child_cred->user_ns &&
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700193 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
194 goto out;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800195 if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700196 goto out;
197 ret = -EPERM;
198out:
David Howellsc69e8d92008-11-14 10:39:19 +1100199 rcu_read_unlock();
200 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
David Howells1d045982008-11-14 10:39:24 +1100203/**
204 * cap_capget - Retrieve a task's capability sets
205 * @target: The task from which to retrieve the capability sets
206 * @effective: The place to record the effective set
207 * @inheritable: The place to record the inheritable set
208 * @permitted: The place to record the permitted set
209 *
210 * This function retrieves the capabilities of the nominated task and returns
211 * them to the caller.
212 */
213int cap_capget(struct task_struct *target, kernel_cap_t *effective,
214 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
David Howellsc69e8d92008-11-14 10:39:19 +1100216 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100219 rcu_read_lock();
220 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100221 *effective = cred->cap_effective;
222 *inheritable = cred->cap_inheritable;
223 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226}
227
David Howells1d045982008-11-14 10:39:24 +1100228/*
229 * Determine whether the inheritable capabilities are limited to the old
230 * permitted set. Returns 1 if they are limited, 0 if they are not.
231 */
Andrew Morgan72c2d582007-10-18 03:05:59 -0700232static inline int cap_inh_is_capped(void)
233{
David Howells1d045982008-11-14 10:39:24 +1100234
235 /* they are so limited unless the current task has the CAP_SETPCAP
236 * capability
Andrew Morgan72c2d582007-10-18 03:05:59 -0700237 */
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800238 if (cap_capable(current_cred(), current_cred()->user_ns,
Eric Paris6a9de492012-01-03 12:25:14 -0500239 CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
David Howells1d045982008-11-14 10:39:24 +1100240 return 0;
David Howells1d045982008-11-14 10:39:24 +1100241 return 1;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700242}
243
David Howells1d045982008-11-14 10:39:24 +1100244/**
245 * cap_capset - Validate and apply proposed changes to current's capabilities
246 * @new: The proposed new credentials; alterations should be made here
247 * @old: The current task's current credentials
248 * @effective: A pointer to the proposed new effective capabilities set
249 * @inheritable: A pointer to the proposed new inheritable capabilities set
250 * @permitted: A pointer to the proposed new permitted capabilities set
251 *
252 * This function validates and applies a proposed mass change to the current
253 * process's capability sets. The changes are made to the proposed new
254 * credentials, and assuming no error, will be committed by the caller of LSM.
255 */
David Howellsd84f4f92008-11-14 10:39:23 +1100256int cap_capset(struct cred *new,
257 const struct cred *old,
258 const kernel_cap_t *effective,
259 const kernel_cap_t *inheritable,
260 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howellsd84f4f92008-11-14 10:39:23 +1100262 if (cap_inh_is_capped() &&
263 !cap_issubset(*inheritable,
264 cap_combine(old->cap_inheritable,
265 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700266 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100268
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800269 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100270 cap_combine(old->cap_inheritable,
271 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800272 /* no new pI capabilities outside bounding set */
273 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100276 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100280 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David Howellsd84f4f92008-11-14 10:39:23 +1100283 new->cap_effective = *effective;
284 new->cap_inheritable = *inheritable;
285 new->cap_permitted = *permitted;
Andy Lutomirski58319052015-09-04 15:42:45 -0700286
287 /*
288 * Mask off ambient bits that are no longer both permitted and
289 * inheritable.
290 */
291 new->cap_ambient = cap_intersect(new->cap_ambient,
292 cap_intersect(*permitted,
293 *inheritable));
294 if (WARN_ON(!cap_ambient_invariant_ok(new)))
295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297}
298
David Howells1d045982008-11-14 10:39:24 +1100299/*
300 * Clear proposed capability sets for execve().
301 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700302static inline void bprm_clear_caps(struct linux_binprm *bprm)
303{
David Howellsa6f76f22008-11-14 10:39:24 +1100304 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700305 bprm->cap_effective = false;
306}
307
David Howells1d045982008-11-14 10:39:24 +1100308/**
309 * cap_inode_need_killpriv - Determine if inode change affects privileges
310 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
311 *
312 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
313 * affects the security markings on that inode, and if it is, should
314 * inode_killpriv() be invoked or the change rejected?
315 *
316 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
317 * -ve to deny the change.
318 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700319int cap_inode_need_killpriv(struct dentry *dentry)
320{
David Howellsc6f493d2015-03-17 22:26:22 +0000321 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700322 int error;
323
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200324 error = __vfs_getxattr(dentry, inode, XATTR_NAME_CAPS, NULL, 0);
325 return error > 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700326}
327
David Howells1d045982008-11-14 10:39:24 +1100328/**
329 * cap_inode_killpriv - Erase the security markings on an inode
330 * @dentry: The inode/dentry to alter
331 *
332 * Erase the privilege-enhancing security markings on an inode.
333 *
334 * Returns 0 if successful, -ve on error.
335 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700336int cap_inode_killpriv(struct dentry *dentry)
337{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200338 int error;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700339
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200340 error = __vfs_removexattr(dentry, XATTR_NAME_CAPS);
341 if (error == -EOPNOTSUPP)
342 error = 0;
343 return error;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700344}
345
David Howells1d045982008-11-14 10:39:24 +1100346/*
347 * Calculate the new process capability sets from the capability sets attached
348 * to a file.
349 */
Eric Parisc0b00442008-11-11 21:48:10 +1100350static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100351 struct linux_binprm *bprm,
Zhi Li4d49f672011-08-11 13:27:50 +0800352 bool *effective,
353 bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700354{
David Howellsa6f76f22008-11-14 10:39:24 +1100355 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100356 unsigned i;
357 int ret = 0;
358
359 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100360 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100361
Zhi Li4d49f672011-08-11 13:27:50 +0800362 if (caps->magic_etc & VFS_CAP_REVISION_MASK)
363 *has_cap = true;
364
Eric Parisc0b00442008-11-11 21:48:10 +1100365 CAP_FOR_EACH_U32(i) {
366 __u32 permitted = caps->permitted.cap[i];
367 __u32 inheritable = caps->inheritable.cap[i];
368
369 /*
370 * pP' = (X & fP) | (pI & fI)
Andy Lutomirski58319052015-09-04 15:42:45 -0700371 * The addition of pA' is handled later.
Eric Parisc0b00442008-11-11 21:48:10 +1100372 */
David Howellsa6f76f22008-11-14 10:39:24 +1100373 new->cap_permitted.cap[i] =
374 (new->cap_bset.cap[i] & permitted) |
375 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100376
David Howellsa6f76f22008-11-14 10:39:24 +1100377 if (permitted & ~new->cap_permitted.cap[i])
378 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100379 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100380 }
381
382 /*
383 * For legacy apps, with no internal support for recognizing they
384 * do not have enough capabilities, we return an error if they are
385 * missing some "forced" (aka file-permitted) capabilities.
386 */
David Howellsa6f76f22008-11-14 10:39:24 +1100387 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100388}
389
David Howells1d045982008-11-14 10:39:24 +1100390/*
391 * Extract the on-exec-apply capability sets for an executable file.
392 */
Eric Parisc0b00442008-11-11 21:48:10 +1100393int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
394{
David Howellsc6f493d2015-03-17 22:26:22 +0000395 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700396 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800397 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100398 int size;
399 struct vfs_cap_data caps;
400
401 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
402
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200403 if (!inode)
Eric Parisc0b00442008-11-11 21:48:10 +1100404 return -ENODATA;
405
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200406 size = __vfs_getxattr((struct dentry *)dentry, inode,
407 XATTR_NAME_CAPS, &caps, XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100408 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100409 /* no data, that's ok */
410 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100411 if (size < 0)
412 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700413
Andrew Morgane338d262008-02-04 22:29:42 -0800414 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700415 return -EINVAL;
416
Eric Parisc0b00442008-11-11 21:48:10 +1100417 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700418
David Howellsa6f76f22008-11-14 10:39:24 +1100419 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800420 case VFS_CAP_REVISION_1:
421 if (size != XATTR_CAPS_SZ_1)
422 return -EINVAL;
423 tocopy = VFS_CAP_U32_1;
424 break;
425 case VFS_CAP_REVISION_2:
426 if (size != XATTR_CAPS_SZ_2)
427 return -EINVAL;
428 tocopy = VFS_CAP_U32_2;
429 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700430 default:
431 return -EINVAL;
432 }
Andrew Morgane338d262008-02-04 22:29:42 -0800433
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700434 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100435 if (i >= tocopy)
436 break;
437 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
438 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800439 }
David Howellsa6f76f22008-11-14 10:39:24 +1100440
Eric Paris7d8b6c62014-07-23 15:36:26 -0400441 cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
442 cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
443
Eric Parisc0b00442008-11-11 21:48:10 +1100444 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700445}
446
David Howells1d045982008-11-14 10:39:24 +1100447/*
448 * Attempt to get the on-exec apply capability sets for an executable file from
449 * its xattrs and, if present, apply them to the proposed credentials being
450 * constructed by execve().
451 */
Zhi Li4d49f672011-08-11 13:27:50 +0800452static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700453{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700454 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100455 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700456
Serge Hallyn3318a382008-10-30 11:52:23 -0500457 bprm_clear_caps(bprm);
458
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600459 if (!file_caps_enabled)
460 return 0;
461
Andy Lutomirski380cf5b2016-06-23 16:41:05 -0500462 if (!mnt_may_suid(bprm->file->f_path.mnt))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700463 return 0;
Andy Lutomirski380cf5b2016-06-23 16:41:05 -0500464
465 /*
466 * This check is redundant with mnt_may_suid() but is kept to make
467 * explicit that capability bits are limited to s_user_ns and its
468 * descendants.
469 */
Seth Forsheed07b8462015-09-23 15:16:04 -0500470 if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
471 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700472
Al Virof4a4a8b2014-12-28 09:27:07 -0500473 rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
Eric Parisc0b00442008-11-11 21:48:10 +1100474 if (rc < 0) {
475 if (rc == -EINVAL)
476 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
477 __func__, rc, bprm->filename);
478 else if (rc == -ENODATA)
479 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700480 goto out;
481 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700482
Zhi Li4d49f672011-08-11 13:27:50 +0800483 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100484 if (rc == -EINVAL)
485 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
486 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700487
488out:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700489 if (rc)
490 bprm_clear_caps(bprm);
491
492 return rc;
493}
494
David Howells1d045982008-11-14 10:39:24 +1100495/**
496 * cap_bprm_set_creds - Set up the proposed credentials for execve().
497 * @bprm: The execution parameters, including the proposed creds
498 *
499 * Set up the proposed credentials for a new execution context being
500 * constructed by execve(). The proposed creds in @bprm->cred is altered,
501 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100502 */
503int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
David Howellsa6f76f22008-11-14 10:39:24 +1100505 const struct cred *old = current_cred();
506 struct cred *new = bprm->cred;
Andy Lutomirski58319052015-09-04 15:42:45 -0700507 bool effective, has_cap = false, is_setid;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700508 int ret;
Eric W. Biederman18815a12012-02-07 16:45:47 -0800509 kuid_t root_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Andy Lutomirski58319052015-09-04 15:42:45 -0700511 if (WARN_ON(!cap_ambient_invariant_ok(old)))
512 return -EPERM;
513
David Howellsa6f76f22008-11-14 10:39:24 +1100514 effective = false;
Zhi Li4d49f672011-08-11 13:27:50 +0800515 ret = get_file_caps(bprm, &effective, &has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100516 if (ret < 0)
517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Eric W. Biederman18815a12012-02-07 16:45:47 -0800519 root_uid = make_kuid(new->user_ns, 0);
520
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700521 if (!issecure(SECURE_NOROOT)) {
522 /*
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500523 * If the legacy file capability is set, then don't set privs
524 * for a setuid root binary run by a non-root user. Do set it
525 * for a root user just to cause least surprise to an admin.
526 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800527 if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500528 warn_setuid_and_fcaps_mixed(bprm->filename);
529 goto skip;
530 }
531 /*
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700532 * To support inheritance of root-permissions and suid-root
533 * executables under compatibility mode, we override the
534 * capability sets for the file.
535 *
David Howellsa6f76f22008-11-14 10:39:24 +1100536 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700537 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800538 if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700539 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100540 new->cap_permitted = cap_combine(old->cap_bset,
541 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800543 if (uid_eq(new->euid, root_uid))
David Howellsa6f76f22008-11-14 10:39:24 +1100544 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500546skip:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700547
Eric Parisd52fc5d2012-04-17 16:26:54 -0400548 /* if we have fs caps, clear dangerous personality flags */
549 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
550 bprm->per_clear |= PER_CLEAR_ON_SETID;
551
552
David Howellsa6f76f22008-11-14 10:39:24 +1100553 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500554 * credentials unless they have the appropriate permit.
555 *
556 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
David Howellsa6f76f22008-11-14 10:39:24 +1100557 */
Andy Lutomirski58319052015-09-04 15:42:45 -0700558 is_setid = !uid_eq(new->euid, old->uid) || !gid_eq(new->egid, old->gid);
559
560 if ((is_setid ||
David Howellsa6f76f22008-11-14 10:39:24 +1100561 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
562 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
563 /* downgrade; they get no more than they had, and maybe less */
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500564 if (!capable(CAP_SETUID) ||
565 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100566 new->euid = new->uid;
567 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Serge E. Hallynb3a222e2009-11-23 16:21:30 -0600569 new->cap_permitted = cap_intersect(new->cap_permitted,
570 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
David Howellsa6f76f22008-11-14 10:39:24 +1100573 new->suid = new->fsuid = new->euid;
574 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Andy Lutomirski58319052015-09-04 15:42:45 -0700576 /* File caps or setid cancels ambient. */
577 if (has_cap || is_setid)
578 cap_clear(new->cap_ambient);
579
580 /*
581 * Now that we've computed pA', update pP' to give:
582 * pP' = (X & fP) | (pI & fI) | pA'
583 */
584 new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
585
586 /*
587 * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
588 * this is the same as pE' = (fE ? pP' : 0) | pA'.
589 */
Eric Paris4bf2ea72011-04-01 17:08:28 -0400590 if (effective)
591 new->cap_effective = new->cap_permitted;
592 else
Andy Lutomirski58319052015-09-04 15:42:45 -0700593 new->cap_effective = new->cap_ambient;
594
595 if (WARN_ON(!cap_ambient_invariant_ok(new)))
596 return -EPERM;
597
David Howellsa6f76f22008-11-14 10:39:24 +1100598 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Eric Paris3fc689e2008-11-11 21:48:18 +1100600 /*
601 * Audit candidate if current->cap_effective is set
602 *
603 * We do not bother to audit if 3 things are true:
604 * 1) cap_effective has all caps
605 * 2) we are root
606 * 3) root is supposed to have all caps (SECURE_NOROOT)
607 * Since this is just a normal root execing a process.
608 *
609 * Number 1 above might fail if you don't have a full bset, but I think
610 * that is interesting information to audit.
611 */
Andy Lutomirski58319052015-09-04 15:42:45 -0700612 if (!cap_issubset(new->cap_effective, new->cap_ambient)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100613 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
Eric W. Biederman18815a12012-02-07 16:45:47 -0800614 !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100615 issecure(SECURE_NOROOT)) {
616 ret = audit_log_bprm_fcaps(bprm, new, old);
617 if (ret < 0)
618 return ret;
619 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
David Howellsd84f4f92008-11-14 10:39:23 +1100622 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Andy Lutomirski58319052015-09-04 15:42:45 -0700623
624 if (WARN_ON(!cap_ambient_invariant_ok(new)))
625 return -EPERM;
626
David Howellsa6f76f22008-11-14 10:39:24 +1100627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
David Howells1d045982008-11-14 10:39:24 +1100630/**
631 * cap_bprm_secureexec - Determine whether a secure execution is required
632 * @bprm: The execution parameters
633 *
634 * Determine whether a secure execution is required, return 1 if it is, and 0
635 * if it is not.
636 *
637 * The credentials have been committed by this point, and so are no longer
638 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100639 */
640int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
David Howellsc69e8d92008-11-14 10:39:19 +1100642 const struct cred *cred = current_cred();
Eric W. Biederman18815a12012-02-07 16:45:47 -0800643 kuid_t root_uid = make_kuid(cred->user_ns, 0);
David Howellsb6dff3e2008-11-14 10:39:16 +1100644
Eric W. Biederman18815a12012-02-07 16:45:47 -0800645 if (!uid_eq(cred->uid, root_uid)) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700646 if (bprm->cap_effective)
647 return 1;
Andy Lutomirski58319052015-09-04 15:42:45 -0700648 if (!cap_issubset(cred->cap_permitted, cred->cap_ambient))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700649 return 1;
650 }
651
Eric W. Biederman18815a12012-02-07 16:45:47 -0800652 return (!uid_eq(cred->euid, cred->uid) ||
653 !gid_eq(cred->egid, cred->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
David Howells1d045982008-11-14 10:39:24 +1100656/**
657 * cap_inode_setxattr - Determine whether an xattr may be altered
658 * @dentry: The inode/dentry being altered
659 * @name: The name of the xattr to be changed
660 * @value: The value that the xattr will be changed to
661 * @size: The size of value
662 * @flags: The replacement flag
663 *
664 * Determine whether an xattr may be altered or set on an inode, returning 0 if
665 * permission is granted, -ve if denied.
666 *
667 * This is used to make sure security xattrs don't get updated or set by those
668 * who aren't privileged to do so.
669 */
David Howells8f0cfa52008-04-29 00:59:41 -0700670int cap_inode_setxattr(struct dentry *dentry, const char *name,
671 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700673 if (!strcmp(name, XATTR_NAME_CAPS)) {
674 if (!capable(CAP_SETFCAP))
675 return -EPERM;
676 return 0;
David Howells1d045982008-11-14 10:39:24 +1100677 }
678
679 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700680 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 !capable(CAP_SYS_ADMIN))
682 return -EPERM;
683 return 0;
684}
685
David Howells1d045982008-11-14 10:39:24 +1100686/**
687 * cap_inode_removexattr - Determine whether an xattr may be removed
688 * @dentry: The inode/dentry being altered
689 * @name: The name of the xattr to be changed
690 *
691 * Determine whether an xattr may be removed from an inode, returning 0 if
692 * permission is granted, -ve if denied.
693 *
694 * This is used to make sure security xattrs don't get removed by those who
695 * aren't privileged to remove them.
696 */
David Howells8f0cfa52008-04-29 00:59:41 -0700697int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700699 if (!strcmp(name, XATTR_NAME_CAPS)) {
700 if (!capable(CAP_SETFCAP))
701 return -EPERM;
702 return 0;
David Howells1d045982008-11-14 10:39:24 +1100703 }
704
705 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700706 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 !capable(CAP_SYS_ADMIN))
708 return -EPERM;
709 return 0;
710}
711
David Howellsa6f76f22008-11-14 10:39:24 +1100712/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
714 * a process after a call to setuid, setreuid, or setresuid.
715 *
716 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
717 * {r,e,s}uid != 0, the permitted and effective capabilities are
718 * cleared.
719 *
720 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
721 * capabilities of the process are cleared.
722 *
723 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
724 * capabilities are set to the permitted capabilities.
725 *
David Howellsa6f76f22008-11-14 10:39:24 +1100726 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * never happen.
728 *
David Howellsa6f76f22008-11-14 10:39:24 +1100729 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *
731 * cevans - New behaviour, Oct '99
732 * A process may, via prctl(), elect to keep its capabilities when it
733 * calls setuid() and switches away from uid==0. Both permitted and
734 * effective sets will be retained.
735 * Without this change, it was impossible for a daemon to drop only some
736 * of its privilege. The call to setuid(!=0) would drop all privileges!
737 * Keeping uid 0 is not an option because uid 0 owns too many vital
738 * files..
739 * Thanks to Olaf Kirch and Peter Benie for spotting this.
740 */
David Howellsd84f4f92008-11-14 10:39:23 +1100741static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Eric W. Biederman18815a12012-02-07 16:45:47 -0800743 kuid_t root_uid = make_kuid(old->user_ns, 0);
744
745 if ((uid_eq(old->uid, root_uid) ||
746 uid_eq(old->euid, root_uid) ||
747 uid_eq(old->suid, root_uid)) &&
748 (!uid_eq(new->uid, root_uid) &&
749 !uid_eq(new->euid, root_uid) &&
Andy Lutomirski58319052015-09-04 15:42:45 -0700750 !uid_eq(new->suid, root_uid))) {
751 if (!issecure(SECURE_KEEP_CAPS)) {
752 cap_clear(new->cap_permitted);
753 cap_clear(new->cap_effective);
754 }
755
756 /*
757 * Pre-ambient programs expect setresuid to nonroot followed
758 * by exec to drop capabilities. We should make sure that
759 * this remains the case.
760 */
761 cap_clear(new->cap_ambient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800763 if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100764 cap_clear(new->cap_effective);
Eric W. Biederman18815a12012-02-07 16:45:47 -0800765 if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100766 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
David Howells1d045982008-11-14 10:39:24 +1100769/**
770 * cap_task_fix_setuid - Fix up the results of setuid() call
771 * @new: The proposed credentials
772 * @old: The current task's current credentials
773 * @flags: Indications of what has changed
774 *
775 * Fix up the results of setuid() call before the credential changes are
776 * actually applied, returning 0 to grant the changes, -ve to deny them.
777 */
David Howellsd84f4f92008-11-14 10:39:23 +1100778int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 switch (flags) {
781 case LSM_SETID_RE:
782 case LSM_SETID_ID:
783 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100784 /* juggle the capabilities to follow [RES]UID changes unless
785 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100786 if (!issecure(SECURE_NO_SETUID_FIXUP))
787 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David Howells1d045982008-11-14 10:39:24 +1100790 case LSM_SETID_FS:
791 /* juggle the capabilties to follow FSUID changes, unless
792 * otherwise suppressed
793 *
David Howellsd84f4f92008-11-14 10:39:23 +1100794 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
795 * if not, we might be a bit too harsh here.
796 */
797 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
Eric W. Biederman18815a12012-02-07 16:45:47 -0800798 kuid_t root_uid = make_kuid(old->user_ns, 0);
799 if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100800 new->cap_effective =
801 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100802
Eric W. Biederman18815a12012-02-07 16:45:47 -0800803 if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100804 new->cap_effective =
805 cap_raise_fs_set(new->cap_effective,
806 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
David Howellsd84f4f92008-11-14 10:39:23 +1100808 break;
David Howells1d045982008-11-14 10:39:24 +1100809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 default:
811 return -EINVAL;
812 }
813
814 return 0;
815}
816
Serge E. Hallynb5376772007-10-16 23:31:36 -0700817/*
818 * Rationale: code calling task_setscheduler, task_setioprio, and
819 * task_setnice, assumes that
820 * . if capable(cap_sys_nice), then those actions should be allowed
821 * . if not capable(cap_sys_nice), but acting on your own processes,
822 * then those actions should be allowed
823 * This is insufficient now since you can call code without suid, but
824 * yet with increased caps.
825 * So we check for increased caps on the target process.
826 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400827static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700828{
Serge Hallynf54fb862013-07-23 13:18:53 -0500829 int is_subset, ret = 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100830
831 rcu_read_lock();
832 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
833 current_cred()->cap_permitted);
Serge Hallynf54fb862013-07-23 13:18:53 -0500834 if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
835 ret = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100836 rcu_read_unlock();
837
Serge Hallynf54fb862013-07-23 13:18:53 -0500838 return ret;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700839}
840
David Howells1d045982008-11-14 10:39:24 +1100841/**
842 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
843 * @p: The task to affect
David Howells1d045982008-11-14 10:39:24 +1100844 *
845 * Detemine if the requested scheduler policy change is permitted for the
846 * specified task, returning 0 if permission is granted, -ve if denied.
847 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +0900848int cap_task_setscheduler(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700849{
850 return cap_safe_nice(p);
851}
852
David Howells1d045982008-11-14 10:39:24 +1100853/**
854 * cap_task_ioprio - Detemine if I/O priority change is permitted
855 * @p: The task to affect
856 * @ioprio: The I/O priority to set
857 *
858 * Detemine if the requested I/O priority change is permitted for the specified
859 * task, returning 0 if permission is granted, -ve if denied.
860 */
861int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700862{
863 return cap_safe_nice(p);
864}
865
David Howells1d045982008-11-14 10:39:24 +1100866/**
867 * cap_task_ioprio - Detemine if task priority change is permitted
868 * @p: The task to affect
869 * @nice: The nice value to set
870 *
871 * Detemine if the requested task priority change is permitted for the
872 * specified task, returning 0 if permission is granted, -ve if denied.
873 */
874int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700875{
876 return cap_safe_nice(p);
877}
878
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800879/*
David Howells1d045982008-11-14 10:39:24 +1100880 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
881 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800882 */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900883static int cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800884{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900885 struct cred *new;
886
Eric W. Biederman160da842013-07-02 10:04:54 -0700887 if (!ns_capable(current_user_ns(), CAP_SETPCAP))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800888 return -EPERM;
889 if (!cap_valid(cap))
890 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100891
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900892 new = prepare_creds();
893 if (!new)
894 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100895 cap_lower(new->cap_bset, cap);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900896 return commit_creds(new);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800897}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700898
David Howells1d045982008-11-14 10:39:24 +1100899/**
900 * cap_task_prctl - Implement process control functions for this security module
901 * @option: The process control function requested
902 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
903 *
904 * Allow process control functions (sys_prctl()) to alter capabilities; may
905 * also deny access to other functions not otherwise implemented here.
906 *
907 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
908 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
909 * modules will consider performing the function.
910 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700911int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100912 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700913{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900914 const struct cred *old = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +1100915 struct cred *new;
David Howellsd84f4f92008-11-14 10:39:23 +1100916
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700917 switch (option) {
918 case PR_CAPBSET_READ:
919 if (!cap_valid(arg2))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900920 return -EINVAL;
921 return !!cap_raised(old->cap_bset, arg2);
David Howellsd84f4f92008-11-14 10:39:23 +1100922
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700923 case PR_CAPBSET_DROP:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900924 return cap_prctl_drop(arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700925
926 /*
927 * The next four prctl's remain to assist with transitioning a
928 * system from legacy UID=0 based privilege (when filesystem
929 * capabilities are not in use) to a system using filesystem
930 * capabilities only - as the POSIX.1e draft intended.
931 *
932 * Note:
933 *
934 * PR_SET_SECUREBITS =
935 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
936 * | issecure_mask(SECURE_NOROOT)
937 * | issecure_mask(SECURE_NOROOT_LOCKED)
938 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
939 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
940 *
941 * will ensure that the current process and all of its
942 * children will be locked into a pure
943 * capability-based-privilege environment.
944 */
945 case PR_SET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900946 if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
947 & (old->securebits ^ arg2)) /*[1]*/
948 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
David Howellsd84f4f92008-11-14 10:39:23 +1100949 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris6a9de492012-01-03 12:25:14 -0500950 || (cap_capable(current_cred(),
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800951 current_cred()->user_ns, CAP_SETPCAP,
David Howells3699c532009-01-06 22:27:01 +0000952 SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700953 /*
954 * [1] no changing of bits that are locked
955 * [2] no unlocking of locks
956 * [3] no setting of unsupported bits
957 * [4] doing anything requires privilege (go read about
958 * the "sendmail capabilities bug")
959 */
David Howellsd84f4f92008-11-14 10:39:23 +1100960 )
961 /* cannot change a locked bit */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900962 return -EPERM;
963
964 new = prepare_creds();
965 if (!new)
966 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100967 new->securebits = arg2;
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900968 return commit_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100969
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700970 case PR_GET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900971 return old->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700972
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700973 case PR_GET_KEEPCAPS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900974 return !!issecure(SECURE_KEEP_CAPS);
David Howellsd84f4f92008-11-14 10:39:23 +1100975
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700976 case PR_SET_KEEPCAPS:
977 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900978 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100979 if (issecure(SECURE_KEEP_CAPS_LOCKED))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900980 return -EPERM;
981
982 new = prepare_creds();
983 if (!new)
984 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100985 if (arg2)
986 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700987 else
David Howellsd84f4f92008-11-14 10:39:23 +1100988 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900989 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700990
Andy Lutomirski58319052015-09-04 15:42:45 -0700991 case PR_CAP_AMBIENT:
992 if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
993 if (arg3 | arg4 | arg5)
994 return -EINVAL;
995
996 new = prepare_creds();
997 if (!new)
998 return -ENOMEM;
999 cap_clear(new->cap_ambient);
1000 return commit_creds(new);
1001 }
1002
1003 if (((!cap_valid(arg3)) | arg4 | arg5))
1004 return -EINVAL;
1005
1006 if (arg2 == PR_CAP_AMBIENT_IS_SET) {
1007 return !!cap_raised(current_cred()->cap_ambient, arg3);
1008 } else if (arg2 != PR_CAP_AMBIENT_RAISE &&
1009 arg2 != PR_CAP_AMBIENT_LOWER) {
1010 return -EINVAL;
1011 } else {
1012 if (arg2 == PR_CAP_AMBIENT_RAISE &&
1013 (!cap_raised(current_cred()->cap_permitted, arg3) ||
1014 !cap_raised(current_cred()->cap_inheritable,
Andy Lutomirski746bf6d2015-09-04 15:42:51 -07001015 arg3) ||
1016 issecure(SECURE_NO_CAP_AMBIENT_RAISE)))
Andy Lutomirski58319052015-09-04 15:42:45 -07001017 return -EPERM;
1018
1019 new = prepare_creds();
1020 if (!new)
1021 return -ENOMEM;
1022 if (arg2 == PR_CAP_AMBIENT_RAISE)
1023 cap_raise(new->cap_ambient, arg3);
1024 else
1025 cap_lower(new->cap_ambient, arg3);
1026 return commit_creds(new);
1027 }
1028
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001029 default:
1030 /* No functionality available - continue with default */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +09001031 return -ENOSYS;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
David Howells1d045982008-11-14 10:39:24 +11001035/**
David Howells1d045982008-11-14 10:39:24 +11001036 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
1037 * @mm: The VM space in which the new mapping is to be made
1038 * @pages: The size of the mapping
1039 *
1040 * Determine whether the allocation of a new virtual mapping by the current
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001041 * task is permitted, returning 1 if permission is granted, 0 if not.
David Howells1d045982008-11-14 10:39:24 +11001042 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001043int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 int cap_sys_admin = 0;
1046
Eric Paris6a9de492012-01-03 12:25:14 -05001047 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00001048 SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 cap_sys_admin = 1;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001050 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
Eric Paris7c738752009-07-31 12:53:58 -04001052
1053/*
Al Virod0077942012-05-30 13:11:37 -04001054 * cap_mmap_addr - check if able to map given addr
1055 * @addr: address attempting to be mapped
1056 *
1057 * If the process is attempting to map memory below dac_mmap_min_addr they need
1058 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
1059 * capability security module. Returns 0 if this mapping should be allowed
1060 * -EPERM if not.
1061 */
1062int cap_mmap_addr(unsigned long addr)
1063{
1064 int ret = 0;
1065
1066 if (addr < dac_mmap_min_addr) {
1067 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
1068 SECURITY_CAP_AUDIT);
1069 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1070 if (ret == 0)
1071 current->flags |= PF_SUPERPRIV;
1072 }
1073 return ret;
1074}
1075
Al Viroe5467852012-05-30 13:30:51 -04001076int cap_mmap_file(struct file *file, unsigned long reqprot,
1077 unsigned long prot, unsigned long flags)
Eric Paris7c738752009-07-31 12:53:58 -04001078{
Al Viroe5467852012-05-30 13:30:51 -04001079 return 0;
Eric Paris7c738752009-07-31 12:53:58 -04001080}
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001081
1082#ifdef CONFIG_SECURITY
1083
1084struct security_hook_list capability_hooks[] = {
1085 LSM_HOOK_INIT(capable, cap_capable),
1086 LSM_HOOK_INIT(settime, cap_settime),
1087 LSM_HOOK_INIT(ptrace_access_check, cap_ptrace_access_check),
1088 LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
1089 LSM_HOOK_INIT(capget, cap_capget),
1090 LSM_HOOK_INIT(capset, cap_capset),
1091 LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
1092 LSM_HOOK_INIT(bprm_secureexec, cap_bprm_secureexec),
1093 LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
1094 LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
1095 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
1096 LSM_HOOK_INIT(mmap_file, cap_mmap_file),
1097 LSM_HOOK_INIT(task_fix_setuid, cap_task_fix_setuid),
1098 LSM_HOOK_INIT(task_prctl, cap_task_prctl),
1099 LSM_HOOK_INIT(task_setscheduler, cap_task_setscheduler),
1100 LSM_HOOK_INIT(task_setioprio, cap_task_setioprio),
1101 LSM_HOOK_INIT(task_setnice, cap_task_setnice),
1102 LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory),
1103};
1104
1105void __init capability_add_hooks(void)
1106{
1107 security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks));
1108}
1109
1110#endif /* CONFIG_SECURITY */