blob: 9d1936519cfdbb94d647c3ac478f6fbdcd3de0c3 [file] [log] [blame]
John Johansen898127c2010-07-29 14:48:06 -07001/*
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"
John Johansend8889d42017-10-11 01:04:48 -070025#include "include/cred.h"
John Johansen898127c2010-07-29 14:48:06 -070026#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"
John Johansencff281f2017-01-16 00:42:15 -080032#include "include/policy_ns.h"
John Johansen898127c2010-07-29 14:48:06 -070033
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
John Johansenb2d09ae2017-06-09 14:22:14 -070054 * @to_label: profile to change to (NOT NULL)
55 * @info: message if there is an error
John Johansen898127c2010-07-29 14:48:06 -070056 *
Oleg Nesterov51775fe2013-10-08 05:46:03 -070057 * Check if current is ptraced and if so if the tracing task is allowed
John Johansen898127c2010-07-29 14:48:06 -070058 * to trace the new domain
59 *
60 * Returns: %0 or error if change not allowed
61 */
John Johansenb2d09ae2017-06-09 14:22:14 -070062static int may_change_ptraced_domain(struct aa_label *to_label,
63 const char **info)
John Johansen898127c2010-07-29 14:48:06 -070064{
65 struct task_struct *tracer;
John Johansen637f6882017-06-09 08:14:28 -070066 struct aa_label *tracerl = NULL;
John Johansen898127c2010-07-29 14:48:06 -070067 int error = 0;
68
69 rcu_read_lock();
Oleg Nesterov51775fe2013-10-08 05:46:03 -070070 tracer = ptrace_parent(current);
John Johansen3cfcc192013-02-18 16:03:34 -080071 if (tracer)
John Johansen898127c2010-07-29 14:48:06 -070072 /* released below */
John Johansen637f6882017-06-09 08:14:28 -070073 tracerl = aa_get_task_label(tracer);
John Johansen898127c2010-07-29 14:48:06 -070074
75 /* not ptraced */
John Johansen637f6882017-06-09 08:14:28 -070076 if (!tracer || unconfined(tracerl))
John Johansen898127c2010-07-29 14:48:06 -070077 goto out;
78
John Johansenb2d09ae2017-06-09 14:22:14 -070079 error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
John Johansen898127c2010-07-29 14:48:06 -070080
81out:
John Johansen04fdc092011-06-28 15:06:38 +010082 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -070083 aa_put_label(tracerl);
John Johansen898127c2010-07-29 14:48:06 -070084
John Johansenb2d09ae2017-06-09 14:22:14 -070085 if (error)
86 *info = "ptrace prevents transition";
John Johansen898127c2010-07-29 14:48:06 -070087 return error;
88}
89
John Johansen93c98a42017-06-09 16:55:04 -070090/**** 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
John Johansen898127c2010-07-29 14:48:06 -0700275/**
276 * change_profile_perms - find permissions for change_profile
277 * @profile: the current profile (NOT NULL)
John Johansen93c98a42017-06-09 16:55:04 -0700278 * @target: label to transition to (NOT NULL)
279 * @stack: whether this is a stacking request
John Johansen898127c2010-07-29 14:48:06 -0700280 * @request: requested perms
281 * @start: state to start matching in
282 *
John Johansen93c98a42017-06-09 16:55:04 -0700283 *
John Johansen898127c2010-07-29 14:48:06 -0700284 * Returns: permission set
John Johansen93c98a42017-06-09 16:55:04 -0700285 *
286 * currently only matches full label A//&B//&C or individual components A, B, C
287 * not arbitrary combinations. Eg. A//&B, C
John Johansen898127c2010-07-29 14:48:06 -0700288 */
John Johansen93c98a42017-06-09 16:55:04 -0700289static 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
John Johansen898127c2010-07-29 14:48:06 -0700304/**
305 * __attach_match_ - find an attachment match
306 * @name - to match against (NOT NULL)
307 * @head - profile list to walk (NOT NULL)
John Johansen844b8292017-11-17 17:42:42 -0800308 * @info - info message if there was an error (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700309 *
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,
John Johansen844b8292017-11-17 17:42:42 -0800320 struct list_head *head,
321 const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700322{
323 int len = 0;
John Johansen844b8292017-11-17 17:42:42 -0800324 bool conflict = false;
John Johansen898127c2010-07-29 14:48:06 -0700325 struct aa_profile *profile, *candidate = NULL;
326
John Johansen01e2b672013-07-10 21:06:43 -0700327 list_for_each_entry_rcu(profile, head, base.list) {
John Johansen06d426d2017-11-17 18:04:37 -0800328 if (profile->label.flags & FLAG_NULL &&
329 &profile->label == ns_unconfined(profile->ns))
John Johansen898127c2010-07-29 14:48:06 -0700330 continue;
John Johansen06d426d2017-11-17 18:04:37 -0800331
John Johansen844b8292017-11-17 17:42:42 -0800332 if (profile->xmatch) {
Matthew Garrett1a3881d2018-01-11 13:07:54 -0800333 if (profile->xmatch_len >= len) {
John Johansen844b8292017-11-17 17:42:42 -0800334 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) {
Matthew Garrett1a3881d2018-01-11 13:07:54 -0800342 if (profile->xmatch_len == len) {
343 conflict = true;
344 continue;
345 }
John Johansen844b8292017-11-17 17:42:42 -0800346 candidate = profile;
347 len = profile->xmatch_len;
348 conflict = false;
349 }
John Johansen898127c2010-07-29 14:48:06 -0700350 }
351 } else if (!strcmp(profile->base.name, name))
352 /* exact non-re match, no more searching required */
353 return profile;
354 }
355
John Johansen844b8292017-11-17 17:42:42 -0800356 if (conflict) {
357 *info = "conflicting profile attachments";
358 return NULL;
359 }
360
John Johansen898127c2010-07-29 14:48:06 -0700361 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)
John Johansen844b8292017-11-17 17:42:42 -0800369 * @info: info message if there was an error
John Johansen898127c2010-07-29 14:48:06 -0700370 *
John Johansen93c98a42017-06-09 16:55:04 -0700371 * Returns: label or NULL if no match found
John Johansen898127c2010-07-29 14:48:06 -0700372 */
John Johansen93c98a42017-06-09 16:55:04 -0700373static struct aa_label *find_attach(struct aa_ns *ns, struct list_head *list,
John Johansen844b8292017-11-17 17:42:42 -0800374 const char *name, const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700375{
376 struct aa_profile *profile;
377
John Johansen01e2b672013-07-10 21:06:43 -0700378 rcu_read_lock();
John Johansen844b8292017-11-17 17:42:42 -0800379 profile = aa_get_profile(__attach_match(name, list, info));
John Johansen01e2b672013-07-10 21:06:43 -0700380 rcu_read_unlock();
John Johansen898127c2010-07-29 14:48:06 -0700381
John Johansen93c98a42017-06-09 16:55:04 -0700382 return profile ? &profile->label : NULL;
John Johansen898127c2010-07-29 14:48:06 -0700383}
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
John Johansen93c98a42017-06-09 16:55:04 -0700394 * @name: returns: name tested to find label (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700395 *
John Johansen93c98a42017-06-09 16:55:04 -0700396 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
John Johansen898127c2010-07-29 14:48:06 -0700397 */
John Johansen2ea3ffb2017-07-18 23:04:47 -0700398struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
399 const char **name)
John Johansen898127c2010-07-29 14:48:06 -0700400{
John Johansen93c98a42017-06-09 16:55:04 -0700401 struct aa_label *label = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700402 u32 xtype = xindex & AA_X_TYPE_MASK;
403 int index = xindex & AA_X_INDEX_MASK;
John Johansen93c98a42017-06-09 16:55:04 -0700404
405 AA_BUG(!name);
John Johansen898127c2010-07-29 14:48:06 -0700406
407 /* index is guaranteed to be in range, validated at load time */
John Johansen93c98a42017-06-09 16:55:04 -0700408 /* 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)) {
John Johansen898127c2010-07-29 14:48:06 -0700413 if (xindex & AA_X_CHILD) {
John Johansen93c98a42017-06-09 16:55:04 -0700414 struct aa_profile *new_profile;
John Johansen898127c2010-07-29 14:48:06 -0700415 /* release by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700416 new_profile = aa_find_child(profile, *name);
417 if (new_profile)
418 label = &new_profile->label;
John Johansen898127c2010-07-29 14:48:06 -0700419 continue;
John Johansen898127c2010-07-29 14:48:06 -0700420 }
John Johansen93c98a42017-06-09 16:55:04 -0700421 label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
422 true, false);
423 if (IS_ERR(label))
424 label = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700425 }
426
427 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700428
429 return label;
John Johansen898127c2010-07-29 14:48:06 -0700430}
431
432/**
John Johansen93c98a42017-06-09 16:55:04 -0700433 * x_to_label - get target label for a given xindex
John Johansen898127c2010-07-29 14:48:06 -0700434 * @profile: current profile (NOT NULL)
435 * @name: name to lookup (NOT NULL)
436 * @xindex: index into x transition table
John Johansen93c98a42017-06-09 16:55:04 -0700437 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700438 *
John Johansen93c98a42017-06-09 16:55:04 -0700439 * find label for a transition index
John Johansen898127c2010-07-29 14:48:06 -0700440 *
John Johansen93c98a42017-06-09 16:55:04 -0700441 * Returns: refcounted label or NULL if not found available
John Johansen898127c2010-07-29 14:48:06 -0700442 */
John Johansen93c98a42017-06-09 16:55:04 -0700443static struct aa_label *x_to_label(struct aa_profile *profile,
444 const char *name, u32 xindex,
445 const char **lookupname,
446 const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700447{
John Johansen93c98a42017-06-09 16:55:04 -0700448 struct aa_label *new = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800449 struct aa_ns *ns = profile->ns;
John Johansen898127c2010-07-29 14:48:06 -0700450 u32 xtype = xindex & AA_X_TYPE_MASK;
John Johansen93c98a42017-06-09 16:55:04 -0700451 const char *stack = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700452
453 switch (xtype) {
454 case AA_X_NONE:
455 /* fail exec unless ix || ux fallback - handled by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700456 *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 */
John Johansen898127c2010-07-29 14:48:06 -0700468 case AA_X_NAME:
469 if (xindex & AA_X_CHILD)
470 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700471 new = find_attach(ns, &profile->base.profiles,
John Johansen844b8292017-11-17 17:42:42 -0800472 name, info);
John Johansen898127c2010-07-29 14:48:06 -0700473 else
474 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700475 new = find_attach(ns, &ns->base.profiles,
John Johansen844b8292017-11-17 17:42:42 -0800476 name, info);
John Johansen93c98a42017-06-09 16:55:04 -0700477 *lookupname = name;
John Johansen898127c2010-07-29 14:48:06 -0700478 break;
479 }
480
John Johansen93c98a42017-06-09 16:55:04 -0700481 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
John Johansen898127c2010-07-29 14:48:06 -0700505 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700506 return new;
507}
508
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)
513{
514 struct aa_label *new = NULL;
515 const char *info = NULL, *name = NULL, *target = NULL;
516 unsigned int state = profile->file.start;
517 struct aa_perms perms = {};
518 bool nonewprivs = false;
519 int error = 0;
520
521 AA_BUG(!profile);
522 AA_BUG(!bprm);
523 AA_BUG(!buffer);
524
525 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
526 &name, &info, profile->disconnected);
527 if (error) {
528 if (profile_unconfined(profile) ||
529 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
530 AA_DEBUG("name lookup ix on error");
531 error = 0;
532 new = aa_get_newest_label(&profile->label);
533 }
534 name = bprm->filename;
535 goto audit;
536 }
537
538 if (profile_unconfined(profile)) {
539 new = find_attach(profile->ns, &profile->ns->base.profiles,
John Johansen844b8292017-11-17 17:42:42 -0800540 name, &info);
John Johansen93c98a42017-06-09 16:55:04 -0700541 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);
547 }
548
549 /* find exec permissions for name */
550 state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
551 if (perms.allow & MAY_EXEC) {
552 /* exec permission determine how to transition */
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;
562 }
563 } else if (COMPLAIN_MODE(profile)) {
564 /* no exec permission - learning mode */
John Johansen5d7c44e2017-11-20 22:26:12 -0800565 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 }
John Johansen93c98a42017-06-09 16:55:04 -0700580 if (!new_profile) {
581 error = -ENOMEM;
582 info = "could not create null profile";
583 } else {
584 error = -EACCES;
585 new = &new_profile->label;
586 }
587 perms.xindex |= AA_X_UNSAFE;
588 } else
589 /* fail exec */
590 error = -EACCES;
591
592 if (!new)
593 goto audit;
594
John Johansen93c98a42017-06-09 16:55:04 -0700595
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;
604 }
605
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;
651 goto audit;
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 }
John Johansen93c98a42017-06-09 16:55:04 -0700671
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);
John Johansen898127c2010-07-29 14:48:06 -0700739}
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
John Johansen93c98a42017-06-09 16:55:04 -0700746 *
747 * TODO: once the other paths are done see if we can't refactor into a fn
John Johansen898127c2010-07-29 14:48:06 -0700748 */
749int apparmor_bprm_set_creds(struct linux_binprm *bprm)
750{
John Johansenf1752212017-01-27 04:09:40 -0800751 struct aa_task_ctx *ctx;
John Johansen93c98a42017-06-09 16:55:04 -0700752 struct aa_label *label, *new = NULL;
753 struct aa_profile *profile;
John Johansen898127c2010-07-29 14:48:06 -0700754 char *buffer = NULL;
John Johansen93c98a42017-06-09 16:55:04 -0700755 const char *info = NULL;
756 int error = 0;
757 bool unsafe = false;
John Johansen898127c2010-07-29 14:48:06 -0700758 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500759 file_inode(bprm->file)->i_uid,
760 file_inode(bprm->file)->i_mode
John Johansen898127c2010-07-29 14:48:06 -0700761 };
John Johansen898127c2010-07-29 14:48:06 -0700762
Kees Cookddb4a142017-07-18 15:25:23 -0700763 if (bprm->called_set_creds)
John Johansen898127c2010-07-29 14:48:06 -0700764 return 0;
765
John Johansende62de52017-10-08 00:43:02 -0700766 ctx = task_ctx(current);
John Johansend9087c42017-01-27 03:53:53 -0800767 AA_BUG(!cred_label(bprm->cred));
John Johansenf1752212017-01-27 04:09:40 -0800768 AA_BUG(!ctx);
John Johansen898127c2010-07-29 14:48:06 -0700769
John Johansend9087c42017-01-27 03:53:53 -0800770 label = aa_get_newest_label(cred_label(bprm->cred));
John Johansen4227c332017-05-23 03:25:14 -0700771
John Johansen9fcf78c2017-10-08 18:26:19 -0700772 /*
773 * Detect no new privs being set, and store the label it
774 * occurred under. Ideally this would happen when nnp
775 * is set but there isn't a good way to do that yet.
776 *
777 * Testing for unconfined must be done before the subset test
778 */
779 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
780 !ctx->nnp)
781 ctx->nnp = aa_get_label(label);
782
John Johansen4227c332017-05-23 03:25:14 -0700783 /* buffer freed below, name is pointer into buffer */
784 get_buffers(buffer);
John Johansen93c98a42017-06-09 16:55:04 -0700785 /* Test for onexec first as onexec override other x transitions. */
John Johansenf1752212017-01-27 04:09:40 -0800786 if (ctx->onexec)
787 new = handle_onexec(label, ctx->onexec, ctx->token,
John Johansen93c98a42017-06-09 16:55:04 -0700788 bprm, buffer, &cond, &unsafe);
789 else
790 new = fn_label_build(label, profile, GFP_ATOMIC,
791 profile_transition(profile, bprm, buffer,
792 &cond, &unsafe));
John Johansen898127c2010-07-29 14:48:06 -0700793
John Johansen93c98a42017-06-09 16:55:04 -0700794 AA_BUG(!new);
795 if (IS_ERR(new)) {
796 error = PTR_ERR(new);
797 goto done;
798 } else if (!new) {
799 error = -ENOMEM;
800 goto done;
John Johansen898127c2010-07-29 14:48:06 -0700801 }
802
John Johansen9fcf78c2017-10-08 18:26:19 -0700803 /* Policy has specified a domain transitions. If no_new_privs and
804 * confined ensure the transition is to confinement that is subset
805 * of the confinement when the task entered no new privs.
806 *
807 * NOTE: Domain transitions from unconfined and to stacked
808 * subsets are allowed even when no_new_privs is set because this
809 * aways results in a further reduction of permissions.
810 */
811 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
812 !unconfined(label) && !aa_label_is_subset(new, ctx->nnp)) {
813 error = -EPERM;
814 info = "no new privs";
815 goto audit;
816 }
John Johansen898127c2010-07-29 14:48:06 -0700817
818 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
819 /* FIXME: currently don't mediate shared state */
820 ;
821 }
822
John Johansen93c98a42017-06-09 16:55:04 -0700823 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
824 /* TODO: test needs to be profile of label to new */
825 error = may_change_ptraced_domain(new, &info);
John Johansenf7da2de2016-04-20 14:18:18 -0700826 if (error)
John Johansen898127c2010-07-29 14:48:06 -0700827 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700828 }
829
John Johansen93c98a42017-06-09 16:55:04 -0700830 if (unsafe) {
831 if (DEBUG_ON) {
832 dbg_printk("scrubbing environment variables for %s "
833 "label=", bprm->filename);
834 aa_label_printk(new, GFP_ATOMIC);
835 dbg_printk("\n");
836 }
Kees Cook993b3ab2017-07-18 15:25:24 -0700837 bprm->secureexec = 1;
John Johansen898127c2010-07-29 14:48:06 -0700838 }
John Johansen898127c2010-07-29 14:48:06 -0700839
John Johansen93c98a42017-06-09 16:55:04 -0700840 if (label->proxy != new->proxy) {
841 /* when transitioning clear unsafe personality bits */
842 if (DEBUG_ON) {
843 dbg_printk("apparmor: clearing unsafe personality "
844 "bits. %s label=", bprm->filename);
845 aa_label_printk(new, GFP_ATOMIC);
846 dbg_printk("\n");
847 }
848 bprm->per_clear |= PER_CLEAR_ON_SETID;
849 }
John Johansend9087c42017-01-27 03:53:53 -0800850 aa_put_label(cred_label(bprm->cred));
851 /* transfer reference, released when cred is freed */
852 cred_label(bprm->cred) = new;
John Johansen898127c2010-07-29 14:48:06 -0700853
John Johansen93c98a42017-06-09 16:55:04 -0700854done:
John Johansen637f6882017-06-09 08:14:28 -0700855 aa_put_label(label);
John Johansen4227c332017-05-23 03:25:14 -0700856 put_buffers(buffer);
John Johansen898127c2010-07-29 14:48:06 -0700857
858 return error;
John Johansen93c98a42017-06-09 16:55:04 -0700859
860audit:
861 error = fn_for_each(label, profile,
862 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
863 bprm->filename, NULL, new,
864 file_inode(bprm->file)->i_uid, info,
865 error));
866 aa_put_label(new);
867 goto done;
John Johansen898127c2010-07-29 14:48:06 -0700868}
869
John Johansen898127c2010-07-29 14:48:06 -0700870/*
871 * Functions for self directed profile change
872 */
873
John Johansen89dbf192017-06-09 17:01:43 -0700874
875/* helper fn for change_hat
John Johansen898127c2010-07-29 14:48:06 -0700876 *
John Johansen89dbf192017-06-09 17:01:43 -0700877 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
John Johansen898127c2010-07-29 14:48:06 -0700878 */
John Johansen89dbf192017-06-09 17:01:43 -0700879static struct aa_label *build_change_hat(struct aa_profile *profile,
880 const char *name, bool sibling)
John Johansen898127c2010-07-29 14:48:06 -0700881{
John Johansen89dbf192017-06-09 17:01:43 -0700882 struct aa_profile *root, *hat = NULL;
883 const char *info = NULL;
884 int error = 0;
885
886 if (sibling && PROFILE_IS_HAT(profile)) {
887 root = aa_get_profile_rcu(&profile->parent);
888 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
889 root = aa_get_profile(profile);
890 } else {
891 info = "conflicting target types";
892 error = -EPERM;
893 goto audit;
894 }
895
896 hat = aa_find_child(root, name);
897 if (!hat) {
898 error = -ENOENT;
899 if (COMPLAIN_MODE(profile)) {
900 hat = aa_new_null_profile(profile, true, name,
901 GFP_KERNEL);
902 if (!hat) {
903 info = "failed null profile create";
904 error = -ENOMEM;
905 }
906 }
907 }
908 aa_put_profile(root);
909
910audit:
911 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
912 name, hat ? hat->base.hname : NULL,
913 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, NULL,
914 error);
915 if (!hat || (error && error != -ENOENT))
916 return ERR_PTR(error);
917 /* if hat && error - complain mode, already audited and we adjust for
918 * complain mode allow by returning hat->label
919 */
920 return &hat->label;
921}
922
923/* helper fn for changing into a hat
924 *
925 * Returns: label for hat transition or ERR_PTR. Does not return NULL
926 */
927static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
928 int count, int flags)
929{
930 struct aa_profile *profile, *root, *hat = NULL;
931 struct aa_label *new;
932 struct label_it it;
933 bool sibling = false;
934 const char *name, *info = NULL;
935 int i, error;
936
937 AA_BUG(!label);
938 AA_BUG(!hats);
939 AA_BUG(count < 1);
940
941 if (PROFILE_IS_HAT(labels_profile(label)))
942 sibling = true;
943
944 /*find first matching hat */
945 for (i = 0; i < count && !hat; i++) {
946 name = hats[i];
947 label_for_each_in_ns(it, labels_ns(label), label, profile) {
948 if (sibling && PROFILE_IS_HAT(profile)) {
949 root = aa_get_profile_rcu(&profile->parent);
950 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
951 root = aa_get_profile(profile);
952 } else { /* conflicting change type */
953 info = "conflicting targets types";
954 error = -EPERM;
955 goto fail;
956 }
957 hat = aa_find_child(root, name);
958 aa_put_profile(root);
959 if (!hat) {
960 if (!COMPLAIN_MODE(profile))
961 goto outer_continue;
962 /* complain mode succeed as if hat */
963 } else if (!PROFILE_IS_HAT(hat)) {
964 info = "target not hat";
965 error = -EPERM;
966 aa_put_profile(hat);
967 goto fail;
968 }
969 aa_put_profile(hat);
970 }
971 /* found a hat for all profiles in ns */
972 goto build;
973outer_continue:
974 ;
975 }
976 /* no hats that match, find appropriate error
977 *
978 * In complain mode audit of the failure is based off of the first
979 * hat supplied. This is done due how userspace interacts with
980 * change_hat.
981 */
982 name = NULL;
983 label_for_each_in_ns(it, labels_ns(label), label, profile) {
984 if (!list_empty(&profile->base.profiles)) {
985 info = "hat not found";
986 error = -ENOENT;
987 goto fail;
988 }
989 }
990 info = "no hats defined";
991 error = -ECHILD;
992
993fail:
994 label_for_each_in_ns(it, labels_ns(label), label, profile) {
995 /*
996 * no target as it has failed to be found or built
997 *
998 * change_hat uses probing and should not log failures
999 * related to missing hats
1000 */
1001 /* TODO: get rid of GLOBAL_ROOT_UID */
1002 if (count > 1 || COMPLAIN_MODE(profile)) {
1003 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
1004 AA_MAY_CHANGEHAT, name, NULL, NULL,
1005 GLOBAL_ROOT_UID, info, error);
1006 }
1007 }
1008 return ERR_PTR(error);
1009
1010build:
1011 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1012 build_change_hat(profile, name, sibling),
1013 aa_get_label(&profile->label));
1014 if (!new) {
1015 info = "label build failed";
1016 error = -ENOMEM;
1017 goto fail;
1018 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1019
1020 return new;
John Johansen898127c2010-07-29 14:48:06 -07001021}
1022
1023/**
1024 * aa_change_hat - change hat to/from subprofile
1025 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1026 * @count: number of hat names in @hats
1027 * @token: magic value to validate the hat change
John Johansendf8073c2017-06-09 11:36:48 -07001028 * @flags: flags affecting behavior of the change
John Johansen898127c2010-07-29 14:48:06 -07001029 *
John Johansen89dbf192017-06-09 17:01:43 -07001030 * Returns %0 on success, error otherwise.
1031 *
John Johansen898127c2010-07-29 14:48:06 -07001032 * Change to the first profile specified in @hats that exists, and store
1033 * the @hat_magic in the current task context. If the count == 0 and the
1034 * @token matches that stored in the current task context, return to the
1035 * top level profile.
1036 *
John Johansen89dbf192017-06-09 17:01:43 -07001037 * change_hat only applies to profiles in the current ns, and each profile
1038 * in the ns must make the same transition otherwise change_hat will fail.
John Johansen898127c2010-07-29 14:48:06 -07001039 */
John Johansendf8073c2017-06-09 11:36:48 -07001040int aa_change_hat(const char *hats[], int count, u64 token, int flags)
John Johansen898127c2010-07-29 14:48:06 -07001041{
1042 const struct cred *cred;
John Johansen9fcf78c2017-10-08 18:26:19 -07001043 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen89dbf192017-06-09 17:01:43 -07001044 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1045 struct aa_profile *profile;
John Johansen2d679f32017-05-29 12:19:39 -07001046 struct aa_perms perms = {};
John Johansen89dbf192017-06-09 17:01:43 -07001047 const char *info = NULL;
John Johansen898127c2010-07-29 14:48:06 -07001048 int error = 0;
1049
1050 /* released below */
1051 cred = get_current_cred();
John Johansen637f6882017-06-09 08:14:28 -07001052 label = aa_get_newest_cred_label(cred);
John Johansenf1752212017-01-27 04:09:40 -08001053 previous = aa_get_newest_label(ctx->previous);
John Johansen898127c2010-07-29 14:48:06 -07001054
John Johansen9fcf78c2017-10-08 18:26:19 -07001055 /*
1056 * Detect no new privs being set, and store the label it
1057 * occurred under. Ideally this would happen when nnp
1058 * is set but there isn't a good way to do that yet.
1059 *
1060 * Testing for unconfined must be done before the subset test
1061 */
1062 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1063 ctx->nnp = aa_get_label(label);
1064
John Johansen637f6882017-06-09 08:14:28 -07001065 if (unconfined(label)) {
John Johansen89dbf192017-06-09 17:01:43 -07001066 info = "unconfined can not change_hat";
John Johansen898127c2010-07-29 14:48:06 -07001067 error = -EPERM;
John Johansen89dbf192017-06-09 17:01:43 -07001068 goto fail;
John Johansen898127c2010-07-29 14:48:06 -07001069 }
1070
1071 if (count) {
John Johansen89dbf192017-06-09 17:01:43 -07001072 new = change_hat(label, hats, count, flags);
1073 AA_BUG(!new);
1074 if (IS_ERR(new)) {
1075 error = PTR_ERR(new);
1076 new = NULL;
1077 /* already audited */
1078 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001079 }
1080
John Johansen89dbf192017-06-09 17:01:43 -07001081 error = may_change_ptraced_domain(new, &info);
1082 if (error)
1083 goto fail;
John Johansen898127c2010-07-29 14:48:06 -07001084
John Johansen9fcf78c2017-10-08 18:26:19 -07001085 /*
1086 * no new privs prevents domain transitions that would
1087 * reduce restrictions.
1088 */
1089 if (task_no_new_privs(current) && !unconfined(label) &&
1090 !aa_label_is_subset(new, ctx->nnp)) {
1091 /* not an apparmor denial per se, so don't log it */
1092 AA_DEBUG("no_new_privs - change_hat denied");
1093 error = -EPERM;
1094 goto out;
1095 }
1096
John Johansen89dbf192017-06-09 17:01:43 -07001097 if (flags & AA_CHANGE_TEST)
1098 goto out;
1099
1100 target = new;
1101 error = aa_set_current_hat(new, token);
1102 if (error == -EACCES)
1103 /* kill task in case of brute force attacks */
1104 goto kill;
1105 } else if (previous && !(flags & AA_CHANGE_TEST)) {
John Johansen9fcf78c2017-10-08 18:26:19 -07001106 /*
1107 * no new privs prevents domain transitions that would
1108 * reduce restrictions.
1109 */
1110 if (task_no_new_privs(current) && !unconfined(label) &&
1111 !aa_label_is_subset(previous, ctx->nnp)) {
1112 /* not an apparmor denial per se, so don't log it */
1113 AA_DEBUG("no_new_privs - change_hat denied");
1114 error = -EPERM;
1115 goto out;
1116 }
1117
John Johansen89dbf192017-06-09 17:01:43 -07001118 /* Return to saved label. Kill task if restore fails
John Johansen898127c2010-07-29 14:48:06 -07001119 * to avoid brute force attacks
1120 */
John Johansen89dbf192017-06-09 17:01:43 -07001121 target = previous;
John Johansen637f6882017-06-09 08:14:28 -07001122 error = aa_restore_previous_label(token);
John Johansen89dbf192017-06-09 17:01:43 -07001123 if (error) {
1124 if (error == -EACCES)
1125 goto kill;
1126 goto fail;
1127 }
1128 } /* else ignore @flags && restores when there is no saved profile */
John Johansen898127c2010-07-29 14:48:06 -07001129
1130out:
John Johansen89dbf192017-06-09 17:01:43 -07001131 aa_put_label(new);
1132 aa_put_label(previous);
John Johansen637f6882017-06-09 08:14:28 -07001133 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -07001134 put_cred(cred);
1135
1136 return error;
John Johansen89dbf192017-06-09 17:01:43 -07001137
1138kill:
1139 info = "failed token match";
1140 perms.kill = AA_MAY_CHANGEHAT;
1141
1142fail:
1143 fn_for_each_in_ns(label, profile,
1144 aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1145 AA_MAY_CHANGEHAT, NULL, NULL, target,
1146 GLOBAL_ROOT_UID, info, error));
1147
1148 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001149}
1150
John Johansen89dbf192017-06-09 17:01:43 -07001151
John Johansene00b02bb2017-06-09 17:07:58 -07001152static int change_profile_perms_wrapper(const char *op, const char *name,
1153 struct aa_profile *profile,
1154 struct aa_label *target, bool stack,
1155 u32 request, struct aa_perms *perms)
1156{
1157 const char *info = NULL;
1158 int error = 0;
1159
John Johansene00b02bb2017-06-09 17:07:58 -07001160 if (!error)
1161 error = change_profile_perms(profile, target, stack, request,
1162 profile->file.start, perms);
1163 if (error)
1164 error = aa_audit_file(profile, perms, op, request, name,
1165 NULL, target, GLOBAL_ROOT_UID, info,
1166 error);
1167
1168 return error;
1169}
John Johansen89dbf192017-06-09 17:01:43 -07001170
John Johansen898127c2010-07-29 14:48:06 -07001171/**
1172 * aa_change_profile - perform a one-way profile transition
John Johansenaa9a39a2017-01-16 00:43:06 -08001173 * @fqname: name of profile may include namespace (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -07001174 * @onexec: whether this transition is to take place immediately or at exec
John Johansendf8073c2017-06-09 11:36:48 -07001175 * @flags: flags affecting change behavior
John Johansen898127c2010-07-29 14:48:06 -07001176 *
1177 * Change to new profile @name. Unlike with hats, there is no way
1178 * to change back. If @name isn't specified the current profile name is
1179 * used.
1180 * If @onexec then the transition is delayed until
1181 * the next exec.
1182 *
1183 * Returns %0 on success, error otherwise.
1184 */
John Johansendf8073c2017-06-09 11:36:48 -07001185int aa_change_profile(const char *fqname, int flags)
John Johansen898127c2010-07-29 14:48:06 -07001186{
John Johansene00b02bb2017-06-09 17:07:58 -07001187 struct aa_label *label, *new = NULL, *target = NULL;
1188 struct aa_profile *profile;
John Johansen2d679f32017-05-29 12:19:39 -07001189 struct aa_perms perms = {};
John Johansene00b02bb2017-06-09 17:07:58 -07001190 const char *info = NULL;
1191 const char *auditname = fqname; /* retain leading & if stack */
1192 bool stack = flags & AA_CHANGE_STACK;
John Johansen9fcf78c2017-10-08 18:26:19 -07001193 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen47f6e5c2017-01-16 00:43:01 -08001194 int error = 0;
John Johansene00b02bb2017-06-09 17:07:58 -07001195 char *op;
John Johansen898127c2010-07-29 14:48:06 -07001196 u32 request;
1197
John Johansen9fcf78c2017-10-08 18:26:19 -07001198 label = aa_get_current_label();
1199
1200 /*
1201 * Detect no new privs being set, and store the label it
1202 * occurred under. Ideally this would happen when nnp
1203 * is set but there isn't a good way to do that yet.
1204 *
1205 * Testing for unconfined must be done before the subset test
1206 */
1207 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1208 ctx->nnp = aa_get_label(label);
1209
John Johansenaa9a39a2017-01-16 00:43:06 -08001210 if (!fqname || !*fqname) {
1211 AA_DEBUG("no profile name");
John Johansen898127c2010-07-29 14:48:06 -07001212 return -EINVAL;
John Johansenaa9a39a2017-01-16 00:43:06 -08001213 }
John Johansen898127c2010-07-29 14:48:06 -07001214
John Johansendf8073c2017-06-09 11:36:48 -07001215 if (flags & AA_CHANGE_ONEXEC) {
John Johansen898127c2010-07-29 14:48:06 -07001216 request = AA_MAY_ONEXEC;
John Johansene00b02bb2017-06-09 17:07:58 -07001217 if (stack)
1218 op = OP_STACK_ONEXEC;
1219 else
1220 op = OP_CHANGE_ONEXEC;
John Johansen898127c2010-07-29 14:48:06 -07001221 } else {
1222 request = AA_MAY_CHANGE_PROFILE;
John Johansene00b02bb2017-06-09 17:07:58 -07001223 if (stack)
1224 op = OP_STACK;
1225 else
1226 op = OP_CHANGE_PROFILE;
John Johansen898127c2010-07-29 14:48:06 -07001227 }
1228
John Johansene00b02bb2017-06-09 17:07:58 -07001229 label = aa_get_current_label();
John Johansen898127c2010-07-29 14:48:06 -07001230
John Johansene00b02bb2017-06-09 17:07:58 -07001231 if (*fqname == '&') {
1232 stack = true;
1233 /* don't have label_parse() do stacking */
1234 fqname++;
John Johansenc29bceb2012-04-12 16:47:51 -05001235 }
John Johansene00b02bb2017-06-09 17:07:58 -07001236 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1237 if (IS_ERR(target)) {
1238 struct aa_profile *tprofile;
John Johansenc29bceb2012-04-12 16:47:51 -05001239
John Johansene00b02bb2017-06-09 17:07:58 -07001240 info = "label not found";
1241 error = PTR_ERR(target);
1242 target = NULL;
1243 /*
1244 * TODO: fixme using labels_profile is not right - do profile
1245 * per complain profile
1246 */
John Johansendf8073c2017-06-09 11:36:48 -07001247 if ((flags & AA_CHANGE_TEST) ||
John Johansene00b02bb2017-06-09 17:07:58 -07001248 !COMPLAIN_MODE(labels_profile(label)))
John Johansen898127c2010-07-29 14:48:06 -07001249 goto audit;
1250 /* released below */
John Johansene00b02bb2017-06-09 17:07:58 -07001251 tprofile = aa_new_null_profile(labels_profile(label), false,
1252 fqname, GFP_KERNEL);
1253 if (!tprofile) {
John Johansen898127c2010-07-29 14:48:06 -07001254 info = "failed null profile create";
1255 error = -ENOMEM;
1256 goto audit;
1257 }
John Johansene00b02bb2017-06-09 17:07:58 -07001258 target = &tprofile->label;
1259 goto check;
John Johansen898127c2010-07-29 14:48:06 -07001260 }
1261
John Johansene00b02bb2017-06-09 17:07:58 -07001262 /*
1263 * self directed transitions only apply to current policy ns
1264 * TODO: currently requiring perms for stacking and straight change
1265 * stacking doesn't strictly need this. Determine how much
1266 * we want to loosen this restriction for stacking
1267 *
1268 * if (!stack) {
1269 */
1270 error = fn_for_each_in_ns(label, profile,
1271 change_profile_perms_wrapper(op, auditname,
1272 profile, target, stack,
1273 request, &perms));
1274 if (error)
1275 /* auditing done in change_profile_perms_wrapper */
1276 goto out;
John Johansenaa9a39a2017-01-16 00:43:06 -08001277
John Johansene00b02bb2017-06-09 17:07:58 -07001278 /* } */
1279
1280check:
John Johansen898127c2010-07-29 14:48:06 -07001281 /* check if tracing task is allowed to trace target domain */
John Johansene00b02bb2017-06-09 17:07:58 -07001282 error = may_change_ptraced_domain(target, &info);
1283 if (error && !fn_for_each_in_ns(label, profile,
1284 COMPLAIN_MODE(profile)))
John Johansen898127c2010-07-29 14:48:06 -07001285 goto audit;
John Johansen898127c2010-07-29 14:48:06 -07001286
John Johansene00b02bb2017-06-09 17:07:58 -07001287 /* TODO: add permission check to allow this
1288 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1289 * info = "not a single threaded task";
1290 * error = -EACCES;
1291 * goto audit;
1292 * }
1293 */
John Johansendf8073c2017-06-09 11:36:48 -07001294 if (flags & AA_CHANGE_TEST)
John Johansene00b02bb2017-06-09 17:07:58 -07001295 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001296
John Johansen9fcf78c2017-10-08 18:26:19 -07001297 /* stacking is always a subset, so only check the nonstack case */
1298 if (!stack) {
1299 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1300 aa_get_label(target),
1301 aa_get_label(&profile->label));
1302 /*
1303 * no new privs prevents domain transitions that would
1304 * reduce restrictions.
1305 */
1306 if (task_no_new_privs(current) && !unconfined(label) &&
1307 !aa_label_is_subset(new, ctx->nnp)) {
1308 /* not an apparmor denial per se, so don't log it */
1309 AA_DEBUG("no_new_privs - change_hat denied");
1310 error = -EPERM;
1311 goto out;
1312 }
1313 }
1314
John Johansene00b02bb2017-06-09 17:07:58 -07001315 if (!(flags & AA_CHANGE_ONEXEC)) {
1316 /* only transition profiles in the current ns */
1317 if (stack)
1318 new = aa_label_merge(label, target, GFP_KERNEL);
John Johansene00b02bb2017-06-09 17:07:58 -07001319 if (IS_ERR_OR_NULL(new)) {
1320 info = "failed to build target label";
1321 error = PTR_ERR(new);
1322 new = NULL;
1323 perms.allow = 0;
1324 goto audit;
1325 }
1326 error = aa_replace_current_label(new);
John Johansen9fcf78c2017-10-08 18:26:19 -07001327 } else {
1328 if (new) {
1329 aa_put_label(new);
1330 new = NULL;
1331 }
1332
John Johansene00b02bb2017-06-09 17:07:58 -07001333 /* full transition will be built in exec path */
1334 error = aa_set_current_onexec(target, stack);
John Johansen9fcf78c2017-10-08 18:26:19 -07001335 }
John Johansen898127c2010-07-29 14:48:06 -07001336
1337audit:
John Johansene00b02bb2017-06-09 17:07:58 -07001338 error = fn_for_each_in_ns(label, profile,
1339 aa_audit_file(profile, &perms, op, request, auditname,
1340 NULL, new ? new : target,
1341 GLOBAL_ROOT_UID, info, error));
John Johansen898127c2010-07-29 14:48:06 -07001342
John Johansene00b02bb2017-06-09 17:07:58 -07001343out:
1344 aa_put_label(new);
1345 aa_put_label(target);
John Johansen637f6882017-06-09 08:14:28 -07001346 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -07001347
1348 return error;
1349}