xref: /OK3568_Linux_fs/kernel/security/apparmor/include/cred.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * AppArmor security module
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file contains AppArmor contexts used to associate "labels" to objects.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 1998-2008 Novell/SUSE
8*4882a593Smuzhiyun  * Copyright 2009-2010 Canonical Ltd.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #ifndef __AA_CONTEXT_H
12*4882a593Smuzhiyun #define __AA_CONTEXT_H
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/cred.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/sched.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "label.h"
19*4882a593Smuzhiyun #include "policy_ns.h"
20*4882a593Smuzhiyun #include "task.h"
21*4882a593Smuzhiyun 
cred_label(const struct cred * cred)22*4882a593Smuzhiyun static inline struct aa_label *cred_label(const struct cred *cred)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	AA_BUG(!blob);
27*4882a593Smuzhiyun 	return *blob;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
set_cred_label(const struct cred * cred,struct aa_label * label)30*4882a593Smuzhiyun static inline void set_cred_label(const struct cred *cred,
31*4882a593Smuzhiyun 				  struct aa_label *label)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	AA_BUG(!blob);
36*4882a593Smuzhiyun 	*blob = label;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun  * aa_cred_raw_label - obtain cred's label
41*4882a593Smuzhiyun  * @cred: cred to obtain label from  (NOT NULL)
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * Returns: confining label
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * does NOT increment reference count
46*4882a593Smuzhiyun  */
aa_cred_raw_label(const struct cred * cred)47*4882a593Smuzhiyun static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	struct aa_label *label = cred_label(cred);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	AA_BUG(!label);
52*4882a593Smuzhiyun 	return label;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun  * aa_get_newest_cred_label - obtain the newest label on a cred
57*4882a593Smuzhiyun  * @cred: cred to obtain label from (NOT NULL)
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Returns: newest version of confining label
60*4882a593Smuzhiyun  */
aa_get_newest_cred_label(const struct cred * cred)61*4882a593Smuzhiyun static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	return aa_get_newest_label(aa_cred_raw_label(cred));
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /**
67*4882a593Smuzhiyun  * __aa_task_raw_label - retrieve another task's label
68*4882a593Smuzhiyun  * @task: task to query  (NOT NULL)
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * Returns: @task's label without incrementing its ref count
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * If @task != current needs to be called in RCU safe critical section
73*4882a593Smuzhiyun  */
__aa_task_raw_label(struct task_struct * task)74*4882a593Smuzhiyun static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	return aa_cred_raw_label(__task_cred(task));
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun  * aa_current_raw_label - find the current tasks confining label
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  * Returns: up to date confining label or the ns unconfined label (NOT NULL)
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * This fn will not update the tasks cred to the most up to date version
85*4882a593Smuzhiyun  * of the label so it is safe to call when inside of locks.
86*4882a593Smuzhiyun  */
aa_current_raw_label(void)87*4882a593Smuzhiyun static inline struct aa_label *aa_current_raw_label(void)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	return aa_cred_raw_label(current_cred());
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * aa_get_current_label - get the newest version of the current tasks label
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * Returns: newest version of confining label (NOT NULL)
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * This fn will not update the tasks cred, so it is safe inside of locks
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * The returned reference must be put with aa_put_label()
100*4882a593Smuzhiyun  */
aa_get_current_label(void)101*4882a593Smuzhiyun static inline struct aa_label *aa_get_current_label(void)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct aa_label *l = aa_current_raw_label();
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (label_is_stale(l))
106*4882a593Smuzhiyun 		return aa_get_newest_label(l);
107*4882a593Smuzhiyun 	return aa_get_label(l);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun  * end_label_crit_section - put a reference found with begin_current_label..
114*4882a593Smuzhiyun  * @label: label reference to put
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * Should only be used with a reference obtained with
117*4882a593Smuzhiyun  * begin_current_label_crit_section and never used in situations where the
118*4882a593Smuzhiyun  * task cred may be updated
119*4882a593Smuzhiyun  */
end_current_label_crit_section(struct aa_label * label)120*4882a593Smuzhiyun static inline void end_current_label_crit_section(struct aa_label *label)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	if (label != aa_current_raw_label())
123*4882a593Smuzhiyun 		aa_put_label(label);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * __begin_current_label_crit_section - current's confining label
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * Returns: up to date confining label or the ns unconfined label (NOT NULL)
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * safe to call inside locks
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * The returned reference must be put with __end_current_label_crit_section()
134*4882a593Smuzhiyun  * This must NOT be used if the task cred could be updated within the
135*4882a593Smuzhiyun  * critical section between __begin_current_label_crit_section() ..
136*4882a593Smuzhiyun  * __end_current_label_crit_section()
137*4882a593Smuzhiyun  */
__begin_current_label_crit_section(void)138*4882a593Smuzhiyun static inline struct aa_label *__begin_current_label_crit_section(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	struct aa_label *label = aa_current_raw_label();
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (label_is_stale(label))
143*4882a593Smuzhiyun 		label = aa_get_newest_label(label);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	return label;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /**
149*4882a593Smuzhiyun  * begin_current_label_crit_section - current's confining label and update it
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * Returns: up to date confining label or the ns unconfined label (NOT NULL)
152*4882a593Smuzhiyun  *
153*4882a593Smuzhiyun  * Not safe to call inside locks
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * The returned reference must be put with end_current_label_crit_section()
156*4882a593Smuzhiyun  * This must NOT be used if the task cred could be updated within the
157*4882a593Smuzhiyun  * critical section between begin_current_label_crit_section() ..
158*4882a593Smuzhiyun  * end_current_label_crit_section()
159*4882a593Smuzhiyun  */
begin_current_label_crit_section(void)160*4882a593Smuzhiyun static inline struct aa_label *begin_current_label_crit_section(void)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct aa_label *label = aa_current_raw_label();
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	might_sleep();
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	if (label_is_stale(label)) {
167*4882a593Smuzhiyun 		label = aa_get_newest_label(label);
168*4882a593Smuzhiyun 		if (aa_replace_current_label(label) == 0)
169*4882a593Smuzhiyun 			/* task cred will keep the reference */
170*4882a593Smuzhiyun 			aa_put_label(label);
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	return label;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
aa_get_current_ns(void)176*4882a593Smuzhiyun static inline struct aa_ns *aa_get_current_ns(void)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	struct aa_label *label;
179*4882a593Smuzhiyun 	struct aa_ns *ns;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	label  = __begin_current_label_crit_section();
182*4882a593Smuzhiyun 	ns = aa_get_ns(labels_ns(label));
183*4882a593Smuzhiyun 	__end_current_label_crit_section(label);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return ns;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun #endif /* __AA_CONTEXT_H */
189