]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - security/apparmor/context.c
UBUNTU: Ubuntu-raspi2-4.10.0-1017.20
[mirror_ubuntu-zesty-kernel.git] / security / apparmor / context.c
1 /*
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_ctx and
17 * the aa_task_ctx.label, both of which are required and are not allowed
18 * to be NULL. The aa_task_ctx is not reference counted and is unique
19 * to each cred (which is reference count). The label pointed to by
20 * the task_ctx 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_ctx
34 * @flags: gfp flags for allocation
35 *
36 * Returns: allocated buffer or NULL on failure
37 */
38 struct aa_task_ctx *aa_alloc_task_context(gfp_t flags)
39 {
40 return kzalloc(sizeof(struct aa_task_ctx), flags);
41 }
42
43 /**
44 * aa_free_task_context - free a task_ctx
45 * @ctx: task_ctx to free (MAYBE NULL)
46 */
47 void aa_free_task_context(struct aa_task_ctx *ctx)
48 {
49 if (ctx) {
50 aa_put_label(ctx->label);
51 aa_put_label(ctx->previous);
52 aa_put_label(ctx->onexec);
53
54 kzfree(ctx);
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 */
63 void aa_dup_task_context(struct aa_task_ctx *new, const struct aa_task_ctx *old)
64 {
65 *new = *old;
66 aa_get_label(new->label);
67 aa_get_label(new->previous);
68 aa_get_label(new->onexec);
69 }
70
71 /**
72 * aa_get_task_label - Get another task's label
73 * @task: task to query (NOT NULL)
74 *
75 * Returns: counted reference to @task's label
76 */
77 struct aa_label *aa_get_task_label(struct task_struct *task)
78 {
79 struct aa_label *p;
80
81 rcu_read_lock();
82 p = aa_get_newest_label(__aa_task_raw_label(task));
83 rcu_read_unlock();
84
85 return p;
86 }
87
88 /**
89 * aa_replace_current_label - replace the current tasks label
90 * @label: new label (NOT NULL)
91 *
92 * Returns: 0 or error on failure
93 */
94 int aa_replace_current_label(struct aa_label *label)
95 {
96 struct aa_task_ctx *ctx = current_ctx();
97 struct cred *new;
98 BUG_ON(!label);
99
100 if (ctx->label == label)
101 return 0;
102
103 if (current_cred() != current_real_cred())
104 return -EBUSY;
105
106 new = prepare_creds();
107 if (!new)
108 return -ENOMEM;
109
110 ctx = cred_ctx(new);
111 if (unconfined(label) || (labels_ns(ctx->label) != labels_ns(label)))
112 /* if switching to unconfined or a different label namespace
113 * clear out context state
114 */
115 aa_clear_task_ctx_trans(ctx);
116
117 aa_get_label(label);
118 aa_put_label(ctx->label);
119 ctx->label = label;
120
121 commit_creds(new);
122 return 0;
123 }
124
125 /**
126 * aa_set_current_onexec - set the tasks change_profile to happen onexec
127 * @label: system label to set at exec (MAYBE NULL to clear value)
128 * @stack: whether stacking should be done
129 * Returns: 0 or error on failure
130 */
131 int aa_set_current_onexec(struct aa_label *label, bool stack)
132 {
133 struct aa_task_ctx *ctx;
134 struct cred *new = prepare_creds();
135 if (!new)
136 return -ENOMEM;
137
138 ctx = cred_ctx(new);
139 aa_get_label(label);
140 aa_clear_task_ctx_trans(ctx);
141 ctx->onexec = label;
142 ctx->token = stack;
143
144 commit_creds(new);
145 return 0;
146 }
147
148 /**
149 * aa_set_current_hat - set the current tasks hat
150 * @label: label to set as the current hat (NOT NULL)
151 * @token: token value that must be specified to change from the hat
152 *
153 * Do switch of tasks hat. If the task is currently in a hat
154 * validate the token to match.
155 *
156 * Returns: 0 or error on failure
157 */
158 int aa_set_current_hat(struct aa_label *label, u64 token)
159 {
160 struct aa_task_ctx *ctx;
161 struct cred *new = prepare_creds();
162 if (!new)
163 return -ENOMEM;
164 BUG_ON(!label);
165
166 ctx = cred_ctx(new);
167 if (!ctx->previous) {
168 /* transfer refcount */
169 ctx->previous = ctx->label;
170 ctx->token = token;
171 } else if (ctx->token == token) {
172 aa_put_label(ctx->label);
173 } else {
174 /* previous_profile && ctx->token != token */
175 abort_creds(new);
176 return -EACCES;
177 }
178 ctx->label = aa_get_newest_label(label);
179 /* clear exec on switching context */
180 aa_put_label(ctx->onexec);
181 ctx->onexec = NULL;
182
183 commit_creds(new);
184 return 0;
185 }
186
187 /**
188 * aa_restore_previous_label - exit from hat context restoring previous label
189 * @token: the token that must be matched to exit hat context
190 *
191 * Attempt to return out of a hat to the previous label. The token
192 * must match the stored token value.
193 *
194 * Returns: 0 or error of failure
195 */
196 int aa_restore_previous_label(u64 token)
197 {
198 struct aa_task_ctx *ctx;
199 struct cred *new = prepare_creds();
200 if (!new)
201 return -ENOMEM;
202
203 ctx = cred_ctx(new);
204 if (ctx->token != token) {
205 abort_creds(new);
206 return -EACCES;
207 }
208 /* ignore restores when there is no saved label */
209 if (!ctx->previous) {
210 abort_creds(new);
211 return 0;
212 }
213
214 aa_put_label(ctx->label);
215 ctx->label = aa_get_newest_label(ctx->previous);
216 BUG_ON(!ctx->label);
217 /* clear exec && prev information when restoring to previous context */
218 aa_clear_task_ctx_trans(ctx);
219
220 commit_creds(new);
221 return 0;
222 }