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 | /** |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 46 | * cap_capable - Determine whether current has a particular effective capability |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 47 | * @cap: The capability to check for |
| 48 | * @audit: Whether to write an audit message or not |
| 49 | * |
| 50 | * Determine whether the nominated task has the specified capability amongst |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 51 | * its effective set, returning 0 if it does, -ve if it does not. Note that |
| 52 | * this uses current's subjective/effective credentials. |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 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 | */ |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 59 | int cap_capable(int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 61 | return cap_raised(current_cap(), cap) ? 0 : -EPERM; |
| 62 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 63 | |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 64 | /** |
| 65 | * cap_has_capability - Determine whether a task has a particular effective capability |
| 66 | * @tsk: The task to query |
| 67 | * @cred: The credentials to use |
| 68 | * @cap: The capability to check for |
| 69 | * @audit: Whether to write an audit message or not |
| 70 | * |
| 71 | * Determine whether the nominated task has the specified capability amongst |
| 72 | * its effective set, returning 0 if it does, -ve if it does not. Note that |
| 73 | * this uses the task's objective/real credentials. |
| 74 | * |
| 75 | * NOTE WELL: cap_has_capability() cannot be used like the kernel's |
| 76 | * has_capability() function. That is, it has the reverse semantics: |
| 77 | * cap_has_capability() returns 0 when a task has a capability, but the |
| 78 | * kernel's has_capability() returns 1 for this case. |
| 79 | */ |
| 80 | int cap_task_capable(struct task_struct *tsk, const struct cred *cred, int cap, |
| 81 | int audit) |
| 82 | { |
| 83 | return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 86 | /** |
| 87 | * cap_settime - Determine whether the current process may set the system clock |
| 88 | * @ts: The time to set |
| 89 | * @tz: The timezone to set |
| 90 | * |
| 91 | * Determine whether the current process may set the system clock and timezone |
| 92 | * information, returning 0 if permission granted, -ve if denied. |
| 93 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | int cap_settime(struct timespec *ts, struct timezone *tz) |
| 95 | { |
| 96 | if (!capable(CAP_SYS_TIME)) |
| 97 | return -EPERM; |
| 98 | return 0; |
| 99 | } |
| 100 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 101 | /** |
| 102 | * cap_ptrace_may_access - Determine whether the current process may access |
| 103 | * another |
| 104 | * @child: The process to be accessed |
| 105 | * @mode: The mode of attachment. |
| 106 | * |
| 107 | * Determine whether a process may access another, returning 0 if permission |
| 108 | * granted, -ve if denied. |
| 109 | */ |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 110 | int cap_ptrace_may_access(struct task_struct *child, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 112 | int ret = 0; |
| 113 | |
| 114 | rcu_read_lock(); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 115 | if (!cap_issubset(__task_cred(child)->cap_permitted, |
| 116 | current_cred()->cap_permitted) && |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 117 | !capable(CAP_SYS_PTRACE)) |
| 118 | ret = -EPERM; |
| 119 | rcu_read_unlock(); |
| 120 | return ret; |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 121 | } |
| 122 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 123 | /** |
| 124 | * cap_ptrace_traceme - Determine whether another process may trace the current |
| 125 | * @parent: The task proposed to be the tracer |
| 126 | * |
| 127 | * Determine whether the nominated task is permitted to trace the current |
| 128 | * process, returning 0 if permission is granted, -ve if denied. |
| 129 | */ |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 130 | int cap_ptrace_traceme(struct task_struct *parent) |
| 131 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 132 | int ret = 0; |
| 133 | |
| 134 | rcu_read_lock(); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 135 | if (!cap_issubset(current_cred()->cap_permitted, |
| 136 | __task_cred(parent)->cap_permitted) && |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 137 | !has_capability(parent, CAP_SYS_PTRACE)) |
| 138 | ret = -EPERM; |
| 139 | rcu_read_unlock(); |
| 140 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 143 | /** |
| 144 | * cap_capget - Retrieve a task's capability sets |
| 145 | * @target: The task from which to retrieve the capability sets |
| 146 | * @effective: The place to record the effective set |
| 147 | * @inheritable: The place to record the inheritable set |
| 148 | * @permitted: The place to record the permitted set |
| 149 | * |
| 150 | * This function retrieves the capabilities of the nominated task and returns |
| 151 | * them to the caller. |
| 152 | */ |
| 153 | int cap_capget(struct task_struct *target, kernel_cap_t *effective, |
| 154 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 156 | const struct cred *cred; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* Derived from kernel/capability.c:sys_capget. */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 159 | rcu_read_lock(); |
| 160 | cred = __task_cred(target); |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 161 | *effective = cred->cap_effective; |
| 162 | *inheritable = cred->cap_inheritable; |
| 163 | *permitted = cred->cap_permitted; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 164 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 168 | /* |
| 169 | * Determine whether the inheritable capabilities are limited to the old |
| 170 | * permitted set. Returns 1 if they are limited, 0 if they are not. |
| 171 | */ |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 172 | static inline int cap_inh_is_capped(void) |
| 173 | { |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 174 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 175 | |
| 176 | /* they are so limited unless the current task has the CAP_SETPCAP |
| 177 | * capability |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 178 | */ |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 179 | if (cap_capable(CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0) |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 180 | return 0; |
| 181 | #endif |
| 182 | return 1; |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 185 | /** |
| 186 | * cap_capset - Validate and apply proposed changes to current's capabilities |
| 187 | * @new: The proposed new credentials; alterations should be made here |
| 188 | * @old: The current task's current credentials |
| 189 | * @effective: A pointer to the proposed new effective capabilities set |
| 190 | * @inheritable: A pointer to the proposed new inheritable capabilities set |
| 191 | * @permitted: A pointer to the proposed new permitted capabilities set |
| 192 | * |
| 193 | * This function validates and applies a proposed mass change to the current |
| 194 | * process's capability sets. The changes are made to the proposed new |
| 195 | * credentials, and assuming no error, will be committed by the caller of LSM. |
| 196 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 197 | int cap_capset(struct cred *new, |
| 198 | const struct cred *old, |
| 199 | const kernel_cap_t *effective, |
| 200 | const kernel_cap_t *inheritable, |
| 201 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 203 | if (cap_inh_is_capped() && |
| 204 | !cap_issubset(*inheritable, |
| 205 | cap_combine(old->cap_inheritable, |
| 206 | old->cap_permitted))) |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 207 | /* incapable of using this inheritable set */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 209 | |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 210 | if (!cap_issubset(*inheritable, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 211 | cap_combine(old->cap_inheritable, |
| 212 | old->cap_bset))) |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 213 | /* no new pI capabilities outside bounding set */ |
| 214 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | /* verify restrictions on target's new Permitted set */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 217 | if (!cap_issubset(*permitted, old->cap_permitted)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 221 | if (!cap_issubset(*effective, *permitted)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 224 | new->cap_effective = *effective; |
| 225 | new->cap_inheritable = *inheritable; |
| 226 | new->cap_permitted = *permitted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 230 | /* |
| 231 | * Clear proposed capability sets for execve(). |
| 232 | */ |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 233 | static inline void bprm_clear_caps(struct linux_binprm *bprm) |
| 234 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 235 | cap_clear(bprm->cred->cap_permitted); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 236 | bprm->cap_effective = false; |
| 237 | } |
| 238 | |
| 239 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 240 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 241 | /** |
| 242 | * cap_inode_need_killpriv - Determine if inode change affects privileges |
| 243 | * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV |
| 244 | * |
| 245 | * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV |
| 246 | * affects the security markings on that inode, and if it is, should |
| 247 | * inode_killpriv() be invoked or the change rejected? |
| 248 | * |
| 249 | * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and |
| 250 | * -ve to deny the change. |
| 251 | */ |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 252 | int cap_inode_need_killpriv(struct dentry *dentry) |
| 253 | { |
| 254 | struct inode *inode = dentry->d_inode; |
| 255 | int error; |
| 256 | |
| 257 | if (!inode->i_op || !inode->i_op->getxattr) |
| 258 | return 0; |
| 259 | |
| 260 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); |
| 261 | if (error <= 0) |
| 262 | return 0; |
| 263 | return 1; |
| 264 | } |
| 265 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 266 | /** |
| 267 | * cap_inode_killpriv - Erase the security markings on an inode |
| 268 | * @dentry: The inode/dentry to alter |
| 269 | * |
| 270 | * Erase the privilege-enhancing security markings on an inode. |
| 271 | * |
| 272 | * Returns 0 if successful, -ve on error. |
| 273 | */ |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 274 | int cap_inode_killpriv(struct dentry *dentry) |
| 275 | { |
| 276 | struct inode *inode = dentry->d_inode; |
| 277 | |
| 278 | if (!inode->i_op || !inode->i_op->removexattr) |
| 279 | return 0; |
| 280 | |
| 281 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); |
| 282 | } |
| 283 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 284 | /* |
| 285 | * Calculate the new process capability sets from the capability sets attached |
| 286 | * to a file. |
| 287 | */ |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 288 | 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] | 289 | struct linux_binprm *bprm, |
| 290 | bool *effective) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 291 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 292 | struct cred *new = bprm->cred; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 293 | unsigned i; |
| 294 | int ret = 0; |
| 295 | |
| 296 | if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 297 | *effective = true; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 298 | |
| 299 | CAP_FOR_EACH_U32(i) { |
| 300 | __u32 permitted = caps->permitted.cap[i]; |
| 301 | __u32 inheritable = caps->inheritable.cap[i]; |
| 302 | |
| 303 | /* |
| 304 | * pP' = (X & fP) | (pI & fI) |
| 305 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 306 | new->cap_permitted.cap[i] = |
| 307 | (new->cap_bset.cap[i] & permitted) | |
| 308 | (new->cap_inheritable.cap[i] & inheritable); |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 309 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 310 | if (permitted & ~new->cap_permitted.cap[i]) |
| 311 | /* insufficient to execute correctly */ |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 312 | ret = -EPERM; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
| 316 | * For legacy apps, with no internal support for recognizing they |
| 317 | * do not have enough capabilities, we return an error if they are |
| 318 | * missing some "forced" (aka file-permitted) capabilities. |
| 319 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 320 | return *effective ? ret : 0; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 321 | } |
| 322 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 323 | /* |
| 324 | * Extract the on-exec-apply capability sets for an executable file. |
| 325 | */ |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 326 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) |
| 327 | { |
| 328 | struct inode *inode = dentry->d_inode; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 329 | __u32 magic_etc; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 330 | unsigned tocopy, i; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 331 | int size; |
| 332 | struct vfs_cap_data caps; |
| 333 | |
| 334 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); |
| 335 | |
| 336 | if (!inode || !inode->i_op || !inode->i_op->getxattr) |
| 337 | return -ENODATA; |
| 338 | |
| 339 | size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, |
| 340 | XATTR_CAPS_SZ); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 341 | if (size == -ENODATA || size == -EOPNOTSUPP) |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 342 | /* no data, that's ok */ |
| 343 | return -ENODATA; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 344 | if (size < 0) |
| 345 | return size; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 346 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 347 | if (size < sizeof(magic_etc)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 348 | return -EINVAL; |
| 349 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 350 | 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] | 351 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 352 | switch (magic_etc & VFS_CAP_REVISION_MASK) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 353 | case VFS_CAP_REVISION_1: |
| 354 | if (size != XATTR_CAPS_SZ_1) |
| 355 | return -EINVAL; |
| 356 | tocopy = VFS_CAP_U32_1; |
| 357 | break; |
| 358 | case VFS_CAP_REVISION_2: |
| 359 | if (size != XATTR_CAPS_SZ_2) |
| 360 | return -EINVAL; |
| 361 | tocopy = VFS_CAP_U32_2; |
| 362 | break; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 363 | default: |
| 364 | return -EINVAL; |
| 365 | } |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 366 | |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 367 | CAP_FOR_EACH_U32(i) { |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 368 | if (i >= tocopy) |
| 369 | break; |
| 370 | cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); |
| 371 | cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 372 | } |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 373 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 374 | return 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 377 | /* |
| 378 | * Attempt to get the on-exec apply capability sets for an executable file from |
| 379 | * its xattrs and, if present, apply them to the proposed credentials being |
| 380 | * constructed by execve(). |
| 381 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 382 | static int get_file_caps(struct linux_binprm *bprm, bool *effective) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 383 | { |
| 384 | struct dentry *dentry; |
| 385 | int rc = 0; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 386 | struct cpu_vfs_cap_data vcaps; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 387 | |
Serge Hallyn | 3318a38 | 2008-10-30 11:52:23 -0500 | [diff] [blame] | 388 | bprm_clear_caps(bprm); |
| 389 | |
Serge E. Hallyn | 1f29fae | 2008-11-05 16:08:52 -0600 | [diff] [blame] | 390 | if (!file_caps_enabled) |
| 391 | return 0; |
| 392 | |
Serge Hallyn | 3318a38 | 2008-10-30 11:52:23 -0500 | [diff] [blame] | 393 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 394 | return 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 395 | |
| 396 | dentry = dget(bprm->file->f_dentry); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 397 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 398 | rc = get_vfs_caps_from_disk(dentry, &vcaps); |
| 399 | if (rc < 0) { |
| 400 | if (rc == -EINVAL) |
| 401 | printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n", |
| 402 | __func__, rc, bprm->filename); |
| 403 | else if (rc == -ENODATA) |
| 404 | rc = 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 405 | goto out; |
| 406 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 407 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 408 | rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective); |
| 409 | if (rc == -EINVAL) |
| 410 | printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n", |
| 411 | __func__, rc, bprm->filename); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 412 | |
| 413 | out: |
| 414 | dput(dentry); |
| 415 | if (rc) |
| 416 | bprm_clear_caps(bprm); |
| 417 | |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | #else |
| 422 | int cap_inode_need_killpriv(struct dentry *dentry) |
| 423 | { |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int cap_inode_killpriv(struct dentry *dentry) |
| 428 | { |
| 429 | return 0; |
| 430 | } |
| 431 | |
Eric Paris | e50a906 | 2008-11-13 18:37:25 -0500 | [diff] [blame] | 432 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) |
| 433 | { |
| 434 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); |
| 435 | return -ENODATA; |
| 436 | } |
| 437 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 438 | 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] | 439 | { |
| 440 | bprm_clear_caps(bprm); |
| 441 | return 0; |
| 442 | } |
| 443 | #endif |
| 444 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 445 | /* |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 446 | * Determine whether a exec'ing process's new permitted capabilities should be |
| 447 | * limited to just what it already has. |
| 448 | * |
| 449 | * This prevents processes that are being ptraced from gaining access to |
| 450 | * CAP_SETPCAP, unless the process they're tracing already has it, and the |
| 451 | * binary they're executing has filecaps that elevate it. |
| 452 | * |
| 453 | * Returns 1 if they should be limited, 0 if they are not. |
| 454 | */ |
| 455 | static inline int cap_limit_ptraced_target(void) |
| 456 | { |
| 457 | #ifndef CONFIG_SECURITY_FILE_CAPABILITIES |
| 458 | if (capable(CAP_SETPCAP)) |
| 459 | return 0; |
| 460 | #endif |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * cap_bprm_set_creds - Set up the proposed credentials for execve(). |
| 466 | * @bprm: The execution parameters, including the proposed creds |
| 467 | * |
| 468 | * Set up the proposed credentials for a new execution context being |
| 469 | * constructed by execve(). The proposed creds in @bprm->cred is altered, |
| 470 | * 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] | 471 | */ |
| 472 | int cap_bprm_set_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 474 | const struct cred *old = current_cred(); |
| 475 | struct cred *new = bprm->cred; |
| 476 | bool effective; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 477 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 479 | effective = false; |
| 480 | ret = get_file_caps(bprm, &effective); |
| 481 | if (ret < 0) |
| 482 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 484 | if (!issecure(SECURE_NOROOT)) { |
| 485 | /* |
| 486 | * To support inheritance of root-permissions and suid-root |
| 487 | * executables under compatibility mode, we override the |
| 488 | * capability sets for the file. |
| 489 | * |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 490 | * 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] | 491 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 492 | if (new->euid == 0 || new->uid == 0) { |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 493 | /* pP' = (cap_bset & ~0) | (pI & ~0) */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 494 | new->cap_permitted = cap_combine(old->cap_bset, |
| 495 | old->cap_inheritable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 497 | if (new->euid == 0) |
| 498 | effective = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 500 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 501 | /* Don't let someone trace a set[ug]id/setpcap binary with the revised |
| 502 | * credentials unless they have the appropriate permit |
| 503 | */ |
| 504 | if ((new->euid != old->uid || |
| 505 | new->egid != old->gid || |
| 506 | !cap_issubset(new->cap_permitted, old->cap_permitted)) && |
| 507 | bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) { |
| 508 | /* downgrade; they get no more than they had, and maybe less */ |
| 509 | if (!capable(CAP_SETUID)) { |
| 510 | new->euid = new->uid; |
| 511 | new->egid = new->gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 513 | if (cap_limit_ptraced_target()) |
| 514 | new->cap_permitted = cap_intersect(new->cap_permitted, |
| 515 | old->cap_permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 518 | new->suid = new->fsuid = new->euid; |
| 519 | new->sgid = new->fsgid = new->egid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 521 | /* For init, we want to retain the capabilities set in the initial |
| 522 | * task. Thus we skip the usual capability rules |
| 523 | */ |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 524 | if (!is_global_init(current)) { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 525 | if (effective) |
| 526 | new->cap_effective = new->cap_permitted; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 527 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 528 | cap_clear(new->cap_effective); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
| 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
| 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 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 730 | /* |
| 731 | * Rationale: code calling task_setscheduler, task_setioprio, and |
| 732 | * task_setnice, assumes that |
| 733 | * . if capable(cap_sys_nice), then those actions should be allowed |
| 734 | * . if not capable(cap_sys_nice), but acting on your own processes, |
| 735 | * then those actions should be allowed |
| 736 | * This is insufficient now since you can call code without suid, but |
| 737 | * yet with increased caps. |
| 738 | * So we check for increased caps on the target process. |
| 739 | */ |
Serge E. Hallyn | de45e80 | 2008-09-26 22:27:47 -0400 | [diff] [blame] | 740 | static int cap_safe_nice(struct task_struct *p) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 741 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 742 | int is_subset; |
| 743 | |
| 744 | rcu_read_lock(); |
| 745 | is_subset = cap_issubset(__task_cred(p)->cap_permitted, |
| 746 | current_cred()->cap_permitted); |
| 747 | rcu_read_unlock(); |
| 748 | |
| 749 | if (!is_subset && !capable(CAP_SYS_NICE)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 750 | return -EPERM; |
| 751 | return 0; |
| 752 | } |
| 753 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 754 | /** |
| 755 | * cap_task_setscheduler - Detemine if scheduler policy change is permitted |
| 756 | * @p: The task to affect |
| 757 | * @policy: The policy to effect |
| 758 | * @lp: The parameters to the scheduling policy |
| 759 | * |
| 760 | * Detemine if the requested scheduler policy change is permitted for the |
| 761 | * specified task, returning 0 if permission is granted, -ve if denied. |
| 762 | */ |
| 763 | int cap_task_setscheduler(struct task_struct *p, int policy, |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 764 | struct sched_param *lp) |
| 765 | { |
| 766 | return cap_safe_nice(p); |
| 767 | } |
| 768 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 769 | /** |
| 770 | * cap_task_ioprio - Detemine if I/O priority change is permitted |
| 771 | * @p: The task to affect |
| 772 | * @ioprio: The I/O priority to set |
| 773 | * |
| 774 | * Detemine if the requested I/O priority change is permitted for the specified |
| 775 | * task, returning 0 if permission is granted, -ve if denied. |
| 776 | */ |
| 777 | int cap_task_setioprio(struct task_struct *p, int ioprio) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 778 | { |
| 779 | return cap_safe_nice(p); |
| 780 | } |
| 781 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 782 | /** |
| 783 | * cap_task_ioprio - Detemine if task priority change is permitted |
| 784 | * @p: The task to affect |
| 785 | * @nice: The nice value to set |
| 786 | * |
| 787 | * Detemine if the requested task priority change is permitted for the |
| 788 | * specified task, returning 0 if permission is granted, -ve if denied. |
| 789 | */ |
| 790 | int cap_task_setnice(struct task_struct *p, int nice) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 791 | { |
| 792 | return cap_safe_nice(p); |
| 793 | } |
| 794 | |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 795 | /* |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 796 | * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from |
| 797 | * 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] | 798 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 799 | static long cap_prctl_drop(struct cred *new, unsigned long cap) |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 800 | { |
| 801 | if (!capable(CAP_SETPCAP)) |
| 802 | return -EPERM; |
| 803 | if (!cap_valid(cap)) |
| 804 | return -EINVAL; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 805 | |
| 806 | cap_lower(new->cap_bset, cap); |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 807 | return 0; |
| 808 | } |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 809 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 810 | #else |
| 811 | int cap_task_setscheduler (struct task_struct *p, int policy, |
| 812 | struct sched_param *lp) |
| 813 | { |
| 814 | return 0; |
| 815 | } |
| 816 | int cap_task_setioprio (struct task_struct *p, int ioprio) |
| 817 | { |
| 818 | return 0; |
| 819 | } |
| 820 | int cap_task_setnice (struct task_struct *p, int nice) |
| 821 | { |
| 822 | return 0; |
| 823 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 824 | #endif |
| 825 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 826 | /** |
| 827 | * cap_task_prctl - Implement process control functions for this security module |
| 828 | * @option: The process control function requested |
| 829 | * @arg2, @arg3, @arg4, @arg5: The argument data for this function |
| 830 | * |
| 831 | * Allow process control functions (sys_prctl()) to alter capabilities; may |
| 832 | * also deny access to other functions not otherwise implemented here. |
| 833 | * |
| 834 | * Returns 0 or +ve on success, -ENOSYS if this function is not implemented |
| 835 | * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM |
| 836 | * modules will consider performing the function. |
| 837 | */ |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 838 | int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 839 | unsigned long arg4, unsigned long arg5) |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 840 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 841 | struct cred *new; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 842 | long error = 0; |
| 843 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 844 | new = prepare_creds(); |
| 845 | if (!new) |
| 846 | return -ENOMEM; |
| 847 | |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 848 | switch (option) { |
| 849 | case PR_CAPBSET_READ: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 850 | error = -EINVAL; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 851 | if (!cap_valid(arg2)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 852 | goto error; |
| 853 | error = !!cap_raised(new->cap_bset, arg2); |
| 854 | goto no_change; |
| 855 | |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 856 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 857 | case PR_CAPBSET_DROP: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 858 | error = cap_prctl_drop(new, arg2); |
| 859 | if (error < 0) |
| 860 | goto error; |
| 861 | goto changed; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * The next four prctl's remain to assist with transitioning a |
| 865 | * system from legacy UID=0 based privilege (when filesystem |
| 866 | * capabilities are not in use) to a system using filesystem |
| 867 | * capabilities only - as the POSIX.1e draft intended. |
| 868 | * |
| 869 | * Note: |
| 870 | * |
| 871 | * PR_SET_SECUREBITS = |
| 872 | * issecure_mask(SECURE_KEEP_CAPS_LOCKED) |
| 873 | * | issecure_mask(SECURE_NOROOT) |
| 874 | * | issecure_mask(SECURE_NOROOT_LOCKED) |
| 875 | * | issecure_mask(SECURE_NO_SETUID_FIXUP) |
| 876 | * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED) |
| 877 | * |
| 878 | * will ensure that the current process and all of its |
| 879 | * children will be locked into a pure |
| 880 | * capability-based-privilege environment. |
| 881 | */ |
| 882 | case PR_SET_SECUREBITS: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 883 | error = -EPERM; |
| 884 | if ((((new->securebits & SECURE_ALL_LOCKS) >> 1) |
| 885 | & (new->securebits ^ arg2)) /*[1]*/ |
| 886 | || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ |
| 887 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 888 | || (cap_capable(CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0) /*[4]*/ |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 889 | /* |
| 890 | * [1] no changing of bits that are locked |
| 891 | * [2] no unlocking of locks |
| 892 | * [3] no setting of unsupported bits |
| 893 | * [4] doing anything requires privilege (go read about |
| 894 | * the "sendmail capabilities bug") |
| 895 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 896 | ) |
| 897 | /* cannot change a locked bit */ |
| 898 | goto error; |
| 899 | new->securebits = arg2; |
| 900 | goto changed; |
| 901 | |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 902 | case PR_GET_SECUREBITS: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 903 | error = new->securebits; |
| 904 | goto no_change; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 905 | |
| 906 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 907 | |
| 908 | case PR_GET_KEEPCAPS: |
| 909 | if (issecure(SECURE_KEEP_CAPS)) |
| 910 | error = 1; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 911 | goto no_change; |
| 912 | |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 913 | case PR_SET_KEEPCAPS: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 914 | error = -EINVAL; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 915 | if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 916 | goto error; |
| 917 | error = -EPERM; |
| 918 | if (issecure(SECURE_KEEP_CAPS_LOCKED)) |
| 919 | goto error; |
| 920 | if (arg2) |
| 921 | new->securebits |= issecure_mask(SECURE_KEEP_CAPS); |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 922 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 923 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
| 924 | goto changed; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 925 | |
| 926 | default: |
| 927 | /* No functionality available - continue with default */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 928 | error = -ENOSYS; |
| 929 | goto error; |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /* Functionality provided */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 933 | changed: |
| 934 | return commit_creds(new); |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 935 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 936 | no_change: |
| 937 | error = 0; |
| 938 | error: |
| 939 | abort_creds(new); |
| 940 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 943 | /** |
| 944 | * cap_syslog - Determine whether syslog function is permitted |
| 945 | * @type: Function requested |
| 946 | * |
| 947 | * Determine whether the current process is permitted to use a particular |
| 948 | * syslog function, returning 0 if permission is granted, -ve if not. |
| 949 | */ |
| 950 | int cap_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
| 952 | if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN)) |
| 953 | return -EPERM; |
| 954 | return 0; |
| 955 | } |
| 956 | |
David Howells | 1d04598 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 957 | /** |
| 958 | * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted |
| 959 | * @mm: The VM space in which the new mapping is to be made |
| 960 | * @pages: The size of the mapping |
| 961 | * |
| 962 | * Determine whether the allocation of a new virtual mapping by the current |
| 963 | * task is permitted, returning 0 if permission is granted, -ve if not. |
| 964 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 965 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { |
| 967 | int cap_sys_admin = 0; |
| 968 | |
David Howells | 14eaddc | 2008-12-31 15:15:42 +0000 | [diff] [blame] | 969 | if (cap_capable(CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | cap_sys_admin = 1; |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 971 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |