]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/apparmor/domain.c
UBUNTU: SAUCE: apparmor: fix nnp subset check failure when, stacking
[mirror_ubuntu-bionic-kernel.git] / security / apparmor / domain.c
CommitLineData
898127c3
JJ
1/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor policy attachment and domain transitions
5 *
6 * Copyright (C) 2002-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
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"
cff281f6 32#include "include/policy_ns.h"
898127c3
JJ
33
34/**
35 * aa_free_domain_entries - free entries in a domain table
36 * @domain: the domain table to free (MAYBE NULL)
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 * may_change_ptraced_domain - check if can change profile on ptraced task
b2d09ae4
JJ
54 * @to_label: profile to change to (NOT NULL)
55 * @info: message if there is an error
898127c3 56 *
51775fe7 57 * Check if current is ptraced and if so if the tracing task is allowed
898127c3
JJ
58 * to trace the new domain
59 *
60 * Returns: %0 or error if change not allowed
61 */
b2d09ae4
JJ
62static int may_change_ptraced_domain(struct aa_label *to_label,
63 const char **info)
898127c3
JJ
64{
65 struct task_struct *tracer;
637f688d 66 struct aa_label *tracerl = NULL;
898127c3
JJ
67 int error = 0;
68
69 rcu_read_lock();
51775fe7 70 tracer = ptrace_parent(current);
3cfcc19e 71 if (tracer)
898127c3 72 /* released below */
637f688d 73 tracerl = aa_get_task_label(tracer);
898127c3
JJ
74
75 /* not ptraced */
637f688d 76 if (!tracer || unconfined(tracerl))
898127c3
JJ
77 goto out;
78
b2d09ae4 79 error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
898127c3
JJ
80
81out:
04fdc099 82 rcu_read_unlock();
637f688d 83 aa_put_label(tracerl);
898127c3 84
b2d09ae4
JJ
85 if (error)
86 *info = "ptrace prevents transition";
898127c3
JJ
87 return error;
88}
89
93c98a48
JJ
90/**** TODO: dedup to aa_label_match - needs perm and dfa, merging
91 * specifically this is an exact copy of aa_label_match except
92 * aa_compute_perms is replaced with aa_compute_fperms
93 * and policy.dfa with file.dfa
94 ****/
95/* match a profile and its associated ns component if needed
96 * Assumes visibility test has already been done.
97 * If a subns profile is not to be matched should be prescreened with
98 * visibility test.
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 /* try matching with namespace name and then profile */
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 * label_compound_match - find perms for full compound label
121 * @profile: profile to find perms for
122 * @label: label to check access permissions for
123 * @stack: whether this is a stacking request
124 * @start: state to start match in
125 * @subns: whether to do permission checks on components in a subns
126 * @request: permissions to request
127 * @perms: perms struct to set
128 *
129 * Returns: 0 on success else ERROR
130 *
131 * For the label A//&B//&C this does the perm match for A//&B//&C
132 * @perms should be preinitialized with allperms OR a previous permission
133 * check to be stacked.
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 /* find first subcomponent that is visible */
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 /* no component visible */
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 * label_components_match - find perms for all subcomponents of a label
181 * @profile: profile to find perms for
182 * @label: label to check access permissions for
183 * @stack: whether this is a stacking request
184 * @start: state to start match in
185 * @subns: whether to do permission checks on components in a subns
186 * @request: permissions to request
187 * @perms: an initialized perms struct to add accumulation to
188 *
189 * Returns: 0 on success else ERROR
190 *
191 * For the label A//&B//&C this does the perm match for each of A and B and C
192 * @perms should be preinitialized with allperms OR a previous permission
193 * check to be stacked.
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 /* find first subcomponent to test */
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 /* no subcomponents visible - no change in perms */
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 * label_match - do a multi-component label match
246 * @profile: profile to match against (NOT NULL)
247 * @label: label to match (NOT NULL)
248 * @stack: whether this is a stacking request
249 * @state: state to start in
250 * @subns: whether to match subns components
251 * @request: permission request
252 * @perms: Returns computed perms (NOT NULL)
253 *
254 * Returns: the state the match finished in, may be the none matching state
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/******* end TODO: dedup *****/
274
898127c3
JJ
275/**
276 * change_profile_perms - find permissions for change_profile
277 * @profile: the current profile (NOT NULL)
93c98a48
JJ
278 * @target: label to transition to (NOT NULL)
279 * @stack: whether this is a stacking request
898127c3
JJ
280 * @request: requested perms
281 * @start: state to start matching in
282 *
93c98a48 283 *
898127c3 284 * Returns: permission set
93c98a48
JJ
285 *
286 * currently only matches full label A//&B//&C or individual components A, B, C
287 * not arbitrary combinations. Eg. A//&B, C
898127c3 288 */
93c98a48
JJ
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 /* TODO: add profile in ns screening */
301 return label_match(profile, target, stack, start, true, request, perms);
302}
303
898127c3
JJ
304/**
305 * __attach_match_ - find an attachment match
306 * @name - to match against (NOT NULL)
307 * @head - profile list to walk (NOT NULL)
844b8292 308 * @info - info message if there was an error (NOT NULL)
898127c3
JJ
309 *
310 * Do a linear search on the profiles in the list. There is a matching
311 * preference where an exact match is preferred over a name which uses
312 * expressions to match, and matching expressions with the greatest
313 * xmatch_len are preferred.
314 *
315 * Requires: @head not be shared or have appropriate locks held
316 *
317 * Returns: profile or NULL if no match found
318 */
319static struct aa_profile *__attach_match(const char *name,
844b8292
JJ
320 struct list_head *head,
321 const char **info)
898127c3
JJ
322{
323 int len = 0;
844b8292 324 bool conflict = false;
898127c3
JJ
325 struct aa_profile *profile, *candidate = NULL;
326
01e2b670 327 list_for_each_entry_rcu(profile, head, base.list) {
06d426d1
JJ
328 if (profile->label.flags & FLAG_NULL &&
329 &profile->label == ns_unconfined(profile->ns))
898127c3 330 continue;
06d426d1 331
844b8292 332 if (profile->xmatch) {
1a3881d3 333 if (profile->xmatch_len >= len) {
844b8292
JJ
334 unsigned int state;
335 u32 perm;
336
337 state = aa_dfa_match(profile->xmatch,
338 DFA_START, name);
339 perm = dfa_user_allow(profile->xmatch, state);
340 /* any accepting state means a valid match. */
341 if (perm & MAY_EXEC) {
1a3881d3
MG
342 if (profile->xmatch_len == len) {
343 conflict = true;
344 continue;
345 }
844b8292
JJ
346 candidate = profile;
347 len = profile->xmatch_len;
348 conflict = false;
349 }
898127c3
JJ
350 }
351 } else if (!strcmp(profile->base.name, name))
352 /* exact non-re match, no more searching required */
353 return profile;
354 }
355
844b8292
JJ
356 if (conflict) {
357 *info = "conflicting profile attachments";
358 return NULL;
359 }
360
898127c3
JJ
361 return candidate;
362}
363
364/**
365 * find_attach - do attachment search for unconfined processes
366 * @ns: the current namespace (NOT NULL)
367 * @list: list to search (NOT NULL)
368 * @name: the executable name to match against (NOT NULL)
844b8292 369 * @info: info message if there was an error
898127c3 370 *
93c98a48 371 * Returns: label or NULL if no match found
898127c3 372 */
93c98a48 373static struct aa_label *find_attach(struct aa_ns *ns, struct list_head *list,
844b8292 374 const char *name, const char **info)
898127c3
JJ
375{
376 struct aa_profile *profile;
377
01e2b670 378 rcu_read_lock();
844b8292 379 profile = aa_get_profile(__attach_match(name, list, info));
01e2b670 380 rcu_read_unlock();
898127c3 381
93c98a48 382 return profile ? &profile->label : NULL;
898127c3
JJ
383}
384
385static const char *next_name(int xtype, const char *name)
386{
387 return NULL;
388}
389
390/**
391 * x_table_lookup - lookup an x transition name via transition table
392 * @profile: current profile (NOT NULL)
393 * @xindex: index into x transition table
93c98a48 394 * @name: returns: name tested to find label (NOT NULL)
898127c3 395 *
93c98a48 396 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
898127c3 397 */
2ea3ffb7
JJ
398struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
399 const char **name)
898127c3 400{
93c98a48 401 struct aa_label *label = NULL;
898127c3
JJ
402 u32 xtype = xindex & AA_X_TYPE_MASK;
403 int index = xindex & AA_X_INDEX_MASK;
898127c3 404
93c98a48 405 AA_BUG(!name);
898127c3 406
93c98a48
JJ
407 /* index is guaranteed to be in range, validated at load time */
408 /* TODO: move lookup parsing to unpack time so this is a straight
409 * index into the resultant label
410 */
411 for (*name = profile->file.trans.table[index]; !label && *name;
412 *name = next_name(xtype, *name)) {
898127c3 413 if (xindex & AA_X_CHILD) {
93c98a48 414 struct aa_profile *new_profile;
898127c3 415 /* release by caller */
93c98a48
JJ
416 new_profile = aa_find_child(profile, *name);
417 if (new_profile)
418 label = &new_profile->label;
898127c3 419 continue;
898127c3 420 }
93c98a48
JJ
421 label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
422 true, false);
423 if (IS_ERR(label))
424 label = NULL;
898127c3
JJ
425 }
426
427 /* released by caller */
93c98a48
JJ
428
429 return label;
898127c3
JJ
430}
431
432/**
93c98a48 433 * x_to_label - get target label for a given xindex
898127c3
JJ
434 * @profile: current profile (NOT NULL)
435 * @name: name to lookup (NOT NULL)
436 * @xindex: index into x transition table
93c98a48 437 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
898127c3 438 *
93c98a48 439 * find label for a transition index
898127c3 440 *
93c98a48 441 * Returns: refcounted label or NULL if not found available
898127c3 442 */
93c98a48
JJ
443static struct aa_label *x_to_label(struct aa_profile *profile,
444 const char *name, u32 xindex,
445 const char **lookupname,
446 const char **info)
898127c3 447{
93c98a48 448 struct aa_label *new = NULL;
98849dff 449 struct aa_ns *ns = profile->ns;
898127c3 450 u32 xtype = xindex & AA_X_TYPE_MASK;
93c98a48 451 const char *stack = NULL;
898127c3
JJ
452
453 switch (xtype) {
454 case AA_X_NONE:
455 /* fail exec unless ix || ux fallback - handled by caller */
93c98a48
JJ
456 *lookupname = NULL;
457 break;
458 case AA_X_TABLE:
459 /* TODO: fix when perm mapping done at unload */
460 stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
461 if (*stack != '&') {
462 /* released by caller */
463 new = x_table_lookup(profile, xindex, lookupname);
464 stack = NULL;
465 break;
466 }
467 /* fall through to X_NAME */
898127c3
JJ
468 case AA_X_NAME:
469 if (xindex & AA_X_CHILD)
470 /* released by caller */
93c98a48 471 new = find_attach(ns, &profile->base.profiles,
844b8292 472 name, info);
898127c3
JJ
473 else
474 /* released by caller */
93c98a48 475 new = find_attach(ns, &ns->base.profiles,
844b8292 476 name, info);
93c98a48 477 *lookupname = name;
898127c3
JJ
478 break;
479 }
480
93c98a48
JJ
481 if (!new) {
482 if (xindex & AA_X_INHERIT) {
483 /* (p|c|n)ix - don't change profile but do
484 * use the newest version
485 */
486 *info = "ix fallback";
487 /* no profile && no error */
488 new = aa_get_newest_label(&profile->label);
489 } else if (xindex & AA_X_UNCONFINED) {
490 new = aa_get_newest_label(ns_unconfined(profile->ns));
491 *info = "ux fallback";
492 }
493 }
494
495 if (new && stack) {
496 /* base the stack on post domain transition */
497 struct aa_label *base = new;
498
499 new = aa_label_parse(base, stack, GFP_ATOMIC, true, false);
500 if (IS_ERR(new))
501 new = NULL;
502 aa_put_label(base);
503 }
504
898127c3 505 /* released by caller */
93c98a48 506 return new;
898127c3
JJ
507}
508
93c98a48
JJ
509static struct aa_label *profile_transition(struct aa_profile *profile,
510 const struct linux_binprm *bprm,
511 char *buffer, struct path_cond *cond,
512 bool *secure_exec)
898127c3 513{
93c98a48
JJ
514 struct aa_label *new = NULL;
515 const char *info = NULL, *name = NULL, *target = NULL;
516 unsigned int state = profile->file.start;
2d679f3c 517 struct aa_perms perms = {};
93c98a48 518 bool nonewprivs = false;
b1d9e6b0 519 int error = 0;
898127c3 520
93c98a48
JJ
521 AA_BUG(!profile);
522 AA_BUG(!bprm);
523 AA_BUG(!buffer);
898127c3 524
4227c333 525 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
72c8a768 526 &name, &info, profile->disconnected);
898127c3 527 if (error) {
637f688d 528 if (profile_unconfined(profile) ||
93c98a48
JJ
529 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
530 AA_DEBUG("name lookup ix on error");
898127c3 531 error = 0;
93c98a48
JJ
532 new = aa_get_newest_label(&profile->label);
533 }
898127c3
JJ
534 name = bprm->filename;
535 goto audit;
536 }
537
637f688d 538 if (profile_unconfined(profile)) {
93c98a48 539 new = find_attach(profile->ns, &profile->ns->base.profiles,
844b8292 540 name, &info);
93c98a48
JJ
541 if (new) {
542 AA_DEBUG("unconfined attached to new label");
543 return new;
544 }
545 AA_DEBUG("unconfined exec no attachment");
546 return aa_get_newest_label(&profile->label);
898127c3
JJ
547 }
548
549 /* find exec permissions for name */
93c98a48 550 state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
898127c3
JJ
551 if (perms.allow & MAY_EXEC) {
552 /* exec permission determine how to transition */
93c98a48
JJ
553 new = x_to_label(profile, name, perms.xindex, &target, &info);
554 if (new && new->proxy == profile->label.proxy && info) {
555 /* hack ix fallback - improve how this is detected */
556 goto audit;
557 } else if (!new) {
558 error = -EACCES;
559 info = "profile transition not found";
560 /* remove MAY_EXEC to audit as failure */
561 perms.allow &= ~MAY_EXEC;
898127c3
JJ
562 }
563 } else if (COMPLAIN_MODE(profile)) {
93c98a48 564 /* no exec permission - learning mode */
5d7c44ef
JJ
565 struct aa_profile *new_profile = NULL;
566 char *n = kstrdup(name, GFP_ATOMIC);
567
568 if (n) {
569 /* name is ptr into buffer */
570 long pos = name - buffer;
571 /* break per cpu buffer hold */
572 put_buffers(buffer);
573 new_profile = aa_new_null_profile(profile, false, n,
574 GFP_KERNEL);
575 get_buffers(buffer);
576 name = buffer + pos;
577 strcpy((char *)name, n);
578 kfree(n);
579 }
898127c3
JJ
580 if (!new_profile) {
581 error = -ENOMEM;
582 info = "could not create null profile";
93c98a48 583 } else {
898127c3 584 error = -EACCES;
93c98a48
JJ
585 new = &new_profile->label;
586 }
898127c3
JJ
587 perms.xindex |= AA_X_UNSAFE;
588 } else
589 /* fail exec */
590 error = -EACCES;
591
93c98a48
JJ
592 if (!new)
593 goto audit;
594
93c98a48
JJ
595
596 if (!(perms.xindex & AA_X_UNSAFE)) {
597 if (DEBUG_ON) {
598 dbg_printk("apparmor: scrubbing environment variables"
599 " for %s profile=", name);
600 aa_label_printk(new, GFP_ATOMIC);
601 dbg_printk("\n");
602 }
603 *secure_exec = true;
c29bceb3
JJ
604 }
605
93c98a48
JJ
606audit:
607 aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
608 cond->uid, info, error);
609 if (!new || nonewprivs) {
610 aa_put_label(new);
611 return ERR_PTR(error);
612 }
613
614 return new;
615}
616
617static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
618 bool stack, const struct linux_binprm *bprm,
619 char *buffer, struct path_cond *cond,
620 bool *secure_exec)
621{
622 unsigned int state = profile->file.start;
623 struct aa_perms perms = {};
624 const char *xname = NULL, *info = "change_profile onexec";
625 int error = -EACCES;
626
627 AA_BUG(!profile);
628 AA_BUG(!onexec);
629 AA_BUG(!bprm);
630 AA_BUG(!buffer);
631
632 if (profile_unconfined(profile)) {
633 /* change_profile on exec already granted */
634 /*
635 * NOTE: Domain transitions from unconfined are allowed
636 * even when no_new_privs is set because this aways results
637 * in a further reduction of permissions.
638 */
639 return 0;
640 }
641
642 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
643 &xname, &info, profile->disconnected);
644 if (error) {
645 if (profile_unconfined(profile) ||
646 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
647 AA_DEBUG("name lookup ix on error");
648 error = 0;
649 }
650 xname = bprm->filename;
898127c3 651 goto audit;
93c98a48
JJ
652 }
653
654 /* find exec permissions for name */
655 state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
656 if (!(perms.allow & AA_MAY_ONEXEC)) {
657 info = "no change_onexec valid for executable";
658 goto audit;
659 }
660 /* test if this exec can be paired with change_profile onexec.
661 * onexec permission is linked to exec with a standard pairing
662 * exec\0change_profile
663 */
664 state = aa_dfa_null_transition(profile->file.dfa, state);
665 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
666 state, &perms);
667 if (error) {
668 perms.allow &= ~AA_MAY_ONEXEC;
669 goto audit;
670 }
93c98a48
JJ
671
672 if (!(perms.xindex & AA_X_UNSAFE)) {
673 if (DEBUG_ON) {
674 dbg_printk("apparmor: scrubbing environment "
675 "variables for %s label=", xname);
676 aa_label_printk(onexec, GFP_ATOMIC);
677 dbg_printk("\n");
678 }
679 *secure_exec = true;
680 }
681
682audit:
683 return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
684 NULL, onexec, cond->uid, info, error);
685}
686
687/* ensure none ns domain transitions are correctly applied with onexec */
688
689static struct aa_label *handle_onexec(struct aa_label *label,
690 struct aa_label *onexec, bool stack,
691 const struct linux_binprm *bprm,
692 char *buffer, struct path_cond *cond,
693 bool *unsafe)
694{
695 struct aa_profile *profile;
696 struct aa_label *new;
697 int error;
698
699 AA_BUG(!label);
700 AA_BUG(!onexec);
701 AA_BUG(!bprm);
702 AA_BUG(!buffer);
703
704 if (!stack) {
705 error = fn_for_each_in_ns(label, profile,
706 profile_onexec(profile, onexec, stack,
707 bprm, buffer, cond, unsafe));
708 if (error)
709 return ERR_PTR(error);
710 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
711 aa_get_newest_label(onexec),
712 profile_transition(profile, bprm, buffer,
713 cond, unsafe));
714
715 } else {
716 /* TODO: determine how much we want to losen this */
717 error = fn_for_each_in_ns(label, profile,
718 profile_onexec(profile, onexec, stack, bprm,
719 buffer, cond, unsafe));
720 if (error)
721 return ERR_PTR(error);
722 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
723 aa_label_merge(&profile->label, onexec,
724 GFP_ATOMIC),
725 profile_transition(profile, bprm, buffer,
726 cond, unsafe));
727 }
728
729 if (new)
730 return new;
731
732 /* TODO: get rid of GLOBAL_ROOT_UID */
733 error = fn_for_each_in_ns(label, profile,
734 aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
735 AA_MAY_ONEXEC, bprm->filename, NULL,
736 onexec, GLOBAL_ROOT_UID,
737 "failed to build target label", -ENOMEM));
738 return ERR_PTR(error);
739}
740
741/**
742 * apparmor_bprm_set_creds - set the new creds on the bprm struct
743 * @bprm: binprm for the exec (NOT NULL)
744 *
745 * Returns: %0 or error on failure
746 *
747 * TODO: once the other paths are done see if we can't refactor into a fn
748 */
749int apparmor_bprm_set_creds(struct linux_binprm *bprm)
750{
751 struct aa_task_ctx *ctx;
752 struct aa_label *label, *new = NULL;
753 struct aa_profile *profile;
754 char *buffer = NULL;
755 const char *info = NULL;
756 int error = 0;
757 bool unsafe = false;
758 struct path_cond cond = {
759 file_inode(bprm->file)->i_uid,
760 file_inode(bprm->file)->i_mode
761 };
762
ddb4a144 763 if (bprm->called_set_creds)
93c98a48
JJ
764 return 0;
765
766 ctx = cred_ctx(bprm->cred);
767 AA_BUG(!ctx);
768
769 label = aa_get_newest_label(ctx->label);
770
771 /* buffer freed below, name is pointer into buffer */
772 get_buffers(buffer);
773 /* Test for onexec first as onexec override other x transitions. */
774 if (ctx->onexec)
775 new = handle_onexec(label, ctx->onexec, ctx->token,
776 bprm, buffer, &cond, &unsafe);
777 else
778 new = fn_label_build(label, profile, GFP_ATOMIC,
779 profile_transition(profile, bprm, buffer,
780 &cond, &unsafe));
781
782 AA_BUG(!new);
783 if (IS_ERR(new)) {
784 error = PTR_ERR(new);
785 goto done;
786 } else if (!new) {
787 error = -ENOMEM;
788 goto done;
789 }
790
d9b32413
JJ
791 /* Policy has specified a domain transitions. If no_new_privs and
792 * confined ensure the transition is to confinement that is subset
793 * of the confinement when the task entered no new privs.
794 *
795 * NOTE: Domain transitions from unconfined and to stacked
796 * subsets are allowed even when no_new_privs is set because this
797 * aways results in a further reduction of permissions.
798 */
799 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
800 !unconfined(label) && !aa_label_is_subset(new, label)) {
801 error = -EPERM;
802 info = "no new privs";
803 goto audit;
804 }
898127c3
JJ
805
806 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
807 /* FIXME: currently don't mediate shared state */
808 ;
809 }
810
93c98a48
JJ
811 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
812 /* TODO: test needs to be profile of label to new */
813 error = may_change_ptraced_domain(new, &info);
f7da2de0 814 if (error)
898127c3 815 goto audit;
898127c3
JJ
816 }
817
93c98a48
JJ
818 if (unsafe) {
819 if (DEBUG_ON) {
820 dbg_printk("scrubbing environment variables for %s "
821 "label=", bprm->filename);
822 aa_label_printk(new, GFP_ATOMIC);
823 dbg_printk("\n");
824 }
993b3ab0 825 bprm->secureexec = 1;
898127c3 826 }
898127c3 827
93c98a48
JJ
828 if (label->proxy != new->proxy) {
829 /* when transitioning clear unsafe personality bits */
830 if (DEBUG_ON) {
831 dbg_printk("apparmor: clearing unsafe personality "
832 "bits. %s label=", bprm->filename);
833 aa_label_printk(new, GFP_ATOMIC);
834 dbg_printk("\n");
835 }
836 bprm->per_clear |= PER_CLEAR_ON_SETID;
837 }
637f688d 838 aa_put_label(ctx->label);
93c98a48
JJ
839 /* transfer reference, released when ctx is freed */
840 ctx->label = new;
898127c3 841
93c98a48
JJ
842done:
843 /* clear out temporary/transitional state from the context */
55a26ebf 844 aa_clear_task_ctx_trans(ctx);
898127c3 845
637f688d 846 aa_put_label(label);
4227c333 847 put_buffers(buffer);
898127c3
JJ
848
849 return error;
93c98a48
JJ
850
851audit:
852 error = fn_for_each(label, profile,
853 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
854 bprm->filename, NULL, new,
855 file_inode(bprm->file)->i_uid, info,
856 error));
857 aa_put_label(new);
858 goto done;
898127c3
JJ
859}
860
898127c3
JJ
861/*
862 * Functions for self directed profile change
863 */
864
89dbf196
JJ
865
866/* helper fn for change_hat
898127c3 867 *
89dbf196 868 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
898127c3 869 */
89dbf196
JJ
870static struct aa_label *build_change_hat(struct aa_profile *profile,
871 const char *name, bool sibling)
898127c3 872{
89dbf196
JJ
873 struct aa_profile *root, *hat = NULL;
874 const char *info = NULL;
875 int error = 0;
876
877 if (sibling && PROFILE_IS_HAT(profile)) {
878 root = aa_get_profile_rcu(&profile->parent);
879 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
880 root = aa_get_profile(profile);
881 } else {
882 info = "conflicting target types";
883 error = -EPERM;
884 goto audit;
885 }
886
887 hat = aa_find_child(root, name);
888 if (!hat) {
889 error = -ENOENT;
890 if (COMPLAIN_MODE(profile)) {
891 hat = aa_new_null_profile(profile, true, name,
892 GFP_KERNEL);
893 if (!hat) {
894 info = "failed null profile create";
895 error = -ENOMEM;
896 }
897 }
898 }
899 aa_put_profile(root);
900
901audit:
902 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
903 name, hat ? hat->base.hname : NULL,
904 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, NULL,
905 error);
906 if (!hat || (error && error != -ENOENT))
907 return ERR_PTR(error);
908 /* if hat && error - complain mode, already audited and we adjust for
909 * complain mode allow by returning hat->label
910 */
911 return &hat->label;
912}
913
914/* helper fn for changing into a hat
915 *
916 * Returns: label for hat transition or ERR_PTR. Does not return NULL
917 */
918static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
919 int count, int flags)
920{
921 struct aa_profile *profile, *root, *hat = NULL;
922 struct aa_label *new;
923 struct label_it it;
924 bool sibling = false;
925 const char *name, *info = NULL;
926 int i, error;
927
928 AA_BUG(!label);
929 AA_BUG(!hats);
930 AA_BUG(count < 1);
931
932 if (PROFILE_IS_HAT(labels_profile(label)))
933 sibling = true;
934
935 /*find first matching hat */
936 for (i = 0; i < count && !hat; i++) {
937 name = hats[i];
938 label_for_each_in_ns(it, labels_ns(label), label, profile) {
939 if (sibling && PROFILE_IS_HAT(profile)) {
940 root = aa_get_profile_rcu(&profile->parent);
941 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
942 root = aa_get_profile(profile);
943 } else { /* conflicting change type */
944 info = "conflicting targets types";
945 error = -EPERM;
946 goto fail;
947 }
948 hat = aa_find_child(root, name);
949 aa_put_profile(root);
950 if (!hat) {
951 if (!COMPLAIN_MODE(profile))
952 goto outer_continue;
953 /* complain mode succeed as if hat */
954 } else if (!PROFILE_IS_HAT(hat)) {
955 info = "target not hat";
956 error = -EPERM;
957 aa_put_profile(hat);
958 goto fail;
959 }
960 aa_put_profile(hat);
961 }
962 /* found a hat for all profiles in ns */
963 goto build;
964outer_continue:
965 ;
966 }
967 /* no hats that match, find appropriate error
968 *
969 * In complain mode audit of the failure is based off of the first
970 * hat supplied. This is done due how userspace interacts with
971 * change_hat.
972 */
973 name = NULL;
974 label_for_each_in_ns(it, labels_ns(label), label, profile) {
975 if (!list_empty(&profile->base.profiles)) {
976 info = "hat not found";
977 error = -ENOENT;
978 goto fail;
979 }
980 }
981 info = "no hats defined";
982 error = -ECHILD;
983
984fail:
985 label_for_each_in_ns(it, labels_ns(label), label, profile) {
986 /*
987 * no target as it has failed to be found or built
988 *
989 * change_hat uses probing and should not log failures
990 * related to missing hats
991 */
992 /* TODO: get rid of GLOBAL_ROOT_UID */
993 if (count > 1 || COMPLAIN_MODE(profile)) {
994 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
995 AA_MAY_CHANGEHAT, name, NULL, NULL,
996 GLOBAL_ROOT_UID, info, error);
997 }
998 }
999 return ERR_PTR(error);
1000
1001build:
1002 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1003 build_change_hat(profile, name, sibling),
1004 aa_get_label(&profile->label));
1005 if (!new) {
1006 info = "label build failed";
1007 error = -ENOMEM;
1008 goto fail;
1009 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1010
1011 return new;
898127c3
JJ
1012}
1013
1014/**
1015 * aa_change_hat - change hat to/from subprofile
1016 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1017 * @count: number of hat names in @hats
1018 * @token: magic value to validate the hat change
df8073c6 1019 * @flags: flags affecting behavior of the change
898127c3 1020 *
89dbf196
JJ
1021 * Returns %0 on success, error otherwise.
1022 *
898127c3
JJ
1023 * Change to the first profile specified in @hats that exists, and store
1024 * the @hat_magic in the current task context. If the count == 0 and the
1025 * @token matches that stored in the current task context, return to the
1026 * top level profile.
1027 *
89dbf196
JJ
1028 * change_hat only applies to profiles in the current ns, and each profile
1029 * in the ns must make the same transition otherwise change_hat will fail.
898127c3 1030 */
df8073c6 1031int aa_change_hat(const char *hats[], int count, u64 token, int flags)
898127c3
JJ
1032{
1033 const struct cred *cred;
55a26ebf 1034 struct aa_task_ctx *ctx;
89dbf196
JJ
1035 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1036 struct aa_profile *profile;
2d679f3c 1037 struct aa_perms perms = {};
89dbf196 1038 const char *info = NULL;
898127c3
JJ
1039 int error = 0;
1040
c29bceb3
JJ
1041 /*
1042 * Fail explicitly requested domain transitions if no_new_privs.
1043 * There is no exception for unconfined as change_hat is not
1044 * available.
1045 */
89dbf196
JJ
1046 if (task_no_new_privs(current)) {
1047 /* not an apparmor denial per se, so don't log it */
1048 AA_DEBUG("no_new_privs - change_hat denied");
c29bceb3 1049 return -EPERM;
89dbf196 1050 }
c29bceb3 1051
898127c3
JJ
1052 /* released below */
1053 cred = get_current_cred();
55a26ebf 1054 ctx = cred_ctx(cred);
637f688d 1055 label = aa_get_newest_cred_label(cred);
89dbf196 1056 previous = aa_get_newest_label(ctx->previous);
898127c3 1057
637f688d 1058 if (unconfined(label)) {
89dbf196 1059 info = "unconfined can not change_hat";
898127c3 1060 error = -EPERM;
89dbf196 1061 goto fail;
898127c3
JJ
1062 }
1063
1064 if (count) {
89dbf196
JJ
1065 new = change_hat(label, hats, count, flags);
1066 AA_BUG(!new);
1067 if (IS_ERR(new)) {
1068 error = PTR_ERR(new);
1069 new = NULL;
1070 /* already audited */
1071 goto out;
898127c3
JJ
1072 }
1073
89dbf196
JJ
1074 error = may_change_ptraced_domain(new, &info);
1075 if (error)
1076 goto fail;
898127c3 1077
89dbf196
JJ
1078 if (flags & AA_CHANGE_TEST)
1079 goto out;
1080
1081 target = new;
1082 error = aa_set_current_hat(new, token);
1083 if (error == -EACCES)
1084 /* kill task in case of brute force attacks */
1085 goto kill;
1086 } else if (previous && !(flags & AA_CHANGE_TEST)) {
1087 /* Return to saved label. Kill task if restore fails
898127c3
JJ
1088 * to avoid brute force attacks
1089 */
89dbf196 1090 target = previous;
637f688d 1091 error = aa_restore_previous_label(token);
89dbf196
JJ
1092 if (error) {
1093 if (error == -EACCES)
1094 goto kill;
1095 goto fail;
1096 }
1097 } /* else ignore @flags && restores when there is no saved profile */
898127c3
JJ
1098
1099out:
89dbf196
JJ
1100 aa_put_label(new);
1101 aa_put_label(previous);
637f688d 1102 aa_put_label(label);
898127c3
JJ
1103 put_cred(cred);
1104
1105 return error;
89dbf196
JJ
1106
1107kill:
1108 info = "failed token match";
1109 perms.kill = AA_MAY_CHANGEHAT;
1110
1111fail:
1112 fn_for_each_in_ns(label, profile,
1113 aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1114 AA_MAY_CHANGEHAT, NULL, NULL, target,
1115 GLOBAL_ROOT_UID, info, error));
1116
1117 goto out;
898127c3
JJ
1118}
1119
89dbf196 1120
e00b02bb
JJ
1121static int change_profile_perms_wrapper(const char *op, const char *name,
1122 struct aa_profile *profile,
1123 struct aa_label *target, bool stack,
1124 u32 request, struct aa_perms *perms)
1125{
1126 const char *info = NULL;
1127 int error = 0;
1128
1129 /*
1130 * Fail explicitly requested domain transitions when no_new_privs
1131 * and not unconfined OR the transition results in a stack on
1132 * the current label.
1133 * Stacking domain transitions and transitions from unconfined are
1134 * allowed even when no_new_privs is set because this aways results
1135 * in a reduction of permissions.
1136 */
1137 if (task_no_new_privs(current) && !stack &&
1138 !profile_unconfined(profile) &&
1139 !aa_label_is_subset(target, &profile->label)) {
1140 info = "no new privs";
1141 error = -EPERM;
1142 }
1143
1144 if (!error)
1145 error = change_profile_perms(profile, target, stack, request,
1146 profile->file.start, perms);
1147 if (error)
1148 error = aa_audit_file(profile, perms, op, request, name,
1149 NULL, target, GLOBAL_ROOT_UID, info,
1150 error);
1151
1152 return error;
1153}
89dbf196 1154
898127c3
JJ
1155/**
1156 * aa_change_profile - perform a one-way profile transition
aa9a39ad 1157 * @fqname: name of profile may include namespace (NOT NULL)
898127c3 1158 * @onexec: whether this transition is to take place immediately or at exec
df8073c6 1159 * @flags: flags affecting change behavior
898127c3
JJ
1160 *
1161 * Change to new profile @name. Unlike with hats, there is no way
1162 * to change back. If @name isn't specified the current profile name is
1163 * used.
1164 * If @onexec then the transition is delayed until
1165 * the next exec.
1166 *
1167 * Returns %0 on success, error otherwise.
1168 */
df8073c6 1169int aa_change_profile(const char *fqname, int flags)
898127c3 1170{
e00b02bb
JJ
1171 struct aa_label *label, *new = NULL, *target = NULL;
1172 struct aa_profile *profile;
2d679f3c 1173 struct aa_perms perms = {};
e00b02bb
JJ
1174 const char *info = NULL;
1175 const char *auditname = fqname; /* retain leading & if stack */
1176 bool stack = flags & AA_CHANGE_STACK;
47f6e5cc 1177 int error = 0;
e00b02bb 1178 char *op;
898127c3
JJ
1179 u32 request;
1180
aa9a39ad
JJ
1181 if (!fqname || !*fqname) {
1182 AA_DEBUG("no profile name");
898127c3 1183 return -EINVAL;
aa9a39ad 1184 }
898127c3 1185
df8073c6 1186 if (flags & AA_CHANGE_ONEXEC) {
898127c3 1187 request = AA_MAY_ONEXEC;
e00b02bb
JJ
1188 if (stack)
1189 op = OP_STACK_ONEXEC;
1190 else
1191 op = OP_CHANGE_ONEXEC;
898127c3
JJ
1192 } else {
1193 request = AA_MAY_CHANGE_PROFILE;
e00b02bb
JJ
1194 if (stack)
1195 op = OP_STACK;
1196 else
1197 op = OP_CHANGE_PROFILE;
898127c3
JJ
1198 }
1199
e00b02bb 1200 label = aa_get_current_label();
898127c3 1201
e00b02bb
JJ
1202 if (*fqname == '&') {
1203 stack = true;
1204 /* don't have label_parse() do stacking */
1205 fqname++;
c29bceb3 1206 }
e00b02bb
JJ
1207 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1208 if (IS_ERR(target)) {
1209 struct aa_profile *tprofile;
c29bceb3 1210
e00b02bb
JJ
1211 info = "label not found";
1212 error = PTR_ERR(target);
1213 target = NULL;
1214 /*
1215 * TODO: fixme using labels_profile is not right - do profile
1216 * per complain profile
1217 */
df8073c6 1218 if ((flags & AA_CHANGE_TEST) ||
e00b02bb 1219 !COMPLAIN_MODE(labels_profile(label)))
898127c3
JJ
1220 goto audit;
1221 /* released below */
e00b02bb
JJ
1222 tprofile = aa_new_null_profile(labels_profile(label), false,
1223 fqname, GFP_KERNEL);
1224 if (!tprofile) {
898127c3
JJ
1225 info = "failed null profile create";
1226 error = -ENOMEM;
1227 goto audit;
1228 }
e00b02bb
JJ
1229 target = &tprofile->label;
1230 goto check;
898127c3
JJ
1231 }
1232
e00b02bb
JJ
1233 /*
1234 * self directed transitions only apply to current policy ns
1235 * TODO: currently requiring perms for stacking and straight change
1236 * stacking doesn't strictly need this. Determine how much
1237 * we want to loosen this restriction for stacking
1238 *
1239 * if (!stack) {
1240 */
1241 error = fn_for_each_in_ns(label, profile,
1242 change_profile_perms_wrapper(op, auditname,
1243 profile, target, stack,
1244 request, &perms));
1245 if (error)
1246 /* auditing done in change_profile_perms_wrapper */
1247 goto out;
aa9a39ad 1248
e00b02bb
JJ
1249 /* } */
1250
1251check:
898127c3 1252 /* check if tracing task is allowed to trace target domain */
e00b02bb
JJ
1253 error = may_change_ptraced_domain(target, &info);
1254 if (error && !fn_for_each_in_ns(label, profile,
1255 COMPLAIN_MODE(profile)))
898127c3 1256 goto audit;
898127c3 1257
e00b02bb
JJ
1258 /* TODO: add permission check to allow this
1259 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1260 * info = "not a single threaded task";
1261 * error = -EACCES;
1262 * goto audit;
1263 * }
1264 */
df8073c6 1265 if (flags & AA_CHANGE_TEST)
e00b02bb 1266 goto out;
898127c3 1267
e00b02bb
JJ
1268 if (!(flags & AA_CHANGE_ONEXEC)) {
1269 /* only transition profiles in the current ns */
1270 if (stack)
1271 new = aa_label_merge(label, target, GFP_KERNEL);
1272 else
1273 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1274 aa_get_label(target),
1275 aa_get_label(&profile->label));
1276 if (IS_ERR_OR_NULL(new)) {
1277 info = "failed to build target label";
08b836c1
JJ
1278 if (!new)
1279 error = -ENOMEM;
1280 else
1281 error = PTR_ERR(new);
e00b02bb
JJ
1282 new = NULL;
1283 perms.allow = 0;
1284 goto audit;
1285 }
1286 error = aa_replace_current_label(new);
1287 } else
1288 /* full transition will be built in exec path */
1289 error = aa_set_current_onexec(target, stack);
898127c3
JJ
1290
1291audit:
e00b02bb
JJ
1292 error = fn_for_each_in_ns(label, profile,
1293 aa_audit_file(profile, &perms, op, request, auditname,
1294 NULL, new ? new : target,
1295 GLOBAL_ROOT_UID, info, error));
898127c3 1296
e00b02bb
JJ
1297out:
1298 aa_put_label(new);
1299 aa_put_label(target);
637f688d 1300 aa_put_label(label);
898127c3
JJ
1301
1302 return error;
1303}