1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * AppArmor security module
5 * This file contains AppArmor contexts used to associate "labels" to objects.
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
11 #ifndef __AA_CONTEXT_H
12 #define __AA_CONTEXT_H
14 #include <linux/cred.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
19 #include "policy_ns.h"
22 static inline struct aa_label
*cred_label(const struct cred
*cred
)
24 struct aa_label
**blob
= cred
->security
+ apparmor_blob_sizes
.lbs_cred
;
30 static inline void set_cred_label(const struct cred
*cred
,
31 struct aa_label
*label
)
33 struct aa_label
**blob
= cred
->security
+ apparmor_blob_sizes
.lbs_cred
;
40 * aa_cred_raw_label - obtain cred's label
41 * @cred: cred to obtain label from (NOT NULL)
43 * Returns: confining label
45 * does NOT increment reference count
47 static inline struct aa_label
*aa_cred_raw_label(const struct cred
*cred
)
49 struct aa_label
*label
= cred_label(cred
);
56 * aa_get_newest_cred_label - obtain the newest label on a cred
57 * @cred: cred to obtain label from (NOT NULL)
59 * Returns: newest version of confining label
61 static inline struct aa_label
*aa_get_newest_cred_label(const struct cred
*cred
)
63 return aa_get_newest_label(aa_cred_raw_label(cred
));
66 static inline struct aa_label
*aa_get_newest_cred_label_condref(const struct cred
*cred
,
69 struct aa_label
*l
= aa_cred_raw_label(cred
);
71 if (unlikely(label_is_stale(l
))) {
73 return aa_get_newest_label(l
);
80 static inline void aa_put_label_condref(struct aa_label
*l
, bool needput
)
82 if (unlikely(needput
))
87 * aa_current_raw_label - find the current tasks confining label
89 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
91 * This fn will not update the tasks cred to the most up to date version
92 * of the label so it is safe to call when inside of locks.
94 static inline struct aa_label
*aa_current_raw_label(void)
96 return aa_cred_raw_label(current_cred());
100 * aa_get_current_label - get the newest version of the current tasks label
102 * Returns: newest version of confining label (NOT NULL)
104 * This fn will not update the tasks cred, so it is safe inside of locks
106 * The returned reference must be put with aa_put_label()
108 static inline struct aa_label
*aa_get_current_label(void)
110 struct aa_label
*l
= aa_current_raw_label();
112 if (label_is_stale(l
))
113 return aa_get_newest_label(l
);
114 return aa_get_label(l
);
117 #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
120 * end_label_crit_section - put a reference found with begin_current_label..
121 * @label: label reference to put
123 * Should only be used with a reference obtained with
124 * begin_current_label_crit_section and never used in situations where the
125 * task cred may be updated
127 static inline void end_current_label_crit_section(struct aa_label
*label
)
129 if (label
!= aa_current_raw_label())
134 * __begin_current_label_crit_section - current's confining label
136 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
138 * safe to call inside locks
140 * The returned reference must be put with __end_current_label_crit_section()
141 * This must NOT be used if the task cred could be updated within the
142 * critical section between __begin_current_label_crit_section() ..
143 * __end_current_label_crit_section()
145 static inline struct aa_label
*__begin_current_label_crit_section(void)
147 struct aa_label
*label
= aa_current_raw_label();
149 if (label_is_stale(label
))
150 label
= aa_get_newest_label(label
);
156 * begin_current_label_crit_section - current's confining label and update it
158 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
160 * Not safe to call inside locks
162 * The returned reference must be put with end_current_label_crit_section()
163 * This must NOT be used if the task cred could be updated within the
164 * critical section between begin_current_label_crit_section() ..
165 * end_current_label_crit_section()
167 static inline struct aa_label
*begin_current_label_crit_section(void)
169 struct aa_label
*label
= aa_current_raw_label();
173 if (label_is_stale(label
)) {
174 label
= aa_get_newest_label(label
);
175 if (aa_replace_current_label(label
) == 0)
176 /* task cred will keep the reference */
183 static inline struct aa_ns
*aa_get_current_ns(void)
185 struct aa_label
*label
;
188 label
= __begin_current_label_crit_section();
189 ns
= aa_get_ns(labels_ns(label
));
190 __end_current_label_crit_section(label
);
195 #endif /* __AA_CONTEXT_H */