blob: 7fdd6f8cc1a2a5385877f16d22b3fd70bf98192b [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
Tushar Beheraae0e6ae2012-03-26 16:54:15 +053085#ifdef CONFIG_ANDROID_PARANOID_NETWORK
Chia-chi Yeh639bb872009-06-19 07:15:05 +080086 if (cap == CAP_NET_RAW && in_egroup_p(AID_NET_RAW))
87 return 0;
88 if (cap == CAP_NET_ADMIN && in_egroup_p(AID_NET_ADMIN))
89 return 0;
Tushar Beheraae0e6ae2012-03-26 16:54:15 +053090#endif
Chia-chi Yeh639bb872009-06-19 07:15:05 +080091
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080092 /* See if cred has the capability in the target user namespace
93 * by examining the target user namespace and all of the target
94 * user namespace's parents.
95 */
96 for (;;) {
Serge E. Hallyn34867402011-03-23 16:43:17 -070097 /* Do we have the necessary capabilities? */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -080098 if (ns == cred->user_ns)
Serge E. Hallyn34867402011-03-23 16:43:17 -070099 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
100
101 /* Have we tried all of the parent namespaces? */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800102 if (ns == &init_user_ns)
Serge E. Hallyn34867402011-03-23 16:43:17 -0700103 return -EPERM;
104
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800105 /*
106 * The owner of the user namespace in the parent of the
107 * user namespace has all caps.
108 */
109 if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
110 return 0;
111
Serge E. Hallyn34867402011-03-23 16:43:17 -0700112 /*
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800113 * If you have a capability in a parent user ns, then you have
Serge E. Hallyn34867402011-03-23 16:43:17 -0700114 * it over all children user namespaces as well.
115 */
Eric W. Biederman520d9ea2012-12-13 18:06:40 -0800116 ns = ns->parent;
Serge E. Hallyn34867402011-03-23 16:43:17 -0700117 }
118
119 /* We never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
David Howells1d045982008-11-14 10:39:24 +1100122/**
123 * cap_settime - Determine whether the current process may set the system clock
124 * @ts: The time to set
125 * @tz: The timezone to set
126 *
127 * Determine whether the current process may set the system clock and timezone
128 * information, returning 0 if permission granted, -ve if denied.
129 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000130int cap_settime(const struct timespec *ts, const struct timezone *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 if (!capable(CAP_SYS_TIME))
133 return -EPERM;
134 return 0;
135}
136
David Howells1d045982008-11-14 10:39:24 +1100137/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000138 * cap_ptrace_access_check - Determine whether the current process may access
David Howells1d045982008-11-14 10:39:24 +1100139 * another
140 * @child: The process to be accessed
141 * @mode: The mode of attachment.
142 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700143 * If we are in the same or an ancestor user_ns and have all the target
144 * task's capabilities, then ptrace access is allowed.
145 * If we have the ptrace capability to the target user_ns, then ptrace
146 * access is allowed.
147 * Else denied.
148 *
David Howells1d045982008-11-14 10:39:24 +1100149 * Determine whether a process may access another, returning 0 if permission
150 * granted, -ve if denied.
151 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000152int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Howellsc69e8d92008-11-14 10:39:19 +1100154 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700155 const struct cred *cred, *child_cred;
Jann Hornab88ce52016-01-20 15:00:04 -0800156 const kernel_cap_t *caller_caps;
David Howellsc69e8d92008-11-14 10:39:19 +1100157
158 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700159 cred = current_cred();
160 child_cred = __task_cred(child);
Jann Hornab88ce52016-01-20 15:00:04 -0800161 if (mode & PTRACE_MODE_FSCREDS)
162 caller_caps = &cred->cap_effective;
163 else
164 caller_caps = &cred->cap_permitted;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800165 if (cred->user_ns == child_cred->user_ns &&
Jann Hornab88ce52016-01-20 15:00:04 -0800166 cap_issubset(child_cred->cap_permitted, *caller_caps))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700167 goto out;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800168 if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700169 goto out;
170 ret = -EPERM;
171out:
David Howellsc69e8d92008-11-14 10:39:19 +1100172 rcu_read_unlock();
173 return ret;
David Howells5cd9c582008-08-14 11:37:28 +0100174}
175
David Howells1d045982008-11-14 10:39:24 +1100176/**
177 * cap_ptrace_traceme - Determine whether another process may trace the current
178 * @parent: The task proposed to be the tracer
179 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700180 * If parent is in the same or an ancestor user_ns and has all current's
181 * capabilities, then ptrace access is allowed.
182 * If parent has the ptrace capability to current's user_ns, then ptrace
183 * access is allowed.
184 * Else denied.
185 *
David Howells1d045982008-11-14 10:39:24 +1100186 * Determine whether the nominated task is permitted to trace the current
187 * process, returning 0 if permission is granted, -ve if denied.
188 */
David Howells5cd9c582008-08-14 11:37:28 +0100189int cap_ptrace_traceme(struct task_struct *parent)
190{
David Howellsc69e8d92008-11-14 10:39:19 +1100191 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700192 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100193
194 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700195 cred = __task_cred(parent);
196 child_cred = current_cred();
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800197 if (cred->user_ns == child_cred->user_ns &&
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700198 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
199 goto out;
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800200 if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700201 goto out;
202 ret = -EPERM;
203out:
David Howellsc69e8d92008-11-14 10:39:19 +1100204 rcu_read_unlock();
205 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
David Howells1d045982008-11-14 10:39:24 +1100208/**
209 * cap_capget - Retrieve a task's capability sets
210 * @target: The task from which to retrieve the capability sets
211 * @effective: The place to record the effective set
212 * @inheritable: The place to record the inheritable set
213 * @permitted: The place to record the permitted set
214 *
215 * This function retrieves the capabilities of the nominated task and returns
216 * them to the caller.
217 */
218int cap_capget(struct task_struct *target, kernel_cap_t *effective,
219 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
David Howellsc69e8d92008-11-14 10:39:19 +1100221 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100224 rcu_read_lock();
225 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100226 *effective = cred->cap_effective;
227 *inheritable = cred->cap_inheritable;
228 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100229 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231}
232
David Howells1d045982008-11-14 10:39:24 +1100233/*
234 * Determine whether the inheritable capabilities are limited to the old
235 * permitted set. Returns 1 if they are limited, 0 if they are not.
236 */
Andrew Morgan72c2d582007-10-18 03:05:59 -0700237static inline int cap_inh_is_capped(void)
238{
David Howells1d045982008-11-14 10:39:24 +1100239
240 /* they are so limited unless the current task has the CAP_SETPCAP
241 * capability
Andrew Morgan72c2d582007-10-18 03:05:59 -0700242 */
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800243 if (cap_capable(current_cred(), current_cred()->user_ns,
Eric Paris6a9de492012-01-03 12:25:14 -0500244 CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
David Howells1d045982008-11-14 10:39:24 +1100245 return 0;
David Howells1d045982008-11-14 10:39:24 +1100246 return 1;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700247}
248
David Howells1d045982008-11-14 10:39:24 +1100249/**
250 * cap_capset - Validate and apply proposed changes to current's capabilities
251 * @new: The proposed new credentials; alterations should be made here
252 * @old: The current task's current credentials
253 * @effective: A pointer to the proposed new effective capabilities set
254 * @inheritable: A pointer to the proposed new inheritable capabilities set
255 * @permitted: A pointer to the proposed new permitted capabilities set
256 *
257 * This function validates and applies a proposed mass change to the current
258 * process's capability sets. The changes are made to the proposed new
259 * credentials, and assuming no error, will be committed by the caller of LSM.
260 */
David Howellsd84f4f92008-11-14 10:39:23 +1100261int cap_capset(struct cred *new,
262 const struct cred *old,
263 const kernel_cap_t *effective,
264 const kernel_cap_t *inheritable,
265 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
David Howellsd84f4f92008-11-14 10:39:23 +1100267 if (cap_inh_is_capped() &&
268 !cap_issubset(*inheritable,
269 cap_combine(old->cap_inheritable,
270 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700271 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100273
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800274 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100275 cap_combine(old->cap_inheritable,
276 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800277 /* no new pI capabilities outside bounding set */
278 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100281 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100285 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David Howellsd84f4f92008-11-14 10:39:23 +1100288 new->cap_effective = *effective;
289 new->cap_inheritable = *inheritable;
290 new->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
293
David Howells1d045982008-11-14 10:39:24 +1100294/*
295 * Clear proposed capability sets for execve().
296 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700297static inline void bprm_clear_caps(struct linux_binprm *bprm)
298{
David Howellsa6f76f22008-11-14 10:39:24 +1100299 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700300 bprm->cap_effective = false;
301}
302
David Howells1d045982008-11-14 10:39:24 +1100303/**
304 * cap_inode_need_killpriv - Determine if inode change affects privileges
305 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
306 *
307 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
308 * affects the security markings on that inode, and if it is, should
309 * inode_killpriv() be invoked or the change rejected?
310 *
311 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
312 * -ve to deny the change.
313 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700314int cap_inode_need_killpriv(struct dentry *dentry)
315{
David Howellsc6f493d2015-03-17 22:26:22 +0000316 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700317 int error;
318
Al Viroacfa4382008-12-04 10:06:33 -0500319 if (!inode->i_op->getxattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700320 return 0;
321
322 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
323 if (error <= 0)
324 return 0;
325 return 1;
326}
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{
David Howellsc6f493d2015-03-17 22:26:22 +0000338 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700339
Al Viroacfa4382008-12-04 10:06:33 -0500340 if (!inode->i_op->removexattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700341 return 0;
342
343 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
344}
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)
371 */
David Howellsa6f76f22008-11-14 10:39:24 +1100372 new->cap_permitted.cap[i] =
373 (new->cap_bset.cap[i] & permitted) |
374 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100375
David Howellsa6f76f22008-11-14 10:39:24 +1100376 if (permitted & ~new->cap_permitted.cap[i])
377 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100378 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100379 }
380
381 /*
382 * For legacy apps, with no internal support for recognizing they
383 * do not have enough capabilities, we return an error if they are
384 * missing some "forced" (aka file-permitted) capabilities.
385 */
David Howellsa6f76f22008-11-14 10:39:24 +1100386 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100387}
388
David Howells1d045982008-11-14 10:39:24 +1100389/*
390 * Extract the on-exec-apply capability sets for an executable file.
391 */
Eric Parisc0b00442008-11-11 21:48:10 +1100392int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
393{
David Howellsc6f493d2015-03-17 22:26:22 +0000394 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700395 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800396 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100397 int size;
398 struct vfs_cap_data caps;
399
400 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
401
Al Viroacfa4382008-12-04 10:06:33 -0500402 if (!inode || !inode->i_op->getxattr)
Eric Parisc0b00442008-11-11 21:48:10 +1100403 return -ENODATA;
404
405 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
406 XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100407 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100408 /* no data, that's ok */
409 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100410 if (size < 0)
411 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700412
Andrew Morgane338d262008-02-04 22:29:42 -0800413 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700414 return -EINVAL;
415
Eric Parisc0b00442008-11-11 21:48:10 +1100416 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700417
David Howellsa6f76f22008-11-14 10:39:24 +1100418 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800419 case VFS_CAP_REVISION_1:
420 if (size != XATTR_CAPS_SZ_1)
421 return -EINVAL;
422 tocopy = VFS_CAP_U32_1;
423 break;
424 case VFS_CAP_REVISION_2:
425 if (size != XATTR_CAPS_SZ_2)
426 return -EINVAL;
427 tocopy = VFS_CAP_U32_2;
428 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700429 default:
430 return -EINVAL;
431 }
Andrew Morgane338d262008-02-04 22:29:42 -0800432
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700433 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100434 if (i >= tocopy)
435 break;
436 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
437 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800438 }
David Howellsa6f76f22008-11-14 10:39:24 +1100439
Eric Paris7d8b6c62014-07-23 15:36:26 -0400440 cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
441 cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
442
Eric Parisc0b00442008-11-11 21:48:10 +1100443 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700444}
445
David Howells1d045982008-11-14 10:39:24 +1100446/*
447 * Attempt to get the on-exec apply capability sets for an executable file from
448 * its xattrs and, if present, apply them to the proposed credentials being
449 * constructed by execve().
450 */
Zhi Li4d49f672011-08-11 13:27:50 +0800451static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700452{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700453 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100454 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700455
Serge Hallyn3318a382008-10-30 11:52:23 -0500456 bprm_clear_caps(bprm);
457
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600458 if (!file_caps_enabled)
459 return 0;
460
Al Viro182be682013-01-24 02:21:54 -0500461 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700462 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700463
Al Virof4a4a8b2014-12-28 09:27:07 -0500464 rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
Eric Parisc0b00442008-11-11 21:48:10 +1100465 if (rc < 0) {
466 if (rc == -EINVAL)
467 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
468 __func__, rc, bprm->filename);
469 else if (rc == -ENODATA)
470 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700471 goto out;
472 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700473
Zhi Li4d49f672011-08-11 13:27:50 +0800474 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100475 if (rc == -EINVAL)
476 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
477 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700478
479out:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700480 if (rc)
481 bprm_clear_caps(bprm);
482
483 return rc;
484}
485
David Howells1d045982008-11-14 10:39:24 +1100486/**
487 * cap_bprm_set_creds - Set up the proposed credentials for execve().
488 * @bprm: The execution parameters, including the proposed creds
489 *
490 * Set up the proposed credentials for a new execution context being
491 * constructed by execve(). The proposed creds in @bprm->cred is altered,
492 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100493 */
494int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
David Howellsa6f76f22008-11-14 10:39:24 +1100496 const struct cred *old = current_cred();
497 struct cred *new = bprm->cred;
Serge Hallyn7d8db182011-08-15 08:29:50 -0500498 bool effective, has_cap = false;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700499 int ret;
Eric W. Biederman18815a12012-02-07 16:45:47 -0800500 kuid_t root_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David Howellsa6f76f22008-11-14 10:39:24 +1100502 effective = false;
Zhi Li4d49f672011-08-11 13:27:50 +0800503 ret = get_file_caps(bprm, &effective, &has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100504 if (ret < 0)
505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Eric W. Biederman18815a12012-02-07 16:45:47 -0800507 root_uid = make_kuid(new->user_ns, 0);
508
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700509 if (!issecure(SECURE_NOROOT)) {
510 /*
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500511 * If the legacy file capability is set, then don't set privs
512 * for a setuid root binary run by a non-root user. Do set it
513 * for a root user just to cause least surprise to an admin.
514 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800515 if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500516 warn_setuid_and_fcaps_mixed(bprm->filename);
517 goto skip;
518 }
519 /*
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700520 * To support inheritance of root-permissions and suid-root
521 * executables under compatibility mode, we override the
522 * capability sets for the file.
523 *
David Howellsa6f76f22008-11-14 10:39:24 +1100524 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700525 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800526 if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700527 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100528 new->cap_permitted = cap_combine(old->cap_bset,
529 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800531 if (uid_eq(new->euid, root_uid))
David Howellsa6f76f22008-11-14 10:39:24 +1100532 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500534skip:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700535
Eric Parisd52fc5d2012-04-17 16:26:54 -0400536 /* if we have fs caps, clear dangerous personality flags */
537 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
538 bprm->per_clear |= PER_CLEAR_ON_SETID;
539
540
David Howellsa6f76f22008-11-14 10:39:24 +1100541 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500542 * credentials unless they have the appropriate permit.
543 *
544 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
David Howellsa6f76f22008-11-14 10:39:24 +1100545 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800546 if ((!uid_eq(new->euid, old->uid) ||
547 !gid_eq(new->egid, old->gid) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100548 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
549 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
550 /* downgrade; they get no more than they had, and maybe less */
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500551 if (!capable(CAP_SETUID) ||
552 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100553 new->euid = new->uid;
554 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Serge E. Hallynb3a222e2009-11-23 16:21:30 -0600556 new->cap_permitted = cap_intersect(new->cap_permitted,
557 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
David Howellsa6f76f22008-11-14 10:39:24 +1100560 new->suid = new->fsuid = new->euid;
561 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Eric Paris4bf2ea72011-04-01 17:08:28 -0400563 if (effective)
564 new->cap_effective = new->cap_permitted;
565 else
566 cap_clear(new->cap_effective);
David Howellsa6f76f22008-11-14 10:39:24 +1100567 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Eric Paris3fc689e2008-11-11 21:48:18 +1100569 /*
570 * Audit candidate if current->cap_effective is set
571 *
572 * We do not bother to audit if 3 things are true:
573 * 1) cap_effective has all caps
574 * 2) we are root
575 * 3) root is supposed to have all caps (SECURE_NOROOT)
576 * Since this is just a normal root execing a process.
577 *
578 * Number 1 above might fail if you don't have a full bset, but I think
579 * that is interesting information to audit.
580 */
David Howellsd84f4f92008-11-14 10:39:23 +1100581 if (!cap_isclear(new->cap_effective)) {
582 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
Eric W. Biederman18815a12012-02-07 16:45:47 -0800583 !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100584 issecure(SECURE_NOROOT)) {
585 ret = audit_log_bprm_fcaps(bprm, new, old);
586 if (ret < 0)
587 return ret;
588 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
David Howellsd84f4f92008-11-14 10:39:23 +1100591 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
David Howellsa6f76f22008-11-14 10:39:24 +1100592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
David Howells1d045982008-11-14 10:39:24 +1100595/**
596 * cap_bprm_secureexec - Determine whether a secure execution is required
597 * @bprm: The execution parameters
598 *
599 * Determine whether a secure execution is required, return 1 if it is, and 0
600 * if it is not.
601 *
602 * The credentials have been committed by this point, and so are no longer
603 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100604 */
605int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
David Howellsc69e8d92008-11-14 10:39:19 +1100607 const struct cred *cred = current_cred();
Eric W. Biederman18815a12012-02-07 16:45:47 -0800608 kuid_t root_uid = make_kuid(cred->user_ns, 0);
David Howellsb6dff3e2008-11-14 10:39:16 +1100609
Eric W. Biederman18815a12012-02-07 16:45:47 -0800610 if (!uid_eq(cred->uid, root_uid)) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700611 if (bprm->cap_effective)
612 return 1;
David Howellsa6f76f22008-11-14 10:39:24 +1100613 if (!cap_isclear(cred->cap_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700614 return 1;
615 }
616
Eric W. Biederman18815a12012-02-07 16:45:47 -0800617 return (!uid_eq(cred->euid, cred->uid) ||
618 !gid_eq(cred->egid, cred->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
David Howells1d045982008-11-14 10:39:24 +1100621/**
622 * cap_inode_setxattr - Determine whether an xattr may be altered
623 * @dentry: The inode/dentry being altered
624 * @name: The name of the xattr to be changed
625 * @value: The value that the xattr will be changed to
626 * @size: The size of value
627 * @flags: The replacement flag
628 *
629 * Determine whether an xattr may be altered or set on an inode, returning 0 if
630 * permission is granted, -ve if denied.
631 *
632 * This is used to make sure security xattrs don't get updated or set by those
633 * who aren't privileged to do so.
634 */
David Howells8f0cfa52008-04-29 00:59:41 -0700635int cap_inode_setxattr(struct dentry *dentry, const char *name,
636 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700638 if (!strcmp(name, XATTR_NAME_CAPS)) {
639 if (!capable(CAP_SETFCAP))
640 return -EPERM;
641 return 0;
David Howells1d045982008-11-14 10:39:24 +1100642 }
643
644 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700645 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 !capable(CAP_SYS_ADMIN))
647 return -EPERM;
648 return 0;
649}
650
David Howells1d045982008-11-14 10:39:24 +1100651/**
652 * cap_inode_removexattr - Determine whether an xattr may be removed
653 * @dentry: The inode/dentry being altered
654 * @name: The name of the xattr to be changed
655 *
656 * Determine whether an xattr may be removed from an inode, returning 0 if
657 * permission is granted, -ve if denied.
658 *
659 * This is used to make sure security xattrs don't get removed by those who
660 * aren't privileged to remove them.
661 */
David Howells8f0cfa52008-04-29 00:59:41 -0700662int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700664 if (!strcmp(name, XATTR_NAME_CAPS)) {
665 if (!capable(CAP_SETFCAP))
666 return -EPERM;
667 return 0;
David Howells1d045982008-11-14 10:39:24 +1100668 }
669
670 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700671 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 !capable(CAP_SYS_ADMIN))
673 return -EPERM;
674 return 0;
675}
676
David Howellsa6f76f22008-11-14 10:39:24 +1100677/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
679 * a process after a call to setuid, setreuid, or setresuid.
680 *
681 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
682 * {r,e,s}uid != 0, the permitted and effective capabilities are
683 * cleared.
684 *
685 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
686 * capabilities of the process are cleared.
687 *
688 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
689 * capabilities are set to the permitted capabilities.
690 *
David Howellsa6f76f22008-11-14 10:39:24 +1100691 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * never happen.
693 *
David Howellsa6f76f22008-11-14 10:39:24 +1100694 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
696 * cevans - New behaviour, Oct '99
697 * A process may, via prctl(), elect to keep its capabilities when it
698 * calls setuid() and switches away from uid==0. Both permitted and
699 * effective sets will be retained.
700 * Without this change, it was impossible for a daemon to drop only some
701 * of its privilege. The call to setuid(!=0) would drop all privileges!
702 * Keeping uid 0 is not an option because uid 0 owns too many vital
703 * files..
704 * Thanks to Olaf Kirch and Peter Benie for spotting this.
705 */
David Howellsd84f4f92008-11-14 10:39:23 +1100706static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Eric W. Biederman18815a12012-02-07 16:45:47 -0800708 kuid_t root_uid = make_kuid(old->user_ns, 0);
709
710 if ((uid_eq(old->uid, root_uid) ||
711 uid_eq(old->euid, root_uid) ||
712 uid_eq(old->suid, root_uid)) &&
713 (!uid_eq(new->uid, root_uid) &&
714 !uid_eq(new->euid, root_uid) &&
715 !uid_eq(new->suid, root_uid)) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700716 !issecure(SECURE_KEEP_CAPS)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100717 cap_clear(new->cap_permitted);
718 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800720 if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100721 cap_clear(new->cap_effective);
Eric W. Biederman18815a12012-02-07 16:45:47 -0800722 if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100723 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
David Howells1d045982008-11-14 10:39:24 +1100726/**
727 * cap_task_fix_setuid - Fix up the results of setuid() call
728 * @new: The proposed credentials
729 * @old: The current task's current credentials
730 * @flags: Indications of what has changed
731 *
732 * Fix up the results of setuid() call before the credential changes are
733 * actually applied, returning 0 to grant the changes, -ve to deny them.
734 */
David Howellsd84f4f92008-11-14 10:39:23 +1100735int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 switch (flags) {
738 case LSM_SETID_RE:
739 case LSM_SETID_ID:
740 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100741 /* juggle the capabilities to follow [RES]UID changes unless
742 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100743 if (!issecure(SECURE_NO_SETUID_FIXUP))
744 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
David Howells1d045982008-11-14 10:39:24 +1100747 case LSM_SETID_FS:
748 /* juggle the capabilties to follow FSUID changes, unless
749 * otherwise suppressed
750 *
David Howellsd84f4f92008-11-14 10:39:23 +1100751 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
752 * if not, we might be a bit too harsh here.
753 */
754 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
Eric W. Biederman18815a12012-02-07 16:45:47 -0800755 kuid_t root_uid = make_kuid(old->user_ns, 0);
756 if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100757 new->cap_effective =
758 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100759
Eric W. Biederman18815a12012-02-07 16:45:47 -0800760 if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100761 new->cap_effective =
762 cap_raise_fs_set(new->cap_effective,
763 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
David Howellsd84f4f92008-11-14 10:39:23 +1100765 break;
David Howells1d045982008-11-14 10:39:24 +1100766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 default:
768 return -EINVAL;
769 }
770
771 return 0;
772}
773
Serge E. Hallynb5376772007-10-16 23:31:36 -0700774/*
775 * Rationale: code calling task_setscheduler, task_setioprio, and
776 * task_setnice, assumes that
777 * . if capable(cap_sys_nice), then those actions should be allowed
778 * . if not capable(cap_sys_nice), but acting on your own processes,
779 * then those actions should be allowed
780 * This is insufficient now since you can call code without suid, but
781 * yet with increased caps.
782 * So we check for increased caps on the target process.
783 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400784static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700785{
Serge Hallynf54fb862013-07-23 13:18:53 -0500786 int is_subset, ret = 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100787
788 rcu_read_lock();
789 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
790 current_cred()->cap_permitted);
Serge Hallynf54fb862013-07-23 13:18:53 -0500791 if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
792 ret = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100793 rcu_read_unlock();
794
Serge Hallynf54fb862013-07-23 13:18:53 -0500795 return ret;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700796}
797
David Howells1d045982008-11-14 10:39:24 +1100798/**
799 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
800 * @p: The task to affect
David Howells1d045982008-11-14 10:39:24 +1100801 *
802 * Detemine if the requested scheduler policy change is permitted for the
803 * specified task, returning 0 if permission is granted, -ve if denied.
804 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +0900805int cap_task_setscheduler(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700806{
807 return cap_safe_nice(p);
808}
809
David Howells1d045982008-11-14 10:39:24 +1100810/**
811 * cap_task_ioprio - Detemine if I/O priority change is permitted
812 * @p: The task to affect
813 * @ioprio: The I/O priority to set
814 *
815 * Detemine if the requested I/O priority change is permitted for the specified
816 * task, returning 0 if permission is granted, -ve if denied.
817 */
818int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700819{
820 return cap_safe_nice(p);
821}
822
David Howells1d045982008-11-14 10:39:24 +1100823/**
824 * cap_task_ioprio - Detemine if task priority change is permitted
825 * @p: The task to affect
826 * @nice: The nice value to set
827 *
828 * Detemine if the requested task priority change is permitted for the
829 * specified task, returning 0 if permission is granted, -ve if denied.
830 */
831int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700832{
833 return cap_safe_nice(p);
834}
835
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800836/*
David Howells1d045982008-11-14 10:39:24 +1100837 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
838 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800839 */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900840static int cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800841{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900842 struct cred *new;
843
Eric W. Biederman160da842013-07-02 10:04:54 -0700844 if (!ns_capable(current_user_ns(), CAP_SETPCAP))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800845 return -EPERM;
846 if (!cap_valid(cap))
847 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100848
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900849 new = prepare_creds();
850 if (!new)
851 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100852 cap_lower(new->cap_bset, cap);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900853 return commit_creds(new);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800854}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700855
David Howells1d045982008-11-14 10:39:24 +1100856/**
857 * cap_task_prctl - Implement process control functions for this security module
858 * @option: The process control function requested
859 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
860 *
861 * Allow process control functions (sys_prctl()) to alter capabilities; may
862 * also deny access to other functions not otherwise implemented here.
863 *
864 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
865 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
866 * modules will consider performing the function.
867 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700868int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100869 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700870{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900871 const struct cred *old = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +1100872 struct cred *new;
David Howellsd84f4f92008-11-14 10:39:23 +1100873
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700874 switch (option) {
875 case PR_CAPBSET_READ:
876 if (!cap_valid(arg2))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900877 return -EINVAL;
878 return !!cap_raised(old->cap_bset, arg2);
David Howellsd84f4f92008-11-14 10:39:23 +1100879
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700880 case PR_CAPBSET_DROP:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900881 return cap_prctl_drop(arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700882
883 /*
884 * The next four prctl's remain to assist with transitioning a
885 * system from legacy UID=0 based privilege (when filesystem
886 * capabilities are not in use) to a system using filesystem
887 * capabilities only - as the POSIX.1e draft intended.
888 *
889 * Note:
890 *
891 * PR_SET_SECUREBITS =
892 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
893 * | issecure_mask(SECURE_NOROOT)
894 * | issecure_mask(SECURE_NOROOT_LOCKED)
895 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
896 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
897 *
898 * will ensure that the current process and all of its
899 * children will be locked into a pure
900 * capability-based-privilege environment.
901 */
902 case PR_SET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900903 if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
904 & (old->securebits ^ arg2)) /*[1]*/
905 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
David Howellsd84f4f92008-11-14 10:39:23 +1100906 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris6a9de492012-01-03 12:25:14 -0500907 || (cap_capable(current_cred(),
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800908 current_cred()->user_ns, CAP_SETPCAP,
David Howells3699c532009-01-06 22:27:01 +0000909 SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700910 /*
911 * [1] no changing of bits that are locked
912 * [2] no unlocking of locks
913 * [3] no setting of unsupported bits
914 * [4] doing anything requires privilege (go read about
915 * the "sendmail capabilities bug")
916 */
David Howellsd84f4f92008-11-14 10:39:23 +1100917 )
918 /* cannot change a locked bit */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900919 return -EPERM;
920
921 new = prepare_creds();
922 if (!new)
923 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100924 new->securebits = arg2;
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900925 return commit_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100926
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700927 case PR_GET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900928 return old->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700929
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700930 case PR_GET_KEEPCAPS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900931 return !!issecure(SECURE_KEEP_CAPS);
David Howellsd84f4f92008-11-14 10:39:23 +1100932
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700933 case PR_SET_KEEPCAPS:
934 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900935 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100936 if (issecure(SECURE_KEEP_CAPS_LOCKED))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900937 return -EPERM;
938
939 new = prepare_creds();
940 if (!new)
941 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100942 if (arg2)
943 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700944 else
David Howellsd84f4f92008-11-14 10:39:23 +1100945 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900946 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700947
948 default:
949 /* No functionality available - continue with default */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900950 return -ENOSYS;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
David Howells1d045982008-11-14 10:39:24 +1100954/**
David Howells1d045982008-11-14 10:39:24 +1100955 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
956 * @mm: The VM space in which the new mapping is to be made
957 * @pages: The size of the mapping
958 *
959 * Determine whether the allocation of a new virtual mapping by the current
960 * task is permitted, returning 0 if permission is granted, -ve if not.
961 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700962int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 int cap_sys_admin = 0;
965
Eric Paris6a9de492012-01-03 12:25:14 -0500966 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +0000967 SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700969 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
Eric Paris7c738752009-07-31 12:53:58 -0400971
972/*
Al Virod0077942012-05-30 13:11:37 -0400973 * cap_mmap_addr - check if able to map given addr
974 * @addr: address attempting to be mapped
975 *
976 * If the process is attempting to map memory below dac_mmap_min_addr they need
977 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
978 * capability security module. Returns 0 if this mapping should be allowed
979 * -EPERM if not.
980 */
981int cap_mmap_addr(unsigned long addr)
982{
983 int ret = 0;
984
985 if (addr < dac_mmap_min_addr) {
986 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
987 SECURITY_CAP_AUDIT);
988 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
989 if (ret == 0)
990 current->flags |= PF_SUPERPRIV;
991 }
992 return ret;
993}
994
Al Viroe5467852012-05-30 13:30:51 -0400995int cap_mmap_file(struct file *file, unsigned long reqprot,
996 unsigned long prot, unsigned long flags)
Eric Paris7c738752009-07-31 12:53:58 -0400997{
Al Viroe5467852012-05-30 13:30:51 -0400998 return 0;
Eric Paris7c738752009-07-31 12:53:58 -0400999}