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