blob: 1c5ee365189bbb9e8b7ee65aab5a6af849ea2a0f [file] [log] [blame]
James Morris3e1c2512009-10-20 13:48:33 +09001/* Common capabilities, needed by capability.o.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Eric Paris3fc689e2008-11-11 21:48:18 +110011#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/security.h>
16#include <linux/file.h>
17#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/pagemap.h>
20#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/netlink.h>
23#include <linux/ptrace.h>
24#include <linux/xattr.h>
25#include <linux/hugetlb.h>
Serge E. Hallynb5376772007-10-16 23:31:36 -070026#include <linux/mount.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070027#include <linux/sched.h>
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -070028#include <linux/prctl.h>
29#include <linux/securebits.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070030#include <linux/user_namespace.h>
Al Viro40401532012-02-13 03:58:52 +000031#include <linux/binfmts.h>
Jonghwan Choi51b79be2012-04-18 17:23:04 -040032#include <linux/personality.h>
Andrew Morgan72c2d582007-10-18 03:05:59 -070033
Chia-chi Yeh04320132009-06-19 07:15:05 +080034#ifdef CONFIG_ANDROID_PARANOID_NETWORK
35#include <linux/android_aid.h>
36#endif
37
Serge E. Hallynb5f22a52009-04-02 18:47:14 -050038/*
39 * If a non-root user executes a setuid-root binary in
40 * !secure(SECURE_NOROOT) mode, then we raise capabilities.
41 * However if fE is also set, then the intent is for only
42 * the file capabilities to be applied, and the setuid-root
43 * bit is left on either to change the uid (plausible) or
44 * to get full privilege on a kernel without file capabilities
45 * support. So in that case we do not raise capabilities.
46 *
47 * Warn if that happens, once per boot.
48 */
David Howellsd7627462010-08-17 23:52:56 +010049static void warn_setuid_and_fcaps_mixed(const char *fname)
Serge E. Hallynb5f22a52009-04-02 18:47:14 -050050{
51 static int warned;
52 if (!warned) {
53 printk(KERN_INFO "warning: `%s' has both setuid-root and"
54 " effective capabilities. Therefore not raising all"
55 " capabilities.\n", fname);
56 warned = 1;
57 }
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
61{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63}
64
David Howells1d045982008-11-14 10:39:24 +110065/**
66 * cap_capable - Determine whether a task has a particular effective capability
David Howells3699c532009-01-06 22:27:01 +000067 * @cred: The credentials to use
Serge E. Hallyn34867402011-03-23 16:43:17 -070068 * @ns: The user namespace in which we need the capability
David Howells1d045982008-11-14 10:39:24 +110069 * @cap: The capability to check for
70 * @audit: Whether to write an audit message or not
71 *
72 * Determine whether the nominated task has the specified capability amongst
73 * its effective set, returning 0 if it does, -ve if it does not.
74 *
David Howells3699c532009-01-06 22:27:01 +000075 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
76 * and has_capability() functions. That is, it has the reverse semantics:
77 * cap_has_capability() returns 0 when a task has a capability, but the
78 * kernel's capable() and has_capability() returns 1 for this case.
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -080079 */
Eric Paris6a9de492012-01-03 12:25:14 -050080int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
81 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Tushar Beherae9a85c72012-03-26 16:54:15 +053083#ifdef CONFIG_ANDROID_PARANOID_NETWORK
James Sullinsa71feb22013-11-21 10:01:19 -060084 if (cap == CAP_NET_RAW &&
85 (in_egroup_p(AID_NET_RAW) || in_egroup_p(AID_NET_BT))
86 )
Chia-chi Yeh04320132009-06-19 07:15:05 +080087 return 0;
James Sullinsa71feb22013-11-21 10:01:19 -060088 if (cap == CAP_NET_ADMIN &&
89 (in_egroup_p(AID_NET_ADMIN) || in_egroup_p(AID_NET_BT_ADMIN))
90 )
Chia-chi Yeh04320132009-06-19 07:15:05 +080091 return 0;
Tushar Beherae9a85c72012-03-26 16:54:15 +053092#endif
Chia-chi Yeh04320132009-06-19 07:15:05 +080093
Serge E. Hallyn34867402011-03-23 16:43:17 -070094 for (;;) {
95 /* The creator of the user namespace has all caps. */
96 if (targ_ns != &init_user_ns && targ_ns->creator == cred->user)
97 return 0;
98
99 /* Do we have the necessary capabilities? */
100 if (targ_ns == cred->user->user_ns)
101 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
102
103 /* Have we tried all of the parent namespaces? */
104 if (targ_ns == &init_user_ns)
105 return -EPERM;
106
107 /*
108 *If you have a capability in a parent user ns, then you have
109 * it over all children user namespaces as well.
110 */
111 targ_ns = targ_ns->creator->user_ns;
112 }
113
114 /* We never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
David Howells1d045982008-11-14 10:39:24 +1100117/**
118 * cap_settime - Determine whether the current process may set the system clock
119 * @ts: The time to set
120 * @tz: The timezone to set
121 *
122 * Determine whether the current process may set the system clock and timezone
123 * information, returning 0 if permission granted, -ve if denied.
124 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000125int cap_settime(const struct timespec *ts, const struct timezone *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 if (!capable(CAP_SYS_TIME))
128 return -EPERM;
129 return 0;
130}
131
David Howells1d045982008-11-14 10:39:24 +1100132/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000133 * cap_ptrace_access_check - Determine whether the current process may access
David Howells1d045982008-11-14 10:39:24 +1100134 * another
135 * @child: The process to be accessed
136 * @mode: The mode of attachment.
137 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700138 * If we are in the same or an ancestor user_ns and have all the target
139 * task's capabilities, then ptrace access is allowed.
140 * If we have the ptrace capability to the target user_ns, then ptrace
141 * access is allowed.
142 * Else denied.
143 *
David Howells1d045982008-11-14 10:39:24 +1100144 * Determine whether a process may access another, returning 0 if permission
145 * granted, -ve if denied.
146 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000147int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
David Howellsc69e8d92008-11-14 10:39:19 +1100149 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700150 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100151
152 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700153 cred = current_cred();
154 child_cred = __task_cred(child);
155 if (cred->user->user_ns == child_cred->user->user_ns &&
156 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
157 goto out;
158 if (ns_capable(child_cred->user->user_ns, CAP_SYS_PTRACE))
159 goto out;
160 ret = -EPERM;
161out:
David Howellsc69e8d92008-11-14 10:39:19 +1100162 rcu_read_unlock();
163 return ret;
David Howells5cd9c582008-08-14 11:37:28 +0100164}
165
David Howells1d045982008-11-14 10:39:24 +1100166/**
167 * cap_ptrace_traceme - Determine whether another process may trace the current
168 * @parent: The task proposed to be the tracer
169 *
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700170 * If parent is in the same or an ancestor user_ns and has all current's
171 * capabilities, then ptrace access is allowed.
172 * If parent has the ptrace capability to current's user_ns, then ptrace
173 * access is allowed.
174 * Else denied.
175 *
David Howells1d045982008-11-14 10:39:24 +1100176 * Determine whether the nominated task is permitted to trace the current
177 * process, returning 0 if permission is granted, -ve if denied.
178 */
David Howells5cd9c582008-08-14 11:37:28 +0100179int cap_ptrace_traceme(struct task_struct *parent)
180{
David Howellsc69e8d92008-11-14 10:39:19 +1100181 int ret = 0;
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700182 const struct cred *cred, *child_cred;
David Howellsc69e8d92008-11-14 10:39:19 +1100183
184 rcu_read_lock();
Serge E. Hallyn8409cca2011-03-23 16:43:20 -0700185 cred = __task_cred(parent);
186 child_cred = current_cred();
187 if (cred->user->user_ns == child_cred->user->user_ns &&
188 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
189 goto out;
190 if (has_ns_capability(parent, child_cred->user->user_ns, CAP_SYS_PTRACE))
191 goto out;
192 ret = -EPERM;
193out:
David Howellsc69e8d92008-11-14 10:39:19 +1100194 rcu_read_unlock();
195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
David Howells1d045982008-11-14 10:39:24 +1100198/**
199 * cap_capget - Retrieve a task's capability sets
200 * @target: The task from which to retrieve the capability sets
201 * @effective: The place to record the effective set
202 * @inheritable: The place to record the inheritable set
203 * @permitted: The place to record the permitted set
204 *
205 * This function retrieves the capabilities of the nominated task and returns
206 * them to the caller.
207 */
208int cap_capget(struct task_struct *target, kernel_cap_t *effective,
209 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
David Howellsc69e8d92008-11-14 10:39:19 +1100211 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100214 rcu_read_lock();
215 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100216 *effective = cred->cap_effective;
217 *inheritable = cred->cap_inheritable;
218 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221}
222
David Howells1d045982008-11-14 10:39:24 +1100223/*
224 * Determine whether the inheritable capabilities are limited to the old
225 * permitted set. Returns 1 if they are limited, 0 if they are not.
226 */
Andrew Morgan72c2d582007-10-18 03:05:59 -0700227static inline int cap_inh_is_capped(void)
228{
David Howells1d045982008-11-14 10:39:24 +1100229
230 /* they are so limited unless the current task has the CAP_SETPCAP
231 * capability
Andrew Morgan72c2d582007-10-18 03:05:59 -0700232 */
Eric Paris6a9de492012-01-03 12:25:14 -0500233 if (cap_capable(current_cred(), current_cred()->user->user_ns,
234 CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
David Howells1d045982008-11-14 10:39:24 +1100235 return 0;
David Howells1d045982008-11-14 10:39:24 +1100236 return 1;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700237}
238
David Howells1d045982008-11-14 10:39:24 +1100239/**
240 * cap_capset - Validate and apply proposed changes to current's capabilities
241 * @new: The proposed new credentials; alterations should be made here
242 * @old: The current task's current credentials
243 * @effective: A pointer to the proposed new effective capabilities set
244 * @inheritable: A pointer to the proposed new inheritable capabilities set
245 * @permitted: A pointer to the proposed new permitted capabilities set
246 *
247 * This function validates and applies a proposed mass change to the current
248 * process's capability sets. The changes are made to the proposed new
249 * credentials, and assuming no error, will be committed by the caller of LSM.
250 */
David Howellsd84f4f92008-11-14 10:39:23 +1100251int cap_capset(struct cred *new,
252 const struct cred *old,
253 const kernel_cap_t *effective,
254 const kernel_cap_t *inheritable,
255 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
David Howellsd84f4f92008-11-14 10:39:23 +1100257 if (cap_inh_is_capped() &&
258 !cap_issubset(*inheritable,
259 cap_combine(old->cap_inheritable,
260 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700261 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100263
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800264 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100265 cap_combine(old->cap_inheritable,
266 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800267 /* no new pI capabilities outside bounding set */
268 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100271 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100275 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Howellsd84f4f92008-11-14 10:39:23 +1100278 new->cap_effective = *effective;
279 new->cap_inheritable = *inheritable;
280 new->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282}
283
David Howells1d045982008-11-14 10:39:24 +1100284/*
285 * Clear proposed capability sets for execve().
286 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700287static inline void bprm_clear_caps(struct linux_binprm *bprm)
288{
David Howellsa6f76f22008-11-14 10:39:24 +1100289 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700290 bprm->cap_effective = false;
291}
292
David Howells1d045982008-11-14 10:39:24 +1100293/**
294 * cap_inode_need_killpriv - Determine if inode change affects privileges
295 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
296 *
297 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
298 * affects the security markings on that inode, and if it is, should
299 * inode_killpriv() be invoked or the change rejected?
300 *
301 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
302 * -ve to deny the change.
303 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700304int cap_inode_need_killpriv(struct dentry *dentry)
305{
306 struct inode *inode = dentry->d_inode;
307 int error;
308
Al Viroacfa4382008-12-04 10:06:33 -0500309 if (!inode->i_op->getxattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700310 return 0;
311
312 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
313 if (error <= 0)
314 return 0;
315 return 1;
316}
317
David Howells1d045982008-11-14 10:39:24 +1100318/**
319 * cap_inode_killpriv - Erase the security markings on an inode
320 * @dentry: The inode/dentry to alter
321 *
322 * Erase the privilege-enhancing security markings on an inode.
323 *
324 * Returns 0 if successful, -ve on error.
325 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700326int cap_inode_killpriv(struct dentry *dentry)
327{
328 struct inode *inode = dentry->d_inode;
329
Al Viroacfa4382008-12-04 10:06:33 -0500330 if (!inode->i_op->removexattr)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700331 return 0;
332
333 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
334}
335
David Howells1d045982008-11-14 10:39:24 +1100336/*
337 * Calculate the new process capability sets from the capability sets attached
338 * to a file.
339 */
Eric Parisc0b00442008-11-11 21:48:10 +1100340static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100341 struct linux_binprm *bprm,
Zhi Li4d49f672011-08-11 13:27:50 +0800342 bool *effective,
343 bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700344{
David Howellsa6f76f22008-11-14 10:39:24 +1100345 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100346 unsigned i;
347 int ret = 0;
348
349 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100350 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100351
Zhi Li4d49f672011-08-11 13:27:50 +0800352 if (caps->magic_etc & VFS_CAP_REVISION_MASK)
353 *has_cap = true;
354
Eric Parisc0b00442008-11-11 21:48:10 +1100355 CAP_FOR_EACH_U32(i) {
356 __u32 permitted = caps->permitted.cap[i];
357 __u32 inheritable = caps->inheritable.cap[i];
358
359 /*
360 * pP' = (X & fP) | (pI & fI)
361 */
David Howellsa6f76f22008-11-14 10:39:24 +1100362 new->cap_permitted.cap[i] =
363 (new->cap_bset.cap[i] & permitted) |
364 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100365
David Howellsa6f76f22008-11-14 10:39:24 +1100366 if (permitted & ~new->cap_permitted.cap[i])
367 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100368 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100369 }
370
371 /*
372 * For legacy apps, with no internal support for recognizing they
373 * do not have enough capabilities, we return an error if they are
374 * missing some "forced" (aka file-permitted) capabilities.
375 */
David Howellsa6f76f22008-11-14 10:39:24 +1100376 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100377}
378
David Howells1d045982008-11-14 10:39:24 +1100379/*
380 * Extract the on-exec-apply capability sets for an executable file.
381 */
Eric Parisc0b00442008-11-11 21:48:10 +1100382int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
383{
384 struct inode *inode = dentry->d_inode;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700385 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800386 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100387 int size;
388 struct vfs_cap_data caps;
389
390 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
391
Al Viroacfa4382008-12-04 10:06:33 -0500392 if (!inode || !inode->i_op->getxattr)
Eric Parisc0b00442008-11-11 21:48:10 +1100393 return -ENODATA;
394
395 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
396 XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100397 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100398 /* no data, that's ok */
399 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100400 if (size < 0)
401 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700402
Andrew Morgane338d262008-02-04 22:29:42 -0800403 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700404 return -EINVAL;
405
Eric Parisc0b00442008-11-11 21:48:10 +1100406 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700407
David Howellsa6f76f22008-11-14 10:39:24 +1100408 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800409 case VFS_CAP_REVISION_1:
410 if (size != XATTR_CAPS_SZ_1)
411 return -EINVAL;
412 tocopy = VFS_CAP_U32_1;
413 break;
414 case VFS_CAP_REVISION_2:
415 if (size != XATTR_CAPS_SZ_2)
416 return -EINVAL;
417 tocopy = VFS_CAP_U32_2;
418 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700419 default:
420 return -EINVAL;
421 }
Andrew Morgane338d262008-02-04 22:29:42 -0800422
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700423 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100424 if (i >= tocopy)
425 break;
426 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
427 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800428 }
David Howellsa6f76f22008-11-14 10:39:24 +1100429
Eric Parisc0b00442008-11-11 21:48:10 +1100430 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700431}
432
David Howells1d045982008-11-14 10:39:24 +1100433/*
434 * Attempt to get the on-exec apply capability sets for an executable file from
435 * its xattrs and, if present, apply them to the proposed credentials being
436 * constructed by execve().
437 */
Zhi Li4d49f672011-08-11 13:27:50 +0800438static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700439{
440 struct dentry *dentry;
441 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100442 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700443
Serge Hallyn3318a382008-10-30 11:52:23 -0500444 bprm_clear_caps(bprm);
445
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600446 if (!file_caps_enabled)
447 return 0;
448
Serge Hallyn3318a382008-10-30 11:52:23 -0500449 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700450 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700451
452 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700453
Eric Parisc0b00442008-11-11 21:48:10 +1100454 rc = get_vfs_caps_from_disk(dentry, &vcaps);
455 if (rc < 0) {
456 if (rc == -EINVAL)
457 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
458 __func__, rc, bprm->filename);
459 else if (rc == -ENODATA)
460 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700461 goto out;
462 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700463
Zhi Li4d49f672011-08-11 13:27:50 +0800464 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100465 if (rc == -EINVAL)
466 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
467 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700468
469out:
470 dput(dentry);
471 if (rc)
472 bprm_clear_caps(bprm);
473
474 return rc;
475}
476
David Howells1d045982008-11-14 10:39:24 +1100477/**
478 * cap_bprm_set_creds - Set up the proposed credentials for execve().
479 * @bprm: The execution parameters, including the proposed creds
480 *
481 * Set up the proposed credentials for a new execution context being
482 * constructed by execve(). The proposed creds in @bprm->cred is altered,
483 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100484 */
485int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
David Howellsa6f76f22008-11-14 10:39:24 +1100487 const struct cred *old = current_cred();
488 struct cred *new = bprm->cred;
Serge Hallyn7d8db182011-08-15 08:29:50 -0500489 bool effective, has_cap = false;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700490 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Howellsa6f76f22008-11-14 10:39:24 +1100492 effective = false;
Zhi Li4d49f672011-08-11 13:27:50 +0800493 ret = get_file_caps(bprm, &effective, &has_cap);
David Howellsa6f76f22008-11-14 10:39:24 +1100494 if (ret < 0)
495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700497 if (!issecure(SECURE_NOROOT)) {
498 /*
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500499 * If the legacy file capability is set, then don't set privs
500 * for a setuid root binary run by a non-root user. Do set it
501 * for a root user just to cause least surprise to an admin.
502 */
Zhi Li4d49f672011-08-11 13:27:50 +0800503 if (has_cap && new->uid != 0 && new->euid == 0) {
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500504 warn_setuid_and_fcaps_mixed(bprm->filename);
505 goto skip;
506 }
507 /*
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700508 * To support inheritance of root-permissions and suid-root
509 * executables under compatibility mode, we override the
510 * capability sets for the file.
511 *
David Howellsa6f76f22008-11-14 10:39:24 +1100512 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700513 */
David Howellsa6f76f22008-11-14 10:39:24 +1100514 if (new->euid == 0 || new->uid == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700515 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100516 new->cap_permitted = cap_combine(old->cap_bset,
517 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
David Howellsa6f76f22008-11-14 10:39:24 +1100519 if (new->euid == 0)
520 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Serge E. Hallynb5f22a52009-04-02 18:47:14 -0500522skip:
Serge E. Hallynb5376772007-10-16 23:31:36 -0700523
Eric Parisd52fc5d2012-04-17 16:26:54 -0400524 /* if we have fs caps, clear dangerous personality flags */
525 if (!cap_issubset(new->cap_permitted, old->cap_permitted))
526 bprm->per_clear |= PER_CLEAR_ON_SETID;
527
528
David Howellsa6f76f22008-11-14 10:39:24 +1100529 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
Andy Lutomirski9bf75df2012-04-12 16:47:50 -0500530 * credentials unless they have the appropriate permit.
531 *
532 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
David Howellsa6f76f22008-11-14 10:39:24 +1100533 */
534 if ((new->euid != old->uid ||
535 new->egid != old->gid ||
536 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
537 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
538 /* downgrade; they get no more than they had, and maybe less */
Andy Lutomirski9bf75df2012-04-12 16:47:50 -0500539 if (!capable(CAP_SETUID) ||
540 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100541 new->euid = new->uid;
542 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Serge E. Hallynb3a222e2009-11-23 16:21:30 -0600544 new->cap_permitted = cap_intersect(new->cap_permitted,
545 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
David Howellsa6f76f22008-11-14 10:39:24 +1100548 new->suid = new->fsuid = new->euid;
549 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Eric Paris4bf2ea72011-04-01 17:08:28 -0400551 if (effective)
552 new->cap_effective = new->cap_permitted;
553 else
554 cap_clear(new->cap_effective);
David Howellsa6f76f22008-11-14 10:39:24 +1100555 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Eric Paris3fc689e2008-11-11 21:48:18 +1100557 /*
558 * Audit candidate if current->cap_effective is set
559 *
560 * We do not bother to audit if 3 things are true:
561 * 1) cap_effective has all caps
562 * 2) we are root
563 * 3) root is supposed to have all caps (SECURE_NOROOT)
564 * Since this is just a normal root execing a process.
565 *
566 * Number 1 above might fail if you don't have a full bset, but I think
567 * that is interesting information to audit.
568 */
David Howellsd84f4f92008-11-14 10:39:23 +1100569 if (!cap_isclear(new->cap_effective)) {
570 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100571 new->euid != 0 || new->uid != 0 ||
572 issecure(SECURE_NOROOT)) {
573 ret = audit_log_bprm_fcaps(bprm, new, old);
574 if (ret < 0)
575 return ret;
576 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
David Howellsd84f4f92008-11-14 10:39:23 +1100579 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
David Howellsa6f76f22008-11-14 10:39:24 +1100580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
David Howells1d045982008-11-14 10:39:24 +1100583/**
584 * cap_bprm_secureexec - Determine whether a secure execution is required
585 * @bprm: The execution parameters
586 *
587 * Determine whether a secure execution is required, return 1 if it is, and 0
588 * if it is not.
589 *
590 * The credentials have been committed by this point, and so are no longer
591 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100592 */
593int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
David Howellsc69e8d92008-11-14 10:39:19 +1100595 const struct cred *cred = current_cred();
David Howellsb6dff3e2008-11-14 10:39:16 +1100596
597 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700598 if (bprm->cap_effective)
599 return 1;
David Howellsa6f76f22008-11-14 10:39:24 +1100600 if (!cap_isclear(cred->cap_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700601 return 1;
602 }
603
David Howellsb6dff3e2008-11-14 10:39:16 +1100604 return (cred->euid != cred->uid ||
605 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
David Howells1d045982008-11-14 10:39:24 +1100608/**
609 * cap_inode_setxattr - Determine whether an xattr may be altered
610 * @dentry: The inode/dentry being altered
611 * @name: The name of the xattr to be changed
612 * @value: The value that the xattr will be changed to
613 * @size: The size of value
614 * @flags: The replacement flag
615 *
616 * Determine whether an xattr may be altered or set on an inode, returning 0 if
617 * permission is granted, -ve if denied.
618 *
619 * This is used to make sure security xattrs don't get updated or set by those
620 * who aren't privileged to do so.
621 */
David Howells8f0cfa52008-04-29 00:59:41 -0700622int cap_inode_setxattr(struct dentry *dentry, const char *name,
623 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700625 if (!strcmp(name, XATTR_NAME_CAPS)) {
626 if (!capable(CAP_SETFCAP))
627 return -EPERM;
628 return 0;
David Howells1d045982008-11-14 10:39:24 +1100629 }
630
631 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700632 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 !capable(CAP_SYS_ADMIN))
634 return -EPERM;
635 return 0;
636}
637
David Howells1d045982008-11-14 10:39:24 +1100638/**
639 * cap_inode_removexattr - Determine whether an xattr may be removed
640 * @dentry: The inode/dentry being altered
641 * @name: The name of the xattr to be changed
642 *
643 * Determine whether an xattr may be removed from an inode, returning 0 if
644 * permission is granted, -ve if denied.
645 *
646 * This is used to make sure security xattrs don't get removed by those who
647 * aren't privileged to remove them.
648 */
David Howells8f0cfa52008-04-29 00:59:41 -0700649int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700651 if (!strcmp(name, XATTR_NAME_CAPS)) {
652 if (!capable(CAP_SETFCAP))
653 return -EPERM;
654 return 0;
David Howells1d045982008-11-14 10:39:24 +1100655 }
656
657 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700658 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 !capable(CAP_SYS_ADMIN))
660 return -EPERM;
661 return 0;
662}
663
David Howellsa6f76f22008-11-14 10:39:24 +1100664/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
666 * a process after a call to setuid, setreuid, or setresuid.
667 *
668 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
669 * {r,e,s}uid != 0, the permitted and effective capabilities are
670 * cleared.
671 *
672 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
673 * capabilities of the process are cleared.
674 *
675 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
676 * capabilities are set to the permitted capabilities.
677 *
David Howellsa6f76f22008-11-14 10:39:24 +1100678 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * never happen.
680 *
David Howellsa6f76f22008-11-14 10:39:24 +1100681 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
683 * cevans - New behaviour, Oct '99
684 * A process may, via prctl(), elect to keep its capabilities when it
685 * calls setuid() and switches away from uid==0. Both permitted and
686 * effective sets will be retained.
687 * Without this change, it was impossible for a daemon to drop only some
688 * of its privilege. The call to setuid(!=0) would drop all privileges!
689 * Keeping uid 0 is not an option because uid 0 owns too many vital
690 * files..
691 * Thanks to Olaf Kirch and Peter Benie for spotting this.
692 */
David Howellsd84f4f92008-11-14 10:39:23 +1100693static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
David Howellsd84f4f92008-11-14 10:39:23 +1100695 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
696 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700697 !issecure(SECURE_KEEP_CAPS)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100698 cap_clear(new->cap_permitted);
699 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
David Howellsd84f4f92008-11-14 10:39:23 +1100701 if (old->euid == 0 && new->euid != 0)
702 cap_clear(new->cap_effective);
703 if (old->euid != 0 && new->euid == 0)
704 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
David Howells1d045982008-11-14 10:39:24 +1100707/**
708 * cap_task_fix_setuid - Fix up the results of setuid() call
709 * @new: The proposed credentials
710 * @old: The current task's current credentials
711 * @flags: Indications of what has changed
712 *
713 * Fix up the results of setuid() call before the credential changes are
714 * actually applied, returning 0 to grant the changes, -ve to deny them.
715 */
David Howellsd84f4f92008-11-14 10:39:23 +1100716int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 switch (flags) {
719 case LSM_SETID_RE:
720 case LSM_SETID_ID:
721 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100722 /* juggle the capabilities to follow [RES]UID changes unless
723 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100724 if (!issecure(SECURE_NO_SETUID_FIXUP))
725 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
David Howells1d045982008-11-14 10:39:24 +1100728 case LSM_SETID_FS:
729 /* juggle the capabilties to follow FSUID changes, unless
730 * otherwise suppressed
731 *
David Howellsd84f4f92008-11-14 10:39:23 +1100732 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
733 * if not, we might be a bit too harsh here.
734 */
735 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
David Howells1d045982008-11-14 10:39:24 +1100736 if (old->fsuid == 0 && new->fsuid != 0)
David Howellsd84f4f92008-11-14 10:39:23 +1100737 new->cap_effective =
738 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100739
740 if (old->fsuid != 0 && new->fsuid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +1100741 new->cap_effective =
742 cap_raise_fs_set(new->cap_effective,
743 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
David Howellsd84f4f92008-11-14 10:39:23 +1100745 break;
David Howells1d045982008-11-14 10:39:24 +1100746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 default:
748 return -EINVAL;
749 }
750
751 return 0;
752}
753
Serge E. Hallynb5376772007-10-16 23:31:36 -0700754/*
755 * Rationale: code calling task_setscheduler, task_setioprio, and
756 * task_setnice, assumes that
757 * . if capable(cap_sys_nice), then those actions should be allowed
758 * . if not capable(cap_sys_nice), but acting on your own processes,
759 * then those actions should be allowed
760 * This is insufficient now since you can call code without suid, but
761 * yet with increased caps.
762 * So we check for increased caps on the target process.
763 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400764static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700765{
David Howellsc69e8d92008-11-14 10:39:19 +1100766 int is_subset;
767
768 rcu_read_lock();
769 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
770 current_cred()->cap_permitted);
771 rcu_read_unlock();
772
773 if (!is_subset && !capable(CAP_SYS_NICE))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700774 return -EPERM;
775 return 0;
776}
777
David Howells1d045982008-11-14 10:39:24 +1100778/**
779 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
780 * @p: The task to affect
David Howells1d045982008-11-14 10:39:24 +1100781 *
782 * Detemine if the requested scheduler policy change is permitted for the
783 * specified task, returning 0 if permission is granted, -ve if denied.
784 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +0900785int cap_task_setscheduler(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700786{
787 return cap_safe_nice(p);
788}
789
David Howells1d045982008-11-14 10:39:24 +1100790/**
791 * cap_task_ioprio - Detemine if I/O priority change is permitted
792 * @p: The task to affect
793 * @ioprio: The I/O priority to set
794 *
795 * Detemine if the requested I/O priority change is permitted for the specified
796 * task, returning 0 if permission is granted, -ve if denied.
797 */
798int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700799{
800 return cap_safe_nice(p);
801}
802
David Howells1d045982008-11-14 10:39:24 +1100803/**
804 * cap_task_ioprio - Detemine if task priority change is permitted
805 * @p: The task to affect
806 * @nice: The nice value to set
807 *
808 * Detemine if the requested task priority change is permitted for the
809 * specified task, returning 0 if permission is granted, -ve if denied.
810 */
811int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700812{
813 return cap_safe_nice(p);
814}
815
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800816/*
David Howells1d045982008-11-14 10:39:24 +1100817 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
818 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800819 */
David Howellsd84f4f92008-11-14 10:39:23 +1100820static long cap_prctl_drop(struct cred *new, unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800821{
822 if (!capable(CAP_SETPCAP))
823 return -EPERM;
824 if (!cap_valid(cap))
825 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100826
827 cap_lower(new->cap_bset, cap);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800828 return 0;
829}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700830
David Howells1d045982008-11-14 10:39:24 +1100831/**
832 * cap_task_prctl - Implement process control functions for this security module
833 * @option: The process control function requested
834 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
835 *
836 * Allow process control functions (sys_prctl()) to alter capabilities; may
837 * also deny access to other functions not otherwise implemented here.
838 *
839 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
840 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
841 * modules will consider performing the function.
842 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700843int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100844 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700845{
David Howellsd84f4f92008-11-14 10:39:23 +1100846 struct cred *new;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700847 long error = 0;
848
David Howellsd84f4f92008-11-14 10:39:23 +1100849 new = prepare_creds();
850 if (!new)
851 return -ENOMEM;
852
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700853 switch (option) {
854 case PR_CAPBSET_READ:
David Howellsd84f4f92008-11-14 10:39:23 +1100855 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700856 if (!cap_valid(arg2))
David Howellsd84f4f92008-11-14 10:39:23 +1100857 goto error;
858 error = !!cap_raised(new->cap_bset, arg2);
859 goto no_change;
860
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700861 case PR_CAPBSET_DROP:
David Howellsd84f4f92008-11-14 10:39:23 +1100862 error = cap_prctl_drop(new, arg2);
863 if (error < 0)
864 goto error;
865 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700866
867 /*
868 * The next four prctl's remain to assist with transitioning a
869 * system from legacy UID=0 based privilege (when filesystem
870 * capabilities are not in use) to a system using filesystem
871 * capabilities only - as the POSIX.1e draft intended.
872 *
873 * Note:
874 *
875 * PR_SET_SECUREBITS =
876 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
877 * | issecure_mask(SECURE_NOROOT)
878 * | issecure_mask(SECURE_NOROOT_LOCKED)
879 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
880 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
881 *
882 * will ensure that the current process and all of its
883 * children will be locked into a pure
884 * capability-based-privilege environment.
885 */
886 case PR_SET_SECUREBITS:
David Howellsd84f4f92008-11-14 10:39:23 +1100887 error = -EPERM;
888 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
889 & (new->securebits ^ arg2)) /*[1]*/
890 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
891 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris6a9de492012-01-03 12:25:14 -0500892 || (cap_capable(current_cred(),
Serge E. Hallyn34867402011-03-23 16:43:17 -0700893 current_cred()->user->user_ns, CAP_SETPCAP,
David Howells3699c532009-01-06 22:27:01 +0000894 SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700895 /*
896 * [1] no changing of bits that are locked
897 * [2] no unlocking of locks
898 * [3] no setting of unsupported bits
899 * [4] doing anything requires privilege (go read about
900 * the "sendmail capabilities bug")
901 */
David Howellsd84f4f92008-11-14 10:39:23 +1100902 )
903 /* cannot change a locked bit */
904 goto error;
905 new->securebits = arg2;
906 goto changed;
907
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700908 case PR_GET_SECUREBITS:
David Howellsd84f4f92008-11-14 10:39:23 +1100909 error = new->securebits;
910 goto no_change;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700911
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700912 case PR_GET_KEEPCAPS:
913 if (issecure(SECURE_KEEP_CAPS))
914 error = 1;
David Howellsd84f4f92008-11-14 10:39:23 +1100915 goto no_change;
916
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700917 case PR_SET_KEEPCAPS:
David Howellsd84f4f92008-11-14 10:39:23 +1100918 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700919 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
David Howellsd84f4f92008-11-14 10:39:23 +1100920 goto error;
921 error = -EPERM;
922 if (issecure(SECURE_KEEP_CAPS_LOCKED))
923 goto error;
924 if (arg2)
925 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700926 else
David Howellsd84f4f92008-11-14 10:39:23 +1100927 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
928 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700929
930 default:
931 /* No functionality available - continue with default */
David Howellsd84f4f92008-11-14 10:39:23 +1100932 error = -ENOSYS;
933 goto error;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700934 }
935
936 /* Functionality provided */
David Howellsd84f4f92008-11-14 10:39:23 +1100937changed:
938 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700939
David Howellsd84f4f92008-11-14 10:39:23 +1100940no_change:
David Howellsd84f4f92008-11-14 10:39:23 +1100941error:
942 abort_creds(new);
943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
David Howells1d045982008-11-14 10:39:24 +1100946/**
David Howells1d045982008-11-14 10:39:24 +1100947 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
948 * @mm: The VM space in which the new mapping is to be made
949 * @pages: The size of the mapping
950 *
951 * Determine whether the allocation of a new virtual mapping by the current
952 * task is permitted, returning 0 if permission is granted, -ve if not.
953 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700954int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 int cap_sys_admin = 0;
957
Eric Paris6a9de492012-01-03 12:25:14 -0500958 if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +0000959 SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700961 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
Eric Paris7c738752009-07-31 12:53:58 -0400963
964/*
965 * cap_file_mmap - check if able to map given addr
966 * @file: unused
967 * @reqprot: unused
968 * @prot: unused
969 * @flags: unused
970 * @addr: address attempting to be mapped
971 * @addr_only: unused
972 *
wzt.wzt@gmail.com6f262d82010-04-19 09:16:17 +0800973 * If the process is attempting to map memory below dac_mmap_min_addr they need
Eric Paris7c738752009-07-31 12:53:58 -0400974 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
975 * capability security module. Returns 0 if this mapping should be allowed
976 * -EPERM if not.
977 */
978int cap_file_mmap(struct file *file, unsigned long reqprot,
979 unsigned long prot, unsigned long flags,
980 unsigned long addr, unsigned long addr_only)
981{
982 int ret = 0;
983
Eric Parisa2551df2009-07-31 12:54:11 -0400984 if (addr < dac_mmap_min_addr) {
Eric Paris6a9de492012-01-03 12:25:14 -0500985 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
Eric Paris7c738752009-07-31 12:53:58 -0400986 SECURITY_CAP_AUDIT);
987 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
988 if (ret == 0)
989 current->flags |= PF_SUPERPRIV;
990 }
991 return ret;
992}