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