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