xref: /linux-6.15/security/apparmor/include/task.h (revision de62de59)
1 /*
2  * AppArmor security module
3  *
4  * This file contains AppArmor task related definitions and mediation
5  *
6  * Copyright 2017 Canonical Ltd.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation, version 2 of the
11  * License.
12  */
13 
14 #ifndef __AA_TASK_H
15 #define __AA_TASK_H
16 
17 #define task_ctx(X) ((X)->security)
18 
19 /*
20  * struct aa_task_ctx - information for current task label change
21  * @onexec: profile to transition to on next exec  (MAY BE NULL)
22  * @previous: profile the task may return to     (MAY BE NULL)
23  * @token: magic value the task must know for returning to @previous_profile
24  */
25 struct aa_task_ctx {
26 	struct aa_label *onexec;
27 	struct aa_label *previous;
28 	u64 token;
29 };
30 
31 int aa_replace_current_label(struct aa_label *label);
32 int aa_set_current_onexec(struct aa_label *label, bool stack);
33 int aa_set_current_hat(struct aa_label *label, u64 token);
34 int aa_restore_previous_label(u64 cookie);
35 struct aa_label *aa_get_task_label(struct task_struct *task);
36 
37 /**
38  * aa_alloc_task_ctx - allocate a new task_ctx
39  * @flags: gfp flags for allocation
40  *
41  * Returns: allocated buffer or NULL on failure
42  */
43 static inline struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags)
44 {
45 	return kzalloc(sizeof(struct aa_task_ctx), flags);
46 }
47 
48 /**
49  * aa_free_task_ctx - free a task_ctx
50  * @ctx: task_ctx to free (MAYBE NULL)
51  */
52 static inline void aa_free_task_ctx(struct aa_task_ctx *ctx)
53 {
54 	if (ctx) {
55 		aa_put_label(ctx->previous);
56 		aa_put_label(ctx->onexec);
57 
58 		kzfree(ctx);
59 	}
60 }
61 
62 /**
63  * aa_dup_task_ctx - duplicate a task context, incrementing reference counts
64  * @new: a blank task context      (NOT NULL)
65  * @old: the task context to copy  (NOT NULL)
66  */
67 static inline void aa_dup_task_ctx(struct aa_task_ctx *new,
68 				   const struct aa_task_ctx *old)
69 {
70 	*new = *old;
71 	aa_get_label(new->previous);
72 	aa_get_label(new->onexec);
73 }
74 
75 /**
76  * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
77  * @ctx: task context to clear (NOT NULL)
78  */
79 static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
80 {
81 	AA_BUG(!ctx);
82 
83 	aa_put_label(ctx->previous);
84 	aa_put_label(ctx->onexec);
85 	ctx->previous = NULL;
86 	ctx->onexec = NULL;
87 	ctx->token = 0;
88 }
89 
90 #endif /* __AA_TASK_H */
91