blob: 0384bf95db68dc573d3a6d5f84f43f9dbc07d012 [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();
75 if (!cap_issubset(child->cred->cap_permitted,
76 current->cred->cap_permitted) &&
77 !capable(CAP_SYS_PTRACE))
78 ret = -EPERM;
79 rcu_read_unlock();
80 return ret;
David 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();
88 if (!cap_issubset(current->cred->cap_permitted,
89 parent->cred->cap_permitted) &&
90 !has_capability(parent, CAP_SYS_PTRACE))
91 ret = -EPERM;
92 rcu_read_unlock();
93 return ret;
Linus 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 */
Eric Paris06112162008-11-11 22:02:50 +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 Howells15a24602008-11-14 10:39:15 +1100135int cap_capset_check(const kernel_cap_t *effective,
136 const kernel_cap_t *inheritable,
137 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
David Howellsb6dff3e2008-11-14 10:39:16 +1100139 const struct cred *cred = current->cred;
140
Andrew Morgan72c2d582007-10-18 03:05:59 -0700141 if (cap_inh_is_capped()
142 && !cap_issubset(*inheritable,
David Howellsb6dff3e2008-11-14 10:39:16 +1100143 cap_combine(cred->cap_inheritable,
144 cred->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;
147 }
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800148 if (!cap_issubset(*inheritable,
David Howellsb6dff3e2008-11-14 10:39:16 +1100149 cap_combine(cred->cap_inheritable,
150 cred->cap_bset))) {
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800151 /* no new pI capabilities outside bounding set */
152 return -EPERM;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* verify restrictions on target's new Permitted set */
156 if (!cap_issubset (*permitted,
David Howellsb6dff3e2008-11-14 10:39:16 +1100157 cap_combine (cred->cap_permitted,
158 cred->cap_permitted))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -EPERM;
160 }
161
162 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
163 if (!cap_issubset (*effective, *permitted)) {
164 return -EPERM;
165 }
166
167 return 0;
168}
169
David Howells15a24602008-11-14 10:39:15 +1100170void cap_capset_set(const kernel_cap_t *effective,
171 const kernel_cap_t *inheritable,
172 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
David Howellsb6dff3e2008-11-14 10:39:16 +1100174 struct cred *cred = current->cred;
175
176 cred->cap_effective = *effective;
177 cred->cap_inheritable = *inheritable;
178 cred->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Serge E. Hallynb5376772007-10-16 23:31:36 -0700181static inline void bprm_clear_caps(struct linux_binprm *bprm)
182{
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700183 cap_clear(bprm->cap_post_exec_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700184 bprm->cap_effective = false;
185}
186
187#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
188
189int cap_inode_need_killpriv(struct dentry *dentry)
190{
191 struct inode *inode = dentry->d_inode;
192 int error;
193
194 if (!inode->i_op || !inode->i_op->getxattr)
195 return 0;
196
197 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
198 if (error <= 0)
199 return 0;
200 return 1;
201}
202
203int cap_inode_killpriv(struct dentry *dentry)
204{
205 struct inode *inode = dentry->d_inode;
206
207 if (!inode->i_op || !inode->i_op->removexattr)
208 return 0;
209
210 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
211}
212
Eric Parisc0b00442008-11-11 21:48:10 +1100213static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
214 struct linux_binprm *bprm)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700215{
Eric Parisc0b00442008-11-11 21:48:10 +1100216 unsigned i;
217 int ret = 0;
218
219 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
220 bprm->cap_effective = true;
221 else
222 bprm->cap_effective = false;
223
224 CAP_FOR_EACH_U32(i) {
225 __u32 permitted = caps->permitted.cap[i];
226 __u32 inheritable = caps->inheritable.cap[i];
227
228 /*
229 * pP' = (X & fP) | (pI & fI)
230 */
231 bprm->cap_post_exec_permitted.cap[i] =
David Howellsb6dff3e2008-11-14 10:39:16 +1100232 (current->cred->cap_bset.cap[i] & permitted) |
233 (current->cred->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100234
235 if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
236 /*
237 * insufficient to execute correctly
238 */
239 ret = -EPERM;
240 }
241 }
242
243 /*
244 * For legacy apps, with no internal support for recognizing they
245 * do not have enough capabilities, we return an error if they are
246 * missing some "forced" (aka file-permitted) capabilities.
247 */
248 return bprm->cap_effective ? ret : 0;
249}
250
251int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
252{
253 struct inode *inode = dentry->d_inode;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700254 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800255 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100256 int size;
257 struct vfs_cap_data caps;
258
259 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
260
261 if (!inode || !inode->i_op || !inode->i_op->getxattr)
262 return -ENODATA;
263
264 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
265 XATTR_CAPS_SZ);
266 if (size == -ENODATA || size == -EOPNOTSUPP) {
267 /* no data, that's ok */
268 return -ENODATA;
269 }
270 if (size < 0)
271 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700272
Andrew Morgane338d262008-02-04 22:29:42 -0800273 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700274 return -EINVAL;
275
Eric Parisc0b00442008-11-11 21:48:10 +1100276 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700277
278 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
Andrew Morgane338d262008-02-04 22:29:42 -0800279 case VFS_CAP_REVISION_1:
280 if (size != XATTR_CAPS_SZ_1)
281 return -EINVAL;
282 tocopy = VFS_CAP_U32_1;
283 break;
284 case VFS_CAP_REVISION_2:
285 if (size != XATTR_CAPS_SZ_2)
286 return -EINVAL;
287 tocopy = VFS_CAP_U32_2;
288 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700289 default:
290 return -EINVAL;
291 }
Andrew Morgane338d262008-02-04 22:29:42 -0800292
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700293 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100294 if (i >= tocopy)
295 break;
296 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
297 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800298 }
Eric Parisc0b00442008-11-11 21:48:10 +1100299 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700300}
301
302/* Locate any VFS capabilities: */
303static int get_file_caps(struct linux_binprm *bprm)
304{
305 struct dentry *dentry;
306 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100307 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700308
Serge Hallyn3318a382008-10-30 11:52:23 -0500309 bprm_clear_caps(bprm);
310
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600311 if (!file_caps_enabled)
312 return 0;
313
Serge Hallyn3318a382008-10-30 11:52:23 -0500314 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700315 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700316
317 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700318
Eric Parisc0b00442008-11-11 21:48:10 +1100319 rc = get_vfs_caps_from_disk(dentry, &vcaps);
320 if (rc < 0) {
321 if (rc == -EINVAL)
322 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
323 __func__, rc, bprm->filename);
324 else if (rc == -ENODATA)
325 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700326 goto out;
327 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700328
Eric Parisc0b00442008-11-11 21:48:10 +1100329 rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700330
331out:
332 dput(dentry);
333 if (rc)
334 bprm_clear_caps(bprm);
335
336 return rc;
337}
338
339#else
340int cap_inode_need_killpriv(struct dentry *dentry)
341{
342 return 0;
343}
344
345int cap_inode_killpriv(struct dentry *dentry)
346{
347 return 0;
348}
349
350static inline int get_file_caps(struct linux_binprm *bprm)
351{
352 bprm_clear_caps(bprm);
353 return 0;
354}
355#endif
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357int cap_bprm_set_security (struct linux_binprm *bprm)
358{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700359 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Serge E. Hallynb5376772007-10-16 23:31:36 -0700361 ret = get_file_caps(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700363 if (!issecure(SECURE_NOROOT)) {
364 /*
365 * To support inheritance of root-permissions and suid-root
366 * executables under compatibility mode, we override the
367 * capability sets for the file.
368 *
369 * If only the real uid is 0, we do not set the effective
370 * bit.
371 */
David Howellsb103c592008-11-14 10:39:11 +1100372 if (bprm->e_uid == 0 || current_uid() == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700373 /* pP' = (cap_bset & ~0) | (pI & ~0) */
374 bprm->cap_post_exec_permitted = cap_combine(
David Howellsb6dff3e2008-11-14 10:39:16 +1100375 current->cred->cap_bset,
376 current->cred->cap_inheritable);
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700377 bprm->cap_effective = (bprm->e_uid == 0);
378 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700381
382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
386{
David Howellsb6dff3e2008-11-14 10:39:16 +1100387 struct cred *cred = current->cred;
Eric Paris3fc689e2008-11-11 21:48:18 +1100388
David Howellsb6dff3e2008-11-14 10:39:16 +1100389 if (bprm->e_uid != cred->uid || bprm->e_gid != cred->gid ||
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700390 !cap_issubset(bprm->cap_post_exec_permitted,
David Howellsb6dff3e2008-11-14 10:39:16 +1100391 cred->cap_permitted)) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700392 set_dumpable(current->mm, suid_dumpable);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700393 current->pdeath_signal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
396 if (!capable(CAP_SETUID)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100397 bprm->e_uid = cred->uid;
398 bprm->e_gid = cred->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Andrew G. Morgan12097262008-07-04 09:59:59 -0700400 if (cap_limit_ptraced_target()) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700401 bprm->cap_post_exec_permitted = cap_intersect(
402 bprm->cap_post_exec_permitted,
David Howellsb6dff3e2008-11-14 10:39:16 +1100403 cred->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
406 }
407
David Howellsb6dff3e2008-11-14 10:39:16 +1100408 cred->suid = cred->euid = cred->fsuid = bprm->e_uid;
409 cred->sgid = cred->egid = cred->fsgid = bprm->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* For init, we want to retain the capabilities set
412 * in the init_task struct. Thus we skip the usual
413 * capability rules */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700414 if (!is_global_init(current)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100415 cred->cap_permitted = bprm->cap_post_exec_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800416 if (bprm->cap_effective)
David Howellsb6dff3e2008-11-14 10:39:16 +1100417 cred->cap_effective = bprm->cap_post_exec_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800418 else
David Howellsb6dff3e2008-11-14 10:39:16 +1100419 cap_clear(cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Eric Paris3fc689e2008-11-11 21:48:18 +1100422 /*
423 * Audit candidate if current->cap_effective is set
424 *
425 * We do not bother to audit if 3 things are true:
426 * 1) cap_effective has all caps
427 * 2) we are root
428 * 3) root is supposed to have all caps (SECURE_NOROOT)
429 * Since this is just a normal root execing a process.
430 *
431 * Number 1 above might fail if you don't have a full bset, but I think
432 * that is interesting information to audit.
433 */
David Howellsb6dff3e2008-11-14 10:39:16 +1100434 if (!cap_isclear(cred->cap_effective)) {
435 if (!cap_issubset(CAP_FULL_SET, cred->cap_effective) ||
436 (bprm->e_uid != 0) || (cred->uid != 0) ||
Eric Paris3fc689e2008-11-11 21:48:18 +1100437 issecure(SECURE_NOROOT))
David Howellsb6dff3e2008-11-14 10:39:16 +1100438 audit_log_bprm_fcaps(bprm, &cred->cap_permitted,
439 &cred->cap_effective);
Eric Paris3fc689e2008-11-11 21:48:18 +1100440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
David Howellsb6dff3e2008-11-14 10:39:16 +1100442 cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445int cap_bprm_secureexec (struct linux_binprm *bprm)
446{
David Howellsc69e8d92008-11-14 10:39:19 +1100447 const struct cred *cred = current_cred();
David Howellsb6dff3e2008-11-14 10:39:16 +1100448
449 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700450 if (bprm->cap_effective)
451 return 1;
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700452 if (!cap_isclear(bprm->cap_post_exec_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700453 return 1;
454 }
455
David Howellsb6dff3e2008-11-14 10:39:16 +1100456 return (cred->euid != cred->uid ||
457 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
David Howells8f0cfa52008-04-29 00:59:41 -0700460int cap_inode_setxattr(struct dentry *dentry, const char *name,
461 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700463 if (!strcmp(name, XATTR_NAME_CAPS)) {
464 if (!capable(CAP_SETFCAP))
465 return -EPERM;
466 return 0;
467 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
469 !capable(CAP_SYS_ADMIN))
470 return -EPERM;
471 return 0;
472}
473
David Howells8f0cfa52008-04-29 00:59:41 -0700474int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700476 if (!strcmp(name, XATTR_NAME_CAPS)) {
477 if (!capable(CAP_SETFCAP))
478 return -EPERM;
479 return 0;
480 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
482 !capable(CAP_SYS_ADMIN))
483 return -EPERM;
484 return 0;
485}
486
487/* moved from kernel/sys.c. */
488/*
489 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
490 * a process after a call to setuid, setreuid, or setresuid.
491 *
492 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
493 * {r,e,s}uid != 0, the permitted and effective capabilities are
494 * cleared.
495 *
496 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
497 * capabilities of the process are cleared.
498 *
499 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
500 * capabilities are set to the permitted capabilities.
501 *
502 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
503 * never happen.
504 *
505 * -astor
506 *
507 * cevans - New behaviour, Oct '99
508 * A process may, via prctl(), elect to keep its capabilities when it
509 * calls setuid() and switches away from uid==0. Both permitted and
510 * effective sets will be retained.
511 * Without this change, it was impossible for a daemon to drop only some
512 * of its privilege. The call to setuid(!=0) would drop all privileges!
513 * Keeping uid 0 is not an option because uid 0 owns too many vital
514 * files..
515 * Thanks to Olaf Kirch and Peter Benie for spotting this.
516 */
517static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
518 int old_suid)
519{
David Howellsb6dff3e2008-11-14 10:39:16 +1100520 struct cred *cred = current->cred;
David Howellsb103c592008-11-14 10:39:11 +1100521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
David Howellsb6dff3e2008-11-14 10:39:16 +1100523 (cred->uid != 0 && cred->euid != 0 && cred->suid != 0) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700524 !issecure(SECURE_KEEP_CAPS)) {
David Howellsc69e8d92008-11-14 10:39:19 +1100525 cap_clear(cred->cap_permitted);
526 cap_clear(cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100528 if (old_euid == 0 && cred->euid != 0) {
David Howellsc69e8d92008-11-14 10:39:19 +1100529 cap_clear(cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100531 if (old_euid != 0 && cred->euid == 0) {
532 cred->cap_effective = cred->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534}
535
536int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
537 int flags)
538{
David Howellsb6dff3e2008-11-14 10:39:16 +1100539 struct cred *cred = current->cred;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 switch (flags) {
542 case LSM_SETID_RE:
543 case LSM_SETID_ID:
544 case LSM_SETID_RES:
545 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
546 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
547 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
548 }
549 break;
550 case LSM_SETID_FS:
551 {
552 uid_t old_fsuid = old_ruid;
553
554 /* Copied from kernel/sys.c:setfsuid. */
555
556 /*
557 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
558 * if not, we might be a bit too harsh here.
559 */
560
561 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100562 if (old_fsuid == 0 && cred->fsuid != 0) {
563 cred->cap_effective =
Andrew Morgane338d262008-02-04 22:29:42 -0800564 cap_drop_fs_set(
David Howellsb6dff3e2008-11-14 10:39:16 +1100565 cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100567 if (old_fsuid != 0 && cred->fsuid == 0) {
568 cred->cap_effective =
Andrew Morgane338d262008-02-04 22:29:42 -0800569 cap_raise_fs_set(
David Howellsb6dff3e2008-11-14 10:39:16 +1100570 cred->cap_effective,
571 cred->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 }
574 break;
575 }
576 default:
577 return -EINVAL;
578 }
579
580 return 0;
581}
582
Serge E. Hallynb5376772007-10-16 23:31:36 -0700583#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
584/*
585 * Rationale: code calling task_setscheduler, task_setioprio, and
586 * task_setnice, assumes that
587 * . if capable(cap_sys_nice), then those actions should be allowed
588 * . if not capable(cap_sys_nice), but acting on your own processes,
589 * then those actions should be allowed
590 * This is insufficient now since you can call code without suid, but
591 * yet with increased caps.
592 * So we check for increased caps on the target process.
593 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400594static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700595{
David Howellsc69e8d92008-11-14 10:39:19 +1100596 int is_subset;
597
598 rcu_read_lock();
599 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
600 current_cred()->cap_permitted);
601 rcu_read_unlock();
602
603 if (!is_subset && !capable(CAP_SYS_NICE))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700604 return -EPERM;
605 return 0;
606}
607
608int cap_task_setscheduler (struct task_struct *p, int policy,
609 struct sched_param *lp)
610{
611 return cap_safe_nice(p);
612}
613
614int cap_task_setioprio (struct task_struct *p, int ioprio)
615{
616 return cap_safe_nice(p);
617}
618
619int cap_task_setnice (struct task_struct *p, int nice)
620{
621 return cap_safe_nice(p);
622}
623
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800624/*
625 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
626 * done without task_capability_lock() because it introduces
627 * no new races - i.e. only another task doing capget() on
628 * this task could get inconsistent info. There can be no
629 * racing writer bc a task can only change its own caps.
630 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700631static long cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800632{
633 if (!capable(CAP_SETPCAP))
634 return -EPERM;
635 if (!cap_valid(cap))
636 return -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +1100637 cap_lower(current->cred->cap_bset, cap);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800638 return 0;
639}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700640
Serge E. Hallynb5376772007-10-16 23:31:36 -0700641#else
642int cap_task_setscheduler (struct task_struct *p, int policy,
643 struct sched_param *lp)
644{
645 return 0;
646}
647int cap_task_setioprio (struct task_struct *p, int ioprio)
648{
649 return 0;
650}
651int cap_task_setnice (struct task_struct *p, int nice)
652{
653 return 0;
654}
Serge E. Hallynb5376772007-10-16 23:31:36 -0700655#endif
656
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700657int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
658 unsigned long arg4, unsigned long arg5, long *rc_p)
659{
David Howells86a264a2008-11-14 10:39:18 +1100660 struct cred *cred = current_cred();
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700661 long error = 0;
662
663 switch (option) {
664 case PR_CAPBSET_READ:
665 if (!cap_valid(arg2))
666 error = -EINVAL;
667 else
David Howellsb6dff3e2008-11-14 10:39:16 +1100668 error = !!cap_raised(cred->cap_bset, arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700669 break;
670#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
671 case PR_CAPBSET_DROP:
672 error = cap_prctl_drop(arg2);
673 break;
674
675 /*
676 * The next four prctl's remain to assist with transitioning a
677 * system from legacy UID=0 based privilege (when filesystem
678 * capabilities are not in use) to a system using filesystem
679 * capabilities only - as the POSIX.1e draft intended.
680 *
681 * Note:
682 *
683 * PR_SET_SECUREBITS =
684 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
685 * | issecure_mask(SECURE_NOROOT)
686 * | issecure_mask(SECURE_NOROOT_LOCKED)
687 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
688 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
689 *
690 * will ensure that the current process and all of its
691 * children will be locked into a pure
692 * capability-based-privilege environment.
693 */
694 case PR_SET_SECUREBITS:
David Howellsb6dff3e2008-11-14 10:39:16 +1100695 if ((((cred->securebits & SECURE_ALL_LOCKS) >> 1)
696 & (cred->securebits ^ arg2)) /*[1]*/
697 || ((cred->securebits & SECURE_ALL_LOCKS
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700698 & ~arg2)) /*[2]*/
699 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris06112162008-11-11 22:02:50 +1100700 || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700701 /*
702 * [1] no changing of bits that are locked
703 * [2] no unlocking of locks
704 * [3] no setting of unsupported bits
705 * [4] doing anything requires privilege (go read about
706 * the "sendmail capabilities bug")
707 */
708 error = -EPERM; /* cannot change a locked bit */
709 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100710 cred->securebits = arg2;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700711 }
712 break;
713 case PR_GET_SECUREBITS:
David Howellsb6dff3e2008-11-14 10:39:16 +1100714 error = cred->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700715 break;
716
717#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
718
719 case PR_GET_KEEPCAPS:
720 if (issecure(SECURE_KEEP_CAPS))
721 error = 1;
722 break;
723 case PR_SET_KEEPCAPS:
724 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
725 error = -EINVAL;
726 else if (issecure(SECURE_KEEP_CAPS_LOCKED))
727 error = -EPERM;
728 else if (arg2)
David Howellsb6dff3e2008-11-14 10:39:16 +1100729 cred->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700730 else
David Howellsb6dff3e2008-11-14 10:39:16 +1100731 cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700732 break;
733
734 default:
735 /* No functionality available - continue with default */
736 return 0;
737 }
738
739 /* Functionality provided */
740 *rc_p = error;
741 return 1;
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744void cap_task_reparent_to_init (struct task_struct *p)
745{
David Howellsb6dff3e2008-11-14 10:39:16 +1100746 struct cred *cred = p->cred;
747
748 cap_set_init_eff(cred->cap_effective);
749 cap_clear(cred->cap_inheritable);
750 cap_set_full(cred->cap_permitted);
751 p->cred->securebits = SECUREBITS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754int cap_syslog (int type)
755{
756 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
757 return -EPERM;
758 return 0;
759}
760
Alan Cox34b4e4a2007-08-22 14:01:28 -0700761int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 int cap_sys_admin = 0;
764
Eric Paris06112162008-11-11 22:02:50 +1100765 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700767 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769