blob: 02d2f01e908d3b2649abab26d82956e9f0f7af83 [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 Johansen898127c2010-07-29 14:48:06 -070054 * @to_profile: profile to change to (NOT NULL)
55 *
Oleg Nesterov51775fe2013-10-08 05:46:03 -070056 * Check if current is ptraced and if so if the tracing task is allowed
John Johansen898127c2010-07-29 14:48:06 -070057 * to trace the new domain
58 *
59 * Returns: %0 or error if change not allowed
60 */
Oleg Nesterov51775fe2013-10-08 05:46:03 -070061static int may_change_ptraced_domain(struct aa_profile *to_profile)
John Johansen898127c2010-07-29 14:48:06 -070062{
63 struct task_struct *tracer;
John Johansen898127c2010-07-29 14:48:06 -070064 struct aa_profile *tracerp = NULL;
65 int error = 0;
66
67 rcu_read_lock();
Oleg Nesterov51775fe2013-10-08 05:46:03 -070068 tracer = ptrace_parent(current);
John Johansen3cfcc192013-02-18 16:03:34 -080069 if (tracer)
John Johansen898127c2010-07-29 14:48:06 -070070 /* released below */
John Johansen3cfcc192013-02-18 16:03:34 -080071 tracerp = aa_get_task_profile(tracer);
John Johansen898127c2010-07-29 14:48:06 -070072
73 /* not ptraced */
74 if (!tracer || unconfined(tracerp))
75 goto out;
76
John Johansendd0c6e82013-10-08 05:37:18 -070077 error = aa_may_ptrace(tracerp, to_profile, PTRACE_MODE_ATTACH);
John Johansen898127c2010-07-29 14:48:06 -070078
79out:
John Johansen04fdc092011-06-28 15:06:38 +010080 rcu_read_unlock();
John Johansen3cfcc192013-02-18 16:03:34 -080081 aa_put_profile(tracerp);
John Johansen898127c2010-07-29 14:48:06 -070082
83 return error;
84}
85
86/**
87 * change_profile_perms - find permissions for change_profile
88 * @profile: the current profile (NOT NULL)
89 * @ns: the namespace being switched to (NOT NULL)
90 * @name: the name of the profile to change to (NOT NULL)
91 * @request: requested perms
92 * @start: state to start matching in
93 *
94 * Returns: permission set
95 */
96static struct file_perms change_profile_perms(struct aa_profile *profile,
97 struct aa_namespace *ns,
98 const char *name, u32 request,
99 unsigned int start)
100{
101 struct file_perms perms;
102 struct path_cond cond = { };
103 unsigned int state;
104
105 if (unconfined(profile)) {
106 perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
107 perms.audit = perms.quiet = perms.kill = 0;
108 return perms;
109 } else if (!profile->file.dfa) {
110 return nullperms;
111 } else if ((ns == profile->ns)) {
112 /* try matching against rules with out namespace prepended */
113 aa_str_perms(profile->file.dfa, start, name, &cond, &perms);
114 if (COMBINED_PERM_MASK(perms) & request)
115 return perms;
116 }
117
118 /* try matching with namespace name and then profile */
119 state = aa_dfa_match(profile->file.dfa, start, ns->base.name);
120 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
121 aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
122
123 return perms;
124}
125
126/**
127 * __attach_match_ - find an attachment match
128 * @name - to match against (NOT NULL)
129 * @head - profile list to walk (NOT NULL)
130 *
131 * Do a linear search on the profiles in the list. There is a matching
132 * preference where an exact match is preferred over a name which uses
133 * expressions to match, and matching expressions with the greatest
134 * xmatch_len are preferred.
135 *
136 * Requires: @head not be shared or have appropriate locks held
137 *
138 * Returns: profile or NULL if no match found
139 */
140static struct aa_profile *__attach_match(const char *name,
141 struct list_head *head)
142{
143 int len = 0;
144 struct aa_profile *profile, *candidate = NULL;
145
John Johansen01e2b672013-07-10 21:06:43 -0700146 list_for_each_entry_rcu(profile, head, base.list) {
John Johansen898127c2010-07-29 14:48:06 -0700147 if (profile->flags & PFLAG_NULL)
148 continue;
149 if (profile->xmatch && profile->xmatch_len > len) {
150 unsigned int state = aa_dfa_match(profile->xmatch,
151 DFA_START, name);
152 u32 perm = dfa_user_allow(profile->xmatch, state);
153 /* any accepting state means a valid match. */
154 if (perm & MAY_EXEC) {
155 candidate = profile;
156 len = profile->xmatch_len;
157 }
158 } else if (!strcmp(profile->base.name, name))
159 /* exact non-re match, no more searching required */
160 return profile;
161 }
162
163 return candidate;
164}
165
166/**
167 * find_attach - do attachment search for unconfined processes
168 * @ns: the current namespace (NOT NULL)
169 * @list: list to search (NOT NULL)
170 * @name: the executable name to match against (NOT NULL)
171 *
172 * Returns: profile or NULL if no match found
173 */
174static struct aa_profile *find_attach(struct aa_namespace *ns,
175 struct list_head *list, const char *name)
176{
177 struct aa_profile *profile;
178
John Johansen01e2b672013-07-10 21:06:43 -0700179 rcu_read_lock();
John Johansen898127c2010-07-29 14:48:06 -0700180 profile = aa_get_profile(__attach_match(name, list));
John Johansen01e2b672013-07-10 21:06:43 -0700181 rcu_read_unlock();
John Johansen898127c2010-07-29 14:48:06 -0700182
183 return profile;
184}
185
186/**
187 * separate_fqname - separate the namespace and profile names
188 * @fqname: the fqname name to split (NOT NULL)
189 * @ns_name: the namespace name if it exists (NOT NULL)
190 *
191 * This is the xtable equivalent routine of aa_split_fqname. It finds the
192 * split in an xtable fqname which contains an embedded \0 instead of a :
193 * if a namespace is specified. This is done so the xtable is constant and
194 * isn't re-split on every lookup.
195 *
196 * Either the profile or namespace name may be optional but if the namespace
197 * is specified the profile name termination must be present. This results
198 * in the following possible encodings:
199 * profile_name\0
200 * :ns_name\0profile_name\0
201 * :ns_name\0\0
202 *
203 * NOTE: the xtable fqname is pre-validated at load time in unpack_trans_table
204 *
205 * Returns: profile name if it is specified else NULL
206 */
207static const char *separate_fqname(const char *fqname, const char **ns_name)
208{
209 const char *name;
210
211 if (fqname[0] == ':') {
212 /* In this case there is guaranteed to be two \0 terminators
213 * in the string. They are verified at load time by
214 * by unpack_trans_table
215 */
216 *ns_name = fqname + 1; /* skip : */
217 name = *ns_name + strlen(*ns_name) + 1;
218 if (!*name)
219 name = NULL;
220 } else {
221 *ns_name = NULL;
222 name = fqname;
223 }
224
225 return name;
226}
227
228static const char *next_name(int xtype, const char *name)
229{
230 return NULL;
231}
232
233/**
234 * x_table_lookup - lookup an x transition name via transition table
235 * @profile: current profile (NOT NULL)
236 * @xindex: index into x transition table
237 *
238 * Returns: refcounted profile, or NULL on failure (MAYBE NULL)
239 */
240static struct aa_profile *x_table_lookup(struct aa_profile *profile, u32 xindex)
241{
242 struct aa_profile *new_profile = NULL;
243 struct aa_namespace *ns = profile->ns;
244 u32 xtype = xindex & AA_X_TYPE_MASK;
245 int index = xindex & AA_X_INDEX_MASK;
246 const char *name;
247
248 /* index is guaranteed to be in range, validated at load time */
249 for (name = profile->file.trans.table[index]; !new_profile && name;
250 name = next_name(xtype, name)) {
251 struct aa_namespace *new_ns;
252 const char *xname = NULL;
253
254 new_ns = NULL;
255 if (xindex & AA_X_CHILD) {
256 /* release by caller */
257 new_profile = aa_find_child(profile, name);
258 continue;
259 } else if (*name == ':') {
260 /* switching namespace */
261 const char *ns_name;
262 xname = name = separate_fqname(name, &ns_name);
263 if (!xname)
264 /* no name so use profile name */
265 xname = profile->base.hname;
266 if (*ns_name == '@') {
267 /* TODO: variable support */
268 ;
269 }
270 /* released below */
271 new_ns = aa_find_namespace(ns, ns_name);
272 if (!new_ns)
273 continue;
274 } else if (*name == '@') {
275 /* TODO: variable support */
276 continue;
277 } else {
278 /* basic namespace lookup */
279 xname = name;
280 }
281
282 /* released by caller */
283 new_profile = aa_lookup_profile(new_ns ? new_ns : ns, xname);
284 aa_put_namespace(new_ns);
285 }
286
287 /* released by caller */
288 return new_profile;
289}
290
291/**
292 * x_to_profile - get target profile for a given xindex
293 * @profile: current profile (NOT NULL)
294 * @name: name to lookup (NOT NULL)
295 * @xindex: index into x transition table
296 *
297 * find profile for a transition index
298 *
299 * Returns: refcounted profile or NULL if not found available
300 */
301static struct aa_profile *x_to_profile(struct aa_profile *profile,
302 const char *name, u32 xindex)
303{
304 struct aa_profile *new_profile = NULL;
305 struct aa_namespace *ns = profile->ns;
306 u32 xtype = xindex & AA_X_TYPE_MASK;
307
308 switch (xtype) {
309 case AA_X_NONE:
310 /* fail exec unless ix || ux fallback - handled by caller */
311 return NULL;
312 case AA_X_NAME:
313 if (xindex & AA_X_CHILD)
314 /* released by caller */
315 new_profile = find_attach(ns, &profile->base.profiles,
316 name);
317 else
318 /* released by caller */
319 new_profile = find_attach(ns, &ns->base.profiles,
320 name);
321 break;
322 case AA_X_TABLE:
323 /* released by caller */
324 new_profile = x_table_lookup(profile, xindex);
325 break;
326 }
327
328 /* released by caller */
329 return new_profile;
330}
331
332/**
333 * apparmor_bprm_set_creds - set the new creds on the bprm struct
334 * @bprm: binprm for the exec (NOT NULL)
335 *
336 * Returns: %0 or error on failure
337 */
338int apparmor_bprm_set_creds(struct linux_binprm *bprm)
339{
340 struct aa_task_cxt *cxt;
341 struct aa_profile *profile, *new_profile = NULL;
342 struct aa_namespace *ns;
343 char *buffer = NULL;
344 unsigned int state;
345 struct file_perms perms = {};
346 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500347 file_inode(bprm->file)->i_uid,
348 file_inode(bprm->file)->i_mode
John Johansen898127c2010-07-29 14:48:06 -0700349 };
John Johansenf7da2de2016-04-20 14:18:18 -0700350 const char *name = NULL, *info = NULL;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700351 int error = 0;
John Johansen898127c2010-07-29 14:48:06 -0700352
353 if (bprm->cred_prepared)
354 return 0;
355
John Johansen214beac2013-02-27 03:43:40 -0800356 cxt = cred_cxt(bprm->cred);
John Johansen898127c2010-07-29 14:48:06 -0700357 BUG_ON(!cxt);
358
John Johansen77b071b2013-07-10 21:07:43 -0700359 profile = aa_get_newest_profile(cxt->profile);
John Johansen898127c2010-07-29 14:48:06 -0700360 /*
361 * get the namespace from the replacement profile as replacement
362 * can change the namespace
363 */
364 ns = profile->ns;
365 state = profile->file.start;
366
367 /* buffer freed below, name is pointer into buffer */
John Johansen57fa1e12012-02-16 06:20:33 -0800368 error = aa_path_name(&bprm->file->f_path, profile->path_flags, &buffer,
369 &name, &info);
John Johansen898127c2010-07-29 14:48:06 -0700370 if (error) {
John Johansen03816502013-07-10 21:12:43 -0700371 if (unconfined(profile) ||
372 (profile->flags & PFLAG_IX_ON_NAME_ERROR))
John Johansen898127c2010-07-29 14:48:06 -0700373 error = 0;
John Johansen898127c2010-07-29 14:48:06 -0700374 name = bprm->filename;
375 goto audit;
376 }
377
378 /* Test for onexec first as onexec directives override other
379 * x transitions.
380 */
381 if (unconfined(profile)) {
382 /* unconfined task */
383 if (cxt->onexec)
384 /* change_profile on exec already been granted */
385 new_profile = aa_get_profile(cxt->onexec);
386 else
387 new_profile = find_attach(ns, &ns->base.profiles, name);
388 if (!new_profile)
389 goto cleanup;
John Johansenc29bceb2012-04-12 16:47:51 -0500390 /*
391 * NOTE: Domain transitions from unconfined are allowed
392 * even when no_new_privs is set because this aways results
393 * in a further reduction of permissions.
394 */
John Johansen898127c2010-07-29 14:48:06 -0700395 goto apply;
396 }
397
398 /* find exec permissions for name */
399 state = aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
400 if (cxt->onexec) {
401 struct file_perms cp;
402 info = "change_profile onexec";
John Johansenf7da2de2016-04-20 14:18:18 -0700403 new_profile = aa_get_newest_profile(cxt->onexec);
John Johansen898127c2010-07-29 14:48:06 -0700404 if (!(perms.allow & AA_MAY_ONEXEC))
405 goto audit;
406
407 /* test if this exec can be paired with change_profile onexec.
408 * onexec permission is linked to exec with a standard pairing
409 * exec\0change_profile
410 */
411 state = aa_dfa_null_transition(profile->file.dfa, state);
John Johansen0421ea92012-03-27 04:14:33 -0700412 cp = change_profile_perms(profile, cxt->onexec->ns,
413 cxt->onexec->base.name,
John Johansen898127c2010-07-29 14:48:06 -0700414 AA_MAY_ONEXEC, state);
415
416 if (!(cp.allow & AA_MAY_ONEXEC))
417 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700418 goto apply;
419 }
420
421 if (perms.allow & MAY_EXEC) {
422 /* exec permission determine how to transition */
423 new_profile = x_to_profile(profile, name, perms.xindex);
424 if (!new_profile) {
425 if (perms.xindex & AA_X_INHERIT) {
426 /* (p|c|n)ix - don't change profile but do
427 * use the newest version, which was picked
428 * up above when getting profile
429 */
430 info = "ix fallback";
431 new_profile = aa_get_profile(profile);
432 goto x_clear;
433 } else if (perms.xindex & AA_X_UNCONFINED) {
John Johansenfa2ac462013-07-10 21:08:43 -0700434 new_profile = aa_get_newest_profile(ns->unconfined);
John Johansen898127c2010-07-29 14:48:06 -0700435 info = "ux fallback";
436 } else {
John Johansen9049a792014-07-25 04:02:03 -0700437 error = -EACCES;
John Johansen898127c2010-07-29 14:48:06 -0700438 info = "profile not found";
John Johansen17322cc2013-02-18 15:59:34 -0800439 /* remove MAY_EXEC to audit as failure */
440 perms.allow &= ~MAY_EXEC;
John Johansen898127c2010-07-29 14:48:06 -0700441 }
442 }
443 } else if (COMPLAIN_MODE(profile)) {
444 /* no exec permission - are we in learning mode */
445 new_profile = aa_new_null_profile(profile, 0);
446 if (!new_profile) {
447 error = -ENOMEM;
448 info = "could not create null profile";
John Johansenf7da2de2016-04-20 14:18:18 -0700449 } else
John Johansen898127c2010-07-29 14:48:06 -0700450 error = -EACCES;
John Johansen898127c2010-07-29 14:48:06 -0700451 perms.xindex |= AA_X_UNSAFE;
452 } else
453 /* fail exec */
454 error = -EACCES;
455
John Johansenc29bceb2012-04-12 16:47:51 -0500456 /*
457 * Policy has specified a domain transition, if no_new_privs then
458 * fail the exec.
459 */
460 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) {
John Johansenc29bceb2012-04-12 16:47:51 -0500461 error = -EPERM;
462 goto cleanup;
463 }
464
John Johansen898127c2010-07-29 14:48:06 -0700465 if (!new_profile)
466 goto audit;
467
468 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
469 /* FIXME: currently don't mediate shared state */
470 ;
471 }
472
473 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
Oleg Nesterov51775fe2013-10-08 05:46:03 -0700474 error = may_change_ptraced_domain(new_profile);
John Johansenf7da2de2016-04-20 14:18:18 -0700475 if (error)
John Johansen898127c2010-07-29 14:48:06 -0700476 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700477 }
478
479 /* Determine if secure exec is needed.
480 * Can be at this point for the following reasons:
481 * 1. unconfined switching to confined
482 * 2. confined switching to different confinement
483 * 3. confined switching to unconfined
484 *
485 * Cases 2 and 3 are marked as requiring secure exec
486 * (unless policy specified "unsafe exec")
487 *
488 * bprm->unsafe is used to cache the AA_X_UNSAFE permission
489 * to avoid having to recompute in secureexec
490 */
491 if (!(perms.xindex & AA_X_UNSAFE)) {
492 AA_DEBUG("scrubbing environment variables for %s profile=%s\n",
493 name, new_profile->base.hname);
494 bprm->unsafe |= AA_SECURE_X_NEEDED;
495 }
496apply:
John Johansen898127c2010-07-29 14:48:06 -0700497 /* when transitioning profiles clear unsafe personality bits */
498 bprm->per_clear |= PER_CLEAR_ON_SETID;
499
500x_clear:
501 aa_put_profile(cxt->profile);
502 /* transfer new profile reference will be released when cxt is freed */
503 cxt->profile = new_profile;
John Johansenf7da2de2016-04-20 14:18:18 -0700504 new_profile = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700505
506 /* clear out all temporary/transitional state from the context */
John Johansen7a2871b2013-02-18 16:05:34 -0800507 aa_clear_task_cxt_trans(cxt);
John Johansen898127c2010-07-29 14:48:06 -0700508
509audit:
510 error = aa_audit_file(profile, &perms, GFP_KERNEL, OP_EXEC, MAY_EXEC,
John Johansenf7da2de2016-04-20 14:18:18 -0700511 name,
512 new_profile ? new_profile->base.hname : NULL,
513 cond.uid, info, error);
John Johansen898127c2010-07-29 14:48:06 -0700514
515cleanup:
John Johansenf7da2de2016-04-20 14:18:18 -0700516 aa_put_profile(new_profile);
John Johansen898127c2010-07-29 14:48:06 -0700517 aa_put_profile(profile);
518 kfree(buffer);
519
520 return error;
521}
522
523/**
524 * apparmor_bprm_secureexec - determine if secureexec is needed
525 * @bprm: binprm for exec (NOT NULL)
526 *
527 * Returns: %1 if secureexec is needed else %0
528 */
529int apparmor_bprm_secureexec(struct linux_binprm *bprm)
530{
John Johansen898127c2010-07-29 14:48:06 -0700531 /* the decision to use secure exec is computed in set_creds
532 * and stored in bprm->unsafe.
533 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700534 if (bprm->unsafe & AA_SECURE_X_NEEDED)
535 return 1;
John Johansen898127c2010-07-29 14:48:06 -0700536
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700537 return 0;
John Johansen898127c2010-07-29 14:48:06 -0700538}
539
540/**
541 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
542 * @bprm: binprm for the exec (NOT NULL)
543 */
544void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
545{
546 struct aa_profile *profile = __aa_current_profile();
John Johansen214beac2013-02-27 03:43:40 -0800547 struct aa_task_cxt *new_cxt = cred_cxt(bprm->cred);
John Johansen898127c2010-07-29 14:48:06 -0700548
549 /* bail out if unconfined or not changing profile */
550 if ((new_cxt->profile == profile) ||
551 (unconfined(new_cxt->profile)))
552 return;
553
554 current->pdeath_signal = 0;
555
556 /* reset soft limits and set hard limits for the new profile */
557 __aa_transition_rlimits(profile, new_cxt->profile);
558}
559
560/**
561 * apparmor_bprm_commited_cred - do cleanup after new creds committed
562 * @bprm: binprm for the exec (NOT NULL)
563 */
564void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
565{
566 /* TODO: cleanup signals - ipc mediation */
567 return;
568}
569
570/*
571 * Functions for self directed profile change
572 */
573
574/**
575 * new_compound_name - create an hname with @n2 appended to @n1
576 * @n1: base of hname (NOT NULL)
577 * @n2: name to append (NOT NULL)
578 *
579 * Returns: new name or NULL on error
580 */
581static char *new_compound_name(const char *n1, const char *n2)
582{
583 char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL);
584 if (name)
585 sprintf(name, "%s//%s", n1, n2);
586 return name;
587}
588
589/**
590 * aa_change_hat - change hat to/from subprofile
591 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
592 * @count: number of hat names in @hats
593 * @token: magic value to validate the hat change
594 * @permtest: true if this is just a permission test
595 *
596 * Change to the first profile specified in @hats that exists, and store
597 * the @hat_magic in the current task context. If the count == 0 and the
598 * @token matches that stored in the current task context, return to the
599 * top level profile.
600 *
601 * Returns %0 on success, error otherwise.
602 */
603int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)
604{
605 const struct cred *cred;
606 struct aa_task_cxt *cxt;
607 struct aa_profile *profile, *previous_profile, *hat = NULL;
608 char *name = NULL;
609 int i;
610 struct file_perms perms = {};
611 const char *target = NULL, *info = NULL;
612 int error = 0;
613
John Johansenc29bceb2012-04-12 16:47:51 -0500614 /*
615 * Fail explicitly requested domain transitions if no_new_privs.
616 * There is no exception for unconfined as change_hat is not
617 * available.
618 */
Kees Cook1d4457f2014-05-21 15:23:46 -0700619 if (task_no_new_privs(current))
John Johansenc29bceb2012-04-12 16:47:51 -0500620 return -EPERM;
621
John Johansen898127c2010-07-29 14:48:06 -0700622 /* released below */
623 cred = get_current_cred();
John Johansen214beac2013-02-27 03:43:40 -0800624 cxt = cred_cxt(cred);
John Johansen3d406582016-08-31 21:10:06 -0700625 profile = aa_get_newest_profile(aa_cred_profile(cred));
626 previous_profile = aa_get_newest_profile(cxt->previous);
John Johansen898127c2010-07-29 14:48:06 -0700627
628 if (unconfined(profile)) {
629 info = "unconfined";
630 error = -EPERM;
631 goto audit;
632 }
633
634 if (count) {
635 /* attempting to change into a new hat or switch to a sibling */
636 struct aa_profile *root;
John Johansen01e2b672013-07-10 21:06:43 -0700637 if (PROFILE_IS_HAT(profile))
638 root = aa_get_profile_rcu(&profile->parent);
639 else
640 root = aa_get_profile(profile);
John Johansen898127c2010-07-29 14:48:06 -0700641
642 /* find first matching hat */
643 for (i = 0; i < count && !hat; i++)
644 /* released below */
645 hat = aa_find_child(root, hats[i]);
646 if (!hat) {
647 if (!COMPLAIN_MODE(root) || permtest) {
648 if (list_empty(&root->base.profiles))
649 error = -ECHILD;
650 else
651 error = -ENOENT;
John Johansen01e2b672013-07-10 21:06:43 -0700652 aa_put_profile(root);
John Johansen898127c2010-07-29 14:48:06 -0700653 goto out;
654 }
655
656 /*
657 * In complain mode and failed to match any hats.
658 * Audit the failure is based off of the first hat
659 * supplied. This is done due how userspace
660 * interacts with change_hat.
661 *
662 * TODO: Add logging of all failed hats
663 */
664
665 /* freed below */
666 name = new_compound_name(root->base.hname, hats[0]);
John Johansen01e2b672013-07-10 21:06:43 -0700667 aa_put_profile(root);
John Johansen898127c2010-07-29 14:48:06 -0700668 target = name;
669 /* released below */
670 hat = aa_new_null_profile(profile, 1);
671 if (!hat) {
672 info = "failed null profile create";
673 error = -ENOMEM;
674 goto audit;
675 }
676 } else {
John Johansen01e2b672013-07-10 21:06:43 -0700677 aa_put_profile(root);
John Johansen898127c2010-07-29 14:48:06 -0700678 target = hat->base.hname;
679 if (!PROFILE_IS_HAT(hat)) {
680 info = "target not hat";
681 error = -EPERM;
682 goto audit;
683 }
684 }
685
Oleg Nesterov51775fe2013-10-08 05:46:03 -0700686 error = may_change_ptraced_domain(hat);
John Johansen898127c2010-07-29 14:48:06 -0700687 if (error) {
688 info = "ptraced";
689 error = -EPERM;
690 goto audit;
691 }
692
693 if (!permtest) {
694 error = aa_set_current_hat(hat, token);
695 if (error == -EACCES)
696 /* kill task in case of brute force attacks */
697 perms.kill = AA_MAY_CHANGEHAT;
698 else if (name && !error)
699 /* reset error for learning of new hats */
700 error = -ENOENT;
701 }
702 } else if (previous_profile) {
703 /* Return to saved profile. Kill task if restore fails
704 * to avoid brute force attacks
705 */
706 target = previous_profile->base.hname;
707 error = aa_restore_previous_profile(token);
708 perms.kill = AA_MAY_CHANGEHAT;
709 } else
710 /* ignore restores when there is no saved profile */
711 goto out;
712
713audit:
714 if (!permtest)
715 error = aa_audit_file(profile, &perms, GFP_KERNEL,
716 OP_CHANGE_HAT, AA_MAY_CHANGEHAT, NULL,
Eric W. Biederman2db81452012-02-07 16:33:13 -0800717 target, GLOBAL_ROOT_UID, info, error);
John Johansen898127c2010-07-29 14:48:06 -0700718
719out:
720 aa_put_profile(hat);
721 kfree(name);
John Johansen3d406582016-08-31 21:10:06 -0700722 aa_put_profile(profile);
723 aa_put_profile(previous_profile);
John Johansen898127c2010-07-29 14:48:06 -0700724 put_cred(cred);
725
726 return error;
727}
728
729/**
730 * aa_change_profile - perform a one-way profile transition
731 * @ns_name: name of the profile namespace to change to (MAYBE NULL)
732 * @hname: name of profile to change to (MAYBE NULL)
733 * @onexec: whether this transition is to take place immediately or at exec
734 * @permtest: true if this is just a permission test
735 *
736 * Change to new profile @name. Unlike with hats, there is no way
737 * to change back. If @name isn't specified the current profile name is
738 * used.
739 * If @onexec then the transition is delayed until
740 * the next exec.
741 *
742 * Returns %0 on success, error otherwise.
743 */
744int aa_change_profile(const char *ns_name, const char *hname, bool onexec,
745 bool permtest)
746{
747 const struct cred *cred;
John Johansen898127c2010-07-29 14:48:06 -0700748 struct aa_profile *profile, *target = NULL;
749 struct aa_namespace *ns = NULL;
750 struct file_perms perms = {};
751 const char *name = NULL, *info = NULL;
752 int op, error = 0;
753 u32 request;
754
755 if (!hname && !ns_name)
756 return -EINVAL;
757
758 if (onexec) {
759 request = AA_MAY_ONEXEC;
760 op = OP_CHANGE_ONEXEC;
761 } else {
762 request = AA_MAY_CHANGE_PROFILE;
763 op = OP_CHANGE_PROFILE;
764 }
765
766 cred = get_current_cred();
John Johansen898127c2010-07-29 14:48:06 -0700767 profile = aa_cred_profile(cred);
768
John Johansenc29bceb2012-04-12 16:47:51 -0500769 /*
770 * Fail explicitly requested domain transitions if no_new_privs
771 * and not unconfined.
772 * Domain transitions from unconfined are allowed even when
773 * no_new_privs is set because this aways results in a reduction
774 * of permissions.
775 */
Kees Cook1d4457f2014-05-21 15:23:46 -0700776 if (task_no_new_privs(current) && !unconfined(profile)) {
John Johansenc29bceb2012-04-12 16:47:51 -0500777 put_cred(cred);
778 return -EPERM;
779 }
780
John Johansen898127c2010-07-29 14:48:06 -0700781 if (ns_name) {
782 /* released below */
783 ns = aa_find_namespace(profile->ns, ns_name);
784 if (!ns) {
785 /* we don't create new namespace in complain mode */
786 name = ns_name;
787 info = "namespace not found";
788 error = -ENOENT;
789 goto audit;
790 }
791 } else
792 /* released below */
793 ns = aa_get_namespace(profile->ns);
794
795 /* if the name was not specified, use the name of the current profile */
796 if (!hname) {
797 if (unconfined(profile))
798 hname = ns->unconfined->base.hname;
799 else
800 hname = profile->base.hname;
801 }
802
803 perms = change_profile_perms(profile, ns, hname, request,
804 profile->file.start);
805 if (!(perms.allow & request)) {
806 error = -EACCES;
807 goto audit;
808 }
809
810 /* released below */
811 target = aa_lookup_profile(ns, hname);
812 if (!target) {
813 info = "profile not found";
814 error = -ENOENT;
815 if (permtest || !COMPLAIN_MODE(profile))
816 goto audit;
817 /* released below */
818 target = aa_new_null_profile(profile, 0);
819 if (!target) {
820 info = "failed null profile create";
821 error = -ENOMEM;
822 goto audit;
823 }
824 }
825
826 /* check if tracing task is allowed to trace target domain */
Oleg Nesterov51775fe2013-10-08 05:46:03 -0700827 error = may_change_ptraced_domain(target);
John Johansen898127c2010-07-29 14:48:06 -0700828 if (error) {
829 info = "ptrace prevents transition";
830 goto audit;
831 }
832
833 if (permtest)
834 goto audit;
835
836 if (onexec)
837 error = aa_set_current_onexec(target);
838 else
839 error = aa_replace_current_profile(target);
840
841audit:
842 if (!permtest)
843 error = aa_audit_file(profile, &perms, GFP_KERNEL, op, request,
Eric W. Biederman2db81452012-02-07 16:33:13 -0800844 name, hname, GLOBAL_ROOT_UID, info, error);
John Johansen898127c2010-07-29 14:48:06 -0700845
846 aa_put_namespace(ns);
847 aa_put_profile(target);
848 put_cred(cred);
849
850 return error;
851}