blob: fa61679f8c7371189da66d94049abf4b2bbf7906 [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{
54 /* Derived from include/linux/sched.h:capable. */
David Howellsb6dff3e2008-11-14 10:39:16 +110055 if (cap_raised(tsk->cred->cap_effective, cap))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return 0;
57 return -EPERM;
58}
59
60int cap_settime(struct timespec *ts, struct timezone *tz)
61{
62 if (!capable(CAP_SYS_TIME))
63 return -EPERM;
64 return 0;
65}
66
David Howells5cd9c582008-08-14 11:37:28 +010067int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
David Howellsb6dff3e2008-11-14 10:39:16 +110070 if (cap_issubset(child->cred->cap_permitted,
71 current->cred->cap_permitted))
David Howells5cd9c582008-08-14 11:37:28 +010072 return 0;
73 if (capable(CAP_SYS_PTRACE))
74 return 0;
75 return -EPERM;
76}
77
78int cap_ptrace_traceme(struct task_struct *parent)
79{
David Howellsb6dff3e2008-11-14 10:39:16 +110080 if (cap_issubset(current->cred->cap_permitted,
81 parent->cred->cap_permitted))
David Howells5cd9c582008-08-14 11:37:28 +010082 return 0;
83 if (has_capability(parent, CAP_SYS_PTRACE))
84 return 0;
85 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88int cap_capget (struct task_struct *target, kernel_cap_t *effective,
89 kernel_cap_t *inheritable, kernel_cap_t *permitted)
90{
David Howellsb6dff3e2008-11-14 10:39:16 +110091 struct cred *cred = target->cred;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* Derived from kernel/capability.c:sys_capget. */
David Howellsb6dff3e2008-11-14 10:39:16 +110094 *effective = cred->cap_effective;
95 *inheritable = cred->cap_inheritable;
96 *permitted = cred->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98}
99
Andrew Morgan72c2d582007-10-18 03:05:59 -0700100#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
101
Andrew Morgan72c2d582007-10-18 03:05:59 -0700102static inline int cap_inh_is_capped(void)
103{
104 /*
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -0800105 * Return 1 if changes to the inheritable set are limited
106 * to the old permitted set. That is, if the current task
107 * does *not* possess the CAP_SETPCAP capability.
Andrew Morgan72c2d582007-10-18 03:05:59 -0700108 */
Eric Paris06112162008-11-11 22:02:50 +1100109 return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0);
Andrew Morgan72c2d582007-10-18 03:05:59 -0700110}
111
Andrew G. Morgan12097262008-07-04 09:59:59 -0700112static inline int cap_limit_ptraced_target(void) { return 1; }
113
Andrew Morgan72c2d582007-10-18 03:05:59 -0700114#else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
115
Andrew Morgan72c2d582007-10-18 03:05:59 -0700116static inline int cap_inh_is_capped(void) { return 1; }
Andrew G. Morgan12097262008-07-04 09:59:59 -0700117static inline int cap_limit_ptraced_target(void)
118{
119 return !capable(CAP_SETPCAP);
120}
Andrew Morgan72c2d582007-10-18 03:05:59 -0700121
122#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
123
David Howells15a24602008-11-14 10:39:15 +1100124int cap_capset_check(const kernel_cap_t *effective,
125 const kernel_cap_t *inheritable,
126 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
David Howellsb6dff3e2008-11-14 10:39:16 +1100128 const struct cred *cred = current->cred;
129
Andrew Morgan72c2d582007-10-18 03:05:59 -0700130 if (cap_inh_is_capped()
131 && !cap_issubset(*inheritable,
David Howellsb6dff3e2008-11-14 10:39:16 +1100132 cap_combine(cred->cap_inheritable,
133 cred->cap_permitted))) {
Andrew Morgan72c2d582007-10-18 03:05:59 -0700134 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EPERM;
136 }
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800137 if (!cap_issubset(*inheritable,
David Howellsb6dff3e2008-11-14 10:39:16 +1100138 cap_combine(cred->cap_inheritable,
139 cred->cap_bset))) {
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800140 /* no new pI capabilities outside bounding set */
141 return -EPERM;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* verify restrictions on target's new Permitted set */
145 if (!cap_issubset (*permitted,
David Howellsb6dff3e2008-11-14 10:39:16 +1100146 cap_combine (cred->cap_permitted,
147 cred->cap_permitted))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EPERM;
149 }
150
151 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
152 if (!cap_issubset (*effective, *permitted)) {
153 return -EPERM;
154 }
155
156 return 0;
157}
158
David Howells15a24602008-11-14 10:39:15 +1100159void cap_capset_set(const kernel_cap_t *effective,
160 const kernel_cap_t *inheritable,
161 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
David Howellsb6dff3e2008-11-14 10:39:16 +1100163 struct cred *cred = current->cred;
164
165 cred->cap_effective = *effective;
166 cred->cap_inheritable = *inheritable;
167 cred->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Serge E. Hallynb5376772007-10-16 23:31:36 -0700170static inline void bprm_clear_caps(struct linux_binprm *bprm)
171{
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700172 cap_clear(bprm->cap_post_exec_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700173 bprm->cap_effective = false;
174}
175
176#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
177
178int cap_inode_need_killpriv(struct dentry *dentry)
179{
180 struct inode *inode = dentry->d_inode;
181 int error;
182
183 if (!inode->i_op || !inode->i_op->getxattr)
184 return 0;
185
186 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
187 if (error <= 0)
188 return 0;
189 return 1;
190}
191
192int cap_inode_killpriv(struct dentry *dentry)
193{
194 struct inode *inode = dentry->d_inode;
195
196 if (!inode->i_op || !inode->i_op->removexattr)
197 return 0;
198
199 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
200}
201
Eric Parisc0b00442008-11-11 21:48:10 +1100202static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
203 struct linux_binprm *bprm)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700204{
Eric Parisc0b00442008-11-11 21:48:10 +1100205 unsigned i;
206 int ret = 0;
207
208 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
209 bprm->cap_effective = true;
210 else
211 bprm->cap_effective = false;
212
213 CAP_FOR_EACH_U32(i) {
214 __u32 permitted = caps->permitted.cap[i];
215 __u32 inheritable = caps->inheritable.cap[i];
216
217 /*
218 * pP' = (X & fP) | (pI & fI)
219 */
220 bprm->cap_post_exec_permitted.cap[i] =
David Howellsb6dff3e2008-11-14 10:39:16 +1100221 (current->cred->cap_bset.cap[i] & permitted) |
222 (current->cred->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100223
224 if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
225 /*
226 * insufficient to execute correctly
227 */
228 ret = -EPERM;
229 }
230 }
231
232 /*
233 * For legacy apps, with no internal support for recognizing they
234 * do not have enough capabilities, we return an error if they are
235 * missing some "forced" (aka file-permitted) capabilities.
236 */
237 return bprm->cap_effective ? ret : 0;
238}
239
240int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
241{
242 struct inode *inode = dentry->d_inode;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700243 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800244 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100245 int size;
246 struct vfs_cap_data caps;
247
248 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
249
250 if (!inode || !inode->i_op || !inode->i_op->getxattr)
251 return -ENODATA;
252
253 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
254 XATTR_CAPS_SZ);
255 if (size == -ENODATA || size == -EOPNOTSUPP) {
256 /* no data, that's ok */
257 return -ENODATA;
258 }
259 if (size < 0)
260 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700261
Andrew Morgane338d262008-02-04 22:29:42 -0800262 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700263 return -EINVAL;
264
Eric Parisc0b00442008-11-11 21:48:10 +1100265 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700266
267 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
Andrew Morgane338d262008-02-04 22:29:42 -0800268 case VFS_CAP_REVISION_1:
269 if (size != XATTR_CAPS_SZ_1)
270 return -EINVAL;
271 tocopy = VFS_CAP_U32_1;
272 break;
273 case VFS_CAP_REVISION_2:
274 if (size != XATTR_CAPS_SZ_2)
275 return -EINVAL;
276 tocopy = VFS_CAP_U32_2;
277 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700278 default:
279 return -EINVAL;
280 }
Andrew Morgane338d262008-02-04 22:29:42 -0800281
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700282 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100283 if (i >= tocopy)
284 break;
285 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
286 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800287 }
Eric Parisc0b00442008-11-11 21:48:10 +1100288 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700289}
290
291/* Locate any VFS capabilities: */
292static int get_file_caps(struct linux_binprm *bprm)
293{
294 struct dentry *dentry;
295 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100296 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700297
Serge Hallyn3318a382008-10-30 11:52:23 -0500298 bprm_clear_caps(bprm);
299
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600300 if (!file_caps_enabled)
301 return 0;
302
Serge Hallyn3318a382008-10-30 11:52:23 -0500303 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700304 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700305
306 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700307
Eric Parisc0b00442008-11-11 21:48:10 +1100308 rc = get_vfs_caps_from_disk(dentry, &vcaps);
309 if (rc < 0) {
310 if (rc == -EINVAL)
311 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
312 __func__, rc, bprm->filename);
313 else if (rc == -ENODATA)
314 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700315 goto out;
316 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700317
Eric Parisc0b00442008-11-11 21:48:10 +1100318 rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700319
320out:
321 dput(dentry);
322 if (rc)
323 bprm_clear_caps(bprm);
324
325 return rc;
326}
327
328#else
329int cap_inode_need_killpriv(struct dentry *dentry)
330{
331 return 0;
332}
333
334int cap_inode_killpriv(struct dentry *dentry)
335{
336 return 0;
337}
338
339static inline int get_file_caps(struct linux_binprm *bprm)
340{
341 bprm_clear_caps(bprm);
342 return 0;
343}
344#endif
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346int cap_bprm_set_security (struct linux_binprm *bprm)
347{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700348 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Serge E. Hallynb5376772007-10-16 23:31:36 -0700350 ret = get_file_caps(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700352 if (!issecure(SECURE_NOROOT)) {
353 /*
354 * To support inheritance of root-permissions and suid-root
355 * executables under compatibility mode, we override the
356 * capability sets for the file.
357 *
358 * If only the real uid is 0, we do not set the effective
359 * bit.
360 */
David Howellsb103c592008-11-14 10:39:11 +1100361 if (bprm->e_uid == 0 || current_uid() == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700362 /* pP' = (cap_bset & ~0) | (pI & ~0) */
363 bprm->cap_post_exec_permitted = cap_combine(
David Howellsb6dff3e2008-11-14 10:39:16 +1100364 current->cred->cap_bset,
365 current->cred->cap_inheritable);
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700366 bprm->cap_effective = (bprm->e_uid == 0);
367 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700370
371 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
375{
David Howellsb6dff3e2008-11-14 10:39:16 +1100376 struct cred *cred = current->cred;
Eric Paris3fc689e2008-11-11 21:48:18 +1100377
David Howellsb6dff3e2008-11-14 10:39:16 +1100378 if (bprm->e_uid != cred->uid || bprm->e_gid != cred->gid ||
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700379 !cap_issubset(bprm->cap_post_exec_permitted,
David Howellsb6dff3e2008-11-14 10:39:16 +1100380 cred->cap_permitted)) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700381 set_dumpable(current->mm, suid_dumpable);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700382 current->pdeath_signal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
385 if (!capable(CAP_SETUID)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100386 bprm->e_uid = cred->uid;
387 bprm->e_gid = cred->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Andrew G. Morgan12097262008-07-04 09:59:59 -0700389 if (cap_limit_ptraced_target()) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700390 bprm->cap_post_exec_permitted = cap_intersect(
391 bprm->cap_post_exec_permitted,
David Howellsb6dff3e2008-11-14 10:39:16 +1100392 cred->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 }
395 }
396
David Howellsb6dff3e2008-11-14 10:39:16 +1100397 cred->suid = cred->euid = cred->fsuid = bprm->e_uid;
398 cred->sgid = cred->egid = cred->fsgid = bprm->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* For init, we want to retain the capabilities set
401 * in the init_task struct. Thus we skip the usual
402 * capability rules */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700403 if (!is_global_init(current)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100404 cred->cap_permitted = bprm->cap_post_exec_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800405 if (bprm->cap_effective)
David Howellsb6dff3e2008-11-14 10:39:16 +1100406 cred->cap_effective = bprm->cap_post_exec_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800407 else
David Howellsb6dff3e2008-11-14 10:39:16 +1100408 cap_clear(cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Eric Paris3fc689e2008-11-11 21:48:18 +1100411 /*
412 * Audit candidate if current->cap_effective is set
413 *
414 * We do not bother to audit if 3 things are true:
415 * 1) cap_effective has all caps
416 * 2) we are root
417 * 3) root is supposed to have all caps (SECURE_NOROOT)
418 * Since this is just a normal root execing a process.
419 *
420 * Number 1 above might fail if you don't have a full bset, but I think
421 * that is interesting information to audit.
422 */
David Howellsb6dff3e2008-11-14 10:39:16 +1100423 if (!cap_isclear(cred->cap_effective)) {
424 if (!cap_issubset(CAP_FULL_SET, cred->cap_effective) ||
425 (bprm->e_uid != 0) || (cred->uid != 0) ||
Eric Paris3fc689e2008-11-11 21:48:18 +1100426 issecure(SECURE_NOROOT))
David Howellsb6dff3e2008-11-14 10:39:16 +1100427 audit_log_bprm_fcaps(bprm, &cred->cap_permitted,
428 &cred->cap_effective);
Eric Paris3fc689e2008-11-11 21:48:18 +1100429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David Howellsb6dff3e2008-11-14 10:39:16 +1100431 cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434int cap_bprm_secureexec (struct linux_binprm *bprm)
435{
David Howellsb6dff3e2008-11-14 10:39:16 +1100436 const struct cred *cred = current->cred;
437
438 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700439 if (bprm->cap_effective)
440 return 1;
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700441 if (!cap_isclear(bprm->cap_post_exec_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700442 return 1;
443 }
444
David Howellsb6dff3e2008-11-14 10:39:16 +1100445 return (cred->euid != cred->uid ||
446 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
David Howells8f0cfa52008-04-29 00:59:41 -0700449int cap_inode_setxattr(struct dentry *dentry, const char *name,
450 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700452 if (!strcmp(name, XATTR_NAME_CAPS)) {
453 if (!capable(CAP_SETFCAP))
454 return -EPERM;
455 return 0;
456 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
458 !capable(CAP_SYS_ADMIN))
459 return -EPERM;
460 return 0;
461}
462
David Howells8f0cfa52008-04-29 00:59:41 -0700463int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700465 if (!strcmp(name, XATTR_NAME_CAPS)) {
466 if (!capable(CAP_SETFCAP))
467 return -EPERM;
468 return 0;
469 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
471 !capable(CAP_SYS_ADMIN))
472 return -EPERM;
473 return 0;
474}
475
476/* moved from kernel/sys.c. */
477/*
478 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
479 * a process after a call to setuid, setreuid, or setresuid.
480 *
481 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
482 * {r,e,s}uid != 0, the permitted and effective capabilities are
483 * cleared.
484 *
485 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
486 * capabilities of the process are cleared.
487 *
488 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
489 * capabilities are set to the permitted capabilities.
490 *
491 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
492 * never happen.
493 *
494 * -astor
495 *
496 * cevans - New behaviour, Oct '99
497 * A process may, via prctl(), elect to keep its capabilities when it
498 * calls setuid() and switches away from uid==0. Both permitted and
499 * effective sets will be retained.
500 * Without this change, it was impossible for a daemon to drop only some
501 * of its privilege. The call to setuid(!=0) would drop all privileges!
502 * Keeping uid 0 is not an option because uid 0 owns too many vital
503 * files..
504 * Thanks to Olaf Kirch and Peter Benie for spotting this.
505 */
506static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
507 int old_suid)
508{
David Howellsb6dff3e2008-11-14 10:39:16 +1100509 struct cred *cred = current->cred;
David Howellsb103c592008-11-14 10:39:11 +1100510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
David Howellsb6dff3e2008-11-14 10:39:16 +1100512 (cred->uid != 0 && cred->euid != 0 && cred->suid != 0) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700513 !issecure(SECURE_KEEP_CAPS)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100514 cap_clear (cred->cap_permitted);
515 cap_clear (cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100517 if (old_euid == 0 && cred->euid != 0) {
518 cap_clear (cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100520 if (old_euid != 0 && cred->euid == 0) {
521 cred->cap_effective = cred->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523}
524
525int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
526 int flags)
527{
David Howellsb6dff3e2008-11-14 10:39:16 +1100528 struct cred *cred = current->cred;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 switch (flags) {
531 case LSM_SETID_RE:
532 case LSM_SETID_ID:
533 case LSM_SETID_RES:
534 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
535 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
536 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
537 }
538 break;
539 case LSM_SETID_FS:
540 {
541 uid_t old_fsuid = old_ruid;
542
543 /* Copied from kernel/sys.c:setfsuid. */
544
545 /*
546 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
547 * if not, we might be a bit too harsh here.
548 */
549
550 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100551 if (old_fsuid == 0 && cred->fsuid != 0) {
552 cred->cap_effective =
Andrew Morgane338d262008-02-04 22:29:42 -0800553 cap_drop_fs_set(
David Howellsb6dff3e2008-11-14 10:39:16 +1100554 cred->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100556 if (old_fsuid != 0 && cred->fsuid == 0) {
557 cred->cap_effective =
Andrew Morgane338d262008-02-04 22:29:42 -0800558 cap_raise_fs_set(
David Howellsb6dff3e2008-11-14 10:39:16 +1100559 cred->cap_effective,
560 cred->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 }
563 break;
564 }
565 default:
566 return -EINVAL;
567 }
568
569 return 0;
570}
571
Serge E. Hallynb5376772007-10-16 23:31:36 -0700572#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
573/*
574 * Rationale: code calling task_setscheduler, task_setioprio, and
575 * task_setnice, assumes that
576 * . if capable(cap_sys_nice), then those actions should be allowed
577 * . if not capable(cap_sys_nice), but acting on your own processes,
578 * then those actions should be allowed
579 * This is insufficient now since you can call code without suid, but
580 * yet with increased caps.
581 * So we check for increased caps on the target process.
582 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400583static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700584{
David Howellsb6dff3e2008-11-14 10:39:16 +1100585 if (!cap_issubset(p->cred->cap_permitted,
586 current->cred->cap_permitted) &&
David Howells5cd9c582008-08-14 11:37:28 +0100587 !capable(CAP_SYS_NICE))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700588 return -EPERM;
589 return 0;
590}
591
592int cap_task_setscheduler (struct task_struct *p, int policy,
593 struct sched_param *lp)
594{
595 return cap_safe_nice(p);
596}
597
598int cap_task_setioprio (struct task_struct *p, int ioprio)
599{
600 return cap_safe_nice(p);
601}
602
603int cap_task_setnice (struct task_struct *p, int nice)
604{
605 return cap_safe_nice(p);
606}
607
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800608/*
609 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
610 * done without task_capability_lock() because it introduces
611 * no new races - i.e. only another task doing capget() on
612 * this task could get inconsistent info. There can be no
613 * racing writer bc a task can only change its own caps.
614 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700615static long cap_prctl_drop(unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800616{
617 if (!capable(CAP_SETPCAP))
618 return -EPERM;
619 if (!cap_valid(cap))
620 return -EINVAL;
David Howellsb6dff3e2008-11-14 10:39:16 +1100621 cap_lower(current->cred->cap_bset, cap);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800622 return 0;
623}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700624
Serge E. Hallynb5376772007-10-16 23:31:36 -0700625#else
626int cap_task_setscheduler (struct task_struct *p, int policy,
627 struct sched_param *lp)
628{
629 return 0;
630}
631int cap_task_setioprio (struct task_struct *p, int ioprio)
632{
633 return 0;
634}
635int cap_task_setnice (struct task_struct *p, int nice)
636{
637 return 0;
638}
Serge E. Hallynb5376772007-10-16 23:31:36 -0700639#endif
640
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700641int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
642 unsigned long arg4, unsigned long arg5, long *rc_p)
643{
David Howellsb6dff3e2008-11-14 10:39:16 +1100644 struct cred *cred = current->cred;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700645 long error = 0;
646
647 switch (option) {
648 case PR_CAPBSET_READ:
649 if (!cap_valid(arg2))
650 error = -EINVAL;
651 else
David Howellsb6dff3e2008-11-14 10:39:16 +1100652 error = !!cap_raised(cred->cap_bset, arg2);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700653 break;
654#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
655 case PR_CAPBSET_DROP:
656 error = cap_prctl_drop(arg2);
657 break;
658
659 /*
660 * The next four prctl's remain to assist with transitioning a
661 * system from legacy UID=0 based privilege (when filesystem
662 * capabilities are not in use) to a system using filesystem
663 * capabilities only - as the POSIX.1e draft intended.
664 *
665 * Note:
666 *
667 * PR_SET_SECUREBITS =
668 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
669 * | issecure_mask(SECURE_NOROOT)
670 * | issecure_mask(SECURE_NOROOT_LOCKED)
671 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
672 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
673 *
674 * will ensure that the current process and all of its
675 * children will be locked into a pure
676 * capability-based-privilege environment.
677 */
678 case PR_SET_SECUREBITS:
David Howellsb6dff3e2008-11-14 10:39:16 +1100679 if ((((cred->securebits & SECURE_ALL_LOCKS) >> 1)
680 & (cred->securebits ^ arg2)) /*[1]*/
681 || ((cred->securebits & SECURE_ALL_LOCKS
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700682 & ~arg2)) /*[2]*/
683 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
Eric Paris06112162008-11-11 22:02:50 +1100684 || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700685 /*
686 * [1] no changing of bits that are locked
687 * [2] no unlocking of locks
688 * [3] no setting of unsupported bits
689 * [4] doing anything requires privilege (go read about
690 * the "sendmail capabilities bug")
691 */
692 error = -EPERM; /* cannot change a locked bit */
693 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100694 cred->securebits = arg2;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700695 }
696 break;
697 case PR_GET_SECUREBITS:
David Howellsb6dff3e2008-11-14 10:39:16 +1100698 error = cred->securebits;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700699 break;
700
701#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
702
703 case PR_GET_KEEPCAPS:
704 if (issecure(SECURE_KEEP_CAPS))
705 error = 1;
706 break;
707 case PR_SET_KEEPCAPS:
708 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
709 error = -EINVAL;
710 else if (issecure(SECURE_KEEP_CAPS_LOCKED))
711 error = -EPERM;
712 else if (arg2)
David Howellsb6dff3e2008-11-14 10:39:16 +1100713 cred->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700714 else
David Howellsb6dff3e2008-11-14 10:39:16 +1100715 cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700716 break;
717
718 default:
719 /* No functionality available - continue with default */
720 return 0;
721 }
722
723 /* Functionality provided */
724 *rc_p = error;
725 return 1;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728void cap_task_reparent_to_init (struct task_struct *p)
729{
David Howellsb6dff3e2008-11-14 10:39:16 +1100730 struct cred *cred = p->cred;
731
732 cap_set_init_eff(cred->cap_effective);
733 cap_clear(cred->cap_inheritable);
734 cap_set_full(cred->cap_permitted);
735 p->cred->securebits = SECUREBITS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738int cap_syslog (int type)
739{
740 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
741 return -EPERM;
742 return 0;
743}
744
Alan Cox34b4e4a2007-08-22 14:01:28 -0700745int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 int cap_sys_admin = 0;
748
Eric Paris06112162008-11-11 22:02:50 +1100749 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700751 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753