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