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