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
33
34
35
36
37void aa_free_domain_entries(struct aa_domain *domain)
38{
39 int i;
40 if (domain) {
41 if (!domain->table)
42 return;
43
44 for (i = 0; i < domain->size; i++)
45 kzfree(domain->table[i]);
46 kzfree(domain->table);
47 domain->table = NULL;
48 }
49}
50
51
52
53
54
55
56
57
58
59
60static int may_change_ptraced_domain(struct aa_profile *to_profile)
61{
62 struct task_struct *tracer;
63 struct aa_profile *tracerp = NULL;
64 int error = 0;
65
66 rcu_read_lock();
67 tracer = ptrace_parent(current);
68 if (tracer)
69
70 tracerp = aa_get_task_profile(tracer);
71
72
73 if (!tracer || unconfined(tracerp))
74 goto out;
75
76 error = aa_may_ptrace(tracerp, to_profile, PTRACE_MODE_ATTACH);
77
78out:
79 rcu_read_unlock();
80 aa_put_profile(tracerp);
81
82 return error;
83}
84
85
86
87
88
89
90
91
92
93
94
95static struct file_perms change_profile_perms(struct aa_profile *profile,
96 struct aa_namespace *ns,
97 const char *name, u32 request,
98 unsigned int start)
99{
100 struct file_perms perms;
101 struct path_cond cond = { };
102 unsigned int state;
103
104 if (unconfined(profile)) {
105 perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
106 perms.audit = perms.quiet = perms.kill = 0;
107 return perms;
108 } else if (!profile->file.dfa) {
109 return nullperms;
110 } else if ((ns == profile->ns)) {
111
112 aa_str_perms(profile->file.dfa, start, name, &cond, &perms);
113 if (COMBINED_PERM_MASK(perms) & request)
114 return perms;
115 }
116
117
118 state = aa_dfa_match(profile->file.dfa, start, ns->base.name);
119 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
120 aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
121
122 return perms;
123}
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139static struct aa_profile *__attach_match(const char *name,
140 struct list_head *head)
141{
142 int len = 0;
143 struct aa_profile *profile, *candidate = NULL;
144
145 list_for_each_entry_rcu(profile, head, base.list) {
146 if (profile->flags & PFLAG_NULL)
147 continue;
148 if (profile->xmatch && profile->xmatch_len > len) {
149 unsigned int state = aa_dfa_match(profile->xmatch,
150 DFA_START, name);
151 u32 perm = dfa_user_allow(profile->xmatch, state);
152
153 if (perm & MAY_EXEC) {
154 candidate = profile;
155 len = profile->xmatch_len;
156 }
157 } else if (!strcmp(profile->base.name, name))
158
159 return profile;
160 }
161
162 return candidate;
163}
164
165
166
167
168
169
170
171
172
173static struct aa_profile *find_attach(struct aa_namespace *ns,
174 struct list_head *list, const char *name)
175{
176 struct aa_profile *profile;
177
178 rcu_read_lock();
179 profile = aa_get_profile(__attach_match(name, list));
180 rcu_read_unlock();
181
182 return profile;
183}
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206static const char *separate_fqname(const char *fqname, const char **ns_name)
207{
208 const char *name;
209
210 if (fqname[0] == ':') {
211
212
213
214
215 *ns_name = fqname + 1;
216 name = *ns_name + strlen(*ns_name) + 1;
217 if (!*name)
218 name = NULL;
219 } else {
220 *ns_name = NULL;
221 name = fqname;
222 }
223
224 return name;
225}
226
227static const char *next_name(int xtype, const char *name)
228{
229 return NULL;
230}
231
232
233
234
235
236
237
238
239static struct aa_profile *x_table_lookup(struct aa_profile *profile, u32 xindex)
240{
241 struct aa_profile *new_profile = NULL;
242 struct aa_namespace *ns = profile->ns;
243 u32 xtype = xindex & AA_X_TYPE_MASK;
244 int index = xindex & AA_X_INDEX_MASK;
245 const char *name;
246
247
248 for (name = profile->file.trans.table[index]; !new_profile && name;
249 name = next_name(xtype, name)) {
250 struct aa_namespace *new_ns;
251 const char *xname = NULL;
252
253 new_ns = NULL;
254 if (xindex & AA_X_CHILD) {
255
256 new_profile = aa_find_child(profile, name);
257 continue;
258 } else if (*name == ':') {
259
260 const char *ns_name;
261 xname = name = separate_fqname(name, &ns_name);
262 if (!xname)
263
264 xname = profile->base.hname;
265 if (*ns_name == '@') {
266
267 ;
268 }
269
270 new_ns = aa_find_namespace(ns, ns_name);
271 if (!new_ns)
272 continue;
273 } else if (*name == '@') {
274
275 continue;
276 } else {
277
278 xname = name;
279 }
280
281
282 new_profile = aa_lookup_profile(new_ns ? new_ns : ns, xname);
283 aa_put_namespace(new_ns);
284 }
285
286
287 return new_profile;
288}
289
290
291
292
293
294
295
296
297
298
299
300static struct aa_profile *x_to_profile(struct aa_profile *profile,
301 const char *name, u32 xindex)
302{
303 struct aa_profile *new_profile = NULL;
304 struct aa_namespace *ns = profile->ns;
305 u32 xtype = xindex & AA_X_TYPE_MASK;
306
307 switch (xtype) {
308 case AA_X_NONE:
309
310 return NULL;
311 case AA_X_NAME:
312 if (xindex & AA_X_CHILD)
313
314 new_profile = find_attach(ns, &profile->base.profiles,
315 name);
316 else
317
318 new_profile = find_attach(ns, &ns->base.profiles,
319 name);
320 break;
321 case AA_X_TABLE:
322
323 new_profile = x_table_lookup(profile, xindex);
324 break;
325 }
326
327
328 return new_profile;
329}
330
331
332
333
334
335
336
337int apparmor_bprm_set_creds(struct linux_binprm *bprm)
338{
339 struct aa_task_cxt *cxt;
340 struct aa_profile *profile, *new_profile = NULL;
341 struct aa_namespace *ns;
342 char *buffer = NULL;
343 unsigned int state;
344 struct file_perms perms = {};
345 struct path_cond cond = {
346 file_inode(bprm->file)->i_uid,
347 file_inode(bprm->file)->i_mode
348 };
349 const char *name = NULL, *target = NULL, *info = NULL;
350 int error = 0;
351
352 if (bprm->cred_prepared)
353 return 0;
354
355 cxt = cred_cxt(bprm->cred);
356 BUG_ON(!cxt);
357
358 profile = aa_get_newest_profile(cxt->profile);
359
360
361
362
363 ns = profile->ns;
364 state = profile->file.start;
365
366
367 error = aa_path_name(&bprm->file->f_path, profile->path_flags, &buffer,
368 &name, &info);
369 if (error) {
370 if (unconfined(profile) ||
371 (profile->flags & PFLAG_IX_ON_NAME_ERROR))
372 error = 0;
373 name = bprm->filename;
374 goto audit;
375 }
376
377
378
379
380 if (unconfined(profile)) {
381
382 if (cxt->onexec)
383
384 new_profile = aa_get_profile(cxt->onexec);
385 else
386 new_profile = find_attach(ns, &ns->base.profiles, name);
387 if (!new_profile)
388 goto cleanup;
389
390
391
392
393
394 goto apply;
395 }
396
397
398 state = aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
399 if (cxt->onexec) {
400 struct file_perms cp;
401 info = "change_profile onexec";
402 if (!(perms.allow & AA_MAY_ONEXEC))
403 goto audit;
404
405
406
407
408
409 state = aa_dfa_null_transition(profile->file.dfa, state);
410 cp = change_profile_perms(profile, cxt->onexec->ns,
411 cxt->onexec->base.name,
412 AA_MAY_ONEXEC, state);
413
414 if (!(cp.allow & AA_MAY_ONEXEC))
415 goto audit;
416 new_profile = aa_get_newest_profile(cxt->onexec);
417 goto apply;
418 }
419
420 if (perms.allow & MAY_EXEC) {
421
422 new_profile = x_to_profile(profile, name, perms.xindex);
423 if (!new_profile) {
424 if (perms.xindex & AA_X_INHERIT) {
425
426
427
428
429 info = "ix fallback";
430 new_profile = aa_get_profile(profile);
431 goto x_clear;
432 } else if (perms.xindex & AA_X_UNCONFINED) {
433 new_profile = aa_get_newest_profile(ns->unconfined);
434 info = "ux fallback";
435 } else {
436 error = -ENOENT;
437 info = "profile not found";
438
439 perms.allow &= ~MAY_EXEC;
440 }
441 }
442 } else if (COMPLAIN_MODE(profile)) {
443
444 new_profile = aa_new_null_profile(profile, 0);
445 if (!new_profile) {
446 error = -ENOMEM;
447 info = "could not create null profile";
448 } else {
449 error = -EACCES;
450 target = new_profile->base.hname;
451 }
452 perms.xindex |= AA_X_UNSAFE;
453 } else
454
455 error = -EACCES;
456
457
458
459
460
461 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) {
462 aa_put_profile(new_profile);
463 error = -EPERM;
464 goto cleanup;
465 }
466
467 if (!new_profile)
468 goto audit;
469
470 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
471
472 ;
473 }
474
475 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
476 error = may_change_ptraced_domain(new_profile);
477 if (error) {
478 aa_put_profile(new_profile);
479 goto audit;
480 }
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495 if (!(perms.xindex & AA_X_UNSAFE)) {
496 AA_DEBUG("scrubbing environment variables for %s profile=%s\n",
497 name, new_profile->base.hname);
498 bprm->unsafe |= AA_SECURE_X_NEEDED;
499 }
500apply:
501 target = new_profile->base.hname;
502
503 bprm->per_clear |= PER_CLEAR_ON_SETID;
504
505x_clear:
506 aa_put_profile(cxt->profile);
507
508 cxt->profile = new_profile;
509
510
511 aa_clear_task_cxt_trans(cxt);
512
513audit:
514 error = aa_audit_file(profile, &perms, GFP_KERNEL, OP_EXEC, MAY_EXEC,
515 name, target, cond.uid, info, error);
516
517cleanup:
518 aa_put_profile(profile);
519 kfree(buffer);
520
521 return error;
522}
523
524
525
526
527
528
529
530int apparmor_bprm_secureexec(struct linux_binprm *bprm)
531{
532
533
534
535 if (bprm->unsafe & AA_SECURE_X_NEEDED)
536 return 1;
537
538 return 0;
539}
540
541
542
543
544
545void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
546{
547 struct aa_profile *profile = __aa_current_profile();
548 struct aa_task_cxt *new_cxt = cred_cxt(bprm->cred);
549
550
551 if ((new_cxt->profile == profile) ||
552 (unconfined(new_cxt->profile)))
553 return;
554
555 current->pdeath_signal = 0;
556
557
558 __aa_transition_rlimits(profile, new_cxt->profile);
559}
560
561
562
563
564
565void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
566{
567
568 return;
569}
570
571
572
573
574
575
576
577
578
579
580
581
582static char *new_compound_name(const char *n1, const char *n2)
583{
584 char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL);
585 if (name)
586 sprintf(name, "%s//%s", n1, n2);
587 return name;
588}
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)
605{
606 const struct cred *cred;
607 struct aa_task_cxt *cxt;
608 struct aa_profile *profile, *previous_profile, *hat = NULL;
609 char *name = NULL;
610 int i;
611 struct file_perms perms = {};
612 const char *target = NULL, *info = NULL;
613 int error = 0;
614
615
616
617
618
619
620 if (task_no_new_privs(current))
621 return -EPERM;
622
623
624 cred = get_current_cred();
625 cxt = cred_cxt(cred);
626 profile = aa_cred_profile(cred);
627 previous_profile = cxt->previous;
628
629 if (unconfined(profile)) {
630 info = "unconfined";
631 error = -EPERM;
632 goto audit;
633 }
634
635 if (count) {
636
637 struct aa_profile *root;
638 if (PROFILE_IS_HAT(profile))
639 root = aa_get_profile_rcu(&profile->parent);
640 else
641 root = aa_get_profile(profile);
642
643
644 for (i = 0; i < count && !hat; i++)
645
646 hat = aa_find_child(root, hats[i]);
647 if (!hat) {
648 if (!COMPLAIN_MODE(root) || permtest) {
649 if (list_empty(&root->base.profiles))
650 error = -ECHILD;
651 else
652 error = -ENOENT;
653 aa_put_profile(root);
654 goto out;
655 }
656
657
658
659
660
661
662
663
664
665
666
667 name = new_compound_name(root->base.hname, hats[0]);
668 aa_put_profile(root);
669 target = name;
670
671 hat = aa_new_null_profile(profile, 1);
672 if (!hat) {
673 info = "failed null profile create";
674 error = -ENOMEM;
675 goto audit;
676 }
677 } else {
678 aa_put_profile(root);
679 target = hat->base.hname;
680 if (!PROFILE_IS_HAT(hat)) {
681 info = "target not hat";
682 error = -EPERM;
683 goto audit;
684 }
685 }
686
687 error = may_change_ptraced_domain(hat);
688 if (error) {
689 info = "ptraced";
690 error = -EPERM;
691 goto audit;
692 }
693
694 if (!permtest) {
695 error = aa_set_current_hat(hat, token);
696 if (error == -EACCES)
697
698 perms.kill = AA_MAY_CHANGEHAT;
699 else if (name && !error)
700
701 error = -ENOENT;
702 }
703 } else if (previous_profile) {
704
705
706
707 target = previous_profile->base.hname;
708 error = aa_restore_previous_profile(token);
709 perms.kill = AA_MAY_CHANGEHAT;
710 } else
711
712 goto out;
713
714audit:
715 if (!permtest)
716 error = aa_audit_file(profile, &perms, GFP_KERNEL,
717 OP_CHANGE_HAT, AA_MAY_CHANGEHAT, NULL,
718 target, GLOBAL_ROOT_UID, info, error);
719
720out:
721 aa_put_profile(hat);
722 kfree(name);
723 put_cred(cred);
724
725 return error;
726}
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743int aa_change_profile(const char *ns_name, const char *hname, bool onexec,
744 bool permtest)
745{
746 const struct cred *cred;
747 struct aa_profile *profile, *target = NULL;
748 struct aa_namespace *ns = NULL;
749 struct file_perms perms = {};
750 const char *name = NULL, *info = NULL;
751 int op, error = 0;
752 u32 request;
753
754 if (!hname && !ns_name)
755 return -EINVAL;
756
757 if (onexec) {
758 request = AA_MAY_ONEXEC;
759 op = OP_CHANGE_ONEXEC;
760 } else {
761 request = AA_MAY_CHANGE_PROFILE;
762 op = OP_CHANGE_PROFILE;
763 }
764
765 cred = get_current_cred();
766 profile = aa_cred_profile(cred);
767
768
769
770
771
772
773
774
775 if (task_no_new_privs(current) && !unconfined(profile)) {
776 put_cred(cred);
777 return -EPERM;
778 }
779
780 if (ns_name) {
781
782 ns = aa_find_namespace(profile->ns, ns_name);
783 if (!ns) {
784
785 name = ns_name;
786 info = "namespace not found";
787 error = -ENOENT;
788 goto audit;
789 }
790 } else
791
792 ns = aa_get_namespace(profile->ns);
793
794
795 if (!hname) {
796 if (unconfined(profile))
797 hname = ns->unconfined->base.hname;
798 else
799 hname = profile->base.hname;
800 }
801
802 perms = change_profile_perms(profile, ns, hname, request,
803 profile->file.start);
804 if (!(perms.allow & request)) {
805 error = -EACCES;
806 goto audit;
807 }
808
809
810 target = aa_lookup_profile(ns, hname);
811 if (!target) {
812 info = "profile not found";
813 error = -ENOENT;
814 if (permtest || !COMPLAIN_MODE(profile))
815 goto audit;
816
817 target = aa_new_null_profile(profile, 0);
818 if (!target) {
819 info = "failed null profile create";
820 error = -ENOMEM;
821 goto audit;
822 }
823 }
824
825
826 error = may_change_ptraced_domain(target);
827 if (error) {
828 info = "ptrace prevents transition";
829 goto audit;
830 }
831
832 if (permtest)
833 goto audit;
834
835 if (onexec)
836 error = aa_set_current_onexec(target);
837 else
838 error = aa_replace_current_profile(target);
839
840audit:
841 if (!permtest)
842 error = aa_audit_file(profile, &perms, GFP_KERNEL, op, request,
843 name, hname, GLOBAL_ROOT_UID, info, error);
844
845 aa_put_namespace(ns);
846 aa_put_profile(target);
847 put_cred(cred);
848
849 return error;
850}
851