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