2 * Implementation of the kernel access vector cache (AVC).
4 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
7 * Update: KaiGai, Kohei <kaigai@ak.jp.nec.com>
8 * Replaced the avc_lock spinlock by RCU.
10 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
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,
14 * as published by the Free Software Foundation.
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 #include <linux/dcache.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/percpu.h>
27 #include <net/af_unix.h>
29 #include <linux/audit.h>
30 #include <linux/ipv6.h>
36 #define AVC_CACHE_SLOTS 512
37 #define AVC_DEF_CACHE_THRESHOLD 512
38 #define AVC_CACHE_RECLAIM 16
40 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
41 #define avc_cache_stats_incr(field) \
43 per_cpu(avc_cache_stats, get_cpu()).field++; \
47 #define avc_cache_stats_incr(field) do {} while (0)
54 struct av_decision avd
;
59 struct hlist_node list
; /* anchored in avc_cache->slots[i] */
60 struct rcu_head rhead
;
64 struct hlist_head slots
[AVC_CACHE_SLOTS
]; /* head for avc_node->list */
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 */
71 struct avc_callback_node
{
72 int (*callback
) (u32 event
, u32 ssid
, u32 tsid
,
73 u16 tclass
, u32 perms
,
80 struct avc_callback_node
*next
;
83 /* Exported via selinufs */
84 unsigned int avc_cache_threshold
= AVC_DEF_CACHE_THRESHOLD
;
86 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
87 DEFINE_PER_CPU(struct avc_cache_stats
, avc_cache_stats
) = { 0 };
90 static struct avc_cache avc_cache
;
91 static struct avc_callback_node
*avc_callbacks
;
92 static struct kmem_cache
*avc_node_cachep
;
94 static inline int avc_hash(u32 ssid
, u32 tsid
, u16 tclass
)
96 return (ssid
^ (tsid
<<2) ^ (tclass
<<4)) & (AVC_CACHE_SLOTS
- 1);
100 * avc_dump_av - Display an access vector in human-readable form.
101 * @tclass: target security class
104 static void avc_dump_av(struct audit_buffer
*ab
, u16 tclass
, u32 av
)
110 audit_log_format(ab
, " null");
114 perms
= secclass_map
[tclass
-1].perms
;
116 audit_log_format(ab
, " {");
119 while (i
< (sizeof(av
) * 8)) {
120 if ((perm
& av
) && perms
[i
]) {
121 audit_log_format(ab
, " %s", perms
[i
]);
129 audit_log_format(ab
, " 0x%x", av
);
131 audit_log_format(ab
, " }");
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
140 static void avc_dump_query(struct audit_buffer
*ab
, u32 ssid
, u32 tsid
, u16 tclass
)
146 rc
= security_sid_to_context(ssid
, &scontext
, &scontext_len
);
148 audit_log_format(ab
, "ssid=%d", ssid
);
150 audit_log_format(ab
, "scontext=%s", scontext
);
154 rc
= security_sid_to_context(tsid
, &scontext
, &scontext_len
);
156 audit_log_format(ab
, " tsid=%d", tsid
);
158 audit_log_format(ab
, " tcontext=%s", scontext
);
162 BUG_ON(tclass
>= ARRAY_SIZE(secclass_map
));
163 audit_log_format(ab
, " tclass=%s", secclass_map
[tclass
-1].name
);
167 * avc_init - Initialize the AVC.
169 * Initialize the access vector cache.
171 void __init
avc_init(void)
175 for (i
= 0; i
< AVC_CACHE_SLOTS
; i
++) {
176 INIT_HLIST_HEAD(&avc_cache
.slots
[i
]);
177 spin_lock_init(&avc_cache
.slots_lock
[i
]);
179 atomic_set(&avc_cache
.active_nodes
, 0);
180 atomic_set(&avc_cache
.lru_hint
, 0);
182 avc_node_cachep
= kmem_cache_create("avc_node", sizeof(struct avc_node
),
183 0, SLAB_PANIC
, NULL
);
185 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_KERNEL
, "AVC INITIALIZED\n");
188 int avc_get_hash_stats(char *page
)
190 int i
, chain_len
, max_chain_len
, slots_used
;
191 struct avc_node
*node
;
192 struct hlist_head
*head
;
198 for (i
= 0; i
< AVC_CACHE_SLOTS
; i
++) {
199 head
= &avc_cache
.slots
[i
];
200 if (!hlist_empty(head
)) {
201 struct hlist_node
*next
;
205 hlist_for_each_entry_rcu(node
, next
, head
, list
)
207 if (chain_len
> max_chain_len
)
208 max_chain_len
= chain_len
;
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
);
220 static void avc_node_free(struct rcu_head
*rhead
)
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
);
227 static void avc_node_delete(struct avc_node
*node
)
229 hlist_del_rcu(&node
->list
);
230 call_rcu(&node
->rhead
, avc_node_free
);
231 atomic_dec(&avc_cache
.active_nodes
);
234 static void avc_node_kill(struct avc_node
*node
)
236 kmem_cache_free(avc_node_cachep
, node
);
237 avc_cache_stats_incr(frees
);
238 atomic_dec(&avc_cache
.active_nodes
);
241 static void avc_node_replace(struct avc_node
*new, struct avc_node
*old
)
243 hlist_replace_rcu(&old
->list
, &new->list
);
244 call_rcu(&old
->rhead
, avc_node_free
);
245 atomic_dec(&avc_cache
.active_nodes
);
248 static inline int avc_reclaim_node(void)
250 struct avc_node
*node
;
251 int hvalue
, try, ecx
;
253 struct hlist_head
*head
;
254 struct hlist_node
*next
;
257 for (try = 0, ecx
= 0; try < AVC_CACHE_SLOTS
; try++) {
258 hvalue
= atomic_inc_return(&avc_cache
.lru_hint
) & (AVC_CACHE_SLOTS
- 1);
259 head
= &avc_cache
.slots
[hvalue
];
260 lock
= &avc_cache
.slots_lock
[hvalue
];
262 if (!spin_trylock_irqsave(lock
, flags
))
266 hlist_for_each_entry(node
, next
, head
, list
) {
267 avc_node_delete(node
);
268 avc_cache_stats_incr(reclaims
);
270 if (ecx
>= AVC_CACHE_RECLAIM
) {
272 spin_unlock_irqrestore(lock
, flags
);
277 spin_unlock_irqrestore(lock
, flags
);
283 static struct avc_node
*avc_alloc_node(void)
285 struct avc_node
*node
;
287 node
= kmem_cache_zalloc(avc_node_cachep
, GFP_ATOMIC
);
291 INIT_RCU_HEAD(&node
->rhead
);
292 INIT_HLIST_NODE(&node
->list
);
293 avc_cache_stats_incr(allocations
);
295 if (atomic_inc_return(&avc_cache
.active_nodes
) > avc_cache_threshold
)
302 static void avc_node_populate(struct avc_node
*node
, u32 ssid
, u32 tsid
, u16 tclass
, struct av_decision
*avd
)
304 node
->ae
.ssid
= ssid
;
305 node
->ae
.tsid
= tsid
;
306 node
->ae
.tclass
= tclass
;
307 memcpy(&node
->ae
.avd
, avd
, sizeof(node
->ae
.avd
));
310 static inline struct avc_node
*avc_search_node(u32 ssid
, u32 tsid
, u16 tclass
)
312 struct avc_node
*node
, *ret
= NULL
;
314 struct hlist_head
*head
;
315 struct hlist_node
*next
;
317 hvalue
= avc_hash(ssid
, tsid
, tclass
);
318 head
= &avc_cache
.slots
[hvalue
];
319 hlist_for_each_entry_rcu(node
, next
, head
, list
) {
320 if (ssid
== node
->ae
.ssid
&&
321 tclass
== node
->ae
.tclass
&&
322 tsid
== node
->ae
.tsid
) {
332 * avc_lookup - Look up an AVC entry.
333 * @ssid: source security identifier
334 * @tsid: target security identifier
335 * @tclass: target security class
337 * Look up an AVC entry that is valid for the
338 * (@ssid, @tsid), interpreting the permissions
339 * based on @tclass. If a valid AVC entry exists,
340 * then this function return the avc_node.
341 * Otherwise, this function returns NULL.
343 static struct avc_node
*avc_lookup(u32 ssid
, u32 tsid
, u16 tclass
)
345 struct avc_node
*node
;
347 avc_cache_stats_incr(lookups
);
348 node
= avc_search_node(ssid
, tsid
, tclass
);
351 avc_cache_stats_incr(hits
);
353 avc_cache_stats_incr(misses
);
358 static int avc_latest_notif_update(int seqno
, int is_insert
)
361 static DEFINE_SPINLOCK(notif_lock
);
364 spin_lock_irqsave(¬if_lock
, flag
);
366 if (seqno
< avc_cache
.latest_notif
) {
367 printk(KERN_WARNING
"SELinux: avc: seqno %d < latest_notif %d\n",
368 seqno
, avc_cache
.latest_notif
);
372 if (seqno
> avc_cache
.latest_notif
)
373 avc_cache
.latest_notif
= seqno
;
375 spin_unlock_irqrestore(¬if_lock
, flag
);
381 * avc_insert - Insert an AVC entry.
382 * @ssid: source security identifier
383 * @tsid: target security identifier
384 * @tclass: target security class
385 * @avd: resulting av decision
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
392 * sequence number @avd->seqno is not less than the latest
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.
397 static struct avc_node
*avc_insert(u32 ssid
, u32 tsid
, u16 tclass
, struct av_decision
*avd
)
399 struct avc_node
*pos
, *node
= NULL
;
403 if (avc_latest_notif_update(avd
->seqno
, 1))
406 node
= avc_alloc_node();
408 struct hlist_head
*head
;
409 struct hlist_node
*next
;
412 hvalue
= avc_hash(ssid
, tsid
, tclass
);
413 avc_node_populate(node
, ssid
, tsid
, tclass
, avd
);
415 head
= &avc_cache
.slots
[hvalue
];
416 lock
= &avc_cache
.slots_lock
[hvalue
];
418 spin_lock_irqsave(lock
, flag
);
419 hlist_for_each_entry(pos
, next
, head
, list
) {
420 if (pos
->ae
.ssid
== ssid
&&
421 pos
->ae
.tsid
== tsid
&&
422 pos
->ae
.tclass
== tclass
) {
423 avc_node_replace(node
, pos
);
427 hlist_add_head_rcu(&node
->list
, head
);
429 spin_unlock_irqrestore(lock
, flag
);
436 * avc_audit_pre_callback - SELinux specific information
437 * will be called by generic audit code
438 * @ab: the audit buffer
441 static void avc_audit_pre_callback(struct audit_buffer
*ab
, void *a
)
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 ");
452 * avc_audit_post_callback - SELinux specific information
453 * will be called by generic audit code
454 * @ab: the audit buffer
457 static void avc_audit_post_callback(struct audit_buffer
*ab
, void *a
)
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
);
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
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.
485 void avc_audit(u32 ssid
, u32 tsid
,
486 u16 tclass
, u32 requested
,
487 struct av_decision
*avd
, int result
, struct common_audit_data
*a
)
489 struct common_audit_data stack_data
;
491 denied
= requested
& ~avd
->allowed
;
494 if (!(audited
& avd
->auditdeny
))
497 audited
= denied
= requested
;
500 if (!(audited
& avd
->auditallow
))
505 memset(a
, 0, sizeof(*a
));
506 a
->type
= LSM_AUDIT_NO_AUDIT
;
508 a
->selinux_audit_data
.tclass
= tclass
;
509 a
->selinux_audit_data
.requested
= requested
;
510 a
->selinux_audit_data
.ssid
= ssid
;
511 a
->selinux_audit_data
.tsid
= tsid
;
512 a
->selinux_audit_data
.audited
= audited
;
513 a
->selinux_audit_data
.denied
= denied
;
514 a
->lsm_pre_audit
= avc_audit_pre_callback
;
515 a
->lsm_post_audit
= avc_audit_post_callback
;
520 * avc_add_callback - Register a callback for security events.
521 * @callback: callback function
522 * @events: security events
523 * @ssid: source security identifier or %SECSID_WILD
524 * @tsid: target security identifier or %SECSID_WILD
525 * @tclass: target security class
526 * @perms: permissions
528 * Register a callback function for events in the set @events
529 * related to the SID pair (@ssid, @tsid) and
530 * and the permissions @perms, interpreting
531 * @perms based on @tclass. Returns %0 on success or
532 * -%ENOMEM if insufficient memory exists to add the callback.
534 int avc_add_callback(int (*callback
)(u32 event
, u32 ssid
, u32 tsid
,
535 u16 tclass
, u32 perms
,
537 u32 events
, u32 ssid
, u32 tsid
,
538 u16 tclass
, u32 perms
)
540 struct avc_callback_node
*c
;
543 c
= kmalloc(sizeof(*c
), GFP_ATOMIC
);
549 c
->callback
= callback
;
554 c
->next
= avc_callbacks
;
560 static inline int avc_sidcmp(u32 x
, u32 y
)
562 return (x
== y
|| x
== SECSID_WILD
|| y
== SECSID_WILD
);
566 * avc_update_node Update an AVC entry
567 * @event : Updating event
568 * @perms : Permission mask bits
569 * @ssid,@tsid,@tclass : identifier of an AVC entry
570 * @seqno : sequence number when decision was made
572 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
573 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
574 * otherwise, this function update the AVC entry. The original AVC-entry object
575 * will release later by RCU.
577 static int avc_update_node(u32 event
, u32 perms
, u32 ssid
, u32 tsid
, u16 tclass
,
582 struct avc_node
*pos
, *node
, *orig
= NULL
;
583 struct hlist_head
*head
;
584 struct hlist_node
*next
;
587 node
= avc_alloc_node();
593 /* Lock the target slot */
594 hvalue
= avc_hash(ssid
, tsid
, tclass
);
596 head
= &avc_cache
.slots
[hvalue
];
597 lock
= &avc_cache
.slots_lock
[hvalue
];
599 spin_lock_irqsave(lock
, flag
);
601 hlist_for_each_entry(pos
, next
, head
, list
) {
602 if (ssid
== pos
->ae
.ssid
&&
603 tsid
== pos
->ae
.tsid
&&
604 tclass
== pos
->ae
.tclass
&&
605 seqno
== pos
->ae
.avd
.seqno
){
618 * Copy and replace original node.
621 avc_node_populate(node
, ssid
, tsid
, tclass
, &orig
->ae
.avd
);
624 case AVC_CALLBACK_GRANT
:
625 node
->ae
.avd
.allowed
|= perms
;
627 case AVC_CALLBACK_TRY_REVOKE
:
628 case AVC_CALLBACK_REVOKE
:
629 node
->ae
.avd
.allowed
&= ~perms
;
631 case AVC_CALLBACK_AUDITALLOW_ENABLE
:
632 node
->ae
.avd
.auditallow
|= perms
;
634 case AVC_CALLBACK_AUDITALLOW_DISABLE
:
635 node
->ae
.avd
.auditallow
&= ~perms
;
637 case AVC_CALLBACK_AUDITDENY_ENABLE
:
638 node
->ae
.avd
.auditdeny
|= perms
;
640 case AVC_CALLBACK_AUDITDENY_DISABLE
:
641 node
->ae
.avd
.auditdeny
&= ~perms
;
644 avc_node_replace(node
, orig
);
646 spin_unlock_irqrestore(lock
, flag
);
652 * avc_flush - Flush the cache
654 static void avc_flush(void)
656 struct hlist_head
*head
;
657 struct hlist_node
*next
;
658 struct avc_node
*node
;
663 for (i
= 0; i
< AVC_CACHE_SLOTS
; i
++) {
664 head
= &avc_cache
.slots
[i
];
665 lock
= &avc_cache
.slots_lock
[i
];
667 spin_lock_irqsave(lock
, flag
);
669 * With preemptable RCU, the outer spinlock does not
670 * prevent RCU grace periods from ending.
673 hlist_for_each_entry(node
, next
, head
, list
)
674 avc_node_delete(node
);
676 spin_unlock_irqrestore(lock
, flag
);
681 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
682 * @seqno: policy sequence number
684 int avc_ss_reset(u32 seqno
)
686 struct avc_callback_node
*c
;
691 for (c
= avc_callbacks
; c
; c
= c
->next
) {
692 if (c
->events
& AVC_CALLBACK_RESET
) {
693 tmprc
= c
->callback(AVC_CALLBACK_RESET
,
695 /* save the first error encountered for the return
696 value and continue processing the callbacks */
702 avc_latest_notif_update(seqno
, 0);
707 * avc_has_perm_noaudit - Check permissions but perform no auditing.
708 * @ssid: source security identifier
709 * @tsid: target security identifier
710 * @tclass: target security class
711 * @requested: requested permissions, interpreted based on @tclass
712 * @flags: AVC_STRICT or 0
713 * @avd: access vector decisions
715 * Check the AVC to determine whether the @requested permissions are granted
716 * for the SID pair (@ssid, @tsid), interpreting the permissions
717 * based on @tclass, and call the security server on a cache miss to obtain
718 * a new decision and add it to the cache. Return a copy of the decisions
719 * in @avd. Return %0 if all @requested permissions are granted,
720 * -%EACCES if any permissions are denied, or another -errno upon
721 * other errors. This function is typically called by avc_has_perm(),
722 * but may also be called directly to separate permission checking from
723 * auditing, e.g. in cases where a lock must be held for the check but
724 * should be released for the auditing.
726 int avc_has_perm_noaudit(u32 ssid
, u32 tsid
,
727 u16 tclass
, u32 requested
,
729 struct av_decision
*in_avd
)
731 struct avc_node
*node
;
732 struct av_decision avd_entry
, *avd
;
740 node
= avc_lookup(ssid
, tsid
, tclass
);
749 rc
= security_compute_av(ssid
, tsid
, tclass
, requested
, avd
);
753 node
= avc_insert(ssid
, tsid
, tclass
, avd
);
756 memcpy(in_avd
, &node
->ae
.avd
, sizeof(*in_avd
));
760 denied
= requested
& ~(avd
->allowed
);
763 if (flags
& AVC_STRICT
)
765 else if (!selinux_enforcing
|| (avd
->flags
& AVD_FLAGS_PERMISSIVE
))
766 avc_update_node(AVC_CALLBACK_GRANT
, requested
, ssid
,
767 tsid
, tclass
, avd
->seqno
);
778 * avc_has_perm - Check permissions and perform any appropriate auditing.
779 * @ssid: source security identifier
780 * @tsid: target security identifier
781 * @tclass: target security class
782 * @requested: requested permissions, interpreted based on @tclass
783 * @auditdata: auxiliary audit data
785 * Check the AVC to determine whether the @requested permissions are granted
786 * for the SID pair (@ssid, @tsid), interpreting the permissions
787 * based on @tclass, and call the security server on a cache miss to obtain
788 * a new decision and add it to the cache. Audit the granting or denial of
789 * permissions in accordance with the policy. Return %0 if all @requested
790 * permissions are granted, -%EACCES if any permissions are denied, or
791 * another -errno upon other errors.
793 int avc_has_perm(u32 ssid
, u32 tsid
, u16 tclass
,
794 u32 requested
, struct common_audit_data
*auditdata
)
796 struct av_decision avd
;
799 rc
= avc_has_perm_noaudit(ssid
, tsid
, tclass
, requested
, 0, &avd
);
800 avc_audit(ssid
, tsid
, tclass
, requested
, &avd
, rc
, auditdata
);
804 u32
avc_policy_seqno(void)
806 return avc_cache
.latest_notif
;
809 void avc_disable(void)
812 * If you are looking at this because you have realized that we are
813 * not destroying the avc_node_cachep it might be easy to fix, but
814 * I don't know the memory barrier semantics well enough to know. It's
815 * possible that some other task dereferenced security_ops when
816 * it still pointed to selinux operations. If that is the case it's
817 * possible that it is about to use the avc and is about to need the
818 * avc_node_cachep. I know I could wrap the security.c security_ops call
819 * in an rcu_lock, but seriously, it's not worth it. Instead I just flush
820 * the cache and get that memory back.
822 if (avc_node_cachep
) {
824 /* kmem_cache_destroy(avc_node_cachep); */