blob: 3f32f594c9999025aec4f6b2feac1dd57887a620 [file] [log] [blame]
John Johansenc75afcd2010-07-29 14:47:59 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor functions used to manipulate object security
5 * contexts.
6 *
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
9 *
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
13 * License.
14 *
15 *
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.
21 *
22 * TODO
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.
26 *
27 */
28
29#include "include/context.h"
30#include "include/policy.h"
31
32/**
33 * aa_alloc_task_context - allocate a new task_cxt
34 * @flags: gfp flags for allocation
35 *
36 * Returns: allocated buffer or NULL on failure
37 */
38struct aa_task_cxt *aa_alloc_task_context(gfp_t flags)
39{
40 return kzalloc(sizeof(struct aa_task_cxt), flags);
41}
42
43/**
44 * aa_free_task_context - free a task_cxt
45 * @cxt: task_cxt to free (MAYBE NULL)
46 */
47void aa_free_task_context(struct aa_task_cxt *cxt)
48{
49 if (cxt) {
50 aa_put_profile(cxt->profile);
51 aa_put_profile(cxt->previous);
52 aa_put_profile(cxt->onexec);
53
54 kzfree(cxt);
55 }
56}
57
58/**
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)
62 */
63void aa_dup_task_context(struct aa_task_cxt *new, const struct aa_task_cxt *old)
64{
65 *new = *old;
66 aa_get_profile(new->profile);
67 aa_get_profile(new->previous);
68 aa_get_profile(new->onexec);
69}
70
71/**
John Johansen3cfcc192013-02-18 16:03:34 -080072 * aa_get_task_profile - Get another task's profile
73 * @task: task to query (NOT NULL)
74 *
75 * Returns: counted reference to @task's profile
76 */
77struct aa_profile *aa_get_task_profile(struct task_struct *task)
78{
79 struct aa_profile *p;
80
81 rcu_read_lock();
82 p = aa_get_profile(__aa_task_profile(task));
83 rcu_read_unlock();
84
85 return p;
86}
87
88/**
John Johansenc75afcd2010-07-29 14:47:59 -070089 * aa_replace_current_profile - replace the current tasks profiles
90 * @profile: new profile (NOT NULL)
91 *
92 * Returns: 0 or error on failure
93 */
94int aa_replace_current_profile(struct aa_profile *profile)
95{
John Johansen214beac2013-02-27 03:43:40 -080096 struct aa_task_cxt *cxt = current_cxt();
John Johansenc75afcd2010-07-29 14:47:59 -070097 struct cred *new;
98 BUG_ON(!profile);
99
100 if (cxt->profile == profile)
101 return 0;
102
John Johansena20aa952017-01-16 00:42:59 -0800103 if (current_cred() != current_real_cred())
104 return -EBUSY;
105
John Johansenc75afcd2010-07-29 14:47:59 -0700106 new = prepare_creds();
107 if (!new)
108 return -ENOMEM;
109
John Johansen214beac2013-02-27 03:43:40 -0800110 cxt = cred_cxt(new);
John Johansen7a2871b2013-02-18 16:05:34 -0800111 if (unconfined(profile) || (cxt->profile->ns != profile->ns))
John Johansenc75afcd2010-07-29 14:47:59 -0700112 /* if switching to unconfined or a different profile namespace
113 * clear out context state
114 */
John Johansen7a2871b2013-02-18 16:05:34 -0800115 aa_clear_task_cxt_trans(cxt);
116
John Johansenc75afcd2010-07-29 14:47:59 -0700117 /* be careful switching cxt->profile, when racing replacement it
John Johansen83995882017-01-16 00:42:19 -0800118 * is possible that cxt->profile->proxy->profile is the reference
John Johansen77b071b2013-07-10 21:07:43 -0700119 * keeping @profile valid, so make sure to get its reference before
120 * dropping the reference on cxt->profile */
John Johansenc75afcd2010-07-29 14:47:59 -0700121 aa_get_profile(profile);
122 aa_put_profile(cxt->profile);
123 cxt->profile = profile;
124
125 commit_creds(new);
126 return 0;
127}
128
129/**
130 * aa_set_current_onexec - set the tasks change_profile to happen onexec
131 * @profile: system profile to set at exec (MAYBE NULL to clear value)
132 *
133 * Returns: 0 or error on failure
134 */
135int aa_set_current_onexec(struct aa_profile *profile)
136{
137 struct aa_task_cxt *cxt;
138 struct cred *new = prepare_creds();
139 if (!new)
140 return -ENOMEM;
141
John Johansen214beac2013-02-27 03:43:40 -0800142 cxt = cred_cxt(new);
John Johansenc75afcd2010-07-29 14:47:59 -0700143 aa_get_profile(profile);
144 aa_put_profile(cxt->onexec);
145 cxt->onexec = profile;
146
147 commit_creds(new);
148 return 0;
149}
150
151/**
152 * aa_set_current_hat - set the current tasks hat
153 * @profile: profile to set as the current hat (NOT NULL)
154 * @token: token value that must be specified to change from the hat
155 *
156 * Do switch of tasks hat. If the task is currently in a hat
157 * validate the token to match.
158 *
159 * Returns: 0 or error on failure
160 */
161int aa_set_current_hat(struct aa_profile *profile, u64 token)
162{
163 struct aa_task_cxt *cxt;
164 struct cred *new = prepare_creds();
165 if (!new)
166 return -ENOMEM;
167 BUG_ON(!profile);
168
John Johansen214beac2013-02-27 03:43:40 -0800169 cxt = cred_cxt(new);
John Johansenc75afcd2010-07-29 14:47:59 -0700170 if (!cxt->previous) {
171 /* transfer refcount */
172 cxt->previous = cxt->profile;
173 cxt->token = token;
174 } else if (cxt->token == token) {
175 aa_put_profile(cxt->profile);
176 } else {
177 /* previous_profile && cxt->token != token */
178 abort_creds(new);
179 return -EACCES;
180 }
John Johansen77b071b2013-07-10 21:07:43 -0700181 cxt->profile = aa_get_newest_profile(profile);
John Johansenc75afcd2010-07-29 14:47:59 -0700182 /* clear exec on switching context */
183 aa_put_profile(cxt->onexec);
184 cxt->onexec = NULL;
185
186 commit_creds(new);
187 return 0;
188}
189
190/**
191 * aa_restore_previous_profile - exit from hat context restoring the profile
192 * @token: the token that must be matched to exit hat context
193 *
194 * Attempt to return out of a hat to the previous profile. The token
195 * must match the stored token value.
196 *
197 * Returns: 0 or error of failure
198 */
199int aa_restore_previous_profile(u64 token)
200{
201 struct aa_task_cxt *cxt;
202 struct cred *new = prepare_creds();
203 if (!new)
204 return -ENOMEM;
205
John Johansen214beac2013-02-27 03:43:40 -0800206 cxt = cred_cxt(new);
John Johansenc75afcd2010-07-29 14:47:59 -0700207 if (cxt->token != token) {
208 abort_creds(new);
209 return -EACCES;
210 }
211 /* ignore restores when there is no saved profile */
212 if (!cxt->previous) {
213 abort_creds(new);
214 return 0;
215 }
216
217 aa_put_profile(cxt->profile);
John Johansen77b071b2013-07-10 21:07:43 -0700218 cxt->profile = aa_get_newest_profile(cxt->previous);
John Johansenc75afcd2010-07-29 14:47:59 -0700219 BUG_ON(!cxt->profile);
John Johansen7a2871b2013-02-18 16:05:34 -0800220 /* clear exec && prev information when restoring to previous context */
221 aa_clear_task_cxt_trans(cxt);
John Johansenc75afcd2010-07-29 14:47:59 -0700222
223 commit_creds(new);
224 return 0;
225}