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