blob: 91f9086d1c676e89ddd43ec6025517e368c21f70 [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 Yeh639bb872009-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{
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080083 struct user_namespace *ns = targ_ns;
Serge E. Hallyn34867402011-03-23 16:43:17 -070084
Chia-chi Yeh639bb872009-06-19 07:15:05 +080085 if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
86 return 0;
87 if (cap == CAP_NET_ADMIN && in_egroup_p(AID_NET_ADMIN))
88 return 0;
89
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080090 /* See if cred has the capability in the target user namespace
91 * by examining the target user namespace and all of the target
92 * user namespace's parents.
93 */
94 for (;;) {
Serge E. Hallyn34867402011-03-23 16:43:17 -070095 /* Do we have the necessary capabilities? */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080096 if (ns == cred->user_ns)
Serge E. Hallyn34867402011-03-23 16:43:17 -070097 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
98
99 /* Have we tried all of the parent namespaces? */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800100 if (ns == &init_user_ns)
Serge E. Hallyn34867402011-03-23 16:43:17 -0700101 return -EPERM;
102
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800103 /*
104 * The owner of the user namespace in the parent of the
105 * user namespace has all caps.
106 */
107 if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
108 return 0;
109
Serge E. Hallyn34867402011-03-23 16:43:17 -0700110 /*
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800111 * If you have a capability in a parent user ns, then you have
Serge E. Hallyn34867402011-03-23 16:43:17 -0700112 * it over all children user namespaces as well.
113 */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800114 ns = ns->parent;
Serge E. Hallyn34867402011-03-23 16:43:17 -0700115 }
116
117 /* We never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
David Howells1d045982008-11-14 10:39:24 +1100120/**
121 * cap_settime - Determine whether the current process may set the system clock
122 * @ts: The time to set
123 * @tz: The timezone to set
124 *
125 * Determine whether the current process may set the system clock and timezone
126 * information, returning 0 if permission granted, -ve if denied.
127 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000128int cap_settime(const struct timespec *ts, const struct timezone *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 if (!capable(CAP_SYS_TIME))
131 return -EPERM;
132 return 0;
133}
134
David Howells1d045982008-11-14 10:39:24 +1100135/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000136 * cap_ptrace_access_check - Determine whether the current process may access
David Howells1d045982008-11-14 10:39:24 +1100137 * another
138 * @child: The process to be accessed
139 * @mode: The mode of attachment.
140 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700141 * If we are in the same or an ancestor user_ns and have all the target
142 * task's capabilities, then ptrace access is allowed.
143 * If we have the ptrace capability to the target user_ns, then ptrace
144 * access is allowed.
145 * Else denied.
146 *
David Howells1d045982008-11-14 10:39:24 +1100147 * Determine whether a process may access another, returning 0 if permission
148 * granted, -ve if denied.
149 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000150int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
David Howellsc69e8d92008-11-14 10:39:19 +1100152 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700153 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100154
155 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700156 cred = current_cred();
157 child_cred = __task_cred(child);
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800158 if (cred->user_ns == child_cred->user_ns &&
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700159 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
160 goto out;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800161 if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700162 goto out;
163 ret = -EPERM;
164out:
David Howellsc69e8d92008-11-14 10:39:19 +1100165 rcu_read_unlock();
166 return ret;
David Howells5cd9c582008-08-14 11:37:28 +0100167}
168
David Howells1d045982008-11-14 10:39:24 +1100169/**
170 * cap_ptrace_traceme - Determine whether another process may trace the current
171 * @parent: The task proposed to be the tracer
172 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700173 * If parent is in the same or an ancestor user_ns and has all current's
174 * capabilities, then ptrace access is allowed.
175 * If parent has the ptrace capability to current's user_ns, then ptrace
176 * access is allowed.
177 * Else denied.
178 *
David Howells1d045982008-11-14 10:39:24 +1100179 * Determine whether the nominated task is permitted to trace the current
180 * process, returning 0 if permission is granted, -ve if denied.
181 */
David Howells5cd9c582008-08-14 11:37:28 +0100182int cap_ptrace_traceme(struct task_struct *parent)
183{
David Howellsc69e8d92008-11-14 10:39:19 +1100184 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700185 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100186
187 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700188 cred = __task_cred(parent);
189 child_cred = current_cred();
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800190 if (cred->user_ns == child_cred->user_ns &&
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700191 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
192 goto out;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800193 if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700194 goto out;
195 ret = -EPERM;
196out:
David Howellsc69e8d92008-11-14 10:39:19 +1100197 rcu_read_unlock();
198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
David Howells1d045982008-11-14 10:39:24 +1100201/**
202 * cap_capget - Retrieve a task's capability sets
203 * @target: The task from which to retrieve the capability sets
204 * @effective: The place to record the effective set
205 * @inheritable: The place to record the inheritable set
206 * @permitted: The place to record the permitted set
207 *
208 * This function retrieves the capabilities of the nominated task and returns
209 * them to the caller.
210 */
211int cap_capget(struct task_struct *target, kernel_cap_t *effective,
212 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howellsc69e8d92008-11-14 10:39:19 +1100214 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100217 rcu_read_lock();
218 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100219 *effective = cred->cap_effective;
220 *inheritable = cred->cap_inheritable;
221 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224}
225
David Howells1d045982008-11-14 10:39:24 +1100226/*
227 * Determine whether the inheritable capabilities are limited to the old
228 * permitted set. Returns 1 if they are limited, 0 if they are not.
229 */
Andrew Morgan72c2d582007-10-18 03:05:59 -0700230static inline int cap_inh_is_capped(void)
231{
David Howells1d045982008-11-14 10:39:24 +1100232
233 /* they are so limited unless the current task has the CAP_SETPCAP
234 * capability
Andrew Morgan72c2d582007-10-18 03:05:59 -0700235 */
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800236 if (cap_capable(current_cred(), current_cred()->user_ns,
Eric Paris6a9de492012-01-03 12:25:14 -0500237 CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
David Howells1d045982008-11-14 10:39:24 +1100238 return 0;
David Howells1d045982008-11-14 10:39:24 +1100239 return 1;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700240}
241
David Howells1d045982008-11-14 10:39:24 +1100242/**
243 * cap_capset - Validate and apply proposed changes to current's capabilities
244 * @new: The proposed new credentials; alterations should be made here
245 * @old: The current task's current credentials
246 * @effective: A pointer to the proposed new effective capabilities set
247 * @inheritable: A pointer to the proposed new inheritable capabilities set
248 * @permitted: A pointer to the proposed new permitted capabilities set
249 *
250 * This function validates and applies a proposed mass change to the current
251 * process's capability sets. The changes are made to the proposed new
252 * credentials, and assuming no error, will be committed by the caller of LSM.
253 */
David Howellsd84f4f92008-11-14 10:39:23 +1100254int cap_capset(struct cred *new,
255 const struct cred *old,
256 const kernel_cap_t *effective,
257 const kernel_cap_t *inheritable,
258 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
David Howellsd84f4f92008-11-14 10:39:23 +1100260 if (cap_inh_is_capped() &&
261 !cap_issubset(*inheritable,
262 cap_combine(old->cap_inheritable,
263 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700264 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100266
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800267 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100268 cap_combine(old->cap_inheritable,
269 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800270 /* no new pI capabilities outside bounding set */
271 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100274 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100278 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Howellsd84f4f92008-11-14 10:39:23 +1100281 new->cap_effective = *effective;
282 new->cap_inheritable = *inheritable;
283 new->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285}
286
David Howells1d045982008-11-14 10:39:24 +1100287/*
288 * Clear proposed capability sets for execve().
289 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700290static inline void bprm_clear_caps(struct linux_binprm *bprm)
291{
David Howellsa6f76f22008-11-14 10:39:24 +1100292 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700293 bprm->cap_effective = false;
294}
295
David Howells1d045982008-11-14 10:39:24 +1100296/**
297 * cap_inode_need_killpriv - Determine if inode change affects privileges
298 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
299 *
300 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
301 * affects the security markings on that inode, and if it is, should
302 * inode_killpriv() be invoked or the change rejected?
303 *
304 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
305 * -ve to deny the change.
306 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700307int cap_inode_need_killpriv(struct dentry *dentry)
308{
David Howellsc6f493d2015-03-17 22:26:22 +0000309 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700310 int error;
311
Al Viroacfa4382008-12-04 10:06:33 -0500312 if (!inode->i_op->getxattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700313 return 0;
314
315 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
316 if (error <= 0)
317 return 0;
318 return 1;
319}
320
David Howells1d045982008-11-14 10:39:24 +1100321/**
322 * cap_inode_killpriv - Erase the security markings on an inode
323 * @dentry: The inode/dentry to alter
324 *
325 * Erase the privilege-enhancing security markings on an inode.
326 *
327 * Returns 0 if successful, -ve on error.
328 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700329int cap_inode_killpriv(struct dentry *dentry)
330{
David Howellsc6f493d2015-03-17 22:26:22 +0000331 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700332
Al Viroacfa4382008-12-04 10:06:33 -0500333 if (!inode->i_op->removexattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700334 return 0;
335
336 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
337}
338
David Howells1d045982008-11-14 10:39:24 +1100339/*
340 * Calculate the new process capability sets from the capability sets attached
341 * to a file.
342 */
Eric Parisc0b00442008-11-11 21:48:10 +1100343static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100344 struct linux_binprm *bprm,
Zhi Li4d49f672011-08-11 13:27:50 +0800345 bool *effective,
346 bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700347{
David Howellsa6f76f22008-11-14 10:39:24 +1100348 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100349 unsigned i;
350 int ret = 0;
351
352 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100353 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100354
Zhi Li4d49f672011-08-11 13:27:50 +0800355 if (caps->magic_etc & VFS_CAP_REVISION_MASK)
356 *has_cap = true;
357
Eric Parisc0b00442008-11-11 21:48:10 +1100358 CAP_FOR_EACH_U32(i) {
359 __u32 permitted = caps->permitted.cap[i];
360 __u32 inheritable = caps->inheritable.cap[i];
361
362 /*
363 * pP' = (X & fP) | (pI & fI)
364 */
David Howellsa6f76f22008-11-14 10:39:24 +1100365 new->cap_permitted.cap[i] =
366 (new->cap_bset.cap[i] & permitted) |
367 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100368
David Howellsa6f76f22008-11-14 10:39:24 +1100369 if (permitted & ~new->cap_permitted.cap[i])
370 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100371 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100372 }
373
374 /*
375 * For legacy apps, with no internal support for recognizing they
376 * do not have enough capabilities, we return an error if they are
377 * missing some "forced" (aka file-permitted) capabilities.
378 */
David Howellsa6f76f22008-11-14 10:39:24 +1100379 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100380}
381
David Howells1d045982008-11-14 10:39:24 +1100382/*
383 * Extract the on-exec-apply capability sets for an executable file.
384 */
Eric Parisc0b00442008-11-11 21:48:10 +1100385int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
386{
David Howellsc6f493d2015-03-17 22:26:22 +0000387 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700388 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800389 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100390 int size;
391 struct vfs_cap_data caps;
392
393 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
394
Al Viroacfa4382008-12-04 10:06:33 -0500395 if (!inode || !inode->i_op->getxattr)
Eric Parisc0b00442008-11-11 21:48:10 +1100396 return -ENODATA;
397
398 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
399 XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100400 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100401 /* no data, that's ok */
402 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100403 if (size < 0)
404 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700405
Andrew Morgane338d262008-02-04 22:29:42 -0800406 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700407 return -EINVAL;
408
Eric Parisc0b00442008-11-11 21:48:10 +1100409 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700410
David Howellsa6f76f22008-11-14 10:39:24 +1100411 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800412 case VFS_CAP_REVISION_1:
413 if (size != XATTR_CAPS_SZ_1)
414 return -EINVAL;
415 tocopy = VFS_CAP_U32_1;
416 break;
417 case VFS_CAP_REVISION_2:
418 if (size != XATTR_CAPS_SZ_2)
419 return -EINVAL;
420 tocopy = VFS_CAP_U32_2;
421 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700422 default:
423 return -EINVAL;
424 }
Andrew Morgane338d262008-02-04 22:29:42 -0800425
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700426 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100427 if (i >= tocopy)
428 break;
429 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
430 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800431 }
David Howellsa6f76f22008-11-14 10:39:24 +1100432
Eric Paris7d8b6c62014-07-23 15:36:26 -0400433 cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
434 cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
435
Eric Parisc0b00442008-11-11 21:48:10 +1100436 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700437}
438
David Howells1d045982008-11-14 10:39:24 +1100439/*
440 * Attempt to get the on-exec apply capability sets for an executable file from
441 * its xattrs and, if present, apply them to the proposed credentials being
442 * constructed by execve().
443 */
Zhi Li4d49f672011-08-11 13:27:50 +0800444static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700445{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700446 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100447 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700448
Serge Hallyn3318a382008-10-30 11:52:23 -0500449 bprm_clear_caps(bprm);
450
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600451 if (!file_caps_enabled)
452 return 0;
453
Al Viro182be682013-01-24 02:21:54 -0500454 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700455 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700456
Al Virof4a4a8b2014-12-28 09:27:07 -0500457 rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
Eric Parisc0b00442008-11-11 21:48:10 +1100458 if (rc < 0) {
459 if (rc == -EINVAL)
460 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
461 __func__, rc, bprm->filename);
462 else if (rc == -ENODATA)
463 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700464 goto out;
465 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700466
Zhi Li4d49f672011-08-11 13:27:50 +0800467 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100468 if (rc == -EINVAL)
469 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
470 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700471
472out:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700473 if (rc)
474 bprm_clear_caps(bprm);
475
476 return rc;
477}
478
David Howells1d045982008-11-14 10:39:24 +1100479/**
480 * cap_bprm_set_creds - Set up the proposed credentials for execve().
481 * @bprm: The execution parameters, including the proposed creds
482 *
483 * Set up the proposed credentials for a new execution context being
484 * constructed by execve(). The proposed creds in @bprm->cred is altered,
485 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100486 */
487int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
David Howellsa6f76f22008-11-14 10:39:24 +1100489 const struct cred *old = current_cred();
490 struct cred *new = bprm->cred;
Serge Hallyn7d8db182011-08-15 08:29:50 -0500491 bool effective, has_cap = false;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700492 int ret;
Eric W. Biederman18815a12012-02-07 16:45:47 -0800493 kuid_t root_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howellsa6f76f22008-11-14 10:39:24 +1100495 effective = false;
Zhi Li4d49f672011-08-11 13:27:50 +0800496 ret = get_file_caps(bprm, &effective, &has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100497 if (ret < 0)
498 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Eric W. Biederman18815a12012-02-07 16:45:47 -0800500 root_uid = make_kuid(new->user_ns, 0);
501
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700502 if (!issecure(SECURE_NOROOT)) {
503 /*
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500504 * If the legacy file capability is set, then don't set privs
505 * for a setuid root binary run by a non-root user. Do set it
506 * for a root user just to cause least surprise to an admin.
507 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800508 if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500509 warn_setuid_and_fcaps_mixed(bprm->filename);
510 goto skip;
511 }
512 /*
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700513 * To support inheritance of root-permissions and suid-root
514 * executables under compatibility mode, we override the
515 * capability sets for the file.
516 *
David Howellsa6f76f22008-11-14 10:39:24 +1100517 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700518 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800519 if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700520 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100521 new->cap_permitted = cap_combine(old->cap_bset,
522 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800524 if (uid_eq(new->euid, root_uid))
David Howellsa6f76f22008-11-14 10:39:24 +1100525 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500527skip:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700528
Eric Parisd52fc5d2012-04-17 16:26:54 -0400529 /* if we have fs caps, clear dangerous personality flags */
530 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
531 bprm->per_clear |= PER_CLEAR_ON_SETID;
532
533
David Howellsa6f76f22008-11-14 10:39:24 +1100534 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500535 * credentials unless they have the appropriate permit.
536 *
537 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
David Howellsa6f76f22008-11-14 10:39:24 +1100538 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800539 if ((!uid_eq(new->euid, old->uid) ||
540 !gid_eq(new->egid, old->gid) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100541 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
542 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
543 /* downgrade; they get no more than they had, and maybe less */
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500544 if (!capable(CAP_SETUID) ||
545 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100546 new->euid = new->uid;
547 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Serge E. Hallynb3a222e2009-11-23 16:21:30 -0600549 new->cap_permitted = cap_intersect(new->cap_permitted,
550 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
David Howellsa6f76f22008-11-14 10:39:24 +1100553 new->suid = new->fsuid = new->euid;
554 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Eric Paris4bf2ea72011-04-01 17:08:28 -0400556 if (effective)
557 new->cap_effective = new->cap_permitted;
558 else
559 cap_clear(new->cap_effective);
David Howellsa6f76f22008-11-14 10:39:24 +1100560 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Paris3fc689e2008-11-11 21:48:18 +1100562 /*
563 * Audit candidate if current->cap_effective is set
564 *
565 * We do not bother to audit if 3 things are true:
566 * 1) cap_effective has all caps
567 * 2) we are root
568 * 3) root is supposed to have all caps (SECURE_NOROOT)
569 * Since this is just a normal root execing a process.
570 *
571 * Number 1 above might fail if you don't have a full bset, but I think
572 * that is interesting information to audit.
573 */
David Howellsd84f4f92008-11-14 10:39:23 +1100574 if (!cap_isclear(new->cap_effective)) {
575 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
Eric W. Biederman18815a12012-02-07 16:45:47 -0800576 !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100577 issecure(SECURE_NOROOT)) {
578 ret = audit_log_bprm_fcaps(bprm, new, old);
579 if (ret < 0)
580 return ret;
581 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
David Howellsd84f4f92008-11-14 10:39:23 +1100584 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
David Howellsa6f76f22008-11-14 10:39:24 +1100585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
David Howells1d045982008-11-14 10:39:24 +1100588/**
589 * cap_bprm_secureexec - Determine whether a secure execution is required
590 * @bprm: The execution parameters
591 *
592 * Determine whether a secure execution is required, return 1 if it is, and 0
593 * if it is not.
594 *
595 * The credentials have been committed by this point, and so are no longer
596 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100597 */
598int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
David Howellsc69e8d92008-11-14 10:39:19 +1100600 const struct cred *cred = current_cred();
Eric W. Biederman18815a12012-02-07 16:45:47 -0800601 kuid_t root_uid = make_kuid(cred->user_ns, 0);
David Howellsb6dff3e2008-11-14 10:39:16 +1100602
Eric W. Biederman18815a12012-02-07 16:45:47 -0800603 if (!uid_eq(cred->uid, root_uid)) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700604 if (bprm->cap_effective)
605 return 1;
David Howellsa6f76f22008-11-14 10:39:24 +1100606 if (!cap_isclear(cred->cap_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700607 return 1;
608 }
609
Eric W. Biederman18815a12012-02-07 16:45:47 -0800610 return (!uid_eq(cred->euid, cred->uid) ||
611 !gid_eq(cred->egid, cred->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
David Howells1d045982008-11-14 10:39:24 +1100614/**
615 * cap_inode_setxattr - Determine whether an xattr may be altered
616 * @dentry: The inode/dentry being altered
617 * @name: The name of the xattr to be changed
618 * @value: The value that the xattr will be changed to
619 * @size: The size of value
620 * @flags: The replacement flag
621 *
622 * Determine whether an xattr may be altered or set on an inode, returning 0 if
623 * permission is granted, -ve if denied.
624 *
625 * This is used to make sure security xattrs don't get updated or set by those
626 * who aren't privileged to do so.
627 */
David Howells8f0cfa52008-04-29 00:59:41 -0700628int cap_inode_setxattr(struct dentry *dentry, const char *name,
629 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700631 if (!strcmp(name, XATTR_NAME_CAPS)) {
632 if (!capable(CAP_SETFCAP))
633 return -EPERM;
634 return 0;
David Howells1d045982008-11-14 10:39:24 +1100635 }
636
637 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700638 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 !capable(CAP_SYS_ADMIN))
640 return -EPERM;
641 return 0;
642}
643
David Howells1d045982008-11-14 10:39:24 +1100644/**
645 * cap_inode_removexattr - Determine whether an xattr may be removed
646 * @dentry: The inode/dentry being altered
647 * @name: The name of the xattr to be changed
648 *
649 * Determine whether an xattr may be removed from 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 removed by those who
653 * aren't privileged to remove them.
654 */
David Howells8f0cfa52008-04-29 00:59:41 -0700655int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700657 if (!strcmp(name, XATTR_NAME_CAPS)) {
658 if (!capable(CAP_SETFCAP))
659 return -EPERM;
660 return 0;
David Howells1d045982008-11-14 10:39:24 +1100661 }
662
663 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700664 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 !capable(CAP_SYS_ADMIN))
666 return -EPERM;
667 return 0;
668}
669
David Howellsa6f76f22008-11-14 10:39:24 +1100670/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
672 * a process after a call to setuid, setreuid, or setresuid.
673 *
674 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
675 * {r,e,s}uid != 0, the permitted and effective capabilities are
676 * cleared.
677 *
678 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
679 * capabilities of the process are cleared.
680 *
681 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
682 * capabilities are set to the permitted capabilities.
683 *
David Howellsa6f76f22008-11-14 10:39:24 +1100684 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * never happen.
686 *
David Howellsa6f76f22008-11-14 10:39:24 +1100687 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *
689 * cevans - New behaviour, Oct '99
690 * A process may, via prctl(), elect to keep its capabilities when it
691 * calls setuid() and switches away from uid==0. Both permitted and
692 * effective sets will be retained.
693 * Without this change, it was impossible for a daemon to drop only some
694 * of its privilege. The call to setuid(!=0) would drop all privileges!
695 * Keeping uid 0 is not an option because uid 0 owns too many vital
696 * files..
697 * Thanks to Olaf Kirch and Peter Benie for spotting this.
698 */
David Howellsd84f4f92008-11-14 10:39:23 +1100699static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Eric W. Biederman18815a12012-02-07 16:45:47 -0800701 kuid_t root_uid = make_kuid(old->user_ns, 0);
702
703 if ((uid_eq(old->uid, root_uid) ||
704 uid_eq(old->euid, root_uid) ||
705 uid_eq(old->suid, root_uid)) &&
706 (!uid_eq(new->uid, root_uid) &&
707 !uid_eq(new->euid, root_uid) &&
708 !uid_eq(new->suid, root_uid)) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700709 !issecure(SECURE_KEEP_CAPS)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100710 cap_clear(new->cap_permitted);
711 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800713 if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100714 cap_clear(new->cap_effective);
Eric W. Biederman18815a12012-02-07 16:45:47 -0800715 if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100716 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
David Howells1d045982008-11-14 10:39:24 +1100719/**
720 * cap_task_fix_setuid - Fix up the results of setuid() call
721 * @new: The proposed credentials
722 * @old: The current task's current credentials
723 * @flags: Indications of what has changed
724 *
725 * Fix up the results of setuid() call before the credential changes are
726 * actually applied, returning 0 to grant the changes, -ve to deny them.
727 */
David Howellsd84f4f92008-11-14 10:39:23 +1100728int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 switch (flags) {
731 case LSM_SETID_RE:
732 case LSM_SETID_ID:
733 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100734 /* juggle the capabilities to follow [RES]UID changes unless
735 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100736 if (!issecure(SECURE_NO_SETUID_FIXUP))
737 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
David Howells1d045982008-11-14 10:39:24 +1100740 case LSM_SETID_FS:
741 /* juggle the capabilties to follow FSUID changes, unless
742 * otherwise suppressed
743 *
David Howellsd84f4f92008-11-14 10:39:23 +1100744 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
745 * if not, we might be a bit too harsh here.
746 */
747 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
Eric W. Biederman18815a12012-02-07 16:45:47 -0800748 kuid_t root_uid = make_kuid(old->user_ns, 0);
749 if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100750 new->cap_effective =
751 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100752
Eric W. Biederman18815a12012-02-07 16:45:47 -0800753 if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100754 new->cap_effective =
755 cap_raise_fs_set(new->cap_effective,
756 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
David Howellsd84f4f92008-11-14 10:39:23 +1100758 break;
David Howells1d045982008-11-14 10:39:24 +1100759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 default:
761 return -EINVAL;
762 }
763
764 return 0;
765}
766
Serge E. Hallynb5376772007-10-16 23:31:36 -0700767/*
768 * Rationale: code calling task_setscheduler, task_setioprio, and
769 * task_setnice, assumes that
770 * . if capable(cap_sys_nice), then those actions should be allowed
771 * . if not capable(cap_sys_nice), but acting on your own processes,
772 * then those actions should be allowed
773 * This is insufficient now since you can call code without suid, but
774 * yet with increased caps.
775 * So we check for increased caps on the target process.
776 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400777static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700778{
Serge Hallynf54fb862013-07-23 13:18:53 -0500779 int is_subset, ret = 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100780
781 rcu_read_lock();
782 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
783 current_cred()->cap_permitted);
Serge Hallynf54fb862013-07-23 13:18:53 -0500784 if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
785 ret = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100786 rcu_read_unlock();
787
Serge Hallynf54fb862013-07-23 13:18:53 -0500788 return ret;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700789}
790
David Howells1d045982008-11-14 10:39:24 +1100791/**
792 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
793 * @p: The task to affect
David Howells1d045982008-11-14 10:39:24 +1100794 *
795 * Detemine if the requested scheduler policy change is permitted for the
796 * specified task, returning 0 if permission is granted, -ve if denied.
797 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +0900798int cap_task_setscheduler(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700799{
800 return cap_safe_nice(p);
801}
802
David Howells1d045982008-11-14 10:39:24 +1100803/**
804 * cap_task_ioprio - Detemine if I/O priority change is permitted
805 * @p: The task to affect
806 * @ioprio: The I/O priority to set
807 *
808 * Detemine if the requested I/O priority change is permitted for the specified
809 * task, returning 0 if permission is granted, -ve if denied.
810 */
811int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700812{
813 return cap_safe_nice(p);
814}
815
David Howells1d045982008-11-14 10:39:24 +1100816/**
817 * cap_task_ioprio - Detemine if task priority change is permitted
818 * @p: The task to affect
819 * @nice: The nice value to set
820 *
821 * Detemine if the requested task priority change is permitted for the
822 * specified task, returning 0 if permission is granted, -ve if denied.
823 */
824int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700825{
826 return cap_safe_nice(p);
827}
828
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800829/*
David Howells1d045982008-11-14 10:39:24 +1100830 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
831 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800832 */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900833static int cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800834{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900835 struct cred *new;
836
Eric W. Biederman160da842013-07-02 10:04:54 -0700837 if (!ns_capable(current_user_ns(), CAP_SETPCAP))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800838 return -EPERM;
839 if (!cap_valid(cap))
840 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100841
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900842 new = prepare_creds();
843 if (!new)
844 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100845 cap_lower(new->cap_bset, cap);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900846 return commit_creds(new);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800847}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700848
David Howells1d045982008-11-14 10:39:24 +1100849/**
850 * cap_task_prctl - Implement process control functions for this security module
851 * @option: The process control function requested
852 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
853 *
854 * Allow process control functions (sys_prctl()) to alter capabilities; may
855 * also deny access to other functions not otherwise implemented here.
856 *
857 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
858 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
859 * modules will consider performing the function.
860 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700861int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100862 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700863{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900864 const struct cred *old = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +1100865 struct cred *new;
David Howellsd84f4f92008-11-14 10:39:23 +1100866
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700867 switch (option) {
868 case PR_CAPBSET_READ:
869 if (!cap_valid(arg2))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900870 return -EINVAL;
871 return !!cap_raised(old->cap_bset, arg2);
David Howellsd84f4f92008-11-14 10:39:23 +1100872
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700873 case PR_CAPBSET_DROP:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900874 return cap_prctl_drop(arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700875
876 /*
877 * The next four prctl's remain to assist with transitioning a
878 * system from legacy UID=0 based privilege (when filesystem
879 * capabilities are not in use) to a system using filesystem
880 * capabilities only - as the POSIX.1e draft intended.
881 *
882 * Note:
883 *
884 * PR_SET_SECUREBITS =
885 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
886 * | issecure_mask(SECURE_NOROOT)
887 * | issecure_mask(SECURE_NOROOT_LOCKED)
888 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
889 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
890 *
891 * will ensure that the current process and all of its
892 * children will be locked into a pure
893 * capability-based-privilege environment.
894 */
895 case PR_SET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900896 if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
897 & (old->securebits ^ arg2)) /*[1]*/
898 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
David Howellsd84f4f92008-11-14 10:39:23 +1100899 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris6a9de492012-01-03 12:25:14 -0500900 || (cap_capable(current_cred(),
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800901 current_cred()->user_ns, CAP_SETPCAP,
David Howells3699c532009-01-06 22:27:01 +0000902 SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700903 /*
904 * [1] no changing of bits that are locked
905 * [2] no unlocking of locks
906 * [3] no setting of unsupported bits
907 * [4] doing anything requires privilege (go read about
908 * the "sendmail capabilities bug")
909 */
David Howellsd84f4f92008-11-14 10:39:23 +1100910 )
911 /* cannot change a locked bit */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900912 return -EPERM;
913
914 new = prepare_creds();
915 if (!new)
916 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100917 new->securebits = arg2;
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900918 return commit_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100919
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700920 case PR_GET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900921 return old->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700922
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700923 case PR_GET_KEEPCAPS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900924 return !!issecure(SECURE_KEEP_CAPS);
David Howellsd84f4f92008-11-14 10:39:23 +1100925
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700926 case PR_SET_KEEPCAPS:
927 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900928 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100929 if (issecure(SECURE_KEEP_CAPS_LOCKED))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900930 return -EPERM;
931
932 new = prepare_creds();
933 if (!new)
934 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100935 if (arg2)
936 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700937 else
David Howellsd84f4f92008-11-14 10:39:23 +1100938 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900939 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700940
941 default:
942 /* No functionality available - continue with default */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900943 return -ENOSYS;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
David Howells1d045982008-11-14 10:39:24 +1100947/**
David Howells1d045982008-11-14 10:39:24 +1100948 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
949 * @mm: The VM space in which the new mapping is to be made
950 * @pages: The size of the mapping
951 *
952 * Determine whether the allocation of a new virtual mapping by the current
953 * task is permitted, returning 0 if permission is granted, -ve if not.
954 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700955int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 int cap_sys_admin = 0;
958
Eric Paris6a9de492012-01-03 12:25:14 -0500959 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +0000960 SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700962 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Eric Paris7c738752009-07-31 12:53:58 -0400964
965/*
Al Virod0077942012-05-30 13:11:37 -0400966 * cap_mmap_addr - check if able to map given addr
967 * @addr: address attempting to be mapped
968 *
969 * If the process is attempting to map memory below dac_mmap_min_addr they need
970 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
971 * capability security module. Returns 0 if this mapping should be allowed
972 * -EPERM if not.
973 */
974int cap_mmap_addr(unsigned long addr)
975{
976 int ret = 0;
977
978 if (addr < dac_mmap_min_addr) {
979 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
980 SECURITY_CAP_AUDIT);
981 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
982 if (ret == 0)
983 current->flags |= PF_SUPERPRIV;
984 }
985 return ret;
986}
987
Al Viroe5467852012-05-30 13:30:51 -0400988int cap_mmap_file(struct file *file, unsigned long reqprot,
989 unsigned long prot, unsigned long flags)
Eric Paris7c738752009-07-31 12:53:58 -0400990{
Al Viroe5467852012-05-30 13:30:51 -0400991 return 0;
Eric Paris7c738752009-07-31 12:53:58 -0400992}