blob: a1d73033b42e2dae24d622ea273a6ea1bf8f27b7 [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"
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"
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
304static struct aa_perms change_profile_perms_wrapper(struct aa_profile *profile,
305 struct aa_profile *target,
306 u32 request,
307 unsigned int start)
John Johansen898127c2010-07-29 14:48:06 -0700308{
John Johansen2d679f32017-05-29 12:19:39 -0700309 struct aa_perms perms;
John Johansen898127c2010-07-29 14:48:06 -0700310
John Johansen637f6882017-06-09 08:14:28 -0700311 if (profile_unconfined(profile)) {
John Johansen898127c2010-07-29 14:48:06 -0700312 perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
313 perms.audit = perms.quiet = perms.kill = 0;
314 return perms;
John Johansen898127c2010-07-29 14:48:06 -0700315 }
316
John Johansen93c98a42017-06-09 16:55:04 -0700317 if (change_profile_perms(profile, &target->label, false, request,
318 start, &perms))
319 return nullperms;
John Johansen898127c2010-07-29 14:48:06 -0700320
321 return perms;
322}
323
324/**
325 * __attach_match_ - find an attachment match
326 * @name - to match against (NOT NULL)
327 * @head - profile list to walk (NOT NULL)
328 *
329 * Do a linear search on the profiles in the list. There is a matching
330 * preference where an exact match is preferred over a name which uses
331 * expressions to match, and matching expressions with the greatest
332 * xmatch_len are preferred.
333 *
334 * Requires: @head not be shared or have appropriate locks held
335 *
336 * Returns: profile or NULL if no match found
337 */
338static struct aa_profile *__attach_match(const char *name,
339 struct list_head *head)
340{
341 int len = 0;
342 struct aa_profile *profile, *candidate = NULL;
343
John Johansen01e2b672013-07-10 21:06:43 -0700344 list_for_each_entry_rcu(profile, head, base.list) {
John Johansen637f6882017-06-09 08:14:28 -0700345 if (profile->label.flags & FLAG_NULL)
John Johansen898127c2010-07-29 14:48:06 -0700346 continue;
347 if (profile->xmatch && profile->xmatch_len > len) {
348 unsigned int state = aa_dfa_match(profile->xmatch,
349 DFA_START, name);
350 u32 perm = dfa_user_allow(profile->xmatch, state);
351 /* any accepting state means a valid match. */
352 if (perm & MAY_EXEC) {
353 candidate = profile;
354 len = profile->xmatch_len;
355 }
356 } else if (!strcmp(profile->base.name, name))
357 /* exact non-re match, no more searching required */
358 return profile;
359 }
360
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)
369 *
John Johansen93c98a42017-06-09 16:55:04 -0700370 * Returns: label or NULL if no match found
John Johansen898127c2010-07-29 14:48:06 -0700371 */
John Johansen93c98a42017-06-09 16:55:04 -0700372static struct aa_label *find_attach(struct aa_ns *ns, struct list_head *list,
373 const char *name)
John Johansen898127c2010-07-29 14:48:06 -0700374{
375 struct aa_profile *profile;
376
John Johansen01e2b672013-07-10 21:06:43 -0700377 rcu_read_lock();
John Johansen898127c2010-07-29 14:48:06 -0700378 profile = aa_get_profile(__attach_match(name, list));
John Johansen01e2b672013-07-10 21:06:43 -0700379 rcu_read_unlock();
John Johansen898127c2010-07-29 14:48:06 -0700380
John Johansen93c98a42017-06-09 16:55:04 -0700381 return profile ? &profile->label : NULL;
John Johansen898127c2010-07-29 14:48:06 -0700382}
383
384static const char *next_name(int xtype, const char *name)
385{
386 return NULL;
387}
388
389/**
390 * x_table_lookup - lookup an x transition name via transition table
391 * @profile: current profile (NOT NULL)
392 * @xindex: index into x transition table
John Johansen93c98a42017-06-09 16:55:04 -0700393 * @name: returns: name tested to find label (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700394 *
John Johansen93c98a42017-06-09 16:55:04 -0700395 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
John Johansen898127c2010-07-29 14:48:06 -0700396 */
John Johansen93c98a42017-06-09 16:55:04 -0700397static struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
398 const char **name)
John Johansen898127c2010-07-29 14:48:06 -0700399{
John Johansen93c98a42017-06-09 16:55:04 -0700400 struct aa_label *label = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700401 u32 xtype = xindex & AA_X_TYPE_MASK;
402 int index = xindex & AA_X_INDEX_MASK;
John Johansen93c98a42017-06-09 16:55:04 -0700403
404 AA_BUG(!name);
John Johansen898127c2010-07-29 14:48:06 -0700405
406 /* index is guaranteed to be in range, validated at load time */
John Johansen93c98a42017-06-09 16:55:04 -0700407 /* TODO: move lookup parsing to unpack time so this is a straight
408 * index into the resultant label
409 */
410 for (*name = profile->file.trans.table[index]; !label && *name;
411 *name = next_name(xtype, *name)) {
John Johansen898127c2010-07-29 14:48:06 -0700412 if (xindex & AA_X_CHILD) {
John Johansen93c98a42017-06-09 16:55:04 -0700413 struct aa_profile *new_profile;
John Johansen898127c2010-07-29 14:48:06 -0700414 /* release by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700415 new_profile = aa_find_child(profile, *name);
416 if (new_profile)
417 label = &new_profile->label;
John Johansen898127c2010-07-29 14:48:06 -0700418 continue;
John Johansen898127c2010-07-29 14:48:06 -0700419 }
John Johansen93c98a42017-06-09 16:55:04 -0700420 label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
421 true, false);
422 if (IS_ERR(label))
423 label = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700424 }
425
426 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700427
428 return label;
John Johansen898127c2010-07-29 14:48:06 -0700429}
430
431/**
John Johansen93c98a42017-06-09 16:55:04 -0700432 * x_to_label - get target label for a given xindex
John Johansen898127c2010-07-29 14:48:06 -0700433 * @profile: current profile (NOT NULL)
434 * @name: name to lookup (NOT NULL)
435 * @xindex: index into x transition table
John Johansen93c98a42017-06-09 16:55:04 -0700436 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700437 *
John Johansen93c98a42017-06-09 16:55:04 -0700438 * find label for a transition index
John Johansen898127c2010-07-29 14:48:06 -0700439 *
John Johansen93c98a42017-06-09 16:55:04 -0700440 * Returns: refcounted label or NULL if not found available
John Johansen898127c2010-07-29 14:48:06 -0700441 */
John Johansen93c98a42017-06-09 16:55:04 -0700442static struct aa_label *x_to_label(struct aa_profile *profile,
443 const char *name, u32 xindex,
444 const char **lookupname,
445 const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700446{
John Johansen93c98a42017-06-09 16:55:04 -0700447 struct aa_label *new = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800448 struct aa_ns *ns = profile->ns;
John Johansen898127c2010-07-29 14:48:06 -0700449 u32 xtype = xindex & AA_X_TYPE_MASK;
John Johansen93c98a42017-06-09 16:55:04 -0700450 const char *stack = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700451
452 switch (xtype) {
453 case AA_X_NONE:
454 /* fail exec unless ix || ux fallback - handled by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700455 *lookupname = NULL;
456 break;
457 case AA_X_TABLE:
458 /* TODO: fix when perm mapping done at unload */
459 stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
460 if (*stack != '&') {
461 /* released by caller */
462 new = x_table_lookup(profile, xindex, lookupname);
463 stack = NULL;
464 break;
465 }
466 /* fall through to X_NAME */
John Johansen898127c2010-07-29 14:48:06 -0700467 case AA_X_NAME:
468 if (xindex & AA_X_CHILD)
469 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700470 new = find_attach(ns, &profile->base.profiles,
471 name);
John Johansen898127c2010-07-29 14:48:06 -0700472 else
473 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700474 new = find_attach(ns, &ns->base.profiles,
475 name);
476 *lookupname = name;
John Johansen898127c2010-07-29 14:48:06 -0700477 break;
478 }
479
John Johansen93c98a42017-06-09 16:55:04 -0700480 if (!new) {
481 if (xindex & AA_X_INHERIT) {
482 /* (p|c|n)ix - don't change profile but do
483 * use the newest version
484 */
485 *info = "ix fallback";
486 /* no profile && no error */
487 new = aa_get_newest_label(&profile->label);
488 } else if (xindex & AA_X_UNCONFINED) {
489 new = aa_get_newest_label(ns_unconfined(profile->ns));
490 *info = "ux fallback";
491 }
492 }
493
494 if (new && stack) {
495 /* base the stack on post domain transition */
496 struct aa_label *base = new;
497
498 new = aa_label_parse(base, stack, GFP_ATOMIC, true, false);
499 if (IS_ERR(new))
500 new = NULL;
501 aa_put_label(base);
502 }
503
John Johansen898127c2010-07-29 14:48:06 -0700504 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700505 return new;
506}
507
508static struct aa_label *profile_transition(struct aa_profile *profile,
509 const struct linux_binprm *bprm,
510 char *buffer, struct path_cond *cond,
511 bool *secure_exec)
512{
513 struct aa_label *new = NULL;
514 const char *info = NULL, *name = NULL, *target = NULL;
515 unsigned int state = profile->file.start;
516 struct aa_perms perms = {};
517 bool nonewprivs = false;
518 int error = 0;
519
520 AA_BUG(!profile);
521 AA_BUG(!bprm);
522 AA_BUG(!buffer);
523
524 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
525 &name, &info, profile->disconnected);
526 if (error) {
527 if (profile_unconfined(profile) ||
528 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
529 AA_DEBUG("name lookup ix on error");
530 error = 0;
531 new = aa_get_newest_label(&profile->label);
532 }
533 name = bprm->filename;
534 goto audit;
535 }
536
537 if (profile_unconfined(profile)) {
538 new = find_attach(profile->ns, &profile->ns->base.profiles,
539 name);
540 if (new) {
541 AA_DEBUG("unconfined attached to new label");
542 return new;
543 }
544 AA_DEBUG("unconfined exec no attachment");
545 return aa_get_newest_label(&profile->label);
546 }
547
548 /* find exec permissions for name */
549 state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
550 if (perms.allow & MAY_EXEC) {
551 /* exec permission determine how to transition */
552 new = x_to_label(profile, name, perms.xindex, &target, &info);
553 if (new && new->proxy == profile->label.proxy && info) {
554 /* hack ix fallback - improve how this is detected */
555 goto audit;
556 } else if (!new) {
557 error = -EACCES;
558 info = "profile transition not found";
559 /* remove MAY_EXEC to audit as failure */
560 perms.allow &= ~MAY_EXEC;
561 }
562 } else if (COMPLAIN_MODE(profile)) {
563 /* no exec permission - learning mode */
564 struct aa_profile *new_profile = aa_new_null_profile(profile,
565 false, name,
566 GFP_ATOMIC);
567 if (!new_profile) {
568 error = -ENOMEM;
569 info = "could not create null profile";
570 } else {
571 error = -EACCES;
572 new = &new_profile->label;
573 }
574 perms.xindex |= AA_X_UNSAFE;
575 } else
576 /* fail exec */
577 error = -EACCES;
578
579 if (!new)
580 goto audit;
581
582 /* Policy has specified a domain transitions. if no_new_privs and
583 * confined and not transitioning to the current domain fail.
584 *
585 * NOTE: Domain transitions from unconfined and to stritly stacked
586 * subsets are allowed even when no_new_privs is set because this
587 * aways results in a further reduction of permissions.
588 */
589 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
590 !profile_unconfined(profile) &&
591 !aa_label_is_subset(new, &profile->label)) {
592 error = -EPERM;
593 info = "no new privs";
594 nonewprivs = true;
595 perms.allow &= ~MAY_EXEC;
596 goto audit;
597 }
598
599 if (!(perms.xindex & AA_X_UNSAFE)) {
600 if (DEBUG_ON) {
601 dbg_printk("apparmor: scrubbing environment variables"
602 " for %s profile=", name);
603 aa_label_printk(new, GFP_ATOMIC);
604 dbg_printk("\n");
605 }
606 *secure_exec = true;
607 }
608
609audit:
610 aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
611 cond->uid, info, error);
612 if (!new || nonewprivs) {
613 aa_put_label(new);
614 return ERR_PTR(error);
615 }
616
617 return new;
618}
619
620static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
621 bool stack, const struct linux_binprm *bprm,
622 char *buffer, struct path_cond *cond,
623 bool *secure_exec)
624{
625 unsigned int state = profile->file.start;
626 struct aa_perms perms = {};
627 const char *xname = NULL, *info = "change_profile onexec";
628 int error = -EACCES;
629
630 AA_BUG(!profile);
631 AA_BUG(!onexec);
632 AA_BUG(!bprm);
633 AA_BUG(!buffer);
634
635 if (profile_unconfined(profile)) {
636 /* change_profile on exec already granted */
637 /*
638 * NOTE: Domain transitions from unconfined are allowed
639 * even when no_new_privs is set because this aways results
640 * in a further reduction of permissions.
641 */
642 return 0;
643 }
644
645 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
646 &xname, &info, profile->disconnected);
647 if (error) {
648 if (profile_unconfined(profile) ||
649 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
650 AA_DEBUG("name lookup ix on error");
651 error = 0;
652 }
653 xname = bprm->filename;
654 goto audit;
655 }
656
657 /* find exec permissions for name */
658 state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
659 if (!(perms.allow & AA_MAY_ONEXEC)) {
660 info = "no change_onexec valid for executable";
661 goto audit;
662 }
663 /* test if this exec can be paired with change_profile onexec.
664 * onexec permission is linked to exec with a standard pairing
665 * exec\0change_profile
666 */
667 state = aa_dfa_null_transition(profile->file.dfa, state);
668 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
669 state, &perms);
670 if (error) {
671 perms.allow &= ~AA_MAY_ONEXEC;
672 goto audit;
673 }
674 /* Policy has specified a domain transitions. if no_new_privs and
675 * confined and not transitioning to the current domain fail.
676 *
677 * NOTE: Domain transitions from unconfined and to stritly stacked
678 * subsets are allowed even when no_new_privs is set because this
679 * aways results in a further reduction of permissions.
680 */
681 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
682 !profile_unconfined(profile) &&
683 !aa_label_is_subset(onexec, &profile->label)) {
684 error = -EPERM;
685 info = "no new privs";
686 perms.allow &= ~AA_MAY_ONEXEC;
687 goto audit;
688 }
689
690 if (!(perms.xindex & AA_X_UNSAFE)) {
691 if (DEBUG_ON) {
692 dbg_printk("apparmor: scrubbing environment "
693 "variables for %s label=", xname);
694 aa_label_printk(onexec, GFP_ATOMIC);
695 dbg_printk("\n");
696 }
697 *secure_exec = true;
698 }
699
700audit:
701 return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
702 NULL, onexec, cond->uid, info, error);
703}
704
705/* ensure none ns domain transitions are correctly applied with onexec */
706
707static struct aa_label *handle_onexec(struct aa_label *label,
708 struct aa_label *onexec, bool stack,
709 const struct linux_binprm *bprm,
710 char *buffer, struct path_cond *cond,
711 bool *unsafe)
712{
713 struct aa_profile *profile;
714 struct aa_label *new;
715 int error;
716
717 AA_BUG(!label);
718 AA_BUG(!onexec);
719 AA_BUG(!bprm);
720 AA_BUG(!buffer);
721
722 if (!stack) {
723 error = fn_for_each_in_ns(label, profile,
724 profile_onexec(profile, onexec, stack,
725 bprm, buffer, cond, unsafe));
726 if (error)
727 return ERR_PTR(error);
728 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
729 aa_get_newest_label(onexec),
730 profile_transition(profile, bprm, buffer,
731 cond, unsafe));
732
733 } else {
734 /* TODO: determine how much we want to losen this */
735 error = fn_for_each_in_ns(label, profile,
736 profile_onexec(profile, onexec, stack, bprm,
737 buffer, cond, unsafe));
738 if (error)
739 return ERR_PTR(error);
740 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
741 aa_label_merge(&profile->label, onexec,
742 GFP_ATOMIC),
743 profile_transition(profile, bprm, buffer,
744 cond, unsafe));
745 }
746
747 if (new)
748 return new;
749
750 /* TODO: get rid of GLOBAL_ROOT_UID */
751 error = fn_for_each_in_ns(label, profile,
752 aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
753 AA_MAY_ONEXEC, bprm->filename, NULL,
754 onexec, GLOBAL_ROOT_UID,
755 "failed to build target label", -ENOMEM));
756 return ERR_PTR(error);
John Johansen898127c2010-07-29 14:48:06 -0700757}
758
759/**
760 * apparmor_bprm_set_creds - set the new creds on the bprm struct
761 * @bprm: binprm for the exec (NOT NULL)
762 *
763 * Returns: %0 or error on failure
John Johansen93c98a42017-06-09 16:55:04 -0700764 *
765 * TODO: once the other paths are done see if we can't refactor into a fn
John Johansen898127c2010-07-29 14:48:06 -0700766 */
767int apparmor_bprm_set_creds(struct linux_binprm *bprm)
768{
John Johansen55a26eb2017-01-16 00:43:00 -0800769 struct aa_task_ctx *ctx;
John Johansen93c98a42017-06-09 16:55:04 -0700770 struct aa_label *label, *new = NULL;
771 struct aa_profile *profile;
John Johansen898127c2010-07-29 14:48:06 -0700772 char *buffer = NULL;
John Johansen93c98a42017-06-09 16:55:04 -0700773 const char *info = NULL;
774 int error = 0;
775 bool unsafe = false;
John Johansen898127c2010-07-29 14:48:06 -0700776 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500777 file_inode(bprm->file)->i_uid,
778 file_inode(bprm->file)->i_mode
John Johansen898127c2010-07-29 14:48:06 -0700779 };
John Johansen898127c2010-07-29 14:48:06 -0700780
781 if (bprm->cred_prepared)
782 return 0;
783
John Johansen55a26eb2017-01-16 00:43:00 -0800784 ctx = cred_ctx(bprm->cred);
785 AA_BUG(!ctx);
John Johansen898127c2010-07-29 14:48:06 -0700786
John Johansen637f6882017-06-09 08:14:28 -0700787 label = aa_get_newest_label(ctx->label);
John Johansen4227c332017-05-23 03:25:14 -0700788
789 /* buffer freed below, name is pointer into buffer */
790 get_buffers(buffer);
John Johansen93c98a42017-06-09 16:55:04 -0700791 /* Test for onexec first as onexec override other x transitions. */
792 if (ctx->onexec)
793 new = handle_onexec(label, ctx->onexec, ctx->token,
794 bprm, buffer, &cond, &unsafe);
795 else
796 new = fn_label_build(label, profile, GFP_ATOMIC,
797 profile_transition(profile, bprm, buffer,
798 &cond, &unsafe));
John Johansen898127c2010-07-29 14:48:06 -0700799
John Johansen93c98a42017-06-09 16:55:04 -0700800 AA_BUG(!new);
801 if (IS_ERR(new)) {
802 error = PTR_ERR(new);
803 goto done;
804 } else if (!new) {
805 error = -ENOMEM;
806 goto done;
John Johansen898127c2010-07-29 14:48:06 -0700807 }
808
John Johansen93c98a42017-06-09 16:55:04 -0700809 /* TODO: Add ns level no_new_privs subset test */
John Johansen898127c2010-07-29 14:48:06 -0700810
811 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
812 /* FIXME: currently don't mediate shared state */
813 ;
814 }
815
John Johansen93c98a42017-06-09 16:55:04 -0700816 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
817 /* TODO: test needs to be profile of label to new */
818 error = may_change_ptraced_domain(new, &info);
John Johansenf7da2de2016-04-20 14:18:18 -0700819 if (error)
John Johansen898127c2010-07-29 14:48:06 -0700820 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700821 }
822
John Johansen93c98a42017-06-09 16:55:04 -0700823 if (unsafe) {
824 if (DEBUG_ON) {
825 dbg_printk("scrubbing environment variables for %s "
826 "label=", bprm->filename);
827 aa_label_printk(new, GFP_ATOMIC);
828 dbg_printk("\n");
829 }
John Johansen898127c2010-07-29 14:48:06 -0700830 bprm->unsafe |= AA_SECURE_X_NEEDED;
831 }
John Johansen898127c2010-07-29 14:48:06 -0700832
John Johansen93c98a42017-06-09 16:55:04 -0700833 if (label->proxy != new->proxy) {
834 /* when transitioning clear unsafe personality bits */
835 if (DEBUG_ON) {
836 dbg_printk("apparmor: clearing unsafe personality "
837 "bits. %s label=", bprm->filename);
838 aa_label_printk(new, GFP_ATOMIC);
839 dbg_printk("\n");
840 }
841 bprm->per_clear |= PER_CLEAR_ON_SETID;
842 }
John Johansen637f6882017-06-09 08:14:28 -0700843 aa_put_label(ctx->label);
John Johansen93c98a42017-06-09 16:55:04 -0700844 /* transfer reference, released when ctx is freed */
845 ctx->label = new;
John Johansen898127c2010-07-29 14:48:06 -0700846
John Johansen93c98a42017-06-09 16:55:04 -0700847done:
848 /* clear out temporary/transitional state from the context */
John Johansen55a26eb2017-01-16 00:43:00 -0800849 aa_clear_task_ctx_trans(ctx);
John Johansen898127c2010-07-29 14:48:06 -0700850
John Johansen637f6882017-06-09 08:14:28 -0700851 aa_put_label(label);
John Johansen4227c332017-05-23 03:25:14 -0700852 put_buffers(buffer);
John Johansen898127c2010-07-29 14:48:06 -0700853
854 return error;
John Johansen93c98a42017-06-09 16:55:04 -0700855
856audit:
857 error = fn_for_each(label, profile,
858 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
859 bprm->filename, NULL, new,
860 file_inode(bprm->file)->i_uid, info,
861 error));
862 aa_put_label(new);
863 goto done;
John Johansen898127c2010-07-29 14:48:06 -0700864}
865
866/**
867 * apparmor_bprm_secureexec - determine if secureexec is needed
868 * @bprm: binprm for exec (NOT NULL)
869 *
870 * Returns: %1 if secureexec is needed else %0
871 */
872int apparmor_bprm_secureexec(struct linux_binprm *bprm)
873{
John Johansen898127c2010-07-29 14:48:06 -0700874 /* the decision to use secure exec is computed in set_creds
875 * and stored in bprm->unsafe.
876 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700877 if (bprm->unsafe & AA_SECURE_X_NEEDED)
878 return 1;
John Johansen898127c2010-07-29 14:48:06 -0700879
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700880 return 0;
John Johansen898127c2010-07-29 14:48:06 -0700881}
882
John Johansen898127c2010-07-29 14:48:06 -0700883/*
884 * Functions for self directed profile change
885 */
886
John Johansen89dbf192017-06-09 17:01:43 -0700887
888/* helper fn for change_hat
John Johansen898127c2010-07-29 14:48:06 -0700889 *
John Johansen89dbf192017-06-09 17:01:43 -0700890 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
John Johansen898127c2010-07-29 14:48:06 -0700891 */
John Johansen89dbf192017-06-09 17:01:43 -0700892static struct aa_label *build_change_hat(struct aa_profile *profile,
893 const char *name, bool sibling)
John Johansen898127c2010-07-29 14:48:06 -0700894{
John Johansen89dbf192017-06-09 17:01:43 -0700895 struct aa_profile *root, *hat = NULL;
896 const char *info = NULL;
897 int error = 0;
898
899 if (sibling && PROFILE_IS_HAT(profile)) {
900 root = aa_get_profile_rcu(&profile->parent);
901 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
902 root = aa_get_profile(profile);
903 } else {
904 info = "conflicting target types";
905 error = -EPERM;
906 goto audit;
907 }
908
909 hat = aa_find_child(root, name);
910 if (!hat) {
911 error = -ENOENT;
912 if (COMPLAIN_MODE(profile)) {
913 hat = aa_new_null_profile(profile, true, name,
914 GFP_KERNEL);
915 if (!hat) {
916 info = "failed null profile create";
917 error = -ENOMEM;
918 }
919 }
920 }
921 aa_put_profile(root);
922
923audit:
924 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
925 name, hat ? hat->base.hname : NULL,
926 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, NULL,
927 error);
928 if (!hat || (error && error != -ENOENT))
929 return ERR_PTR(error);
930 /* if hat && error - complain mode, already audited and we adjust for
931 * complain mode allow by returning hat->label
932 */
933 return &hat->label;
934}
935
936/* helper fn for changing into a hat
937 *
938 * Returns: label for hat transition or ERR_PTR. Does not return NULL
939 */
940static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
941 int count, int flags)
942{
943 struct aa_profile *profile, *root, *hat = NULL;
944 struct aa_label *new;
945 struct label_it it;
946 bool sibling = false;
947 const char *name, *info = NULL;
948 int i, error;
949
950 AA_BUG(!label);
951 AA_BUG(!hats);
952 AA_BUG(count < 1);
953
954 if (PROFILE_IS_HAT(labels_profile(label)))
955 sibling = true;
956
957 /*find first matching hat */
958 for (i = 0; i < count && !hat; i++) {
959 name = hats[i];
960 label_for_each_in_ns(it, labels_ns(label), label, profile) {
961 if (sibling && PROFILE_IS_HAT(profile)) {
962 root = aa_get_profile_rcu(&profile->parent);
963 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
964 root = aa_get_profile(profile);
965 } else { /* conflicting change type */
966 info = "conflicting targets types";
967 error = -EPERM;
968 goto fail;
969 }
970 hat = aa_find_child(root, name);
971 aa_put_profile(root);
972 if (!hat) {
973 if (!COMPLAIN_MODE(profile))
974 goto outer_continue;
975 /* complain mode succeed as if hat */
976 } else if (!PROFILE_IS_HAT(hat)) {
977 info = "target not hat";
978 error = -EPERM;
979 aa_put_profile(hat);
980 goto fail;
981 }
982 aa_put_profile(hat);
983 }
984 /* found a hat for all profiles in ns */
985 goto build;
986outer_continue:
987 ;
988 }
989 /* no hats that match, find appropriate error
990 *
991 * In complain mode audit of the failure is based off of the first
992 * hat supplied. This is done due how userspace interacts with
993 * change_hat.
994 */
995 name = NULL;
996 label_for_each_in_ns(it, labels_ns(label), label, profile) {
997 if (!list_empty(&profile->base.profiles)) {
998 info = "hat not found";
999 error = -ENOENT;
1000 goto fail;
1001 }
1002 }
1003 info = "no hats defined";
1004 error = -ECHILD;
1005
1006fail:
1007 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1008 /*
1009 * no target as it has failed to be found or built
1010 *
1011 * change_hat uses probing and should not log failures
1012 * related to missing hats
1013 */
1014 /* TODO: get rid of GLOBAL_ROOT_UID */
1015 if (count > 1 || COMPLAIN_MODE(profile)) {
1016 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
1017 AA_MAY_CHANGEHAT, name, NULL, NULL,
1018 GLOBAL_ROOT_UID, info, error);
1019 }
1020 }
1021 return ERR_PTR(error);
1022
1023build:
1024 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1025 build_change_hat(profile, name, sibling),
1026 aa_get_label(&profile->label));
1027 if (!new) {
1028 info = "label build failed";
1029 error = -ENOMEM;
1030 goto fail;
1031 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1032
1033 return new;
John Johansen898127c2010-07-29 14:48:06 -07001034}
1035
1036/**
1037 * aa_change_hat - change hat to/from subprofile
1038 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1039 * @count: number of hat names in @hats
1040 * @token: magic value to validate the hat change
John Johansendf8073c2017-06-09 11:36:48 -07001041 * @flags: flags affecting behavior of the change
John Johansen898127c2010-07-29 14:48:06 -07001042 *
John Johansen89dbf192017-06-09 17:01:43 -07001043 * Returns %0 on success, error otherwise.
1044 *
John Johansen898127c2010-07-29 14:48:06 -07001045 * Change to the first profile specified in @hats that exists, and store
1046 * the @hat_magic in the current task context. If the count == 0 and the
1047 * @token matches that stored in the current task context, return to the
1048 * top level profile.
1049 *
John Johansen89dbf192017-06-09 17:01:43 -07001050 * change_hat only applies to profiles in the current ns, and each profile
1051 * in the ns must make the same transition otherwise change_hat will fail.
John Johansen898127c2010-07-29 14:48:06 -07001052 */
John Johansendf8073c2017-06-09 11:36:48 -07001053int aa_change_hat(const char *hats[], int count, u64 token, int flags)
John Johansen898127c2010-07-29 14:48:06 -07001054{
1055 const struct cred *cred;
John Johansen55a26eb2017-01-16 00:43:00 -08001056 struct aa_task_ctx *ctx;
John Johansen89dbf192017-06-09 17:01:43 -07001057 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1058 struct aa_profile *profile;
John Johansen2d679f32017-05-29 12:19:39 -07001059 struct aa_perms perms = {};
John Johansen89dbf192017-06-09 17:01:43 -07001060 const char *info = NULL;
John Johansen898127c2010-07-29 14:48:06 -07001061 int error = 0;
1062
John Johansenc29bceb2012-04-12 16:47:51 -05001063 /*
1064 * Fail explicitly requested domain transitions if no_new_privs.
1065 * There is no exception for unconfined as change_hat is not
1066 * available.
1067 */
John Johansen89dbf192017-06-09 17:01:43 -07001068 if (task_no_new_privs(current)) {
1069 /* not an apparmor denial per se, so don't log it */
1070 AA_DEBUG("no_new_privs - change_hat denied");
John Johansenc29bceb2012-04-12 16:47:51 -05001071 return -EPERM;
John Johansen89dbf192017-06-09 17:01:43 -07001072 }
John Johansenc29bceb2012-04-12 16:47:51 -05001073
John Johansen898127c2010-07-29 14:48:06 -07001074 /* released below */
1075 cred = get_current_cred();
John Johansen55a26eb2017-01-16 00:43:00 -08001076 ctx = cred_ctx(cred);
John Johansen637f6882017-06-09 08:14:28 -07001077 label = aa_get_newest_cred_label(cred);
John Johansen89dbf192017-06-09 17:01:43 -07001078 previous = aa_get_newest_label(ctx->previous);
John Johansen898127c2010-07-29 14:48:06 -07001079
John Johansen637f6882017-06-09 08:14:28 -07001080 if (unconfined(label)) {
John Johansen89dbf192017-06-09 17:01:43 -07001081 info = "unconfined can not change_hat";
John Johansen898127c2010-07-29 14:48:06 -07001082 error = -EPERM;
John Johansen89dbf192017-06-09 17:01:43 -07001083 goto fail;
John Johansen898127c2010-07-29 14:48:06 -07001084 }
1085
1086 if (count) {
John Johansen89dbf192017-06-09 17:01:43 -07001087 new = change_hat(label, hats, count, flags);
1088 AA_BUG(!new);
1089 if (IS_ERR(new)) {
1090 error = PTR_ERR(new);
1091 new = NULL;
1092 /* already audited */
1093 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001094 }
1095
John Johansen89dbf192017-06-09 17:01:43 -07001096 error = may_change_ptraced_domain(new, &info);
1097 if (error)
1098 goto fail;
John Johansen898127c2010-07-29 14:48:06 -07001099
John Johansen89dbf192017-06-09 17:01:43 -07001100 if (flags & AA_CHANGE_TEST)
1101 goto out;
1102
1103 target = new;
1104 error = aa_set_current_hat(new, token);
1105 if (error == -EACCES)
1106 /* kill task in case of brute force attacks */
1107 goto kill;
1108 } else if (previous && !(flags & AA_CHANGE_TEST)) {
1109 /* Return to saved label. Kill task if restore fails
John Johansen898127c2010-07-29 14:48:06 -07001110 * to avoid brute force attacks
1111 */
John Johansen89dbf192017-06-09 17:01:43 -07001112 target = previous;
John Johansen637f6882017-06-09 08:14:28 -07001113 error = aa_restore_previous_label(token);
John Johansen89dbf192017-06-09 17:01:43 -07001114 if (error) {
1115 if (error == -EACCES)
1116 goto kill;
1117 goto fail;
1118 }
1119 } /* else ignore @flags && restores when there is no saved profile */
John Johansen898127c2010-07-29 14:48:06 -07001120
1121out:
John Johansen89dbf192017-06-09 17:01:43 -07001122 aa_put_label(new);
1123 aa_put_label(previous);
John Johansen637f6882017-06-09 08:14:28 -07001124 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -07001125 put_cred(cred);
1126
1127 return error;
John Johansen89dbf192017-06-09 17:01:43 -07001128
1129kill:
1130 info = "failed token match";
1131 perms.kill = AA_MAY_CHANGEHAT;
1132
1133fail:
1134 fn_for_each_in_ns(label, profile,
1135 aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1136 AA_MAY_CHANGEHAT, NULL, NULL, target,
1137 GLOBAL_ROOT_UID, info, error));
1138
1139 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001140}
1141
John Johansen89dbf192017-06-09 17:01:43 -07001142
1143
John Johansen898127c2010-07-29 14:48:06 -07001144/**
1145 * aa_change_profile - perform a one-way profile transition
John Johansenaa9a39a2017-01-16 00:43:06 -08001146 * @fqname: name of profile may include namespace (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -07001147 * @onexec: whether this transition is to take place immediately or at exec
John Johansendf8073c2017-06-09 11:36:48 -07001148 * @flags: flags affecting change behavior
John Johansen898127c2010-07-29 14:48:06 -07001149 *
1150 * Change to new profile @name. Unlike with hats, there is no way
1151 * to change back. If @name isn't specified the current profile name is
1152 * used.
1153 * If @onexec then the transition is delayed until
1154 * the next exec.
1155 *
1156 * Returns %0 on success, error otherwise.
1157 */
John Johansendf8073c2017-06-09 11:36:48 -07001158int aa_change_profile(const char *fqname, int flags)
John Johansen898127c2010-07-29 14:48:06 -07001159{
1160 const struct cred *cred;
John Johansen637f6882017-06-09 08:14:28 -07001161 struct aa_label *label;
John Johansen898127c2010-07-29 14:48:06 -07001162 struct aa_profile *profile, *target = NULL;
John Johansen2d679f32017-05-29 12:19:39 -07001163 struct aa_perms perms = {};
John Johansenaa9a39a2017-01-16 00:43:06 -08001164 const char *info = NULL, *op;
John Johansen47f6e5c2017-01-16 00:43:01 -08001165 int error = 0;
John Johansen898127c2010-07-29 14:48:06 -07001166 u32 request;
1167
John Johansenaa9a39a2017-01-16 00:43:06 -08001168 if (!fqname || !*fqname) {
1169 AA_DEBUG("no profile name");
John Johansen898127c2010-07-29 14:48:06 -07001170 return -EINVAL;
John Johansenaa9a39a2017-01-16 00:43:06 -08001171 }
John Johansen898127c2010-07-29 14:48:06 -07001172
John Johansendf8073c2017-06-09 11:36:48 -07001173 if (flags & AA_CHANGE_ONEXEC) {
John Johansen898127c2010-07-29 14:48:06 -07001174 request = AA_MAY_ONEXEC;
1175 op = OP_CHANGE_ONEXEC;
1176 } else {
1177 request = AA_MAY_CHANGE_PROFILE;
1178 op = OP_CHANGE_PROFILE;
1179 }
1180
1181 cred = get_current_cred();
John Johansen637f6882017-06-09 08:14:28 -07001182 label = aa_get_newest_cred_label(cred);
1183 profile = labels_profile(label);
John Johansen898127c2010-07-29 14:48:06 -07001184
John Johansenc29bceb2012-04-12 16:47:51 -05001185 /*
1186 * Fail explicitly requested domain transitions if no_new_privs
1187 * and not unconfined.
1188 * Domain transitions from unconfined are allowed even when
1189 * no_new_privs is set because this aways results in a reduction
1190 * of permissions.
1191 */
John Johansen637f6882017-06-09 08:14:28 -07001192 if (task_no_new_privs(current) && !profile_unconfined(profile)) {
John Johansenc29bceb2012-04-12 16:47:51 -05001193 put_cred(cred);
1194 return -EPERM;
1195 }
1196
John Johansen637f6882017-06-09 08:14:28 -07001197 target = aa_fqlookupn_profile(label, fqname, strlen(fqname));
John Johansen898127c2010-07-29 14:48:06 -07001198 if (!target) {
1199 info = "profile not found";
1200 error = -ENOENT;
John Johansendf8073c2017-06-09 11:36:48 -07001201 if ((flags & AA_CHANGE_TEST) ||
1202 !COMPLAIN_MODE(profile))
John Johansen898127c2010-07-29 14:48:06 -07001203 goto audit;
1204 /* released below */
John Johansenaa9a39a2017-01-16 00:43:06 -08001205 target = aa_new_null_profile(profile, false, fqname,
1206 GFP_KERNEL);
John Johansen898127c2010-07-29 14:48:06 -07001207 if (!target) {
1208 info = "failed null profile create";
1209 error = -ENOMEM;
1210 goto audit;
1211 }
1212 }
1213
John Johansen93c98a42017-06-09 16:55:04 -07001214 perms = change_profile_perms_wrapper(profile, target, request,
1215 profile->file.start);
John Johansenaa9a39a2017-01-16 00:43:06 -08001216 if (!(perms.allow & request)) {
1217 error = -EACCES;
1218 goto audit;
1219 }
1220
John Johansen898127c2010-07-29 14:48:06 -07001221 /* check if tracing task is allowed to trace target domain */
John Johansenb2d09ae2017-06-09 14:22:14 -07001222 error = may_change_ptraced_domain(&target->label, &info);
John Johansen898127c2010-07-29 14:48:06 -07001223 if (error) {
1224 info = "ptrace prevents transition";
1225 goto audit;
1226 }
1227
John Johansendf8073c2017-06-09 11:36:48 -07001228 if (flags & AA_CHANGE_TEST)
John Johansen898127c2010-07-29 14:48:06 -07001229 goto audit;
1230
John Johansendf8073c2017-06-09 11:36:48 -07001231 if (flags & AA_CHANGE_ONEXEC)
John Johansen637f6882017-06-09 08:14:28 -07001232 error = aa_set_current_onexec(&target->label, 0);
John Johansen898127c2010-07-29 14:48:06 -07001233 else
John Johansen637f6882017-06-09 08:14:28 -07001234 error = aa_replace_current_label(&target->label);
John Johansen898127c2010-07-29 14:48:06 -07001235
1236audit:
John Johansendf8073c2017-06-09 11:36:48 -07001237 if (!(flags & AA_CHANGE_TEST))
John Johansenaa9a39a2017-01-16 00:43:06 -08001238 error = aa_audit_file(profile, &perms, op, request, NULL,
John Johansen98c3d182017-06-09 15:48:20 -07001239 fqname, NULL, GLOBAL_ROOT_UID, info,
1240 error);
John Johansen898127c2010-07-29 14:48:06 -07001241
John Johansen898127c2010-07-29 14:48:06 -07001242 aa_put_profile(target);
John Johansen637f6882017-06-09 08:14:28 -07001243 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -07001244 put_cred(cred);
1245
1246 return error;
1247}