Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Implementation of the kernel access vector cache (AVC). | |
3 | * | |
4 | * Authors: Stephen Smalley, <sds@epoch.ncsc.mil> | |
95fff33b | 5 | * James Morris <jmorris@redhat.com> |
1da177e4 LT |
6 | * |
7 | * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com> | |
95fff33b | 8 | * Replaced the avc_lock spinlock by RCU. |
1da177e4 LT |
9 | * |
10 | * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2, | |
95fff33b | 14 | * as published by the Free Software Foundation. |
1da177e4 LT |
15 | */ |
16 | #include <linux/types.h> | |
17 | #include <linux/stddef.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/fs.h> | |
21 | #include <linux/dcache.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <linux/percpu.h> | |
25 | #include <net/sock.h> | |
26 | #include <linux/un.h> | |
27 | #include <net/af_unix.h> | |
28 | #include <linux/ip.h> | |
29 | #include <linux/audit.h> | |
30 | #include <linux/ipv6.h> | |
31 | #include <net/ipv6.h> | |
32 | #include "avc.h" | |
33 | #include "avc_ss.h" | |
c6d3aaa4 | 34 | #include "classmap.h" |
5c458998 | 35 | |
1da177e4 LT |
36 | #define AVC_CACHE_SLOTS 512 |
37 | #define AVC_DEF_CACHE_THRESHOLD 512 | |
38 | #define AVC_CACHE_RECLAIM 16 | |
39 | ||
40 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS | |
95fff33b | 41 | #define avc_cache_stats_incr(field) \ |
1da177e4 LT |
42 | do { \ |
43 | per_cpu(avc_cache_stats, get_cpu()).field++; \ | |
44 | put_cpu(); \ | |
45 | } while (0) | |
46 | #else | |
47 | #define avc_cache_stats_incr(field) do {} while (0) | |
48 | #endif | |
49 | ||
50 | struct avc_entry { | |
51 | u32 ssid; | |
52 | u32 tsid; | |
53 | u16 tclass; | |
54 | struct av_decision avd; | |
1da177e4 LT |
55 | }; |
56 | ||
57 | struct avc_node { | |
58 | struct avc_entry ae; | |
26036651 | 59 | struct hlist_node list; /* anchored in avc_cache->slots[i] */ |
95fff33b | 60 | struct rcu_head rhead; |
1da177e4 LT |
61 | }; |
62 | ||
63 | struct avc_cache { | |
26036651 | 64 | struct hlist_head slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */ |
1da177e4 LT |
65 | spinlock_t slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */ |
66 | atomic_t lru_hint; /* LRU hint for reclaim scan */ | |
67 | atomic_t active_nodes; | |
68 | u32 latest_notif; /* latest revocation notification */ | |
69 | }; | |
70 | ||
71 | struct avc_callback_node { | |
72 | int (*callback) (u32 event, u32 ssid, u32 tsid, | |
95fff33b EP |
73 | u16 tclass, u32 perms, |
74 | u32 *out_retained); | |
1da177e4 LT |
75 | u32 events; |
76 | u32 ssid; | |
77 | u32 tsid; | |
78 | u16 tclass; | |
79 | u32 perms; | |
80 | struct avc_callback_node *next; | |
81 | }; | |
82 | ||
83 | /* Exported via selinufs */ | |
84 | unsigned int avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD; | |
85 | ||
86 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS | |
87 | DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 }; | |
88 | #endif | |
89 | ||
90 | static struct avc_cache avc_cache; | |
91 | static struct avc_callback_node *avc_callbacks; | |
e18b890b | 92 | static struct kmem_cache *avc_node_cachep; |
1da177e4 LT |
93 | |
94 | static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass) | |
95 | { | |
96 | return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1); | |
97 | } | |
98 | ||
99 | /** | |
100 | * avc_dump_av - Display an access vector in human-readable form. | |
101 | * @tclass: target security class | |
102 | * @av: access vector | |
103 | */ | |
44c2d9bd | 104 | static void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av) |
1da177e4 | 105 | { |
c6d3aaa4 SS |
106 | const char **perms; |
107 | int i, perm; | |
1da177e4 LT |
108 | |
109 | if (av == 0) { | |
110 | audit_log_format(ab, " null"); | |
111 | return; | |
112 | } | |
113 | ||
c6d3aaa4 | 114 | perms = secclass_map[tclass-1].perms; |
1da177e4 LT |
115 | |
116 | audit_log_format(ab, " {"); | |
117 | i = 0; | |
118 | perm = 1; | |
c6d3aaa4 | 119 | while (i < (sizeof(av) * 8)) { |
0bce9527 | 120 | if ((perm & av) && perms[i]) { |
c6d3aaa4 | 121 | audit_log_format(ab, " %s", perms[i]); |
1da177e4 LT |
122 | av &= ~perm; |
123 | } | |
124 | i++; | |
125 | perm <<= 1; | |
126 | } | |
127 | ||
1da177e4 LT |
128 | if (av) |
129 | audit_log_format(ab, " 0x%x", av); | |
130 | ||
131 | audit_log_format(ab, " }"); | |
132 | } | |
133 | ||
134 | /** | |
135 | * avc_dump_query - Display a SID pair and a class in human-readable form. | |
136 | * @ssid: source security identifier | |
137 | * @tsid: target security identifier | |
138 | * @tclass: target security class | |
139 | */ | |
140 | static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass) | |
141 | { | |
142 | int rc; | |
143 | char *scontext; | |
144 | u32 scontext_len; | |
145 | ||
95fff33b | 146 | rc = security_sid_to_context(ssid, &scontext, &scontext_len); |
1da177e4 LT |
147 | if (rc) |
148 | audit_log_format(ab, "ssid=%d", ssid); | |
149 | else { | |
150 | audit_log_format(ab, "scontext=%s", scontext); | |
151 | kfree(scontext); | |
152 | } | |
153 | ||
154 | rc = security_sid_to_context(tsid, &scontext, &scontext_len); | |
155 | if (rc) | |
156 | audit_log_format(ab, " tsid=%d", tsid); | |
157 | else { | |
158 | audit_log_format(ab, " tcontext=%s", scontext); | |
159 | kfree(scontext); | |
160 | } | |
a764ae4b | 161 | |
c6d3aaa4 SS |
162 | BUG_ON(tclass >= ARRAY_SIZE(secclass_map)); |
163 | audit_log_format(ab, " tclass=%s", secclass_map[tclass-1].name); | |
1da177e4 LT |
164 | } |
165 | ||
166 | /** | |
167 | * avc_init - Initialize the AVC. | |
168 | * | |
169 | * Initialize the access vector cache. | |
170 | */ | |
171 | void __init avc_init(void) | |
172 | { | |
173 | int i; | |
174 | ||
175 | for (i = 0; i < AVC_CACHE_SLOTS; i++) { | |
26036651 | 176 | INIT_HLIST_HEAD(&avc_cache.slots[i]); |
1da177e4 LT |
177 | spin_lock_init(&avc_cache.slots_lock[i]); |
178 | } | |
179 | atomic_set(&avc_cache.active_nodes, 0); | |
180 | atomic_set(&avc_cache.lru_hint, 0); | |
181 | ||
182 | avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node), | |
20c2df83 | 183 | 0, SLAB_PANIC, NULL); |
1da177e4 | 184 | |
9ad9ad38 | 185 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_KERNEL, "AVC INITIALIZED\n"); |
1da177e4 LT |
186 | } |
187 | ||
188 | int avc_get_hash_stats(char *page) | |
189 | { | |
190 | int i, chain_len, max_chain_len, slots_used; | |
191 | struct avc_node *node; | |
26036651 | 192 | struct hlist_head *head; |
1da177e4 LT |
193 | |
194 | rcu_read_lock(); | |
195 | ||
196 | slots_used = 0; | |
197 | max_chain_len = 0; | |
198 | for (i = 0; i < AVC_CACHE_SLOTS; i++) { | |
edf3d1ae | 199 | head = &avc_cache.slots[i]; |
26036651 EP |
200 | if (!hlist_empty(head)) { |
201 | struct hlist_node *next; | |
202 | ||
1da177e4 LT |
203 | slots_used++; |
204 | chain_len = 0; | |
26036651 | 205 | hlist_for_each_entry_rcu(node, next, head, list) |
1da177e4 LT |
206 | chain_len++; |
207 | if (chain_len > max_chain_len) | |
208 | max_chain_len = chain_len; | |
209 | } | |
210 | } | |
211 | ||
212 | rcu_read_unlock(); | |
213 | ||
214 | return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n" | |
215 | "longest chain: %d\n", | |
216 | atomic_read(&avc_cache.active_nodes), | |
217 | slots_used, AVC_CACHE_SLOTS, max_chain_len); | |
218 | } | |
219 | ||
220 | static void avc_node_free(struct rcu_head *rhead) | |
221 | { | |
222 | struct avc_node *node = container_of(rhead, struct avc_node, rhead); | |
223 | kmem_cache_free(avc_node_cachep, node); | |
224 | avc_cache_stats_incr(frees); | |
225 | } | |
226 | ||
227 | static void avc_node_delete(struct avc_node *node) | |
228 | { | |
26036651 | 229 | hlist_del_rcu(&node->list); |
1da177e4 LT |
230 | call_rcu(&node->rhead, avc_node_free); |
231 | atomic_dec(&avc_cache.active_nodes); | |
232 | } | |
233 | ||
234 | static void avc_node_kill(struct avc_node *node) | |
235 | { | |
236 | kmem_cache_free(avc_node_cachep, node); | |
237 | avc_cache_stats_incr(frees); | |
238 | atomic_dec(&avc_cache.active_nodes); | |
239 | } | |
240 | ||
241 | static void avc_node_replace(struct avc_node *new, struct avc_node *old) | |
242 | { | |
26036651 | 243 | hlist_replace_rcu(&old->list, &new->list); |
1da177e4 LT |
244 | call_rcu(&old->rhead, avc_node_free); |
245 | atomic_dec(&avc_cache.active_nodes); | |
246 | } | |
247 | ||
248 | static inline int avc_reclaim_node(void) | |
249 | { | |
250 | struct avc_node *node; | |
251 | int hvalue, try, ecx; | |
252 | unsigned long flags; | |
26036651 EP |
253 | struct hlist_head *head; |
254 | struct hlist_node *next; | |
edf3d1ae | 255 | spinlock_t *lock; |
1da177e4 | 256 | |
95fff33b | 257 | for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) { |
1da177e4 | 258 | hvalue = atomic_inc_return(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1); |
edf3d1ae EP |
259 | head = &avc_cache.slots[hvalue]; |
260 | lock = &avc_cache.slots_lock[hvalue]; | |
1da177e4 | 261 | |
edf3d1ae | 262 | if (!spin_trylock_irqsave(lock, flags)) |
1da177e4 LT |
263 | continue; |
264 | ||
61844250 | 265 | rcu_read_lock(); |
26036651 | 266 | hlist_for_each_entry(node, next, head, list) { |
906d27d9 EP |
267 | avc_node_delete(node); |
268 | avc_cache_stats_incr(reclaims); | |
269 | ecx++; | |
270 | if (ecx >= AVC_CACHE_RECLAIM) { | |
271 | rcu_read_unlock(); | |
edf3d1ae | 272 | spin_unlock_irqrestore(lock, flags); |
906d27d9 | 273 | goto out; |
1da177e4 LT |
274 | } |
275 | } | |
61844250 | 276 | rcu_read_unlock(); |
edf3d1ae | 277 | spin_unlock_irqrestore(lock, flags); |
1da177e4 LT |
278 | } |
279 | out: | |
280 | return ecx; | |
281 | } | |
282 | ||
283 | static struct avc_node *avc_alloc_node(void) | |
284 | { | |
285 | struct avc_node *node; | |
286 | ||
c3762229 | 287 | node = kmem_cache_zalloc(avc_node_cachep, GFP_ATOMIC); |
1da177e4 LT |
288 | if (!node) |
289 | goto out; | |
290 | ||
1da177e4 | 291 | INIT_RCU_HEAD(&node->rhead); |
26036651 | 292 | INIT_HLIST_NODE(&node->list); |
1da177e4 LT |
293 | avc_cache_stats_incr(allocations); |
294 | ||
295 | if (atomic_inc_return(&avc_cache.active_nodes) > avc_cache_threshold) | |
296 | avc_reclaim_node(); | |
297 | ||
298 | out: | |
299 | return node; | |
300 | } | |
301 | ||
21193dcd | 302 | static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd) |
1da177e4 LT |
303 | { |
304 | node->ae.ssid = ssid; | |
305 | node->ae.tsid = tsid; | |
306 | node->ae.tclass = tclass; | |
21193dcd | 307 | memcpy(&node->ae.avd, avd, sizeof(node->ae.avd)); |
1da177e4 LT |
308 | } |
309 | ||
310 | static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass) | |
311 | { | |
312 | struct avc_node *node, *ret = NULL; | |
313 | int hvalue; | |
26036651 EP |
314 | struct hlist_head *head; |
315 | struct hlist_node *next; | |
1da177e4 LT |
316 | |
317 | hvalue = avc_hash(ssid, tsid, tclass); | |
edf3d1ae | 318 | head = &avc_cache.slots[hvalue]; |
26036651 | 319 | hlist_for_each_entry_rcu(node, next, head, list) { |
1da177e4 LT |
320 | if (ssid == node->ae.ssid && |
321 | tclass == node->ae.tclass && | |
322 | tsid == node->ae.tsid) { | |
323 | ret = node; | |
324 | break; | |
325 | } | |
326 | } | |
327 | ||
1da177e4 LT |
328 | return ret; |
329 | } | |
330 | ||
331 | /** | |
332 | * avc_lookup - Look up an AVC entry. | |
333 | * @ssid: source security identifier | |
334 | * @tsid: target security identifier | |
335 | * @tclass: target security class | |
1da177e4 LT |
336 | * |
337 | * Look up an AVC entry that is valid for the | |
1da177e4 LT |
338 | * (@ssid, @tsid), interpreting the permissions |
339 | * based on @tclass. If a valid AVC entry exists, | |
6382dc33 | 340 | * then this function returns the avc_node. |
1da177e4 LT |
341 | * Otherwise, this function returns NULL. |
342 | */ | |
f1c6381a | 343 | static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass) |
1da177e4 LT |
344 | { |
345 | struct avc_node *node; | |
346 | ||
347 | avc_cache_stats_incr(lookups); | |
348 | node = avc_search_node(ssid, tsid, tclass); | |
349 | ||
f1c6381a | 350 | if (node) |
1da177e4 | 351 | avc_cache_stats_incr(hits); |
f1c6381a EP |
352 | else |
353 | avc_cache_stats_incr(misses); | |
1da177e4 | 354 | |
1da177e4 LT |
355 | return node; |
356 | } | |
357 | ||
358 | static int avc_latest_notif_update(int seqno, int is_insert) | |
359 | { | |
360 | int ret = 0; | |
361 | static DEFINE_SPINLOCK(notif_lock); | |
362 | unsigned long flag; | |
363 | ||
364 | spin_lock_irqsave(¬if_lock, flag); | |
365 | if (is_insert) { | |
366 | if (seqno < avc_cache.latest_notif) { | |
744ba35e | 367 | printk(KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n", |
1da177e4 LT |
368 | seqno, avc_cache.latest_notif); |
369 | ret = -EAGAIN; | |
370 | } | |
371 | } else { | |
372 | if (seqno > avc_cache.latest_notif) | |
373 | avc_cache.latest_notif = seqno; | |
374 | } | |
375 | spin_unlock_irqrestore(¬if_lock, flag); | |
376 | ||
377 | return ret; | |
378 | } | |
379 | ||
380 | /** | |
381 | * avc_insert - Insert an AVC entry. | |
382 | * @ssid: source security identifier | |
383 | * @tsid: target security identifier | |
384 | * @tclass: target security class | |
21193dcd | 385 | * @avd: resulting av decision |
1da177e4 LT |
386 | * |
387 | * Insert an AVC entry for the SID pair | |
388 | * (@ssid, @tsid) and class @tclass. | |
389 | * The access vectors and the sequence number are | |
390 | * normally provided by the security server in | |
391 | * response to a security_compute_av() call. If the | |
21193dcd | 392 | * sequence number @avd->seqno is not less than the latest |
1da177e4 LT |
393 | * revocation notification, then the function copies |
394 | * the access vectors into a cache entry, returns | |
395 | * avc_node inserted. Otherwise, this function returns NULL. | |
396 | */ | |
21193dcd | 397 | static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd) |
1da177e4 LT |
398 | { |
399 | struct avc_node *pos, *node = NULL; | |
400 | int hvalue; | |
401 | unsigned long flag; | |
402 | ||
21193dcd | 403 | if (avc_latest_notif_update(avd->seqno, 1)) |
1da177e4 LT |
404 | goto out; |
405 | ||
406 | node = avc_alloc_node(); | |
407 | if (node) { | |
26036651 EP |
408 | struct hlist_head *head; |
409 | struct hlist_node *next; | |
edf3d1ae EP |
410 | spinlock_t *lock; |
411 | ||
1da177e4 | 412 | hvalue = avc_hash(ssid, tsid, tclass); |
21193dcd | 413 | avc_node_populate(node, ssid, tsid, tclass, avd); |
1da177e4 | 414 | |
edf3d1ae EP |
415 | head = &avc_cache.slots[hvalue]; |
416 | lock = &avc_cache.slots_lock[hvalue]; | |
417 | ||
418 | spin_lock_irqsave(lock, flag); | |
26036651 | 419 | hlist_for_each_entry(pos, next, head, list) { |
1da177e4 LT |
420 | if (pos->ae.ssid == ssid && |
421 | pos->ae.tsid == tsid && | |
422 | pos->ae.tclass == tclass) { | |
95fff33b | 423 | avc_node_replace(node, pos); |
1da177e4 LT |
424 | goto found; |
425 | } | |
426 | } | |
26036651 | 427 | hlist_add_head_rcu(&node->list, head); |
1da177e4 | 428 | found: |
edf3d1ae | 429 | spin_unlock_irqrestore(lock, flag); |
1da177e4 LT |
430 | } |
431 | out: | |
432 | return node; | |
433 | } | |
434 | ||
2bf49690 TL |
435 | /** |
436 | * avc_audit_pre_callback - SELinux specific information | |
437 | * will be called by generic audit code | |
438 | * @ab: the audit buffer | |
439 | * @a: audit_data | |
440 | */ | |
441 | static void avc_audit_pre_callback(struct audit_buffer *ab, void *a) | |
1da177e4 | 442 | { |
2bf49690 TL |
443 | struct common_audit_data *ad = a; |
444 | audit_log_format(ab, "avc: %s ", | |
445 | ad->selinux_audit_data.denied ? "denied" : "granted"); | |
446 | avc_dump_av(ab, ad->selinux_audit_data.tclass, | |
447 | ad->selinux_audit_data.audited); | |
448 | audit_log_format(ab, " for "); | |
1da177e4 LT |
449 | } |
450 | ||
2bf49690 TL |
451 | /** |
452 | * avc_audit_post_callback - SELinux specific information | |
453 | * will be called by generic audit code | |
454 | * @ab: the audit buffer | |
455 | * @a: audit_data | |
456 | */ | |
457 | static void avc_audit_post_callback(struct audit_buffer *ab, void *a) | |
1da177e4 | 458 | { |
2bf49690 TL |
459 | struct common_audit_data *ad = a; |
460 | audit_log_format(ab, " "); | |
461 | avc_dump_query(ab, ad->selinux_audit_data.ssid, | |
462 | ad->selinux_audit_data.tsid, | |
463 | ad->selinux_audit_data.tclass); | |
1da177e4 LT |
464 | } |
465 | ||
466 | /** | |
467 | * avc_audit - Audit the granting or denial of permissions. | |
468 | * @ssid: source security identifier | |
469 | * @tsid: target security identifier | |
470 | * @tclass: target security class | |
471 | * @requested: requested permissions | |
472 | * @avd: access vector decisions | |
473 | * @result: result from avc_has_perm_noaudit | |
474 | * @a: auxiliary audit data | |
475 | * | |
476 | * Audit the granting or denial of permissions in accordance | |
477 | * with the policy. This function is typically called by | |
478 | * avc_has_perm() after a permission check, but can also be | |
479 | * called directly by callers who use avc_has_perm_noaudit() | |
480 | * in order to separate the permission check from the auditing. | |
481 | * For example, this separation is useful when the permission check must | |
482 | * be performed under a lock, to allow the lock to be released | |
483 | * before calling the auditing code. | |
484 | */ | |
485 | void avc_audit(u32 ssid, u32 tsid, | |
95fff33b | 486 | u16 tclass, u32 requested, |
2bf49690 | 487 | struct av_decision *avd, int result, struct common_audit_data *a) |
1da177e4 | 488 | { |
2bf49690 | 489 | struct common_audit_data stack_data; |
be940d62 | 490 | u32 denied, audited; |
be940d62 | 491 | denied = requested & ~avd->allowed; |
b6cac5a3 SS |
492 | if (denied) |
493 | audited = denied & avd->auditdeny; | |
494 | else if (result) | |
be940d62 | 495 | audited = denied = requested; |
b6cac5a3 SS |
496 | else |
497 | audited = requested & avd->auditallow; | |
498 | if (!audited) | |
499 | return; | |
2bf49690 TL |
500 | if (!a) { |
501 | a = &stack_data; | |
502 | memset(a, 0, sizeof(*a)); | |
503 | a->type = LSM_AUDIT_NO_AUDIT; | |
be940d62 | 504 | } |
2bf49690 TL |
505 | a->selinux_audit_data.tclass = tclass; |
506 | a->selinux_audit_data.requested = requested; | |
507 | a->selinux_audit_data.ssid = ssid; | |
508 | a->selinux_audit_data.tsid = tsid; | |
509 | a->selinux_audit_data.audited = audited; | |
510 | a->selinux_audit_data.denied = denied; | |
511 | a->lsm_pre_audit = avc_audit_pre_callback; | |
512 | a->lsm_post_audit = avc_audit_post_callback; | |
513 | common_lsm_audit(a); | |
1da177e4 LT |
514 | } |
515 | ||
516 | /** | |
517 | * avc_add_callback - Register a callback for security events. | |
518 | * @callback: callback function | |
519 | * @events: security events | |
520 | * @ssid: source security identifier or %SECSID_WILD | |
521 | * @tsid: target security identifier or %SECSID_WILD | |
522 | * @tclass: target security class | |
523 | * @perms: permissions | |
524 | * | |
525 | * Register a callback function for events in the set @events | |
6382dc33 | 526 | * related to the SID pair (@ssid, @tsid) |
1da177e4 LT |
527 | * and the permissions @perms, interpreting |
528 | * @perms based on @tclass. Returns %0 on success or | |
529 | * -%ENOMEM if insufficient memory exists to add the callback. | |
530 | */ | |
531 | int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid, | |
95fff33b EP |
532 | u16 tclass, u32 perms, |
533 | u32 *out_retained), | |
534 | u32 events, u32 ssid, u32 tsid, | |
535 | u16 tclass, u32 perms) | |
1da177e4 LT |
536 | { |
537 | struct avc_callback_node *c; | |
538 | int rc = 0; | |
539 | ||
540 | c = kmalloc(sizeof(*c), GFP_ATOMIC); | |
541 | if (!c) { | |
542 | rc = -ENOMEM; | |
543 | goto out; | |
544 | } | |
545 | ||
546 | c->callback = callback; | |
547 | c->events = events; | |
548 | c->ssid = ssid; | |
549 | c->tsid = tsid; | |
550 | c->perms = perms; | |
551 | c->next = avc_callbacks; | |
552 | avc_callbacks = c; | |
553 | out: | |
554 | return rc; | |
555 | } | |
556 | ||
557 | static inline int avc_sidcmp(u32 x, u32 y) | |
558 | { | |
559 | return (x == y || x == SECSID_WILD || y == SECSID_WILD); | |
560 | } | |
561 | ||
562 | /** | |
563 | * avc_update_node Update an AVC entry | |
564 | * @event : Updating event | |
565 | * @perms : Permission mask bits | |
566 | * @ssid,@tsid,@tclass : identifier of an AVC entry | |
a5dda683 | 567 | * @seqno : sequence number when decision was made |
1da177e4 LT |
568 | * |
569 | * if a valid AVC entry doesn't exist,this function returns -ENOENT. | |
570 | * if kmalloc() called internal returns NULL, this function returns -ENOMEM. | |
6382dc33 | 571 | * otherwise, this function updates the AVC entry. The original AVC-entry object |
1da177e4 LT |
572 | * will release later by RCU. |
573 | */ | |
a5dda683 EP |
574 | static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass, |
575 | u32 seqno) | |
1da177e4 LT |
576 | { |
577 | int hvalue, rc = 0; | |
578 | unsigned long flag; | |
579 | struct avc_node *pos, *node, *orig = NULL; | |
26036651 EP |
580 | struct hlist_head *head; |
581 | struct hlist_node *next; | |
edf3d1ae | 582 | spinlock_t *lock; |
1da177e4 LT |
583 | |
584 | node = avc_alloc_node(); | |
585 | if (!node) { | |
586 | rc = -ENOMEM; | |
587 | goto out; | |
588 | } | |
589 | ||
590 | /* Lock the target slot */ | |
591 | hvalue = avc_hash(ssid, tsid, tclass); | |
1da177e4 | 592 | |
edf3d1ae EP |
593 | head = &avc_cache.slots[hvalue]; |
594 | lock = &avc_cache.slots_lock[hvalue]; | |
595 | ||
596 | spin_lock_irqsave(lock, flag); | |
597 | ||
26036651 | 598 | hlist_for_each_entry(pos, next, head, list) { |
95fff33b EP |
599 | if (ssid == pos->ae.ssid && |
600 | tsid == pos->ae.tsid && | |
a5dda683 EP |
601 | tclass == pos->ae.tclass && |
602 | seqno == pos->ae.avd.seqno){ | |
1da177e4 LT |
603 | orig = pos; |
604 | break; | |
605 | } | |
606 | } | |
607 | ||
608 | if (!orig) { | |
609 | rc = -ENOENT; | |
610 | avc_node_kill(node); | |
611 | goto out_unlock; | |
612 | } | |
613 | ||
614 | /* | |
615 | * Copy and replace original node. | |
616 | */ | |
617 | ||
21193dcd | 618 | avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd); |
1da177e4 LT |
619 | |
620 | switch (event) { | |
621 | case AVC_CALLBACK_GRANT: | |
622 | node->ae.avd.allowed |= perms; | |
623 | break; | |
624 | case AVC_CALLBACK_TRY_REVOKE: | |
625 | case AVC_CALLBACK_REVOKE: | |
626 | node->ae.avd.allowed &= ~perms; | |
627 | break; | |
628 | case AVC_CALLBACK_AUDITALLOW_ENABLE: | |
629 | node->ae.avd.auditallow |= perms; | |
630 | break; | |
631 | case AVC_CALLBACK_AUDITALLOW_DISABLE: | |
632 | node->ae.avd.auditallow &= ~perms; | |
633 | break; | |
634 | case AVC_CALLBACK_AUDITDENY_ENABLE: | |
635 | node->ae.avd.auditdeny |= perms; | |
636 | break; | |
637 | case AVC_CALLBACK_AUDITDENY_DISABLE: | |
638 | node->ae.avd.auditdeny &= ~perms; | |
639 | break; | |
640 | } | |
641 | avc_node_replace(node, orig); | |
642 | out_unlock: | |
edf3d1ae | 643 | spin_unlock_irqrestore(lock, flag); |
1da177e4 LT |
644 | out: |
645 | return rc; | |
646 | } | |
647 | ||
648 | /** | |
008574b1 | 649 | * avc_flush - Flush the cache |
1da177e4 | 650 | */ |
008574b1 | 651 | static void avc_flush(void) |
1da177e4 | 652 | { |
26036651 EP |
653 | struct hlist_head *head; |
654 | struct hlist_node *next; | |
008574b1 | 655 | struct avc_node *node; |
edf3d1ae | 656 | spinlock_t *lock; |
008574b1 EP |
657 | unsigned long flag; |
658 | int i; | |
1da177e4 LT |
659 | |
660 | for (i = 0; i < AVC_CACHE_SLOTS; i++) { | |
edf3d1ae EP |
661 | head = &avc_cache.slots[i]; |
662 | lock = &avc_cache.slots_lock[i]; | |
663 | ||
664 | spin_lock_irqsave(lock, flag); | |
61844250 PM |
665 | /* |
666 | * With preemptable RCU, the outer spinlock does not | |
667 | * prevent RCU grace periods from ending. | |
668 | */ | |
669 | rcu_read_lock(); | |
26036651 | 670 | hlist_for_each_entry(node, next, head, list) |
1da177e4 | 671 | avc_node_delete(node); |
61844250 | 672 | rcu_read_unlock(); |
edf3d1ae | 673 | spin_unlock_irqrestore(lock, flag); |
1da177e4 | 674 | } |
008574b1 EP |
675 | } |
676 | ||
677 | /** | |
678 | * avc_ss_reset - Flush the cache and revalidate migrated permissions. | |
679 | * @seqno: policy sequence number | |
680 | */ | |
681 | int avc_ss_reset(u32 seqno) | |
682 | { | |
683 | struct avc_callback_node *c; | |
684 | int rc = 0, tmprc; | |
685 | ||
686 | avc_flush(); | |
1da177e4 LT |
687 | |
688 | for (c = avc_callbacks; c; c = c->next) { | |
689 | if (c->events & AVC_CALLBACK_RESET) { | |
376bd9cb | 690 | tmprc = c->callback(AVC_CALLBACK_RESET, |
95fff33b | 691 | 0, 0, 0, 0, NULL); |
376bd9cb DG |
692 | /* save the first error encountered for the return |
693 | value and continue processing the callbacks */ | |
694 | if (!rc) | |
695 | rc = tmprc; | |
1da177e4 LT |
696 | } |
697 | } | |
698 | ||
699 | avc_latest_notif_update(seqno, 0); | |
1da177e4 LT |
700 | return rc; |
701 | } | |
702 | ||
703 | /** | |
704 | * avc_has_perm_noaudit - Check permissions but perform no auditing. | |
705 | * @ssid: source security identifier | |
706 | * @tsid: target security identifier | |
707 | * @tclass: target security class | |
708 | * @requested: requested permissions, interpreted based on @tclass | |
2c3c05db | 709 | * @flags: AVC_STRICT or 0 |
1da177e4 LT |
710 | * @avd: access vector decisions |
711 | * | |
712 | * Check the AVC to determine whether the @requested permissions are granted | |
713 | * for the SID pair (@ssid, @tsid), interpreting the permissions | |
714 | * based on @tclass, and call the security server on a cache miss to obtain | |
715 | * a new decision and add it to the cache. Return a copy of the decisions | |
716 | * in @avd. Return %0 if all @requested permissions are granted, | |
717 | * -%EACCES if any permissions are denied, or another -errno upon | |
718 | * other errors. This function is typically called by avc_has_perm(), | |
719 | * but may also be called directly to separate permission checking from | |
720 | * auditing, e.g. in cases where a lock must be held for the check but | |
721 | * should be released for the auditing. | |
722 | */ | |
723 | int avc_has_perm_noaudit(u32 ssid, u32 tsid, | |
2c3c05db SS |
724 | u16 tclass, u32 requested, |
725 | unsigned flags, | |
21193dcd | 726 | struct av_decision *in_avd) |
1da177e4 LT |
727 | { |
728 | struct avc_node *node; | |
21193dcd | 729 | struct av_decision avd_entry, *avd; |
1da177e4 LT |
730 | int rc = 0; |
731 | u32 denied; | |
732 | ||
eda4f69c EP |
733 | BUG_ON(!requested); |
734 | ||
1da177e4 LT |
735 | rcu_read_lock(); |
736 | ||
f1c6381a | 737 | node = avc_lookup(ssid, tsid, tclass); |
1da177e4 LT |
738 | if (!node) { |
739 | rcu_read_unlock(); | |
21193dcd EP |
740 | |
741 | if (in_avd) | |
742 | avd = in_avd; | |
743 | else | |
744 | avd = &avd_entry; | |
745 | ||
19439d05 | 746 | security_compute_av(ssid, tsid, tclass, avd); |
1da177e4 | 747 | rcu_read_lock(); |
21193dcd EP |
748 | node = avc_insert(ssid, tsid, tclass, avd); |
749 | } else { | |
750 | if (in_avd) | |
751 | memcpy(in_avd, &node->ae.avd, sizeof(*in_avd)); | |
752 | avd = &node->ae.avd; | |
1da177e4 LT |
753 | } |
754 | ||
21193dcd | 755 | denied = requested & ~(avd->allowed); |
1da177e4 | 756 | |
eda4f69c | 757 | if (denied) { |
64dbf074 | 758 | if (flags & AVC_STRICT) |
1da177e4 | 759 | rc = -EACCES; |
8a6f83af | 760 | else if (!selinux_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE)) |
64dbf074 | 761 | avc_update_node(AVC_CALLBACK_GRANT, requested, ssid, |
21193dcd | 762 | tsid, tclass, avd->seqno); |
1da177e4 | 763 | else |
64dbf074 | 764 | rc = -EACCES; |
1da177e4 LT |
765 | } |
766 | ||
767 | rcu_read_unlock(); | |
1da177e4 LT |
768 | return rc; |
769 | } | |
770 | ||
771 | /** | |
772 | * avc_has_perm - Check permissions and perform any appropriate auditing. | |
773 | * @ssid: source security identifier | |
774 | * @tsid: target security identifier | |
775 | * @tclass: target security class | |
776 | * @requested: requested permissions, interpreted based on @tclass | |
777 | * @auditdata: auxiliary audit data | |
778 | * | |
779 | * Check the AVC to determine whether the @requested permissions are granted | |
780 | * for the SID pair (@ssid, @tsid), interpreting the permissions | |
781 | * based on @tclass, and call the security server on a cache miss to obtain | |
782 | * a new decision and add it to the cache. Audit the granting or denial of | |
783 | * permissions in accordance with the policy. Return %0 if all @requested | |
784 | * permissions are granted, -%EACCES if any permissions are denied, or | |
785 | * another -errno upon other errors. | |
786 | */ | |
787 | int avc_has_perm(u32 ssid, u32 tsid, u16 tclass, | |
2bf49690 | 788 | u32 requested, struct common_audit_data *auditdata) |
1da177e4 LT |
789 | { |
790 | struct av_decision avd; | |
791 | int rc; | |
792 | ||
2c3c05db | 793 | rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd); |
1da177e4 LT |
794 | avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata); |
795 | return rc; | |
796 | } | |
788e7dd4 YN |
797 | |
798 | u32 avc_policy_seqno(void) | |
799 | { | |
800 | return avc_cache.latest_notif; | |
801 | } | |
89c86576 TL |
802 | |
803 | void avc_disable(void) | |
804 | { | |
5224ee08 EP |
805 | /* |
806 | * If you are looking at this because you have realized that we are | |
807 | * not destroying the avc_node_cachep it might be easy to fix, but | |
808 | * I don't know the memory barrier semantics well enough to know. It's | |
809 | * possible that some other task dereferenced security_ops when | |
810 | * it still pointed to selinux operations. If that is the case it's | |
811 | * possible that it is about to use the avc and is about to need the | |
812 | * avc_node_cachep. I know I could wrap the security.c security_ops call | |
813 | * in an rcu_lock, but seriously, it's not worth it. Instead I just flush | |
814 | * the cache and get that memory back. | |
815 | */ | |
816 | if (avc_node_cachep) { | |
817 | avc_flush(); | |
818 | /* kmem_cache_destroy(avc_node_cachep); */ | |
819 | } | |
89c86576 | 820 | } |