1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/errno.h>
16#include <linux/fdtable.h>
17#include <linux/file.h>
18#include <linux/mount.h>
19#include <linux/syscalls.h>
20#include <linux/tracehook.h>
21#include <linux/personality.h>
22
23#include "include/audit.h"
24#include "include/apparmorfs.h"
25#include "include/context.h"
26#include "include/domain.h"
27#include "include/file.h"
28#include "include/ipc.h"
29#include "include/match.h"
30#include "include/path.h"
31#include "include/policy.h"
32#include "include/policy_ns.h"
33
34
35
36
37
38void aa_free_domain_entries(struct aa_domain *domain)
39{
40 int i;
41 if (domain) {
42 if (!domain->table)
43 return;
44
45 for (i = 0; i < domain->size; i++)
46 kzfree(domain->table[i]);
47 kzfree(domain->table);
48 domain->table = NULL;
49 }
50}
51
52
53
54
55
56
57
58
59
60
61
62static int may_change_ptraced_domain(struct aa_label *to_label,
63 const char **info)
64{
65 struct task_struct *tracer;
66 struct aa_label *tracerl = NULL;
67 int error = 0;
68
69 rcu_read_lock();
70 tracer = ptrace_parent(current);
71 if (tracer)
72
73 tracerl = aa_get_task_label(tracer);
74
75
76 if (!tracer || unconfined(tracerl))
77 goto out;
78
79 error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
80
81out:
82 rcu_read_unlock();
83 aa_put_label(tracerl);
84
85 if (error)
86 *info = "ptrace prevents transition";
87 return error;
88}
89
90
91
92
93
94
95
96
97
98
99
100static inline unsigned int match_component(struct aa_profile *profile,
101 struct aa_profile *tp,
102 bool stack, unsigned int state)
103{
104 const char *ns_name;
105
106 if (stack)
107 state = aa_dfa_match(profile->file.dfa, state, "&");
108 if (profile->ns == tp->ns)
109 return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
110
111
112 ns_name = aa_ns_name(profile->ns, tp->ns, true);
113 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
114 state = aa_dfa_match(profile->file.dfa, state, ns_name);
115 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
116 return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
117}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135static int label_compound_match(struct aa_profile *profile,
136 struct aa_label *label, bool stack,
137 unsigned int state, bool subns, u32 request,
138 struct aa_perms *perms)
139{
140 struct aa_profile *tp;
141 struct label_it i;
142 struct path_cond cond = { };
143
144
145 label_for_each(i, label, tp) {
146 if (!aa_ns_visible(profile->ns, tp->ns, subns))
147 continue;
148 state = match_component(profile, tp, stack, state);
149 if (!state)
150 goto fail;
151 goto next;
152 }
153
154
155 *perms = allperms;
156 return 0;
157
158next:
159 label_for_each_cont(i, label, tp) {
160 if (!aa_ns_visible(profile->ns, tp->ns, subns))
161 continue;
162 state = aa_dfa_match(profile->file.dfa, state, "//&");
163 state = match_component(profile, tp, false, state);
164 if (!state)
165 goto fail;
166 }
167 *perms = aa_compute_fperms(profile->file.dfa, state, &cond);
168 aa_apply_modes_to_perms(profile, perms);
169 if ((perms->allow & request) != request)
170 return -EACCES;
171
172 return 0;
173
174fail:
175 *perms = nullperms;
176 return -EACCES;
177}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195static int label_components_match(struct aa_profile *profile,
196 struct aa_label *label, bool stack,
197 unsigned int start, bool subns, u32 request,
198 struct aa_perms *perms)
199{
200 struct aa_profile *tp;
201 struct label_it i;
202 struct aa_perms tmp;
203 struct path_cond cond = { };
204 unsigned int state = 0;
205
206
207 label_for_each(i, label, tp) {
208 if (!aa_ns_visible(profile->ns, tp->ns, subns))
209 continue;
210 state = match_component(profile, tp, stack, start);
211 if (!state)
212 goto fail;
213 goto next;
214 }
215
216
217 return 0;
218
219next:
220 tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
221 aa_apply_modes_to_perms(profile, &tmp);
222 aa_perms_accum(perms, &tmp);
223 label_for_each_cont(i, label, tp) {
224 if (!aa_ns_visible(profile->ns, tp->ns, subns))
225 continue;
226 state = match_component(profile, tp, stack, start);
227 if (!state)
228 goto fail;
229 tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
230 aa_apply_modes_to_perms(profile, &tmp);
231 aa_perms_accum(perms, &tmp);
232 }
233
234 if ((perms->allow & request) != request)
235 return -EACCES;
236
237 return 0;
238
239fail:
240 *perms = nullperms;
241 return -EACCES;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256static int label_match(struct aa_profile *profile, struct aa_label *label,
257 bool stack, unsigned int state, bool subns, u32 request,
258 struct aa_perms *perms)
259{
260 int error;
261
262 *perms = nullperms;
263 error = label_compound_match(profile, label, stack, state, subns,
264 request, perms);
265 if (!error)
266 return error;
267
268 *perms = allperms;
269 return label_components_match(profile, label, stack, state, subns,
270 request, perms);
271}
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289static int change_profile_perms(struct aa_profile *profile,
290 struct aa_label *target, bool stack,
291 u32 request, unsigned int start,
292 struct aa_perms *perms)
293{
294 if (profile_unconfined(profile)) {
295 perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
296 perms->audit = perms->quiet = perms->kill = 0;
297 return 0;
298 }
299
300
301 return label_match(profile, target, stack, start, true, request, perms);
302}
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318static struct aa_profile *__attach_match(const char *name,
319 struct list_head *head)
320{
321 int len = 0;
322 struct aa_profile *profile, *candidate = NULL;
323
324 list_for_each_entry_rcu(profile, head, base.list) {
325 if (profile->label.flags & FLAG_NULL)
326 continue;
327 if (profile->xmatch && profile->xmatch_len > len) {
328 unsigned int state = aa_dfa_match(profile->xmatch,
329 DFA_START, name);
330 u32 perm = dfa_user_allow(profile->xmatch, state);
331
332 if (perm & MAY_EXEC) {
333 candidate = profile;
334 len = profile->xmatch_len;
335 }
336 } else if (!strcmp(profile->base.name, name))
337
338 return profile;
339 }
340
341 return candidate;
342}
343
344
345
346
347
348
349
350
351
352static struct aa_label *find_attach(struct aa_ns *ns, struct list_head *list,
353 const char *name)
354{
355 struct aa_profile *profile;
356
357 rcu_read_lock();
358 profile = aa_get_profile(__attach_match(name, list));
359 rcu_read_unlock();
360
361 return profile ? &profile->label : NULL;
362}
363
364static const char *next_name(int xtype, const char *name)
365{
366 return NULL;
367}
368
369
370
371
372
373
374
375
376
377static struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
378 const char **name)
379{
380 struct aa_label *label = NULL;
381 u32 xtype = xindex & AA_X_TYPE_MASK;
382 int index = xindex & AA_X_INDEX_MASK;
383
384 AA_BUG(!name);
385
386
387
388
389
390 for (*name = profile->file.trans.table[index]; !label && *name;
391 *name = next_name(xtype, *name)) {
392 if (xindex & AA_X_CHILD) {
393 struct aa_profile *new_profile;
394
395 new_profile = aa_find_child(profile, *name);
396 if (new_profile)
397 label = &new_profile->label;
398 continue;
399 }
400 label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
401 true, false);
402 if (IS_ERR(label))
403 label = NULL;
404 }
405
406
407
408 return label;
409}
410
411
412
413
414
415
416
417
418
419
420
421
422static struct aa_label *x_to_label(struct aa_profile *profile,
423 const char *name, u32 xindex,
424 const char **lookupname,
425 const char **info)
426{
427 struct aa_label *new = NULL;
428 struct aa_ns *ns = profile->ns;
429 u32 xtype = xindex & AA_X_TYPE_MASK;
430 const char *stack = NULL;
431
432 switch (xtype) {
433 case AA_X_NONE:
434
435 *lookupname = NULL;
436 break;
437 case AA_X_TABLE:
438
439 stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
440 if (*stack != '&') {
441
442 new = x_table_lookup(profile, xindex, lookupname);
443 stack = NULL;
444 break;
445 }
446
447 case AA_X_NAME:
448 if (xindex & AA_X_CHILD)
449
450 new = find_attach(ns, &profile->base.profiles,
451 name);
452 else
453
454 new = find_attach(ns, &ns->base.profiles,
455 name);
456 *lookupname = name;
457 break;
458 }
459
460 if (!new) {
461 if (xindex & AA_X_INHERIT) {
462
463
464
465 *info = "ix fallback";
466
467 new = aa_get_newest_label(&profile->label);
468 } else if (xindex & AA_X_UNCONFINED) {
469 new = aa_get_newest_label(ns_unconfined(profile->ns));
470 *info = "ux fallback";
471 }
472 }
473
474 if (new && stack) {
475
476 struct aa_label *base = new;
477
478 new = aa_label_parse(base, stack, GFP_ATOMIC, true, false);
479 if (IS_ERR(new))
480 new = NULL;
481 aa_put_label(base);
482 }
483
484
485 return new;
486}
487
488static struct aa_label *profile_transition(struct aa_profile *profile,
489 const struct linux_binprm *bprm,
490 char *buffer, struct path_cond *cond,
491 bool *secure_exec)
492{
493 struct aa_label *new = NULL;
494 const char *info = NULL, *name = NULL, *target = NULL;
495 unsigned int state = profile->file.start;
496 struct aa_perms perms = {};
497 bool nonewprivs = false;
498 int error = 0;
499
500 AA_BUG(!profile);
501 AA_BUG(!bprm);
502 AA_BUG(!buffer);
503
504 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
505 &name, &info, profile->disconnected);
506 if (error) {
507 if (profile_unconfined(profile) ||
508 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
509 AA_DEBUG("name lookup ix on error");
510 error = 0;
511 new = aa_get_newest_label(&profile->label);
512 }
513 name = bprm->filename;
514 goto audit;
515 }
516
517 if (profile_unconfined(profile)) {
518 new = find_attach(profile->ns, &profile->ns->base.profiles,
519 name);
520 if (new) {
521 AA_DEBUG("unconfined attached to new label");
522 return new;
523 }
524 AA_DEBUG("unconfined exec no attachment");
525 return aa_get_newest_label(&profile->label);
526 }
527
528
529 state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
530 if (perms.allow & MAY_EXEC) {
531
532 new = x_to_label(profile, name, perms.xindex, &target, &info);
533 if (new && new->proxy == profile->label.proxy && info) {
534
535 goto audit;
536 } else if (!new) {
537 error = -EACCES;
538 info = "profile transition not found";
539
540 perms.allow &= ~MAY_EXEC;
541 }
542 } else if (COMPLAIN_MODE(profile)) {
543
544 struct aa_profile *new_profile = aa_new_null_profile(profile,
545 false, name,
546 GFP_ATOMIC);
547 if (!new_profile) {
548 error = -ENOMEM;
549 info = "could not create null profile";
550 } else {
551 error = -EACCES;
552 new = &new_profile->label;
553 }
554 perms.xindex |= AA_X_UNSAFE;
555 } else
556
557 error = -EACCES;
558
559 if (!new)
560 goto audit;
561
562
563
564
565
566
567
568
569 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
570 !profile_unconfined(profile) &&
571 !aa_label_is_subset(new, &profile->label)) {
572 error = -EPERM;
573 info = "no new privs";
574 nonewprivs = true;
575 perms.allow &= ~MAY_EXEC;
576 goto audit;
577 }
578
579 if (!(perms.xindex & AA_X_UNSAFE)) {
580 if (DEBUG_ON) {
581 dbg_printk("apparmor: scrubbing environment variables"
582 " for %s profile=", name);
583 aa_label_printk(new, GFP_ATOMIC);
584 dbg_printk("\n");
585 }
586 *secure_exec = true;
587 }
588
589audit:
590 aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
591 cond->uid, info, error);
592 if (!new || nonewprivs) {
593 aa_put_label(new);
594 return ERR_PTR(error);
595 }
596
597 return new;
598}
599
600static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
601 bool stack, const struct linux_binprm *bprm,
602 char *buffer, struct path_cond *cond,
603 bool *secure_exec)
604{
605 unsigned int state = profile->file.start;
606 struct aa_perms perms = {};
607 const char *xname = NULL, *info = "change_profile onexec";
608 int error = -EACCES;
609
610 AA_BUG(!profile);
611 AA_BUG(!onexec);
612 AA_BUG(!bprm);
613 AA_BUG(!buffer);
614
615 if (profile_unconfined(profile)) {
616
617
618
619
620
621
622 return 0;
623 }
624
625 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
626 &xname, &info, profile->disconnected);
627 if (error) {
628 if (profile_unconfined(profile) ||
629 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
630 AA_DEBUG("name lookup ix on error");
631 error = 0;
632 }
633 xname = bprm->filename;
634 goto audit;
635 }
636
637
638 state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
639 if (!(perms.allow & AA_MAY_ONEXEC)) {
640 info = "no change_onexec valid for executable";
641 goto audit;
642 }
643
644
645
646
647 state = aa_dfa_null_transition(profile->file.dfa, state);
648 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
649 state, &perms);
650 if (error) {
651 perms.allow &= ~AA_MAY_ONEXEC;
652 goto audit;
653 }
654
655
656
657
658
659
660
661 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
662 !profile_unconfined(profile) &&
663 !aa_label_is_subset(onexec, &profile->label)) {
664 error = -EPERM;
665 info = "no new privs";
666 perms.allow &= ~AA_MAY_ONEXEC;
667 goto audit;
668 }
669
670 if (!(perms.xindex & AA_X_UNSAFE)) {
671 if (DEBUG_ON) {
672 dbg_printk("apparmor: scrubbing environment "
673 "variables for %s label=", xname);
674 aa_label_printk(onexec, GFP_ATOMIC);
675 dbg_printk("\n");
676 }
677 *secure_exec = true;
678 }
679
680audit:
681 return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
682 NULL, onexec, cond->uid, info, error);
683}
684
685
686
687static struct aa_label *handle_onexec(struct aa_label *label,
688 struct aa_label *onexec, bool stack,
689 const struct linux_binprm *bprm,
690 char *buffer, struct path_cond *cond,
691 bool *unsafe)
692{
693 struct aa_profile *profile;
694 struct aa_label *new;
695 int error;
696
697 AA_BUG(!label);
698 AA_BUG(!onexec);
699 AA_BUG(!bprm);
700 AA_BUG(!buffer);
701
702 if (!stack) {
703 error = fn_for_each_in_ns(label, profile,
704 profile_onexec(profile, onexec, stack,
705 bprm, buffer, cond, unsafe));
706 if (error)
707 return ERR_PTR(error);
708 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
709 aa_get_newest_label(onexec),
710 profile_transition(profile, bprm, buffer,
711 cond, unsafe));
712
713 } else {
714
715 error = fn_for_each_in_ns(label, profile,
716 profile_onexec(profile, onexec, stack, bprm,
717 buffer, cond, unsafe));
718 if (error)
719 return ERR_PTR(error);
720 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
721 aa_label_merge(&profile->label, onexec,
722 GFP_ATOMIC),
723 profile_transition(profile, bprm, buffer,
724 cond, unsafe));
725 }
726
727 if (new)
728 return new;
729
730
731 error = fn_for_each_in_ns(label, profile,
732 aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
733 AA_MAY_ONEXEC, bprm->filename, NULL,
734 onexec, GLOBAL_ROOT_UID,
735 "failed to build target label", -ENOMEM));
736 return ERR_PTR(error);
737}
738
739
740
741
742
743
744
745
746
747int apparmor_bprm_set_creds(struct linux_binprm *bprm)
748{
749 struct aa_task_ctx *ctx;
750 struct aa_label *label, *new = NULL;
751 struct aa_profile *profile;
752 char *buffer = NULL;
753 const char *info = NULL;
754 int error = 0;
755 bool unsafe = false;
756 struct path_cond cond = {
757 file_inode(bprm->file)->i_uid,
758 file_inode(bprm->file)->i_mode
759 };
760
761 if (bprm->cred_prepared)
762 return 0;
763
764 ctx = cred_ctx(bprm->cred);
765 AA_BUG(!ctx);
766
767 label = aa_get_newest_label(ctx->label);
768
769
770 get_buffers(buffer);
771
772 if (ctx->onexec)
773 new = handle_onexec(label, ctx->onexec, ctx->token,
774 bprm, buffer, &cond, &unsafe);
775 else
776 new = fn_label_build(label, profile, GFP_ATOMIC,
777 profile_transition(profile, bprm, buffer,
778 &cond, &unsafe));
779
780 AA_BUG(!new);
781 if (IS_ERR(new)) {
782 error = PTR_ERR(new);
783 goto done;
784 } else if (!new) {
785 error = -ENOMEM;
786 goto done;
787 }
788
789
790
791 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
792
793 ;
794 }
795
796 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
797
798 error = may_change_ptraced_domain(new, &info);
799 if (error)
800 goto audit;
801 }
802
803 if (unsafe) {
804 if (DEBUG_ON) {
805 dbg_printk("scrubbing environment variables for %s "
806 "label=", bprm->filename);
807 aa_label_printk(new, GFP_ATOMIC);
808 dbg_printk("\n");
809 }
810 bprm->unsafe |= AA_SECURE_X_NEEDED;
811 }
812
813 if (label->proxy != new->proxy) {
814
815 if (DEBUG_ON) {
816 dbg_printk("apparmor: clearing unsafe personality "
817 "bits. %s label=", bprm->filename);
818 aa_label_printk(new, GFP_ATOMIC);
819 dbg_printk("\n");
820 }
821 bprm->per_clear |= PER_CLEAR_ON_SETID;
822 }
823 aa_put_label(ctx->label);
824
825 ctx->label = new;
826
827done:
828
829 aa_clear_task_ctx_trans(ctx);
830
831 aa_put_label(label);
832 put_buffers(buffer);
833
834 return error;
835
836audit:
837 error = fn_for_each(label, profile,
838 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
839 bprm->filename, NULL, new,
840 file_inode(bprm->file)->i_uid, info,
841 error));
842 aa_put_label(new);
843 goto done;
844}
845
846
847
848
849
850
851
852int apparmor_bprm_secureexec(struct linux_binprm *bprm)
853{
854
855
856
857 if (bprm->unsafe & AA_SECURE_X_NEEDED)
858 return 1;
859
860 return 0;
861}
862
863
864
865
866
867
868
869
870
871
872static struct aa_label *build_change_hat(struct aa_profile *profile,
873 const char *name, bool sibling)
874{
875 struct aa_profile *root, *hat = NULL;
876 const char *info = NULL;
877 int error = 0;
878
879 if (sibling && PROFILE_IS_HAT(profile)) {
880 root = aa_get_profile_rcu(&profile->parent);
881 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
882 root = aa_get_profile(profile);
883 } else {
884 info = "conflicting target types";
885 error = -EPERM;
886 goto audit;
887 }
888
889 hat = aa_find_child(root, name);
890 if (!hat) {
891 error = -ENOENT;
892 if (COMPLAIN_MODE(profile)) {
893 hat = aa_new_null_profile(profile, true, name,
894 GFP_KERNEL);
895 if (!hat) {
896 info = "failed null profile create";
897 error = -ENOMEM;
898 }
899 }
900 }
901 aa_put_profile(root);
902
903audit:
904 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
905 name, hat ? hat->base.hname : NULL,
906 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, NULL,
907 error);
908 if (!hat || (error && error != -ENOENT))
909 return ERR_PTR(error);
910
911
912
913 return &hat->label;
914}
915
916
917
918
919
920static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
921 int count, int flags)
922{
923 struct aa_profile *profile, *root, *hat = NULL;
924 struct aa_label *new;
925 struct label_it it;
926 bool sibling = false;
927 const char *name, *info = NULL;
928 int i, error;
929
930 AA_BUG(!label);
931 AA_BUG(!hats);
932 AA_BUG(count < 1);
933
934 if (PROFILE_IS_HAT(labels_profile(label)))
935 sibling = true;
936
937
938 for (i = 0; i < count && !hat; i++) {
939 name = hats[i];
940 label_for_each_in_ns(it, labels_ns(label), label, profile) {
941 if (sibling && PROFILE_IS_HAT(profile)) {
942 root = aa_get_profile_rcu(&profile->parent);
943 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
944 root = aa_get_profile(profile);
945 } else {
946 info = "conflicting targets types";
947 error = -EPERM;
948 goto fail;
949 }
950 hat = aa_find_child(root, name);
951 aa_put_profile(root);
952 if (!hat) {
953 if (!COMPLAIN_MODE(profile))
954 goto outer_continue;
955
956 } else if (!PROFILE_IS_HAT(hat)) {
957 info = "target not hat";
958 error = -EPERM;
959 aa_put_profile(hat);
960 goto fail;
961 }
962 aa_put_profile(hat);
963 }
964
965 goto build;
966outer_continue:
967 ;
968 }
969
970
971
972
973
974
975 name = NULL;
976 label_for_each_in_ns(it, labels_ns(label), label, profile) {
977 if (!list_empty(&profile->base.profiles)) {
978 info = "hat not found";
979 error = -ENOENT;
980 goto fail;
981 }
982 }
983 info = "no hats defined";
984 error = -ECHILD;
985
986fail:
987 label_for_each_in_ns(it, labels_ns(label), label, profile) {
988
989
990
991
992
993
994
995 if (count > 1 || COMPLAIN_MODE(profile)) {
996 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
997 AA_MAY_CHANGEHAT, name, NULL, NULL,
998 GLOBAL_ROOT_UID, info, error);
999 }
1000 }
1001 return ERR_PTR(error);
1002
1003build:
1004 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1005 build_change_hat(profile, name, sibling),
1006 aa_get_label(&profile->label));
1007 if (!new) {
1008 info = "label build failed";
1009 error = -ENOMEM;
1010 goto fail;
1011 }
1012
1013 return new;
1014}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033int aa_change_hat(const char *hats[], int count, u64 token, int flags)
1034{
1035 const struct cred *cred;
1036 struct aa_task_ctx *ctx;
1037 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1038 struct aa_profile *profile;
1039 struct aa_perms perms = {};
1040 const char *info = NULL;
1041 int error = 0;
1042
1043
1044
1045
1046
1047
1048 if (task_no_new_privs(current)) {
1049
1050 AA_DEBUG("no_new_privs - change_hat denied");
1051 return -EPERM;
1052 }
1053
1054
1055 cred = get_current_cred();
1056 ctx = cred_ctx(cred);
1057 label = aa_get_newest_cred_label(cred);
1058 previous = aa_get_newest_label(ctx->previous);
1059
1060 if (unconfined(label)) {
1061 info = "unconfined can not change_hat";
1062 error = -EPERM;
1063 goto fail;
1064 }
1065
1066 if (count) {
1067 new = change_hat(label, hats, count, flags);
1068 AA_BUG(!new);
1069 if (IS_ERR(new)) {
1070 error = PTR_ERR(new);
1071 new = NULL;
1072
1073 goto out;
1074 }
1075
1076 error = may_change_ptraced_domain(new, &info);
1077 if (error)
1078 goto fail;
1079
1080 if (flags & AA_CHANGE_TEST)
1081 goto out;
1082
1083 target = new;
1084 error = aa_set_current_hat(new, token);
1085 if (error == -EACCES)
1086
1087 goto kill;
1088 } else if (previous && !(flags & AA_CHANGE_TEST)) {
1089
1090
1091
1092 target = previous;
1093 error = aa_restore_previous_label(token);
1094 if (error) {
1095 if (error == -EACCES)
1096 goto kill;
1097 goto fail;
1098 }
1099 }
1100
1101out:
1102 aa_put_label(new);
1103 aa_put_label(previous);
1104 aa_put_label(label);
1105 put_cred(cred);
1106
1107 return error;
1108
1109kill:
1110 info = "failed token match";
1111 perms.kill = AA_MAY_CHANGEHAT;
1112
1113fail:
1114 fn_for_each_in_ns(label, profile,
1115 aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1116 AA_MAY_CHANGEHAT, NULL, NULL, target,
1117 GLOBAL_ROOT_UID, info, error));
1118
1119 goto out;
1120}
1121
1122
1123static int change_profile_perms_wrapper(const char *op, const char *name,
1124 struct aa_profile *profile,
1125 struct aa_label *target, bool stack,
1126 u32 request, struct aa_perms *perms)
1127{
1128 const char *info = NULL;
1129 int error = 0;
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 if (task_no_new_privs(current) && !stack &&
1140 !profile_unconfined(profile) &&
1141 !aa_label_is_subset(target, &profile->label)) {
1142 info = "no new privs";
1143 error = -EPERM;
1144 }
1145
1146 if (!error)
1147 error = change_profile_perms(profile, target, stack, request,
1148 profile->file.start, perms);
1149 if (error)
1150 error = aa_audit_file(profile, perms, op, request, name,
1151 NULL, target, GLOBAL_ROOT_UID, info,
1152 error);
1153
1154 return error;
1155}
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171int aa_change_profile(const char *fqname, int flags)
1172{
1173 struct aa_label *label, *new = NULL, *target = NULL;
1174 struct aa_profile *profile;
1175 struct aa_perms perms = {};
1176 const char *info = NULL;
1177 const char *auditname = fqname;
1178 bool stack = flags & AA_CHANGE_STACK;
1179 int error = 0;
1180 char *op;
1181 u32 request;
1182
1183 if (!fqname || !*fqname) {
1184 AA_DEBUG("no profile name");
1185 return -EINVAL;
1186 }
1187
1188 if (flags & AA_CHANGE_ONEXEC) {
1189 request = AA_MAY_ONEXEC;
1190 if (stack)
1191 op = OP_STACK_ONEXEC;
1192 else
1193 op = OP_CHANGE_ONEXEC;
1194 } else {
1195 request = AA_MAY_CHANGE_PROFILE;
1196 if (stack)
1197 op = OP_STACK;
1198 else
1199 op = OP_CHANGE_PROFILE;
1200 }
1201
1202 label = aa_get_current_label();
1203
1204 if (*fqname == '&') {
1205 stack = true;
1206
1207 fqname++;
1208 }
1209 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1210 if (IS_ERR(target)) {
1211 struct aa_profile *tprofile;
1212
1213 info = "label not found";
1214 error = PTR_ERR(target);
1215 target = NULL;
1216
1217
1218
1219
1220 if ((flags & AA_CHANGE_TEST) ||
1221 !COMPLAIN_MODE(labels_profile(label)))
1222 goto audit;
1223
1224 tprofile = aa_new_null_profile(labels_profile(label), false,
1225 fqname, GFP_KERNEL);
1226 if (!tprofile) {
1227 info = "failed null profile create";
1228 error = -ENOMEM;
1229 goto audit;
1230 }
1231 target = &tprofile->label;
1232 goto check;
1233 }
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243 error = fn_for_each_in_ns(label, profile,
1244 change_profile_perms_wrapper(op, auditname,
1245 profile, target, stack,
1246 request, &perms));
1247 if (error)
1248
1249 goto out;
1250
1251
1252
1253check:
1254
1255 error = may_change_ptraced_domain(target, &info);
1256 if (error && !fn_for_each_in_ns(label, profile,
1257 COMPLAIN_MODE(profile)))
1258 goto audit;
1259
1260
1261
1262
1263
1264
1265
1266
1267 if (flags & AA_CHANGE_TEST)
1268 goto out;
1269
1270 if (!(flags & AA_CHANGE_ONEXEC)) {
1271
1272 if (stack)
1273 new = aa_label_merge(label, target, GFP_KERNEL);
1274 else
1275 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1276 aa_get_label(target),
1277 aa_get_label(&profile->label));
1278 if (IS_ERR_OR_NULL(new)) {
1279 info = "failed to build target label";
1280 error = PTR_ERR(new);
1281 new = NULL;
1282 perms.allow = 0;
1283 goto audit;
1284 }
1285 error = aa_replace_current_label(new);
1286 } else
1287
1288 error = aa_set_current_onexec(target, stack);
1289
1290audit:
1291 error = fn_for_each_in_ns(label, profile,
1292 aa_audit_file(profile, &perms, op, request, auditname,
1293 NULL, new ? new : target,
1294 GLOBAL_ROOT_UID, info, error));
1295
1296out:
1297 aa_put_label(new);
1298 aa_put_label(target);
1299 aa_put_label(label);
1300
1301 return error;
1302}
1303