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