]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - security/apparmor/context.c
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_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.
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"
34 * aa_free_task_context - free a task_ctx
35 * @ctx: task_ctx to free (MAYBE NULL)
37 void aa_free_task_context(struct aa_task_ctx
*ctx
)
40 aa_put_label(ctx
->label
);
41 aa_put_label(ctx
->previous
);
42 aa_put_label(ctx
->onexec
);
47 * aa_dup_task_context - duplicate a task context, incrementing reference counts
48 * @new: a blank task context (NOT NULL)
49 * @old: the task context to copy (NOT NULL)
51 void aa_dup_task_context(struct aa_task_ctx
*new, const struct aa_task_ctx
*old
)
54 aa_get_label(new->label
);
55 aa_get_label(new->previous
);
56 aa_get_label(new->onexec
);
60 * aa_get_task_label - Get another task's label
61 * @task: task to query (NOT NULL)
63 * Returns: counted reference to @task's label
65 struct aa_label
*aa_get_task_label(struct task_struct
*task
)
70 p
= aa_get_newest_label(__aa_task_raw_label(task
));
77 * aa_replace_current_label - replace the current tasks label
78 * @label: new label (NOT NULL)
80 * Returns: 0 or error on failure
82 int aa_replace_current_label(struct aa_label
*label
)
84 struct aa_task_ctx
*ctx
= current_ctx();
88 if (ctx
->label
== label
)
91 if (current_cred() != current_real_cred())
94 new = prepare_creds();
99 if (unconfined(label
) || (labels_ns(ctx
->label
) != labels_ns(label
)))
100 /* if switching to unconfined or a different label namespace
101 * clear out context state
103 aa_clear_task_ctx_trans(ctx
);
106 * be careful switching ctx->profile, when racing replacement it
107 * is possible that ctx->profile->proxy->profile is the reference
108 * keeping @profile valid, so make sure to get its reference before
109 * dropping the reference on ctx->profile
112 aa_put_label(ctx
->label
);
120 * aa_set_current_onexec - set the tasks change_profile to happen onexec
121 * @label: system label to set at exec (MAYBE NULL to clear value)
122 * @stack: whether stacking should be done
123 * Returns: 0 or error on failure
125 int aa_set_current_onexec(struct aa_label
*label
, bool stack
)
127 struct aa_task_ctx
*ctx
;
128 struct cred
*new = prepare_creds();
134 aa_clear_task_ctx_trans(ctx
);
143 * aa_set_current_hat - set the current tasks hat
144 * @label: label to set as the current hat (NOT NULL)
145 * @token: token value that must be specified to change from the hat
147 * Do switch of tasks hat. If the task is currently in a hat
148 * validate the token to match.
150 * Returns: 0 or error on failure
152 int aa_set_current_hat(struct aa_label
*label
, u64 token
)
154 struct aa_task_ctx
*ctx
;
155 struct cred
*new = prepare_creds();
161 if (!ctx
->previous
) {
162 /* transfer refcount */
163 ctx
->previous
= ctx
->label
;
165 } else if (ctx
->token
== token
) {
166 aa_put_label(ctx
->label
);
168 /* previous_profile && ctx->token != token */
172 ctx
->label
= aa_get_newest_label(label
);
173 /* clear exec on switching context */
174 aa_put_label(ctx
->onexec
);
182 * aa_restore_previous_label - exit from hat context restoring previous label
183 * @token: the token that must be matched to exit hat context
185 * Attempt to return out of a hat to the previous label. The token
186 * must match the stored token value.
188 * Returns: 0 or error of failure
190 int aa_restore_previous_label(u64 token
)
192 struct aa_task_ctx
*ctx
;
193 struct cred
*new = prepare_creds();
198 if (ctx
->token
!= token
) {
202 /* ignore restores when there is no saved label */
203 if (!ctx
->previous
) {
208 aa_put_label(ctx
->label
);
209 ctx
->label
= aa_get_newest_label(ctx
->previous
);
211 /* clear exec && prev information when restoring to previous context */
212 aa_clear_task_ctx_trans(ctx
);