1
2
3
4
5
6
7
8
9
10#include <linux/capability.h>
11#include <linux/audit.h>
12#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>
21#include <linux/skbuff.h>
22#include <linux/netlink.h>
23#include <linux/ptrace.h>
24#include <linux/xattr.h>
25#include <linux/hugetlb.h>
26#include <linux/mount.h>
27#include <linux/sched.h>
28#include <linux/prctl.h>
29#include <linux/securebits.h>
30
31int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
32{
33 NETLINK_CB(skb).eff_cap = current_cap();
34 return 0;
35}
36
37int cap_netlink_recv(struct sk_buff *skb, int cap)
38{
39 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
40 return -EPERM;
41 return 0;
42}
43EXPORT_SYMBOL(cap_netlink_recv);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
61 int audit)
62{
63 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
64}
65
66
67
68
69
70
71
72
73
74int cap_settime(struct timespec *ts, struct timezone *tz)
75{
76 if (!capable(CAP_SYS_TIME))
77 return -EPERM;
78 return 0;
79}
80
81
82
83
84
85
86
87
88
89
90int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
91{
92 int ret = 0;
93
94 rcu_read_lock();
95 if (!cap_issubset(__task_cred(child)->cap_permitted,
96 current_cred()->cap_permitted) &&
97 !capable(CAP_SYS_PTRACE))
98 ret = -EPERM;
99 rcu_read_unlock();
100 return ret;
101}
102
103
104
105
106
107
108
109
110int cap_ptrace_traceme(struct task_struct *parent)
111{
112 int ret = 0;
113
114 rcu_read_lock();
115 if (!cap_issubset(current_cred()->cap_permitted,
116 __task_cred(parent)->cap_permitted) &&
117 !has_capability(parent, CAP_SYS_PTRACE))
118 ret = -EPERM;
119 rcu_read_unlock();
120 return ret;
121}
122
123
124
125
126
127
128
129
130
131
132
133int cap_capget(struct task_struct *target, kernel_cap_t *effective,
134 kernel_cap_t *inheritable, kernel_cap_t *permitted)
135{
136 const struct cred *cred;
137
138
139 rcu_read_lock();
140 cred = __task_cred(target);
141 *effective = cred->cap_effective;
142 *inheritable = cred->cap_inheritable;
143 *permitted = cred->cap_permitted;
144 rcu_read_unlock();
145 return 0;
146}
147
148
149
150
151
152static inline int cap_inh_is_capped(void)
153{
154#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
155
156
157
158
159 if (cap_capable(current, current_cred(), CAP_SETPCAP,
160 SECURITY_CAP_AUDIT) == 0)
161 return 0;
162#endif
163 return 1;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178int cap_capset(struct cred *new,
179 const struct cred *old,
180 const kernel_cap_t *effective,
181 const kernel_cap_t *inheritable,
182 const kernel_cap_t *permitted)
183{
184 if (cap_inh_is_capped() &&
185 !cap_issubset(*inheritable,
186 cap_combine(old->cap_inheritable,
187 old->cap_permitted)))
188
189 return -EPERM;
190
191 if (!cap_issubset(*inheritable,
192 cap_combine(old->cap_inheritable,
193 old->cap_bset)))
194
195 return -EPERM;
196
197
198 if (!cap_issubset(*permitted, old->cap_permitted))
199 return -EPERM;
200
201
202 if (!cap_issubset(*effective, *permitted))
203 return -EPERM;
204
205 new->cap_effective = *effective;
206 new->cap_inheritable = *inheritable;
207 new->cap_permitted = *permitted;
208 return 0;
209}
210
211
212
213
214static inline void bprm_clear_caps(struct linux_binprm *bprm)
215{
216 cap_clear(bprm->cred->cap_permitted);
217 bprm->cap_effective = false;
218}
219
220#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
221
222
223
224
225
226
227
228
229
230
231
232
233int cap_inode_need_killpriv(struct dentry *dentry)
234{
235 struct inode *inode = dentry->d_inode;
236 int error;
237
238 if (!inode->i_op->getxattr)
239 return 0;
240
241 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
242 if (error <= 0)
243 return 0;
244 return 1;
245}
246
247
248
249
250
251
252
253
254
255int cap_inode_killpriv(struct dentry *dentry)
256{
257 struct inode *inode = dentry->d_inode;
258
259 if (!inode->i_op->removexattr)
260 return 0;
261
262 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
263}
264
265
266
267
268
269static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
270 struct linux_binprm *bprm,
271 bool *effective)
272{
273 struct cred *new = bprm->cred;
274 unsigned i;
275 int ret = 0;
276
277 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
278 *effective = true;
279
280 CAP_FOR_EACH_U32(i) {
281 __u32 permitted = caps->permitted.cap[i];
282 __u32 inheritable = caps->inheritable.cap[i];
283
284
285
286
287 new->cap_permitted.cap[i] =
288 (new->cap_bset.cap[i] & permitted) |
289 (new->cap_inheritable.cap[i] & inheritable);
290
291 if (permitted & ~new->cap_permitted.cap[i])
292
293 ret = -EPERM;
294 }
295
296
297
298
299
300
301 return *effective ? ret : 0;
302}
303
304
305
306
307int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
308{
309 struct inode *inode = dentry->d_inode;
310 __u32 magic_etc;
311 unsigned tocopy, i;
312 int size;
313 struct vfs_cap_data caps;
314
315 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
316
317 if (!inode || !inode->i_op->getxattr)
318 return -ENODATA;
319
320 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
321 XATTR_CAPS_SZ);
322 if (size == -ENODATA || size == -EOPNOTSUPP)
323
324 return -ENODATA;
325 if (size < 0)
326 return size;
327
328 if (size < sizeof(magic_etc))
329 return -EINVAL;
330
331 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
332
333 switch (magic_etc & VFS_CAP_REVISION_MASK) {
334 case VFS_CAP_REVISION_1:
335 if (size != XATTR_CAPS_SZ_1)
336 return -EINVAL;
337 tocopy = VFS_CAP_U32_1;
338 break;
339 case VFS_CAP_REVISION_2:
340 if (size != XATTR_CAPS_SZ_2)
341 return -EINVAL;
342 tocopy = VFS_CAP_U32_2;
343 break;
344 default:
345 return -EINVAL;
346 }
347
348 CAP_FOR_EACH_U32(i) {
349 if (i >= tocopy)
350 break;
351 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
352 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
353 }
354
355 return 0;
356}
357
358
359
360
361
362
363static int get_file_caps(struct linux_binprm *bprm, bool *effective)
364{
365 struct dentry *dentry;
366 int rc = 0;
367 struct cpu_vfs_cap_data vcaps;
368
369 bprm_clear_caps(bprm);
370
371 if (!file_caps_enabled)
372 return 0;
373
374 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
375 return 0;
376
377 dentry = dget(bprm->file->f_dentry);
378
379 rc = get_vfs_caps_from_disk(dentry, &vcaps);
380 if (rc < 0) {
381 if (rc == -EINVAL)
382 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
383 __func__, rc, bprm->filename);
384 else if (rc == -ENODATA)
385 rc = 0;
386 goto out;
387 }
388
389 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
390 if (rc == -EINVAL)
391 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
392 __func__, rc, bprm->filename);
393
394out:
395 dput(dentry);
396 if (rc)
397 bprm_clear_caps(bprm);
398
399 return rc;
400}
401
402#else
403int cap_inode_need_killpriv(struct dentry *dentry)
404{
405 return 0;
406}
407
408int cap_inode_killpriv(struct dentry *dentry)
409{
410 return 0;
411}
412
413int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
414{
415 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
416 return -ENODATA;
417}
418
419static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
420{
421 bprm_clear_caps(bprm);
422 return 0;
423}
424#endif
425
426
427
428
429
430
431
432
433
434
435
436static inline int cap_limit_ptraced_target(void)
437{
438#ifndef CONFIG_SECURITY_FILE_CAPABILITIES
439 if (capable(CAP_SETPCAP))
440 return 0;
441#endif
442 return 1;
443}
444
445
446
447
448
449
450
451
452
453int cap_bprm_set_creds(struct linux_binprm *bprm)
454{
455 const struct cred *old = current_cred();
456 struct cred *new = bprm->cred;
457 bool effective;
458 int ret;
459
460 effective = false;
461 ret = get_file_caps(bprm, &effective);
462 if (ret < 0)
463 return ret;
464
465 if (!issecure(SECURE_NOROOT)) {
466
467
468
469
470
471
472
473 if (new->euid == 0 || new->uid == 0) {
474
475 new->cap_permitted = cap_combine(old->cap_bset,
476 old->cap_inheritable);
477 }
478 if (new->euid == 0)
479 effective = true;
480 }
481
482
483
484
485 if ((new->euid != old->uid ||
486 new->egid != old->gid ||
487 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
488 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
489
490 if (!capable(CAP_SETUID)) {
491 new->euid = new->uid;
492 new->egid = new->gid;
493 }
494 if (cap_limit_ptraced_target())
495 new->cap_permitted = cap_intersect(new->cap_permitted,
496 old->cap_permitted);
497 }
498
499 new->suid = new->fsuid = new->euid;
500 new->sgid = new->fsgid = new->egid;
501
502
503
504
505 if (!is_global_init(current)) {
506 if (effective)
507 new->cap_effective = new->cap_permitted;
508 else
509 cap_clear(new->cap_effective);
510 }
511 bprm->cap_effective = effective;
512
513
514
515
516
517
518
519
520
521
522
523
524
525 if (!cap_isclear(new->cap_effective)) {
526 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
527 new->euid != 0 || new->uid != 0 ||
528 issecure(SECURE_NOROOT)) {
529 ret = audit_log_bprm_fcaps(bprm, new, old);
530 if (ret < 0)
531 return ret;
532 }
533 }
534
535 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
536 return 0;
537}
538
539
540
541
542
543
544
545
546
547
548
549int cap_bprm_secureexec(struct linux_binprm *bprm)
550{
551 const struct cred *cred = current_cred();
552
553 if (cred->uid != 0) {
554 if (bprm->cap_effective)
555 return 1;
556 if (!cap_isclear(cred->cap_permitted))
557 return 1;
558 }
559
560 return (cred->euid != cred->uid ||
561 cred->egid != cred->gid);
562}
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578int cap_inode_setxattr(struct dentry *dentry, const char *name,
579 const void *value, size_t size, int flags)
580{
581 if (!strcmp(name, XATTR_NAME_CAPS)) {
582 if (!capable(CAP_SETFCAP))
583 return -EPERM;
584 return 0;
585 }
586
587 if (!strncmp(name, XATTR_SECURITY_PREFIX,
588 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
589 !capable(CAP_SYS_ADMIN))
590 return -EPERM;
591 return 0;
592}
593
594
595
596
597
598
599
600
601
602
603
604
605int cap_inode_removexattr(struct dentry *dentry, const char *name)
606{
607 if (!strcmp(name, XATTR_NAME_CAPS)) {
608 if (!capable(CAP_SETFCAP))
609 return -EPERM;
610 return 0;
611 }
612
613 if (!strncmp(name, XATTR_SECURITY_PREFIX,
614 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
615 !capable(CAP_SYS_ADMIN))
616 return -EPERM;
617 return 0;
618}
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
650{
651 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
652 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
653 !issecure(SECURE_KEEP_CAPS)) {
654 cap_clear(new->cap_permitted);
655 cap_clear(new->cap_effective);
656 }
657 if (old->euid == 0 && new->euid != 0)
658 cap_clear(new->cap_effective);
659 if (old->euid != 0 && new->euid == 0)
660 new->cap_effective = new->cap_permitted;
661}
662
663
664
665
666
667
668
669
670
671
672int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
673{
674 switch (flags) {
675 case LSM_SETID_RE:
676 case LSM_SETID_ID:
677 case LSM_SETID_RES:
678
679
680 if (!issecure(SECURE_NO_SETUID_FIXUP))
681 cap_emulate_setxuid(new, old);
682 break;
683
684 case LSM_SETID_FS:
685
686
687
688
689
690
691 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
692 if (old->fsuid == 0 && new->fsuid != 0)
693 new->cap_effective =
694 cap_drop_fs_set(new->cap_effective);
695
696 if (old->fsuid != 0 && new->fsuid == 0)
697 new->cap_effective =
698 cap_raise_fs_set(new->cap_effective,
699 new->cap_permitted);
700 }
701 break;
702
703 default:
704 return -EINVAL;
705 }
706
707 return 0;
708}
709
710#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
711
712
713
714
715
716
717
718
719
720
721static int cap_safe_nice(struct task_struct *p)
722{
723 int is_subset;
724
725 rcu_read_lock();
726 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
727 current_cred()->cap_permitted);
728 rcu_read_unlock();
729
730 if (!is_subset && !capable(CAP_SYS_NICE))
731 return -EPERM;
732 return 0;
733}
734
735
736
737
738
739
740
741
742
743
744int cap_task_setscheduler(struct task_struct *p, int policy,
745 struct sched_param *lp)
746{
747 return cap_safe_nice(p);
748}
749
750
751
752
753
754
755
756
757
758int cap_task_setioprio(struct task_struct *p, int ioprio)
759{
760 return cap_safe_nice(p);
761}
762
763
764
765
766
767
768
769
770
771int cap_task_setnice(struct task_struct *p, int nice)
772{
773 return cap_safe_nice(p);
774}
775
776
777
778
779
780static long cap_prctl_drop(struct cred *new, unsigned long cap)
781{
782 if (!capable(CAP_SETPCAP))
783 return -EPERM;
784 if (!cap_valid(cap))
785 return -EINVAL;
786
787 cap_lower(new->cap_bset, cap);
788 return 0;
789}
790
791#else
792int cap_task_setscheduler (struct task_struct *p, int policy,
793 struct sched_param *lp)
794{
795 return 0;
796}
797int cap_task_setioprio (struct task_struct *p, int ioprio)
798{
799 return 0;
800}
801int cap_task_setnice (struct task_struct *p, int nice)
802{
803 return 0;
804}
805#endif
806
807
808
809
810
811
812
813
814
815
816
817
818
819int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
820 unsigned long arg4, unsigned long arg5)
821{
822 struct cred *new;
823 long error = 0;
824
825 new = prepare_creds();
826 if (!new)
827 return -ENOMEM;
828
829 switch (option) {
830 case PR_CAPBSET_READ:
831 error = -EINVAL;
832 if (!cap_valid(arg2))
833 goto error;
834 error = !!cap_raised(new->cap_bset, arg2);
835 goto no_change;
836
837#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
838 case PR_CAPBSET_DROP:
839 error = cap_prctl_drop(new, arg2);
840 if (error < 0)
841 goto error;
842 goto changed;
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863 case PR_SET_SECUREBITS:
864 error = -EPERM;
865 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
866 & (new->securebits ^ arg2))
867 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2))
868 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS))
869 || (cap_capable(current, current_cred(), CAP_SETPCAP,
870 SECURITY_CAP_AUDIT) != 0)
871
872
873
874
875
876
877
878 )
879
880 goto error;
881 new->securebits = arg2;
882 goto changed;
883
884 case PR_GET_SECUREBITS:
885 error = new->securebits;
886 goto no_change;
887
888#endif
889
890 case PR_GET_KEEPCAPS:
891 if (issecure(SECURE_KEEP_CAPS))
892 error = 1;
893 goto no_change;
894
895 case PR_SET_KEEPCAPS:
896 error = -EINVAL;
897 if (arg2 > 1)
898 goto error;
899 error = -EPERM;
900 if (issecure(SECURE_KEEP_CAPS_LOCKED))
901 goto error;
902 if (arg2)
903 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
904 else
905 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
906 goto changed;
907
908 default:
909
910 error = -ENOSYS;
911 goto error;
912 }
913
914
915changed:
916 return commit_creds(new);
917
918no_change:
919error:
920 abort_creds(new);
921 return error;
922}
923
924
925
926
927
928
929
930
931int cap_syslog(int type)
932{
933 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
934 return -EPERM;
935 return 0;
936}
937
938
939
940
941
942
943
944
945
946int cap_vm_enough_memory(struct mm_struct *mm, long pages)
947{
948 int cap_sys_admin = 0;
949
950 if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
951 SECURITY_CAP_NOAUDIT) == 0)
952 cap_sys_admin = 1;
953 return __vm_enough_memory(mm, pages, cap_sys_admin);
954}
955