2 * AppArmor security module
4 * This file contains AppArmor contexts used to associate "labels" to objects.
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
15 #ifndef __AA_CONTEXT_H
16 #define __AA_CONTEXT_H
18 #include <linux/cred.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
23 #include "policy_ns.h"
25 #define cred_ctx(X) ((X)->security)
26 #define current_ctx() cred_ctx(current_cred())
28 /* struct aa_file_ctx - the AppArmor context the file was opened in
29 * @perms: the permission the file was opened with
31 * The file_ctx could currently be directly stored in file->f_security
32 * as the profile reference is now stored in the f_cred. However the
33 * ctx struct will expand in the future so we keep the struct.
40 * aa_alloc_file_context - allocate file_ctx
41 * @gfp: gfp flags for allocation
43 * Returns: file_ctx or NULL on failure
45 static inline struct aa_file_ctx
*aa_alloc_file_context(gfp_t gfp
)
47 return kzalloc(sizeof(struct aa_file_ctx
), gfp
);
51 * aa_free_file_context - free a file_ctx
52 * @ctx: file_ctx to free (MAYBE_NULL)
54 static inline void aa_free_file_context(struct aa_file_ctx
*ctx
)
61 * struct aa_task_ctx - primary label for confined tasks
62 * @profile: the current profile (NOT NULL)
63 * @exec: profile to transition to on next exec (MAYBE NULL)
64 * @previous: profile the task may return to (MAYBE NULL)
65 * @token: magic value the task must know for returning to @previous_profile
67 * Contains the task's current profile (which could change due to
68 * change_hat). Plus the hat_magic needed during change_hat.
70 * TODO: make so a task can be confined by a stack of contexts
73 struct aa_profile
*profile
;
74 struct aa_profile
*onexec
;
75 struct aa_profile
*previous
;
79 struct aa_task_ctx
*aa_alloc_task_context(gfp_t flags
);
80 void aa_free_task_context(struct aa_task_ctx
*ctx
);
81 void aa_dup_task_context(struct aa_task_ctx
*new,
82 const struct aa_task_ctx
*old
);
83 int aa_replace_current_profile(struct aa_profile
*profile
);
84 int aa_set_current_onexec(struct aa_profile
*profile
);
85 int aa_set_current_hat(struct aa_profile
*profile
, u64 token
);
86 int aa_restore_previous_profile(u64 cookie
);
87 struct aa_profile
*aa_get_task_profile(struct task_struct
*task
);
91 * aa_cred_profile - obtain cred's profiles
92 * @cred: cred to obtain profiles from (NOT NULL)
94 * Returns: confining profile
96 * does NOT increment reference count
98 static inline struct aa_profile
*aa_cred_profile(const struct cred
*cred
)
100 struct aa_task_ctx
*ctx
= cred_ctx(cred
);
102 AA_BUG(!ctx
|| !ctx
->profile
);
107 * __aa_task_profile - retrieve another task's profile
108 * @task: task to query (NOT NULL)
110 * Returns: @task's profile without incrementing its ref count
112 * If @task != current needs to be called in RCU safe critical section
114 static inline struct aa_profile
*__aa_task_profile(struct task_struct
*task
)
116 return aa_cred_profile(__task_cred(task
));
120 * __aa_task_is_confined - determine if @task has any confinement
121 * @task: task to check confinement of (NOT NULL)
123 * If @task != current needs to be called in RCU safe critical section
125 static inline bool __aa_task_is_confined(struct task_struct
*task
)
127 return !unconfined(__aa_task_profile(task
));
131 * __aa_current_profile - find the current tasks confining profile
133 * Returns: up to date confining profile or the ns unconfined profile (NOT NULL)
135 * This fn will not update the tasks cred to the most up to date version
136 * of the profile so it is safe to call when inside of locks.
138 static inline struct aa_profile
*__aa_current_profile(void)
140 return aa_cred_profile(current_cred());
144 * aa_current_profile - find the current tasks confining profile and do updates
146 * Returns: up to date confining profile or the ns unconfined profile (NOT NULL)
148 * This fn will update the tasks cred structure if the profile has been
149 * replaced. Not safe to call inside locks
151 static inline struct aa_profile
*aa_current_profile(void)
153 const struct aa_task_ctx
*ctx
= current_ctx();
154 struct aa_profile
*profile
;
156 AA_BUG(!ctx
|| !ctx
->profile
);
158 if (profile_is_stale(ctx
->profile
)) {
159 profile
= aa_get_newest_profile(ctx
->profile
);
160 aa_replace_current_profile(profile
);
161 aa_put_profile(profile
);
168 static inline struct aa_ns
*aa_get_current_ns(void)
170 return aa_get_ns(__aa_current_profile()->ns
);
174 * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
175 * @ctx: task context to clear (NOT NULL)
177 static inline void aa_clear_task_ctx_trans(struct aa_task_ctx
*ctx
)
179 aa_put_profile(ctx
->previous
);
180 aa_put_profile(ctx
->onexec
);
181 ctx
->previous
= NULL
;
186 #endif /* __AA_CONTEXT_H */