blob: 7f0b2a68717d6fd8d57837308dfe219848c8e30d [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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070043EXPORT_SYMBOL(cap_netlink_recv);
44
David Howells1d045982008-11-14 10:39:24 +110045/**
David Howells14eaddc2008-12-31 15:15:42 +000046 * cap_capable - Determine whether current has a particular effective capability
David Howells1d045982008-11-14 10:39:24 +110047 * @cap: The capability to check for
48 * @audit: Whether to write an audit message or not
49 *
50 * Determine whether the nominated task has the specified capability amongst
David Howells14eaddc2008-12-31 15:15:42 +000051 * its effective set, returning 0 if it does, -ve if it does not. Note that
52 * this uses current's subjective/effective credentials.
David Howells1d045982008-11-14 10:39:24 +110053 *
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -080054 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
David Howells1d045982008-11-14 10:39:24 +110055 * function. That is, it has the reverse semantics: cap_capable() returns 0
56 * when a task has a capability, but the kernel's capable() returns 1 for this
57 * case.
Andrew G. Morgana6dbb1e2008-01-21 17:18:30 -080058 */
David Howells14eaddc2008-12-31 15:15:42 +000059int cap_capable(int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
David Howells14eaddc2008-12-31 15:15:42 +000061 return cap_raised(current_cap(), cap) ? 0 : -EPERM;
62}
David Howellsc69e8d92008-11-14 10:39:19 +110063
David Howells14eaddc2008-12-31 15:15:42 +000064/**
65 * cap_has_capability - Determine whether a task has a particular effective capability
66 * @tsk: The task to query
67 * @cred: The credentials to use
68 * @cap: The capability to check for
69 * @audit: Whether to write an audit message or not
70 *
71 * Determine whether the nominated task has the specified capability amongst
72 * its effective set, returning 0 if it does, -ve if it does not. Note that
73 * this uses the task's objective/real credentials.
74 *
75 * NOTE WELL: cap_has_capability() cannot be used like the kernel's
76 * has_capability() function. That is, it has the reverse semantics:
77 * cap_has_capability() returns 0 when a task has a capability, but the
78 * kernel's has_capability() returns 1 for this case.
79 */
80int cap_task_capable(struct task_struct *tsk, const struct cred *cred, int cap,
81 int audit)
82{
83 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
David Howells1d045982008-11-14 10:39:24 +110086/**
87 * cap_settime - Determine whether the current process may set the system clock
88 * @ts: The time to set
89 * @tz: The timezone to set
90 *
91 * Determine whether the current process may set the system clock and timezone
92 * information, returning 0 if permission granted, -ve if denied.
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094int cap_settime(struct timespec *ts, struct timezone *tz)
95{
96 if (!capable(CAP_SYS_TIME))
97 return -EPERM;
98 return 0;
99}
100
David Howells1d045982008-11-14 10:39:24 +1100101/**
102 * cap_ptrace_may_access - Determine whether the current process may access
103 * another
104 * @child: The process to be accessed
105 * @mode: The mode of attachment.
106 *
107 * Determine whether a process may access another, returning 0 if permission
108 * granted, -ve if denied.
109 */
David Howells5cd9c582008-08-14 11:37:28 +0100110int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
David Howellsc69e8d92008-11-14 10:39:19 +1100112 int ret = 0;
113
114 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100115 if (!cap_issubset(__task_cred(child)->cap_permitted,
116 current_cred()->cap_permitted) &&
David Howellsc69e8d92008-11-14 10:39:19 +1100117 !capable(CAP_SYS_PTRACE))
118 ret = -EPERM;
119 rcu_read_unlock();
120 return ret;
David Howells5cd9c582008-08-14 11:37:28 +0100121}
122
David Howells1d045982008-11-14 10:39:24 +1100123/**
124 * cap_ptrace_traceme - Determine whether another process may trace the current
125 * @parent: The task proposed to be the tracer
126 *
127 * Determine whether the nominated task is permitted to trace the current
128 * process, returning 0 if permission is granted, -ve if denied.
129 */
David Howells5cd9c582008-08-14 11:37:28 +0100130int cap_ptrace_traceme(struct task_struct *parent)
131{
David Howellsc69e8d92008-11-14 10:39:19 +1100132 int ret = 0;
133
134 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100135 if (!cap_issubset(current_cred()->cap_permitted,
136 __task_cred(parent)->cap_permitted) &&
David Howellsc69e8d92008-11-14 10:39:19 +1100137 !has_capability(parent, CAP_SYS_PTRACE))
138 ret = -EPERM;
139 rcu_read_unlock();
140 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
David Howells1d045982008-11-14 10:39:24 +1100143/**
144 * cap_capget - Retrieve a task's capability sets
145 * @target: The task from which to retrieve the capability sets
146 * @effective: The place to record the effective set
147 * @inheritable: The place to record the inheritable set
148 * @permitted: The place to record the permitted set
149 *
150 * This function retrieves the capabilities of the nominated task and returns
151 * them to the caller.
152 */
153int cap_capget(struct task_struct *target, kernel_cap_t *effective,
154 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
David Howellsc69e8d92008-11-14 10:39:19 +1100156 const struct cred *cred;
David Howellsb6dff3e2008-11-14 10:39:16 +1100157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* Derived from kernel/capability.c:sys_capget. */
David Howellsc69e8d92008-11-14 10:39:19 +1100159 rcu_read_lock();
160 cred = __task_cred(target);
David Howellsb6dff3e2008-11-14 10:39:16 +1100161 *effective = cred->cap_effective;
162 *inheritable = cred->cap_inheritable;
163 *permitted = cred->cap_permitted;
David Howellsc69e8d92008-11-14 10:39:19 +1100164 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166}
167
David Howells1d045982008-11-14 10:39:24 +1100168/*
169 * Determine whether the inheritable capabilities are limited to the old
170 * permitted set. Returns 1 if they are limited, 0 if they are not.
171 */
Andrew Morgan72c2d582007-10-18 03:05:59 -0700172static inline int cap_inh_is_capped(void)
173{
David Howells1d045982008-11-14 10:39:24 +1100174#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
175
176 /* they are so limited unless the current task has the CAP_SETPCAP
177 * capability
Andrew Morgan72c2d582007-10-18 03:05:59 -0700178 */
David Howells14eaddc2008-12-31 15:15:42 +0000179 if (cap_capable(CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
David Howells1d045982008-11-14 10:39:24 +1100180 return 0;
181#endif
182 return 1;
Andrew Morgan72c2d582007-10-18 03:05:59 -0700183}
184
David Howells1d045982008-11-14 10:39:24 +1100185/**
186 * cap_capset - Validate and apply proposed changes to current's capabilities
187 * @new: The proposed new credentials; alterations should be made here
188 * @old: The current task's current credentials
189 * @effective: A pointer to the proposed new effective capabilities set
190 * @inheritable: A pointer to the proposed new inheritable capabilities set
191 * @permitted: A pointer to the proposed new permitted capabilities set
192 *
193 * This function validates and applies a proposed mass change to the current
194 * process's capability sets. The changes are made to the proposed new
195 * credentials, and assuming no error, will be committed by the caller of LSM.
196 */
David Howellsd84f4f92008-11-14 10:39:23 +1100197int cap_capset(struct cred *new,
198 const struct cred *old,
199 const kernel_cap_t *effective,
200 const kernel_cap_t *inheritable,
201 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
David Howellsd84f4f92008-11-14 10:39:23 +1100203 if (cap_inh_is_capped() &&
204 !cap_issubset(*inheritable,
205 cap_combine(old->cap_inheritable,
206 old->cap_permitted)))
Andrew Morgan72c2d582007-10-18 03:05:59 -0700207 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100209
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800210 if (!cap_issubset(*inheritable,
David Howellsd84f4f92008-11-14 10:39:23 +1100211 cap_combine(old->cap_inheritable,
212 old->cap_bset)))
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800213 /* no new pI capabilities outside bounding set */
214 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* verify restrictions on target's new Permitted set */
David Howellsd84f4f92008-11-14 10:39:23 +1100217 if (!cap_issubset(*permitted, old->cap_permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
David Howellsd84f4f92008-11-14 10:39:23 +1100221 if (!cap_issubset(*effective, *permitted))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David Howellsd84f4f92008-11-14 10:39:23 +1100224 new->cap_effective = *effective;
225 new->cap_inheritable = *inheritable;
226 new->cap_permitted = *permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228}
229
David Howells1d045982008-11-14 10:39:24 +1100230/*
231 * Clear proposed capability sets for execve().
232 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700233static inline void bprm_clear_caps(struct linux_binprm *bprm)
234{
David Howellsa6f76f22008-11-14 10:39:24 +1100235 cap_clear(bprm->cred->cap_permitted);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700236 bprm->cap_effective = false;
237}
238
239#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
240
David Howells1d045982008-11-14 10:39:24 +1100241/**
242 * cap_inode_need_killpriv - Determine if inode change affects privileges
243 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
244 *
245 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
246 * affects the security markings on that inode, and if it is, should
247 * inode_killpriv() be invoked or the change rejected?
248 *
249 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
250 * -ve to deny the change.
251 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700252int cap_inode_need_killpriv(struct dentry *dentry)
253{
254 struct inode *inode = dentry->d_inode;
255 int error;
256
257 if (!inode->i_op || !inode->i_op->getxattr)
258 return 0;
259
260 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
261 if (error <= 0)
262 return 0;
263 return 1;
264}
265
David Howells1d045982008-11-14 10:39:24 +1100266/**
267 * cap_inode_killpriv - Erase the security markings on an inode
268 * @dentry: The inode/dentry to alter
269 *
270 * Erase the privilege-enhancing security markings on an inode.
271 *
272 * Returns 0 if successful, -ve on error.
273 */
Serge E. Hallynb5376772007-10-16 23:31:36 -0700274int cap_inode_killpriv(struct dentry *dentry)
275{
276 struct inode *inode = dentry->d_inode;
277
278 if (!inode->i_op || !inode->i_op->removexattr)
279 return 0;
280
281 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
282}
283
David Howells1d045982008-11-14 10:39:24 +1100284/*
285 * Calculate the new process capability sets from the capability sets attached
286 * to a file.
287 */
Eric Parisc0b00442008-11-11 21:48:10 +1100288static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
David Howellsa6f76f22008-11-14 10:39:24 +1100289 struct linux_binprm *bprm,
290 bool *effective)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700291{
David Howellsa6f76f22008-11-14 10:39:24 +1100292 struct cred *new = bprm->cred;
Eric Parisc0b00442008-11-11 21:48:10 +1100293 unsigned i;
294 int ret = 0;
295
296 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
David Howellsa6f76f22008-11-14 10:39:24 +1100297 *effective = true;
Eric Parisc0b00442008-11-11 21:48:10 +1100298
299 CAP_FOR_EACH_U32(i) {
300 __u32 permitted = caps->permitted.cap[i];
301 __u32 inheritable = caps->inheritable.cap[i];
302
303 /*
304 * pP' = (X & fP) | (pI & fI)
305 */
David Howellsa6f76f22008-11-14 10:39:24 +1100306 new->cap_permitted.cap[i] =
307 (new->cap_bset.cap[i] & permitted) |
308 (new->cap_inheritable.cap[i] & inheritable);
Eric Parisc0b00442008-11-11 21:48:10 +1100309
David Howellsa6f76f22008-11-14 10:39:24 +1100310 if (permitted & ~new->cap_permitted.cap[i])
311 /* insufficient to execute correctly */
Eric Parisc0b00442008-11-11 21:48:10 +1100312 ret = -EPERM;
Eric Parisc0b00442008-11-11 21:48:10 +1100313 }
314
315 /*
316 * For legacy apps, with no internal support for recognizing they
317 * do not have enough capabilities, we return an error if they are
318 * missing some "forced" (aka file-permitted) capabilities.
319 */
David Howellsa6f76f22008-11-14 10:39:24 +1100320 return *effective ? ret : 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100321}
322
David Howells1d045982008-11-14 10:39:24 +1100323/*
324 * Extract the on-exec-apply capability sets for an executable file.
325 */
Eric Parisc0b00442008-11-11 21:48:10 +1100326int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
327{
328 struct inode *inode = dentry->d_inode;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700329 __u32 magic_etc;
Andrew Morgane338d262008-02-04 22:29:42 -0800330 unsigned tocopy, i;
Eric Parisc0b00442008-11-11 21:48:10 +1100331 int size;
332 struct vfs_cap_data caps;
333
334 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
335
336 if (!inode || !inode->i_op || !inode->i_op->getxattr)
337 return -ENODATA;
338
339 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
340 XATTR_CAPS_SZ);
David Howellsa6f76f22008-11-14 10:39:24 +1100341 if (size == -ENODATA || size == -EOPNOTSUPP)
Eric Parisc0b00442008-11-11 21:48:10 +1100342 /* no data, that's ok */
343 return -ENODATA;
Eric Parisc0b00442008-11-11 21:48:10 +1100344 if (size < 0)
345 return size;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700346
Andrew Morgane338d262008-02-04 22:29:42 -0800347 if (size < sizeof(magic_etc))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700348 return -EINVAL;
349
Eric Parisc0b00442008-11-11 21:48:10 +1100350 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700351
David Howellsa6f76f22008-11-14 10:39:24 +1100352 switch (magic_etc & VFS_CAP_REVISION_MASK) {
Andrew Morgane338d262008-02-04 22:29:42 -0800353 case VFS_CAP_REVISION_1:
354 if (size != XATTR_CAPS_SZ_1)
355 return -EINVAL;
356 tocopy = VFS_CAP_U32_1;
357 break;
358 case VFS_CAP_REVISION_2:
359 if (size != XATTR_CAPS_SZ_2)
360 return -EINVAL;
361 tocopy = VFS_CAP_U32_2;
362 break;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700363 default:
364 return -EINVAL;
365 }
Andrew Morgane338d262008-02-04 22:29:42 -0800366
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700367 CAP_FOR_EACH_U32(i) {
Eric Parisc0b00442008-11-11 21:48:10 +1100368 if (i >= tocopy)
369 break;
370 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
371 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
Andrew Morgane338d262008-02-04 22:29:42 -0800372 }
David Howellsa6f76f22008-11-14 10:39:24 +1100373
Eric Parisc0b00442008-11-11 21:48:10 +1100374 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700375}
376
David Howells1d045982008-11-14 10:39:24 +1100377/*
378 * Attempt to get the on-exec apply capability sets for an executable file from
379 * its xattrs and, if present, apply them to the proposed credentials being
380 * constructed by execve().
381 */
David Howellsa6f76f22008-11-14 10:39:24 +1100382static int get_file_caps(struct linux_binprm *bprm, bool *effective)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700383{
384 struct dentry *dentry;
385 int rc = 0;
Eric Parisc0b00442008-11-11 21:48:10 +1100386 struct cpu_vfs_cap_data vcaps;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700387
Serge Hallyn3318a382008-10-30 11:52:23 -0500388 bprm_clear_caps(bprm);
389
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -0600390 if (!file_caps_enabled)
391 return 0;
392
Serge Hallyn3318a382008-10-30 11:52:23 -0500393 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700394 return 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700395
396 dentry = dget(bprm->file->f_dentry);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700397
Eric Parisc0b00442008-11-11 21:48:10 +1100398 rc = get_vfs_caps_from_disk(dentry, &vcaps);
399 if (rc < 0) {
400 if (rc == -EINVAL)
401 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
402 __func__, rc, bprm->filename);
403 else if (rc == -ENODATA)
404 rc = 0;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700405 goto out;
406 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700407
David Howellsa6f76f22008-11-14 10:39:24 +1100408 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
409 if (rc == -EINVAL)
410 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
411 __func__, rc, bprm->filename);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700412
413out:
414 dput(dentry);
415 if (rc)
416 bprm_clear_caps(bprm);
417
418 return rc;
419}
420
421#else
422int cap_inode_need_killpriv(struct dentry *dentry)
423{
424 return 0;
425}
426
427int cap_inode_killpriv(struct dentry *dentry)
428{
429 return 0;
430}
431
Eric Parise50a9062008-11-13 18:37:25 -0500432int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
433{
434 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
435 return -ENODATA;
436}
437
David Howellsa6f76f22008-11-14 10:39:24 +1100438static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700439{
440 bprm_clear_caps(bprm);
441 return 0;
442}
443#endif
444
David Howellsa6f76f22008-11-14 10:39:24 +1100445/*
David Howells1d045982008-11-14 10:39:24 +1100446 * Determine whether a exec'ing process's new permitted capabilities should be
447 * limited to just what it already has.
448 *
449 * This prevents processes that are being ptraced from gaining access to
450 * CAP_SETPCAP, unless the process they're tracing already has it, and the
451 * binary they're executing has filecaps that elevate it.
452 *
453 * Returns 1 if they should be limited, 0 if they are not.
454 */
455static inline int cap_limit_ptraced_target(void)
456{
457#ifndef CONFIG_SECURITY_FILE_CAPABILITIES
458 if (capable(CAP_SETPCAP))
459 return 0;
460#endif
461 return 1;
462}
463
464/**
465 * cap_bprm_set_creds - Set up the proposed credentials for execve().
466 * @bprm: The execution parameters, including the proposed creds
467 *
468 * Set up the proposed credentials for a new execution context being
469 * constructed by execve(). The proposed creds in @bprm->cred is altered,
470 * which won't take effect immediately. Returns 0 if successful, -ve on error.
David Howellsa6f76f22008-11-14 10:39:24 +1100471 */
472int cap_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
David Howellsa6f76f22008-11-14 10:39:24 +1100474 const struct cred *old = current_cred();
475 struct cred *new = bprm->cred;
476 bool effective;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700477 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David Howellsa6f76f22008-11-14 10:39:24 +1100479 effective = false;
480 ret = get_file_caps(bprm, &effective);
481 if (ret < 0)
482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700484 if (!issecure(SECURE_NOROOT)) {
485 /*
486 * To support inheritance of root-permissions and suid-root
487 * executables under compatibility mode, we override the
488 * capability sets for the file.
489 *
David Howellsa6f76f22008-11-14 10:39:24 +1100490 * If only the real uid is 0, we do not set the effective bit.
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700491 */
David Howellsa6f76f22008-11-14 10:39:24 +1100492 if (new->euid == 0 || new->uid == 0) {
Andrew G. Morgan5459c162008-07-23 21:28:24 -0700493 /* pP' = (cap_bset & ~0) | (pI & ~0) */
David Howellsa6f76f22008-11-14 10:39:24 +1100494 new->cap_permitted = cap_combine(old->cap_bset,
495 old->cap_inheritable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
David Howellsa6f76f22008-11-14 10:39:24 +1100497 if (new->euid == 0)
498 effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700500
David Howellsa6f76f22008-11-14 10:39:24 +1100501 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
502 * credentials unless they have the appropriate permit
503 */
504 if ((new->euid != old->uid ||
505 new->egid != old->gid ||
506 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
507 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
508 /* downgrade; they get no more than they had, and maybe less */
509 if (!capable(CAP_SETUID)) {
510 new->euid = new->uid;
511 new->egid = new->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
David Howellsa6f76f22008-11-14 10:39:24 +1100513 if (cap_limit_ptraced_target())
514 new->cap_permitted = cap_intersect(new->cap_permitted,
515 old->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
David Howellsa6f76f22008-11-14 10:39:24 +1100518 new->suid = new->fsuid = new->euid;
519 new->sgid = new->fsgid = new->egid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David Howellsa6f76f22008-11-14 10:39:24 +1100521 /* For init, we want to retain the capabilities set in the initial
522 * task. Thus we skip the usual capability rules
523 */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700524 if (!is_global_init(current)) {
David Howellsa6f76f22008-11-14 10:39:24 +1100525 if (effective)
526 new->cap_effective = new->cap_permitted;
Andrew Morgane338d262008-02-04 22:29:42 -0800527 else
David Howellsd84f4f92008-11-14 10:39:23 +1100528 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
David Howellsa6f76f22008-11-14 10:39:24 +1100530 bprm->cap_effective = effective;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Eric Paris3fc689e2008-11-11 21:48:18 +1100532 /*
533 * Audit candidate if current->cap_effective is set
534 *
535 * We do not bother to audit if 3 things are true:
536 * 1) cap_effective has all caps
537 * 2) we are root
538 * 3) root is supposed to have all caps (SECURE_NOROOT)
539 * Since this is just a normal root execing a process.
540 *
541 * Number 1 above might fail if you don't have a full bset, but I think
542 * that is interesting information to audit.
543 */
David Howellsd84f4f92008-11-14 10:39:23 +1100544 if (!cap_isclear(new->cap_effective)) {
545 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
David Howellsa6f76f22008-11-14 10:39:24 +1100546 new->euid != 0 || new->uid != 0 ||
547 issecure(SECURE_NOROOT)) {
548 ret = audit_log_bprm_fcaps(bprm, new, old);
549 if (ret < 0)
550 return ret;
551 }
Eric Paris3fc689e2008-11-11 21:48:18 +1100552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
David Howellsd84f4f92008-11-14 10:39:23 +1100554 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
David Howellsa6f76f22008-11-14 10:39:24 +1100555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
David Howells1d045982008-11-14 10:39:24 +1100558/**
559 * cap_bprm_secureexec - Determine whether a secure execution is required
560 * @bprm: The execution parameters
561 *
562 * Determine whether a secure execution is required, return 1 if it is, and 0
563 * if it is not.
564 *
565 * The credentials have been committed by this point, and so are no longer
566 * available through @bprm->cred.
David Howellsa6f76f22008-11-14 10:39:24 +1100567 */
568int cap_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
David Howellsc69e8d92008-11-14 10:39:19 +1100570 const struct cred *cred = current_cred();
David Howellsb6dff3e2008-11-14 10:39:16 +1100571
572 if (cred->uid != 0) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700573 if (bprm->cap_effective)
574 return 1;
David Howellsa6f76f22008-11-14 10:39:24 +1100575 if (!cap_isclear(cred->cap_permitted))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700576 return 1;
577 }
578
David Howellsb6dff3e2008-11-14 10:39:16 +1100579 return (cred->euid != cred->uid ||
580 cred->egid != cred->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
David Howells1d045982008-11-14 10:39:24 +1100583/**
584 * cap_inode_setxattr - Determine whether an xattr may be altered
585 * @dentry: The inode/dentry being altered
586 * @name: The name of the xattr to be changed
587 * @value: The value that the xattr will be changed to
588 * @size: The size of value
589 * @flags: The replacement flag
590 *
591 * Determine whether an xattr may be altered or set on an inode, returning 0 if
592 * permission is granted, -ve if denied.
593 *
594 * This is used to make sure security xattrs don't get updated or set by those
595 * who aren't privileged to do so.
596 */
David Howells8f0cfa52008-04-29 00:59:41 -0700597int cap_inode_setxattr(struct dentry *dentry, const char *name,
598 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700600 if (!strcmp(name, XATTR_NAME_CAPS)) {
601 if (!capable(CAP_SETFCAP))
602 return -EPERM;
603 return 0;
David Howells1d045982008-11-14 10:39:24 +1100604 }
605
606 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
608 !capable(CAP_SYS_ADMIN))
609 return -EPERM;
610 return 0;
611}
612
David Howells1d045982008-11-14 10:39:24 +1100613/**
614 * cap_inode_removexattr - Determine whether an xattr may be removed
615 * @dentry: The inode/dentry being altered
616 * @name: The name of the xattr to be changed
617 *
618 * Determine whether an xattr may be removed from an inode, returning 0 if
619 * permission is granted, -ve if denied.
620 *
621 * This is used to make sure security xattrs don't get removed by those who
622 * aren't privileged to remove them.
623 */
David Howells8f0cfa52008-04-29 00:59:41 -0700624int cap_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700626 if (!strcmp(name, XATTR_NAME_CAPS)) {
627 if (!capable(CAP_SETFCAP))
628 return -EPERM;
629 return 0;
David Howells1d045982008-11-14 10:39:24 +1100630 }
631
632 if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
634 !capable(CAP_SYS_ADMIN))
635 return -EPERM;
636 return 0;
637}
638
David Howellsa6f76f22008-11-14 10:39:24 +1100639/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
641 * a process after a call to setuid, setreuid, or setresuid.
642 *
643 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
644 * {r,e,s}uid != 0, the permitted and effective capabilities are
645 * cleared.
646 *
647 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
648 * capabilities of the process are cleared.
649 *
650 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
651 * capabilities are set to the permitted capabilities.
652 *
David Howellsa6f76f22008-11-14 10:39:24 +1100653 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * never happen.
655 *
David Howellsa6f76f22008-11-14 10:39:24 +1100656 * -astor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 *
658 * cevans - New behaviour, Oct '99
659 * A process may, via prctl(), elect to keep its capabilities when it
660 * calls setuid() and switches away from uid==0. Both permitted and
661 * effective sets will be retained.
662 * Without this change, it was impossible for a daemon to drop only some
663 * of its privilege. The call to setuid(!=0) would drop all privileges!
664 * Keeping uid 0 is not an option because uid 0 owns too many vital
665 * files..
666 * Thanks to Olaf Kirch and Peter Benie for spotting this.
667 */
David Howellsd84f4f92008-11-14 10:39:23 +1100668static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
David Howellsd84f4f92008-11-14 10:39:23 +1100670 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
671 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700672 !issecure(SECURE_KEEP_CAPS)) {
David Howellsd84f4f92008-11-14 10:39:23 +1100673 cap_clear(new->cap_permitted);
674 cap_clear(new->cap_effective);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
David Howellsd84f4f92008-11-14 10:39:23 +1100676 if (old->euid == 0 && new->euid != 0)
677 cap_clear(new->cap_effective);
678 if (old->euid != 0 && new->euid == 0)
679 new->cap_effective = new->cap_permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
David Howells1d045982008-11-14 10:39:24 +1100682/**
683 * cap_task_fix_setuid - Fix up the results of setuid() call
684 * @new: The proposed credentials
685 * @old: The current task's current credentials
686 * @flags: Indications of what has changed
687 *
688 * Fix up the results of setuid() call before the credential changes are
689 * actually applied, returning 0 to grant the changes, -ve to deny them.
690 */
David Howellsd84f4f92008-11-14 10:39:23 +1100691int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 switch (flags) {
694 case LSM_SETID_RE:
695 case LSM_SETID_ID:
696 case LSM_SETID_RES:
David Howells1d045982008-11-14 10:39:24 +1100697 /* juggle the capabilities to follow [RES]UID changes unless
698 * otherwise suppressed */
David Howellsd84f4f92008-11-14 10:39:23 +1100699 if (!issecure(SECURE_NO_SETUID_FIXUP))
700 cap_emulate_setxuid(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David Howells1d045982008-11-14 10:39:24 +1100703 case LSM_SETID_FS:
704 /* juggle the capabilties to follow FSUID changes, unless
705 * otherwise suppressed
706 *
David Howellsd84f4f92008-11-14 10:39:23 +1100707 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
708 * if not, we might be a bit too harsh here.
709 */
710 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
David Howells1d045982008-11-14 10:39:24 +1100711 if (old->fsuid == 0 && new->fsuid != 0)
David Howellsd84f4f92008-11-14 10:39:23 +1100712 new->cap_effective =
713 cap_drop_fs_set(new->cap_effective);
David Howells1d045982008-11-14 10:39:24 +1100714
715 if (old->fsuid != 0 && new->fsuid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +1100716 new->cap_effective =
717 cap_raise_fs_set(new->cap_effective,
718 new->cap_permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
David Howellsd84f4f92008-11-14 10:39:23 +1100720 break;
David Howells1d045982008-11-14 10:39:24 +1100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 default:
723 return -EINVAL;
724 }
725
726 return 0;
727}
728
Serge E. Hallynb5376772007-10-16 23:31:36 -0700729#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
730/*
731 * Rationale: code calling task_setscheduler, task_setioprio, and
732 * task_setnice, assumes that
733 * . if capable(cap_sys_nice), then those actions should be allowed
734 * . if not capable(cap_sys_nice), but acting on your own processes,
735 * then those actions should be allowed
736 * This is insufficient now since you can call code without suid, but
737 * yet with increased caps.
738 * So we check for increased caps on the target process.
739 */
Serge E. Hallynde45e802008-09-26 22:27:47 -0400740static int cap_safe_nice(struct task_struct *p)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700741{
David Howellsc69e8d92008-11-14 10:39:19 +1100742 int is_subset;
743
744 rcu_read_lock();
745 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
746 current_cred()->cap_permitted);
747 rcu_read_unlock();
748
749 if (!is_subset && !capable(CAP_SYS_NICE))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700750 return -EPERM;
751 return 0;
752}
753
David Howells1d045982008-11-14 10:39:24 +1100754/**
755 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
756 * @p: The task to affect
757 * @policy: The policy to effect
758 * @lp: The parameters to the scheduling policy
759 *
760 * Detemine if the requested scheduler policy change is permitted for the
761 * specified task, returning 0 if permission is granted, -ve if denied.
762 */
763int cap_task_setscheduler(struct task_struct *p, int policy,
Serge E. Hallynb5376772007-10-16 23:31:36 -0700764 struct sched_param *lp)
765{
766 return cap_safe_nice(p);
767}
768
David Howells1d045982008-11-14 10:39:24 +1100769/**
770 * cap_task_ioprio - Detemine if I/O priority change is permitted
771 * @p: The task to affect
772 * @ioprio: The I/O priority to set
773 *
774 * Detemine if the requested I/O priority change is permitted for the specified
775 * task, returning 0 if permission is granted, -ve if denied.
776 */
777int cap_task_setioprio(struct task_struct *p, int ioprio)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700778{
779 return cap_safe_nice(p);
780}
781
David Howells1d045982008-11-14 10:39:24 +1100782/**
783 * cap_task_ioprio - Detemine if task priority change is permitted
784 * @p: The task to affect
785 * @nice: The nice value to set
786 *
787 * Detemine if the requested task priority change is permitted for the
788 * specified task, returning 0 if permission is granted, -ve if denied.
789 */
790int cap_task_setnice(struct task_struct *p, int nice)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700791{
792 return cap_safe_nice(p);
793}
794
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800795/*
David Howells1d045982008-11-14 10:39:24 +1100796 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
797 * the current task's bounding set. Returns 0 on success, -ve on error.
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800798 */
David Howellsd84f4f92008-11-14 10:39:23 +1100799static long cap_prctl_drop(struct cred *new, unsigned long cap)
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800800{
801 if (!capable(CAP_SETPCAP))
802 return -EPERM;
803 if (!cap_valid(cap))
804 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +1100805
806 cap_lower(new->cap_bset, cap);
Serge E. Hallyn3b7391d2008-02-04 22:29:45 -0800807 return 0;
808}
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700809
Serge E. Hallynb5376772007-10-16 23:31:36 -0700810#else
811int cap_task_setscheduler (struct task_struct *p, int policy,
812 struct sched_param *lp)
813{
814 return 0;
815}
816int cap_task_setioprio (struct task_struct *p, int ioprio)
817{
818 return 0;
819}
820int cap_task_setnice (struct task_struct *p, int nice)
821{
822 return 0;
823}
Serge E. Hallynb5376772007-10-16 23:31:36 -0700824#endif
825
David Howells1d045982008-11-14 10:39:24 +1100826/**
827 * cap_task_prctl - Implement process control functions for this security module
828 * @option: The process control function requested
829 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
830 *
831 * Allow process control functions (sys_prctl()) to alter capabilities; may
832 * also deny access to other functions not otherwise implemented here.
833 *
834 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
835 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
836 * modules will consider performing the function.
837 */
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700838int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
David Howellsd84f4f92008-11-14 10:39:23 +1100839 unsigned long arg4, unsigned long arg5)
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700840{
David Howellsd84f4f92008-11-14 10:39:23 +1100841 struct cred *new;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700842 long error = 0;
843
David Howellsd84f4f92008-11-14 10:39:23 +1100844 new = prepare_creds();
845 if (!new)
846 return -ENOMEM;
847
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700848 switch (option) {
849 case PR_CAPBSET_READ:
David Howellsd84f4f92008-11-14 10:39:23 +1100850 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700851 if (!cap_valid(arg2))
David Howellsd84f4f92008-11-14 10:39:23 +1100852 goto error;
853 error = !!cap_raised(new->cap_bset, arg2);
854 goto no_change;
855
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700856#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
857 case PR_CAPBSET_DROP:
David Howellsd84f4f92008-11-14 10:39:23 +1100858 error = cap_prctl_drop(new, arg2);
859 if (error < 0)
860 goto error;
861 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700862
863 /*
864 * The next four prctl's remain to assist with transitioning a
865 * system from legacy UID=0 based privilege (when filesystem
866 * capabilities are not in use) to a system using filesystem
867 * capabilities only - as the POSIX.1e draft intended.
868 *
869 * Note:
870 *
871 * PR_SET_SECUREBITS =
872 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
873 * | issecure_mask(SECURE_NOROOT)
874 * | issecure_mask(SECURE_NOROOT_LOCKED)
875 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
876 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
877 *
878 * will ensure that the current process and all of its
879 * children will be locked into a pure
880 * capability-based-privilege environment.
881 */
882 case PR_SET_SECUREBITS:
David Howellsd84f4f92008-11-14 10:39:23 +1100883 error = -EPERM;
884 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
885 & (new->securebits ^ arg2)) /*[1]*/
886 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
887 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
David Howells14eaddc2008-12-31 15:15:42 +0000888 || (cap_capable(CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0) /*[4]*/
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700889 /*
890 * [1] no changing of bits that are locked
891 * [2] no unlocking of locks
892 * [3] no setting of unsupported bits
893 * [4] doing anything requires privilege (go read about
894 * the "sendmail capabilities bug")
895 */
David Howellsd84f4f92008-11-14 10:39:23 +1100896 )
897 /* cannot change a locked bit */
898 goto error;
899 new->securebits = arg2;
900 goto changed;
901
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700902 case PR_GET_SECUREBITS:
David Howellsd84f4f92008-11-14 10:39:23 +1100903 error = new->securebits;
904 goto no_change;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700905
906#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
907
908 case PR_GET_KEEPCAPS:
909 if (issecure(SECURE_KEEP_CAPS))
910 error = 1;
David Howellsd84f4f92008-11-14 10:39:23 +1100911 goto no_change;
912
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700913 case PR_SET_KEEPCAPS:
David Howellsd84f4f92008-11-14 10:39:23 +1100914 error = -EINVAL;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700915 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
David Howellsd84f4f92008-11-14 10:39:23 +1100916 goto error;
917 error = -EPERM;
918 if (issecure(SECURE_KEEP_CAPS_LOCKED))
919 goto error;
920 if (arg2)
921 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700922 else
David Howellsd84f4f92008-11-14 10:39:23 +1100923 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
924 goto changed;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700925
926 default:
927 /* No functionality available - continue with default */
David Howellsd84f4f92008-11-14 10:39:23 +1100928 error = -ENOSYS;
929 goto error;
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700930 }
931
932 /* Functionality provided */
David Howellsd84f4f92008-11-14 10:39:23 +1100933changed:
934 return commit_creds(new);
Andrew G. Morgan3898b1b2008-04-28 02:13:40 -0700935
David Howellsd84f4f92008-11-14 10:39:23 +1100936no_change:
937 error = 0;
938error:
939 abort_creds(new);
940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
David Howells1d045982008-11-14 10:39:24 +1100943/**
944 * cap_syslog - Determine whether syslog function is permitted
945 * @type: Function requested
946 *
947 * Determine whether the current process is permitted to use a particular
948 * syslog function, returning 0 if permission is granted, -ve if not.
949 */
950int cap_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
953 return -EPERM;
954 return 0;
955}
956
David Howells1d045982008-11-14 10:39:24 +1100957/**
958 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
959 * @mm: The VM space in which the new mapping is to be made
960 * @pages: The size of the mapping
961 *
962 * Determine whether the allocation of a new virtual mapping by the current
963 * task is permitted, returning 0 if permission is granted, -ve if not.
964 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700965int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 int cap_sys_admin = 0;
968
David Howells14eaddc2008-12-31 15:15:42 +0000969 if (cap_capable(CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700971 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}