16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.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) this_cpu_inc(avc_cache_stats.field)
43 #define avc_cache_stats_incr(field) do {} while (0)
76 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
109 while (i < (
sizeof(av) * 8)) {
110 if ((perm & av) && perms[i]) {
180 int i, chain_len, max_chain_len, slots_used;
190 if (!hlist_empty(head)) {
195 hlist_for_each_entry_rcu(node, next, head,
list)
197 if (chain_len > max_chain_len)
198 max_chain_len = chain_len;
205 "longest chain: %d\n",
207 slots_used, AVC_CACHE_SLOTS, max_chain_len);
210 static void avc_node_free(
struct rcu_head *rhead)
219 hlist_del_rcu(&node->
list);
233 hlist_replace_rcu(&old->
list, &new->list);
238 static inline int avc_reclaim_node(
void)
241 int hvalue,
try,
ecx;
257 avc_node_delete(node);
262 spin_unlock_irqrestore(lock, flags);
267 spin_unlock_irqrestore(lock, flags);
273 static struct avc_node *avc_alloc_node(
void)
281 INIT_HLIST_NODE(&node->
list);
294 node->
ae.tsid = tsid;
295 node->
ae.tclass = tclass;
296 memcpy(&node->
ae.avd, avd,
sizeof(node->
ae.avd));
306 hvalue = avc_hash(ssid, tsid, tclass);
308 hlist_for_each_entry_rcu(node, next, head,
list) {
309 if (ssid == node->
ae.ssid &&
310 tclass == node->
ae.tclass &&
311 tsid == node->
ae.tsid) {
337 node = avc_search_node(ssid, tsid, tclass);
346 static int avc_latest_notif_update(
int seqno,
int is_insert)
363 spin_unlock_irqrestore(¬if_lock, flag);
391 if (avc_latest_notif_update(avd->
seqno, 1))
394 node = avc_alloc_node();
400 hvalue = avc_hash(ssid, tsid, tclass);
401 avc_node_populate(node, ssid, tsid, tclass, avd);
408 if (pos->
ae.ssid == ssid &&
409 pos->
ae.tsid == tsid &&
410 pos->
ae.tclass == tclass) {
411 avc_node_replace(node, pos);
415 hlist_add_head_rcu(&node->
list, head);
417 spin_unlock_irqrestore(lock, flag);
429 static void avc_audit_pre_callback(
struct audit_buffer *ab,
void *
a)
433 ad->selinux_audit_data->denied ?
"denied" :
"granted");
434 avc_dump_av(ab, ad->selinux_audit_data->tclass,
435 ad->selinux_audit_data->audited);
445 static void avc_audit_post_callback(
struct audit_buffer *ab,
void *
a)
449 avc_dump_query(ab, ad->selinux_audit_data->ssid,
450 ad->selinux_audit_data->tsid,
451 ad->selinux_audit_data->tclass);
486 a->selinux_audit_data = &sad;
514 c->
next = avc_callbacks;
520 static inline int avc_sidcmp(
u32 x,
u32 y)
547 node = avc_alloc_node();
554 hvalue = avc_hash(ssid, tsid, tclass);
562 if (ssid == pos->
ae.ssid &&
563 tsid == pos->
ae.tsid &&
564 tclass == pos->
ae.tclass &&
565 seqno == pos->
ae.avd.seqno){
581 avc_node_populate(node, ssid, tsid, tclass, &orig->
ae.avd);
585 node->
ae.avd.allowed |= perms;
589 node->
ae.avd.allowed &= ~perms;
592 node->
ae.avd.auditallow |= perms;
595 node->
ae.avd.auditallow &= ~perms;
598 node->
ae.avd.auditdeny |= perms;
601 node->
ae.avd.auditdeny &= ~perms;
604 avc_node_replace(node, orig);
606 spin_unlock_irqrestore(lock, flag);
614 static void avc_flush(
void)
634 avc_node_delete(node);
636 spin_unlock_irqrestore(lock, flag);
651 for (c = avc_callbacks;
c; c = c->
next) {
661 avc_latest_notif_update(seqno, 0);
680 return avc_insert(ssid, tsid, tclass, avd);
684 u16 tclass,
u32 requested,
695 tsid, tclass, avd->
seqno);
721 u16 tclass,
u32 requested,
733 node = avc_lookup(ssid, tsid, tclass);
735 node = avc_compute_av(ssid, tsid, tclass, avd);
737 memcpy(avd, &node->
ae.avd,
sizeof(*avd));
741 denied = requested & ~(avd->
allowed);
743 rc = avc_denied(ssid, tsid, tclass, requested, flags, avd);
775 rc2 = avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata,
800 if (avc_node_cachep) {