Commit | Line | Data |
---|---|---|
c88d4c7b JJ |
1 | /* |
2 | * AppArmor security module | |
3 | * | |
4 | * This file contains AppArmor policy definitions. | |
5 | * | |
6 | * Copyright (C) 1998-2008 Novell/SUSE | |
7 | * Copyright 2009-2010 Canonical Ltd. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License as | |
11 | * published by the Free Software Foundation, version 2 of the | |
12 | * License. | |
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 | ||
2d4cee7e | 32 | extern const char *const profile_mode_names[]; |
c88d4c7b JJ |
33 | #define APPARMOR_NAMES_MAX_INDEX 3 |
34 | ||
50c5ecd5 JJ |
35 | #define PROFILE_MODE(_profile, _mode) \ |
36 | ((aa_g_profile_mode == (_mode)) || \ | |
37 | ((_profile)->mode == (_mode))) | |
c88d4c7b | 38 | |
50c5ecd5 JJ |
39 | #define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN) |
40 | ||
41 | #define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL) | |
c88d4c7b JJ |
42 | |
43 | #define PROFILE_IS_HAT(_profile) ((_profile)->flags & PFLAG_HAT) | |
44 | ||
01e2b670 JJ |
45 | #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2) |
46 | ||
c88d4c7b JJ |
47 | /* |
48 | * FIXME: currently need a clean way to replace and remove profiles as a | |
49 | * set. It should be done at the namespace level. | |
50 | * Either, with a set of profiles loaded at the namespace level or via | |
51 | * a mark and remove marked interface. | |
52 | */ | |
53 | enum profile_mode { | |
54 | APPARMOR_ENFORCE, /* enforce access rules */ | |
55 | APPARMOR_COMPLAIN, /* allow and log access violations */ | |
56 | APPARMOR_KILL, /* kill task on access violation */ | |
57 | }; | |
58 | ||
59 | enum profile_flags { | |
60 | PFLAG_HAT = 1, /* profile is a hat */ | |
61 | PFLAG_UNCONFINED = 2, /* profile is an unconfined profile */ | |
62 | PFLAG_NULL = 4, /* profile is null learning profile */ | |
63 | PFLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */ | |
64 | PFLAG_IMMUTABLE = 0x10, /* don't allow changes/replacement */ | |
65 | PFLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */ | |
66 | PFLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */ | |
67 | PFLAG_OLD_NULL_TRANS = 0x100, /* use // as the null transition */ | |
68 | ||
69 | /* These flags must correspond with PATH_flags */ | |
70 | PFLAG_MEDIATE_DELETED = 0x10000, /* mediate instead delegate deleted */ | |
71 | }; | |
72 | ||
73 | struct aa_profile; | |
74 | ||
75 | /* struct aa_policy - common part of both namespaces and profiles | |
76 | * @name: name of the object | |
77 | * @hname - The hierarchical name | |
78 | * @count: reference count of the obj | |
79 | * @list: list policy object is on | |
01e2b670 | 80 | * @rcu: rcu head used when removing from @list |
c88d4c7b JJ |
81 | * @profiles: head of the profiles list contained in the object |
82 | */ | |
83 | struct aa_policy { | |
84 | char *name; | |
85 | char *hname; | |
86 | struct kref count; | |
87 | struct list_head list; | |
88 | struct list_head profiles; | |
01e2b670 | 89 | struct rcu_head rcu; |
c88d4c7b JJ |
90 | }; |
91 | ||
92 | /* struct aa_ns_acct - accounting of profiles in namespace | |
93 | * @max_size: maximum space allowed for all profiles in namespace | |
94 | * @max_count: maximum number of profiles that can be in this namespace | |
95 | * @size: current size of profiles | |
96 | * @count: current count of profiles (includes null profiles) | |
97 | */ | |
98 | struct aa_ns_acct { | |
99 | int max_size; | |
100 | int max_count; | |
101 | int size; | |
102 | int count; | |
103 | }; | |
104 | ||
105 | /* struct aa_namespace - namespace for a set of profiles | |
106 | * @base: common policy | |
107 | * @parent: parent of namespace | |
108 | * @lock: lock for modifying the object | |
109 | * @acct: accounting for the namespace | |
110 | * @unconfined: special unconfined profile for the namespace | |
111 | * @sub_ns: list of namespaces under the current namespace. | |
a4987857 | 112 | * @uniq_null: uniq value used for null learning profiles |
c88d4c7b JJ |
113 | * |
114 | * An aa_namespace defines the set profiles that are searched to determine | |
115 | * which profile to attach to a task. Profiles can not be shared between | |
116 | * aa_namespaces and profile names within a namespace are guaranteed to be | |
117 | * unique. When profiles in separate namespaces have the same name they | |
118 | * are NOT considered to be equivalent. | |
119 | * | |
120 | * Namespaces are hierarchical and only namespaces and profiles below the | |
121 | * current namespace are visible. | |
122 | * | |
123 | * Namespace names must be unique and can not contain the characters :/\0 | |
124 | * | |
125 | * FIXME TODO: add vserver support of namespaces (can it all be done in | |
126 | * userspace?) | |
127 | */ | |
128 | struct aa_namespace { | |
129 | struct aa_policy base; | |
130 | struct aa_namespace *parent; | |
01e2b670 | 131 | struct mutex lock; |
c88d4c7b JJ |
132 | struct aa_ns_acct acct; |
133 | struct aa_profile *unconfined; | |
134 | struct list_head sub_ns; | |
a4987857 | 135 | atomic_t uniq_null; |
c88d4c7b JJ |
136 | }; |
137 | ||
ad5ff3db JJ |
138 | /* struct aa_policydb - match engine for a policy |
139 | * dfa: dfa pattern match | |
140 | * start: set of start states for the different classes of data | |
141 | */ | |
142 | struct aa_policydb { | |
143 | /* Generic policy DFA specific rule types will be subsections of it */ | |
144 | struct aa_dfa *dfa; | |
145 | unsigned int start[AA_CLASS_LAST + 1]; | |
146 | ||
147 | }; | |
148 | ||
c88d4c7b JJ |
149 | /* struct aa_profile - basic confinement data |
150 | * @base - base components of the profile (name, refcount, lists, lock ...) | |
151 | * @parent: parent of profile | |
152 | * @ns: namespace the profile is in | |
153 | * @replacedby: is set to the profile that replaced this profile | |
154 | * @rename: optional profile name that this profile renamed | |
155 | * @xmatch: optional extended matching for unconfined executables names | |
156 | * @xmatch_len: xmatch prefix len, used to determine xmatch priority | |
c88d4c7b JJ |
157 | * @audit: the auditing mode of the profile |
158 | * @mode: the enforcement mode of the profile | |
159 | * @flags: flags controlling profile behavior | |
160 | * @path_flags: flags controlling path generation behavior | |
161 | * @size: the memory consumed by this profiles rules | |
ad5ff3db | 162 | * @policy: general match rules governing policy |
c88d4c7b JJ |
163 | * @file: The set of rules governing basic file access and domain transitions |
164 | * @caps: capabilities for the profile | |
165 | * @rlimits: rlimits for the profile | |
166 | * | |
167 | * The AppArmor profile contains the basic confinement data. Each profile | |
168 | * has a name, and exists in a namespace. The @name and @exec_match are | |
169 | * used to determine profile attachment against unconfined tasks. All other | |
170 | * attachments are determined by profile X transition rules. | |
171 | * | |
172 | * The @replacedby field is write protected by the profile lock. Reads | |
01e2b670 | 173 | * are assumed to be atomic. |
c88d4c7b JJ |
174 | * |
175 | * Profiles have a hierarchy where hats and children profiles keep | |
176 | * a reference to their parent. | |
177 | * | |
178 | * Profile names can not begin with a : and can not contain the \0 | |
179 | * character. If a profile name begins with / it will be considered when | |
180 | * determining profile attachment on "unconfined" tasks. | |
181 | */ | |
182 | struct aa_profile { | |
183 | struct aa_policy base; | |
01e2b670 | 184 | struct aa_profile __rcu *parent; |
c88d4c7b JJ |
185 | |
186 | struct aa_namespace *ns; | |
187 | struct aa_profile *replacedby; | |
188 | const char *rename; | |
189 | ||
190 | struct aa_dfa *xmatch; | |
191 | int xmatch_len; | |
c88d4c7b JJ |
192 | enum audit_mode audit; |
193 | enum profile_mode mode; | |
194 | u32 flags; | |
195 | u32 path_flags; | |
196 | int size; | |
197 | ||
ad5ff3db | 198 | struct aa_policydb policy; |
c88d4c7b JJ |
199 | struct aa_file_rules file; |
200 | struct aa_caps caps; | |
201 | struct aa_rlimit rlimits; | |
202 | }; | |
203 | ||
204 | extern struct aa_namespace *root_ns; | |
205 | extern enum profile_mode aa_g_profile_mode; | |
206 | ||
207 | void aa_add_profile(struct aa_policy *common, struct aa_profile *profile); | |
208 | ||
209 | bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view); | |
210 | const char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child); | |
211 | int aa_alloc_root_ns(void); | |
212 | void aa_free_root_ns(void); | |
213 | void aa_free_namespace_kref(struct kref *kref); | |
214 | ||
215 | struct aa_namespace *aa_find_namespace(struct aa_namespace *root, | |
216 | const char *name); | |
217 | ||
218 | static inline struct aa_policy *aa_get_common(struct aa_policy *c) | |
219 | { | |
220 | if (c) | |
221 | kref_get(&c->count); | |
222 | ||
223 | return c; | |
224 | } | |
225 | ||
226 | /** | |
227 | * aa_get_namespace - increment references count on @ns | |
228 | * @ns: namespace to increment reference count of (MAYBE NULL) | |
229 | * | |
230 | * Returns: pointer to @ns, if @ns is NULL returns NULL | |
231 | * Requires: @ns must be held with valid refcount when called | |
232 | */ | |
233 | static inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns) | |
234 | { | |
235 | if (ns) | |
236 | kref_get(&(ns->base.count)); | |
237 | ||
238 | return ns; | |
239 | } | |
240 | ||
241 | /** | |
242 | * aa_put_namespace - decrement refcount on @ns | |
243 | * @ns: namespace to put reference of | |
244 | * | |
245 | * Decrement reference count of @ns and if no longer in use free it | |
246 | */ | |
247 | static inline void aa_put_namespace(struct aa_namespace *ns) | |
248 | { | |
249 | if (ns) | |
250 | kref_put(&ns->base.count, aa_free_namespace_kref); | |
251 | } | |
252 | ||
253 | struct aa_profile *aa_alloc_profile(const char *name); | |
254 | struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat); | |
255 | void aa_free_profile_kref(struct kref *kref); | |
256 | struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name); | |
257 | struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name); | |
258 | struct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name); | |
259 | ||
260 | ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace); | |
261 | ssize_t aa_remove_profiles(char *name, size_t size); | |
262 | ||
263 | #define PROF_ADD 1 | |
264 | #define PROF_REPLACE 0 | |
265 | ||
266 | #define unconfined(X) ((X)->flags & PFLAG_UNCONFINED) | |
267 | ||
268 | /** | |
269 | * aa_newest_version - find the newest version of @profile | |
270 | * @profile: the profile to check for newer versions of (NOT NULL) | |
271 | * | |
272 | * Returns: newest version of @profile, if @profile is the newest version | |
273 | * return @profile. | |
274 | * | |
275 | * NOTE: the profile returned is not refcounted, The refcount on @profile | |
276 | * must be held until the caller decides what to do with the returned newest | |
277 | * version. | |
278 | */ | |
279 | static inline struct aa_profile *aa_newest_version(struct aa_profile *profile) | |
280 | { | |
281 | while (profile->replacedby) | |
282 | profile = profile->replacedby; | |
283 | ||
284 | return profile; | |
285 | } | |
286 | ||
287 | /** | |
288 | * aa_get_profile - increment refcount on profile @p | |
289 | * @p: profile (MAYBE NULL) | |
290 | * | |
291 | * Returns: pointer to @p if @p is NULL will return NULL | |
292 | * Requires: @p must be held with valid refcount when called | |
293 | */ | |
294 | static inline struct aa_profile *aa_get_profile(struct aa_profile *p) | |
295 | { | |
296 | if (p) | |
297 | kref_get(&(p->base.count)); | |
298 | ||
299 | return p; | |
300 | } | |
301 | ||
01e2b670 JJ |
302 | /** |
303 | * aa_get_profile_not0 - increment refcount on profile @p found via lookup | |
304 | * @p: profile (MAYBE NULL) | |
305 | * | |
306 | * Returns: pointer to @p if @p is NULL will return NULL | |
307 | * Requires: @p must be held with valid refcount when called | |
308 | */ | |
309 | static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p) | |
310 | { | |
311 | if (p && kref_get_not0(&p->base.count)) | |
312 | return p; | |
313 | ||
314 | return NULL; | |
315 | } | |
316 | ||
317 | /** | |
318 | * aa_get_profile_rcu - increment a refcount profile that can be replaced | |
319 | * @p: pointer to profile that can be replaced (NOT NULL) | |
320 | * | |
321 | * Returns: pointer to a refcounted profile. | |
322 | * else NULL if no profile | |
323 | */ | |
324 | static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p) | |
325 | { | |
326 | struct aa_profile *c; | |
327 | ||
328 | rcu_read_lock(); | |
329 | do { | |
330 | c = rcu_dereference(*p); | |
331 | } while (c && !kref_get_not0(&c->base.count)); | |
332 | rcu_read_unlock(); | |
333 | ||
334 | return c; | |
335 | } | |
336 | ||
c88d4c7b JJ |
337 | /** |
338 | * aa_put_profile - decrement refcount on profile @p | |
339 | * @p: profile (MAYBE NULL) | |
340 | */ | |
341 | static inline void aa_put_profile(struct aa_profile *p) | |
342 | { | |
343 | if (p) | |
344 | kref_put(&p->base.count, aa_free_profile_kref); | |
345 | } | |
346 | ||
347 | static inline int AUDIT_MODE(struct aa_profile *profile) | |
348 | { | |
349 | if (aa_g_audit != AUDIT_NORMAL) | |
350 | return aa_g_audit; | |
351 | ||
352 | return profile->audit; | |
353 | } | |
354 | ||
355 | bool aa_may_manage_policy(int op); | |
356 | ||
357 | #endif /* __AA_POLICY_H */ |