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 | { |
| 33 | NETLINK_CB(skb).eff_cap = current->cap_effective; |
| 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 | } |
| 43 | |
| 44 | EXPORT_SYMBOL(cap_netlink_recv); |
| 45 | |
Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 46 | /* |
| 47 | * NOTE WELL: cap_capable() cannot be used like the kernel's capable() |
| 48 | * function. That is, it has the reverse semantics: cap_capable() |
| 49 | * returns 0 when a task has a capability, but the kernel's capable() |
| 50 | * returns 1 for this case. |
| 51 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int cap_capable (struct task_struct *tsk, int cap) |
| 53 | { |
| 54 | /* Derived from include/linux/sched.h:capable. */ |
| 55 | if (cap_raised(tsk->cap_effective, cap)) |
| 56 | return 0; |
| 57 | return -EPERM; |
| 58 | } |
| 59 | |
| 60 | int cap_settime(struct timespec *ts, struct timezone *tz) |
| 61 | { |
| 62 | if (!capable(CAP_SYS_TIME)) |
| 63 | return -EPERM; |
| 64 | return 0; |
| 65 | } |
| 66 | |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 67 | int cap_ptrace_may_access(struct task_struct *child, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 70 | if (cap_issubset(child->cap_permitted, current->cap_permitted)) |
| 71 | return 0; |
| 72 | if (capable(CAP_SYS_PTRACE)) |
| 73 | return 0; |
| 74 | return -EPERM; |
| 75 | } |
| 76 | |
| 77 | int cap_ptrace_traceme(struct task_struct *parent) |
| 78 | { |
| 79 | /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ |
| 80 | if (cap_issubset(current->cap_permitted, parent->cap_permitted)) |
| 81 | return 0; |
| 82 | if (has_capability(parent, CAP_SYS_PTRACE)) |
| 83 | return 0; |
| 84 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int cap_capget (struct task_struct *target, kernel_cap_t *effective, |
| 88 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
| 89 | { |
| 90 | /* Derived from kernel/capability.c:sys_capget. */ |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 91 | *effective = target->cap_effective; |
| 92 | *inheritable = target->cap_inheritable; |
| 93 | *permitted = target->cap_permitted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 97 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 98 | |
| 99 | static inline int cap_block_setpcap(struct task_struct *target) |
| 100 | { |
| 101 | /* |
| 102 | * No support for remote process capability manipulation with |
| 103 | * filesystem capability support. |
| 104 | */ |
| 105 | return (target != current); |
| 106 | } |
| 107 | |
| 108 | static inline int cap_inh_is_capped(void) |
| 109 | { |
| 110 | /* |
Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 111 | * Return 1 if changes to the inheritable set are limited |
| 112 | * to the old permitted set. That is, if the current task |
| 113 | * does *not* possess the CAP_SETPCAP capability. |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 114 | */ |
Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 115 | return (cap_capable(current, CAP_SETPCAP) != 0); |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Andrew G. Morgan | 1209726 | 2008-07-04 09:59:59 -0700 | [diff] [blame] | 118 | static inline int cap_limit_ptraced_target(void) { return 1; } |
| 119 | |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 120 | #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 121 | |
| 122 | static inline int cap_block_setpcap(struct task_struct *t) { return 0; } |
| 123 | static inline int cap_inh_is_capped(void) { return 1; } |
Andrew G. Morgan | 1209726 | 2008-07-04 09:59:59 -0700 | [diff] [blame] | 124 | static inline int cap_limit_ptraced_target(void) |
| 125 | { |
| 126 | return !capable(CAP_SETPCAP); |
| 127 | } |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 128 | |
| 129 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | int cap_capset_check (struct task_struct *target, kernel_cap_t *effective, |
| 132 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
| 133 | { |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 134 | if (cap_block_setpcap(target)) { |
| 135 | return -EPERM; |
| 136 | } |
| 137 | if (cap_inh_is_capped() |
| 138 | && !cap_issubset(*inheritable, |
| 139 | cap_combine(target->cap_inheritable, |
| 140 | current->cap_permitted))) { |
| 141 | /* incapable of using this inheritable set */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return -EPERM; |
| 143 | } |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 144 | if (!cap_issubset(*inheritable, |
| 145 | cap_combine(target->cap_inheritable, |
| 146 | current->cap_bset))) { |
| 147 | /* no new pI capabilities outside bounding set */ |
| 148 | return -EPERM; |
| 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | /* verify restrictions on target's new Permitted set */ |
| 152 | if (!cap_issubset (*permitted, |
| 153 | cap_combine (target->cap_permitted, |
| 154 | current->cap_permitted))) { |
| 155 | return -EPERM; |
| 156 | } |
| 157 | |
| 158 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ |
| 159 | if (!cap_issubset (*effective, *permitted)) { |
| 160 | return -EPERM; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | void cap_capset_set (struct task_struct *target, kernel_cap_t *effective, |
| 167 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
| 168 | { |
| 169 | target->cap_effective = *effective; |
| 170 | target->cap_inheritable = *inheritable; |
| 171 | target->cap_permitted = *permitted; |
| 172 | } |
| 173 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 174 | static inline void bprm_clear_caps(struct linux_binprm *bprm) |
| 175 | { |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 176 | cap_clear(bprm->cap_post_exec_permitted); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 177 | bprm->cap_effective = false; |
| 178 | } |
| 179 | |
| 180 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 181 | |
| 182 | int cap_inode_need_killpriv(struct dentry *dentry) |
| 183 | { |
| 184 | struct inode *inode = dentry->d_inode; |
| 185 | int error; |
| 186 | |
| 187 | if (!inode->i_op || !inode->i_op->getxattr) |
| 188 | return 0; |
| 189 | |
| 190 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); |
| 191 | if (error <= 0) |
| 192 | return 0; |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | int cap_inode_killpriv(struct dentry *dentry) |
| 197 | { |
| 198 | struct inode *inode = dentry->d_inode; |
| 199 | |
| 200 | if (!inode->i_op || !inode->i_op->removexattr) |
| 201 | return 0; |
| 202 | |
| 203 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); |
| 204 | } |
| 205 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 206 | static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, |
| 207 | struct linux_binprm *bprm) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 208 | { |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 209 | unsigned i; |
| 210 | int ret = 0; |
| 211 | |
| 212 | if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) |
| 213 | bprm->cap_effective = true; |
| 214 | else |
| 215 | bprm->cap_effective = false; |
| 216 | |
| 217 | CAP_FOR_EACH_U32(i) { |
| 218 | __u32 permitted = caps->permitted.cap[i]; |
| 219 | __u32 inheritable = caps->inheritable.cap[i]; |
| 220 | |
| 221 | /* |
| 222 | * pP' = (X & fP) | (pI & fI) |
| 223 | */ |
| 224 | bprm->cap_post_exec_permitted.cap[i] = |
| 225 | (current->cap_bset.cap[i] & permitted) | |
| 226 | (current->cap_inheritable.cap[i] & inheritable); |
| 227 | |
| 228 | if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) { |
| 229 | /* |
| 230 | * insufficient to execute correctly |
| 231 | */ |
| 232 | ret = -EPERM; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * For legacy apps, with no internal support for recognizing they |
| 238 | * do not have enough capabilities, we return an error if they are |
| 239 | * missing some "forced" (aka file-permitted) capabilities. |
| 240 | */ |
| 241 | return bprm->cap_effective ? ret : 0; |
| 242 | } |
| 243 | |
| 244 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) |
| 245 | { |
| 246 | struct inode *inode = dentry->d_inode; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 247 | __u32 magic_etc; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 248 | unsigned tocopy, i; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 249 | int size; |
| 250 | struct vfs_cap_data caps; |
| 251 | |
| 252 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); |
| 253 | |
| 254 | if (!inode || !inode->i_op || !inode->i_op->getxattr) |
| 255 | return -ENODATA; |
| 256 | |
| 257 | size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, |
| 258 | XATTR_CAPS_SZ); |
| 259 | if (size == -ENODATA || size == -EOPNOTSUPP) { |
| 260 | /* no data, that's ok */ |
| 261 | return -ENODATA; |
| 262 | } |
| 263 | if (size < 0) |
| 264 | return size; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 265 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 266 | if (size < sizeof(magic_etc)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 267 | return -EINVAL; |
| 268 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 269 | 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] | 270 | |
| 271 | switch ((magic_etc & VFS_CAP_REVISION_MASK)) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 272 | case VFS_CAP_REVISION_1: |
| 273 | if (size != XATTR_CAPS_SZ_1) |
| 274 | return -EINVAL; |
| 275 | tocopy = VFS_CAP_U32_1; |
| 276 | break; |
| 277 | case VFS_CAP_REVISION_2: |
| 278 | if (size != XATTR_CAPS_SZ_2) |
| 279 | return -EINVAL; |
| 280 | tocopy = VFS_CAP_U32_2; |
| 281 | break; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 282 | default: |
| 283 | return -EINVAL; |
| 284 | } |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 285 | |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 286 | CAP_FOR_EACH_U32(i) { |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 287 | if (i >= tocopy) |
| 288 | break; |
| 289 | cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); |
| 290 | cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 291 | } |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 292 | return 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* Locate any VFS capabilities: */ |
| 296 | static int get_file_caps(struct linux_binprm *bprm) |
| 297 | { |
| 298 | struct dentry *dentry; |
| 299 | int rc = 0; |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 300 | struct cpu_vfs_cap_data vcaps; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 301 | |
Serge Hallyn | 3318a38 | 2008-10-30 11:52:23 -0500 | [diff] [blame] | 302 | bprm_clear_caps(bprm); |
| 303 | |
Serge E. Hallyn | 1f29fae | 2008-11-05 16:08:52 -0600 | [diff] [blame] | 304 | if (!file_caps_enabled) |
| 305 | return 0; |
| 306 | |
Serge Hallyn | 3318a38 | 2008-10-30 11:52:23 -0500 | [diff] [blame] | 307 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 308 | return 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 309 | |
| 310 | dentry = dget(bprm->file->f_dentry); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 311 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 312 | rc = get_vfs_caps_from_disk(dentry, &vcaps); |
| 313 | if (rc < 0) { |
| 314 | if (rc == -EINVAL) |
| 315 | printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n", |
| 316 | __func__, rc, bprm->filename); |
| 317 | else if (rc == -ENODATA) |
| 318 | rc = 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 319 | goto out; |
| 320 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 321 | |
Eric Paris | c0b0044 | 2008-11-11 21:48:10 +1100 | [diff] [blame] | 322 | rc = bprm_caps_from_vfs_caps(&vcaps, bprm); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 323 | |
| 324 | out: |
| 325 | dput(dentry); |
| 326 | if (rc) |
| 327 | bprm_clear_caps(bprm); |
| 328 | |
| 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | #else |
| 333 | int cap_inode_need_killpriv(struct dentry *dentry) |
| 334 | { |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | int cap_inode_killpriv(struct dentry *dentry) |
| 339 | { |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static inline int get_file_caps(struct linux_binprm *bprm) |
| 344 | { |
| 345 | bprm_clear_caps(bprm); |
| 346 | return 0; |
| 347 | } |
| 348 | #endif |
| 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int cap_bprm_set_security (struct linux_binprm *bprm) |
| 351 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 352 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 354 | ret = get_file_caps(bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 356 | if (!issecure(SECURE_NOROOT)) { |
| 357 | /* |
| 358 | * To support inheritance of root-permissions and suid-root |
| 359 | * executables under compatibility mode, we override the |
| 360 | * capability sets for the file. |
| 361 | * |
| 362 | * If only the real uid is 0, we do not set the effective |
| 363 | * bit. |
| 364 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (bprm->e_uid == 0 || current->uid == 0) { |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 366 | /* pP' = (cap_bset & ~0) | (pI & ~0) */ |
| 367 | bprm->cap_post_exec_permitted = cap_combine( |
| 368 | current->cap_bset, current->cap_inheritable |
| 369 | ); |
| 370 | bprm->cap_effective = (bprm->e_uid == 0); |
| 371 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 374 | |
| 375 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe) |
| 379 | { |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame^] | 380 | kernel_cap_t pP = current->cap_permitted; |
| 381 | kernel_cap_t pE = current->cap_effective; |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | if (bprm->e_uid != current->uid || bprm->e_gid != current->gid || |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 384 | !cap_issubset(bprm->cap_post_exec_permitted, |
| 385 | current->cap_permitted)) { |
Kawai, Hidehiro | 6c5d523 | 2007-07-19 01:48:27 -0700 | [diff] [blame] | 386 | set_dumpable(current->mm, suid_dumpable); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 387 | current->pdeath_signal = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) { |
| 390 | if (!capable(CAP_SETUID)) { |
| 391 | bprm->e_uid = current->uid; |
| 392 | bprm->e_gid = current->gid; |
| 393 | } |
Andrew G. Morgan | 1209726 | 2008-07-04 09:59:59 -0700 | [diff] [blame] | 394 | if (cap_limit_ptraced_target()) { |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 395 | bprm->cap_post_exec_permitted = cap_intersect( |
| 396 | bprm->cap_post_exec_permitted, |
| 397 | current->cap_permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | current->suid = current->euid = current->fsuid = bprm->e_uid; |
| 403 | current->sgid = current->egid = current->fsgid = bprm->e_gid; |
| 404 | |
| 405 | /* For init, we want to retain the capabilities set |
| 406 | * in the init_task struct. Thus we skip the usual |
| 407 | * capability rules */ |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 408 | if (!is_global_init(current)) { |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 409 | current->cap_permitted = bprm->cap_post_exec_permitted; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 410 | if (bprm->cap_effective) |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 411 | current->cap_effective = bprm->cap_post_exec_permitted; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 412 | else |
| 413 | cap_clear(current->cap_effective); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame^] | 416 | /* |
| 417 | * Audit candidate if current->cap_effective is set |
| 418 | * |
| 419 | * We do not bother to audit if 3 things are true: |
| 420 | * 1) cap_effective has all caps |
| 421 | * 2) we are root |
| 422 | * 3) root is supposed to have all caps (SECURE_NOROOT) |
| 423 | * Since this is just a normal root execing a process. |
| 424 | * |
| 425 | * Number 1 above might fail if you don't have a full bset, but I think |
| 426 | * that is interesting information to audit. |
| 427 | */ |
| 428 | if (!cap_isclear(current->cap_effective)) { |
| 429 | if (!cap_issubset(CAP_FULL_SET, current->cap_effective) || |
| 430 | (bprm->e_uid != 0) || (current->uid != 0) || |
| 431 | issecure(SECURE_NOROOT)) |
| 432 | audit_log_bprm_fcaps(bprm, &pP, &pE); |
| 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 435 | current->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | int cap_bprm_secureexec (struct linux_binprm *bprm) |
| 439 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 440 | if (current->uid != 0) { |
| 441 | if (bprm->cap_effective) |
| 442 | return 1; |
Andrew G. Morgan | 5459c16 | 2008-07-23 21:28:24 -0700 | [diff] [blame] | 443 | if (!cap_isclear(bprm->cap_post_exec_permitted)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 444 | return 1; |
| 445 | } |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return (current->euid != current->uid || |
| 448 | current->egid != current->gid); |
| 449 | } |
| 450 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 451 | int cap_inode_setxattr(struct dentry *dentry, const char *name, |
| 452 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 454 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
| 455 | if (!capable(CAP_SETFCAP)) |
| 456 | return -EPERM; |
| 457 | return 0; |
| 458 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
| 460 | !capable(CAP_SYS_ADMIN)) |
| 461 | return -EPERM; |
| 462 | return 0; |
| 463 | } |
| 464 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 465 | int cap_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 467 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
| 468 | if (!capable(CAP_SETFCAP)) |
| 469 | return -EPERM; |
| 470 | return 0; |
| 471 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
| 473 | !capable(CAP_SYS_ADMIN)) |
| 474 | return -EPERM; |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* moved from kernel/sys.c. */ |
| 479 | /* |
| 480 | * cap_emulate_setxuid() fixes the effective / permitted capabilities of |
| 481 | * a process after a call to setuid, setreuid, or setresuid. |
| 482 | * |
| 483 | * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of |
| 484 | * {r,e,s}uid != 0, the permitted and effective capabilities are |
| 485 | * cleared. |
| 486 | * |
| 487 | * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective |
| 488 | * capabilities of the process are cleared. |
| 489 | * |
| 490 | * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective |
| 491 | * capabilities are set to the permitted capabilities. |
| 492 | * |
| 493 | * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should |
| 494 | * never happen. |
| 495 | * |
| 496 | * -astor |
| 497 | * |
| 498 | * cevans - New behaviour, Oct '99 |
| 499 | * A process may, via prctl(), elect to keep its capabilities when it |
| 500 | * calls setuid() and switches away from uid==0. Both permitted and |
| 501 | * effective sets will be retained. |
| 502 | * Without this change, it was impossible for a daemon to drop only some |
| 503 | * of its privilege. The call to setuid(!=0) would drop all privileges! |
| 504 | * Keeping uid 0 is not an option because uid 0 owns too many vital |
| 505 | * files.. |
| 506 | * Thanks to Olaf Kirch and Peter Benie for spotting this. |
| 507 | */ |
| 508 | static inline void cap_emulate_setxuid (int old_ruid, int old_euid, |
| 509 | int old_suid) |
| 510 | { |
| 511 | if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) && |
| 512 | (current->uid != 0 && current->euid != 0 && current->suid != 0) && |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 513 | !issecure(SECURE_KEEP_CAPS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | cap_clear (current->cap_permitted); |
| 515 | cap_clear (current->cap_effective); |
| 516 | } |
| 517 | if (old_euid == 0 && current->euid != 0) { |
| 518 | cap_clear (current->cap_effective); |
| 519 | } |
| 520 | if (old_euid != 0 && current->euid == 0) { |
| 521 | current->cap_effective = current->cap_permitted; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid, |
| 526 | int flags) |
| 527 | { |
| 528 | switch (flags) { |
| 529 | case LSM_SETID_RE: |
| 530 | case LSM_SETID_ID: |
| 531 | case LSM_SETID_RES: |
| 532 | /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */ |
| 533 | if (!issecure (SECURE_NO_SETUID_FIXUP)) { |
| 534 | cap_emulate_setxuid (old_ruid, old_euid, old_suid); |
| 535 | } |
| 536 | break; |
| 537 | case LSM_SETID_FS: |
| 538 | { |
| 539 | uid_t old_fsuid = old_ruid; |
| 540 | |
| 541 | /* Copied from kernel/sys.c:setfsuid. */ |
| 542 | |
| 543 | /* |
| 544 | * FIXME - is fsuser used for all CAP_FS_MASK capabilities? |
| 545 | * if not, we might be a bit too harsh here. |
| 546 | */ |
| 547 | |
| 548 | if (!issecure (SECURE_NO_SETUID_FIXUP)) { |
| 549 | if (old_fsuid == 0 && current->fsuid != 0) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 550 | current->cap_effective = |
| 551 | cap_drop_fs_set( |
| 552 | current->cap_effective); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | if (old_fsuid != 0 && current->fsuid == 0) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 555 | current->cap_effective = |
| 556 | cap_raise_fs_set( |
| 557 | current->cap_effective, |
| 558 | current->cap_permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | break; |
| 562 | } |
| 563 | default: |
| 564 | return -EINVAL; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 570 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 571 | /* |
| 572 | * Rationale: code calling task_setscheduler, task_setioprio, and |
| 573 | * task_setnice, assumes that |
| 574 | * . if capable(cap_sys_nice), then those actions should be allowed |
| 575 | * . if not capable(cap_sys_nice), but acting on your own processes, |
| 576 | * then those actions should be allowed |
| 577 | * This is insufficient now since you can call code without suid, but |
| 578 | * yet with increased caps. |
| 579 | * So we check for increased caps on the target process. |
| 580 | */ |
Serge E. Hallyn | de45e80 | 2008-09-26 22:27:47 -0400 | [diff] [blame] | 581 | static int cap_safe_nice(struct task_struct *p) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 582 | { |
| 583 | if (!cap_issubset(p->cap_permitted, current->cap_permitted) && |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 584 | !capable(CAP_SYS_NICE)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 585 | return -EPERM; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | int cap_task_setscheduler (struct task_struct *p, int policy, |
| 590 | struct sched_param *lp) |
| 591 | { |
| 592 | return cap_safe_nice(p); |
| 593 | } |
| 594 | |
| 595 | int cap_task_setioprio (struct task_struct *p, int ioprio) |
| 596 | { |
| 597 | return cap_safe_nice(p); |
| 598 | } |
| 599 | |
| 600 | int cap_task_setnice (struct task_struct *p, int nice) |
| 601 | { |
| 602 | return cap_safe_nice(p); |
| 603 | } |
| 604 | |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 605 | /* |
| 606 | * called from kernel/sys.c for prctl(PR_CABSET_DROP) |
| 607 | * done without task_capability_lock() because it introduces |
| 608 | * no new races - i.e. only another task doing capget() on |
| 609 | * this task could get inconsistent info. There can be no |
| 610 | * racing writer bc a task can only change its own caps. |
| 611 | */ |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 612 | static long cap_prctl_drop(unsigned long cap) |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 613 | { |
| 614 | if (!capable(CAP_SETPCAP)) |
| 615 | return -EPERM; |
| 616 | if (!cap_valid(cap)) |
| 617 | return -EINVAL; |
| 618 | cap_lower(current->cap_bset, cap); |
| 619 | return 0; |
| 620 | } |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 621 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 622 | #else |
| 623 | int cap_task_setscheduler (struct task_struct *p, int policy, |
| 624 | struct sched_param *lp) |
| 625 | { |
| 626 | return 0; |
| 627 | } |
| 628 | int cap_task_setioprio (struct task_struct *p, int ioprio) |
| 629 | { |
| 630 | return 0; |
| 631 | } |
| 632 | int cap_task_setnice (struct task_struct *p, int nice) |
| 633 | { |
| 634 | return 0; |
| 635 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 636 | #endif |
| 637 | |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 638 | int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, |
| 639 | unsigned long arg4, unsigned long arg5, long *rc_p) |
| 640 | { |
| 641 | long error = 0; |
| 642 | |
| 643 | switch (option) { |
| 644 | case PR_CAPBSET_READ: |
| 645 | if (!cap_valid(arg2)) |
| 646 | error = -EINVAL; |
| 647 | else |
| 648 | error = !!cap_raised(current->cap_bset, arg2); |
| 649 | break; |
| 650 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 651 | case PR_CAPBSET_DROP: |
| 652 | error = cap_prctl_drop(arg2); |
| 653 | break; |
| 654 | |
| 655 | /* |
| 656 | * The next four prctl's remain to assist with transitioning a |
| 657 | * system from legacy UID=0 based privilege (when filesystem |
| 658 | * capabilities are not in use) to a system using filesystem |
| 659 | * capabilities only - as the POSIX.1e draft intended. |
| 660 | * |
| 661 | * Note: |
| 662 | * |
| 663 | * PR_SET_SECUREBITS = |
| 664 | * issecure_mask(SECURE_KEEP_CAPS_LOCKED) |
| 665 | * | issecure_mask(SECURE_NOROOT) |
| 666 | * | issecure_mask(SECURE_NOROOT_LOCKED) |
| 667 | * | issecure_mask(SECURE_NO_SETUID_FIXUP) |
| 668 | * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED) |
| 669 | * |
| 670 | * will ensure that the current process and all of its |
| 671 | * children will be locked into a pure |
| 672 | * capability-based-privilege environment. |
| 673 | */ |
| 674 | case PR_SET_SECUREBITS: |
| 675 | if ((((current->securebits & SECURE_ALL_LOCKS) >> 1) |
| 676 | & (current->securebits ^ arg2)) /*[1]*/ |
| 677 | || ((current->securebits & SECURE_ALL_LOCKS |
| 678 | & ~arg2)) /*[2]*/ |
| 679 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ |
| 680 | || (cap_capable(current, CAP_SETPCAP) != 0)) { /*[4]*/ |
| 681 | /* |
| 682 | * [1] no changing of bits that are locked |
| 683 | * [2] no unlocking of locks |
| 684 | * [3] no setting of unsupported bits |
| 685 | * [4] doing anything requires privilege (go read about |
| 686 | * the "sendmail capabilities bug") |
| 687 | */ |
| 688 | error = -EPERM; /* cannot change a locked bit */ |
| 689 | } else { |
| 690 | current->securebits = arg2; |
| 691 | } |
| 692 | break; |
| 693 | case PR_GET_SECUREBITS: |
| 694 | error = current->securebits; |
| 695 | break; |
| 696 | |
| 697 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 698 | |
| 699 | case PR_GET_KEEPCAPS: |
| 700 | if (issecure(SECURE_KEEP_CAPS)) |
| 701 | error = 1; |
| 702 | break; |
| 703 | case PR_SET_KEEPCAPS: |
| 704 | if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ |
| 705 | error = -EINVAL; |
| 706 | else if (issecure(SECURE_KEEP_CAPS_LOCKED)) |
| 707 | error = -EPERM; |
| 708 | else if (arg2) |
| 709 | current->securebits |= issecure_mask(SECURE_KEEP_CAPS); |
| 710 | else |
| 711 | current->securebits &= |
| 712 | ~issecure_mask(SECURE_KEEP_CAPS); |
| 713 | break; |
| 714 | |
| 715 | default: |
| 716 | /* No functionality available - continue with default */ |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /* Functionality provided */ |
| 721 | *rc_p = error; |
| 722 | return 1; |
| 723 | } |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | void cap_task_reparent_to_init (struct task_struct *p) |
| 726 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame] | 727 | cap_set_init_eff(p->cap_effective); |
| 728 | cap_clear(p->cap_inheritable); |
| 729 | cap_set_full(p->cap_permitted); |
Andrew G. Morgan | 3898b1b | 2008-04-28 02:13:40 -0700 | [diff] [blame] | 730 | p->securebits = SECUREBITS_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | return; |
| 732 | } |
| 733 | |
| 734 | int cap_syslog (int type) |
| 735 | { |
| 736 | if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN)) |
| 737 | return -EPERM; |
| 738 | return 0; |
| 739 | } |
| 740 | |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 741 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | { |
| 743 | int cap_sys_admin = 0; |
| 744 | |
| 745 | if (cap_capable(current, CAP_SYS_ADMIN) == 0) |
| 746 | cap_sys_admin = 1; |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 747 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |