blob: 43f902750a1b6c802d650b6e9bbafde70ec8ae70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Common capabilities, needed by capability.o and root_plug.o
2 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/security.h>
15#include <linux/file.h>
16#include <linux/mm.h>
17#include <linux/mman.h>
18#include <linux/pagemap.h>
19#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/skbuff.h>
21#include <linux/netlink.h>
22#include <linux/ptrace.h>
23#include <linux/xattr.h>
24#include <linux/hugetlb.h>
Serge E. Hallynb5376772007-10-16 23:31:36 -070025#include <linux/mount.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070026#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Andrew Morgan72c2d582007-10-18 03:05:59 -070028#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
29/*
30 * Because of the reduced scope of CAP_SETPCAP when filesystem
31 * capabilities are in effect, it is safe to allow this capability to
32 * be available in the default configuration.
33 */
34# define CAP_INIT_BSET CAP_FULL_SET
35#else /* ie. ndef CONFIG_SECURITY_FILE_CAPABILITIES */
36# define CAP_INIT_BSET CAP_INIT_EFF_SET
37#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
38
39kernel_cap_t cap_bset = CAP_INIT_BSET; /* systemwide capability bound */
40EXPORT_SYMBOL(cap_bset);
41
42/* Global security state */
43
44unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
45EXPORT_SYMBOL(securebits);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
48{
49 NETLINK_CB(skb).eff_cap = current->cap_effective;
50 return 0;
51}
52
Darrel Goeddelc7bdb542006-06-27 13:26:11 -070053int cap_netlink_recv(struct sk_buff *skb, int cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Darrel Goeddelc7bdb542006-06-27 13:26:11 -070055 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -EPERM;
57 return 0;
58}
59
60EXPORT_SYMBOL(cap_netlink_recv);
61
62int cap_capable (struct task_struct *tsk, int cap)
63{
64 /* Derived from include/linux/sched.h:capable. */
65 if (cap_raised(tsk->cap_effective, cap))
66 return 0;
67 return -EPERM;
68}
69
70int cap_settime(struct timespec *ts, struct timezone *tz)
71{
72 if (!capable(CAP_SYS_TIME))
73 return -EPERM;
74 return 0;
75}
76
77int cap_ptrace (struct task_struct *parent, struct task_struct *child)
78{
79 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
Chris Wrightd4eb82c2006-03-25 03:07:41 -080080 if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
81 !__capable(parent, CAP_SYS_PTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -EPERM;
83 return 0;
84}
85
86int cap_capget (struct task_struct *target, kernel_cap_t *effective,
87 kernel_cap_t *inheritable, kernel_cap_t *permitted)
88{
89 /* Derived from kernel/capability.c:sys_capget. */
90 *effective = cap_t (target->cap_effective);
91 *inheritable = cap_t (target->cap_inheritable);
92 *permitted = cap_t (target->cap_permitted);
93 return 0;
94}
95
Andrew Morgan72c2d582007-10-18 03:05:59 -070096#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
97
98static inline int cap_block_setpcap(struct task_struct *target)
99{
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)
108{
109 /*
110 * return 1 if changes to the inheritable set are limited
111 * to the old permitted set.
112 */
113 return !cap_capable(current, CAP_SETPCAP);
114}
115
116#else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
117
118static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
119static inline int cap_inh_is_capped(void) { return 1; }
120
121#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
124 kernel_cap_t *inheritable, kernel_cap_t *permitted)
125{
Andrew Morgan72c2d582007-10-18 03:05:59 -0700126 if (cap_block_setpcap(target)) {
127 return -EPERM;
128 }
129 if (cap_inh_is_capped()
130 && !cap_issubset(*inheritable,
131 cap_combine(target->cap_inheritable,
132 current->cap_permitted))) {
133 /* incapable of using this inheritable set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EPERM;
135 }
136
137 /* verify restrictions on target's new Permitted set */
138 if (!cap_issubset (*permitted,
139 cap_combine (target->cap_permitted,
140 current->cap_permitted))) {
141 return -EPERM;
142 }
143
144 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
145 if (!cap_issubset (*effective, *permitted)) {
146 return -EPERM;
147 }
148
149 return 0;
150}
151
152void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
153 kernel_cap_t *inheritable, kernel_cap_t *permitted)
154{
155 target->cap_effective = *effective;
156 target->cap_inheritable = *inheritable;
157 target->cap_permitted = *permitted;
158}
159
Serge E. Hallynb5376772007-10-16 23:31:36 -0700160static inline void bprm_clear_caps(struct linux_binprm *bprm)
161{
162 cap_clear(bprm->cap_inheritable);
163 cap_clear(bprm->cap_permitted);
164 bprm->cap_effective = false;
165}
166
167#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
168
169int cap_inode_need_killpriv(struct dentry *dentry)
170{
171 struct inode *inode = dentry->d_inode;
172 int error;
173
174 if (!inode->i_op || !inode->i_op->getxattr)
175 return 0;
176
177 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
178 if (error <= 0)
179 return 0;
180 return 1;
181}
182
183int cap_inode_killpriv(struct dentry *dentry)
184{
185 struct inode *inode = dentry->d_inode;
186
187 if (!inode->i_op || !inode->i_op->removexattr)
188 return 0;
189
190 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
191}
192
193static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
194 int size)
195{
196 __u32 magic_etc;
197
198 if (size != XATTR_CAPS_SZ)
199 return -EINVAL;
200
201 magic_etc = le32_to_cpu(caps[0]);
202
203 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
204 case VFS_CAP_REVISION:
205 if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
206 bprm->cap_effective = true;
207 else
208 bprm->cap_effective = false;
209 bprm->cap_permitted = to_cap_t( le32_to_cpu(caps[1]) );
210 bprm->cap_inheritable = to_cap_t( le32_to_cpu(caps[2]) );
211 return 0;
212 default:
213 return -EINVAL;
214 }
215}
216
217/* Locate any VFS capabilities: */
218static int get_file_caps(struct linux_binprm *bprm)
219{
220 struct dentry *dentry;
221 int rc = 0;
222 __le32 v1caps[XATTR_CAPS_SZ];
223 struct inode *inode;
224
225 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
226 bprm_clear_caps(bprm);
227 return 0;
228 }
229
230 dentry = dget(bprm->file->f_dentry);
231 inode = dentry->d_inode;
232 if (!inode->i_op || !inode->i_op->getxattr)
233 goto out;
234
235 rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &v1caps,
236 XATTR_CAPS_SZ);
237 if (rc == -ENODATA || rc == -EOPNOTSUPP) {
238 /* no data, that's ok */
239 rc = 0;
240 goto out;
241 }
242 if (rc < 0)
243 goto out;
244
245 rc = cap_from_disk(v1caps, bprm, rc);
246 if (rc)
247 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
248 __FUNCTION__, rc, bprm->filename);
249
250out:
251 dput(dentry);
252 if (rc)
253 bprm_clear_caps(bprm);
254
255 return rc;
256}
257
258#else
259int cap_inode_need_killpriv(struct dentry *dentry)
260{
261 return 0;
262}
263
264int cap_inode_killpriv(struct dentry *dentry)
265{
266 return 0;
267}
268
269static inline int get_file_caps(struct linux_binprm *bprm)
270{
271 bprm_clear_caps(bprm);
272 return 0;
273}
274#endif
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276int cap_bprm_set_security (struct linux_binprm *bprm)
277{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700278 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Serge E. Hallynb5376772007-10-16 23:31:36 -0700280 ret = get_file_caps(bprm);
281 if (ret)
282 printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
283 __FUNCTION__, ret, bprm->filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* To support inheritance of root-permissions and suid-root
286 * executables under compatibility mode, we raise all three
287 * capability sets for the file.
288 *
289 * If only the real uid is 0, we only raise the inheritable
290 * and permitted sets of the executable file.
291 */
292
293 if (!issecure (SECURE_NOROOT)) {
294 if (bprm->e_uid == 0 || current->uid == 0) {
295 cap_set_full (bprm->cap_inheritable);
296 cap_set_full (bprm->cap_permitted);
297 }
298 if (bprm->e_uid == 0)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700299 bprm->cap_effective = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Serge E. Hallynb5376772007-10-16 23:31:36 -0700301
302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
306{
307 /* Derived from fs/exec.c:compute_creds. */
308 kernel_cap_t new_permitted, working;
309
310 new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
311 working = cap_intersect (bprm->cap_inheritable,
312 current->cap_inheritable);
313 new_permitted = cap_combine (new_permitted, working);
314
315 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
316 !cap_issubset (new_permitted, current->cap_permitted)) {
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700317 set_dumpable(current->mm, suid_dumpable);
Serge E. Hallynb5376772007-10-16 23:31:36 -0700318 current->pdeath_signal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
321 if (!capable(CAP_SETUID)) {
322 bprm->e_uid = current->uid;
323 bprm->e_gid = current->gid;
324 }
325 if (!capable (CAP_SETPCAP)) {
326 new_permitted = cap_intersect (new_permitted,
327 current->cap_permitted);
328 }
329 }
330 }
331
332 current->suid = current->euid = current->fsuid = bprm->e_uid;
333 current->sgid = current->egid = current->fsgid = bprm->e_gid;
334
335 /* For init, we want to retain the capabilities set
336 * in the init_task struct. Thus we skip the usual
337 * capability rules */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700338 if (!is_global_init(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 current->cap_permitted = new_permitted;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700340 current->cap_effective = bprm->cap_effective ?
341 new_permitted : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 /* AUD: Audit candidate if current->cap_effective is set */
345
346 current->keep_capabilities = 0;
347}
348
349int cap_bprm_secureexec (struct linux_binprm *bprm)
350{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700351 if (current->uid != 0) {
352 if (bprm->cap_effective)
353 return 1;
354 if (!cap_isclear(bprm->cap_permitted))
355 return 1;
356 if (!cap_isclear(bprm->cap_inheritable))
357 return 1;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return (current->euid != current->uid ||
361 current->egid != current->gid);
362}
363
364int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
365 size_t size, int flags)
366{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700367 if (!strcmp(name, XATTR_NAME_CAPS)) {
368 if (!capable(CAP_SETFCAP))
369 return -EPERM;
370 return 0;
371 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
373 !capable(CAP_SYS_ADMIN))
374 return -EPERM;
375 return 0;
376}
377
378int cap_inode_removexattr(struct dentry *dentry, char *name)
379{
Serge E. Hallynb5376772007-10-16 23:31:36 -0700380 if (!strcmp(name, XATTR_NAME_CAPS)) {
381 if (!capable(CAP_SETFCAP))
382 return -EPERM;
383 return 0;
384 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
386 !capable(CAP_SYS_ADMIN))
387 return -EPERM;
388 return 0;
389}
390
391/* moved from kernel/sys.c. */
392/*
393 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
394 * a process after a call to setuid, setreuid, or setresuid.
395 *
396 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
397 * {r,e,s}uid != 0, the permitted and effective capabilities are
398 * cleared.
399 *
400 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
401 * capabilities of the process are cleared.
402 *
403 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
404 * capabilities are set to the permitted capabilities.
405 *
406 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
407 * never happen.
408 *
409 * -astor
410 *
411 * cevans - New behaviour, Oct '99
412 * A process may, via prctl(), elect to keep its capabilities when it
413 * calls setuid() and switches away from uid==0. Both permitted and
414 * effective sets will be retained.
415 * Without this change, it was impossible for a daemon to drop only some
416 * of its privilege. The call to setuid(!=0) would drop all privileges!
417 * Keeping uid 0 is not an option because uid 0 owns too many vital
418 * files..
419 * Thanks to Olaf Kirch and Peter Benie for spotting this.
420 */
421static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
422 int old_suid)
423{
424 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
425 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
426 !current->keep_capabilities) {
427 cap_clear (current->cap_permitted);
428 cap_clear (current->cap_effective);
429 }
430 if (old_euid == 0 && current->euid != 0) {
431 cap_clear (current->cap_effective);
432 }
433 if (old_euid != 0 && current->euid == 0) {
434 current->cap_effective = current->cap_permitted;
435 }
436}
437
438int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
439 int flags)
440{
441 switch (flags) {
442 case LSM_SETID_RE:
443 case LSM_SETID_ID:
444 case LSM_SETID_RES:
445 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
446 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
447 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
448 }
449 break;
450 case LSM_SETID_FS:
451 {
452 uid_t old_fsuid = old_ruid;
453
454 /* Copied from kernel/sys.c:setfsuid. */
455
456 /*
457 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
458 * if not, we might be a bit too harsh here.
459 */
460
461 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
462 if (old_fsuid == 0 && current->fsuid != 0) {
463 cap_t (current->cap_effective) &=
464 ~CAP_FS_MASK;
465 }
466 if (old_fsuid != 0 && current->fsuid == 0) {
467 cap_t (current->cap_effective) |=
468 (cap_t (current->cap_permitted) &
469 CAP_FS_MASK);
470 }
471 }
472 break;
473 }
474 default:
475 return -EINVAL;
476 }
477
478 return 0;
479}
480
Serge E. Hallynb5376772007-10-16 23:31:36 -0700481#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
482/*
483 * Rationale: code calling task_setscheduler, task_setioprio, and
484 * task_setnice, assumes that
485 * . if capable(cap_sys_nice), then those actions should be allowed
486 * . if not capable(cap_sys_nice), but acting on your own processes,
487 * then those actions should be allowed
488 * This is insufficient now since you can call code without suid, but
489 * yet with increased caps.
490 * So we check for increased caps on the target process.
491 */
492static inline int cap_safe_nice(struct task_struct *p)
493{
494 if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
495 !__capable(current, CAP_SYS_NICE))
496 return -EPERM;
497 return 0;
498}
499
500int cap_task_setscheduler (struct task_struct *p, int policy,
501 struct sched_param *lp)
502{
503 return cap_safe_nice(p);
504}
505
506int cap_task_setioprio (struct task_struct *p, int ioprio)
507{
508 return cap_safe_nice(p);
509}
510
511int cap_task_setnice (struct task_struct *p, int nice)
512{
513 return cap_safe_nice(p);
514}
515
516int cap_task_kill(struct task_struct *p, struct siginfo *info,
517 int sig, u32 secid)
518{
519 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
520 return 0;
521
522 if (secid)
523 /*
524 * Signal sent as a particular user.
525 * Capabilities are ignored. May be wrong, but it's the
526 * only thing we can do at the moment.
527 * Used only by usb drivers?
528 */
529 return 0;
530 if (cap_issubset(p->cap_permitted, current->cap_permitted))
531 return 0;
532 if (capable(CAP_KILL))
533 return 0;
534
535 return -EPERM;
536}
537#else
538int cap_task_setscheduler (struct task_struct *p, int policy,
539 struct sched_param *lp)
540{
541 return 0;
542}
543int cap_task_setioprio (struct task_struct *p, int ioprio)
544{
545 return 0;
546}
547int cap_task_setnice (struct task_struct *p, int nice)
548{
549 return 0;
550}
551int cap_task_kill(struct task_struct *p, struct siginfo *info,
552 int sig, u32 secid)
553{
554 return 0;
555}
556#endif
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558void cap_task_reparent_to_init (struct task_struct *p)
559{
560 p->cap_effective = CAP_INIT_EFF_SET;
561 p->cap_inheritable = CAP_INIT_INH_SET;
562 p->cap_permitted = CAP_FULL_SET;
563 p->keep_capabilities = 0;
564 return;
565}
566
567int cap_syslog (int type)
568{
569 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
570 return -EPERM;
571 return 0;
572}
573
Alan Cox34b4e4a2007-08-22 14:01:28 -0700574int cap_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 int cap_sys_admin = 0;
577
578 if (cap_capable(current, CAP_SYS_ADMIN) == 0)
579 cap_sys_admin = 1;
Alan Cox34b4e4a2007-08-22 14:01:28 -0700580 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582