Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / security / apparmor / include / cred.h
blob0b9ae4804ef73d10937f166ee08eac6718fb2bb8
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
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.
9 */
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>
18 #include "label.h"
19 #include "policy_ns.h"
20 #include "task.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;
26 AA_BUG(!blob);
27 return *blob;
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;
35 AA_BUG(!blob);
36 *blob = label;
39 /**
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);
51 AA_BUG(!label);
52 return label;
55 /**
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 /**
67 * __aa_task_raw_label - retrieve another task's label
68 * @task: task to query (NOT NULL)
70 * Returns: @task's label without incrementing its ref count
72 * If @task != current needs to be called in RCU safe critical section
74 static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
76 return aa_cred_raw_label(__task_cred(task));
79 /**
80 * aa_current_raw_label - find the current tasks confining label
82 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
84 * This fn will not update the tasks cred to the most up to date version
85 * of the label so it is safe to call when inside of locks.
87 static inline struct aa_label *aa_current_raw_label(void)
89 return aa_cred_raw_label(current_cred());
92 /**
93 * aa_get_current_label - get the newest version of the current tasks label
95 * Returns: newest version of confining label (NOT NULL)
97 * This fn will not update the tasks cred, so it is safe inside of locks
99 * The returned reference must be put with aa_put_label()
101 static inline struct aa_label *aa_get_current_label(void)
103 struct aa_label *l = aa_current_raw_label();
105 if (label_is_stale(l))
106 return aa_get_newest_label(l);
107 return aa_get_label(l);
110 #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
113 * end_label_crit_section - put a reference found with begin_current_label..
114 * @label: label reference to put
116 * Should only be used with a reference obtained with
117 * begin_current_label_crit_section and never used in situations where the
118 * task cred may be updated
120 static inline void end_current_label_crit_section(struct aa_label *label)
122 if (label != aa_current_raw_label())
123 aa_put_label(label);
127 * __begin_current_label_crit_section - current's confining label
129 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
131 * safe to call inside locks
133 * The returned reference must be put with __end_current_label_crit_section()
134 * This must NOT be used if the task cred could be updated within the
135 * critical section between __begin_current_label_crit_section() ..
136 * __end_current_label_crit_section()
138 static inline struct aa_label *__begin_current_label_crit_section(void)
140 struct aa_label *label = aa_current_raw_label();
142 if (label_is_stale(label))
143 label = aa_get_newest_label(label);
145 return label;
149 * begin_current_label_crit_section - current's confining label and update it
151 * Returns: up to date confining label or the ns unconfined label (NOT NULL)
153 * Not safe to call inside locks
155 * The returned reference must be put with end_current_label_crit_section()
156 * This must NOT be used if the task cred could be updated within the
157 * critical section between begin_current_label_crit_section() ..
158 * end_current_label_crit_section()
160 static inline struct aa_label *begin_current_label_crit_section(void)
162 struct aa_label *label = aa_current_raw_label();
164 might_sleep();
166 if (label_is_stale(label)) {
167 label = aa_get_newest_label(label);
168 if (aa_replace_current_label(label) == 0)
169 /* task cred will keep the reference */
170 aa_put_label(label);
173 return label;
176 static inline struct aa_ns *aa_get_current_ns(void)
178 struct aa_label *label;
179 struct aa_ns *ns;
181 label = __begin_current_label_crit_section();
182 ns = aa_get_ns(labels_ns(label));
183 __end_current_label_crit_section(label);
185 return ns;
188 #endif /* __AA_CONTEXT_H */