blob: 51dfa11e8e56348d8c88ead0612df718e225a621 [file] [log] [blame]
Andrew Morgane338d262008-02-04 22:29:42 -08001/* Common capabilities, needed by capability.o and root_plug.o
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Eric Paris3fc689e2008-11-11 21:48:18 +110011#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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 Torvalds1da177e2005-04-16 15:20:36 -070021#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. Hallynb5376772007-10-16 23:31:36 -070026#include <linux/mount.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070027#include <linux/sched.h>
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -070028#include <linux/prctl.h>
29#include <linux/securebits.h>
Andrew Morgan72c2d582007-10-18 03:05:59 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
32{
David Howellsb6dff3e2008-11-14 10:39:16 +110033 NETLINK_CB(skb).eff_cap = current_cap();
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return 0;
35}
36
Darrel Goeddelc7bdb542006-06-27 13:26:11 -070037int cap_netlink_recv(struct sk_buff *skb, int cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Darrel Goeddelc7bdb542006-06-27 13:26:11 -070039 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return -EPERM;
41 return 0;
42}
43
44EXPORT_SYMBOL(cap_netlink_recv);
45
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -080046/*
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 Paris06112162008-11-11 22:02:50 +110052int cap_capable(struct task_struct *tsk, int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
David Howellsc69e8d92008-11-14 10:39:19 +110054 __u32 cap_raised;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* Derived from include/linux/sched.h:capable. */
David Howellsc69e8d92008-11-14 10:39:19 +110057 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 Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63int cap_settime(struct timespec *ts, struct timezone *tz)
64{
65 if (!capable(CAP_SYS_TIME))
66 return -EPERM;
67 return 0;
68}
69
David Howells5cd9c582008-08-14 11:37:28 +010070int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
David Howellsc69e8d92008-11-14 10:39:19 +110072 int ret = 0;
73
74 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +110075 if (!cap_issubset(__task_cred(child)->cap_permitted,
76 current_cred()->cap_permitted) &&
David Howellsc69e8d92008-11-14 10:39:19 +110077 !capable(CAP_SYS_PTRACE))
78 ret = -EPERM;
79 rcu_read_unlock();
80 return ret;
David Howells5cd9c582008-08-14 11:37:28 +010081}
82
83int cap_ptrace_traceme(struct task_struct *parent)
84{
David Howellsc69e8d92008-11-14 10:39:19 +110085 int ret = 0;
86
87 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +110088 if (!cap_issubset(current_cred()->cap_permitted,
89 __task_cred(parent)->cap_permitted) &&
David Howellsc69e8d92008-11-14 10:39:19 +110090 !has_capability(parent, CAP_SYS_PTRACE))
91 ret = -EPERM;
92 rcu_read_unlock();
93 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96int cap_capget (struct task_struct *target, kernel_cap_t *effective,
97 kernel_cap_t *inheritable, kernel_cap_t *permitted)
98{
David Howellsc69e8d92008-11-14 10:39:19 +110099 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100102 rcu_read_lock();
103 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100104 *effective = cred->cap_effective;
105 *inheritable = cred->cap_inheritable;
106 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100107 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
Andrew Morgan72c2d582007-10-18 03:05:59 -0700111#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
112
Andrew Morgan72c2d582007-10-18 03:05:59 -0700113static inline int cap_inh_is_capped(void)
114{
115 /*
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -0800116 * 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 Morgan72c2d582007-10-18 03:05:59 -0700119 */
David Howellsd84f4f92008-11-14 10:39:23 +1100120 return cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700121}
122
Andrew G. Morgan12097262008-07-04 09:59:59 -0700123static inline int cap_limit_ptraced_target(void) { return 1; }
124
Andrew Morgan72c2d582007-10-18 03:05:59 -0700125#else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
126
Andrew Morgan72c2d582007-10-18 03:05:59 -0700127static inline int cap_inh_is_capped(void) { return 1; }
Andrew G. Morgan12097262008-07-04 09:59:59 -0700128static inline int cap_limit_ptraced_target(void)
129{
130 return !capable(CAP_SETPCAP);
131}
Andrew Morgan72c2d582007-10-18 03:05:59 -0700132
133#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
134
David Howellsd84f4f92008-11-14 10:39:23 +1100135int 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 Torvalds1da177e2005-04-16 15:20:36 -0700140{
David Howellsd84f4f92008-11-14 10:39:23 +1100141 if (cap_inh_is_capped() &&
142 !cap_issubset(*inheritable,
143 cap_combine(old->cap_inheritable,
144 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700145 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100147
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800148 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100149 cap_combine(old->cap_inheritable,
150 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800151 /* no new pI capabilities outside bounding set */
152 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100155 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100159 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
David Howellsd84f4f92008-11-14 10:39:23 +1100162 new->cap_effective = *effective;
163 new->cap_inheritable = *inheritable;
164 new->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166}
167
Serge E. Hallynb5376772007-10-16 23:31:36 -0700168static inline void bprm_clear_caps(struct linux_binprm *bprm)
169{
David Howellsa6f76f22008-11-14 10:39:24 +1100170 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700171 bprm->cap_effective = false;
172}
173
174#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
175
176int 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
190int 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 Parisc0b00442008-11-11 21:48:10 +1100200static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100201 struct linux_binprm *bprm,
202 bool *effective)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700203{
David Howellsa6f76f22008-11-14 10:39:24 +1100204 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100205 unsigned i;
206 int ret = 0;
207
208 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100209 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100210
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 Howellsa6f76f22008-11-14 10:39:24 +1100218 new->cap_permitted.cap[i] =
219 (new->cap_bset.cap[i] & permitted) |
220 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100221
David Howellsa6f76f22008-11-14 10:39:24 +1100222 if (permitted & ~new->cap_permitted.cap[i])
223 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100224 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100225 }
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 Howellsa6f76f22008-11-14 10:39:24 +1100232 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100233}
234
235int 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. Hallynb5376772007-10-16 23:31:36 -0700238 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800239 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100240 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 Howellsa6f76f22008-11-14 10:39:24 +1100250 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100251 /* no data, that's ok */
252 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100253 if (size < 0)
254 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700255
Andrew Morgane338d262008-02-04 22:29:42 -0800256 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700257 return -EINVAL;
258
Eric Parisc0b00442008-11-11 21:48:10 +1100259 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700260
David Howellsa6f76f22008-11-14 10:39:24 +1100261 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800262 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. Hallynb5376772007-10-16 23:31:36 -0700272 default:
273 return -EINVAL;
274 }
Andrew Morgane338d262008-02-04 22:29:42 -0800275
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700276 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100277 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 Morgane338d262008-02-04 22:29:42 -0800281 }
David Howellsa6f76f22008-11-14 10:39:24 +1100282
Eric Parisc0b00442008-11-11 21:48:10 +1100283 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700284}
285
286/* Locate any VFS capabilities: */
David Howellsa6f76f22008-11-14 10:39:24 +1100287static int get_file_caps(struct linux_binprm *bprm, bool *effective)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700288{
289 struct dentry *dentry;
290 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100291 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700292
Serge Hallyn3318a382008-10-30 11:52:23 -0500293 bprm_clear_caps(bprm);
294
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600295 if (!file_caps_enabled)
296 return 0;
297
Serge Hallyn3318a382008-10-30 11:52:23 -0500298 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700299 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700300
301 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700302
Eric Parisc0b00442008-11-11 21:48:10 +1100303 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. Hallynb5376772007-10-16 23:31:36 -0700310 goto out;
311 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700312
David Howellsa6f76f22008-11-14 10:39:24 +1100313 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. Hallynb5376772007-10-16 23:31:36 -0700317
318out:
319 dput(dentry);
320 if (rc)
321 bprm_clear_caps(bprm);
322
323 return rc;
324}
325
326#else
327int cap_inode_need_killpriv(struct dentry *dentry)
328{
329 return 0;
330}
331
332int cap_inode_killpriv(struct dentry *dentry)
333{
334 return 0;
335}
336
David Howellsa6f76f22008-11-14 10:39:24 +1100337static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700338{
339 bprm_clear_caps(bprm);
340 return 0;
341}
342#endif
343
David Howellsa6f76f22008-11-14 10:39:24 +1100344/*
345 * set up the new credentials for an exec'd task
346 */
347int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
David Howellsa6f76f22008-11-14 10:39:24 +1100349 const struct cred *old = current_cred();
350 struct cred *new = bprm->cred;
351 bool effective;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700352 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David Howellsa6f76f22008-11-14 10:39:24 +1100354 effective = false;
355 ret = get_file_caps(bprm, &effective);
356 if (ret < 0)
357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700359 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 Howellsa6f76f22008-11-14 10:39:24 +1100365 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700366 */
David Howellsa6f76f22008-11-14 10:39:24 +1100367 if (new->euid == 0 || new->uid == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700368 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100369 new->cap_permitted = cap_combine(old->cap_bset,
370 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
David Howellsa6f76f22008-11-14 10:39:24 +1100372 if (new->euid == 0)
373 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700375
David Howellsa6f76f22008-11-14 10:39:24 +1100376 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700387 }
David Howellsa6f76f22008-11-14 10:39:24 +1100388 if (cap_limit_ptraced_target())
389 new->cap_permitted = cap_intersect(new->cap_permitted,
390 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
David Howellsa6f76f22008-11-14 10:39:24 +1100393 new->suid = new->fsuid = new->euid;
394 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
David Howellsa6f76f22008-11-14 10:39:24 +1100396 /* For init, we want to retain the capabilities set in the initial
397 * task. Thus we skip the usual capability rules
398 */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700399 if (!is_global_init(current)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100400 if (effective)
401 new->cap_effective = new->cap_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800402 else
David Howellsd84f4f92008-11-14 10:39:23 +1100403 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
David Howellsa6f76f22008-11-14 10:39:24 +1100405 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Eric Paris3fc689e2008-11-11 21:48:18 +1100407 /*
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 Howellsd84f4f92008-11-14 10:39:23 +1100419 if (!cap_isclear(new->cap_effective)) {
420 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100421 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 Paris3fc689e2008-11-11 21:48:18 +1100427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Howellsd84f4f92008-11-14 10:39:23 +1100429 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
David Howellsa6f76f22008-11-14 10:39:24 +1100430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
David Howellsa6f76f22008-11-14 10:39:24 +1100433/*
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 */
438int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
David Howellsc69e8d92008-11-14 10:39:19 +1100440 const struct cred *cred = current_cred();
David Howellsb6dff3e2008-11-14 10:39:16 +1100441
442 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700443 if (bprm->cap_effective)
444 return 1;
David Howellsa6f76f22008-11-14 10:39:24 +1100445 if (!cap_isclear(cred->cap_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700446 return 1;
447 }
448
David Howellsb6dff3e2008-11-14 10:39:16 +1100449 return (cred->euid != cred->uid ||
450 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
David Howells8f0cfa52008-04-29 00:59:41 -0700453int cap_inode_setxattr(struct dentry *dentry, const char *name,
454 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700456 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 Torvalds1da177e2005-04-16 15:20:36 -0700461 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
462 !capable(CAP_SYS_ADMIN))
463 return -EPERM;
464 return 0;
465}
466
David Howells8f0cfa52008-04-29 00:59:41 -0700467int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700469 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 Torvalds1da177e2005-04-16 15:20:36 -0700474 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 Howellsa6f76f22008-11-14 10:39:24 +1100481/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * 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 Howellsa6f76f22008-11-14 10:39:24 +1100495 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * never happen.
497 *
David Howellsa6f76f22008-11-14 10:39:24 +1100498 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
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 Howellsd84f4f92008-11-14 10:39:23 +1100510static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
David Howellsd84f4f92008-11-14 10:39:23 +1100512 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
513 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700514 !issecure(SECURE_KEEP_CAPS)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100515 cap_clear(new->cap_permitted);
516 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
David Howellsd84f4f92008-11-14 10:39:23 +1100518 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 Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
David Howellsd84f4f92008-11-14 10:39:23 +1100524int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
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 Howellsd84f4f92008-11-14 10:39:23 +1100531 if (!issecure(SECURE_NO_SETUID_FIXUP))
532 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534 case LSM_SETID_FS:
David Howellsd84f4f92008-11-14 10:39:23 +1100535 /* Copied from kernel/sys.c:setfsuid. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David Howellsd84f4f92008-11-14 10:39:23 +1100537 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700545 }
David Howellsd84f4f92008-11-14 10:39:23 +1100546 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 Torvalds1da177e2005-04-16 15:20:36 -0700551 }
David Howellsd84f4f92008-11-14 10:39:23 +1100552 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 default:
554 return -EINVAL;
555 }
556
557 return 0;
558}
559
Serge E. Hallynb5376772007-10-16 23:31:36 -0700560#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. Hallynde45e802008-09-26 22:27:47 -0400571static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700572{
David Howellsc69e8d92008-11-14 10:39:19 +1100573 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. Hallynb5376772007-10-16 23:31:36 -0700581 return -EPERM;
582 return 0;
583}
584
585int cap_task_setscheduler (struct task_struct *p, int policy,
586 struct sched_param *lp)
587{
588 return cap_safe_nice(p);
589}
590
591int cap_task_setioprio (struct task_struct *p, int ioprio)
592{
593 return cap_safe_nice(p);
594}
595
596int cap_task_setnice (struct task_struct *p, int nice)
597{
598 return cap_safe_nice(p);
599}
600
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800601/*
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 Howellsd84f4f92008-11-14 10:39:23 +1100608static long cap_prctl_drop(struct cred *new, unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800609{
610 if (!capable(CAP_SETPCAP))
611 return -EPERM;
612 if (!cap_valid(cap))
613 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100614
615 cap_lower(new->cap_bset, cap);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800616 return 0;
617}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700618
Serge E. Hallynb5376772007-10-16 23:31:36 -0700619#else
620int cap_task_setscheduler (struct task_struct *p, int policy,
621 struct sched_param *lp)
622{
623 return 0;
624}
625int cap_task_setioprio (struct task_struct *p, int ioprio)
626{
627 return 0;
628}
629int cap_task_setnice (struct task_struct *p, int nice)
630{
631 return 0;
632}
Serge E. Hallynb5376772007-10-16 23:31:36 -0700633#endif
634
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700635int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100636 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700637{
David Howellsd84f4f92008-11-14 10:39:23 +1100638 struct cred *new;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700639 long error = 0;
640
David Howellsd84f4f92008-11-14 10:39:23 +1100641 new = prepare_creds();
642 if (!new)
643 return -ENOMEM;
644
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700645 switch (option) {
646 case PR_CAPBSET_READ:
David Howellsd84f4f92008-11-14 10:39:23 +1100647 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700648 if (!cap_valid(arg2))
David Howellsd84f4f92008-11-14 10:39:23 +1100649 goto error;
650 error = !!cap_raised(new->cap_bset, arg2);
651 goto no_change;
652
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700653#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
654 case PR_CAPBSET_DROP:
David Howellsd84f4f92008-11-14 10:39:23 +1100655 error = cap_prctl_drop(new, arg2);
656 if (error < 0)
657 goto error;
658 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700659
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 Howellsd84f4f92008-11-14 10:39:23 +1100680 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. Morgan3898b1b2008-04-28 02:13:40 -0700686 /*
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 Howellsd84f4f92008-11-14 10:39:23 +1100693 )
694 /* cannot change a locked bit */
695 goto error;
696 new->securebits = arg2;
697 goto changed;
698
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700699 case PR_GET_SECUREBITS:
David Howellsd84f4f92008-11-14 10:39:23 +1100700 error = new->securebits;
701 goto no_change;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700702
703#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
704
705 case PR_GET_KEEPCAPS:
706 if (issecure(SECURE_KEEP_CAPS))
707 error = 1;
David Howellsd84f4f92008-11-14 10:39:23 +1100708 goto no_change;
709
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700710 case PR_SET_KEEPCAPS:
David Howellsd84f4f92008-11-14 10:39:23 +1100711 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700712 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
David Howellsd84f4f92008-11-14 10:39:23 +1100713 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. Morgan3898b1b2008-04-28 02:13:40 -0700719 else
David Howellsd84f4f92008-11-14 10:39:23 +1100720 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
721 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700722
723 default:
724 /* No functionality available - continue with default */
David Howellsd84f4f92008-11-14 10:39:23 +1100725 error = -ENOSYS;
726 goto error;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700727 }
728
729 /* Functionality provided */
David Howellsd84f4f92008-11-14 10:39:23 +1100730changed:
731 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700732
David Howellsd84f4f92008-11-14 10:39:23 +1100733no_change:
734 error = 0;
735error:
736 abort_creds(new);
737 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740int cap_syslog (int type)
741{
742 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
743 return -EPERM;
744 return 0;
745}
746
Alan Cox34b4e4a2007-08-22 14:01:28 -0700747int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 int cap_sys_admin = 0;
750
Eric Paris06112162008-11-11 22:02:50 +1100751 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700753 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}