blob: 8d6797c849feeeb80e57b4141e3c3003f1136e5f [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 Johansen637f6882017-06-09 08:14:28 -070064 struct aa_label *tracerl = NULL;
John Johansen898127c2010-07-29 14:48:06 -070065 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 Johansen637f6882017-06-09 08:14:28 -070071 tracerl = aa_get_task_label(tracer);
John Johansen898127c2010-07-29 14:48:06 -070072
73 /* not ptraced */
John Johansen637f6882017-06-09 08:14:28 -070074 if (!tracer || unconfined(tracerl))
John Johansen898127c2010-07-29 14:48:06 -070075 goto out;
76
John Johansen637f6882017-06-09 08:14:28 -070077 error = aa_may_ptrace(labels_profile(tracerl), to_profile,
78 PTRACE_MODE_ATTACH);
John Johansen898127c2010-07-29 14:48:06 -070079
80out:
John Johansen04fdc092011-06-28 15:06:38 +010081 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -070082 aa_put_label(tracerl);
John Johansen898127c2010-07-29 14:48:06 -070083
84 return error;
85}
86
87/**
88 * change_profile_perms - find permissions for change_profile
89 * @profile: the current profile (NOT NULL)
90 * @ns: the namespace being switched to (NOT NULL)
91 * @name: the name of the profile to change to (NOT NULL)
92 * @request: requested perms
93 * @start: state to start matching in
94 *
95 * Returns: permission set
96 */
John Johansen2d679f32017-05-29 12:19:39 -070097static struct aa_perms change_profile_perms(struct aa_profile *profile,
98 struct aa_ns *ns,
99 const char *name, u32 request,
100 unsigned int start)
John Johansen898127c2010-07-29 14:48:06 -0700101{
John Johansen2d679f32017-05-29 12:19:39 -0700102 struct aa_perms perms;
John Johansen898127c2010-07-29 14:48:06 -0700103 struct path_cond cond = { };
104 unsigned int state;
105
John Johansen637f6882017-06-09 08:14:28 -0700106 if (profile_unconfined(profile)) {
John Johansen898127c2010-07-29 14:48:06 -0700107 perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
108 perms.audit = perms.quiet = perms.kill = 0;
109 return perms;
110 } else if (!profile->file.dfa) {
111 return nullperms;
112 } else if ((ns == profile->ns)) {
113 /* try matching against rules with out namespace prepended */
114 aa_str_perms(profile->file.dfa, start, name, &cond, &perms);
115 if (COMBINED_PERM_MASK(perms) & request)
116 return perms;
117 }
118
119 /* try matching with namespace name and then profile */
120 state = aa_dfa_match(profile->file.dfa, start, ns->base.name);
121 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
122 aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
123
124 return perms;
125}
126
127/**
128 * __attach_match_ - find an attachment match
129 * @name - to match against (NOT NULL)
130 * @head - profile list to walk (NOT NULL)
131 *
132 * Do a linear search on the profiles in the list. There is a matching
133 * preference where an exact match is preferred over a name which uses
134 * expressions to match, and matching expressions with the greatest
135 * xmatch_len are preferred.
136 *
137 * Requires: @head not be shared or have appropriate locks held
138 *
139 * Returns: profile or NULL if no match found
140 */
141static struct aa_profile *__attach_match(const char *name,
142 struct list_head *head)
143{
144 int len = 0;
145 struct aa_profile *profile, *candidate = NULL;
146
John Johansen01e2b672013-07-10 21:06:43 -0700147 list_for_each_entry_rcu(profile, head, base.list) {
John Johansen637f6882017-06-09 08:14:28 -0700148 if (profile->label.flags & FLAG_NULL)
John Johansen898127c2010-07-29 14:48:06 -0700149 continue;
150 if (profile->xmatch && profile->xmatch_len > len) {
151 unsigned int state = aa_dfa_match(profile->xmatch,
152 DFA_START, name);
153 u32 perm = dfa_user_allow(profile->xmatch, state);
154 /* any accepting state means a valid match. */
155 if (perm & MAY_EXEC) {
156 candidate = profile;
157 len = profile->xmatch_len;
158 }
159 } else if (!strcmp(profile->base.name, name))
160 /* exact non-re match, no more searching required */
161 return profile;
162 }
163
164 return candidate;
165}
166
167/**
168 * find_attach - do attachment search for unconfined processes
169 * @ns: the current namespace (NOT NULL)
170 * @list: list to search (NOT NULL)
171 * @name: the executable name to match against (NOT NULL)
172 *
173 * Returns: profile or NULL if no match found
174 */
John Johansen98849df2017-01-16 00:42:16 -0800175static struct aa_profile *find_attach(struct aa_ns *ns,
John Johansen898127c2010-07-29 14:48:06 -0700176 struct list_head *list, const char *name)
177{
178 struct aa_profile *profile;
179
John Johansen01e2b672013-07-10 21:06:43 -0700180 rcu_read_lock();
John Johansen898127c2010-07-29 14:48:06 -0700181 profile = aa_get_profile(__attach_match(name, list));
John Johansen01e2b672013-07-10 21:06:43 -0700182 rcu_read_unlock();
John Johansen898127c2010-07-29 14:48:06 -0700183
184 return profile;
185}
186
187/**
188 * separate_fqname - separate the namespace and profile names
189 * @fqname: the fqname name to split (NOT NULL)
190 * @ns_name: the namespace name if it exists (NOT NULL)
191 *
192 * This is the xtable equivalent routine of aa_split_fqname. It finds the
193 * split in an xtable fqname which contains an embedded \0 instead of a :
194 * if a namespace is specified. This is done so the xtable is constant and
195 * isn't re-split on every lookup.
196 *
197 * Either the profile or namespace name may be optional but if the namespace
198 * is specified the profile name termination must be present. This results
199 * in the following possible encodings:
200 * profile_name\0
201 * :ns_name\0profile_name\0
202 * :ns_name\0\0
203 *
204 * NOTE: the xtable fqname is pre-validated at load time in unpack_trans_table
205 *
206 * Returns: profile name if it is specified else NULL
207 */
208static const char *separate_fqname(const char *fqname, const char **ns_name)
209{
210 const char *name;
211
212 if (fqname[0] == ':') {
213 /* In this case there is guaranteed to be two \0 terminators
214 * in the string. They are verified at load time by
215 * by unpack_trans_table
216 */
217 *ns_name = fqname + 1; /* skip : */
218 name = *ns_name + strlen(*ns_name) + 1;
219 if (!*name)
220 name = NULL;
221 } else {
222 *ns_name = NULL;
223 name = fqname;
224 }
225
226 return name;
227}
228
229static const char *next_name(int xtype, const char *name)
230{
231 return NULL;
232}
233
234/**
235 * x_table_lookup - lookup an x transition name via transition table
236 * @profile: current profile (NOT NULL)
237 * @xindex: index into x transition table
238 *
239 * Returns: refcounted profile, or NULL on failure (MAYBE NULL)
240 */
241static struct aa_profile *x_table_lookup(struct aa_profile *profile, u32 xindex)
242{
243 struct aa_profile *new_profile = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800244 struct aa_ns *ns = profile->ns;
John Johansen898127c2010-07-29 14:48:06 -0700245 u32 xtype = xindex & AA_X_TYPE_MASK;
246 int index = xindex & AA_X_INDEX_MASK;
247 const char *name;
248
249 /* index is guaranteed to be in range, validated at load time */
250 for (name = profile->file.trans.table[index]; !new_profile && name;
251 name = next_name(xtype, name)) {
John Johansen98849df2017-01-16 00:42:16 -0800252 struct aa_ns *new_ns;
John Johansen898127c2010-07-29 14:48:06 -0700253 const char *xname = NULL;
254
255 new_ns = NULL;
256 if (xindex & AA_X_CHILD) {
257 /* release by caller */
258 new_profile = aa_find_child(profile, name);
259 continue;
260 } else if (*name == ':') {
261 /* switching namespace */
262 const char *ns_name;
263 xname = name = separate_fqname(name, &ns_name);
264 if (!xname)
265 /* no name so use profile name */
266 xname = profile->base.hname;
267 if (*ns_name == '@') {
268 /* TODO: variable support */
269 ;
270 }
271 /* released below */
John Johansen98849df2017-01-16 00:42:16 -0800272 new_ns = aa_find_ns(ns, ns_name);
John Johansen898127c2010-07-29 14:48:06 -0700273 if (!new_ns)
274 continue;
275 } else if (*name == '@') {
276 /* TODO: variable support */
277 continue;
278 } else {
279 /* basic namespace lookup */
280 xname = name;
281 }
282
283 /* released by caller */
284 new_profile = aa_lookup_profile(new_ns ? new_ns : ns, xname);
John Johansen98849df2017-01-16 00:42:16 -0800285 aa_put_ns(new_ns);
John Johansen898127c2010-07-29 14:48:06 -0700286 }
287
288 /* released by caller */
289 return new_profile;
290}
291
292/**
293 * x_to_profile - get target profile for a given xindex
294 * @profile: current profile (NOT NULL)
295 * @name: name to lookup (NOT NULL)
296 * @xindex: index into x transition table
297 *
298 * find profile for a transition index
299 *
300 * Returns: refcounted profile or NULL if not found available
301 */
302static struct aa_profile *x_to_profile(struct aa_profile *profile,
303 const char *name, u32 xindex)
304{
305 struct aa_profile *new_profile = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800306 struct aa_ns *ns = profile->ns;
John Johansen898127c2010-07-29 14:48:06 -0700307 u32 xtype = xindex & AA_X_TYPE_MASK;
308
309 switch (xtype) {
310 case AA_X_NONE:
311 /* fail exec unless ix || ux fallback - handled by caller */
312 return NULL;
313 case AA_X_NAME:
314 if (xindex & AA_X_CHILD)
315 /* released by caller */
316 new_profile = find_attach(ns, &profile->base.profiles,
317 name);
318 else
319 /* released by caller */
320 new_profile = find_attach(ns, &ns->base.profiles,
321 name);
322 break;
323 case AA_X_TABLE:
324 /* released by caller */
325 new_profile = x_table_lookup(profile, xindex);
326 break;
327 }
328
329 /* released by caller */
330 return new_profile;
331}
332
333/**
334 * apparmor_bprm_set_creds - set the new creds on the bprm struct
335 * @bprm: binprm for the exec (NOT NULL)
336 *
337 * Returns: %0 or error on failure
338 */
339int apparmor_bprm_set_creds(struct linux_binprm *bprm)
340{
John Johansen55a26eb2017-01-16 00:43:00 -0800341 struct aa_task_ctx *ctx;
John Johansen637f6882017-06-09 08:14:28 -0700342 struct aa_label *label;
John Johansen898127c2010-07-29 14:48:06 -0700343 struct aa_profile *profile, *new_profile = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800344 struct aa_ns *ns;
John Johansen898127c2010-07-29 14:48:06 -0700345 char *buffer = NULL;
346 unsigned int state;
John Johansen2d679f32017-05-29 12:19:39 -0700347 struct aa_perms perms = {};
John Johansen898127c2010-07-29 14:48:06 -0700348 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500349 file_inode(bprm->file)->i_uid,
350 file_inode(bprm->file)->i_mode
John Johansen898127c2010-07-29 14:48:06 -0700351 };
John Johansenf7da2de2016-04-20 14:18:18 -0700352 const char *name = NULL, *info = NULL;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700353 int error = 0;
John Johansen898127c2010-07-29 14:48:06 -0700354
355 if (bprm->cred_prepared)
356 return 0;
357
John Johansen55a26eb2017-01-16 00:43:00 -0800358 ctx = cred_ctx(bprm->cred);
359 AA_BUG(!ctx);
John Johansen898127c2010-07-29 14:48:06 -0700360
John Johansen637f6882017-06-09 08:14:28 -0700361 label = aa_get_newest_label(ctx->label);
362 profile = labels_profile(label);
John Johansen4227c332017-05-23 03:25:14 -0700363
364 /* buffer freed below, name is pointer into buffer */
365 get_buffers(buffer);
John Johansen898127c2010-07-29 14:48:06 -0700366 /*
367 * get the namespace from the replacement profile as replacement
368 * can change the namespace
369 */
370 ns = profile->ns;
371 state = profile->file.start;
372
John Johansen4227c332017-05-23 03:25:14 -0700373 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
John Johansen72c8a762017-05-22 03:06:52 -0700374 &name, &info, profile->disconnected);
John Johansen898127c2010-07-29 14:48:06 -0700375 if (error) {
John Johansen637f6882017-06-09 08:14:28 -0700376 if (profile_unconfined(profile) ||
377 (profile->label.flags & FLAG_IX_ON_NAME_ERROR))
John Johansen898127c2010-07-29 14:48:06 -0700378 error = 0;
John Johansen898127c2010-07-29 14:48:06 -0700379 name = bprm->filename;
380 goto audit;
381 }
382
383 /* Test for onexec first as onexec directives override other
384 * x transitions.
385 */
John Johansen637f6882017-06-09 08:14:28 -0700386 if (profile_unconfined(profile)) {
John Johansen898127c2010-07-29 14:48:06 -0700387 /* unconfined task */
John Johansen55a26eb2017-01-16 00:43:00 -0800388 if (ctx->onexec)
John Johansen898127c2010-07-29 14:48:06 -0700389 /* change_profile on exec already been granted */
John Johansen637f6882017-06-09 08:14:28 -0700390 new_profile = labels_profile(aa_get_label(ctx->onexec));
John Johansen898127c2010-07-29 14:48:06 -0700391 else
392 new_profile = find_attach(ns, &ns->base.profiles, name);
393 if (!new_profile)
394 goto cleanup;
John Johansenc29bceb2012-04-12 16:47:51 -0500395 /*
396 * NOTE: Domain transitions from unconfined are allowed
397 * even when no_new_privs is set because this aways results
398 * in a further reduction of permissions.
399 */
John Johansen898127c2010-07-29 14:48:06 -0700400 goto apply;
401 }
402
403 /* find exec permissions for name */
404 state = aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
John Johansen55a26eb2017-01-16 00:43:00 -0800405 if (ctx->onexec) {
John Johansen2d679f32017-05-29 12:19:39 -0700406 struct aa_perms cp;
John Johansen898127c2010-07-29 14:48:06 -0700407 info = "change_profile onexec";
John Johansen637f6882017-06-09 08:14:28 -0700408 new_profile = labels_profile(aa_get_newest_label(ctx->onexec));
John Johansen898127c2010-07-29 14:48:06 -0700409 if (!(perms.allow & AA_MAY_ONEXEC))
410 goto audit;
411
412 /* test if this exec can be paired with change_profile onexec.
413 * onexec permission is linked to exec with a standard pairing
414 * exec\0change_profile
415 */
416 state = aa_dfa_null_transition(profile->file.dfa, state);
John Johansen637f6882017-06-09 08:14:28 -0700417 cp = change_profile_perms(profile, labels_ns(ctx->onexec),
418 labels_profile(ctx->onexec)->base.name,
419 AA_MAY_ONEXEC, state);
John Johansen898127c2010-07-29 14:48:06 -0700420
421 if (!(cp.allow & AA_MAY_ONEXEC))
422 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700423 goto apply;
424 }
425
426 if (perms.allow & MAY_EXEC) {
427 /* exec permission determine how to transition */
428 new_profile = x_to_profile(profile, name, perms.xindex);
429 if (!new_profile) {
430 if (perms.xindex & AA_X_INHERIT) {
431 /* (p|c|n)ix - don't change profile but do
432 * use the newest version, which was picked
433 * up above when getting profile
434 */
435 info = "ix fallback";
436 new_profile = aa_get_profile(profile);
437 goto x_clear;
438 } else if (perms.xindex & AA_X_UNCONFINED) {
John Johansenfa2ac462013-07-10 21:08:43 -0700439 new_profile = aa_get_newest_profile(ns->unconfined);
John Johansen898127c2010-07-29 14:48:06 -0700440 info = "ux fallback";
441 } else {
John Johansen9049a792014-07-25 04:02:03 -0700442 error = -EACCES;
John Johansen898127c2010-07-29 14:48:06 -0700443 info = "profile not found";
John Johansen17322cc2013-02-18 15:59:34 -0800444 /* remove MAY_EXEC to audit as failure */
445 perms.allow &= ~MAY_EXEC;
John Johansen898127c2010-07-29 14:48:06 -0700446 }
447 }
448 } else if (COMPLAIN_MODE(profile)) {
449 /* no exec permission - are we in learning mode */
John Johansen181f7c92017-01-16 00:42:36 -0800450 new_profile = aa_new_null_profile(profile, false, name,
451 GFP_ATOMIC);
John Johansen898127c2010-07-29 14:48:06 -0700452 if (!new_profile) {
453 error = -ENOMEM;
454 info = "could not create null profile";
John Johansenf7da2de2016-04-20 14:18:18 -0700455 } else
John Johansen898127c2010-07-29 14:48:06 -0700456 error = -EACCES;
John Johansen898127c2010-07-29 14:48:06 -0700457 perms.xindex |= AA_X_UNSAFE;
458 } else
459 /* fail exec */
460 error = -EACCES;
461
John Johansenc29bceb2012-04-12 16:47:51 -0500462 /*
463 * Policy has specified a domain transition, if no_new_privs then
464 * fail the exec.
465 */
466 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) {
John Johansenc29bceb2012-04-12 16:47:51 -0500467 error = -EPERM;
468 goto cleanup;
469 }
470
John Johansen898127c2010-07-29 14:48:06 -0700471 if (!new_profile)
472 goto audit;
473
474 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
475 /* FIXME: currently don't mediate shared state */
476 ;
477 }
478
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300479 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Oleg Nesterov51775fe2013-10-08 05:46:03 -0700480 error = may_change_ptraced_domain(new_profile);
John Johansenf7da2de2016-04-20 14:18:18 -0700481 if (error)
John Johansen898127c2010-07-29 14:48:06 -0700482 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700483 }
484
485 /* Determine if secure exec is needed.
486 * Can be at this point for the following reasons:
487 * 1. unconfined switching to confined
488 * 2. confined switching to different confinement
489 * 3. confined switching to unconfined
490 *
491 * Cases 2 and 3 are marked as requiring secure exec
492 * (unless policy specified "unsafe exec")
493 *
494 * bprm->unsafe is used to cache the AA_X_UNSAFE permission
495 * to avoid having to recompute in secureexec
496 */
497 if (!(perms.xindex & AA_X_UNSAFE)) {
498 AA_DEBUG("scrubbing environment variables for %s profile=%s\n",
499 name, new_profile->base.hname);
500 bprm->unsafe |= AA_SECURE_X_NEEDED;
501 }
502apply:
John Johansen898127c2010-07-29 14:48:06 -0700503 /* when transitioning profiles clear unsafe personality bits */
504 bprm->per_clear |= PER_CLEAR_ON_SETID;
505
506x_clear:
John Johansen637f6882017-06-09 08:14:28 -0700507 aa_put_label(ctx->label);
John Johansen55a26eb2017-01-16 00:43:00 -0800508 /* transfer new profile reference will be released when ctx is freed */
John Johansen637f6882017-06-09 08:14:28 -0700509 ctx->label = &new_profile->label;
John Johansenf7da2de2016-04-20 14:18:18 -0700510 new_profile = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700511
512 /* clear out all temporary/transitional state from the context */
John Johansen55a26eb2017-01-16 00:43:00 -0800513 aa_clear_task_ctx_trans(ctx);
John Johansen898127c2010-07-29 14:48:06 -0700514
515audit:
John Johansenef88a7a2017-01-16 00:43:02 -0800516 error = aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name,
John Johansenf7da2de2016-04-20 14:18:18 -0700517 new_profile ? new_profile->base.hname : NULL,
518 cond.uid, info, error);
John Johansen898127c2010-07-29 14:48:06 -0700519
520cleanup:
John Johansenf7da2de2016-04-20 14:18:18 -0700521 aa_put_profile(new_profile);
John Johansen637f6882017-06-09 08:14:28 -0700522 aa_put_label(label);
John Johansen4227c332017-05-23 03:25:14 -0700523 put_buffers(buffer);
John Johansen898127c2010-07-29 14:48:06 -0700524
525 return error;
526}
527
528/**
529 * apparmor_bprm_secureexec - determine if secureexec is needed
530 * @bprm: binprm for exec (NOT NULL)
531 *
532 * Returns: %1 if secureexec is needed else %0
533 */
534int apparmor_bprm_secureexec(struct linux_binprm *bprm)
535{
John Johansen898127c2010-07-29 14:48:06 -0700536 /* the decision to use secure exec is computed in set_creds
537 * and stored in bprm->unsafe.
538 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700539 if (bprm->unsafe & AA_SECURE_X_NEEDED)
540 return 1;
John Johansen898127c2010-07-29 14:48:06 -0700541
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700542 return 0;
John Johansen898127c2010-07-29 14:48:06 -0700543}
544
John Johansen898127c2010-07-29 14:48:06 -0700545/*
546 * Functions for self directed profile change
547 */
548
549/**
550 * new_compound_name - create an hname with @n2 appended to @n1
551 * @n1: base of hname (NOT NULL)
552 * @n2: name to append (NOT NULL)
553 *
554 * Returns: new name or NULL on error
555 */
556static char *new_compound_name(const char *n1, const char *n2)
557{
558 char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL);
559 if (name)
560 sprintf(name, "%s//%s", n1, n2);
561 return name;
562}
563
564/**
565 * aa_change_hat - change hat to/from subprofile
566 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
567 * @count: number of hat names in @hats
568 * @token: magic value to validate the hat change
John Johansendf8073c2017-06-09 11:36:48 -0700569 * @flags: flags affecting behavior of the change
John Johansen898127c2010-07-29 14:48:06 -0700570 *
571 * Change to the first profile specified in @hats that exists, and store
572 * the @hat_magic in the current task context. If the count == 0 and the
573 * @token matches that stored in the current task context, return to the
574 * top level profile.
575 *
576 * Returns %0 on success, error otherwise.
577 */
John Johansendf8073c2017-06-09 11:36:48 -0700578int aa_change_hat(const char *hats[], int count, u64 token, int flags)
John Johansen898127c2010-07-29 14:48:06 -0700579{
580 const struct cred *cred;
John Johansen55a26eb2017-01-16 00:43:00 -0800581 struct aa_task_ctx *ctx;
John Johansen637f6882017-06-09 08:14:28 -0700582 struct aa_label *label, *previous_label;
583 struct aa_profile *profile, *hat = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700584 char *name = NULL;
585 int i;
John Johansen2d679f32017-05-29 12:19:39 -0700586 struct aa_perms perms = {};
John Johansen898127c2010-07-29 14:48:06 -0700587 const char *target = NULL, *info = NULL;
588 int error = 0;
589
John Johansenc29bceb2012-04-12 16:47:51 -0500590 /*
591 * Fail explicitly requested domain transitions if no_new_privs.
592 * There is no exception for unconfined as change_hat is not
593 * available.
594 */
Kees Cook1d4457f2014-05-21 15:23:46 -0700595 if (task_no_new_privs(current))
John Johansenc29bceb2012-04-12 16:47:51 -0500596 return -EPERM;
597
John Johansen898127c2010-07-29 14:48:06 -0700598 /* released below */
599 cred = get_current_cred();
John Johansen55a26eb2017-01-16 00:43:00 -0800600 ctx = cred_ctx(cred);
John Johansen637f6882017-06-09 08:14:28 -0700601 label = aa_get_newest_cred_label(cred);
602 previous_label = aa_get_newest_label(ctx->previous);
603 profile = labels_profile(label);
John Johansen898127c2010-07-29 14:48:06 -0700604
John Johansen637f6882017-06-09 08:14:28 -0700605 if (unconfined(label)) {
John Johansen898127c2010-07-29 14:48:06 -0700606 info = "unconfined";
607 error = -EPERM;
608 goto audit;
609 }
610
611 if (count) {
612 /* attempting to change into a new hat or switch to a sibling */
613 struct aa_profile *root;
John Johansen01e2b672013-07-10 21:06:43 -0700614 if (PROFILE_IS_HAT(profile))
615 root = aa_get_profile_rcu(&profile->parent);
616 else
617 root = aa_get_profile(profile);
John Johansen898127c2010-07-29 14:48:06 -0700618
619 /* find first matching hat */
620 for (i = 0; i < count && !hat; i++)
621 /* released below */
622 hat = aa_find_child(root, hats[i]);
623 if (!hat) {
John Johansendf8073c2017-06-09 11:36:48 -0700624 if (!COMPLAIN_MODE(root) || (flags & AA_CHANGE_TEST)) {
John Johansen898127c2010-07-29 14:48:06 -0700625 if (list_empty(&root->base.profiles))
626 error = -ECHILD;
627 else
628 error = -ENOENT;
John Johansen01e2b672013-07-10 21:06:43 -0700629 aa_put_profile(root);
John Johansen898127c2010-07-29 14:48:06 -0700630 goto out;
631 }
632
633 /*
634 * In complain mode and failed to match any hats.
635 * Audit the failure is based off of the first hat
636 * supplied. This is done due how userspace
637 * interacts with change_hat.
638 *
639 * TODO: Add logging of all failed hats
640 */
641
642 /* freed below */
643 name = new_compound_name(root->base.hname, hats[0]);
John Johansen01e2b672013-07-10 21:06:43 -0700644 aa_put_profile(root);
John Johansen898127c2010-07-29 14:48:06 -0700645 target = name;
646 /* released below */
John Johansen181f7c92017-01-16 00:42:36 -0800647 hat = aa_new_null_profile(profile, true, hats[0],
648 GFP_KERNEL);
John Johansen898127c2010-07-29 14:48:06 -0700649 if (!hat) {
650 info = "failed null profile create";
651 error = -ENOMEM;
652 goto audit;
653 }
654 } else {
John Johansen01e2b672013-07-10 21:06:43 -0700655 aa_put_profile(root);
John Johansen898127c2010-07-29 14:48:06 -0700656 target = hat->base.hname;
657 if (!PROFILE_IS_HAT(hat)) {
658 info = "target not hat";
659 error = -EPERM;
660 goto audit;
661 }
662 }
663
Oleg Nesterov51775fe2013-10-08 05:46:03 -0700664 error = may_change_ptraced_domain(hat);
John Johansen898127c2010-07-29 14:48:06 -0700665 if (error) {
666 info = "ptraced";
667 error = -EPERM;
668 goto audit;
669 }
670
John Johansendf8073c2017-06-09 11:36:48 -0700671 if (!(flags & AA_CHANGE_TEST)) {
John Johansen637f6882017-06-09 08:14:28 -0700672 error = aa_set_current_hat(&hat->label, token);
John Johansen898127c2010-07-29 14:48:06 -0700673 if (error == -EACCES)
674 /* kill task in case of brute force attacks */
675 perms.kill = AA_MAY_CHANGEHAT;
676 else if (name && !error)
677 /* reset error for learning of new hats */
678 error = -ENOENT;
679 }
John Johansen637f6882017-06-09 08:14:28 -0700680 } else if (previous_label) {
John Johansen898127c2010-07-29 14:48:06 -0700681 /* Return to saved profile. Kill task if restore fails
682 * to avoid brute force attacks
683 */
John Johansen637f6882017-06-09 08:14:28 -0700684 target = previous_label->hname;
685 error = aa_restore_previous_label(token);
John Johansen898127c2010-07-29 14:48:06 -0700686 perms.kill = AA_MAY_CHANGEHAT;
687 } else
688 /* ignore restores when there is no saved profile */
689 goto out;
690
691audit:
John Johansendf8073c2017-06-09 11:36:48 -0700692 if (!(flags & AA_CHANGE_TEST))
John Johansenef88a7a2017-01-16 00:43:02 -0800693 error = aa_audit_file(profile, &perms, OP_CHANGE_HAT,
694 AA_MAY_CHANGEHAT, NULL, target,
695 GLOBAL_ROOT_UID, info, error);
John Johansen898127c2010-07-29 14:48:06 -0700696
697out:
698 aa_put_profile(hat);
699 kfree(name);
John Johansen637f6882017-06-09 08:14:28 -0700700 aa_put_label(label);
701 aa_put_label(previous_label);
John Johansen898127c2010-07-29 14:48:06 -0700702 put_cred(cred);
703
704 return error;
705}
706
707/**
708 * aa_change_profile - perform a one-way profile transition
John Johansenaa9a39a2017-01-16 00:43:06 -0800709 * @fqname: name of profile may include namespace (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700710 * @onexec: whether this transition is to take place immediately or at exec
John Johansendf8073c2017-06-09 11:36:48 -0700711 * @flags: flags affecting change behavior
John Johansen898127c2010-07-29 14:48:06 -0700712 *
713 * Change to new profile @name. Unlike with hats, there is no way
714 * to change back. If @name isn't specified the current profile name is
715 * used.
716 * If @onexec then the transition is delayed until
717 * the next exec.
718 *
719 * Returns %0 on success, error otherwise.
720 */
John Johansendf8073c2017-06-09 11:36:48 -0700721int aa_change_profile(const char *fqname, int flags)
John Johansen898127c2010-07-29 14:48:06 -0700722{
723 const struct cred *cred;
John Johansen637f6882017-06-09 08:14:28 -0700724 struct aa_label *label;
John Johansen898127c2010-07-29 14:48:06 -0700725 struct aa_profile *profile, *target = NULL;
John Johansen2d679f32017-05-29 12:19:39 -0700726 struct aa_perms perms = {};
John Johansenaa9a39a2017-01-16 00:43:06 -0800727 const char *info = NULL, *op;
John Johansen47f6e5c2017-01-16 00:43:01 -0800728 int error = 0;
John Johansen898127c2010-07-29 14:48:06 -0700729 u32 request;
730
John Johansenaa9a39a2017-01-16 00:43:06 -0800731 if (!fqname || !*fqname) {
732 AA_DEBUG("no profile name");
John Johansen898127c2010-07-29 14:48:06 -0700733 return -EINVAL;
John Johansenaa9a39a2017-01-16 00:43:06 -0800734 }
John Johansen898127c2010-07-29 14:48:06 -0700735
John Johansendf8073c2017-06-09 11:36:48 -0700736 if (flags & AA_CHANGE_ONEXEC) {
John Johansen898127c2010-07-29 14:48:06 -0700737 request = AA_MAY_ONEXEC;
738 op = OP_CHANGE_ONEXEC;
739 } else {
740 request = AA_MAY_CHANGE_PROFILE;
741 op = OP_CHANGE_PROFILE;
742 }
743
744 cred = get_current_cred();
John Johansen637f6882017-06-09 08:14:28 -0700745 label = aa_get_newest_cred_label(cred);
746 profile = labels_profile(label);
John Johansen898127c2010-07-29 14:48:06 -0700747
John Johansenc29bceb2012-04-12 16:47:51 -0500748 /*
749 * Fail explicitly requested domain transitions if no_new_privs
750 * and not unconfined.
751 * Domain transitions from unconfined are allowed even when
752 * no_new_privs is set because this aways results in a reduction
753 * of permissions.
754 */
John Johansen637f6882017-06-09 08:14:28 -0700755 if (task_no_new_privs(current) && !profile_unconfined(profile)) {
John Johansenc29bceb2012-04-12 16:47:51 -0500756 put_cred(cred);
757 return -EPERM;
758 }
759
John Johansen637f6882017-06-09 08:14:28 -0700760 target = aa_fqlookupn_profile(label, fqname, strlen(fqname));
John Johansen898127c2010-07-29 14:48:06 -0700761 if (!target) {
762 info = "profile not found";
763 error = -ENOENT;
John Johansendf8073c2017-06-09 11:36:48 -0700764 if ((flags & AA_CHANGE_TEST) ||
765 !COMPLAIN_MODE(profile))
John Johansen898127c2010-07-29 14:48:06 -0700766 goto audit;
767 /* released below */
John Johansenaa9a39a2017-01-16 00:43:06 -0800768 target = aa_new_null_profile(profile, false, fqname,
769 GFP_KERNEL);
John Johansen898127c2010-07-29 14:48:06 -0700770 if (!target) {
771 info = "failed null profile create";
772 error = -ENOMEM;
773 goto audit;
774 }
775 }
776
John Johansenaa9a39a2017-01-16 00:43:06 -0800777 perms = change_profile_perms(profile, target->ns, target->base.hname,
778 request, profile->file.start);
779 if (!(perms.allow & request)) {
780 error = -EACCES;
781 goto audit;
782 }
783
John Johansen898127c2010-07-29 14:48:06 -0700784 /* check if tracing task is allowed to trace target domain */
Oleg Nesterov51775fe2013-10-08 05:46:03 -0700785 error = may_change_ptraced_domain(target);
John Johansen898127c2010-07-29 14:48:06 -0700786 if (error) {
787 info = "ptrace prevents transition";
788 goto audit;
789 }
790
John Johansendf8073c2017-06-09 11:36:48 -0700791 if (flags & AA_CHANGE_TEST)
John Johansen898127c2010-07-29 14:48:06 -0700792 goto audit;
793
John Johansendf8073c2017-06-09 11:36:48 -0700794 if (flags & AA_CHANGE_ONEXEC)
John Johansen637f6882017-06-09 08:14:28 -0700795 error = aa_set_current_onexec(&target->label, 0);
John Johansen898127c2010-07-29 14:48:06 -0700796 else
John Johansen637f6882017-06-09 08:14:28 -0700797 error = aa_replace_current_label(&target->label);
John Johansen898127c2010-07-29 14:48:06 -0700798
799audit:
John Johansendf8073c2017-06-09 11:36:48 -0700800 if (!(flags & AA_CHANGE_TEST))
John Johansenaa9a39a2017-01-16 00:43:06 -0800801 error = aa_audit_file(profile, &perms, op, request, NULL,
802 fqname, GLOBAL_ROOT_UID, info, error);
John Johansen898127c2010-07-29 14:48:06 -0700803
John Johansen898127c2010-07-29 14:48:06 -0700804 aa_put_profile(target);
John Johansen637f6882017-06-09 08:14:28 -0700805 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -0700806 put_cred(cred);
807
808 return error;
809}