blob: 959026c4d4b92236068374a116bb889e9c7ed2a2 [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;
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700291
292 /*
293 * Mask off ambient bits that are no longer both permitted and
294 * inheritable.
295 */
296 new->cap_ambient = cap_intersect(new->cap_ambient,
297 cap_intersect(*permitted,
298 *inheritable));
299 if (WARN_ON(!cap_ambient_invariant_ok(new)))
300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302}
303
David Howells1d045982008-11-14 10:39:24 +1100304/*
305 * Clear proposed capability sets for execve().
306 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700307static inline void bprm_clear_caps(struct linux_binprm *bprm)
308{
David Howellsa6f76f22008-11-14 10:39:24 +1100309 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700310 bprm->cap_effective = false;
311}
312
David Howells1d045982008-11-14 10:39:24 +1100313/**
314 * cap_inode_need_killpriv - Determine if inode change affects privileges
315 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
316 *
317 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
318 * affects the security markings on that inode, and if it is, should
319 * inode_killpriv() be invoked or the change rejected?
320 *
321 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
322 * -ve to deny the change.
323 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700324int cap_inode_need_killpriv(struct dentry *dentry)
325{
David Howellsc6f493d2015-03-17 22:26:22 +0000326 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700327 int error;
328
Al Viroacfa4382008-12-04 10:06:33 -0500329 if (!inode->i_op->getxattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700330 return 0;
331
332 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
333 if (error <= 0)
334 return 0;
335 return 1;
336}
337
David Howells1d045982008-11-14 10:39:24 +1100338/**
339 * cap_inode_killpriv - Erase the security markings on an inode
340 * @dentry: The inode/dentry to alter
341 *
342 * Erase the privilege-enhancing security markings on an inode.
343 *
344 * Returns 0 if successful, -ve on error.
345 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700346int cap_inode_killpriv(struct dentry *dentry)
347{
David Howellsc6f493d2015-03-17 22:26:22 +0000348 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700349
Al Viroacfa4382008-12-04 10:06:33 -0500350 if (!inode->i_op->removexattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700351 return 0;
352
353 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
354}
355
David Howells1d045982008-11-14 10:39:24 +1100356/*
357 * Calculate the new process capability sets from the capability sets attached
358 * to a file.
359 */
Eric Parisc0b00442008-11-11 21:48:10 +1100360static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100361 struct linux_binprm *bprm,
Zhi Li4d49f672011-08-11 13:27:50 +0800362 bool *effective,
363 bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700364{
David Howellsa6f76f22008-11-14 10:39:24 +1100365 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100366 unsigned i;
367 int ret = 0;
368
369 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100370 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100371
Zhi Li4d49f672011-08-11 13:27:50 +0800372 if (caps->magic_etc & VFS_CAP_REVISION_MASK)
373 *has_cap = true;
374
Eric Parisc0b00442008-11-11 21:48:10 +1100375 CAP_FOR_EACH_U32(i) {
376 __u32 permitted = caps->permitted.cap[i];
377 __u32 inheritable = caps->inheritable.cap[i];
378
379 /*
380 * pP' = (X & fP) | (pI & fI)
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700381 * The addition of pA' is handled later.
Eric Parisc0b00442008-11-11 21:48:10 +1100382 */
David Howellsa6f76f22008-11-14 10:39:24 +1100383 new->cap_permitted.cap[i] =
384 (new->cap_bset.cap[i] & permitted) |
385 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100386
David Howellsa6f76f22008-11-14 10:39:24 +1100387 if (permitted & ~new->cap_permitted.cap[i])
388 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100389 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100390 }
391
392 /*
393 * For legacy apps, with no internal support for recognizing they
394 * do not have enough capabilities, we return an error if they are
395 * missing some "forced" (aka file-permitted) capabilities.
396 */
David Howellsa6f76f22008-11-14 10:39:24 +1100397 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100398}
399
David Howells1d045982008-11-14 10:39:24 +1100400/*
401 * Extract the on-exec-apply capability sets for an executable file.
402 */
Eric Parisc0b00442008-11-11 21:48:10 +1100403int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
404{
David Howellsc6f493d2015-03-17 22:26:22 +0000405 struct inode *inode = d_backing_inode(dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700406 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800407 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100408 int size;
409 struct vfs_cap_data caps;
410
411 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
412
Al Viroacfa4382008-12-04 10:06:33 -0500413 if (!inode || !inode->i_op->getxattr)
Eric Parisc0b00442008-11-11 21:48:10 +1100414 return -ENODATA;
415
416 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
417 XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100418 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100419 /* no data, that's ok */
420 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100421 if (size < 0)
422 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700423
Andrew Morgane338d262008-02-04 22:29:42 -0800424 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700425 return -EINVAL;
426
Eric Parisc0b00442008-11-11 21:48:10 +1100427 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700428
David Howellsa6f76f22008-11-14 10:39:24 +1100429 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800430 case VFS_CAP_REVISION_1:
431 if (size != XATTR_CAPS_SZ_1)
432 return -EINVAL;
433 tocopy = VFS_CAP_U32_1;
434 break;
435 case VFS_CAP_REVISION_2:
436 if (size != XATTR_CAPS_SZ_2)
437 return -EINVAL;
438 tocopy = VFS_CAP_U32_2;
439 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700440 default:
441 return -EINVAL;
442 }
Andrew Morgane338d262008-02-04 22:29:42 -0800443
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700444 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100445 if (i >= tocopy)
446 break;
447 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
448 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800449 }
David Howellsa6f76f22008-11-14 10:39:24 +1100450
Eric Paris7d8b6c62014-07-23 15:36:26 -0400451 cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
452 cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
453
Eric Parisc0b00442008-11-11 21:48:10 +1100454 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700455}
456
David Howells1d045982008-11-14 10:39:24 +1100457/*
458 * Attempt to get the on-exec apply capability sets for an executable file from
459 * its xattrs and, if present, apply them to the proposed credentials being
460 * constructed by execve().
461 */
Zhi Li4d49f672011-08-11 13:27:50 +0800462static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700463{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700464 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100465 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700466
Serge Hallyn3318a382008-10-30 11:52:23 -0500467 bprm_clear_caps(bprm);
468
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600469 if (!file_caps_enabled)
470 return 0;
471
Al Viro182be682013-01-24 02:21:54 -0500472 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700473 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700474
Al Virof4a4a8b2014-12-28 09:27:07 -0500475 rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
Eric Parisc0b00442008-11-11 21:48:10 +1100476 if (rc < 0) {
477 if (rc == -EINVAL)
478 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
479 __func__, rc, bprm->filename);
480 else if (rc == -ENODATA)
481 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700482 goto out;
483 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700484
Zhi Li4d49f672011-08-11 13:27:50 +0800485 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100486 if (rc == -EINVAL)
487 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
488 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700489
490out:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700491 if (rc)
492 bprm_clear_caps(bprm);
493
494 return rc;
495}
496
David Howells1d045982008-11-14 10:39:24 +1100497/**
498 * cap_bprm_set_creds - Set up the proposed credentials for execve().
499 * @bprm: The execution parameters, including the proposed creds
500 *
501 * Set up the proposed credentials for a new execution context being
502 * constructed by execve(). The proposed creds in @bprm->cred is altered,
503 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100504 */
505int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
David Howellsa6f76f22008-11-14 10:39:24 +1100507 const struct cred *old = current_cred();
508 struct cred *new = bprm->cred;
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700509 bool effective, has_cap = false, is_setid;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700510 int ret;
Eric W. Biederman18815a12012-02-07 16:45:47 -0800511 kuid_t root_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700513 if (WARN_ON(!cap_ambient_invariant_ok(old)))
514 return -EPERM;
515
David Howellsa6f76f22008-11-14 10:39:24 +1100516 effective = false;
Zhi Li4d49f672011-08-11 13:27:50 +0800517 ret = get_file_caps(bprm, &effective, &has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100518 if (ret < 0)
519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Eric W. Biederman18815a12012-02-07 16:45:47 -0800521 root_uid = make_kuid(new->user_ns, 0);
522
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700523 if (!issecure(SECURE_NOROOT)) {
524 /*
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500525 * If the legacy file capability is set, then don't set privs
526 * for a setuid root binary run by a non-root user. Do set it
527 * for a root user just to cause least surprise to an admin.
528 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800529 if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500530 warn_setuid_and_fcaps_mixed(bprm->filename);
531 goto skip;
532 }
533 /*
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700534 * To support inheritance of root-permissions and suid-root
535 * executables under compatibility mode, we override the
536 * capability sets for the file.
537 *
David Howellsa6f76f22008-11-14 10:39:24 +1100538 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700539 */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800540 if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700541 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100542 new->cap_permitted = cap_combine(old->cap_bset,
543 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800545 if (uid_eq(new->euid, root_uid))
David Howellsa6f76f22008-11-14 10:39:24 +1100546 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500548skip:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700549
Eric Parisd52fc5d2012-04-17 16:26:54 -0400550 /* if we have fs caps, clear dangerous personality flags */
551 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
552 bprm->per_clear |= PER_CLEAR_ON_SETID;
553
554
David Howellsa6f76f22008-11-14 10:39:24 +1100555 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500556 * credentials unless they have the appropriate permit.
557 *
558 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
David Howellsa6f76f22008-11-14 10:39:24 +1100559 */
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700560 is_setid = !uid_eq(new->euid, old->uid) || !gid_eq(new->egid, old->gid);
561
562 if ((is_setid ||
David Howellsa6f76f22008-11-14 10:39:24 +1100563 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
564 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
565 /* downgrade; they get no more than they had, and maybe less */
Andy Lutomirski259e5e62012-04-12 16:47:50 -0500566 if (!capable(CAP_SETUID) ||
567 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100568 new->euid = new->uid;
569 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Serge E. Hallynb3a222e2009-11-23 16:21:30 -0600571 new->cap_permitted = cap_intersect(new->cap_permitted,
572 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
David Howellsa6f76f22008-11-14 10:39:24 +1100575 new->suid = new->fsuid = new->euid;
576 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700578 /* File caps or setid cancels ambient. */
579 if (has_cap || is_setid)
580 cap_clear(new->cap_ambient);
581
582 /*
583 * Now that we've computed pA', update pP' to give:
584 * pP' = (X & fP) | (pI & fI) | pA'
585 */
586 new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
587
588 /*
589 * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
590 * this is the same as pE' = (fE ? pP' : 0) | pA'.
591 */
Eric Paris4bf2ea72011-04-01 17:08:28 -0400592 if (effective)
593 new->cap_effective = new->cap_permitted;
594 else
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700595 new->cap_effective = new->cap_ambient;
596
597 if (WARN_ON(!cap_ambient_invariant_ok(new)))
598 return -EPERM;
599
David Howellsa6f76f22008-11-14 10:39:24 +1100600 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Eric Paris3fc689e2008-11-11 21:48:18 +1100602 /*
603 * Audit candidate if current->cap_effective is set
604 *
605 * We do not bother to audit if 3 things are true:
606 * 1) cap_effective has all caps
607 * 2) we are root
608 * 3) root is supposed to have all caps (SECURE_NOROOT)
609 * Since this is just a normal root execing a process.
610 *
611 * Number 1 above might fail if you don't have a full bset, but I think
612 * that is interesting information to audit.
613 */
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700614 if (!cap_issubset(new->cap_effective, new->cap_ambient)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100615 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
Eric W. Biederman18815a12012-02-07 16:45:47 -0800616 !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100617 issecure(SECURE_NOROOT)) {
618 ret = audit_log_bprm_fcaps(bprm, new, old);
619 if (ret < 0)
620 return ret;
621 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
David Howellsd84f4f92008-11-14 10:39:23 +1100624 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700625
626 if (WARN_ON(!cap_ambient_invariant_ok(new)))
627 return -EPERM;
628
David Howellsa6f76f22008-11-14 10:39:24 +1100629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
David Howells1d045982008-11-14 10:39:24 +1100632/**
633 * cap_bprm_secureexec - Determine whether a secure execution is required
634 * @bprm: The execution parameters
635 *
636 * Determine whether a secure execution is required, return 1 if it is, and 0
637 * if it is not.
638 *
639 * The credentials have been committed by this point, and so are no longer
640 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100641 */
642int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
David Howellsc69e8d92008-11-14 10:39:19 +1100644 const struct cred *cred = current_cred();
Eric W. Biederman18815a12012-02-07 16:45:47 -0800645 kuid_t root_uid = make_kuid(cred->user_ns, 0);
David Howellsb6dff3e2008-11-14 10:39:16 +1100646
Eric W. Biederman18815a12012-02-07 16:45:47 -0800647 if (!uid_eq(cred->uid, root_uid)) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700648 if (bprm->cap_effective)
649 return 1;
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700650 if (!cap_issubset(cred->cap_permitted, cred->cap_ambient))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700651 return 1;
652 }
653
Eric W. Biederman18815a12012-02-07 16:45:47 -0800654 return (!uid_eq(cred->euid, cred->uid) ||
655 !gid_eq(cred->egid, cred->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
David Howells1d045982008-11-14 10:39:24 +1100658/**
659 * cap_inode_setxattr - Determine whether an xattr may be altered
660 * @dentry: The inode/dentry being altered
661 * @name: The name of the xattr to be changed
662 * @value: The value that the xattr will be changed to
663 * @size: The size of value
664 * @flags: The replacement flag
665 *
666 * Determine whether an xattr may be altered or set on an inode, returning 0 if
667 * permission is granted, -ve if denied.
668 *
669 * This is used to make sure security xattrs don't get updated or set by those
670 * who aren't privileged to do so.
671 */
David Howells8f0cfa52008-04-29 00:59:41 -0700672int cap_inode_setxattr(struct dentry *dentry, const char *name,
673 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700675 if (!strcmp(name, XATTR_NAME_CAPS)) {
676 if (!capable(CAP_SETFCAP))
677 return -EPERM;
678 return 0;
David Howells1d045982008-11-14 10:39:24 +1100679 }
680
681 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700682 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 !capable(CAP_SYS_ADMIN))
684 return -EPERM;
685 return 0;
686}
687
David Howells1d045982008-11-14 10:39:24 +1100688/**
689 * cap_inode_removexattr - Determine whether an xattr may be removed
690 * @dentry: The inode/dentry being altered
691 * @name: The name of the xattr to be changed
692 *
693 * Determine whether an xattr may be removed from an inode, returning 0 if
694 * permission is granted, -ve if denied.
695 *
696 * This is used to make sure security xattrs don't get removed by those who
697 * aren't privileged to remove them.
698 */
David Howells8f0cfa52008-04-29 00:59:41 -0700699int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700701 if (!strcmp(name, XATTR_NAME_CAPS)) {
702 if (!capable(CAP_SETFCAP))
703 return -EPERM;
704 return 0;
David Howells1d045982008-11-14 10:39:24 +1100705 }
706
707 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700708 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 !capable(CAP_SYS_ADMIN))
710 return -EPERM;
711 return 0;
712}
713
David Howellsa6f76f22008-11-14 10:39:24 +1100714/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
716 * a process after a call to setuid, setreuid, or setresuid.
717 *
718 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
719 * {r,e,s}uid != 0, the permitted and effective capabilities are
720 * cleared.
721 *
722 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
723 * capabilities of the process are cleared.
724 *
725 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
726 * capabilities are set to the permitted capabilities.
727 *
David Howellsa6f76f22008-11-14 10:39:24 +1100728 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * never happen.
730 *
David Howellsa6f76f22008-11-14 10:39:24 +1100731 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
733 * cevans - New behaviour, Oct '99
734 * A process may, via prctl(), elect to keep its capabilities when it
735 * calls setuid() and switches away from uid==0. Both permitted and
736 * effective sets will be retained.
737 * Without this change, it was impossible for a daemon to drop only some
738 * of its privilege. The call to setuid(!=0) would drop all privileges!
739 * Keeping uid 0 is not an option because uid 0 owns too many vital
740 * files..
741 * Thanks to Olaf Kirch and Peter Benie for spotting this.
742 */
David Howellsd84f4f92008-11-14 10:39:23 +1100743static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Eric W. Biederman18815a12012-02-07 16:45:47 -0800745 kuid_t root_uid = make_kuid(old->user_ns, 0);
746
747 if ((uid_eq(old->uid, root_uid) ||
748 uid_eq(old->euid, root_uid) ||
749 uid_eq(old->suid, root_uid)) &&
750 (!uid_eq(new->uid, root_uid) &&
751 !uid_eq(new->euid, root_uid) &&
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700752 !uid_eq(new->suid, root_uid))) {
753 if (!issecure(SECURE_KEEP_CAPS)) {
754 cap_clear(new->cap_permitted);
755 cap_clear(new->cap_effective);
756 }
757
758 /*
759 * Pre-ambient programs expect setresuid to nonroot followed
760 * by exec to drop capabilities. We should make sure that
761 * this remains the case.
762 */
763 cap_clear(new->cap_ambient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Eric W. Biederman18815a12012-02-07 16:45:47 -0800765 if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100766 cap_clear(new->cap_effective);
Eric W. Biederman18815a12012-02-07 16:45:47 -0800767 if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100768 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
David Howells1d045982008-11-14 10:39:24 +1100771/**
772 * cap_task_fix_setuid - Fix up the results of setuid() call
773 * @new: The proposed credentials
774 * @old: The current task's current credentials
775 * @flags: Indications of what has changed
776 *
777 * Fix up the results of setuid() call before the credential changes are
778 * actually applied, returning 0 to grant the changes, -ve to deny them.
779 */
David Howellsd84f4f92008-11-14 10:39:23 +1100780int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 switch (flags) {
783 case LSM_SETID_RE:
784 case LSM_SETID_ID:
785 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100786 /* juggle the capabilities to follow [RES]UID changes unless
787 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100788 if (!issecure(SECURE_NO_SETUID_FIXUP))
789 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
David Howells1d045982008-11-14 10:39:24 +1100792 case LSM_SETID_FS:
793 /* juggle the capabilties to follow FSUID changes, unless
794 * otherwise suppressed
795 *
David Howellsd84f4f92008-11-14 10:39:23 +1100796 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
797 * if not, we might be a bit too harsh here.
798 */
799 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
Eric W. Biederman18815a12012-02-07 16:45:47 -0800800 kuid_t root_uid = make_kuid(old->user_ns, 0);
801 if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100802 new->cap_effective =
803 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100804
Eric W. Biederman18815a12012-02-07 16:45:47 -0800805 if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100806 new->cap_effective =
807 cap_raise_fs_set(new->cap_effective,
808 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
David Howellsd84f4f92008-11-14 10:39:23 +1100810 break;
David Howells1d045982008-11-14 10:39:24 +1100811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 default:
813 return -EINVAL;
814 }
815
816 return 0;
817}
818
Serge E. Hallynb5376772007-10-16 23:31:36 -0700819/*
820 * Rationale: code calling task_setscheduler, task_setioprio, and
821 * task_setnice, assumes that
822 * . if capable(cap_sys_nice), then those actions should be allowed
823 * . if not capable(cap_sys_nice), but acting on your own processes,
824 * then those actions should be allowed
825 * This is insufficient now since you can call code without suid, but
826 * yet with increased caps.
827 * So we check for increased caps on the target process.
828 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400829static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700830{
Serge Hallynf54fb862013-07-23 13:18:53 -0500831 int is_subset, ret = 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100832
833 rcu_read_lock();
834 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
835 current_cred()->cap_permitted);
Serge Hallynf54fb862013-07-23 13:18:53 -0500836 if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
837 ret = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +1100838 rcu_read_unlock();
839
Serge Hallynf54fb862013-07-23 13:18:53 -0500840 return ret;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700841}
842
David Howells1d045982008-11-14 10:39:24 +1100843/**
844 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
845 * @p: The task to affect
David Howells1d045982008-11-14 10:39:24 +1100846 *
847 * Detemine if the requested scheduler policy change is permitted for the
848 * specified task, returning 0 if permission is granted, -ve if denied.
849 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +0900850int cap_task_setscheduler(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700851{
852 return cap_safe_nice(p);
853}
854
David Howells1d045982008-11-14 10:39:24 +1100855/**
856 * cap_task_ioprio - Detemine if I/O priority change is permitted
857 * @p: The task to affect
858 * @ioprio: The I/O priority to set
859 *
860 * Detemine if the requested I/O priority change is permitted for the specified
861 * task, returning 0 if permission is granted, -ve if denied.
862 */
863int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700864{
865 return cap_safe_nice(p);
866}
867
David Howells1d045982008-11-14 10:39:24 +1100868/**
869 * cap_task_ioprio - Detemine if task priority change is permitted
870 * @p: The task to affect
871 * @nice: The nice value to set
872 *
873 * Detemine if the requested task priority change is permitted for the
874 * specified task, returning 0 if permission is granted, -ve if denied.
875 */
876int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700877{
878 return cap_safe_nice(p);
879}
880
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800881/*
David Howells1d045982008-11-14 10:39:24 +1100882 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
883 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800884 */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900885static int cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800886{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900887 struct cred *new;
888
Eric W. Biederman160da842013-07-02 10:04:54 -0700889 if (!ns_capable(current_user_ns(), CAP_SETPCAP))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800890 return -EPERM;
891 if (!cap_valid(cap))
892 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100893
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900894 new = prepare_creds();
895 if (!new)
896 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100897 cap_lower(new->cap_bset, cap);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900898 return commit_creds(new);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800899}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700900
David Howells1d045982008-11-14 10:39:24 +1100901/**
902 * cap_task_prctl - Implement process control functions for this security module
903 * @option: The process control function requested
904 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
905 *
906 * Allow process control functions (sys_prctl()) to alter capabilities; may
907 * also deny access to other functions not otherwise implemented here.
908 *
909 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
910 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
911 * modules will consider performing the function.
912 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700913int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100914 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700915{
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900916 const struct cred *old = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +1100917 struct cred *new;
David Howellsd84f4f92008-11-14 10:39:23 +1100918
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700919 switch (option) {
920 case PR_CAPBSET_READ:
921 if (!cap_valid(arg2))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900922 return -EINVAL;
923 return !!cap_raised(old->cap_bset, arg2);
David Howellsd84f4f92008-11-14 10:39:23 +1100924
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700925 case PR_CAPBSET_DROP:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900926 return cap_prctl_drop(arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700927
928 /*
929 * The next four prctl's remain to assist with transitioning a
930 * system from legacy UID=0 based privilege (when filesystem
931 * capabilities are not in use) to a system using filesystem
932 * capabilities only - as the POSIX.1e draft intended.
933 *
934 * Note:
935 *
936 * PR_SET_SECUREBITS =
937 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
938 * | issecure_mask(SECURE_NOROOT)
939 * | issecure_mask(SECURE_NOROOT_LOCKED)
940 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
941 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
942 *
943 * will ensure that the current process and all of its
944 * children will be locked into a pure
945 * capability-based-privilege environment.
946 */
947 case PR_SET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900948 if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
949 & (old->securebits ^ arg2)) /*[1]*/
950 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
David Howellsd84f4f92008-11-14 10:39:23 +1100951 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris6a9de492012-01-03 12:25:14 -0500952 || (cap_capable(current_cred(),
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800953 current_cred()->user_ns, CAP_SETPCAP,
David Howells3699c532009-01-06 22:27:01 +0000954 SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700955 /*
956 * [1] no changing of bits that are locked
957 * [2] no unlocking of locks
958 * [3] no setting of unsupported bits
959 * [4] doing anything requires privilege (go read about
960 * the "sendmail capabilities bug")
961 */
David Howellsd84f4f92008-11-14 10:39:23 +1100962 )
963 /* cannot change a locked bit */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900964 return -EPERM;
965
966 new = prepare_creds();
967 if (!new)
968 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100969 new->securebits = arg2;
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900970 return commit_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100971
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700972 case PR_GET_SECUREBITS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900973 return old->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700974
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700975 case PR_GET_KEEPCAPS:
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900976 return !!issecure(SECURE_KEEP_CAPS);
David Howellsd84f4f92008-11-14 10:39:23 +1100977
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700978 case PR_SET_KEEPCAPS:
979 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900980 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100981 if (issecure(SECURE_KEEP_CAPS_LOCKED))
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900982 return -EPERM;
983
984 new = prepare_creds();
985 if (!new)
986 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +1100987 if (arg2)
988 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700989 else
David Howellsd84f4f92008-11-14 10:39:23 +1100990 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Tetsuo Handa6d6f3322014-07-22 21:20:01 +0900991 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700992
Andy Lutomirski23d398e2015-09-04 15:42:45 -0700993 case PR_CAP_AMBIENT:
994 if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
995 if (arg3 | arg4 | arg5)
996 return -EINVAL;
997
998 new = prepare_creds();
999 if (!new)
1000 return -ENOMEM;
1001 cap_clear(new->cap_ambient);
1002 return commit_creds(new);
1003 }
1004
1005 if (((!cap_valid(arg3)) | arg4 | arg5))
1006 return -EINVAL;
1007
1008 if (arg2 == PR_CAP_AMBIENT_IS_SET) {
1009 return !!cap_raised(current_cred()->cap_ambient, arg3);
1010 } else if (arg2 != PR_CAP_AMBIENT_RAISE &&
1011 arg2 != PR_CAP_AMBIENT_LOWER) {
1012 return -EINVAL;
1013 } else {
1014 if (arg2 == PR_CAP_AMBIENT_RAISE &&
1015 (!cap_raised(current_cred()->cap_permitted, arg3) ||
1016 !cap_raised(current_cred()->cap_inheritable,
1017 arg3)))
1018 return -EPERM;
1019
1020 new = prepare_creds();
1021 if (!new)
1022 return -ENOMEM;
1023 if (arg2 == PR_CAP_AMBIENT_RAISE)
1024 cap_raise(new->cap_ambient, arg3);
1025 else
1026 cap_lower(new->cap_ambient, arg3);
1027 return commit_creds(new);
1028 }
1029
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001030 default:
1031 /* No functionality available - continue with default */
Tetsuo Handa6d6f3322014-07-22 21:20:01 +09001032 return -ENOSYS;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
David Howells1d045982008-11-14 10:39:24 +11001036/**
David Howells1d045982008-11-14 10:39:24 +11001037 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
1038 * @mm: The VM space in which the new mapping is to be made
1039 * @pages: The size of the mapping
1040 *
1041 * Determine whether the allocation of a new virtual mapping by the current
1042 * task is permitted, returning 0 if permission is granted, -ve if not.
1043 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001044int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 int cap_sys_admin = 0;
1047
Eric Paris6a9de492012-01-03 12:25:14 -05001048 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00001049 SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -07001051 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
Eric Paris7c738752009-07-31 12:53:58 -04001053
1054/*
Al Virod0077942012-05-30 13:11:37 -04001055 * cap_mmap_addr - check if able to map given addr
1056 * @addr: address attempting to be mapped
1057 *
1058 * If the process is attempting to map memory below dac_mmap_min_addr they need
1059 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
1060 * capability security module. Returns 0 if this mapping should be allowed
1061 * -EPERM if not.
1062 */
1063int cap_mmap_addr(unsigned long addr)
1064{
1065 int ret = 0;
1066
1067 if (addr < dac_mmap_min_addr) {
1068 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
1069 SECURITY_CAP_AUDIT);
1070 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1071 if (ret == 0)
1072 current->flags |= PF_SUPERPRIV;
1073 }
1074 return ret;
1075}
1076
Al Viroe5467852012-05-30 13:30:51 -04001077int cap_mmap_file(struct file *file, unsigned long reqprot,
1078 unsigned long prot, unsigned long flags)
Eric Paris7c738752009-07-31 12:53:58 -04001079{
Al Viroe5467852012-05-30 13:30:51 -04001080 return 0;
Eric Paris7c738752009-07-31 12:53:58 -04001081}