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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 28 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 29 | /* |
| 30 | * Because of the reduced scope of CAP_SETPCAP when filesystem |
| 31 | * capabilities are in effect, it is safe to allow this capability to |
| 32 | * be available in the default configuration. |
| 33 | */ |
| 34 | # define CAP_INIT_BSET CAP_FULL_SET |
| 35 | #else /* ie. ndef CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 36 | # define CAP_INIT_BSET CAP_INIT_EFF_SET |
| 37 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 38 | |
| 39 | kernel_cap_t cap_bset = CAP_INIT_BSET; /* systemwide capability bound */ |
| 40 | EXPORT_SYMBOL(cap_bset); |
| 41 | |
| 42 | /* Global security state */ |
| 43 | |
| 44 | unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */ |
| 45 | EXPORT_SYMBOL(securebits); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | int cap_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 48 | { |
| 49 | NETLINK_CB(skb).eff_cap = current->cap_effective; |
| 50 | return 0; |
| 51 | } |
| 52 | |
Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 53 | int cap_netlink_recv(struct sk_buff *skb, int cap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 55 | if (!cap_raised(NETLINK_CB(skb).eff_cap, cap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return -EPERM; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | EXPORT_SYMBOL(cap_netlink_recv); |
| 61 | |
Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 62 | /* |
| 63 | * NOTE WELL: cap_capable() cannot be used like the kernel's capable() |
| 64 | * function. That is, it has the reverse semantics: cap_capable() |
| 65 | * returns 0 when a task has a capability, but the kernel's capable() |
| 66 | * returns 1 for this case. |
| 67 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | int cap_capable (struct task_struct *tsk, int cap) |
| 69 | { |
| 70 | /* Derived from include/linux/sched.h:capable. */ |
| 71 | if (cap_raised(tsk->cap_effective, cap)) |
| 72 | return 0; |
| 73 | return -EPERM; |
| 74 | } |
| 75 | |
| 76 | int cap_settime(struct timespec *ts, struct timezone *tz) |
| 77 | { |
| 78 | if (!capable(CAP_SYS_TIME)) |
| 79 | return -EPERM; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int cap_ptrace (struct task_struct *parent, struct task_struct *child) |
| 84 | { |
| 85 | /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ |
Chris Wright | d4eb82c | 2006-03-25 03:07:41 -0800 | [diff] [blame] | 86 | if (!cap_issubset(child->cap_permitted, parent->cap_permitted) && |
| 87 | !__capable(parent, CAP_SYS_PTRACE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return -EPERM; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int cap_capget (struct task_struct *target, kernel_cap_t *effective, |
| 93 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
| 94 | { |
| 95 | /* Derived from kernel/capability.c:sys_capget. */ |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 96 | *effective = target->cap_effective; |
| 97 | *inheritable = target->cap_inheritable; |
| 98 | *permitted = target->cap_permitted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 103 | |
| 104 | static inline int cap_block_setpcap(struct task_struct *target) |
| 105 | { |
| 106 | /* |
| 107 | * No support for remote process capability manipulation with |
| 108 | * filesystem capability support. |
| 109 | */ |
| 110 | return (target != current); |
| 111 | } |
| 112 | |
| 113 | static inline int cap_inh_is_capped(void) |
| 114 | { |
| 115 | /* |
Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 116 | * Return 1 if changes to the inheritable set are limited |
| 117 | * to the old permitted set. That is, if the current task |
| 118 | * does *not* possess the CAP_SETPCAP capability. |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 119 | */ |
Andrew G. Morgan | a6dbb1e | 2008-01-21 17:18:30 -0800 | [diff] [blame] | 120 | return (cap_capable(current, CAP_SETPCAP) != 0); |
Andrew Morgan | 72c2d58 | 2007-10-18 03:05:59 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */ |
| 124 | |
| 125 | static inline int cap_block_setpcap(struct task_struct *t) { return 0; } |
| 126 | static inline int cap_inh_is_capped(void) { return 1; } |
| 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 | } |
| 143 | |
| 144 | /* verify restrictions on target's new Permitted set */ |
| 145 | if (!cap_issubset (*permitted, |
| 146 | cap_combine (target->cap_permitted, |
| 147 | current->cap_permitted))) { |
| 148 | return -EPERM; |
| 149 | } |
| 150 | |
| 151 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ |
| 152 | if (!cap_issubset (*effective, *permitted)) { |
| 153 | return -EPERM; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void cap_capset_set (struct task_struct *target, kernel_cap_t *effective, |
| 160 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
| 161 | { |
| 162 | target->cap_effective = *effective; |
| 163 | target->cap_inheritable = *inheritable; |
| 164 | target->cap_permitted = *permitted; |
| 165 | } |
| 166 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 167 | static inline void bprm_clear_caps(struct linux_binprm *bprm) |
| 168 | { |
| 169 | cap_clear(bprm->cap_inheritable); |
| 170 | cap_clear(bprm->cap_permitted); |
| 171 | bprm->cap_effective = false; |
| 172 | } |
| 173 | |
| 174 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 175 | |
| 176 | int cap_inode_need_killpriv(struct dentry *dentry) |
| 177 | { |
| 178 | struct inode *inode = dentry->d_inode; |
| 179 | int error; |
| 180 | |
| 181 | if (!inode->i_op || !inode->i_op->getxattr) |
| 182 | return 0; |
| 183 | |
| 184 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); |
| 185 | if (error <= 0) |
| 186 | return 0; |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | int cap_inode_killpriv(struct dentry *dentry) |
| 191 | { |
| 192 | struct inode *inode = dentry->d_inode; |
| 193 | |
| 194 | if (!inode->i_op || !inode->i_op->removexattr) |
| 195 | return 0; |
| 196 | |
| 197 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); |
| 198 | } |
| 199 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 200 | static inline int cap_from_disk(struct vfs_cap_data *caps, |
| 201 | struct linux_binprm *bprm, unsigned size) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 202 | { |
| 203 | __u32 magic_etc; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 204 | unsigned tocopy, i; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 205 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 206 | if (size < sizeof(magic_etc)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 207 | return -EINVAL; |
| 208 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 209 | magic_etc = le32_to_cpu(caps->magic_etc); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 210 | |
| 211 | switch ((magic_etc & VFS_CAP_REVISION_MASK)) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 212 | case VFS_CAP_REVISION_1: |
| 213 | if (size != XATTR_CAPS_SZ_1) |
| 214 | return -EINVAL; |
| 215 | tocopy = VFS_CAP_U32_1; |
| 216 | break; |
| 217 | case VFS_CAP_REVISION_2: |
| 218 | if (size != XATTR_CAPS_SZ_2) |
| 219 | return -EINVAL; |
| 220 | tocopy = VFS_CAP_U32_2; |
| 221 | break; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 222 | default: |
| 223 | return -EINVAL; |
| 224 | } |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 225 | |
| 226 | if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE) { |
| 227 | bprm->cap_effective = true; |
| 228 | } else { |
| 229 | bprm->cap_effective = false; |
| 230 | } |
| 231 | |
| 232 | for (i = 0; i < tocopy; ++i) { |
| 233 | bprm->cap_permitted.cap[i] = |
| 234 | le32_to_cpu(caps->data[i].permitted); |
| 235 | bprm->cap_inheritable.cap[i] = |
| 236 | le32_to_cpu(caps->data[i].inheritable); |
| 237 | } |
| 238 | while (i < VFS_CAP_U32) { |
| 239 | bprm->cap_permitted.cap[i] = 0; |
| 240 | bprm->cap_inheritable.cap[i] = 0; |
| 241 | i++; |
| 242 | } |
| 243 | |
| 244 | return 0; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Locate any VFS capabilities: */ |
| 248 | static int get_file_caps(struct linux_binprm *bprm) |
| 249 | { |
| 250 | struct dentry *dentry; |
| 251 | int rc = 0; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 252 | struct vfs_cap_data vcaps; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 253 | struct inode *inode; |
| 254 | |
| 255 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) { |
| 256 | bprm_clear_caps(bprm); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | dentry = dget(bprm->file->f_dentry); |
| 261 | inode = dentry->d_inode; |
| 262 | if (!inode->i_op || !inode->i_op->getxattr) |
| 263 | goto out; |
| 264 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 265 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &vcaps, |
| 266 | XATTR_CAPS_SZ); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 267 | if (rc == -ENODATA || rc == -EOPNOTSUPP) { |
| 268 | /* no data, that's ok */ |
| 269 | rc = 0; |
| 270 | goto out; |
| 271 | } |
| 272 | if (rc < 0) |
| 273 | goto out; |
| 274 | |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 275 | rc = cap_from_disk(&vcaps, bprm, rc); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 276 | if (rc) |
| 277 | printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n", |
| 278 | __FUNCTION__, rc, bprm->filename); |
| 279 | |
| 280 | out: |
| 281 | dput(dentry); |
| 282 | if (rc) |
| 283 | bprm_clear_caps(bprm); |
| 284 | |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | #else |
| 289 | int cap_inode_need_killpriv(struct dentry *dentry) |
| 290 | { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int cap_inode_killpriv(struct dentry *dentry) |
| 295 | { |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static inline int get_file_caps(struct linux_binprm *bprm) |
| 300 | { |
| 301 | bprm_clear_caps(bprm); |
| 302 | return 0; |
| 303 | } |
| 304 | #endif |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | int cap_bprm_set_security (struct linux_binprm *bprm) |
| 307 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 308 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 310 | ret = get_file_caps(bprm); |
| 311 | if (ret) |
| 312 | printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n", |
| 313 | __FUNCTION__, ret, bprm->filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | /* To support inheritance of root-permissions and suid-root |
| 316 | * executables under compatibility mode, we raise all three |
| 317 | * capability sets for the file. |
| 318 | * |
| 319 | * If only the real uid is 0, we only raise the inheritable |
| 320 | * and permitted sets of the executable file. |
| 321 | */ |
| 322 | |
| 323 | if (!issecure (SECURE_NOROOT)) { |
| 324 | if (bprm->e_uid == 0 || current->uid == 0) { |
| 325 | cap_set_full (bprm->cap_inheritable); |
| 326 | cap_set_full (bprm->cap_permitted); |
| 327 | } |
| 328 | if (bprm->e_uid == 0) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 329 | bprm->cap_effective = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 331 | |
| 332 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe) |
| 336 | { |
| 337 | /* Derived from fs/exec.c:compute_creds. */ |
| 338 | kernel_cap_t new_permitted, working; |
| 339 | |
| 340 | new_permitted = cap_intersect (bprm->cap_permitted, cap_bset); |
| 341 | working = cap_intersect (bprm->cap_inheritable, |
| 342 | current->cap_inheritable); |
| 343 | new_permitted = cap_combine (new_permitted, working); |
| 344 | |
| 345 | if (bprm->e_uid != current->uid || bprm->e_gid != current->gid || |
| 346 | !cap_issubset (new_permitted, current->cap_permitted)) { |
Kawai, Hidehiro | 6c5d523 | 2007-07-19 01:48:27 -0700 | [diff] [blame] | 347 | set_dumpable(current->mm, suid_dumpable); |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 348 | current->pdeath_signal = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) { |
| 351 | if (!capable(CAP_SETUID)) { |
| 352 | bprm->e_uid = current->uid; |
| 353 | bprm->e_gid = current->gid; |
| 354 | } |
| 355 | if (!capable (CAP_SETPCAP)) { |
| 356 | new_permitted = cap_intersect (new_permitted, |
| 357 | current->cap_permitted); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | current->suid = current->euid = current->fsuid = bprm->e_uid; |
| 363 | current->sgid = current->egid = current->fsgid = bprm->e_gid; |
| 364 | |
| 365 | /* For init, we want to retain the capabilities set |
| 366 | * in the init_task struct. Thus we skip the usual |
| 367 | * capability rules */ |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 368 | if (!is_global_init(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | current->cap_permitted = new_permitted; |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 370 | if (bprm->cap_effective) |
| 371 | current->cap_effective = new_permitted; |
| 372 | else |
| 373 | cap_clear(current->cap_effective); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* AUD: Audit candidate if current->cap_effective is set */ |
| 377 | |
| 378 | current->keep_capabilities = 0; |
| 379 | } |
| 380 | |
| 381 | int cap_bprm_secureexec (struct linux_binprm *bprm) |
| 382 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 383 | if (current->uid != 0) { |
| 384 | if (bprm->cap_effective) |
| 385 | return 1; |
| 386 | if (!cap_isclear(bprm->cap_permitted)) |
| 387 | return 1; |
| 388 | if (!cap_isclear(bprm->cap_inheritable)) |
| 389 | return 1; |
| 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return (current->euid != current->uid || |
| 393 | current->egid != current->gid); |
| 394 | } |
| 395 | |
| 396 | int cap_inode_setxattr(struct dentry *dentry, char *name, void *value, |
| 397 | size_t size, int flags) |
| 398 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 399 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
| 400 | if (!capable(CAP_SETFCAP)) |
| 401 | return -EPERM; |
| 402 | return 0; |
| 403 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
| 405 | !capable(CAP_SYS_ADMIN)) |
| 406 | return -EPERM; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int cap_inode_removexattr(struct dentry *dentry, char *name) |
| 411 | { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 412 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
| 413 | if (!capable(CAP_SETFCAP)) |
| 414 | return -EPERM; |
| 415 | return 0; |
| 416 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
| 418 | !capable(CAP_SYS_ADMIN)) |
| 419 | return -EPERM; |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* moved from kernel/sys.c. */ |
| 424 | /* |
| 425 | * cap_emulate_setxuid() fixes the effective / permitted capabilities of |
| 426 | * a process after a call to setuid, setreuid, or setresuid. |
| 427 | * |
| 428 | * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of |
| 429 | * {r,e,s}uid != 0, the permitted and effective capabilities are |
| 430 | * cleared. |
| 431 | * |
| 432 | * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective |
| 433 | * capabilities of the process are cleared. |
| 434 | * |
| 435 | * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective |
| 436 | * capabilities are set to the permitted capabilities. |
| 437 | * |
| 438 | * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should |
| 439 | * never happen. |
| 440 | * |
| 441 | * -astor |
| 442 | * |
| 443 | * cevans - New behaviour, Oct '99 |
| 444 | * A process may, via prctl(), elect to keep its capabilities when it |
| 445 | * calls setuid() and switches away from uid==0. Both permitted and |
| 446 | * effective sets will be retained. |
| 447 | * Without this change, it was impossible for a daemon to drop only some |
| 448 | * of its privilege. The call to setuid(!=0) would drop all privileges! |
| 449 | * Keeping uid 0 is not an option because uid 0 owns too many vital |
| 450 | * files.. |
| 451 | * Thanks to Olaf Kirch and Peter Benie for spotting this. |
| 452 | */ |
| 453 | static inline void cap_emulate_setxuid (int old_ruid, int old_euid, |
| 454 | int old_suid) |
| 455 | { |
| 456 | if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) && |
| 457 | (current->uid != 0 && current->euid != 0 && current->suid != 0) && |
| 458 | !current->keep_capabilities) { |
| 459 | cap_clear (current->cap_permitted); |
| 460 | cap_clear (current->cap_effective); |
| 461 | } |
| 462 | if (old_euid == 0 && current->euid != 0) { |
| 463 | cap_clear (current->cap_effective); |
| 464 | } |
| 465 | if (old_euid != 0 && current->euid == 0) { |
| 466 | current->cap_effective = current->cap_permitted; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid, |
| 471 | int flags) |
| 472 | { |
| 473 | switch (flags) { |
| 474 | case LSM_SETID_RE: |
| 475 | case LSM_SETID_ID: |
| 476 | case LSM_SETID_RES: |
| 477 | /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */ |
| 478 | if (!issecure (SECURE_NO_SETUID_FIXUP)) { |
| 479 | cap_emulate_setxuid (old_ruid, old_euid, old_suid); |
| 480 | } |
| 481 | break; |
| 482 | case LSM_SETID_FS: |
| 483 | { |
| 484 | uid_t old_fsuid = old_ruid; |
| 485 | |
| 486 | /* Copied from kernel/sys.c:setfsuid. */ |
| 487 | |
| 488 | /* |
| 489 | * FIXME - is fsuser used for all CAP_FS_MASK capabilities? |
| 490 | * if not, we might be a bit too harsh here. |
| 491 | */ |
| 492 | |
| 493 | if (!issecure (SECURE_NO_SETUID_FIXUP)) { |
| 494 | if (old_fsuid == 0 && current->fsuid != 0) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 495 | current->cap_effective = |
| 496 | cap_drop_fs_set( |
| 497 | current->cap_effective); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | if (old_fsuid != 0 && current->fsuid == 0) { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 500 | current->cap_effective = |
| 501 | cap_raise_fs_set( |
| 502 | current->cap_effective, |
| 503 | current->cap_permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | default: |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 515 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
| 516 | /* |
| 517 | * Rationale: code calling task_setscheduler, task_setioprio, and |
| 518 | * task_setnice, assumes that |
| 519 | * . if capable(cap_sys_nice), then those actions should be allowed |
| 520 | * . if not capable(cap_sys_nice), but acting on your own processes, |
| 521 | * then those actions should be allowed |
| 522 | * This is insufficient now since you can call code without suid, but |
| 523 | * yet with increased caps. |
| 524 | * So we check for increased caps on the target process. |
| 525 | */ |
| 526 | static inline int cap_safe_nice(struct task_struct *p) |
| 527 | { |
| 528 | if (!cap_issubset(p->cap_permitted, current->cap_permitted) && |
| 529 | !__capable(current, CAP_SYS_NICE)) |
| 530 | return -EPERM; |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | int cap_task_setscheduler (struct task_struct *p, int policy, |
| 535 | struct sched_param *lp) |
| 536 | { |
| 537 | return cap_safe_nice(p); |
| 538 | } |
| 539 | |
| 540 | int cap_task_setioprio (struct task_struct *p, int ioprio) |
| 541 | { |
| 542 | return cap_safe_nice(p); |
| 543 | } |
| 544 | |
| 545 | int cap_task_setnice (struct task_struct *p, int nice) |
| 546 | { |
| 547 | return cap_safe_nice(p); |
| 548 | } |
| 549 | |
| 550 | int cap_task_kill(struct task_struct *p, struct siginfo *info, |
| 551 | int sig, u32 secid) |
| 552 | { |
| 553 | if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) |
| 554 | return 0; |
| 555 | |
Serge E. Hallyn | 8ec2328 | 2007-11-28 16:21:47 -0800 | [diff] [blame] | 556 | /* |
| 557 | * Running a setuid root program raises your capabilities. |
| 558 | * Killing your own setuid root processes was previously |
| 559 | * allowed. |
| 560 | * We must preserve legacy signal behavior in this case. |
| 561 | */ |
| 562 | if (p->euid == 0 && p->uid == current->uid) |
| 563 | return 0; |
| 564 | |
Serge E. Hallyn | 91ad997 | 2007-11-14 17:00:34 -0800 | [diff] [blame] | 565 | /* sigcont is permitted within same session */ |
| 566 | if (sig == SIGCONT && (task_session_nr(current) == task_session_nr(p))) |
| 567 | return 0; |
| 568 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 569 | if (secid) |
| 570 | /* |
| 571 | * Signal sent as a particular user. |
| 572 | * Capabilities are ignored. May be wrong, but it's the |
| 573 | * only thing we can do at the moment. |
| 574 | * Used only by usb drivers? |
| 575 | */ |
| 576 | return 0; |
| 577 | if (cap_issubset(p->cap_permitted, current->cap_permitted)) |
| 578 | return 0; |
| 579 | if (capable(CAP_KILL)) |
| 580 | return 0; |
| 581 | |
| 582 | return -EPERM; |
| 583 | } |
| 584 | #else |
| 585 | int cap_task_setscheduler (struct task_struct *p, int policy, |
| 586 | struct sched_param *lp) |
| 587 | { |
| 588 | return 0; |
| 589 | } |
| 590 | int cap_task_setioprio (struct task_struct *p, int ioprio) |
| 591 | { |
| 592 | return 0; |
| 593 | } |
| 594 | int cap_task_setnice (struct task_struct *p, int nice) |
| 595 | { |
| 596 | return 0; |
| 597 | } |
| 598 | int cap_task_kill(struct task_struct *p, struct siginfo *info, |
| 599 | int sig, u32 secid) |
| 600 | { |
| 601 | return 0; |
| 602 | } |
| 603 | #endif |
| 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | void cap_task_reparent_to_init (struct task_struct *p) |
| 606 | { |
Andrew Morgan | e338d26 | 2008-02-04 22:29:42 -0800 | [diff] [blame^] | 607 | cap_set_init_eff(p->cap_effective); |
| 608 | cap_clear(p->cap_inheritable); |
| 609 | cap_set_full(p->cap_permitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | p->keep_capabilities = 0; |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | int cap_syslog (int type) |
| 615 | { |
| 616 | if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN)) |
| 617 | return -EPERM; |
| 618 | return 0; |
| 619 | } |
| 620 | |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 621 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
| 623 | int cap_sys_admin = 0; |
| 624 | |
| 625 | if (cap_capable(current, CAP_SYS_ADMIN) == 0) |
| 626 | cap_sys_admin = 1; |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 627 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |