aboutsummaryrefslogtreecommitdiffstats
path: root/security/commoncap.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/commoncap.c')
-rw-r--r--security/commoncap.c830
1 files changed, 537 insertions, 293 deletions
diff --git a/security/commoncap.c b/security/commoncap.c
index 3976613db829..79713545cd63 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -8,6 +8,7 @@
8 */ 8 */
9 9
10#include <linux/capability.h> 10#include <linux/capability.h>
11#include <linux/audit.h>
11#include <linux/module.h> 12#include <linux/module.h>
12#include <linux/init.h> 13#include <linux/init.h>
13#include <linux/kernel.h> 14#include <linux/kernel.h>
@@ -29,7 +30,7 @@
29 30
30int cap_netlink_send(struct sock *sk, struct sk_buff *skb) 31int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
31{ 32{
32 NETLINK_CB(skb).eff_cap = current->cap_effective; 33 NETLINK_CB(skb).eff_cap = current_cap();
33 return 0; 34 return 0;
34} 35}
35 36
@@ -39,23 +40,41 @@ int cap_netlink_recv(struct sk_buff *skb, int cap)
39 return -EPERM; 40 return -EPERM;
40 return 0; 41 return 0;
41} 42}
42
43EXPORT_SYMBOL(cap_netlink_recv); 43EXPORT_SYMBOL(cap_netlink_recv);
44 44
45/* 45/**
46 * cap_capable - Determine whether a task has a particular effective capability
47 * @tsk: The task to query
48 * @cap: The capability to check for
49 * @audit: Whether to write an audit message or not
50 *
51 * Determine whether the nominated task has the specified capability amongst
52 * its effective set, returning 0 if it does, -ve if it does not.
53 *
46 * NOTE WELL: cap_capable() cannot be used like the kernel's capable() 54 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
47 * function. That is, it has the reverse semantics: cap_capable() 55 * function. That is, it has the reverse semantics: cap_capable() returns 0
48 * returns 0 when a task has a capability, but the kernel's capable() 56 * when a task has a capability, but the kernel's capable() returns 1 for this
49 * returns 1 for this case. 57 * case.
50 */ 58 */
51int cap_capable (struct task_struct *tsk, int cap) 59int cap_capable(struct task_struct *tsk, int cap, int audit)
52{ 60{
61 __u32 cap_raised;
62
53 /* Derived from include/linux/sched.h:capable. */ 63 /* Derived from include/linux/sched.h:capable. */
54 if (cap_raised(tsk->cap_effective, cap)) 64 rcu_read_lock();
55 return 0; 65 cap_raised = cap_raised(__task_cred(tsk)->cap_effective, cap);
56 return -EPERM; 66 rcu_read_unlock();
67 return cap_raised ? 0 : -EPERM;
57} 68}
58 69
70/**
71 * cap_settime - Determine whether the current process may set the system clock
72 * @ts: The time to set
73 * @tz: The timezone to set
74 *
75 * Determine whether the current process may set the system clock and timezone
76 * information, returning 0 if permission granted, -ve if denied.
77 */
59int cap_settime(struct timespec *ts, struct timezone *tz) 78int cap_settime(struct timespec *ts, struct timezone *tz)
60{ 79{
61 if (!capable(CAP_SYS_TIME)) 80 if (!capable(CAP_SYS_TIME))
@@ -63,121 +82,157 @@ int cap_settime(struct timespec *ts, struct timezone *tz)
63 return 0; 82 return 0;
64} 83}
65 84
85/**
86 * cap_ptrace_may_access - Determine whether the current process may access
87 * another
88 * @child: The process to be accessed
89 * @mode: The mode of attachment.
90 *
91 * Determine whether a process may access another, returning 0 if permission
92 * granted, -ve if denied.
93 */
66int cap_ptrace_may_access(struct task_struct *child, unsigned int mode) 94int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
67{ 95{
68 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ 96 int ret = 0;
69 if (cap_issubset(child->cap_permitted, current->cap_permitted)) 97
70 return 0; 98 rcu_read_lock();
71 if (capable(CAP_SYS_PTRACE)) 99 if (!cap_issubset(__task_cred(child)->cap_permitted,
72 return 0; 100 current_cred()->cap_permitted) &&
73 return -EPERM; 101 !capable(CAP_SYS_PTRACE))
102 ret = -EPERM;
103 rcu_read_unlock();
104 return ret;
74} 105}
75 106
107/**
108 * cap_ptrace_traceme - Determine whether another process may trace the current
109 * @parent: The task proposed to be the tracer
110 *
111 * Determine whether the nominated task is permitted to trace the current
112 * process, returning 0 if permission is granted, -ve if denied.
113 */
76int cap_ptrace_traceme(struct task_struct *parent) 114int cap_ptrace_traceme(struct task_struct *parent)
77{ 115{
78 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ 116 int ret = 0;
79 if (cap_issubset(current->cap_permitted, parent->cap_permitted)) 117
80 return 0; 118 rcu_read_lock();
81 if (has_capability(parent, CAP_SYS_PTRACE)) 119 if (!cap_issubset(current_cred()->cap_permitted,
82 return 0; 120 __task_cred(parent)->cap_permitted) &&
83 return -EPERM; 121 !has_capability(parent, CAP_SYS_PTRACE))
122 ret = -EPERM;
123 rcu_read_unlock();
124 return ret;
84} 125}
85 126
86int cap_capget (struct task_struct *target, kernel_cap_t *effective, 127/**
87 kernel_cap_t *inheritable, kernel_cap_t *permitted) 128 * cap_capget - Retrieve a task's capability sets
129 * @target: The task from which to retrieve the capability sets
130 * @effective: The place to record the effective set
131 * @inheritable: The place to record the inheritable set
132 * @permitted: The place to record the permitted set
133 *
134 * This function retrieves the capabilities of the nominated task and returns
135 * them to the caller.
136 */
137int cap_capget(struct task_struct *target, kernel_cap_t *effective,
138 kernel_cap_t *inheritable, kernel_cap_t *permitted)
88{ 139{
140 const struct cred *cred;
141
89 /* Derived from kernel/capability.c:sys_capget. */ 142 /* Derived from kernel/capability.c:sys_capget. */
90 *effective = target->cap_effective; 143 rcu_read_lock();
91 *inheritable = target->cap_inheritable; 144 cred = __task_cred(target);
92 *permitted = target->cap_permitted; 145 *effective = cred->cap_effective;
146 *inheritable = cred->cap_inheritable;
147 *permitted = cred->cap_permitted;
148 rcu_read_unlock();
93 return 0; 149 return 0;
94} 150}
95 151
96#ifdef CONFIG_SECURITY_FILE_CAPABILITIES 152/*
97 153 * Determine whether the inheritable capabilities are limited to the old
98static inline int cap_block_setpcap(struct task_struct *target) 154 * permitted set. Returns 1 if they are limited, 0 if they are not.
99{ 155 */
100 /*
101 * No support for remote process capability manipulation with
102 * filesystem capability support.
103 */
104 return (target != current);
105}
106
107static inline int cap_inh_is_capped(void) 156static inline int cap_inh_is_capped(void)
108{ 157{
109 /* 158#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
110 * Return 1 if changes to the inheritable set are limited
111 * to the old permitted set. That is, if the current task
112 * does *not* possess the CAP_SETPCAP capability.
113 */
114 return (cap_capable(current, CAP_SETPCAP) != 0);
115}
116
117static inline int cap_limit_ptraced_target(void) { return 1; }
118
119#else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
120 159
121static inline int cap_block_setpcap(struct task_struct *t) { return 0; } 160 /* they are so limited unless the current task has the CAP_SETPCAP
122static inline int cap_inh_is_capped(void) { return 1; } 161 * capability
123static inline int cap_limit_ptraced_target(void) 162 */
124{ 163 if (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
125 return !capable(CAP_SETPCAP); 164 return 0;
165#endif
166 return 1;
126} 167}
127 168
128#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ 169/**
129 170 * cap_capset - Validate and apply proposed changes to current's capabilities
130int cap_capset_check (struct task_struct *target, kernel_cap_t *effective, 171 * @new: The proposed new credentials; alterations should be made here
131 kernel_cap_t *inheritable, kernel_cap_t *permitted) 172 * @old: The current task's current credentials
132{ 173 * @effective: A pointer to the proposed new effective capabilities set
133 if (cap_block_setpcap(target)) { 174 * @inheritable: A pointer to the proposed new inheritable capabilities set
134 return -EPERM; 175 * @permitted: A pointer to the proposed new permitted capabilities set
135 } 176 *
136 if (cap_inh_is_capped() 177 * This function validates and applies a proposed mass change to the current
137 && !cap_issubset(*inheritable, 178 * process's capability sets. The changes are made to the proposed new
138 cap_combine(target->cap_inheritable, 179 * credentials, and assuming no error, will be committed by the caller of LSM.
139 current->cap_permitted))) { 180 */
181int cap_capset(struct cred *new,
182 const struct cred *old,
183 const kernel_cap_t *effective,
184 const kernel_cap_t *inheritable,
185 const kernel_cap_t *permitted)
186{
187 if (cap_inh_is_capped() &&
188 !cap_issubset(*inheritable,
189 cap_combine(old->cap_inheritable,
190 old->cap_permitted)))
140 /* incapable of using this inheritable set */ 191 /* incapable of using this inheritable set */
141 return -EPERM; 192 return -EPERM;
142 } 193
143 if (!cap_issubset(*inheritable, 194 if (!cap_issubset(*inheritable,
144 cap_combine(target->cap_inheritable, 195 cap_combine(old->cap_inheritable,
145 current->cap_bset))) { 196 old->cap_bset)))
146 /* no new pI capabilities outside bounding set */ 197 /* no new pI capabilities outside bounding set */
147 return -EPERM; 198 return -EPERM;
148 }
149 199
150 /* verify restrictions on target's new Permitted set */ 200 /* verify restrictions on target's new Permitted set */
151 if (!cap_issubset (*permitted, 201 if (!cap_issubset(*permitted, old->cap_permitted))
152 cap_combine (target->cap_permitted,
153 current->cap_permitted))) {
154 return -EPERM; 202 return -EPERM;
155 }
156 203
157 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ 204 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
158 if (!cap_issubset (*effective, *permitted)) { 205 if (!cap_issubset(*effective, *permitted))
159 return -EPERM; 206 return -EPERM;
160 }
161 207
208 new->cap_effective = *effective;
209 new->cap_inheritable = *inheritable;
210 new->cap_permitted = *permitted;
162 return 0; 211 return 0;
163} 212}
164 213
165void cap_capset_set (struct task_struct *target, kernel_cap_t *effective, 214/*
166 kernel_cap_t *inheritable, kernel_cap_t *permitted) 215 * Clear proposed capability sets for execve().
167{ 216 */
168 target->cap_effective = *effective;
169 target->cap_inheritable = *inheritable;
170 target->cap_permitted = *permitted;
171}
172
173static inline void bprm_clear_caps(struct linux_binprm *bprm) 217static inline void bprm_clear_caps(struct linux_binprm *bprm)
174{ 218{
175 cap_clear(bprm->cap_post_exec_permitted); 219 cap_clear(bprm->cred->cap_permitted);
176 bprm->cap_effective = false; 220 bprm->cap_effective = false;
177} 221}
178 222
179#ifdef CONFIG_SECURITY_FILE_CAPABILITIES 223#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
180 224
225/**
226 * cap_inode_need_killpriv - Determine if inode change affects privileges
227 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
228 *
229 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
230 * affects the security markings on that inode, and if it is, should
231 * inode_killpriv() be invoked or the change rejected?
232 *
233 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
234 * -ve to deny the change.
235 */
181int cap_inode_need_killpriv(struct dentry *dentry) 236int cap_inode_need_killpriv(struct dentry *dentry)
182{ 237{
183 struct inode *inode = dentry->d_inode; 238 struct inode *inode = dentry->d_inode;
@@ -192,6 +247,14 @@ int cap_inode_need_killpriv(struct dentry *dentry)
192 return 1; 247 return 1;
193} 248}
194 249
250/**
251 * cap_inode_killpriv - Erase the security markings on an inode
252 * @dentry: The inode/dentry to alter
253 *
254 * Erase the privilege-enhancing security markings on an inode.
255 *
256 * Returns 0 if successful, -ve on error.
257 */
195int cap_inode_killpriv(struct dentry *dentry) 258int cap_inode_killpriv(struct dentry *dentry)
196{ 259{
197 struct inode *inode = dentry->d_inode; 260 struct inode *inode = dentry->d_inode;
@@ -202,19 +265,75 @@ int cap_inode_killpriv(struct dentry *dentry)
202 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); 265 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
203} 266}
204 267
205static inline int cap_from_disk(struct vfs_cap_data *caps, 268/*
206 struct linux_binprm *bprm, unsigned size) 269 * Calculate the new process capability sets from the capability sets attached
270 * to a file.
271 */
272static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
273 struct linux_binprm *bprm,
274 bool *effective)
275{
276 struct cred *new = bprm->cred;
277 unsigned i;
278 int ret = 0;
279
280 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
281 *effective = true;
282
283 CAP_FOR_EACH_U32(i) {
284 __u32 permitted = caps->permitted.cap[i];
285 __u32 inheritable = caps->inheritable.cap[i];
286
287 /*
288 * pP' = (X & fP) | (pI & fI)
289 */
290 new->cap_permitted.cap[i] =
291 (new->cap_bset.cap[i] & permitted) |
292 (new->cap_inheritable.cap[i] & inheritable);
293
294 if (permitted & ~new->cap_permitted.cap[i])
295 /* insufficient to execute correctly */
296 ret = -EPERM;
297 }
298
299 /*
300 * For legacy apps, with no internal support for recognizing they
301 * do not have enough capabilities, we return an error if they are
302 * missing some "forced" (aka file-permitted) capabilities.
303 */
304 return *effective ? ret : 0;
305}
306
307/*
308 * Extract the on-exec-apply capability sets for an executable file.
309 */
310int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
207{ 311{
312 struct inode *inode = dentry->d_inode;
208 __u32 magic_etc; 313 __u32 magic_etc;
209 unsigned tocopy, i; 314 unsigned tocopy, i;
210 int ret; 315 int size;
316 struct vfs_cap_data caps;
317
318 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
319
320 if (!inode || !inode->i_op || !inode->i_op->getxattr)
321 return -ENODATA;
322
323 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
324 XATTR_CAPS_SZ);
325 if (size == -ENODATA || size == -EOPNOTSUPP)
326 /* no data, that's ok */
327 return -ENODATA;
328 if (size < 0)
329 return size;
211 330
212 if (size < sizeof(magic_etc)) 331 if (size < sizeof(magic_etc))
213 return -EINVAL; 332 return -EINVAL;
214 333
215 magic_etc = le32_to_cpu(caps->magic_etc); 334 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
216 335
217 switch ((magic_etc & VFS_CAP_REVISION_MASK)) { 336 switch (magic_etc & VFS_CAP_REVISION_MASK) {
218 case VFS_CAP_REVISION_1: 337 case VFS_CAP_REVISION_1:
219 if (size != XATTR_CAPS_SZ_1) 338 if (size != XATTR_CAPS_SZ_1)
220 return -EINVAL; 339 return -EINVAL;
@@ -229,77 +348,48 @@ static inline int cap_from_disk(struct vfs_cap_data *caps,
229 return -EINVAL; 348 return -EINVAL;
230 } 349 }
231 350
232 if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE) {
233 bprm->cap_effective = true;
234 } else {
235 bprm->cap_effective = false;
236 }
237
238 ret = 0;
239
240 CAP_FOR_EACH_U32(i) { 351 CAP_FOR_EACH_U32(i) {
241 __u32 value_cpu; 352 if (i >= tocopy)
242 353 break;
243 if (i >= tocopy) { 354 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
244 /* 355 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
245 * Legacy capability sets have no upper bits
246 */
247 bprm->cap_post_exec_permitted.cap[i] = 0;
248 continue;
249 }
250 /*
251 * pP' = (X & fP) | (pI & fI)
252 */
253 value_cpu = le32_to_cpu(caps->data[i].permitted);
254 bprm->cap_post_exec_permitted.cap[i] =
255 (current->cap_bset.cap[i] & value_cpu) |
256 (current->cap_inheritable.cap[i] &
257 le32_to_cpu(caps->data[i].inheritable));
258 if (value_cpu & ~bprm->cap_post_exec_permitted.cap[i]) {
259 /*
260 * insufficient to execute correctly
261 */
262 ret = -EPERM;
263 }
264 } 356 }
265 357
266 /* 358 return 0;
267 * For legacy apps, with no internal support for recognizing they
268 * do not have enough capabilities, we return an error if they are
269 * missing some "forced" (aka file-permitted) capabilities.
270 */
271 return bprm->cap_effective ? ret : 0;
272} 359}
273 360
274/* Locate any VFS capabilities: */ 361/*
275static int get_file_caps(struct linux_binprm *bprm) 362 * Attempt to get the on-exec apply capability sets for an executable file from
363 * its xattrs and, if present, apply them to the proposed credentials being
364 * constructed by execve().
365 */
366static int get_file_caps(struct linux_binprm *bprm, bool *effective)
276{ 367{
277 struct dentry *dentry; 368 struct dentry *dentry;
278 int rc = 0; 369 int rc = 0;
279 struct vfs_cap_data vcaps; 370 struct cpu_vfs_cap_data vcaps;
280 struct inode *inode;
281 371
282 bprm_clear_caps(bprm); 372 bprm_clear_caps(bprm);
283 373
374 if (!file_caps_enabled)
375 return 0;
376
284 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) 377 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
285 return 0; 378 return 0;
286 379
287 dentry = dget(bprm->file->f_dentry); 380 dentry = dget(bprm->file->f_dentry);
288 inode = dentry->d_inode;
289 if (!inode->i_op || !inode->i_op->getxattr)
290 goto out;
291 381
292 rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &vcaps, 382 rc = get_vfs_caps_from_disk(dentry, &vcaps);
293 XATTR_CAPS_SZ); 383 if (rc < 0) {
294 if (rc == -ENODATA || rc == -EOPNOTSUPP) { 384 if (rc == -EINVAL)
295 /* no data, that's ok */ 385 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
296 rc = 0; 386 __func__, rc, bprm->filename);
387 else if (rc == -ENODATA)
388 rc = 0;
297 goto out; 389 goto out;
298 } 390 }
299 if (rc < 0)
300 goto out;
301 391
302 rc = cap_from_disk(&vcaps, bprm, rc); 392 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
303 if (rc == -EINVAL) 393 if (rc == -EINVAL)
304 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n", 394 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
305 __func__, rc, bprm->filename); 395 __func__, rc, bprm->filename);
@@ -323,18 +413,57 @@ int cap_inode_killpriv(struct dentry *dentry)
323 return 0; 413 return 0;
324} 414}
325 415
326static inline int get_file_caps(struct linux_binprm *bprm) 416int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
417{
418 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
419 return -ENODATA;
420}
421
422static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
327{ 423{
328 bprm_clear_caps(bprm); 424 bprm_clear_caps(bprm);
329 return 0; 425 return 0;
330} 426}
331#endif 427#endif
332 428
333int cap_bprm_set_security (struct linux_binprm *bprm) 429/*
430 * Determine whether a exec'ing process's new permitted capabilities should be
431 * limited to just what it already has.
432 *
433 * This prevents processes that are being ptraced from gaining access to
434 * CAP_SETPCAP, unless the process they're tracing already has it, and the
435 * binary they're executing has filecaps that elevate it.
436 *
437 * Returns 1 if they should be limited, 0 if they are not.
438 */
439static inline int cap_limit_ptraced_target(void)
440{
441#ifndef CONFIG_SECURITY_FILE_CAPABILITIES
442 if (capable(CAP_SETPCAP))
443 return 0;
444#endif
445 return 1;
446}
447
448/**
449 * cap_bprm_set_creds - Set up the proposed credentials for execve().
450 * @bprm: The execution parameters, including the proposed creds
451 *
452 * Set up the proposed credentials for a new execution context being
453 * constructed by execve(). The proposed creds in @bprm->cred is altered,
454 * which won't take effect immediately. Returns 0 if successful, -ve on error.
455 */
456int cap_bprm_set_creds(struct linux_binprm *bprm)
334{ 457{
458 const struct cred *old = current_cred();
459 struct cred *new = bprm->cred;
460 bool effective;
335 int ret; 461 int ret;
336 462
337 ret = get_file_caps(bprm); 463 effective = false;
464 ret = get_file_caps(bprm, &effective);
465 if (ret < 0)
466 return ret;
338 467
339 if (!issecure(SECURE_NOROOT)) { 468 if (!issecure(SECURE_NOROOT)) {
340 /* 469 /*
@@ -342,75 +471,113 @@ int cap_bprm_set_security (struct linux_binprm *bprm)
342 * executables under compatibility mode, we override the 471 * executables under compatibility mode, we override the
343 * capability sets for the file. 472 * capability sets for the file.
344 * 473 *
345 * If only the real uid is 0, we do not set the effective 474 * If only the real uid is 0, we do not set the effective bit.
346 * bit.
347 */ 475 */
348 if (bprm->e_uid == 0 || current->uid == 0) { 476 if (new->euid == 0 || new->uid == 0) {
349 /* pP' = (cap_bset & ~0) | (pI & ~0) */ 477 /* pP' = (cap_bset & ~0) | (pI & ~0) */
350 bprm->cap_post_exec_permitted = cap_combine( 478 new->cap_permitted = cap_combine(old->cap_bset,
351 current->cap_bset, current->cap_inheritable 479 old->cap_inheritable);
352 );
353 bprm->cap_effective = (bprm->e_uid == 0);
354 ret = 0;
355 } 480 }
481 if (new->euid == 0)
482 effective = true;
356 } 483 }
357 484
358 return ret; 485 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
359} 486 * credentials unless they have the appropriate permit
360 487 */
361void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe) 488 if ((new->euid != old->uid ||
362{ 489 new->egid != old->gid ||
363 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid || 490 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
364 !cap_issubset(bprm->cap_post_exec_permitted, 491 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
365 current->cap_permitted)) { 492 /* downgrade; they get no more than they had, and maybe less */
366 set_dumpable(current->mm, suid_dumpable); 493 if (!capable(CAP_SETUID)) {
367 current->pdeath_signal = 0; 494 new->euid = new->uid;
368 495 new->egid = new->gid;
369 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
370 if (!capable(CAP_SETUID)) {
371 bprm->e_uid = current->uid;
372 bprm->e_gid = current->gid;
373 }
374 if (cap_limit_ptraced_target()) {
375 bprm->cap_post_exec_permitted = cap_intersect(
376 bprm->cap_post_exec_permitted,
377 current->cap_permitted);
378 }
379 } 496 }
497 if (cap_limit_ptraced_target())
498 new->cap_permitted = cap_intersect(new->cap_permitted,
499 old->cap_permitted);
380 } 500 }
381 501
382 current->suid = current->euid = current->fsuid = bprm->e_uid; 502 new->suid = new->fsuid = new->euid;
383 current->sgid = current->egid = current->fsgid = bprm->e_gid; 503 new->sgid = new->fsgid = new->egid;
384 504
385 /* For init, we want to retain the capabilities set 505 /* For init, we want to retain the capabilities set in the initial
386 * in the init_task struct. Thus we skip the usual 506 * task. Thus we skip the usual capability rules
387 * capability rules */ 507 */
388 if (!is_global_init(current)) { 508 if (!is_global_init(current)) {
389 current->cap_permitted = bprm->cap_post_exec_permitted; 509 if (effective)
390 if (bprm->cap_effective) 510 new->cap_effective = new->cap_permitted;
391 current->cap_effective = bprm->cap_post_exec_permitted;
392 else 511 else
393 cap_clear(current->cap_effective); 512 cap_clear(new->cap_effective);
394 } 513 }
514 bprm->cap_effective = effective;
395 515
396 /* AUD: Audit candidate if current->cap_effective is set */ 516 /*
517 * Audit candidate if current->cap_effective is set
518 *
519 * We do not bother to audit if 3 things are true:
520 * 1) cap_effective has all caps
521 * 2) we are root
522 * 3) root is supposed to have all caps (SECURE_NOROOT)
523 * Since this is just a normal root execing a process.
524 *
525 * Number 1 above might fail if you don't have a full bset, but I think
526 * that is interesting information to audit.
527 */
528 if (!cap_isclear(new->cap_effective)) {
529 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
530 new->euid != 0 || new->uid != 0 ||
531 issecure(SECURE_NOROOT)) {
532 ret = audit_log_bprm_fcaps(bprm, new, old);
533 if (ret < 0)
534 return ret;
535 }
536 }
397 537
398 current->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); 538 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
539 return 0;
399} 540}
400 541
401int cap_bprm_secureexec (struct linux_binprm *bprm) 542/**
543 * cap_bprm_secureexec - Determine whether a secure execution is required
544 * @bprm: The execution parameters
545 *
546 * Determine whether a secure execution is required, return 1 if it is, and 0
547 * if it is not.
548 *
549 * The credentials have been committed by this point, and so are no longer
550 * available through @bprm->cred.
551 */
552int cap_bprm_secureexec(struct linux_binprm *bprm)
402{ 553{
403 if (current->uid != 0) { 554 const struct cred *cred = current_cred();
555
556 if (cred->uid != 0) {
404 if (bprm->cap_effective) 557 if (bprm->cap_effective)
405 return 1; 558 return 1;
406 if (!cap_isclear(bprm->cap_post_exec_permitted)) 559 if (!cap_isclear(cred->cap_permitted))
407 return 1; 560 return 1;
408 } 561 }
409 562
410 return (current->euid != current->uid || 563 return (cred->euid != cred->uid ||
411 current->egid != current->gid); 564 cred->egid != cred->gid);
412} 565}
413 566
567/**
568 * cap_inode_setxattr - Determine whether an xattr may be altered
569 * @dentry: The inode/dentry being altered
570 * @name: The name of the xattr to be changed
571 * @value: The value that the xattr will be changed to
572 * @size: The size of value
573 * @flags: The replacement flag
574 *
575 * Determine whether an xattr may be altered or set on an inode, returning 0 if
576 * permission is granted, -ve if denied.
577 *
578 * This is used to make sure security xattrs don't get updated or set by those
579 * who aren't privileged to do so.
580 */
414int cap_inode_setxattr(struct dentry *dentry, const char *name, 581int cap_inode_setxattr(struct dentry *dentry, const char *name,
415 const void *value, size_t size, int flags) 582 const void *value, size_t size, int flags)
416{ 583{
@@ -418,28 +585,42 @@ int cap_inode_setxattr(struct dentry *dentry, const char *name,
418 if (!capable(CAP_SETFCAP)) 585 if (!capable(CAP_SETFCAP))
419 return -EPERM; 586 return -EPERM;
420 return 0; 587 return 0;
421 } else if (!strncmp(name, XATTR_SECURITY_PREFIX, 588 }
589
590 if (!strncmp(name, XATTR_SECURITY_PREFIX,
422 sizeof(XATTR_SECURITY_PREFIX) - 1) && 591 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
423 !capable(CAP_SYS_ADMIN)) 592 !capable(CAP_SYS_ADMIN))
424 return -EPERM; 593 return -EPERM;
425 return 0; 594 return 0;
426} 595}
427 596
597/**
598 * cap_inode_removexattr - Determine whether an xattr may be removed
599 * @dentry: The inode/dentry being altered
600 * @name: The name of the xattr to be changed
601 *
602 * Determine whether an xattr may be removed from an inode, returning 0 if
603 * permission is granted, -ve if denied.
604 *
605 * This is used to make sure security xattrs don't get removed by those who
606 * aren't privileged to remove them.
607 */
428int cap_inode_removexattr(struct dentry *dentry, const char *name) 608int cap_inode_removexattr(struct dentry *dentry, const char *name)
429{ 609{
430 if (!strcmp(name, XATTR_NAME_CAPS)) { 610 if (!strcmp(name, XATTR_NAME_CAPS)) {
431 if (!capable(CAP_SETFCAP)) 611 if (!capable(CAP_SETFCAP))
432 return -EPERM; 612 return -EPERM;
433 return 0; 613 return 0;
434 } else if (!strncmp(name, XATTR_SECURITY_PREFIX, 614 }
615
616 if (!strncmp(name, XATTR_SECURITY_PREFIX,
435 sizeof(XATTR_SECURITY_PREFIX) - 1) && 617 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
436 !capable(CAP_SYS_ADMIN)) 618 !capable(CAP_SYS_ADMIN))
437 return -EPERM; 619 return -EPERM;
438 return 0; 620 return 0;
439} 621}
440 622
441/* moved from kernel/sys.c. */ 623/*
442/*
443 * cap_emulate_setxuid() fixes the effective / permitted capabilities of 624 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
444 * a process after a call to setuid, setreuid, or setresuid. 625 * a process after a call to setuid, setreuid, or setresuid.
445 * 626 *
@@ -453,10 +634,10 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
453 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective 634 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
454 * capabilities are set to the permitted capabilities. 635 * capabilities are set to the permitted capabilities.
455 * 636 *
456 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should 637 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
457 * never happen. 638 * never happen.
458 * 639 *
459 * -astor 640 * -astor
460 * 641 *
461 * cevans - New behaviour, Oct '99 642 * cevans - New behaviour, Oct '99
462 * A process may, via prctl(), elect to keep its capabilities when it 643 * A process may, via prctl(), elect to keep its capabilities when it
@@ -468,61 +649,60 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
468 * files.. 649 * files..
469 * Thanks to Olaf Kirch and Peter Benie for spotting this. 650 * Thanks to Olaf Kirch and Peter Benie for spotting this.
470 */ 651 */
471static inline void cap_emulate_setxuid (int old_ruid, int old_euid, 652static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
472 int old_suid)
473{ 653{
474 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) && 654 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
475 (current->uid != 0 && current->euid != 0 && current->suid != 0) && 655 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
476 !issecure(SECURE_KEEP_CAPS)) { 656 !issecure(SECURE_KEEP_CAPS)) {
477 cap_clear (current->cap_permitted); 657 cap_clear(new->cap_permitted);
478 cap_clear (current->cap_effective); 658 cap_clear(new->cap_effective);
479 }
480 if (old_euid == 0 && current->euid != 0) {
481 cap_clear (current->cap_effective);
482 }
483 if (old_euid != 0 && current->euid == 0) {
484 current->cap_effective = current->cap_permitted;
485 } 659 }
660 if (old->euid == 0 && new->euid != 0)
661 cap_clear(new->cap_effective);
662 if (old->euid != 0 && new->euid == 0)
663 new->cap_effective = new->cap_permitted;
486} 664}
487 665
488int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid, 666/**
489 int flags) 667 * cap_task_fix_setuid - Fix up the results of setuid() call
668 * @new: The proposed credentials
669 * @old: The current task's current credentials
670 * @flags: Indications of what has changed
671 *
672 * Fix up the results of setuid() call before the credential changes are
673 * actually applied, returning 0 to grant the changes, -ve to deny them.
674 */
675int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
490{ 676{
491 switch (flags) { 677 switch (flags) {
492 case LSM_SETID_RE: 678 case LSM_SETID_RE:
493 case LSM_SETID_ID: 679 case LSM_SETID_ID:
494 case LSM_SETID_RES: 680 case LSM_SETID_RES:
495 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */ 681 /* juggle the capabilities to follow [RES]UID changes unless
496 if (!issecure (SECURE_NO_SETUID_FIXUP)) { 682 * otherwise suppressed */
497 cap_emulate_setxuid (old_ruid, old_euid, old_suid); 683 if (!issecure(SECURE_NO_SETUID_FIXUP))
498 } 684 cap_emulate_setxuid(new, old);
499 break; 685 break;
500 case LSM_SETID_FS:
501 {
502 uid_t old_fsuid = old_ruid;
503 686
504 /* Copied from kernel/sys.c:setfsuid. */ 687 case LSM_SETID_FS:
505 688 /* juggle the capabilties to follow FSUID changes, unless
506 /* 689 * otherwise suppressed
507 * FIXME - is fsuser used for all CAP_FS_MASK capabilities? 690 *
508 * if not, we might be a bit too harsh here. 691 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
509 */ 692 * if not, we might be a bit too harsh here.
510 693 */
511 if (!issecure (SECURE_NO_SETUID_FIXUP)) { 694 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
512 if (old_fsuid == 0 && current->fsuid != 0) { 695 if (old->fsuid == 0 && new->fsuid != 0)
513 current->cap_effective = 696 new->cap_effective =
514 cap_drop_fs_set( 697 cap_drop_fs_set(new->cap_effective);
515 current->cap_effective); 698
516 } 699 if (old->fsuid != 0 && new->fsuid == 0)
517 if (old_fsuid != 0 && current->fsuid == 0) { 700 new->cap_effective =
518 current->cap_effective = 701 cap_raise_fs_set(new->cap_effective,
519 cap_raise_fs_set( 702 new->cap_permitted);
520 current->cap_effective,
521 current->cap_permitted);
522 }
523 }
524 break;
525 } 703 }
704 break;
705
526 default: 706 default:
527 return -EINVAL; 707 return -EINVAL;
528 } 708 }
@@ -543,42 +723,71 @@ int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
543 */ 723 */
544static int cap_safe_nice(struct task_struct *p) 724static int cap_safe_nice(struct task_struct *p)
545{ 725{
546 if (!cap_issubset(p->cap_permitted, current->cap_permitted) && 726 int is_subset;
547 !capable(CAP_SYS_NICE)) 727
728 rcu_read_lock();
729 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
730 current_cred()->cap_permitted);
731 rcu_read_unlock();
732
733 if (!is_subset && !capable(CAP_SYS_NICE))
548 return -EPERM; 734 return -EPERM;
549 return 0; 735 return 0;
550} 736}
551 737
552int cap_task_setscheduler (struct task_struct *p, int policy, 738/**
739 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
740 * @p: The task to affect
741 * @policy: The policy to effect
742 * @lp: The parameters to the scheduling policy
743 *
744 * Detemine if the requested scheduler policy change is permitted for the
745 * specified task, returning 0 if permission is granted, -ve if denied.
746 */
747int cap_task_setscheduler(struct task_struct *p, int policy,
553 struct sched_param *lp) 748 struct sched_param *lp)
554{ 749{
555 return cap_safe_nice(p); 750 return cap_safe_nice(p);
556} 751}
557 752
558int cap_task_setioprio (struct task_struct *p, int ioprio) 753/**
754 * cap_task_ioprio - Detemine if I/O priority change is permitted
755 * @p: The task to affect
756 * @ioprio: The I/O priority to set
757 *
758 * Detemine if the requested I/O priority change is permitted for the specified
759 * task, returning 0 if permission is granted, -ve if denied.
760 */
761int cap_task_setioprio(struct task_struct *p, int ioprio)
559{ 762{
560 return cap_safe_nice(p); 763 return cap_safe_nice(p);
561} 764}
562 765
563int cap_task_setnice (struct task_struct *p, int nice) 766/**
767 * cap_task_ioprio - Detemine if task priority change is permitted
768 * @p: The task to affect
769 * @nice: The nice value to set
770 *
771 * Detemine if the requested task priority change is permitted for the
772 * specified task, returning 0 if permission is granted, -ve if denied.
773 */
774int cap_task_setnice(struct task_struct *p, int nice)
564{ 775{
565 return cap_safe_nice(p); 776 return cap_safe_nice(p);
566} 777}
567 778
568/* 779/*
569 * called from kernel/sys.c for prctl(PR_CABSET_DROP) 780 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
570 * done without task_capability_lock() because it introduces 781 * the current task's bounding set. Returns 0 on success, -ve on error.
571 * no new races - i.e. only another task doing capget() on
572 * this task could get inconsistent info. There can be no
573 * racing writer bc a task can only change its own caps.
574 */ 782 */
575static long cap_prctl_drop(unsigned long cap) 783static long cap_prctl_drop(struct cred *new, unsigned long cap)
576{ 784{
577 if (!capable(CAP_SETPCAP)) 785 if (!capable(CAP_SETPCAP))
578 return -EPERM; 786 return -EPERM;
579 if (!cap_valid(cap)) 787 if (!cap_valid(cap))
580 return -EINVAL; 788 return -EINVAL;
581 cap_lower(current->cap_bset, cap); 789
790 cap_lower(new->cap_bset, cap);
582 return 0; 791 return 0;
583} 792}
584 793
@@ -598,22 +807,42 @@ int cap_task_setnice (struct task_struct *p, int nice)
598} 807}
599#endif 808#endif
600 809
810/**
811 * cap_task_prctl - Implement process control functions for this security module
812 * @option: The process control function requested
813 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
814 *
815 * Allow process control functions (sys_prctl()) to alter capabilities; may
816 * also deny access to other functions not otherwise implemented here.
817 *
818 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
819 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
820 * modules will consider performing the function.
821 */
601int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, 822int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
602 unsigned long arg4, unsigned long arg5, long *rc_p) 823 unsigned long arg4, unsigned long arg5)
603{ 824{
825 struct cred *new;
604 long error = 0; 826 long error = 0;
605 827
828 new = prepare_creds();
829 if (!new)
830 return -ENOMEM;
831
606 switch (option) { 832 switch (option) {
607 case PR_CAPBSET_READ: 833 case PR_CAPBSET_READ:
834 error = -EINVAL;
608 if (!cap_valid(arg2)) 835 if (!cap_valid(arg2))
609 error = -EINVAL; 836 goto error;
610 else 837 error = !!cap_raised(new->cap_bset, arg2);
611 error = !!cap_raised(current->cap_bset, arg2); 838 goto no_change;
612 break; 839
613#ifdef CONFIG_SECURITY_FILE_CAPABILITIES 840#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
614 case PR_CAPBSET_DROP: 841 case PR_CAPBSET_DROP:
615 error = cap_prctl_drop(arg2); 842 error = cap_prctl_drop(new, arg2);
616 break; 843 if (error < 0)
844 goto error;
845 goto changed;
617 846
618 /* 847 /*
619 * The next four prctl's remain to assist with transitioning a 848 * The next four prctl's remain to assist with transitioning a
@@ -635,12 +864,12 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
635 * capability-based-privilege environment. 864 * capability-based-privilege environment.
636 */ 865 */
637 case PR_SET_SECUREBITS: 866 case PR_SET_SECUREBITS:
638 if ((((current->securebits & SECURE_ALL_LOCKS) >> 1) 867 error = -EPERM;
639 & (current->securebits ^ arg2)) /*[1]*/ 868 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
640 || ((current->securebits & SECURE_ALL_LOCKS 869 & (new->securebits ^ arg2)) /*[1]*/
641 & ~arg2)) /*[2]*/ 870 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
642 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ 871 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
643 || (cap_capable(current, CAP_SETPCAP) != 0)) { /*[4]*/ 872 || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0) /*[4]*/
644 /* 873 /*
645 * [1] no changing of bits that are locked 874 * [1] no changing of bits that are locked
646 * [2] no unlocking of locks 875 * [2] no unlocking of locks
@@ -648,65 +877,80 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
648 * [4] doing anything requires privilege (go read about 877 * [4] doing anything requires privilege (go read about
649 * the "sendmail capabilities bug") 878 * the "sendmail capabilities bug")
650 */ 879 */
651 error = -EPERM; /* cannot change a locked bit */ 880 )
652 } else { 881 /* cannot change a locked bit */
653 current->securebits = arg2; 882 goto error;
654 } 883 new->securebits = arg2;
655 break; 884 goto changed;
885
656 case PR_GET_SECUREBITS: 886 case PR_GET_SECUREBITS:
657 error = current->securebits; 887 error = new->securebits;
658 break; 888 goto no_change;
659 889
660#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ 890#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
661 891
662 case PR_GET_KEEPCAPS: 892 case PR_GET_KEEPCAPS:
663 if (issecure(SECURE_KEEP_CAPS)) 893 if (issecure(SECURE_KEEP_CAPS))
664 error = 1; 894 error = 1;
665 break; 895 goto no_change;
896
666 case PR_SET_KEEPCAPS: 897 case PR_SET_KEEPCAPS:
898 error = -EINVAL;
667 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ 899 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
668 error = -EINVAL; 900 goto error;
669 else if (issecure(SECURE_KEEP_CAPS_LOCKED)) 901 error = -EPERM;
670 error = -EPERM; 902 if (issecure(SECURE_KEEP_CAPS_LOCKED))
671 else if (arg2) 903 goto error;
672 current->securebits |= issecure_mask(SECURE_KEEP_CAPS); 904 if (arg2)
905 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
673 else 906 else
674 current->securebits &= 907 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
675 ~issecure_mask(SECURE_KEEP_CAPS); 908 goto changed;
676 break;
677 909
678 default: 910 default:
679 /* No functionality available - continue with default */ 911 /* No functionality available - continue with default */
680 return 0; 912 error = -ENOSYS;
913 goto error;
681 } 914 }
682 915
683 /* Functionality provided */ 916 /* Functionality provided */
684 *rc_p = error; 917changed:
685 return 1; 918 return commit_creds(new);
686}
687 919
688void cap_task_reparent_to_init (struct task_struct *p) 920no_change:
689{ 921 error = 0;
690 cap_set_init_eff(p->cap_effective); 922error:
691 cap_clear(p->cap_inheritable); 923 abort_creds(new);
692 cap_set_full(p->cap_permitted); 924 return error;
693 p->securebits = SECUREBITS_DEFAULT;
694 return;
695} 925}
696 926
697int cap_syslog (int type) 927/**
928 * cap_syslog - Determine whether syslog function is permitted
929 * @type: Function requested
930 *
931 * Determine whether the current process is permitted to use a particular
932 * syslog function, returning 0 if permission is granted, -ve if not.
933 */
934int cap_syslog(int type)
698{ 935{
699 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN)) 936 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
700 return -EPERM; 937 return -EPERM;
701 return 0; 938 return 0;
702} 939}
703 940
941/**
942 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
943 * @mm: The VM space in which the new mapping is to be made
944 * @pages: The size of the mapping
945 *
946 * Determine whether the allocation of a new virtual mapping by the current
947 * task is permitted, returning 0 if permission is granted, -ve if not.
948 */
704int cap_vm_enough_memory(struct mm_struct *mm, long pages) 949int cap_vm_enough_memory(struct mm_struct *mm, long pages)
705{ 950{
706 int cap_sys_admin = 0; 951 int cap_sys_admin = 0;
707 952
708 if (cap_capable(current, CAP_SYS_ADMIN) == 0) 953 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
709 cap_sys_admin = 1; 954 cap_sys_admin = 1;
710 return __vm_enough_memory(mm, pages, cap_sys_admin); 955 return __vm_enough_memory(mm, pages, cap_sys_admin);
711} 956}
712