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