| James Morris | 3e1c251 | 2009-10-20 13:48:33 +0900 | [diff] [blame] | 1 | /* Common capabilities, needed by capability.o. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * | 
|  | 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.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> | 
| Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 11 | #include <linux/audit.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #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. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 26 | #include <linux/mount.h> | 
| Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 27 | #include <linux/sched.h> | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 28 | #include <linux/prctl.h> | 
|  | 29 | #include <linux/securebits.h> | 
| Serge E. Hallyn | 3486740a | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 30 | #include <linux/user_namespace.h> | 
| Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 31 |  | 
| Serge E. Hallyn | b5f22a5 | 2009-04-02 18:47:14 -0500 | [diff] [blame] | 32 | /* | 
|  | 33 | * If a non-root user executes a setuid-root binary in | 
|  | 34 | * !secure(SECURE_NOROOT) mode, then we raise capabilities. | 
|  | 35 | * However if fE is also set, then the intent is for only | 
|  | 36 | * the file capabilities to be applied, and the setuid-root | 
|  | 37 | * bit is left on either to change the uid (plausible) or | 
|  | 38 | * to get full privilege on a kernel without file capabilities | 
|  | 39 | * support.  So in that case we do not raise capabilities. | 
|  | 40 | * | 
|  | 41 | * Warn if that happens, once per boot. | 
|  | 42 | */ | 
| David Howells | d762746 | 2010-08-17 23:52:56 +0100 | [diff] [blame] | 43 | static void warn_setuid_and_fcaps_mixed(const char *fname) | 
| Serge E. Hallyn | b5f22a5 | 2009-04-02 18:47:14 -0500 | [diff] [blame] | 44 | { | 
|  | 45 | static int warned; | 
|  | 46 | if (!warned) { | 
|  | 47 | printk(KERN_INFO "warning: `%s' has both setuid-root and" | 
|  | 48 | " effective capabilities. Therefore not raising all" | 
|  | 49 | " capabilities.\n", fname); | 
|  | 50 | warned = 1; | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int cap_netlink_send(struct sock *sk, struct sk_buff *skb) | 
|  | 55 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return 0; | 
|  | 57 | } | 
|  | 58 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 59 | /** | 
|  | 60 | * cap_capable - Determine whether a task has a particular effective capability | 
| David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 61 | * @cred: The credentials to use | 
| Serge E. Hallyn | 3486740a | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 62 | * @ns:  The user namespace in which we need the capability | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 63 | * @cap: The capability to check for | 
|  | 64 | * @audit: Whether to write an audit message or not | 
|  | 65 | * | 
|  | 66 | * Determine whether the nominated task has the specified capability amongst | 
|  | 67 | * its effective set, returning 0 if it does, -ve if it does not. | 
|  | 68 | * | 
| David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 69 | * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable() | 
|  | 70 | * and has_capability() functions.  That is, it has the reverse semantics: | 
|  | 71 | * cap_has_capability() returns 0 when a task has a capability, but the | 
|  | 72 | * kernel's capable() and has_capability() returns 1 for this case. | 
| Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 73 | */ | 
| Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 74 | int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, | 
|  | 75 | int cap, int audit) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { | 
| Serge E. Hallyn | 3486740a | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 77 | for (;;) { | 
|  | 78 | /* The creator of the user namespace has all caps. */ | 
|  | 79 | if (targ_ns != &init_user_ns && targ_ns->creator == cred->user) | 
|  | 80 | return 0; | 
|  | 81 |  | 
|  | 82 | /* Do we have the necessary capabilities? */ | 
|  | 83 | if (targ_ns == cred->user->user_ns) | 
|  | 84 | return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; | 
|  | 85 |  | 
|  | 86 | /* Have we tried all of the parent namespaces? */ | 
|  | 87 | if (targ_ns == &init_user_ns) | 
|  | 88 | return -EPERM; | 
|  | 89 |  | 
|  | 90 | /* | 
|  | 91 | *If you have a capability in a parent user ns, then you have | 
|  | 92 | * it over all children user namespaces as well. | 
|  | 93 | */ | 
|  | 94 | targ_ns = targ_ns->creator->user_ns; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | /* We never get here */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 100 | /** | 
|  | 101 | * cap_settime - Determine whether the current process may set the system clock | 
|  | 102 | * @ts: The time to set | 
|  | 103 | * @tz: The timezone to set | 
|  | 104 | * | 
|  | 105 | * Determine whether the current process may set the system clock and timezone | 
|  | 106 | * information, returning 0 if permission granted, -ve if denied. | 
|  | 107 | */ | 
| Richard Cochran | 1e6d767 | 2011-02-01 13:50:58 +0000 | [diff] [blame] | 108 | int cap_settime(const struct timespec *ts, const struct timezone *tz) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { | 
|  | 110 | if (!capable(CAP_SYS_TIME)) | 
|  | 111 | return -EPERM; | 
|  | 112 | return 0; | 
|  | 113 | } | 
|  | 114 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 115 | /** | 
| Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 116 | * cap_ptrace_access_check - Determine whether the current process may access | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 117 | *			   another | 
|  | 118 | * @child: The process to be accessed | 
|  | 119 | * @mode: The mode of attachment. | 
|  | 120 | * | 
| Serge E. Hallyn | 8409cca | 2011-03-23 16:43:20 -0700 | [diff] [blame] | 121 | * If we are in the same or an ancestor user_ns and have all the target | 
|  | 122 | * task's capabilities, then ptrace access is allowed. | 
|  | 123 | * If we have the ptrace capability to the target user_ns, then ptrace | 
|  | 124 | * access is allowed. | 
|  | 125 | * Else denied. | 
|  | 126 | * | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 127 | * Determine whether a process may access another, returning 0 if permission | 
|  | 128 | * granted, -ve if denied. | 
|  | 129 | */ | 
| Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 130 | int cap_ptrace_access_check(struct task_struct *child, unsigned int mode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 132 | int ret = 0; | 
| Serge E. Hallyn | 8409cca | 2011-03-23 16:43:20 -0700 | [diff] [blame] | 133 | const struct cred *cred, *child_cred; | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 134 |  | 
|  | 135 | rcu_read_lock(); | 
| Serge E. Hallyn | 8409cca | 2011-03-23 16:43:20 -0700 | [diff] [blame] | 136 | cred = current_cred(); | 
|  | 137 | child_cred = __task_cred(child); | 
|  | 138 | if (cred->user->user_ns == child_cred->user->user_ns && | 
|  | 139 | cap_issubset(child_cred->cap_permitted, cred->cap_permitted)) | 
|  | 140 | goto out; | 
|  | 141 | if (ns_capable(child_cred->user->user_ns, CAP_SYS_PTRACE)) | 
|  | 142 | goto out; | 
|  | 143 | ret = -EPERM; | 
|  | 144 | out: | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 145 | rcu_read_unlock(); | 
|  | 146 | return ret; | 
| David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 149 | /** | 
|  | 150 | * cap_ptrace_traceme - Determine whether another process may trace the current | 
|  | 151 | * @parent: The task proposed to be the tracer | 
|  | 152 | * | 
| Serge E. Hallyn | 8409cca | 2011-03-23 16:43:20 -0700 | [diff] [blame] | 153 | * If parent is in the same or an ancestor user_ns and has all current's | 
|  | 154 | * capabilities, then ptrace access is allowed. | 
|  | 155 | * If parent has the ptrace capability to current's user_ns, then ptrace | 
|  | 156 | * access is allowed. | 
|  | 157 | * Else denied. | 
|  | 158 | * | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 159 | * Determine whether the nominated task is permitted to trace the current | 
|  | 160 | * process, returning 0 if permission is granted, -ve if denied. | 
|  | 161 | */ | 
| David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 162 | int cap_ptrace_traceme(struct task_struct *parent) | 
|  | 163 | { | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 164 | int ret = 0; | 
| Serge E. Hallyn | 8409cca | 2011-03-23 16:43:20 -0700 | [diff] [blame] | 165 | const struct cred *cred, *child_cred; | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 166 |  | 
|  | 167 | rcu_read_lock(); | 
| Serge E. Hallyn | 8409cca | 2011-03-23 16:43:20 -0700 | [diff] [blame] | 168 | cred = __task_cred(parent); | 
|  | 169 | child_cred = current_cred(); | 
|  | 170 | if (cred->user->user_ns == child_cred->user->user_ns && | 
|  | 171 | cap_issubset(child_cred->cap_permitted, cred->cap_permitted)) | 
|  | 172 | goto out; | 
|  | 173 | if (has_ns_capability(parent, child_cred->user->user_ns, CAP_SYS_PTRACE)) | 
|  | 174 | goto out; | 
|  | 175 | ret = -EPERM; | 
|  | 176 | out: | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 177 | rcu_read_unlock(); | 
|  | 178 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 181 | /** | 
|  | 182 | * cap_capget - Retrieve a task's capability sets | 
|  | 183 | * @target: The task from which to retrieve the capability sets | 
|  | 184 | * @effective: The place to record the effective set | 
|  | 185 | * @inheritable: The place to record the inheritable set | 
|  | 186 | * @permitted: The place to record the permitted set | 
|  | 187 | * | 
|  | 188 | * This function retrieves the capabilities of the nominated task and returns | 
|  | 189 | * them to the caller. | 
|  | 190 | */ | 
|  | 191 | int cap_capget(struct task_struct *target, kernel_cap_t *effective, | 
|  | 192 | kernel_cap_t *inheritable, kernel_cap_t *permitted) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 194 | const struct cred *cred; | 
| David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 195 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* Derived from kernel/capability.c:sys_capget. */ | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 197 | rcu_read_lock(); | 
|  | 198 | cred = __task_cred(target); | 
| David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 199 | *effective   = cred->cap_effective; | 
|  | 200 | *inheritable = cred->cap_inheritable; | 
|  | 201 | *permitted   = cred->cap_permitted; | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 202 | rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 206 | /* | 
|  | 207 | * Determine whether the inheritable capabilities are limited to the old | 
|  | 208 | * permitted set.  Returns 1 if they are limited, 0 if they are not. | 
|  | 209 | */ | 
| Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 210 | static inline int cap_inh_is_capped(void) | 
|  | 211 | { | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 212 |  | 
|  | 213 | /* they are so limited unless the current task has the CAP_SETPCAP | 
|  | 214 | * capability | 
| Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 215 | */ | 
| Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 216 | if (cap_capable(current_cred(), current_cred()->user->user_ns, | 
|  | 217 | CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0) | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 218 | return 0; | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 219 | return 1; | 
| Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 222 | /** | 
|  | 223 | * cap_capset - Validate and apply proposed changes to current's capabilities | 
|  | 224 | * @new: The proposed new credentials; alterations should be made here | 
|  | 225 | * @old: The current task's current credentials | 
|  | 226 | * @effective: A pointer to the proposed new effective capabilities set | 
|  | 227 | * @inheritable: A pointer to the proposed new inheritable capabilities set | 
|  | 228 | * @permitted: A pointer to the proposed new permitted capabilities set | 
|  | 229 | * | 
|  | 230 | * This function validates and applies a proposed mass change to the current | 
|  | 231 | * process's capability sets.  The changes are made to the proposed new | 
|  | 232 | * credentials, and assuming no error, will be committed by the caller of LSM. | 
|  | 233 | */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 234 | int cap_capset(struct cred *new, | 
|  | 235 | const struct cred *old, | 
|  | 236 | const kernel_cap_t *effective, | 
|  | 237 | const kernel_cap_t *inheritable, | 
|  | 238 | const kernel_cap_t *permitted) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 240 | if (cap_inh_is_capped() && | 
|  | 241 | !cap_issubset(*inheritable, | 
|  | 242 | cap_combine(old->cap_inheritable, | 
|  | 243 | old->cap_permitted))) | 
| Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 244 | /* incapable of using this inheritable set */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return -EPERM; | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 246 |  | 
| Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 247 | if (!cap_issubset(*inheritable, | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 248 | cap_combine(old->cap_inheritable, | 
|  | 249 | old->cap_bset))) | 
| Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 250 | /* no new pI capabilities outside bounding set */ | 
|  | 251 | return -EPERM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 |  | 
|  | 253 | /* verify restrictions on target's new Permitted set */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 254 | if (!cap_issubset(*permitted, old->cap_permitted)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return -EPERM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 |  | 
|  | 257 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 258 | if (!cap_issubset(*effective, *permitted)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return -EPERM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 |  | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 261 | new->cap_effective   = *effective; | 
|  | 262 | new->cap_inheritable = *inheritable; | 
|  | 263 | new->cap_permitted   = *permitted; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | return 0; | 
|  | 265 | } | 
|  | 266 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 267 | /* | 
|  | 268 | * Clear proposed capability sets for execve(). | 
|  | 269 | */ | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 270 | static inline void bprm_clear_caps(struct linux_binprm *bprm) | 
|  | 271 | { | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 272 | cap_clear(bprm->cred->cap_permitted); | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 273 | bprm->cap_effective = false; | 
|  | 274 | } | 
|  | 275 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 276 | /** | 
|  | 277 | * cap_inode_need_killpriv - Determine if inode change affects privileges | 
|  | 278 | * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV | 
|  | 279 | * | 
|  | 280 | * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV | 
|  | 281 | * affects the security markings on that inode, and if it is, should | 
|  | 282 | * inode_killpriv() be invoked or the change rejected? | 
|  | 283 | * | 
|  | 284 | * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and | 
|  | 285 | * -ve to deny the change. | 
|  | 286 | */ | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 287 | int cap_inode_need_killpriv(struct dentry *dentry) | 
|  | 288 | { | 
|  | 289 | struct inode *inode = dentry->d_inode; | 
|  | 290 | int error; | 
|  | 291 |  | 
| Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 292 | if (!inode->i_op->getxattr) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 293 | return 0; | 
|  | 294 |  | 
|  | 295 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); | 
|  | 296 | if (error <= 0) | 
|  | 297 | return 0; | 
|  | 298 | return 1; | 
|  | 299 | } | 
|  | 300 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 301 | /** | 
|  | 302 | * cap_inode_killpriv - Erase the security markings on an inode | 
|  | 303 | * @dentry: The inode/dentry to alter | 
|  | 304 | * | 
|  | 305 | * Erase the privilege-enhancing security markings on an inode. | 
|  | 306 | * | 
|  | 307 | * Returns 0 if successful, -ve on error. | 
|  | 308 | */ | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 309 | int cap_inode_killpriv(struct dentry *dentry) | 
|  | 310 | { | 
|  | 311 | struct inode *inode = dentry->d_inode; | 
|  | 312 |  | 
| Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 313 | if (!inode->i_op->removexattr) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 314 | return 0; | 
|  | 315 |  | 
|  | 316 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); | 
|  | 317 | } | 
|  | 318 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 319 | /* | 
|  | 320 | * Calculate the new process capability sets from the capability sets attached | 
|  | 321 | * to a file. | 
|  | 322 | */ | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 323 | static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 324 | struct linux_binprm *bprm, | 
| Zhi Li | 4d49f67 | 2011-08-11 13:27:50 +0800 | [diff] [blame] | 325 | bool *effective, | 
|  | 326 | bool *has_cap) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 327 | { | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 328 | struct cred *new = bprm->cred; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 329 | unsigned i; | 
|  | 330 | int ret = 0; | 
|  | 331 |  | 
|  | 332 | if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 333 | *effective = true; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 334 |  | 
| Zhi Li | 4d49f67 | 2011-08-11 13:27:50 +0800 | [diff] [blame] | 335 | if (caps->magic_etc & VFS_CAP_REVISION_MASK) | 
|  | 336 | *has_cap = true; | 
|  | 337 |  | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 338 | CAP_FOR_EACH_U32(i) { | 
|  | 339 | __u32 permitted = caps->permitted.cap[i]; | 
|  | 340 | __u32 inheritable = caps->inheritable.cap[i]; | 
|  | 341 |  | 
|  | 342 | /* | 
|  | 343 | * pP' = (X & fP) | (pI & fI) | 
|  | 344 | */ | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 345 | new->cap_permitted.cap[i] = | 
|  | 346 | (new->cap_bset.cap[i] & permitted) | | 
|  | 347 | (new->cap_inheritable.cap[i] & inheritable); | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 348 |  | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 349 | if (permitted & ~new->cap_permitted.cap[i]) | 
|  | 350 | /* insufficient to execute correctly */ | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 351 | ret = -EPERM; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
|  | 354 | /* | 
|  | 355 | * For legacy apps, with no internal support for recognizing they | 
|  | 356 | * do not have enough capabilities, we return an error if they are | 
|  | 357 | * missing some "forced" (aka file-permitted) capabilities. | 
|  | 358 | */ | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 359 | return *effective ? ret : 0; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 360 | } | 
|  | 361 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 362 | /* | 
|  | 363 | * Extract the on-exec-apply capability sets for an executable file. | 
|  | 364 | */ | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 365 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) | 
|  | 366 | { | 
|  | 367 | struct inode *inode = dentry->d_inode; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 368 | __u32 magic_etc; | 
| Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 369 | unsigned tocopy, i; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 370 | int size; | 
|  | 371 | struct vfs_cap_data caps; | 
|  | 372 |  | 
|  | 373 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); | 
|  | 374 |  | 
| Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 375 | if (!inode || !inode->i_op->getxattr) | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 376 | return -ENODATA; | 
|  | 377 |  | 
|  | 378 | size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, | 
|  | 379 | XATTR_CAPS_SZ); | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 380 | if (size == -ENODATA || size == -EOPNOTSUPP) | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 381 | /* no data, that's ok */ | 
|  | 382 | return -ENODATA; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 383 | if (size < 0) | 
|  | 384 | return size; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 385 |  | 
| Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 386 | if (size < sizeof(magic_etc)) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 387 | return -EINVAL; | 
|  | 388 |  | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 389 | cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc); | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 390 |  | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 391 | switch (magic_etc & VFS_CAP_REVISION_MASK) { | 
| Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 392 | case VFS_CAP_REVISION_1: | 
|  | 393 | if (size != XATTR_CAPS_SZ_1) | 
|  | 394 | return -EINVAL; | 
|  | 395 | tocopy = VFS_CAP_U32_1; | 
|  | 396 | break; | 
|  | 397 | case VFS_CAP_REVISION_2: | 
|  | 398 | if (size != XATTR_CAPS_SZ_2) | 
|  | 399 | return -EINVAL; | 
|  | 400 | tocopy = VFS_CAP_U32_2; | 
|  | 401 | break; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 402 | default: | 
|  | 403 | return -EINVAL; | 
|  | 404 | } | 
| Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 405 |  | 
| Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 406 | CAP_FOR_EACH_U32(i) { | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 407 | if (i >= tocopy) | 
|  | 408 | break; | 
|  | 409 | cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); | 
|  | 410 | cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); | 
| Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 411 | } | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 412 |  | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 413 | return 0; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 414 | } | 
|  | 415 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 416 | /* | 
|  | 417 | * Attempt to get the on-exec apply capability sets for an executable file from | 
|  | 418 | * its xattrs and, if present, apply them to the proposed credentials being | 
|  | 419 | * constructed by execve(). | 
|  | 420 | */ | 
| Zhi Li | 4d49f67 | 2011-08-11 13:27:50 +0800 | [diff] [blame] | 421 | static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 422 | { | 
|  | 423 | struct dentry *dentry; | 
|  | 424 | int rc = 0; | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 425 | struct cpu_vfs_cap_data vcaps; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 426 |  | 
| Serge Hallyn | 3318a38 | 2008-10-30 11:52:23 -0500 | [diff] [blame] | 427 | bprm_clear_caps(bprm); | 
|  | 428 |  | 
| Serge E. Hallyn | 1f29fae | 2008-11-05 16:08:52 -0600 | [diff] [blame] | 429 | if (!file_caps_enabled) | 
|  | 430 | return 0; | 
|  | 431 |  | 
| Serge Hallyn | 3318a38 | 2008-10-30 11:52:23 -0500 | [diff] [blame] | 432 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 433 | return 0; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 434 |  | 
|  | 435 | dentry = dget(bprm->file->f_dentry); | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 436 |  | 
| Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 437 | rc = get_vfs_caps_from_disk(dentry, &vcaps); | 
|  | 438 | if (rc < 0) { | 
|  | 439 | if (rc == -EINVAL) | 
|  | 440 | printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n", | 
|  | 441 | __func__, rc, bprm->filename); | 
|  | 442 | else if (rc == -ENODATA) | 
|  | 443 | rc = 0; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 444 | goto out; | 
|  | 445 | } | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 446 |  | 
| Zhi Li | 4d49f67 | 2011-08-11 13:27:50 +0800 | [diff] [blame] | 447 | rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap); | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 448 | if (rc == -EINVAL) | 
|  | 449 | printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n", | 
|  | 450 | __func__, rc, bprm->filename); | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 451 |  | 
|  | 452 | out: | 
|  | 453 | dput(dentry); | 
|  | 454 | if (rc) | 
|  | 455 | bprm_clear_caps(bprm); | 
|  | 456 |  | 
|  | 457 | return rc; | 
|  | 458 | } | 
|  | 459 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 460 | /** | 
|  | 461 | * cap_bprm_set_creds - Set up the proposed credentials for execve(). | 
|  | 462 | * @bprm: The execution parameters, including the proposed creds | 
|  | 463 | * | 
|  | 464 | * Set up the proposed credentials for a new execution context being | 
|  | 465 | * constructed by execve().  The proposed creds in @bprm->cred is altered, | 
|  | 466 | * which won't take effect immediately.  Returns 0 if successful, -ve on error. | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 467 | */ | 
|  | 468 | int cap_bprm_set_creds(struct linux_binprm *bprm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 470 | const struct cred *old = current_cred(); | 
|  | 471 | struct cred *new = bprm->cred; | 
| Serge Hallyn | 7d8db18 | 2011-08-15 08:29:50 -0500 | [diff] [blame] | 472 | bool effective, has_cap = false; | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 473 | int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 |  | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 475 | effective = false; | 
| Zhi Li | 4d49f67 | 2011-08-11 13:27:50 +0800 | [diff] [blame] | 476 | ret = get_file_caps(bprm, &effective, &has_cap); | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 477 | if (ret < 0) | 
|  | 478 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 |  | 
| Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 480 | if (!issecure(SECURE_NOROOT)) { | 
|  | 481 | /* | 
| Serge E. Hallyn | b5f22a5 | 2009-04-02 18:47:14 -0500 | [diff] [blame] | 482 | * If the legacy file capability is set, then don't set privs | 
|  | 483 | * for a setuid root binary run by a non-root user.  Do set it | 
|  | 484 | * for a root user just to cause least surprise to an admin. | 
|  | 485 | */ | 
| Zhi Li | 4d49f67 | 2011-08-11 13:27:50 +0800 | [diff] [blame] | 486 | if (has_cap && new->uid != 0 && new->euid == 0) { | 
| Serge E. Hallyn | b5f22a5 | 2009-04-02 18:47:14 -0500 | [diff] [blame] | 487 | warn_setuid_and_fcaps_mixed(bprm->filename); | 
|  | 488 | goto skip; | 
|  | 489 | } | 
|  | 490 | /* | 
| Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 491 | * To support inheritance of root-permissions and suid-root | 
|  | 492 | * executables under compatibility mode, we override the | 
|  | 493 | * capability sets for the file. | 
|  | 494 | * | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 495 | * If only the real uid is 0, we do not set the effective bit. | 
| Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 496 | */ | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 497 | if (new->euid == 0 || new->uid == 0) { | 
| Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 498 | /* pP' = (cap_bset & ~0) | (pI & ~0) */ | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 499 | new->cap_permitted = cap_combine(old->cap_bset, | 
|  | 500 | old->cap_inheritable); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 502 | if (new->euid == 0) | 
|  | 503 | effective = true; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } | 
| Serge E. Hallyn | b5f22a5 | 2009-04-02 18:47:14 -0500 | [diff] [blame] | 505 | skip: | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 506 |  | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 507 | /* Don't let someone trace a set[ug]id/setpcap binary with the revised | 
|  | 508 | * credentials unless they have the appropriate permit | 
|  | 509 | */ | 
|  | 510 | if ((new->euid != old->uid || | 
|  | 511 | new->egid != old->gid || | 
|  | 512 | !cap_issubset(new->cap_permitted, old->cap_permitted)) && | 
|  | 513 | bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) { | 
|  | 514 | /* downgrade; they get no more than they had, and maybe less */ | 
|  | 515 | if (!capable(CAP_SETUID)) { | 
|  | 516 | new->euid = new->uid; | 
|  | 517 | new->egid = new->gid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } | 
| Serge E. Hallyn | b3a222e | 2009-11-23 16:21:30 -0600 | [diff] [blame] | 519 | new->cap_permitted = cap_intersect(new->cap_permitted, | 
|  | 520 | old->cap_permitted); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } | 
|  | 522 |  | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 523 | new->suid = new->fsuid = new->euid; | 
|  | 524 | new->sgid = new->fsgid = new->egid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 |  | 
| Eric Paris | 4bf2ea7 | 2011-04-01 17:08:28 -0400 | [diff] [blame] | 526 | if (effective) | 
|  | 527 | new->cap_effective = new->cap_permitted; | 
|  | 528 | else | 
|  | 529 | cap_clear(new->cap_effective); | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 530 | bprm->cap_effective = effective; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 |  | 
| Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 532 | /* | 
|  | 533 | * Audit candidate if current->cap_effective is set | 
|  | 534 | * | 
|  | 535 | * We do not bother to audit if 3 things are true: | 
|  | 536 | *   1) cap_effective has all caps | 
|  | 537 | *   2) we are root | 
|  | 538 | *   3) root is supposed to have all caps (SECURE_NOROOT) | 
|  | 539 | * Since this is just a normal root execing a process. | 
|  | 540 | * | 
|  | 541 | * Number 1 above might fail if you don't have a full bset, but I think | 
|  | 542 | * that is interesting information to audit. | 
|  | 543 | */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 544 | if (!cap_isclear(new->cap_effective)) { | 
|  | 545 | if (!cap_issubset(CAP_FULL_SET, new->cap_effective) || | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 546 | new->euid != 0 || new->uid != 0 || | 
|  | 547 | issecure(SECURE_NOROOT)) { | 
|  | 548 | ret = audit_log_bprm_fcaps(bprm, new, old); | 
|  | 549 | if (ret < 0) | 
|  | 550 | return ret; | 
|  | 551 | } | 
| Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 552 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 |  | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 554 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 555 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } | 
|  | 557 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 558 | /** | 
|  | 559 | * cap_bprm_secureexec - Determine whether a secure execution is required | 
|  | 560 | * @bprm: The execution parameters | 
|  | 561 | * | 
|  | 562 | * Determine whether a secure execution is required, return 1 if it is, and 0 | 
|  | 563 | * if it is not. | 
|  | 564 | * | 
|  | 565 | * The credentials have been committed by this point, and so are no longer | 
|  | 566 | * available through @bprm->cred. | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 567 | */ | 
|  | 568 | int cap_bprm_secureexec(struct linux_binprm *bprm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 570 | const struct cred *cred = current_cred(); | 
| David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 571 |  | 
|  | 572 | if (cred->uid != 0) { | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 573 | if (bprm->cap_effective) | 
|  | 574 | return 1; | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 575 | if (!cap_isclear(cred->cap_permitted)) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 576 | return 1; | 
|  | 577 | } | 
|  | 578 |  | 
| David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 579 | return (cred->euid != cred->uid || | 
|  | 580 | cred->egid != cred->gid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } | 
|  | 582 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 583 | /** | 
|  | 584 | * cap_inode_setxattr - Determine whether an xattr may be altered | 
|  | 585 | * @dentry: The inode/dentry being altered | 
|  | 586 | * @name: The name of the xattr to be changed | 
|  | 587 | * @value: The value that the xattr will be changed to | 
|  | 588 | * @size: The size of value | 
|  | 589 | * @flags: The replacement flag | 
|  | 590 | * | 
|  | 591 | * Determine whether an xattr may be altered or set on an inode, returning 0 if | 
|  | 592 | * permission is granted, -ve if denied. | 
|  | 593 | * | 
|  | 594 | * This is used to make sure security xattrs don't get updated or set by those | 
|  | 595 | * who aren't privileged to do so. | 
|  | 596 | */ | 
| David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 597 | int cap_inode_setxattr(struct dentry *dentry, const char *name, | 
|  | 598 | const void *value, size_t size, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 600 | if (!strcmp(name, XATTR_NAME_CAPS)) { | 
|  | 601 | if (!capable(CAP_SETFCAP)) | 
|  | 602 | return -EPERM; | 
|  | 603 | return 0; | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 604 | } | 
|  | 605 |  | 
|  | 606 | if (!strncmp(name, XATTR_SECURITY_PREFIX, | 
| Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 607 | sizeof(XATTR_SECURITY_PREFIX) - 1) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | !capable(CAP_SYS_ADMIN)) | 
|  | 609 | return -EPERM; | 
|  | 610 | return 0; | 
|  | 611 | } | 
|  | 612 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 613 | /** | 
|  | 614 | * cap_inode_removexattr - Determine whether an xattr may be removed | 
|  | 615 | * @dentry: The inode/dentry being altered | 
|  | 616 | * @name: The name of the xattr to be changed | 
|  | 617 | * | 
|  | 618 | * Determine whether an xattr may be removed from an inode, returning 0 if | 
|  | 619 | * permission is granted, -ve if denied. | 
|  | 620 | * | 
|  | 621 | * This is used to make sure security xattrs don't get removed by those who | 
|  | 622 | * aren't privileged to remove them. | 
|  | 623 | */ | 
| David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 624 | int cap_inode_removexattr(struct dentry *dentry, const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 626 | if (!strcmp(name, XATTR_NAME_CAPS)) { | 
|  | 627 | if (!capable(CAP_SETFCAP)) | 
|  | 628 | return -EPERM; | 
|  | 629 | return 0; | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 630 | } | 
|  | 631 |  | 
|  | 632 | if (!strncmp(name, XATTR_SECURITY_PREFIX, | 
| Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 633 | sizeof(XATTR_SECURITY_PREFIX) - 1) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | !capable(CAP_SYS_ADMIN)) | 
|  | 635 | return -EPERM; | 
|  | 636 | return 0; | 
|  | 637 | } | 
|  | 638 |  | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 639 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | * cap_emulate_setxuid() fixes the effective / permitted capabilities of | 
|  | 641 | * a process after a call to setuid, setreuid, or setresuid. | 
|  | 642 | * | 
|  | 643 | *  1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of | 
|  | 644 | *  {r,e,s}uid != 0, the permitted and effective capabilities are | 
|  | 645 | *  cleared. | 
|  | 646 | * | 
|  | 647 | *  2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective | 
|  | 648 | *  capabilities of the process are cleared. | 
|  | 649 | * | 
|  | 650 | *  3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective | 
|  | 651 | *  capabilities are set to the permitted capabilities. | 
|  | 652 | * | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 653 | *  fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | *  never happen. | 
|  | 655 | * | 
| David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 656 | *  -astor | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | * | 
|  | 658 | * cevans - New behaviour, Oct '99 | 
|  | 659 | * A process may, via prctl(), elect to keep its capabilities when it | 
|  | 660 | * calls setuid() and switches away from uid==0. Both permitted and | 
|  | 661 | * effective sets will be retained. | 
|  | 662 | * Without this change, it was impossible for a daemon to drop only some | 
|  | 663 | * of its privilege. The call to setuid(!=0) would drop all privileges! | 
|  | 664 | * Keeping uid 0 is not an option because uid 0 owns too many vital | 
|  | 665 | * files.. | 
|  | 666 | * Thanks to Olaf Kirch and Peter Benie for spotting this. | 
|  | 667 | */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 668 | static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 670 | if ((old->uid == 0 || old->euid == 0 || old->suid == 0) && | 
|  | 671 | (new->uid != 0 && new->euid != 0 && new->suid != 0) && | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 672 | !issecure(SECURE_KEEP_CAPS)) { | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 673 | cap_clear(new->cap_permitted); | 
|  | 674 | cap_clear(new->cap_effective); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 676 | if (old->euid == 0 && new->euid != 0) | 
|  | 677 | cap_clear(new->cap_effective); | 
|  | 678 | if (old->euid != 0 && new->euid == 0) | 
|  | 679 | new->cap_effective = new->cap_permitted; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } | 
|  | 681 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 682 | /** | 
|  | 683 | * cap_task_fix_setuid - Fix up the results of setuid() call | 
|  | 684 | * @new: The proposed credentials | 
|  | 685 | * @old: The current task's current credentials | 
|  | 686 | * @flags: Indications of what has changed | 
|  | 687 | * | 
|  | 688 | * Fix up the results of setuid() call before the credential changes are | 
|  | 689 | * actually applied, returning 0 to grant the changes, -ve to deny them. | 
|  | 690 | */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 691 | int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { | 
|  | 693 | switch (flags) { | 
|  | 694 | case LSM_SETID_RE: | 
|  | 695 | case LSM_SETID_ID: | 
|  | 696 | case LSM_SETID_RES: | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 697 | /* juggle the capabilities to follow [RES]UID changes unless | 
|  | 698 | * otherwise suppressed */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 699 | if (!issecure(SECURE_NO_SETUID_FIXUP)) | 
|  | 700 | cap_emulate_setxuid(new, old); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 703 | case LSM_SETID_FS: | 
|  | 704 | /* juggle the capabilties to follow FSUID changes, unless | 
|  | 705 | * otherwise suppressed | 
|  | 706 | * | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 707 | * FIXME - is fsuser used for all CAP_FS_MASK capabilities? | 
|  | 708 | *          if not, we might be a bit too harsh here. | 
|  | 709 | */ | 
|  | 710 | if (!issecure(SECURE_NO_SETUID_FIXUP)) { | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 711 | if (old->fsuid == 0 && new->fsuid != 0) | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 712 | new->cap_effective = | 
|  | 713 | cap_drop_fs_set(new->cap_effective); | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 714 |  | 
|  | 715 | if (old->fsuid != 0 && new->fsuid == 0) | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 716 | new->cap_effective = | 
|  | 717 | cap_raise_fs_set(new->cap_effective, | 
|  | 718 | new->cap_permitted); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 720 | break; | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 721 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | default: | 
|  | 723 | return -EINVAL; | 
|  | 724 | } | 
|  | 725 |  | 
|  | 726 | return 0; | 
|  | 727 | } | 
|  | 728 |  | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 729 | /* | 
|  | 730 | * Rationale: code calling task_setscheduler, task_setioprio, and | 
|  | 731 | * task_setnice, assumes that | 
|  | 732 | *   . if capable(cap_sys_nice), then those actions should be allowed | 
|  | 733 | *   . if not capable(cap_sys_nice), but acting on your own processes, | 
|  | 734 | *   	then those actions should be allowed | 
|  | 735 | * This is insufficient now since you can call code without suid, but | 
|  | 736 | * yet with increased caps. | 
|  | 737 | * So we check for increased caps on the target process. | 
|  | 738 | */ | 
| Serge E. Hallyn | de45e80 | 2008-09-26 22:27:47 -0400 | [diff] [blame] | 739 | static int cap_safe_nice(struct task_struct *p) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 740 | { | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 741 | int is_subset; | 
|  | 742 |  | 
|  | 743 | rcu_read_lock(); | 
|  | 744 | is_subset = cap_issubset(__task_cred(p)->cap_permitted, | 
|  | 745 | current_cred()->cap_permitted); | 
|  | 746 | rcu_read_unlock(); | 
|  | 747 |  | 
|  | 748 | if (!is_subset && !capable(CAP_SYS_NICE)) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 749 | return -EPERM; | 
|  | 750 | return 0; | 
|  | 751 | } | 
|  | 752 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 753 | /** | 
|  | 754 | * cap_task_setscheduler - Detemine if scheduler policy change is permitted | 
|  | 755 | * @p: The task to affect | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 756 | * | 
|  | 757 | * Detemine if the requested scheduler policy change is permitted for the | 
|  | 758 | * specified task, returning 0 if permission is granted, -ve if denied. | 
|  | 759 | */ | 
| KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 760 | int cap_task_setscheduler(struct task_struct *p) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 761 | { | 
|  | 762 | return cap_safe_nice(p); | 
|  | 763 | } | 
|  | 764 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 765 | /** | 
|  | 766 | * cap_task_ioprio - Detemine if I/O priority change is permitted | 
|  | 767 | * @p: The task to affect | 
|  | 768 | * @ioprio: The I/O priority to set | 
|  | 769 | * | 
|  | 770 | * Detemine if the requested I/O priority change is permitted for the specified | 
|  | 771 | * task, returning 0 if permission is granted, -ve if denied. | 
|  | 772 | */ | 
|  | 773 | int cap_task_setioprio(struct task_struct *p, int ioprio) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 774 | { | 
|  | 775 | return cap_safe_nice(p); | 
|  | 776 | } | 
|  | 777 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 778 | /** | 
|  | 779 | * cap_task_ioprio - Detemine if task priority change is permitted | 
|  | 780 | * @p: The task to affect | 
|  | 781 | * @nice: The nice value to set | 
|  | 782 | * | 
|  | 783 | * Detemine if the requested task priority change is permitted for the | 
|  | 784 | * specified task, returning 0 if permission is granted, -ve if denied. | 
|  | 785 | */ | 
|  | 786 | int cap_task_setnice(struct task_struct *p, int nice) | 
| Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 787 | { | 
|  | 788 | return cap_safe_nice(p); | 
|  | 789 | } | 
|  | 790 |  | 
| Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 791 | /* | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 792 | * Implement PR_CAPBSET_DROP.  Attempt to remove the specified capability from | 
|  | 793 | * the current task's bounding set.  Returns 0 on success, -ve on error. | 
| Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 794 | */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 795 | static long cap_prctl_drop(struct cred *new, unsigned long cap) | 
| Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 796 | { | 
|  | 797 | if (!capable(CAP_SETPCAP)) | 
|  | 798 | return -EPERM; | 
|  | 799 | if (!cap_valid(cap)) | 
|  | 800 | return -EINVAL; | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 801 |  | 
|  | 802 | cap_lower(new->cap_bset, cap); | 
| Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 803 | return 0; | 
|  | 804 | } | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 805 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 806 | /** | 
|  | 807 | * cap_task_prctl - Implement process control functions for this security module | 
|  | 808 | * @option: The process control function requested | 
|  | 809 | * @arg2, @arg3, @arg4, @arg5: The argument data for this function | 
|  | 810 | * | 
|  | 811 | * Allow process control functions (sys_prctl()) to alter capabilities; may | 
|  | 812 | * also deny access to other functions not otherwise implemented here. | 
|  | 813 | * | 
|  | 814 | * Returns 0 or +ve on success, -ENOSYS if this function is not implemented | 
|  | 815 | * here, other -ve on error.  If -ENOSYS is returned, sys_prctl() and other LSM | 
|  | 816 | * modules will consider performing the function. | 
|  | 817 | */ | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 818 | int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 819 | unsigned long arg4, unsigned long arg5) | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 820 | { | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 821 | struct cred *new; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 822 | long error = 0; | 
|  | 823 |  | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 824 | new = prepare_creds(); | 
|  | 825 | if (!new) | 
|  | 826 | return -ENOMEM; | 
|  | 827 |  | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 828 | switch (option) { | 
|  | 829 | case PR_CAPBSET_READ: | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 830 | error = -EINVAL; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 831 | if (!cap_valid(arg2)) | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 832 | goto error; | 
|  | 833 | error = !!cap_raised(new->cap_bset, arg2); | 
|  | 834 | goto no_change; | 
|  | 835 |  | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 836 | case PR_CAPBSET_DROP: | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 837 | error = cap_prctl_drop(new, arg2); | 
|  | 838 | if (error < 0) | 
|  | 839 | goto error; | 
|  | 840 | goto changed; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 841 |  | 
|  | 842 | /* | 
|  | 843 | * The next four prctl's remain to assist with transitioning a | 
|  | 844 | * system from legacy UID=0 based privilege (when filesystem | 
|  | 845 | * capabilities are not in use) to a system using filesystem | 
|  | 846 | * capabilities only - as the POSIX.1e draft intended. | 
|  | 847 | * | 
|  | 848 | * Note: | 
|  | 849 | * | 
|  | 850 | *  PR_SET_SECUREBITS = | 
|  | 851 | *      issecure_mask(SECURE_KEEP_CAPS_LOCKED) | 
|  | 852 | *    | issecure_mask(SECURE_NOROOT) | 
|  | 853 | *    | issecure_mask(SECURE_NOROOT_LOCKED) | 
|  | 854 | *    | issecure_mask(SECURE_NO_SETUID_FIXUP) | 
|  | 855 | *    | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED) | 
|  | 856 | * | 
|  | 857 | * will ensure that the current process and all of its | 
|  | 858 | * children will be locked into a pure | 
|  | 859 | * capability-based-privilege environment. | 
|  | 860 | */ | 
|  | 861 | case PR_SET_SECUREBITS: | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 862 | error = -EPERM; | 
|  | 863 | if ((((new->securebits & SECURE_ALL_LOCKS) >> 1) | 
|  | 864 | & (new->securebits ^ arg2))			/*[1]*/ | 
|  | 865 | || ((new->securebits & SECURE_ALL_LOCKS & ~arg2))	/*[2]*/ | 
|  | 866 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS))	/*[3]*/ | 
| Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 867 | || (cap_capable(current_cred(), | 
| Serge E. Hallyn | 3486740a | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 868 | current_cred()->user->user_ns, CAP_SETPCAP, | 
| David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 869 | SECURITY_CAP_AUDIT) != 0)		/*[4]*/ | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 870 | /* | 
|  | 871 | * [1] no changing of bits that are locked | 
|  | 872 | * [2] no unlocking of locks | 
|  | 873 | * [3] no setting of unsupported bits | 
|  | 874 | * [4] doing anything requires privilege (go read about | 
|  | 875 | *     the "sendmail capabilities bug") | 
|  | 876 | */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 877 | ) | 
|  | 878 | /* cannot change a locked bit */ | 
|  | 879 | goto error; | 
|  | 880 | new->securebits = arg2; | 
|  | 881 | goto changed; | 
|  | 882 |  | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 883 | case PR_GET_SECUREBITS: | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 884 | error = new->securebits; | 
|  | 885 | goto no_change; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 886 |  | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 887 | case PR_GET_KEEPCAPS: | 
|  | 888 | if (issecure(SECURE_KEEP_CAPS)) | 
|  | 889 | error = 1; | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 890 | goto no_change; | 
|  | 891 |  | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 892 | case PR_SET_KEEPCAPS: | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 893 | error = -EINVAL; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 894 | if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 895 | goto error; | 
|  | 896 | error = -EPERM; | 
|  | 897 | if (issecure(SECURE_KEEP_CAPS_LOCKED)) | 
|  | 898 | goto error; | 
|  | 899 | if (arg2) | 
|  | 900 | new->securebits |= issecure_mask(SECURE_KEEP_CAPS); | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 901 | else | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 902 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); | 
|  | 903 | goto changed; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 904 |  | 
|  | 905 | default: | 
|  | 906 | /* No functionality available - continue with default */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 907 | error = -ENOSYS; | 
|  | 908 | goto error; | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 909 | } | 
|  | 910 |  | 
|  | 911 | /* Functionality provided */ | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 912 | changed: | 
|  | 913 | return commit_creds(new); | 
| Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 914 |  | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 915 | no_change: | 
| David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 916 | error: | 
|  | 917 | abort_creds(new); | 
|  | 918 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } | 
|  | 920 |  | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 921 | /** | 
| David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 922 | * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted | 
|  | 923 | * @mm: The VM space in which the new mapping is to be made | 
|  | 924 | * @pages: The size of the mapping | 
|  | 925 | * | 
|  | 926 | * Determine whether the allocation of a new virtual mapping by the current | 
|  | 927 | * task is permitted, returning 0 if permission is granted, -ve if not. | 
|  | 928 | */ | 
| Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 929 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { | 
|  | 931 | int cap_sys_admin = 0; | 
|  | 932 |  | 
| Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 933 | if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, | 
| David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 934 | SECURITY_CAP_NOAUDIT) == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | cap_sys_admin = 1; | 
| Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 936 | return __vm_enough_memory(mm, pages, cap_sys_admin); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } | 
| Eric Paris | 7c73875 | 2009-07-31 12:53:58 -0400 | [diff] [blame] | 938 |  | 
|  | 939 | /* | 
|  | 940 | * cap_file_mmap - check if able to map given addr | 
|  | 941 | * @file: unused | 
|  | 942 | * @reqprot: unused | 
|  | 943 | * @prot: unused | 
|  | 944 | * @flags: unused | 
|  | 945 | * @addr: address attempting to be mapped | 
|  | 946 | * @addr_only: unused | 
|  | 947 | * | 
| wzt.wzt@gmail.com | 6f262d8 | 2010-04-19 09:16:17 +0800 | [diff] [blame] | 948 | * If the process is attempting to map memory below dac_mmap_min_addr they need | 
| Eric Paris | 7c73875 | 2009-07-31 12:53:58 -0400 | [diff] [blame] | 949 | * CAP_SYS_RAWIO.  The other parameters to this function are unused by the | 
|  | 950 | * capability security module.  Returns 0 if this mapping should be allowed | 
|  | 951 | * -EPERM if not. | 
|  | 952 | */ | 
|  | 953 | int cap_file_mmap(struct file *file, unsigned long reqprot, | 
|  | 954 | unsigned long prot, unsigned long flags, | 
|  | 955 | unsigned long addr, unsigned long addr_only) | 
|  | 956 | { | 
|  | 957 | int ret = 0; | 
|  | 958 |  | 
| Eric Paris | a2551df | 2009-07-31 12:54:11 -0400 | [diff] [blame] | 959 | if (addr < dac_mmap_min_addr) { | 
| Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 960 | ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO, | 
| Eric Paris | 7c73875 | 2009-07-31 12:53:58 -0400 | [diff] [blame] | 961 | SECURITY_CAP_AUDIT); | 
|  | 962 | /* set PF_SUPERPRIV if it turns out we allow the low mmap */ | 
|  | 963 | if (ret == 0) | 
|  | 964 | current->flags |= PF_SUPERPRIV; | 
|  | 965 | } | 
|  | 966 | return ret; | 
|  | 967 | } |