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