blob: 6bcafe8d226d021dfade2f78118dc768d787a5c6 [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>
Matthew Garrett8e51f902018-02-08 12:37:19 -080022#include <linux/xattr.h>
John Johansen898127c2010-07-29 14:48:06 -070023
24#include "include/audit.h"
25#include "include/apparmorfs.h"
John Johansend8889d42017-10-11 01:04:48 -070026#include "include/cred.h"
John Johansen898127c2010-07-29 14:48:06 -070027#include "include/domain.h"
28#include "include/file.h"
29#include "include/ipc.h"
30#include "include/match.h"
31#include "include/path.h"
32#include "include/policy.h"
John Johansencff281f2017-01-16 00:42:15 -080033#include "include/policy_ns.h"
John Johansen898127c2010-07-29 14:48:06 -070034
35/**
36 * aa_free_domain_entries - free entries in a domain table
37 * @domain: the domain table to free (MAYBE NULL)
38 */
39void aa_free_domain_entries(struct aa_domain *domain)
40{
41 int i;
42 if (domain) {
43 if (!domain->table)
44 return;
45
46 for (i = 0; i < domain->size; i++)
47 kzfree(domain->table[i]);
48 kzfree(domain->table);
49 domain->table = NULL;
50 }
51}
52
53/**
54 * may_change_ptraced_domain - check if can change profile on ptraced task
John Johansenb2d09ae2017-06-09 14:22:14 -070055 * @to_label: profile to change to (NOT NULL)
56 * @info: message if there is an error
John Johansen898127c2010-07-29 14:48:06 -070057 *
Oleg Nesterov51775fe2013-10-08 05:46:03 -070058 * Check if current is ptraced and if so if the tracing task is allowed
John Johansen898127c2010-07-29 14:48:06 -070059 * to trace the new domain
60 *
61 * Returns: %0 or error if change not allowed
62 */
John Johansenb2d09ae2017-06-09 14:22:14 -070063static int may_change_ptraced_domain(struct aa_label *to_label,
64 const char **info)
John Johansen898127c2010-07-29 14:48:06 -070065{
66 struct task_struct *tracer;
John Johansen637f6882017-06-09 08:14:28 -070067 struct aa_label *tracerl = NULL;
John Johansen898127c2010-07-29 14:48:06 -070068 int error = 0;
69
70 rcu_read_lock();
Oleg Nesterov51775fe2013-10-08 05:46:03 -070071 tracer = ptrace_parent(current);
John Johansen3cfcc192013-02-18 16:03:34 -080072 if (tracer)
John Johansen898127c2010-07-29 14:48:06 -070073 /* released below */
John Johansen637f6882017-06-09 08:14:28 -070074 tracerl = aa_get_task_label(tracer);
John Johansen898127c2010-07-29 14:48:06 -070075
76 /* not ptraced */
John Johansen637f6882017-06-09 08:14:28 -070077 if (!tracer || unconfined(tracerl))
John Johansen898127c2010-07-29 14:48:06 -070078 goto out;
79
John Johansenb2d09ae2017-06-09 14:22:14 -070080 error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
John Johansen898127c2010-07-29 14:48:06 -070081
82out:
John Johansen04fdc092011-06-28 15:06:38 +010083 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -070084 aa_put_label(tracerl);
John Johansen898127c2010-07-29 14:48:06 -070085
John Johansenb2d09ae2017-06-09 14:22:14 -070086 if (error)
87 *info = "ptrace prevents transition";
John Johansen898127c2010-07-29 14:48:06 -070088 return error;
89}
90
John Johansen93c98a42017-06-09 16:55:04 -070091/**** TODO: dedup to aa_label_match - needs perm and dfa, merging
92 * specifically this is an exact copy of aa_label_match except
93 * aa_compute_perms is replaced with aa_compute_fperms
94 * and policy.dfa with file.dfa
95 ****/
96/* match a profile and its associated ns component if needed
97 * Assumes visibility test has already been done.
98 * If a subns profile is not to be matched should be prescreened with
99 * visibility test.
100 */
101static inline unsigned int match_component(struct aa_profile *profile,
102 struct aa_profile *tp,
103 bool stack, unsigned int state)
104{
105 const char *ns_name;
106
107 if (stack)
108 state = aa_dfa_match(profile->file.dfa, state, "&");
109 if (profile->ns == tp->ns)
110 return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
111
112 /* try matching with namespace name and then profile */
113 ns_name = aa_ns_name(profile->ns, tp->ns, true);
114 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
115 state = aa_dfa_match(profile->file.dfa, state, ns_name);
116 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
117 return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
118}
119
120/**
121 * label_compound_match - find perms for full compound label
122 * @profile: profile to find perms for
123 * @label: label to check access permissions for
124 * @stack: whether this is a stacking request
125 * @start: state to start match in
126 * @subns: whether to do permission checks on components in a subns
127 * @request: permissions to request
128 * @perms: perms struct to set
129 *
130 * Returns: 0 on success else ERROR
131 *
132 * For the label A//&B//&C this does the perm match for A//&B//&C
133 * @perms should be preinitialized with allperms OR a previous permission
134 * check to be stacked.
135 */
136static int label_compound_match(struct aa_profile *profile,
137 struct aa_label *label, bool stack,
138 unsigned int state, bool subns, u32 request,
139 struct aa_perms *perms)
140{
141 struct aa_profile *tp;
142 struct label_it i;
143 struct path_cond cond = { };
144
145 /* find first subcomponent that is visible */
146 label_for_each(i, label, tp) {
147 if (!aa_ns_visible(profile->ns, tp->ns, subns))
148 continue;
149 state = match_component(profile, tp, stack, state);
150 if (!state)
151 goto fail;
152 goto next;
153 }
154
155 /* no component visible */
156 *perms = allperms;
157 return 0;
158
159next:
160 label_for_each_cont(i, label, tp) {
161 if (!aa_ns_visible(profile->ns, tp->ns, subns))
162 continue;
163 state = aa_dfa_match(profile->file.dfa, state, "//&");
164 state = match_component(profile, tp, false, state);
165 if (!state)
166 goto fail;
167 }
168 *perms = aa_compute_fperms(profile->file.dfa, state, &cond);
169 aa_apply_modes_to_perms(profile, perms);
170 if ((perms->allow & request) != request)
171 return -EACCES;
172
173 return 0;
174
175fail:
176 *perms = nullperms;
177 return -EACCES;
178}
179
180/**
181 * label_components_match - find perms for all subcomponents of a label
182 * @profile: profile to find perms for
183 * @label: label to check access permissions for
184 * @stack: whether this is a stacking request
185 * @start: state to start match in
186 * @subns: whether to do permission checks on components in a subns
187 * @request: permissions to request
188 * @perms: an initialized perms struct to add accumulation to
189 *
190 * Returns: 0 on success else ERROR
191 *
192 * For the label A//&B//&C this does the perm match for each of A and B and C
193 * @perms should be preinitialized with allperms OR a previous permission
194 * check to be stacked.
195 */
196static int label_components_match(struct aa_profile *profile,
197 struct aa_label *label, bool stack,
198 unsigned int start, bool subns, u32 request,
199 struct aa_perms *perms)
200{
201 struct aa_profile *tp;
202 struct label_it i;
203 struct aa_perms tmp;
204 struct path_cond cond = { };
205 unsigned int state = 0;
206
207 /* find first subcomponent to test */
208 label_for_each(i, label, tp) {
209 if (!aa_ns_visible(profile->ns, tp->ns, subns))
210 continue;
211 state = match_component(profile, tp, stack, start);
212 if (!state)
213 goto fail;
214 goto next;
215 }
216
217 /* no subcomponents visible - no change in perms */
218 return 0;
219
220next:
221 tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
222 aa_apply_modes_to_perms(profile, &tmp);
223 aa_perms_accum(perms, &tmp);
224 label_for_each_cont(i, label, tp) {
225 if (!aa_ns_visible(profile->ns, tp->ns, subns))
226 continue;
227 state = match_component(profile, tp, stack, start);
228 if (!state)
229 goto fail;
230 tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
231 aa_apply_modes_to_perms(profile, &tmp);
232 aa_perms_accum(perms, &tmp);
233 }
234
235 if ((perms->allow & request) != request)
236 return -EACCES;
237
238 return 0;
239
240fail:
241 *perms = nullperms;
242 return -EACCES;
243}
244
245/**
246 * label_match - do a multi-component label match
247 * @profile: profile to match against (NOT NULL)
248 * @label: label to match (NOT NULL)
249 * @stack: whether this is a stacking request
250 * @state: state to start in
251 * @subns: whether to match subns components
252 * @request: permission request
253 * @perms: Returns computed perms (NOT NULL)
254 *
255 * Returns: the state the match finished in, may be the none matching state
256 */
257static int label_match(struct aa_profile *profile, struct aa_label *label,
258 bool stack, unsigned int state, bool subns, u32 request,
259 struct aa_perms *perms)
260{
261 int error;
262
263 *perms = nullperms;
264 error = label_compound_match(profile, label, stack, state, subns,
265 request, perms);
266 if (!error)
267 return error;
268
269 *perms = allperms;
270 return label_components_match(profile, label, stack, state, subns,
271 request, perms);
272}
273
274/******* end TODO: dedup *****/
275
John Johansen898127c2010-07-29 14:48:06 -0700276/**
277 * change_profile_perms - find permissions for change_profile
278 * @profile: the current profile (NOT NULL)
John Johansen93c98a42017-06-09 16:55:04 -0700279 * @target: label to transition to (NOT NULL)
280 * @stack: whether this is a stacking request
John Johansen898127c2010-07-29 14:48:06 -0700281 * @request: requested perms
282 * @start: state to start matching in
283 *
John Johansen93c98a42017-06-09 16:55:04 -0700284 *
John Johansen898127c2010-07-29 14:48:06 -0700285 * Returns: permission set
John Johansen93c98a42017-06-09 16:55:04 -0700286 *
287 * currently only matches full label A//&B//&C or individual components A, B, C
288 * not arbitrary combinations. Eg. A//&B, C
John Johansen898127c2010-07-29 14:48:06 -0700289 */
John Johansen93c98a42017-06-09 16:55:04 -0700290static int change_profile_perms(struct aa_profile *profile,
291 struct aa_label *target, bool stack,
292 u32 request, unsigned int start,
293 struct aa_perms *perms)
294{
295 if (profile_unconfined(profile)) {
296 perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
297 perms->audit = perms->quiet = perms->kill = 0;
298 return 0;
299 }
300
301 /* TODO: add profile in ns screening */
302 return label_match(profile, target, stack, start, true, request, perms);
303}
304
John Johansen898127c2010-07-29 14:48:06 -0700305/**
Matthew Garrett8e51f902018-02-08 12:37:19 -0800306 * aa_xattrs_match - check whether a file matches the xattrs defined in profile
307 * @bprm: binprm struct for the process to validate
308 * @profile: profile to match against (NOT NULL)
309 *
310 * Returns: number of extended attributes that matched, or < 0 on error
311 */
312static int aa_xattrs_match(const struct linux_binprm *bprm,
313 struct aa_profile *profile)
314{
315 int i;
316 size_t size;
317 struct dentry *d;
318 char *value = NULL;
319 int value_size = 0, ret = profile->xattr_count;
320
321 if (!bprm || !profile->xattr_count)
322 return 0;
323
324 d = bprm->file->f_path.dentry;
325
326 for (i = 0; i < profile->xattr_count; i++) {
327 size = vfs_getxattr_alloc(d, profile->xattrs[i], &value,
328 value_size, GFP_KERNEL);
329 if (size < 0) {
330 ret = -EINVAL;
331 goto out;
332 }
333
334 /* Check the xattr value, not just presence */
335 if (profile->xattr_lens[i]) {
336 if (profile->xattr_lens[i] != size) {
337 ret = -EINVAL;
338 goto out;
339 }
340
341 if (memcmp(value, profile->xattr_values[i], size)) {
342 ret = -EINVAL;
343 goto out;
344 }
345 }
346 }
347
348out:
349 kfree(value);
350 return ret;
351}
352
353/**
John Johansen898127c2010-07-29 14:48:06 -0700354 * __attach_match_ - find an attachment match
Matthew Garrett8e51f902018-02-08 12:37:19 -0800355 * @bprm - binprm structure of transitioning task
John Johansen898127c2010-07-29 14:48:06 -0700356 * @name - to match against (NOT NULL)
357 * @head - profile list to walk (NOT NULL)
John Johansen844b8292017-11-17 17:42:42 -0800358 * @info - info message if there was an error (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700359 *
360 * Do a linear search on the profiles in the list. There is a matching
361 * preference where an exact match is preferred over a name which uses
362 * expressions to match, and matching expressions with the greatest
363 * xmatch_len are preferred.
364 *
365 * Requires: @head not be shared or have appropriate locks held
366 *
367 * Returns: profile or NULL if no match found
368 */
Matthew Garrett8e51f902018-02-08 12:37:19 -0800369static struct aa_profile *__attach_match(const struct linux_binprm *bprm,
370 const char *name,
John Johansen844b8292017-11-17 17:42:42 -0800371 struct list_head *head,
372 const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700373{
Matthew Garrett8e51f902018-02-08 12:37:19 -0800374 int len = 0, xattrs = 0;
John Johansen844b8292017-11-17 17:42:42 -0800375 bool conflict = false;
John Johansen898127c2010-07-29 14:48:06 -0700376 struct aa_profile *profile, *candidate = NULL;
377
John Johansen01e2b672013-07-10 21:06:43 -0700378 list_for_each_entry_rcu(profile, head, base.list) {
John Johansen06d426d2017-11-17 18:04:37 -0800379 if (profile->label.flags & FLAG_NULL &&
380 &profile->label == ns_unconfined(profile->ns))
John Johansen898127c2010-07-29 14:48:06 -0700381 continue;
John Johansen06d426d2017-11-17 18:04:37 -0800382
Matthew Garrett8e51f902018-02-08 12:37:19 -0800383 /* Find the "best" matching profile. Profiles must
384 * match the path and extended attributes (if any)
385 * associated with the file. A more specific path
386 * match will be preferred over a less specific one,
387 * and a match with more matching extended attributes
388 * will be preferred over one with fewer. If the best
389 * match has both the same level of path specificity
390 * and the same number of matching extended attributes
391 * as another profile, signal a conflict and refuse to
392 * match.
393 */
John Johansen844b8292017-11-17 17:42:42 -0800394 if (profile->xmatch) {
Matthew Garrett8e51f902018-02-08 12:37:19 -0800395 unsigned int state;
396 u32 perm;
John Johansen844b8292017-11-17 17:42:42 -0800397
Matthew Garrett8e51f902018-02-08 12:37:19 -0800398 if (profile->xmatch_len < len)
399 continue;
400
401 state = aa_dfa_match(profile->xmatch,
402 DFA_START, name);
403 perm = dfa_user_allow(profile->xmatch, state);
404 /* any accepting state means a valid match. */
405 if (perm & MAY_EXEC) {
406 int ret = aa_xattrs_match(bprm, profile);
407
408 /* Fail matching if the xattrs don't match */
409 if (ret < 0)
410 continue;
411
412 /* The new match isn't more specific
413 * than the current best match
414 */
415 if (profile->xmatch_len == len &&
416 ret <= xattrs) {
417 /* Match is equivalent, so conflict */
418 if (ret == xattrs)
Matthew Garrett1a3881d2018-01-11 13:07:54 -0800419 conflict = true;
Matthew Garrett8e51f902018-02-08 12:37:19 -0800420 continue;
John Johansen844b8292017-11-17 17:42:42 -0800421 }
Matthew Garrett8e51f902018-02-08 12:37:19 -0800422
423 /* Either the same length with more matching
424 * xattrs, or a longer match
425 */
426 candidate = profile;
427 len = profile->xmatch_len;
428 xattrs = ret;
429 conflict = false;
John Johansen898127c2010-07-29 14:48:06 -0700430 }
Matthew Garrett8e51f902018-02-08 12:37:19 -0800431 } else if (!strcmp(profile->base.name, name) &&
432 aa_xattrs_match(bprm, profile) >= 0)
John Johansen898127c2010-07-29 14:48:06 -0700433 /* exact non-re match, no more searching required */
434 return profile;
435 }
436
John Johansen844b8292017-11-17 17:42:42 -0800437 if (conflict) {
438 *info = "conflicting profile attachments";
439 return NULL;
440 }
441
John Johansen898127c2010-07-29 14:48:06 -0700442 return candidate;
443}
444
445/**
446 * find_attach - do attachment search for unconfined processes
Matthew Garrett8e51f902018-02-08 12:37:19 -0800447 * @bprm - binprm structure of transitioning task
John Johansen898127c2010-07-29 14:48:06 -0700448 * @ns: the current namespace (NOT NULL)
449 * @list: list to search (NOT NULL)
450 * @name: the executable name to match against (NOT NULL)
John Johansen844b8292017-11-17 17:42:42 -0800451 * @info: info message if there was an error
John Johansen898127c2010-07-29 14:48:06 -0700452 *
John Johansen93c98a42017-06-09 16:55:04 -0700453 * Returns: label or NULL if no match found
John Johansen898127c2010-07-29 14:48:06 -0700454 */
Matthew Garrett8e51f902018-02-08 12:37:19 -0800455static struct aa_label *find_attach(const struct linux_binprm *bprm,
456 struct aa_ns *ns, struct list_head *list,
John Johansen844b8292017-11-17 17:42:42 -0800457 const char *name, const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700458{
459 struct aa_profile *profile;
460
John Johansen01e2b672013-07-10 21:06:43 -0700461 rcu_read_lock();
Matthew Garrett8e51f902018-02-08 12:37:19 -0800462 profile = aa_get_profile(__attach_match(bprm, name, list, info));
John Johansen01e2b672013-07-10 21:06:43 -0700463 rcu_read_unlock();
John Johansen898127c2010-07-29 14:48:06 -0700464
John Johansen93c98a42017-06-09 16:55:04 -0700465 return profile ? &profile->label : NULL;
John Johansen898127c2010-07-29 14:48:06 -0700466}
467
468static const char *next_name(int xtype, const char *name)
469{
470 return NULL;
471}
472
473/**
474 * x_table_lookup - lookup an x transition name via transition table
475 * @profile: current profile (NOT NULL)
476 * @xindex: index into x transition table
John Johansen93c98a42017-06-09 16:55:04 -0700477 * @name: returns: name tested to find label (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700478 *
John Johansen93c98a42017-06-09 16:55:04 -0700479 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
John Johansen898127c2010-07-29 14:48:06 -0700480 */
John Johansen2ea3ffb2017-07-18 23:04:47 -0700481struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
482 const char **name)
John Johansen898127c2010-07-29 14:48:06 -0700483{
John Johansen93c98a42017-06-09 16:55:04 -0700484 struct aa_label *label = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700485 u32 xtype = xindex & AA_X_TYPE_MASK;
486 int index = xindex & AA_X_INDEX_MASK;
John Johansen93c98a42017-06-09 16:55:04 -0700487
488 AA_BUG(!name);
John Johansen898127c2010-07-29 14:48:06 -0700489
490 /* index is guaranteed to be in range, validated at load time */
John Johansen93c98a42017-06-09 16:55:04 -0700491 /* TODO: move lookup parsing to unpack time so this is a straight
492 * index into the resultant label
493 */
494 for (*name = profile->file.trans.table[index]; !label && *name;
495 *name = next_name(xtype, *name)) {
John Johansen898127c2010-07-29 14:48:06 -0700496 if (xindex & AA_X_CHILD) {
John Johansen93c98a42017-06-09 16:55:04 -0700497 struct aa_profile *new_profile;
John Johansen898127c2010-07-29 14:48:06 -0700498 /* release by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700499 new_profile = aa_find_child(profile, *name);
500 if (new_profile)
501 label = &new_profile->label;
John Johansen898127c2010-07-29 14:48:06 -0700502 continue;
John Johansen898127c2010-07-29 14:48:06 -0700503 }
John Johansen93c98a42017-06-09 16:55:04 -0700504 label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
505 true, false);
506 if (IS_ERR(label))
507 label = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700508 }
509
510 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700511
512 return label;
John Johansen898127c2010-07-29 14:48:06 -0700513}
514
515/**
John Johansen93c98a42017-06-09 16:55:04 -0700516 * x_to_label - get target label for a given xindex
John Johansen898127c2010-07-29 14:48:06 -0700517 * @profile: current profile (NOT NULL)
Matthew Garrett8e51f902018-02-08 12:37:19 -0800518 * @bprm: binprm structure of transitioning task
John Johansen898127c2010-07-29 14:48:06 -0700519 * @name: name to lookup (NOT NULL)
520 * @xindex: index into x transition table
John Johansen93c98a42017-06-09 16:55:04 -0700521 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -0700522 *
John Johansen93c98a42017-06-09 16:55:04 -0700523 * find label for a transition index
John Johansen898127c2010-07-29 14:48:06 -0700524 *
John Johansen93c98a42017-06-09 16:55:04 -0700525 * Returns: refcounted label or NULL if not found available
John Johansen898127c2010-07-29 14:48:06 -0700526 */
John Johansen93c98a42017-06-09 16:55:04 -0700527static struct aa_label *x_to_label(struct aa_profile *profile,
Matthew Garrett8e51f902018-02-08 12:37:19 -0800528 const struct linux_binprm *bprm,
John Johansen93c98a42017-06-09 16:55:04 -0700529 const char *name, u32 xindex,
530 const char **lookupname,
531 const char **info)
John Johansen898127c2010-07-29 14:48:06 -0700532{
John Johansen93c98a42017-06-09 16:55:04 -0700533 struct aa_label *new = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800534 struct aa_ns *ns = profile->ns;
John Johansen898127c2010-07-29 14:48:06 -0700535 u32 xtype = xindex & AA_X_TYPE_MASK;
John Johansen93c98a42017-06-09 16:55:04 -0700536 const char *stack = NULL;
John Johansen898127c2010-07-29 14:48:06 -0700537
538 switch (xtype) {
539 case AA_X_NONE:
540 /* fail exec unless ix || ux fallback - handled by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700541 *lookupname = NULL;
542 break;
543 case AA_X_TABLE:
544 /* TODO: fix when perm mapping done at unload */
545 stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
546 if (*stack != '&') {
547 /* released by caller */
548 new = x_table_lookup(profile, xindex, lookupname);
549 stack = NULL;
550 break;
551 }
552 /* fall through to X_NAME */
John Johansen898127c2010-07-29 14:48:06 -0700553 case AA_X_NAME:
554 if (xindex & AA_X_CHILD)
555 /* released by caller */
Matthew Garrett8e51f902018-02-08 12:37:19 -0800556 new = find_attach(bprm, ns, &profile->base.profiles,
John Johansen844b8292017-11-17 17:42:42 -0800557 name, info);
John Johansen898127c2010-07-29 14:48:06 -0700558 else
559 /* released by caller */
Matthew Garrett8e51f902018-02-08 12:37:19 -0800560 new = find_attach(bprm, ns, &ns->base.profiles,
John Johansen844b8292017-11-17 17:42:42 -0800561 name, info);
John Johansen93c98a42017-06-09 16:55:04 -0700562 *lookupname = name;
John Johansen898127c2010-07-29 14:48:06 -0700563 break;
564 }
565
John Johansen93c98a42017-06-09 16:55:04 -0700566 if (!new) {
567 if (xindex & AA_X_INHERIT) {
568 /* (p|c|n)ix - don't change profile but do
569 * use the newest version
570 */
571 *info = "ix fallback";
572 /* no profile && no error */
573 new = aa_get_newest_label(&profile->label);
574 } else if (xindex & AA_X_UNCONFINED) {
575 new = aa_get_newest_label(ns_unconfined(profile->ns));
576 *info = "ux fallback";
577 }
578 }
579
580 if (new && stack) {
581 /* base the stack on post domain transition */
582 struct aa_label *base = new;
583
584 new = aa_label_parse(base, stack, GFP_ATOMIC, true, false);
585 if (IS_ERR(new))
586 new = NULL;
587 aa_put_label(base);
588 }
589
John Johansen898127c2010-07-29 14:48:06 -0700590 /* released by caller */
John Johansen93c98a42017-06-09 16:55:04 -0700591 return new;
592}
593
594static struct aa_label *profile_transition(struct aa_profile *profile,
595 const struct linux_binprm *bprm,
596 char *buffer, struct path_cond *cond,
597 bool *secure_exec)
598{
599 struct aa_label *new = NULL;
Matthew Garrett8e51f902018-02-08 12:37:19 -0800600 struct aa_profile *component;
601 struct label_it i;
John Johansen93c98a42017-06-09 16:55:04 -0700602 const char *info = NULL, *name = NULL, *target = NULL;
603 unsigned int state = profile->file.start;
604 struct aa_perms perms = {};
605 bool nonewprivs = false;
606 int error = 0;
607
608 AA_BUG(!profile);
609 AA_BUG(!bprm);
610 AA_BUG(!buffer);
611
612 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
613 &name, &info, profile->disconnected);
614 if (error) {
615 if (profile_unconfined(profile) ||
616 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
617 AA_DEBUG("name lookup ix on error");
618 error = 0;
619 new = aa_get_newest_label(&profile->label);
620 }
621 name = bprm->filename;
622 goto audit;
623 }
624
625 if (profile_unconfined(profile)) {
Matthew Garrett8e51f902018-02-08 12:37:19 -0800626 new = find_attach(bprm, profile->ns,
627 &profile->ns->base.profiles, name, &info);
John Johansen93c98a42017-06-09 16:55:04 -0700628 if (new) {
629 AA_DEBUG("unconfined attached to new label");
630 return new;
631 }
632 AA_DEBUG("unconfined exec no attachment");
633 return aa_get_newest_label(&profile->label);
634 }
635
636 /* find exec permissions for name */
637 state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
638 if (perms.allow & MAY_EXEC) {
639 /* exec permission determine how to transition */
Matthew Garrett8e51f902018-02-08 12:37:19 -0800640 new = x_to_label(profile, bprm, name, perms.xindex, &target,
641 &info);
John Johansen93c98a42017-06-09 16:55:04 -0700642 if (new && new->proxy == profile->label.proxy && info) {
643 /* hack ix fallback - improve how this is detected */
644 goto audit;
645 } else if (!new) {
646 error = -EACCES;
647 info = "profile transition not found";
648 /* remove MAY_EXEC to audit as failure */
649 perms.allow &= ~MAY_EXEC;
Matthew Garrett8e51f902018-02-08 12:37:19 -0800650 } else {
651 /* verify that each component's xattr requirements are
652 * met, and fail execution otherwise
653 */
654 label_for_each(i, new, component) {
655 if (aa_xattrs_match(bprm, component) < 0) {
656 error = -EACCES;
657 info = "required xattrs not present";
658 perms.allow &= ~MAY_EXEC;
659 aa_put_label(new);
660 new = NULL;
661 goto audit;
662 }
663 }
John Johansen93c98a42017-06-09 16:55:04 -0700664 }
665 } else if (COMPLAIN_MODE(profile)) {
666 /* no exec permission - learning mode */
John Johansen5d7c44e2017-11-20 22:26:12 -0800667 struct aa_profile *new_profile = NULL;
668 char *n = kstrdup(name, GFP_ATOMIC);
669
670 if (n) {
671 /* name is ptr into buffer */
672 long pos = name - buffer;
673 /* break per cpu buffer hold */
674 put_buffers(buffer);
675 new_profile = aa_new_null_profile(profile, false, n,
676 GFP_KERNEL);
677 get_buffers(buffer);
678 name = buffer + pos;
679 strcpy((char *)name, n);
680 kfree(n);
681 }
John Johansen93c98a42017-06-09 16:55:04 -0700682 if (!new_profile) {
683 error = -ENOMEM;
684 info = "could not create null profile";
685 } else {
686 error = -EACCES;
687 new = &new_profile->label;
688 }
689 perms.xindex |= AA_X_UNSAFE;
690 } else
691 /* fail exec */
692 error = -EACCES;
693
694 if (!new)
695 goto audit;
696
John Johansen93c98a42017-06-09 16:55:04 -0700697
698 if (!(perms.xindex & AA_X_UNSAFE)) {
699 if (DEBUG_ON) {
700 dbg_printk("apparmor: scrubbing environment variables"
701 " for %s profile=", name);
702 aa_label_printk(new, GFP_ATOMIC);
703 dbg_printk("\n");
704 }
705 *secure_exec = true;
706 }
707
708audit:
709 aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
710 cond->uid, info, error);
711 if (!new || nonewprivs) {
712 aa_put_label(new);
713 return ERR_PTR(error);
714 }
715
716 return new;
717}
718
719static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
720 bool stack, const struct linux_binprm *bprm,
721 char *buffer, struct path_cond *cond,
722 bool *secure_exec)
723{
724 unsigned int state = profile->file.start;
725 struct aa_perms perms = {};
726 const char *xname = NULL, *info = "change_profile onexec";
727 int error = -EACCES;
728
729 AA_BUG(!profile);
730 AA_BUG(!onexec);
731 AA_BUG(!bprm);
732 AA_BUG(!buffer);
733
734 if (profile_unconfined(profile)) {
735 /* change_profile on exec already granted */
736 /*
737 * NOTE: Domain transitions from unconfined are allowed
738 * even when no_new_privs is set because this aways results
739 * in a further reduction of permissions.
740 */
741 return 0;
742 }
743
744 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
745 &xname, &info, profile->disconnected);
746 if (error) {
747 if (profile_unconfined(profile) ||
748 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
749 AA_DEBUG("name lookup ix on error");
750 error = 0;
751 }
752 xname = bprm->filename;
753 goto audit;
754 }
755
756 /* find exec permissions for name */
757 state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
758 if (!(perms.allow & AA_MAY_ONEXEC)) {
759 info = "no change_onexec valid for executable";
760 goto audit;
761 }
762 /* test if this exec can be paired with change_profile onexec.
763 * onexec permission is linked to exec with a standard pairing
764 * exec\0change_profile
765 */
766 state = aa_dfa_null_transition(profile->file.dfa, state);
767 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
768 state, &perms);
769 if (error) {
770 perms.allow &= ~AA_MAY_ONEXEC;
771 goto audit;
772 }
John Johansen93c98a42017-06-09 16:55:04 -0700773
774 if (!(perms.xindex & AA_X_UNSAFE)) {
775 if (DEBUG_ON) {
776 dbg_printk("apparmor: scrubbing environment "
777 "variables for %s label=", xname);
778 aa_label_printk(onexec, GFP_ATOMIC);
779 dbg_printk("\n");
780 }
781 *secure_exec = true;
782 }
783
784audit:
785 return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
786 NULL, onexec, cond->uid, info, error);
787}
788
789/* ensure none ns domain transitions are correctly applied with onexec */
790
791static struct aa_label *handle_onexec(struct aa_label *label,
792 struct aa_label *onexec, bool stack,
793 const struct linux_binprm *bprm,
794 char *buffer, struct path_cond *cond,
795 bool *unsafe)
796{
797 struct aa_profile *profile;
798 struct aa_label *new;
799 int error;
800
801 AA_BUG(!label);
802 AA_BUG(!onexec);
803 AA_BUG(!bprm);
804 AA_BUG(!buffer);
805
806 if (!stack) {
807 error = fn_for_each_in_ns(label, profile,
808 profile_onexec(profile, onexec, stack,
809 bprm, buffer, cond, unsafe));
810 if (error)
811 return ERR_PTR(error);
812 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
813 aa_get_newest_label(onexec),
814 profile_transition(profile, bprm, buffer,
815 cond, unsafe));
816
817 } else {
818 /* TODO: determine how much we want to losen this */
819 error = fn_for_each_in_ns(label, profile,
820 profile_onexec(profile, onexec, stack, bprm,
821 buffer, cond, unsafe));
822 if (error)
823 return ERR_PTR(error);
824 new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
825 aa_label_merge(&profile->label, onexec,
826 GFP_ATOMIC),
827 profile_transition(profile, bprm, buffer,
828 cond, unsafe));
829 }
830
831 if (new)
832 return new;
833
834 /* TODO: get rid of GLOBAL_ROOT_UID */
835 error = fn_for_each_in_ns(label, profile,
836 aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
837 AA_MAY_ONEXEC, bprm->filename, NULL,
838 onexec, GLOBAL_ROOT_UID,
839 "failed to build target label", -ENOMEM));
840 return ERR_PTR(error);
John Johansen898127c2010-07-29 14:48:06 -0700841}
842
843/**
844 * apparmor_bprm_set_creds - set the new creds on the bprm struct
845 * @bprm: binprm for the exec (NOT NULL)
846 *
847 * Returns: %0 or error on failure
John Johansen93c98a42017-06-09 16:55:04 -0700848 *
849 * TODO: once the other paths are done see if we can't refactor into a fn
John Johansen898127c2010-07-29 14:48:06 -0700850 */
851int apparmor_bprm_set_creds(struct linux_binprm *bprm)
852{
John Johansenf1752212017-01-27 04:09:40 -0800853 struct aa_task_ctx *ctx;
John Johansen93c98a42017-06-09 16:55:04 -0700854 struct aa_label *label, *new = NULL;
855 struct aa_profile *profile;
John Johansen898127c2010-07-29 14:48:06 -0700856 char *buffer = NULL;
John Johansen93c98a42017-06-09 16:55:04 -0700857 const char *info = NULL;
858 int error = 0;
859 bool unsafe = false;
John Johansen898127c2010-07-29 14:48:06 -0700860 struct path_cond cond = {
Al Viro496ad9a2013-01-23 17:07:38 -0500861 file_inode(bprm->file)->i_uid,
862 file_inode(bprm->file)->i_mode
John Johansen898127c2010-07-29 14:48:06 -0700863 };
John Johansen898127c2010-07-29 14:48:06 -0700864
Kees Cookddb4a142017-07-18 15:25:23 -0700865 if (bprm->called_set_creds)
John Johansen898127c2010-07-29 14:48:06 -0700866 return 0;
867
John Johansende62de52017-10-08 00:43:02 -0700868 ctx = task_ctx(current);
John Johansend9087c42017-01-27 03:53:53 -0800869 AA_BUG(!cred_label(bprm->cred));
John Johansenf1752212017-01-27 04:09:40 -0800870 AA_BUG(!ctx);
John Johansen898127c2010-07-29 14:48:06 -0700871
John Johansend9087c42017-01-27 03:53:53 -0800872 label = aa_get_newest_label(cred_label(bprm->cred));
John Johansen4227c332017-05-23 03:25:14 -0700873
John Johansen9fcf78c2017-10-08 18:26:19 -0700874 /*
875 * Detect no new privs being set, and store the label it
876 * occurred under. Ideally this would happen when nnp
877 * is set but there isn't a good way to do that yet.
878 *
879 * Testing for unconfined must be done before the subset test
880 */
881 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
882 !ctx->nnp)
883 ctx->nnp = aa_get_label(label);
884
John Johansen4227c332017-05-23 03:25:14 -0700885 /* buffer freed below, name is pointer into buffer */
886 get_buffers(buffer);
John Johansen93c98a42017-06-09 16:55:04 -0700887 /* Test for onexec first as onexec override other x transitions. */
John Johansenf1752212017-01-27 04:09:40 -0800888 if (ctx->onexec)
889 new = handle_onexec(label, ctx->onexec, ctx->token,
John Johansen93c98a42017-06-09 16:55:04 -0700890 bprm, buffer, &cond, &unsafe);
891 else
892 new = fn_label_build(label, profile, GFP_ATOMIC,
893 profile_transition(profile, bprm, buffer,
894 &cond, &unsafe));
John Johansen898127c2010-07-29 14:48:06 -0700895
John Johansen93c98a42017-06-09 16:55:04 -0700896 AA_BUG(!new);
897 if (IS_ERR(new)) {
898 error = PTR_ERR(new);
899 goto done;
900 } else if (!new) {
901 error = -ENOMEM;
902 goto done;
John Johansen898127c2010-07-29 14:48:06 -0700903 }
904
John Johansen9fcf78c2017-10-08 18:26:19 -0700905 /* Policy has specified a domain transitions. If no_new_privs and
906 * confined ensure the transition is to confinement that is subset
907 * of the confinement when the task entered no new privs.
908 *
909 * NOTE: Domain transitions from unconfined and to stacked
910 * subsets are allowed even when no_new_privs is set because this
911 * aways results in a further reduction of permissions.
912 */
913 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
914 !unconfined(label) && !aa_label_is_subset(new, ctx->nnp)) {
915 error = -EPERM;
916 info = "no new privs";
917 goto audit;
918 }
John Johansen898127c2010-07-29 14:48:06 -0700919
920 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
921 /* FIXME: currently don't mediate shared state */
922 ;
923 }
924
John Johansen93c98a42017-06-09 16:55:04 -0700925 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
926 /* TODO: test needs to be profile of label to new */
927 error = may_change_ptraced_domain(new, &info);
John Johansenf7da2de2016-04-20 14:18:18 -0700928 if (error)
John Johansen898127c2010-07-29 14:48:06 -0700929 goto audit;
John Johansen898127c2010-07-29 14:48:06 -0700930 }
931
John Johansen93c98a42017-06-09 16:55:04 -0700932 if (unsafe) {
933 if (DEBUG_ON) {
934 dbg_printk("scrubbing environment variables for %s "
935 "label=", bprm->filename);
936 aa_label_printk(new, GFP_ATOMIC);
937 dbg_printk("\n");
938 }
Kees Cook993b3ab2017-07-18 15:25:24 -0700939 bprm->secureexec = 1;
John Johansen898127c2010-07-29 14:48:06 -0700940 }
John Johansen898127c2010-07-29 14:48:06 -0700941
John Johansen93c98a42017-06-09 16:55:04 -0700942 if (label->proxy != new->proxy) {
943 /* when transitioning clear unsafe personality bits */
944 if (DEBUG_ON) {
945 dbg_printk("apparmor: clearing unsafe personality "
946 "bits. %s label=", bprm->filename);
947 aa_label_printk(new, GFP_ATOMIC);
948 dbg_printk("\n");
949 }
950 bprm->per_clear |= PER_CLEAR_ON_SETID;
951 }
John Johansend9087c42017-01-27 03:53:53 -0800952 aa_put_label(cred_label(bprm->cred));
953 /* transfer reference, released when cred is freed */
954 cred_label(bprm->cred) = new;
John Johansen898127c2010-07-29 14:48:06 -0700955
John Johansen93c98a42017-06-09 16:55:04 -0700956done:
John Johansen637f6882017-06-09 08:14:28 -0700957 aa_put_label(label);
John Johansen4227c332017-05-23 03:25:14 -0700958 put_buffers(buffer);
John Johansen898127c2010-07-29 14:48:06 -0700959
960 return error;
John Johansen93c98a42017-06-09 16:55:04 -0700961
962audit:
963 error = fn_for_each(label, profile,
964 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
965 bprm->filename, NULL, new,
966 file_inode(bprm->file)->i_uid, info,
967 error));
968 aa_put_label(new);
969 goto done;
John Johansen898127c2010-07-29 14:48:06 -0700970}
971
John Johansen898127c2010-07-29 14:48:06 -0700972/*
973 * Functions for self directed profile change
974 */
975
John Johansen89dbf192017-06-09 17:01:43 -0700976
977/* helper fn for change_hat
John Johansen898127c2010-07-29 14:48:06 -0700978 *
John Johansen89dbf192017-06-09 17:01:43 -0700979 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
John Johansen898127c2010-07-29 14:48:06 -0700980 */
John Johansen89dbf192017-06-09 17:01:43 -0700981static struct aa_label *build_change_hat(struct aa_profile *profile,
982 const char *name, bool sibling)
John Johansen898127c2010-07-29 14:48:06 -0700983{
John Johansen89dbf192017-06-09 17:01:43 -0700984 struct aa_profile *root, *hat = NULL;
985 const char *info = NULL;
986 int error = 0;
987
988 if (sibling && PROFILE_IS_HAT(profile)) {
989 root = aa_get_profile_rcu(&profile->parent);
990 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
991 root = aa_get_profile(profile);
992 } else {
993 info = "conflicting target types";
994 error = -EPERM;
995 goto audit;
996 }
997
998 hat = aa_find_child(root, name);
999 if (!hat) {
1000 error = -ENOENT;
1001 if (COMPLAIN_MODE(profile)) {
1002 hat = aa_new_null_profile(profile, true, name,
1003 GFP_KERNEL);
1004 if (!hat) {
1005 info = "failed null profile create";
1006 error = -ENOMEM;
1007 }
1008 }
1009 }
1010 aa_put_profile(root);
1011
1012audit:
1013 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
1014 name, hat ? hat->base.hname : NULL,
1015 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, NULL,
1016 error);
1017 if (!hat || (error && error != -ENOENT))
1018 return ERR_PTR(error);
1019 /* if hat && error - complain mode, already audited and we adjust for
1020 * complain mode allow by returning hat->label
1021 */
1022 return &hat->label;
1023}
1024
1025/* helper fn for changing into a hat
1026 *
1027 * Returns: label for hat transition or ERR_PTR. Does not return NULL
1028 */
1029static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
1030 int count, int flags)
1031{
1032 struct aa_profile *profile, *root, *hat = NULL;
1033 struct aa_label *new;
1034 struct label_it it;
1035 bool sibling = false;
1036 const char *name, *info = NULL;
1037 int i, error;
1038
1039 AA_BUG(!label);
1040 AA_BUG(!hats);
1041 AA_BUG(count < 1);
1042
1043 if (PROFILE_IS_HAT(labels_profile(label)))
1044 sibling = true;
1045
1046 /*find first matching hat */
1047 for (i = 0; i < count && !hat; i++) {
1048 name = hats[i];
1049 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1050 if (sibling && PROFILE_IS_HAT(profile)) {
1051 root = aa_get_profile_rcu(&profile->parent);
1052 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
1053 root = aa_get_profile(profile);
1054 } else { /* conflicting change type */
1055 info = "conflicting targets types";
1056 error = -EPERM;
1057 goto fail;
1058 }
1059 hat = aa_find_child(root, name);
1060 aa_put_profile(root);
1061 if (!hat) {
1062 if (!COMPLAIN_MODE(profile))
1063 goto outer_continue;
1064 /* complain mode succeed as if hat */
1065 } else if (!PROFILE_IS_HAT(hat)) {
1066 info = "target not hat";
1067 error = -EPERM;
1068 aa_put_profile(hat);
1069 goto fail;
1070 }
1071 aa_put_profile(hat);
1072 }
1073 /* found a hat for all profiles in ns */
1074 goto build;
1075outer_continue:
1076 ;
1077 }
1078 /* no hats that match, find appropriate error
1079 *
1080 * In complain mode audit of the failure is based off of the first
1081 * hat supplied. This is done due how userspace interacts with
1082 * change_hat.
1083 */
1084 name = NULL;
1085 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1086 if (!list_empty(&profile->base.profiles)) {
1087 info = "hat not found";
1088 error = -ENOENT;
1089 goto fail;
1090 }
1091 }
1092 info = "no hats defined";
1093 error = -ECHILD;
1094
1095fail:
1096 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1097 /*
1098 * no target as it has failed to be found or built
1099 *
1100 * change_hat uses probing and should not log failures
1101 * related to missing hats
1102 */
1103 /* TODO: get rid of GLOBAL_ROOT_UID */
1104 if (count > 1 || COMPLAIN_MODE(profile)) {
1105 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
1106 AA_MAY_CHANGEHAT, name, NULL, NULL,
1107 GLOBAL_ROOT_UID, info, error);
1108 }
1109 }
1110 return ERR_PTR(error);
1111
1112build:
1113 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1114 build_change_hat(profile, name, sibling),
1115 aa_get_label(&profile->label));
1116 if (!new) {
1117 info = "label build failed";
1118 error = -ENOMEM;
1119 goto fail;
1120 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1121
1122 return new;
John Johansen898127c2010-07-29 14:48:06 -07001123}
1124
1125/**
1126 * aa_change_hat - change hat to/from subprofile
1127 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1128 * @count: number of hat names in @hats
1129 * @token: magic value to validate the hat change
John Johansendf8073c2017-06-09 11:36:48 -07001130 * @flags: flags affecting behavior of the change
John Johansen898127c2010-07-29 14:48:06 -07001131 *
John Johansen89dbf192017-06-09 17:01:43 -07001132 * Returns %0 on success, error otherwise.
1133 *
John Johansen898127c2010-07-29 14:48:06 -07001134 * Change to the first profile specified in @hats that exists, and store
1135 * the @hat_magic in the current task context. If the count == 0 and the
1136 * @token matches that stored in the current task context, return to the
1137 * top level profile.
1138 *
John Johansen89dbf192017-06-09 17:01:43 -07001139 * change_hat only applies to profiles in the current ns, and each profile
1140 * in the ns must make the same transition otherwise change_hat will fail.
John Johansen898127c2010-07-29 14:48:06 -07001141 */
John Johansendf8073c2017-06-09 11:36:48 -07001142int aa_change_hat(const char *hats[], int count, u64 token, int flags)
John Johansen898127c2010-07-29 14:48:06 -07001143{
1144 const struct cred *cred;
John Johansen9fcf78c2017-10-08 18:26:19 -07001145 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen89dbf192017-06-09 17:01:43 -07001146 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1147 struct aa_profile *profile;
John Johansen2d679f32017-05-29 12:19:39 -07001148 struct aa_perms perms = {};
John Johansen89dbf192017-06-09 17:01:43 -07001149 const char *info = NULL;
John Johansen898127c2010-07-29 14:48:06 -07001150 int error = 0;
1151
1152 /* released below */
1153 cred = get_current_cred();
John Johansen637f6882017-06-09 08:14:28 -07001154 label = aa_get_newest_cred_label(cred);
John Johansenf1752212017-01-27 04:09:40 -08001155 previous = aa_get_newest_label(ctx->previous);
John Johansen898127c2010-07-29 14:48:06 -07001156
John Johansen9fcf78c2017-10-08 18:26:19 -07001157 /*
1158 * Detect no new privs being set, and store the label it
1159 * occurred under. Ideally this would happen when nnp
1160 * is set but there isn't a good way to do that yet.
1161 *
1162 * Testing for unconfined must be done before the subset test
1163 */
1164 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1165 ctx->nnp = aa_get_label(label);
1166
John Johansen637f6882017-06-09 08:14:28 -07001167 if (unconfined(label)) {
John Johansen89dbf192017-06-09 17:01:43 -07001168 info = "unconfined can not change_hat";
John Johansen898127c2010-07-29 14:48:06 -07001169 error = -EPERM;
John Johansen89dbf192017-06-09 17:01:43 -07001170 goto fail;
John Johansen898127c2010-07-29 14:48:06 -07001171 }
1172
1173 if (count) {
John Johansen89dbf192017-06-09 17:01:43 -07001174 new = change_hat(label, hats, count, flags);
1175 AA_BUG(!new);
1176 if (IS_ERR(new)) {
1177 error = PTR_ERR(new);
1178 new = NULL;
1179 /* already audited */
1180 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001181 }
1182
John Johansen89dbf192017-06-09 17:01:43 -07001183 error = may_change_ptraced_domain(new, &info);
1184 if (error)
1185 goto fail;
John Johansen898127c2010-07-29 14:48:06 -07001186
John Johansen9fcf78c2017-10-08 18:26:19 -07001187 /*
1188 * no new privs prevents domain transitions that would
1189 * reduce restrictions.
1190 */
1191 if (task_no_new_privs(current) && !unconfined(label) &&
1192 !aa_label_is_subset(new, ctx->nnp)) {
1193 /* not an apparmor denial per se, so don't log it */
1194 AA_DEBUG("no_new_privs - change_hat denied");
1195 error = -EPERM;
1196 goto out;
1197 }
1198
John Johansen89dbf192017-06-09 17:01:43 -07001199 if (flags & AA_CHANGE_TEST)
1200 goto out;
1201
1202 target = new;
1203 error = aa_set_current_hat(new, token);
1204 if (error == -EACCES)
1205 /* kill task in case of brute force attacks */
1206 goto kill;
1207 } else if (previous && !(flags & AA_CHANGE_TEST)) {
John Johansen9fcf78c2017-10-08 18:26:19 -07001208 /*
1209 * no new privs prevents domain transitions that would
1210 * reduce restrictions.
1211 */
1212 if (task_no_new_privs(current) && !unconfined(label) &&
1213 !aa_label_is_subset(previous, ctx->nnp)) {
1214 /* not an apparmor denial per se, so don't log it */
1215 AA_DEBUG("no_new_privs - change_hat denied");
1216 error = -EPERM;
1217 goto out;
1218 }
1219
John Johansen89dbf192017-06-09 17:01:43 -07001220 /* Return to saved label. Kill task if restore fails
John Johansen898127c2010-07-29 14:48:06 -07001221 * to avoid brute force attacks
1222 */
John Johansen89dbf192017-06-09 17:01:43 -07001223 target = previous;
John Johansen637f6882017-06-09 08:14:28 -07001224 error = aa_restore_previous_label(token);
John Johansen89dbf192017-06-09 17:01:43 -07001225 if (error) {
1226 if (error == -EACCES)
1227 goto kill;
1228 goto fail;
1229 }
1230 } /* else ignore @flags && restores when there is no saved profile */
John Johansen898127c2010-07-29 14:48:06 -07001231
1232out:
John Johansen89dbf192017-06-09 17:01:43 -07001233 aa_put_label(new);
1234 aa_put_label(previous);
John Johansen637f6882017-06-09 08:14:28 -07001235 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -07001236 put_cred(cred);
1237
1238 return error;
John Johansen89dbf192017-06-09 17:01:43 -07001239
1240kill:
1241 info = "failed token match";
1242 perms.kill = AA_MAY_CHANGEHAT;
1243
1244fail:
1245 fn_for_each_in_ns(label, profile,
1246 aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1247 AA_MAY_CHANGEHAT, NULL, NULL, target,
1248 GLOBAL_ROOT_UID, info, error));
1249
1250 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001251}
1252
John Johansen89dbf192017-06-09 17:01:43 -07001253
John Johansene00b02bb2017-06-09 17:07:58 -07001254static int change_profile_perms_wrapper(const char *op, const char *name,
1255 struct aa_profile *profile,
1256 struct aa_label *target, bool stack,
1257 u32 request, struct aa_perms *perms)
1258{
1259 const char *info = NULL;
1260 int error = 0;
1261
John Johansene00b02bb2017-06-09 17:07:58 -07001262 if (!error)
1263 error = change_profile_perms(profile, target, stack, request,
1264 profile->file.start, perms);
1265 if (error)
1266 error = aa_audit_file(profile, perms, op, request, name,
1267 NULL, target, GLOBAL_ROOT_UID, info,
1268 error);
1269
1270 return error;
1271}
John Johansen89dbf192017-06-09 17:01:43 -07001272
John Johansen898127c2010-07-29 14:48:06 -07001273/**
1274 * aa_change_profile - perform a one-way profile transition
John Johansenaa9a39a2017-01-16 00:43:06 -08001275 * @fqname: name of profile may include namespace (NOT NULL)
John Johansen898127c2010-07-29 14:48:06 -07001276 * @onexec: whether this transition is to take place immediately or at exec
John Johansendf8073c2017-06-09 11:36:48 -07001277 * @flags: flags affecting change behavior
John Johansen898127c2010-07-29 14:48:06 -07001278 *
1279 * Change to new profile @name. Unlike with hats, there is no way
1280 * to change back. If @name isn't specified the current profile name is
1281 * used.
1282 * If @onexec then the transition is delayed until
1283 * the next exec.
1284 *
1285 * Returns %0 on success, error otherwise.
1286 */
John Johansendf8073c2017-06-09 11:36:48 -07001287int aa_change_profile(const char *fqname, int flags)
John Johansen898127c2010-07-29 14:48:06 -07001288{
John Johansene00b02bb2017-06-09 17:07:58 -07001289 struct aa_label *label, *new = NULL, *target = NULL;
1290 struct aa_profile *profile;
John Johansen2d679f32017-05-29 12:19:39 -07001291 struct aa_perms perms = {};
John Johansene00b02bb2017-06-09 17:07:58 -07001292 const char *info = NULL;
1293 const char *auditname = fqname; /* retain leading & if stack */
1294 bool stack = flags & AA_CHANGE_STACK;
John Johansen9fcf78c2017-10-08 18:26:19 -07001295 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen47f6e5c2017-01-16 00:43:01 -08001296 int error = 0;
John Johansene00b02bb2017-06-09 17:07:58 -07001297 char *op;
John Johansen898127c2010-07-29 14:48:06 -07001298 u32 request;
1299
John Johansen9fcf78c2017-10-08 18:26:19 -07001300 label = aa_get_current_label();
1301
1302 /*
1303 * Detect no new privs being set, and store the label it
1304 * occurred under. Ideally this would happen when nnp
1305 * is set but there isn't a good way to do that yet.
1306 *
1307 * Testing for unconfined must be done before the subset test
1308 */
1309 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1310 ctx->nnp = aa_get_label(label);
1311
John Johansenaa9a39a2017-01-16 00:43:06 -08001312 if (!fqname || !*fqname) {
1313 AA_DEBUG("no profile name");
John Johansen898127c2010-07-29 14:48:06 -07001314 return -EINVAL;
John Johansenaa9a39a2017-01-16 00:43:06 -08001315 }
John Johansen898127c2010-07-29 14:48:06 -07001316
John Johansendf8073c2017-06-09 11:36:48 -07001317 if (flags & AA_CHANGE_ONEXEC) {
John Johansen898127c2010-07-29 14:48:06 -07001318 request = AA_MAY_ONEXEC;
John Johansene00b02bb2017-06-09 17:07:58 -07001319 if (stack)
1320 op = OP_STACK_ONEXEC;
1321 else
1322 op = OP_CHANGE_ONEXEC;
John Johansen898127c2010-07-29 14:48:06 -07001323 } else {
1324 request = AA_MAY_CHANGE_PROFILE;
John Johansene00b02bb2017-06-09 17:07:58 -07001325 if (stack)
1326 op = OP_STACK;
1327 else
1328 op = OP_CHANGE_PROFILE;
John Johansen898127c2010-07-29 14:48:06 -07001329 }
1330
John Johansene00b02bb2017-06-09 17:07:58 -07001331 label = aa_get_current_label();
John Johansen898127c2010-07-29 14:48:06 -07001332
John Johansene00b02bb2017-06-09 17:07:58 -07001333 if (*fqname == '&') {
1334 stack = true;
1335 /* don't have label_parse() do stacking */
1336 fqname++;
John Johansenc29bceb2012-04-12 16:47:51 -05001337 }
John Johansene00b02bb2017-06-09 17:07:58 -07001338 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1339 if (IS_ERR(target)) {
1340 struct aa_profile *tprofile;
John Johansenc29bceb2012-04-12 16:47:51 -05001341
John Johansene00b02bb2017-06-09 17:07:58 -07001342 info = "label not found";
1343 error = PTR_ERR(target);
1344 target = NULL;
1345 /*
1346 * TODO: fixme using labels_profile is not right - do profile
1347 * per complain profile
1348 */
John Johansendf8073c2017-06-09 11:36:48 -07001349 if ((flags & AA_CHANGE_TEST) ||
John Johansene00b02bb2017-06-09 17:07:58 -07001350 !COMPLAIN_MODE(labels_profile(label)))
John Johansen898127c2010-07-29 14:48:06 -07001351 goto audit;
1352 /* released below */
John Johansene00b02bb2017-06-09 17:07:58 -07001353 tprofile = aa_new_null_profile(labels_profile(label), false,
1354 fqname, GFP_KERNEL);
1355 if (!tprofile) {
John Johansen898127c2010-07-29 14:48:06 -07001356 info = "failed null profile create";
1357 error = -ENOMEM;
1358 goto audit;
1359 }
John Johansene00b02bb2017-06-09 17:07:58 -07001360 target = &tprofile->label;
1361 goto check;
John Johansen898127c2010-07-29 14:48:06 -07001362 }
1363
John Johansene00b02bb2017-06-09 17:07:58 -07001364 /*
1365 * self directed transitions only apply to current policy ns
1366 * TODO: currently requiring perms for stacking and straight change
1367 * stacking doesn't strictly need this. Determine how much
1368 * we want to loosen this restriction for stacking
1369 *
1370 * if (!stack) {
1371 */
1372 error = fn_for_each_in_ns(label, profile,
1373 change_profile_perms_wrapper(op, auditname,
1374 profile, target, stack,
1375 request, &perms));
1376 if (error)
1377 /* auditing done in change_profile_perms_wrapper */
1378 goto out;
John Johansenaa9a39a2017-01-16 00:43:06 -08001379
John Johansene00b02bb2017-06-09 17:07:58 -07001380 /* } */
1381
1382check:
John Johansen898127c2010-07-29 14:48:06 -07001383 /* check if tracing task is allowed to trace target domain */
John Johansene00b02bb2017-06-09 17:07:58 -07001384 error = may_change_ptraced_domain(target, &info);
1385 if (error && !fn_for_each_in_ns(label, profile,
1386 COMPLAIN_MODE(profile)))
John Johansen898127c2010-07-29 14:48:06 -07001387 goto audit;
John Johansen898127c2010-07-29 14:48:06 -07001388
John Johansene00b02bb2017-06-09 17:07:58 -07001389 /* TODO: add permission check to allow this
1390 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1391 * info = "not a single threaded task";
1392 * error = -EACCES;
1393 * goto audit;
1394 * }
1395 */
John Johansendf8073c2017-06-09 11:36:48 -07001396 if (flags & AA_CHANGE_TEST)
John Johansene00b02bb2017-06-09 17:07:58 -07001397 goto out;
John Johansen898127c2010-07-29 14:48:06 -07001398
John Johansen9fcf78c2017-10-08 18:26:19 -07001399 /* stacking is always a subset, so only check the nonstack case */
1400 if (!stack) {
1401 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1402 aa_get_label(target),
1403 aa_get_label(&profile->label));
1404 /*
1405 * no new privs prevents domain transitions that would
1406 * reduce restrictions.
1407 */
1408 if (task_no_new_privs(current) && !unconfined(label) &&
1409 !aa_label_is_subset(new, ctx->nnp)) {
1410 /* not an apparmor denial per se, so don't log it */
1411 AA_DEBUG("no_new_privs - change_hat denied");
1412 error = -EPERM;
1413 goto out;
1414 }
1415 }
1416
John Johansene00b02bb2017-06-09 17:07:58 -07001417 if (!(flags & AA_CHANGE_ONEXEC)) {
1418 /* only transition profiles in the current ns */
1419 if (stack)
1420 new = aa_label_merge(label, target, GFP_KERNEL);
John Johansene00b02bb2017-06-09 17:07:58 -07001421 if (IS_ERR_OR_NULL(new)) {
1422 info = "failed to build target label";
1423 error = PTR_ERR(new);
1424 new = NULL;
1425 perms.allow = 0;
1426 goto audit;
1427 }
1428 error = aa_replace_current_label(new);
John Johansen9fcf78c2017-10-08 18:26:19 -07001429 } else {
1430 if (new) {
1431 aa_put_label(new);
1432 new = NULL;
1433 }
1434
John Johansene00b02bb2017-06-09 17:07:58 -07001435 /* full transition will be built in exec path */
1436 error = aa_set_current_onexec(target, stack);
John Johansen9fcf78c2017-10-08 18:26:19 -07001437 }
John Johansen898127c2010-07-29 14:48:06 -07001438
1439audit:
John Johansene00b02bb2017-06-09 17:07:58 -07001440 error = fn_for_each_in_ns(label, profile,
1441 aa_audit_file(profile, &perms, op, request, auditname,
1442 NULL, new ? new : target,
1443 GLOBAL_ROOT_UID, info, error));
John Johansen898127c2010-07-29 14:48:06 -07001444
John Johansene00b02bb2017-06-09 17:07:58 -07001445out:
1446 aa_put_label(new);
1447 aa_put_label(target);
John Johansen637f6882017-06-09 08:14:28 -07001448 aa_put_label(label);
John Johansen898127c2010-07-29 14:48:06 -07001449
1450 return error;
1451}