blob: b5419273f92d89f2597fdb08d72eb26266a89497 [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{
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700170 cap_clear(bprm->cap_post_exec_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,
201 struct linux_binprm *bprm)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700202{
Eric Parisc0b00442008-11-11 21:48:10 +1100203 unsigned i;
204 int ret = 0;
205
206 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
207 bprm->cap_effective = true;
208 else
209 bprm->cap_effective = false;
210
211 CAP_FOR_EACH_U32(i) {
212 __u32 permitted = caps->permitted.cap[i];
213 __u32 inheritable = caps->inheritable.cap[i];
214
215 /*
216 * pP' = (X & fP) | (pI & fI)
217 */
218 bprm->cap_post_exec_permitted.cap[i] =
David Howellsb6dff3e2008-11-14 10:39:16 +1100219 (current->cred->cap_bset.cap[i] & permitted) |
220 (current->cred->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100221
222 if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
223 /*
224 * insufficient to execute correctly
225 */
226 ret = -EPERM;
227 }
228 }
229
230 /*
231 * For legacy apps, with no internal support for recognizing they
232 * do not have enough capabilities, we return an error if they are
233 * missing some "forced" (aka file-permitted) capabilities.
234 */
235 return bprm->cap_effective ? ret : 0;
236}
237
238int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
239{
240 struct inode *inode = dentry->d_inode;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700241 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800242 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100243 int size;
244 struct vfs_cap_data caps;
245
246 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
247
248 if (!inode || !inode->i_op || !inode->i_op->getxattr)
249 return -ENODATA;
250
251 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
252 XATTR_CAPS_SZ);
253 if (size == -ENODATA || size == -EOPNOTSUPP) {
254 /* no data, that's ok */
255 return -ENODATA;
256 }
257 if (size < 0)
258 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700259
Andrew Morgane338d262008-02-04 22:29:42 -0800260 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700261 return -EINVAL;
262
Eric Parisc0b00442008-11-11 21:48:10 +1100263 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700264
265 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
Andrew Morgane338d262008-02-04 22:29:42 -0800266 case VFS_CAP_REVISION_1:
267 if (size != XATTR_CAPS_SZ_1)
268 return -EINVAL;
269 tocopy = VFS_CAP_U32_1;
270 break;
271 case VFS_CAP_REVISION_2:
272 if (size != XATTR_CAPS_SZ_2)
273 return -EINVAL;
274 tocopy = VFS_CAP_U32_2;
275 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700276 default:
277 return -EINVAL;
278 }
Andrew Morgane338d262008-02-04 22:29:42 -0800279
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700280 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100281 if (i >= tocopy)
282 break;
283 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
284 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800285 }
Eric Parisc0b00442008-11-11 21:48:10 +1100286 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700287}
288
289/* Locate any VFS capabilities: */
290static int get_file_caps(struct linux_binprm *bprm)
291{
292 struct dentry *dentry;
293 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100294 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700295
Serge Hallyn3318a382008-10-30 11:52:23 -0500296 bprm_clear_caps(bprm);
297
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600298 if (!file_caps_enabled)
299 return 0;
300
Serge Hallyn3318a382008-10-30 11:52:23 -0500301 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700302 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700303
304 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700305
Eric Parisc0b00442008-11-11 21:48:10 +1100306 rc = get_vfs_caps_from_disk(dentry, &vcaps);
307 if (rc < 0) {
308 if (rc == -EINVAL)
309 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
310 __func__, rc, bprm->filename);
311 else if (rc == -ENODATA)
312 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700313 goto out;
314 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700315
Eric Parisc0b00442008-11-11 21:48:10 +1100316 rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
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
337static inline int get_file_caps(struct linux_binprm *bprm)
338{
339 bprm_clear_caps(bprm);
340 return 0;
341}
342#endif
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344int cap_bprm_set_security (struct linux_binprm *bprm)
345{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700346 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Serge E. Hallynb5376772007-10-16 23:31:36 -0700348 ret = get_file_caps(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700350 if (!issecure(SECURE_NOROOT)) {
351 /*
352 * To support inheritance of root-permissions and suid-root
353 * executables under compatibility mode, we override the
354 * capability sets for the file.
355 *
356 * If only the real uid is 0, we do not set the effective
357 * bit.
358 */
David Howellsb103c592008-11-14 10:39:11 +1100359 if (bprm->e_uid == 0 || current_uid() == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700360 /* pP' = (cap_bset & ~0) | (pI & ~0) */
361 bprm->cap_post_exec_permitted = cap_combine(
David Howellsb6dff3e2008-11-14 10:39:16 +1100362 current->cred->cap_bset,
363 current->cred->cap_inheritable);
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700364 bprm->cap_effective = (bprm->e_uid == 0);
365 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700368
369 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
David Howellsd84f4f92008-11-14 10:39:23 +1100372int cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
David Howellsd84f4f92008-11-14 10:39:23 +1100374 const struct cred *old = current_cred();
375 struct cred *new;
Eric Paris3fc689e2008-11-11 21:48:18 +1100376
David Howellsd84f4f92008-11-14 10:39:23 +1100377 new = prepare_creds();
378 if (!new)
379 return -ENOMEM;
380
381 if (bprm->e_uid != old->uid || bprm->e_gid != old->gid ||
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700382 !cap_issubset(bprm->cap_post_exec_permitted,
David Howellsd84f4f92008-11-14 10:39:23 +1100383 old->cap_permitted)) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700384 set_dumpable(current->mm, suid_dumpable);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700385 current->pdeath_signal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
388 if (!capable(CAP_SETUID)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100389 bprm->e_uid = old->uid;
390 bprm->e_gid = old->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Andrew G. Morgan12097262008-07-04 09:59:59 -0700392 if (cap_limit_ptraced_target()) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700393 bprm->cap_post_exec_permitted = cap_intersect(
394 bprm->cap_post_exec_permitted,
David Howellsd84f4f92008-11-14 10:39:23 +1100395 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 }
398 }
399
David Howellsd84f4f92008-11-14 10:39:23 +1100400 new->suid = new->euid = new->fsuid = bprm->e_uid;
401 new->sgid = new->egid = new->fsgid = bprm->e_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* For init, we want to retain the capabilities set
404 * in the init_task struct. Thus we skip the usual
405 * capability rules */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700406 if (!is_global_init(current)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100407 new->cap_permitted = bprm->cap_post_exec_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800408 if (bprm->cap_effective)
David Howellsd84f4f92008-11-14 10:39:23 +1100409 new->cap_effective = bprm->cap_post_exec_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800410 else
David Howellsd84f4f92008-11-14 10:39:23 +1100411 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Eric Paris3fc689e2008-11-11 21:48:18 +1100414 /*
415 * Audit candidate if current->cap_effective is set
416 *
417 * We do not bother to audit if 3 things are true:
418 * 1) cap_effective has all caps
419 * 2) we are root
420 * 3) root is supposed to have all caps (SECURE_NOROOT)
421 * Since this is just a normal root execing a process.
422 *
423 * Number 1 above might fail if you don't have a full bset, but I think
424 * that is interesting information to audit.
425 */
David Howellsd84f4f92008-11-14 10:39:23 +1100426 if (!cap_isclear(new->cap_effective)) {
427 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
428 bprm->e_uid != 0 || new->uid != 0 ||
Eric Paris3fc689e2008-11-11 21:48:18 +1100429 issecure(SECURE_NOROOT))
David Howellsd84f4f92008-11-14 10:39:23 +1100430 audit_log_bprm_fcaps(bprm, new, old);
Eric Paris3fc689e2008-11-11 21:48:18 +1100431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
David Howellsd84f4f92008-11-14 10:39:23 +1100433 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
434 return commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437int cap_bprm_secureexec (struct linux_binprm *bprm)
438{
David Howellsc69e8d92008-11-14 10:39:19 +1100439 const struct cred *cred = current_cred();
David Howellsb6dff3e2008-11-14 10:39:16 +1100440
441 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700442 if (bprm->cap_effective)
443 return 1;
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700444 if (!cap_isclear(bprm->cap_post_exec_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700445 return 1;
446 }
447
David Howellsb6dff3e2008-11-14 10:39:16 +1100448 return (cred->euid != cred->uid ||
449 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
David Howells8f0cfa52008-04-29 00:59:41 -0700452int cap_inode_setxattr(struct dentry *dentry, const char *name,
453 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700455 if (!strcmp(name, XATTR_NAME_CAPS)) {
456 if (!capable(CAP_SETFCAP))
457 return -EPERM;
458 return 0;
459 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
461 !capable(CAP_SYS_ADMIN))
462 return -EPERM;
463 return 0;
464}
465
David Howells8f0cfa52008-04-29 00:59:41 -0700466int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700468 if (!strcmp(name, XATTR_NAME_CAPS)) {
469 if (!capable(CAP_SETFCAP))
470 return -EPERM;
471 return 0;
472 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
474 !capable(CAP_SYS_ADMIN))
475 return -EPERM;
476 return 0;
477}
478
479/* moved from kernel/sys.c. */
480/*
481 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
482 * a process after a call to setuid, setreuid, or setresuid.
483 *
484 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
485 * {r,e,s}uid != 0, the permitted and effective capabilities are
486 * cleared.
487 *
488 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
489 * capabilities of the process are cleared.
490 *
491 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
492 * capabilities are set to the permitted capabilities.
493 *
494 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
495 * never happen.
496 *
497 * -astor
498 *
499 * cevans - New behaviour, Oct '99
500 * A process may, via prctl(), elect to keep its capabilities when it
501 * calls setuid() and switches away from uid==0. Both permitted and
502 * effective sets will be retained.
503 * Without this change, it was impossible for a daemon to drop only some
504 * of its privilege. The call to setuid(!=0) would drop all privileges!
505 * Keeping uid 0 is not an option because uid 0 owns too many vital
506 * files..
507 * Thanks to Olaf Kirch and Peter Benie for spotting this.
508 */
David Howellsd84f4f92008-11-14 10:39:23 +1100509static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
David Howellsd84f4f92008-11-14 10:39:23 +1100511 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
512 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700513 !issecure(SECURE_KEEP_CAPS)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100514 cap_clear(new->cap_permitted);
515 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
David Howellsd84f4f92008-11-14 10:39:23 +1100517 if (old->euid == 0 && new->euid != 0)
518 cap_clear(new->cap_effective);
519 if (old->euid != 0 && new->euid == 0)
520 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
David Howellsd84f4f92008-11-14 10:39:23 +1100523int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 switch (flags) {
526 case LSM_SETID_RE:
527 case LSM_SETID_ID:
528 case LSM_SETID_RES:
529 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
David Howellsd84f4f92008-11-14 10:39:23 +1100530 if (!issecure(SECURE_NO_SETUID_FIXUP))
531 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533 case LSM_SETID_FS:
David Howellsd84f4f92008-11-14 10:39:23 +1100534 /* Copied from kernel/sys.c:setfsuid. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David Howellsd84f4f92008-11-14 10:39:23 +1100536 /*
537 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
538 * if not, we might be a bit too harsh here.
539 */
540 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
541 if (old->fsuid == 0 && new->fsuid != 0) {
542 new->cap_effective =
543 cap_drop_fs_set(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
David Howellsd84f4f92008-11-14 10:39:23 +1100545 if (old->fsuid != 0 && new->fsuid == 0) {
546 new->cap_effective =
547 cap_raise_fs_set(new->cap_effective,
548 new->cap_permitted);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
David Howellsd84f4f92008-11-14 10:39:23 +1100551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 default:
553 return -EINVAL;
554 }
555
556 return 0;
557}
558
Serge E. Hallynb5376772007-10-16 23:31:36 -0700559#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
560/*
561 * Rationale: code calling task_setscheduler, task_setioprio, and
562 * task_setnice, assumes that
563 * . if capable(cap_sys_nice), then those actions should be allowed
564 * . if not capable(cap_sys_nice), but acting on your own processes,
565 * then those actions should be allowed
566 * This is insufficient now since you can call code without suid, but
567 * yet with increased caps.
568 * So we check for increased caps on the target process.
569 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400570static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700571{
David Howellsc69e8d92008-11-14 10:39:19 +1100572 int is_subset;
573
574 rcu_read_lock();
575 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
576 current_cred()->cap_permitted);
577 rcu_read_unlock();
578
579 if (!is_subset && !capable(CAP_SYS_NICE))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700580 return -EPERM;
581 return 0;
582}
583
584int cap_task_setscheduler (struct task_struct *p, int policy,
585 struct sched_param *lp)
586{
587 return cap_safe_nice(p);
588}
589
590int cap_task_setioprio (struct task_struct *p, int ioprio)
591{
592 return cap_safe_nice(p);
593}
594
595int cap_task_setnice (struct task_struct *p, int nice)
596{
597 return cap_safe_nice(p);
598}
599
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800600/*
601 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
602 * done without task_capability_lock() because it introduces
603 * no new races - i.e. only another task doing capget() on
604 * this task could get inconsistent info. There can be no
605 * racing writer bc a task can only change its own caps.
606 */
David Howellsd84f4f92008-11-14 10:39:23 +1100607static long cap_prctl_drop(struct cred *new, unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800608{
609 if (!capable(CAP_SETPCAP))
610 return -EPERM;
611 if (!cap_valid(cap))
612 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100613
614 cap_lower(new->cap_bset, cap);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800615 return 0;
616}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700617
Serge E. Hallynb5376772007-10-16 23:31:36 -0700618#else
619int cap_task_setscheduler (struct task_struct *p, int policy,
620 struct sched_param *lp)
621{
622 return 0;
623}
624int cap_task_setioprio (struct task_struct *p, int ioprio)
625{
626 return 0;
627}
628int cap_task_setnice (struct task_struct *p, int nice)
629{
630 return 0;
631}
Serge E. Hallynb5376772007-10-16 23:31:36 -0700632#endif
633
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700634int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100635 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700636{
David Howellsd84f4f92008-11-14 10:39:23 +1100637 struct cred *new;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700638 long error = 0;
639
David Howellsd84f4f92008-11-14 10:39:23 +1100640 new = prepare_creds();
641 if (!new)
642 return -ENOMEM;
643
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700644 switch (option) {
645 case PR_CAPBSET_READ:
David Howellsd84f4f92008-11-14 10:39:23 +1100646 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700647 if (!cap_valid(arg2))
David Howellsd84f4f92008-11-14 10:39:23 +1100648 goto error;
649 error = !!cap_raised(new->cap_bset, arg2);
650 goto no_change;
651
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700652#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
653 case PR_CAPBSET_DROP:
David Howellsd84f4f92008-11-14 10:39:23 +1100654 error = cap_prctl_drop(new, arg2);
655 if (error < 0)
656 goto error;
657 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700658
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 Howellsd84f4f92008-11-14 10:39:23 +1100679 error = -EPERM;
680 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
681 & (new->securebits ^ arg2)) /*[1]*/
682 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
683 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
684 || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. 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 */
David Howellsd84f4f92008-11-14 10:39:23 +1100692 )
693 /* cannot change a locked bit */
694 goto error;
695 new->securebits = arg2;
696 goto changed;
697
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700698 case PR_GET_SECUREBITS:
David Howellsd84f4f92008-11-14 10:39:23 +1100699 error = new->securebits;
700 goto no_change;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700701
702#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
703
704 case PR_GET_KEEPCAPS:
705 if (issecure(SECURE_KEEP_CAPS))
706 error = 1;
David Howellsd84f4f92008-11-14 10:39:23 +1100707 goto no_change;
708
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700709 case PR_SET_KEEPCAPS:
David Howellsd84f4f92008-11-14 10:39:23 +1100710 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700711 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
David Howellsd84f4f92008-11-14 10:39:23 +1100712 goto error;
713 error = -EPERM;
714 if (issecure(SECURE_KEEP_CAPS_LOCKED))
715 goto error;
716 if (arg2)
717 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700718 else
David Howellsd84f4f92008-11-14 10:39:23 +1100719 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
720 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700721
722 default:
723 /* No functionality available - continue with default */
David Howellsd84f4f92008-11-14 10:39:23 +1100724 error = -ENOSYS;
725 goto error;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700726 }
727
728 /* Functionality provided */
David Howellsd84f4f92008-11-14 10:39:23 +1100729changed:
730 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700731
David Howellsd84f4f92008-11-14 10:39:23 +1100732no_change:
733 error = 0;
734error:
735 abort_creds(new);
736 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739int cap_syslog (int type)
740{
741 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
742 return -EPERM;
743 return 0;
744}
745
Alan Cox34b4e4a2007-08-22 14:01:28 -0700746int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 int cap_sys_admin = 0;
749
Eric Paris06112162008-11-11 22:02:50 +1100750 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700752 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754