1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor task related definitions and mediation
7 * Copyright 2017 Canonical Ltd.
10 * If a task uses change_hat it currently does not return to the old
11 * cred or task context but instead creates a new one. Ideally the task
12 * should return to the previous cred if it has not been modified.
15 #include "include/cred.h"
16 #include "include/task.h"
19 * aa_get_task_label - Get another task's label
20 * @task: task to query (NOT NULL)
22 * Returns: counted reference to @task's label
24 struct aa_label
*aa_get_task_label(struct task_struct
*task
)
29 p
= aa_get_newest_label(__aa_task_raw_label(task
));
36 * aa_replace_current_label - replace the current tasks label
37 * @label: new label (NOT NULL)
39 * Returns: 0 or error on failure
41 int aa_replace_current_label(struct aa_label
*label
)
43 struct aa_label
*old
= aa_current_raw_label();
44 struct aa_task_ctx
*ctx
= task_ctx(current
);
52 if (current_cred() != current_real_cred())
55 new = prepare_creds();
59 if (ctx
->nnp
&& label_is_stale(ctx
->nnp
)) {
60 struct aa_label
*tmp
= ctx
->nnp
;
62 ctx
->nnp
= aa_get_newest_label(tmp
);
65 if (unconfined(label
) || (labels_ns(old
) != labels_ns(label
)))
67 * if switching to unconfined or a different label namespace
68 * clear out context state
70 aa_clear_task_ctx_trans(task_ctx(current
));
73 * be careful switching cred label, when racing replacement it
74 * is possible that the cred labels's->proxy->label is the reference
75 * keeping @label valid, so make sure to get its reference before
76 * dropping the reference on the cred's label
79 aa_put_label(cred_label(new));
80 set_cred_label(new, label
);
88 * aa_set_current_onexec - set the tasks change_profile to happen onexec
89 * @label: system label to set at exec (MAYBE NULL to clear value)
90 * @stack: whether stacking should be done
91 * Returns: 0 or error on failure
93 int aa_set_current_onexec(struct aa_label
*label
, bool stack
)
95 struct aa_task_ctx
*ctx
= task_ctx(current
);
98 aa_put_label(ctx
->onexec
);
106 * aa_set_current_hat - set the current tasks hat
107 * @label: label to set as the current hat (NOT NULL)
108 * @token: token value that must be specified to change from the hat
110 * Do switch of tasks hat. If the task is currently in a hat
111 * validate the token to match.
113 * Returns: 0 or error on failure
115 int aa_set_current_hat(struct aa_label
*label
, u64 token
)
117 struct aa_task_ctx
*ctx
= task_ctx(current
);
120 new = prepare_creds();
125 if (!ctx
->previous
) {
126 /* transfer refcount */
127 ctx
->previous
= cred_label(new);
129 } else if (ctx
->token
== token
) {
130 aa_put_label(cred_label(new));
132 /* previous_profile && ctx->token != token */
137 set_cred_label(new, aa_get_newest_label(label
));
138 /* clear exec on switching context */
139 aa_put_label(ctx
->onexec
);
147 * aa_restore_previous_label - exit from hat context restoring previous label
148 * @token: the token that must be matched to exit hat context
150 * Attempt to return out of a hat to the previous label. The token
151 * must match the stored token value.
153 * Returns: 0 or error of failure
155 int aa_restore_previous_label(u64 token
)
157 struct aa_task_ctx
*ctx
= task_ctx(current
);
160 if (ctx
->token
!= token
)
162 /* ignore restores when there is no saved label */
166 new = prepare_creds();
170 aa_put_label(cred_label(new));
171 set_cred_label(new, aa_get_newest_label(ctx
->previous
));
172 AA_BUG(!cred_label(new));
173 /* clear exec && prev information when restoring to previous context */
174 aa_clear_task_ctx_trans(ctx
);