xref: /linux/security/apparmor/include/task.h (revision 37077e4cfad4905128853f83c9e4ddf6a31e04f3)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor task related definitions and mediation
6  *
7  * Copyright 2017 Canonical Ltd.
8  */
9 
10 #ifndef __AA_TASK_H
11 #define __AA_TASK_H
12 
13 #include <linux/sched.h>
14 
15 #include "audit.h"
16 #include "label.h"
17 
18 static inline struct aa_task_ctx *task_ctx(struct task_struct *task)
19 {
20 	return task->security + apparmor_blob_sizes.lbs_task;
21 }
22 
23 /*
24  * struct aa_task_ctx - information for current task label change
25  * @nnp: snapshot of label at time of no_new_privs
26  * @onexec: profile to transition to on next exec  (MAY BE NULL)
27  * @previous: profile the task may return to     (MAY BE NULL)
28  * @token: magic value the task must know for returning to @previous_profile
29  */
30 struct aa_task_ctx {
31 	struct aa_label *nnp;
32 	struct aa_label *onexec;
33 	struct aa_label *previous;
34 	u64 token;
35 };
36 
37 int aa_replace_current_label(struct aa_label *label);
38 void aa_set_current_onexec(struct aa_label *label, bool stack);
39 int aa_set_current_hat(struct aa_label *label, u64 token);
40 int aa_restore_previous_label(u64 cookie);
41 struct aa_label *aa_get_task_label(struct task_struct *task);
42 
43 /**
44  * aa_free_task_ctx - free a task_ctx
45  * @ctx: task_ctx to free (MAYBE NULL)
46  */
47 static inline void aa_free_task_ctx(struct aa_task_ctx *ctx)
48 {
49 	if (ctx) {
50 		aa_put_label(ctx->nnp);
51 		aa_put_label(ctx->previous);
52 		aa_put_label(ctx->onexec);
53 	}
54 }
55 
56 /**
57  * aa_dup_task_ctx - duplicate a task context, incrementing reference counts
58  * @new: a blank task context      (NOT NULL)
59  * @old: the task context to copy  (NOT NULL)
60  */
61 static inline void aa_dup_task_ctx(struct aa_task_ctx *new,
62 				   const struct aa_task_ctx *old)
63 {
64 	*new = *old;
65 	aa_get_label(new->nnp);
66 	aa_get_label(new->previous);
67 	aa_get_label(new->onexec);
68 }
69 
70 /**
71  * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
72  * @ctx: task context to clear (NOT NULL)
73  */
74 static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
75 {
76 	AA_BUG(!ctx);
77 
78 	aa_put_label(ctx->previous);
79 	aa_put_label(ctx->onexec);
80 	ctx->previous = NULL;
81 	ctx->onexec = NULL;
82 	ctx->token = 0;
83 }
84 
85 #define AA_PTRACE_TRACE		MAY_WRITE
86 #define AA_PTRACE_READ		MAY_READ
87 #define AA_MAY_BE_TRACED	AA_MAY_APPEND
88 #define AA_MAY_BE_READ		AA_MAY_CREATE
89 #define PTRACE_PERM_SHIFT	2
90 
91 #define AA_PTRACE_PERM_MASK (AA_PTRACE_READ | AA_PTRACE_TRACE | \
92 			     AA_MAY_BE_READ | AA_MAY_BE_TRACED)
93 #define AA_SIGNAL_PERM_MASK (MAY_READ | MAY_WRITE)
94 
95 #define AA_SFS_SIG_MASK "hup int quit ill trap abrt bus fpe kill usr1 " \
96 	"segv usr2 pipe alrm term stkflt chld cont stop stp ttin ttou urg " \
97 	"xcpu xfsz vtalrm prof winch io pwr sys emt lost"
98 
99 int aa_may_ptrace(const struct cred *tracer_cred, struct aa_label *tracer,
100 		  const struct cred *tracee_cred, struct aa_label *tracee,
101 		  u32 request);
102 
103 
104 
105 #define AA_USERNS_CREATE	8
106 
107 int aa_profile_ns_perm(struct aa_profile *profile,
108 		       struct apparmor_audit_data *ad, u32 request);
109 
110 #endif /* __AA_TASK_H */
111