1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef __AA_POLICY_H
16#define __AA_POLICY_H
17
18#include <linux/capability.h>
19#include <linux/cred.h>
20#include <linux/kref.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/socket.h>
24
25#include "apparmor.h"
26#include "audit.h"
27#include "capability.h"
28#include "domain.h"
29#include "file.h"
30#include "resource.h"
31
32extern const char *const aa_profile_mode_names[];
33#define APPARMOR_MODE_NAMES_MAX_INDEX 4
34
35#define PROFILE_MODE(_profile, _mode) \
36 ((aa_g_profile_mode == (_mode)) || \
37 ((_profile)->mode == (_mode)))
38
39#define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN)
40
41#define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL)
42
43#define PROFILE_IS_HAT(_profile) ((_profile)->flags & PFLAG_HAT)
44
45#define PROFILE_INVALID(_profile) ((_profile)->flags & PFLAG_INVALID)
46
47#define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
48
49
50
51
52
53
54
55enum profile_mode {
56 APPARMOR_ENFORCE,
57 APPARMOR_COMPLAIN,
58 APPARMOR_KILL,
59 APPARMOR_UNCONFINED,
60};
61
62enum profile_flags {
63 PFLAG_HAT = 1,
64 PFLAG_NULL = 4,
65 PFLAG_IX_ON_NAME_ERROR = 8,
66 PFLAG_IMMUTABLE = 0x10,
67 PFLAG_USER_DEFINED = 0x20,
68 PFLAG_NO_LIST_REF = 0x40,
69 PFLAG_OLD_NULL_TRANS = 0x100,
70 PFLAG_INVALID = 0x200,
71 PFLAG_NS_COUNT = 0x400,
72
73
74 PFLAG_MEDIATE_DELETED = 0x10000,
75};
76
77struct aa_profile;
78
79
80
81
82
83
84
85struct aa_policy {
86 char *name;
87 char *hname;
88 struct list_head list;
89 struct list_head profiles;
90};
91
92
93
94
95
96
97
98struct aa_ns_acct {
99 int max_size;
100 int max_count;
101 int size;
102 int count;
103};
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130struct aa_namespace {
131 struct aa_policy base;
132 struct aa_namespace *parent;
133 struct mutex lock;
134 struct aa_ns_acct acct;
135 struct aa_profile *unconfined;
136 struct list_head sub_ns;
137 atomic_t uniq_null;
138 long uniq_id;
139
140 struct dentry *dents[AAFS_NS_SIZEOF];
141};
142
143
144
145
146
147struct aa_policydb {
148
149 struct aa_dfa *dfa;
150 unsigned int start[AA_CLASS_LAST + 1];
151
152};
153
154struct aa_replacedby {
155 struct kref count;
156 struct aa_profile __rcu *profile;
157};
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198struct aa_profile {
199 struct aa_policy base;
200 struct kref count;
201 struct rcu_head rcu;
202 struct aa_profile __rcu *parent;
203
204 struct aa_namespace *ns;
205 struct aa_replacedby *replacedby;
206 const char *rename;
207
208 const char *attach;
209 struct aa_dfa *xmatch;
210 int xmatch_len;
211 enum audit_mode audit;
212 long mode;
213 long flags;
214 u32 path_flags;
215 int size;
216
217 struct aa_policydb policy;
218 struct aa_file_rules file;
219 struct aa_caps caps;
220 struct aa_rlimit rlimits;
221
222 unsigned char *hash;
223 char *dirname;
224 struct dentry *dents[AAFS_PROF_SIZEOF];
225};
226
227extern struct aa_namespace *root_ns;
228extern enum profile_mode aa_g_profile_mode;
229
230void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
231
232bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
233const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
234int aa_alloc_root_ns(void);
235void aa_free_root_ns(void);
236void aa_free_namespace_kref(struct kref *kref);
237
238struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
239 const char *name);
240
241
242void aa_free_replacedby_kref(struct kref *kref);
243struct aa_profile *aa_alloc_profile(const char *name);
244struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
245void aa_free_profile(struct aa_profile *profile);
246void aa_free_profile_kref(struct kref *kref);
247struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
248struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
249struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
250
251ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
252ssize_t aa_remove_profiles(char *name, size_t size);
253
254#define PROF_ADD 1
255#define PROF_REPLACE 0
256
257#define unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
258
259
260static inline struct aa_profile *aa_deref_parent(struct aa_profile *p)
261{
262 return rcu_dereference_protected(p->parent,
263 mutex_is_locked(&p->ns->lock));
264}
265
266
267
268
269
270
271
272
273static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
274{
275 if (p)
276 kref_get(&(p->count));
277
278 return p;
279}
280
281
282
283
284
285
286
287
288static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
289{
290 if (p && kref_get_not0(&p->count))
291 return p;
292
293 return NULL;
294}
295
296
297
298
299
300
301
302
303static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
304{
305 struct aa_profile *c;
306
307 rcu_read_lock();
308 do {
309 c = rcu_dereference(*p);
310 } while (c && !kref_get_not0(&c->count));
311 rcu_read_unlock();
312
313 return c;
314}
315
316
317
318
319
320
321
322
323
324static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
325{
326 if (!p)
327 return NULL;
328
329 if (PROFILE_INVALID(p))
330 return aa_get_profile_rcu(&p->replacedby->profile);
331
332 return aa_get_profile(p);
333}
334
335
336
337
338
339static inline void aa_put_profile(struct aa_profile *p)
340{
341 if (p)
342 kref_put(&p->count, aa_free_profile_kref);
343}
344
345static inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p)
346{
347 if (p)
348 kref_get(&(p->count));
349
350 return p;
351}
352
353static inline void aa_put_replacedby(struct aa_replacedby *p)
354{
355 if (p)
356 kref_put(&p->count, aa_free_replacedby_kref);
357}
358
359
360static inline void __aa_update_replacedby(struct aa_profile *orig,
361 struct aa_profile *new)
362{
363 struct aa_profile *tmp;
364 tmp = rcu_dereference_protected(orig->replacedby->profile,
365 mutex_is_locked(&orig->ns->lock));
366 rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new));
367 orig->flags |= PFLAG_INVALID;
368 aa_put_profile(tmp);
369}
370
371
372
373
374
375
376
377
378static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
379{
380 if (ns)
381 aa_get_profile(ns->unconfined);
382
383 return ns;
384}
385
386
387
388
389
390
391
392static inline void aa_put_namespace(struct aa_namespace *ns)
393{
394 if (ns)
395 aa_put_profile(ns->unconfined);
396}
397
398static inline int AUDIT_MODE(struct aa_profile *profile)
399{
400 if (aa_g_audit != AUDIT_NORMAL)
401 return aa_g_audit;
402
403 return profile->audit;
404}
405
406bool aa_may_manage_policy(int op);
407
408#endif
409