2 * AppArmor security module
4 * This file contains AppArmor functions used to manipulate object security
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2 of the
16 * AppArmor sets confinement on every task, via the the aa_task_cxt and
17 * the aa_task_cxt.profile, both of which are required and are not allowed
18 * to be NULL. The aa_task_cxt is not reference counted and is unique
19 * to each cred (which is reference count). The profile pointed to by
20 * the task_cxt is reference counted.
23 * If a task uses change_hat it currently does not return to the old
24 * cred or task context but instead creates a new one. Ideally the task
25 * should return to the previous cred if it has not been modified.
29 #include "include/context.h"
30 #include "include/policy.h"
33 * aa_alloc_task_context - allocate a new task_cxt
34 * @flags: gfp flags for allocation
36 * Returns: allocated buffer or NULL on failure
38 struct aa_task_cxt *aa_alloc_task_context(gfp_t flags)
40 return kzalloc(sizeof(struct aa_task_cxt), flags);
44 * aa_free_task_context - free a task_cxt
45 * @cxt: task_cxt to free (MAYBE NULL)
47 void aa_free_task_context(struct aa_task_cxt *cxt)
50 aa_put_profile(cxt->profile);
51 aa_put_profile(cxt->previous);
52 aa_put_profile(cxt->onexec);
59 * aa_dup_task_context - duplicate a task context, incrementing reference counts
60 * @new: a blank task context (NOT NULL)
61 * @old: the task context to copy (NOT NULL)
63 void aa_dup_task_context(struct aa_task_cxt *new, const struct aa_task_cxt *old)
66 aa_get_profile(new->profile);
67 aa_get_profile(new->previous);
68 aa_get_profile(new->onexec);
72 * aa_get_task_profile - Get another task's profile
73 * @task: task to query (NOT NULL)
75 * Returns: counted reference to @task's profile
77 struct aa_profile *aa_get_task_profile(struct task_struct *task)
82 p = aa_get_profile(__aa_task_profile(task));
89 * aa_replace_current_profile - replace the current tasks profiles
90 * @profile: new profile (NOT NULL)
92 * Returns: 0 or error on failure
94 int aa_replace_current_profile(struct aa_profile *profile)
96 struct aa_task_cxt *cxt = current_cxt();
100 if (cxt->profile == profile)
103 new = prepare_creds();
108 if (unconfined(profile) || (cxt->profile->ns != profile->ns))
109 /* if switching to unconfined or a different profile namespace
110 * clear out context state
112 aa_clear_task_cxt_trans(cxt);
114 /* be careful switching cxt->profile, when racing replacement it
115 * is possible that cxt->profile->replacedby is the reference keeping
116 * @profile valid, so make sure to get its reference before dropping
117 * the reference on cxt->profile */
118 aa_get_profile(profile);
119 aa_put_profile(cxt->profile);
120 cxt->profile = profile;
127 * aa_set_current_onexec - set the tasks change_profile to happen onexec
128 * @profile: system profile to set at exec (MAYBE NULL to clear value)
130 * Returns: 0 or error on failure
132 int aa_set_current_onexec(struct aa_profile *profile)
134 struct aa_task_cxt *cxt;
135 struct cred *new = prepare_creds();
140 aa_get_profile(profile);
141 aa_put_profile(cxt->onexec);
142 cxt->onexec = profile;
149 * aa_set_current_hat - set the current tasks hat
150 * @profile: profile to set as the current hat (NOT NULL)
151 * @token: token value that must be specified to change from the hat
153 * Do switch of tasks hat. If the task is currently in a hat
154 * validate the token to match.
156 * Returns: 0 or error on failure
158 int aa_set_current_hat(struct aa_profile *profile, u64 token)
160 struct aa_task_cxt *cxt;
161 struct cred *new = prepare_creds();
167 if (!cxt->previous) {
168 /* transfer refcount */
169 cxt->previous = cxt->profile;
171 } else if (cxt->token == token) {
172 aa_put_profile(cxt->profile);
174 /* previous_profile && cxt->token != token */
178 cxt->profile = aa_get_profile(aa_newest_version(profile));
179 /* clear exec on switching context */
180 aa_put_profile(cxt->onexec);
188 * aa_restore_previous_profile - exit from hat context restoring the profile
189 * @token: the token that must be matched to exit hat context
191 * Attempt to return out of a hat to the previous profile. The token
192 * must match the stored token value.
194 * Returns: 0 or error of failure
196 int aa_restore_previous_profile(u64 token)
198 struct aa_task_cxt *cxt;
199 struct cred *new = prepare_creds();
204 if (cxt->token != token) {
208 /* ignore restores when there is no saved profile */
209 if (!cxt->previous) {
214 aa_put_profile(cxt->profile);
215 cxt->profile = aa_newest_version(cxt->previous);
216 BUG_ON(!cxt->profile);
217 if (unlikely(cxt->profile != cxt->previous)) {
218 aa_get_profile(cxt->profile);
219 aa_put_profile(cxt->previous);
221 /* ref has been transfered so avoid putting ref in clear_task_cxt */
222 cxt->previous = NULL;
223 /* clear exec && prev information when restoring to previous context */
224 aa_clear_task_cxt_trans(cxt);