xref: /linux/security/selinux/avc.c (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Implementation of the kernel access vector cache (AVC).
4  *
5  * Authors:  Stephen Smalley, <sds@tycho.nsa.gov>
6  *	     James Morris <jmorris@redhat.com>
7  *
8  * Update:   KaiGai, Kohei <kaigai@ak.jp.nec.com>
9  *	Replaced the avc_lock spinlock by RCU.
10  *
11  * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
12  */
13 #include <linux/types.h>
14 #include <linux/stddef.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/dcache.h>
19 #include <linux/init.h>
20 #include <linux/skbuff.h>
21 #include <linux/percpu.h>
22 #include <linux/list.h>
23 #include <net/sock.h>
24 #include <linux/un.h>
25 #include <net/af_unix.h>
26 #include <linux/ip.h>
27 #include <linux/audit.h>
28 #include <linux/ipv6.h>
29 #include <net/ipv6.h>
30 #include "avc.h"
31 #include "avc_ss.h"
32 #include "classmap.h"
33 
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/avc.h>
36 
37 #define AVC_CACHE_SLOTS			512
38 #define AVC_DEF_CACHE_THRESHOLD		512
39 #define AVC_CACHE_RECLAIM		16
40 
41 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
42 #define avc_cache_stats_incr(field)	this_cpu_inc(avc_cache_stats.field)
43 #else
44 #define avc_cache_stats_incr(field)	do {} while (0)
45 #endif
46 
47 struct avc_entry {
48 	u32			ssid;
49 	u32			tsid;
50 	u16			tclass;
51 	struct av_decision	avd;
52 	struct avc_xperms_node	*xp_node;
53 };
54 
55 struct avc_node {
56 	struct avc_entry	ae;
57 	struct hlist_node	list; /* anchored in avc_cache->slots[i] */
58 	struct rcu_head		rhead;
59 };
60 
61 struct avc_xperms_decision_node {
62 	struct extended_perms_decision xpd;
63 	struct list_head xpd_list; /* list of extended_perms_decision */
64 };
65 
66 struct avc_xperms_node {
67 	struct extended_perms xp;
68 	struct list_head xpd_head; /* list head of extended_perms_decision */
69 };
70 
71 struct avc_cache {
72 	struct hlist_head	slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
73 	spinlock_t		slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
74 	atomic_t		lru_hint;	/* LRU hint for reclaim scan */
75 	atomic_t		active_nodes;
76 	u32			latest_notif;	/* latest revocation notification */
77 };
78 
79 struct avc_callback_node {
80 	int (*callback) (u32 event);
81 	u32 events;
82 	struct avc_callback_node *next;
83 };
84 
85 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
86 DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
87 #endif
88 
89 struct selinux_avc {
90 	unsigned int avc_cache_threshold;
91 	struct avc_cache avc_cache;
92 };
93 
94 static struct selinux_avc selinux_avc;
95 
96 void selinux_avc_init(struct selinux_avc **avc)
97 {
98 	int i;
99 
100 	selinux_avc.avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;
101 	for (i = 0; i < AVC_CACHE_SLOTS; i++) {
102 		INIT_HLIST_HEAD(&selinux_avc.avc_cache.slots[i]);
103 		spin_lock_init(&selinux_avc.avc_cache.slots_lock[i]);
104 	}
105 	atomic_set(&selinux_avc.avc_cache.active_nodes, 0);
106 	atomic_set(&selinux_avc.avc_cache.lru_hint, 0);
107 	*avc = &selinux_avc;
108 }
109 
110 unsigned int avc_get_cache_threshold(struct selinux_avc *avc)
111 {
112 	return avc->avc_cache_threshold;
113 }
114 
115 void avc_set_cache_threshold(struct selinux_avc *avc,
116 			     unsigned int cache_threshold)
117 {
118 	avc->avc_cache_threshold = cache_threshold;
119 }
120 
121 static struct avc_callback_node *avc_callbacks __ro_after_init;
122 static struct kmem_cache *avc_node_cachep __ro_after_init;
123 static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
124 static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
125 static struct kmem_cache *avc_xperms_cachep __ro_after_init;
126 
127 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
128 {
129 	return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
130 }
131 
132 /**
133  * avc_init - Initialize the AVC.
134  *
135  * Initialize the access vector cache.
136  */
137 void __init avc_init(void)
138 {
139 	avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),
140 					0, SLAB_PANIC, NULL);
141 	avc_xperms_cachep = kmem_cache_create("avc_xperms_node",
142 					sizeof(struct avc_xperms_node),
143 					0, SLAB_PANIC, NULL);
144 	avc_xperms_decision_cachep = kmem_cache_create(
145 					"avc_xperms_decision_node",
146 					sizeof(struct avc_xperms_decision_node),
147 					0, SLAB_PANIC, NULL);
148 	avc_xperms_data_cachep = kmem_cache_create("avc_xperms_data",
149 					sizeof(struct extended_perms_data),
150 					0, SLAB_PANIC, NULL);
151 }
152 
153 int avc_get_hash_stats(struct selinux_avc *avc, char *page)
154 {
155 	int i, chain_len, max_chain_len, slots_used;
156 	struct avc_node *node;
157 	struct hlist_head *head;
158 
159 	rcu_read_lock();
160 
161 	slots_used = 0;
162 	max_chain_len = 0;
163 	for (i = 0; i < AVC_CACHE_SLOTS; i++) {
164 		head = &avc->avc_cache.slots[i];
165 		if (!hlist_empty(head)) {
166 			slots_used++;
167 			chain_len = 0;
168 			hlist_for_each_entry_rcu(node, head, list)
169 				chain_len++;
170 			if (chain_len > max_chain_len)
171 				max_chain_len = chain_len;
172 		}
173 	}
174 
175 	rcu_read_unlock();
176 
177 	return scnprintf(page, PAGE_SIZE, "entries: %d\nbuckets used: %d/%d\n"
178 			 "longest chain: %d\n",
179 			 atomic_read(&avc->avc_cache.active_nodes),
180 			 slots_used, AVC_CACHE_SLOTS, max_chain_len);
181 }
182 
183 /*
184  * using a linked list for extended_perms_decision lookup because the list is
185  * always small. i.e. less than 5, typically 1
186  */
187 static struct extended_perms_decision *avc_xperms_decision_lookup(u8 driver,
188 					struct avc_xperms_node *xp_node)
189 {
190 	struct avc_xperms_decision_node *xpd_node;
191 
192 	list_for_each_entry(xpd_node, &xp_node->xpd_head, xpd_list) {
193 		if (xpd_node->xpd.driver == driver)
194 			return &xpd_node->xpd;
195 	}
196 	return NULL;
197 }
198 
199 static inline unsigned int
200 avc_xperms_has_perm(struct extended_perms_decision *xpd,
201 					u8 perm, u8 which)
202 {
203 	unsigned int rc = 0;
204 
205 	if ((which == XPERMS_ALLOWED) &&
206 			(xpd->used & XPERMS_ALLOWED))
207 		rc = security_xperm_test(xpd->allowed->p, perm);
208 	else if ((which == XPERMS_AUDITALLOW) &&
209 			(xpd->used & XPERMS_AUDITALLOW))
210 		rc = security_xperm_test(xpd->auditallow->p, perm);
211 	else if ((which == XPERMS_DONTAUDIT) &&
212 			(xpd->used & XPERMS_DONTAUDIT))
213 		rc = security_xperm_test(xpd->dontaudit->p, perm);
214 	return rc;
215 }
216 
217 static void avc_xperms_allow_perm(struct avc_xperms_node *xp_node,
218 				u8 driver, u8 perm)
219 {
220 	struct extended_perms_decision *xpd;
221 	security_xperm_set(xp_node->xp.drivers.p, driver);
222 	xpd = avc_xperms_decision_lookup(driver, xp_node);
223 	if (xpd && xpd->allowed)
224 		security_xperm_set(xpd->allowed->p, perm);
225 }
226 
227 static void avc_xperms_decision_free(struct avc_xperms_decision_node *xpd_node)
228 {
229 	struct extended_perms_decision *xpd;
230 
231 	xpd = &xpd_node->xpd;
232 	if (xpd->allowed)
233 		kmem_cache_free(avc_xperms_data_cachep, xpd->allowed);
234 	if (xpd->auditallow)
235 		kmem_cache_free(avc_xperms_data_cachep, xpd->auditallow);
236 	if (xpd->dontaudit)
237 		kmem_cache_free(avc_xperms_data_cachep, xpd->dontaudit);
238 	kmem_cache_free(avc_xperms_decision_cachep, xpd_node);
239 }
240 
241 static void avc_xperms_free(struct avc_xperms_node *xp_node)
242 {
243 	struct avc_xperms_decision_node *xpd_node, *tmp;
244 
245 	if (!xp_node)
246 		return;
247 
248 	list_for_each_entry_safe(xpd_node, tmp, &xp_node->xpd_head, xpd_list) {
249 		list_del(&xpd_node->xpd_list);
250 		avc_xperms_decision_free(xpd_node);
251 	}
252 	kmem_cache_free(avc_xperms_cachep, xp_node);
253 }
254 
255 static void avc_copy_xperms_decision(struct extended_perms_decision *dest,
256 					struct extended_perms_decision *src)
257 {
258 	dest->driver = src->driver;
259 	dest->used = src->used;
260 	if (dest->used & XPERMS_ALLOWED)
261 		memcpy(dest->allowed->p, src->allowed->p,
262 				sizeof(src->allowed->p));
263 	if (dest->used & XPERMS_AUDITALLOW)
264 		memcpy(dest->auditallow->p, src->auditallow->p,
265 				sizeof(src->auditallow->p));
266 	if (dest->used & XPERMS_DONTAUDIT)
267 		memcpy(dest->dontaudit->p, src->dontaudit->p,
268 				sizeof(src->dontaudit->p));
269 }
270 
271 /*
272  * similar to avc_copy_xperms_decision, but only copy decision
273  * information relevant to this perm
274  */
275 static inline void avc_quick_copy_xperms_decision(u8 perm,
276 			struct extended_perms_decision *dest,
277 			struct extended_perms_decision *src)
278 {
279 	/*
280 	 * compute index of the u32 of the 256 bits (8 u32s) that contain this
281 	 * command permission
282 	 */
283 	u8 i = perm >> 5;
284 
285 	dest->used = src->used;
286 	if (dest->used & XPERMS_ALLOWED)
287 		dest->allowed->p[i] = src->allowed->p[i];
288 	if (dest->used & XPERMS_AUDITALLOW)
289 		dest->auditallow->p[i] = src->auditallow->p[i];
290 	if (dest->used & XPERMS_DONTAUDIT)
291 		dest->dontaudit->p[i] = src->dontaudit->p[i];
292 }
293 
294 static struct avc_xperms_decision_node
295 		*avc_xperms_decision_alloc(u8 which)
296 {
297 	struct avc_xperms_decision_node *xpd_node;
298 	struct extended_perms_decision *xpd;
299 
300 	xpd_node = kmem_cache_zalloc(avc_xperms_decision_cachep,
301 				     GFP_NOWAIT | __GFP_NOWARN);
302 	if (!xpd_node)
303 		return NULL;
304 
305 	xpd = &xpd_node->xpd;
306 	if (which & XPERMS_ALLOWED) {
307 		xpd->allowed = kmem_cache_zalloc(avc_xperms_data_cachep,
308 						GFP_NOWAIT | __GFP_NOWARN);
309 		if (!xpd->allowed)
310 			goto error;
311 	}
312 	if (which & XPERMS_AUDITALLOW) {
313 		xpd->auditallow = kmem_cache_zalloc(avc_xperms_data_cachep,
314 						GFP_NOWAIT | __GFP_NOWARN);
315 		if (!xpd->auditallow)
316 			goto error;
317 	}
318 	if (which & XPERMS_DONTAUDIT) {
319 		xpd->dontaudit = kmem_cache_zalloc(avc_xperms_data_cachep,
320 						GFP_NOWAIT | __GFP_NOWARN);
321 		if (!xpd->dontaudit)
322 			goto error;
323 	}
324 	return xpd_node;
325 error:
326 	avc_xperms_decision_free(xpd_node);
327 	return NULL;
328 }
329 
330 static int avc_add_xperms_decision(struct avc_node *node,
331 			struct extended_perms_decision *src)
332 {
333 	struct avc_xperms_decision_node *dest_xpd;
334 
335 	node->ae.xp_node->xp.len++;
336 	dest_xpd = avc_xperms_decision_alloc(src->used);
337 	if (!dest_xpd)
338 		return -ENOMEM;
339 	avc_copy_xperms_decision(&dest_xpd->xpd, src);
340 	list_add(&dest_xpd->xpd_list, &node->ae.xp_node->xpd_head);
341 	return 0;
342 }
343 
344 static struct avc_xperms_node *avc_xperms_alloc(void)
345 {
346 	struct avc_xperms_node *xp_node;
347 
348 	xp_node = kmem_cache_zalloc(avc_xperms_cachep, GFP_NOWAIT | __GFP_NOWARN);
349 	if (!xp_node)
350 		return xp_node;
351 	INIT_LIST_HEAD(&xp_node->xpd_head);
352 	return xp_node;
353 }
354 
355 static int avc_xperms_populate(struct avc_node *node,
356 				struct avc_xperms_node *src)
357 {
358 	struct avc_xperms_node *dest;
359 	struct avc_xperms_decision_node *dest_xpd;
360 	struct avc_xperms_decision_node *src_xpd;
361 
362 	if (src->xp.len == 0)
363 		return 0;
364 	dest = avc_xperms_alloc();
365 	if (!dest)
366 		return -ENOMEM;
367 
368 	memcpy(dest->xp.drivers.p, src->xp.drivers.p, sizeof(dest->xp.drivers.p));
369 	dest->xp.len = src->xp.len;
370 
371 	/* for each source xpd allocate a destination xpd and copy */
372 	list_for_each_entry(src_xpd, &src->xpd_head, xpd_list) {
373 		dest_xpd = avc_xperms_decision_alloc(src_xpd->xpd.used);
374 		if (!dest_xpd)
375 			goto error;
376 		avc_copy_xperms_decision(&dest_xpd->xpd, &src_xpd->xpd);
377 		list_add(&dest_xpd->xpd_list, &dest->xpd_head);
378 	}
379 	node->ae.xp_node = dest;
380 	return 0;
381 error:
382 	avc_xperms_free(dest);
383 	return -ENOMEM;
384 
385 }
386 
387 static inline u32 avc_xperms_audit_required(u32 requested,
388 					struct av_decision *avd,
389 					struct extended_perms_decision *xpd,
390 					u8 perm,
391 					int result,
392 					u32 *deniedp)
393 {
394 	u32 denied, audited;
395 
396 	denied = requested & ~avd->allowed;
397 	if (unlikely(denied)) {
398 		audited = denied & avd->auditdeny;
399 		if (audited && xpd) {
400 			if (avc_xperms_has_perm(xpd, perm, XPERMS_DONTAUDIT))
401 				audited &= ~requested;
402 		}
403 	} else if (result) {
404 		audited = denied = requested;
405 	} else {
406 		audited = requested & avd->auditallow;
407 		if (audited && xpd) {
408 			if (!avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW))
409 				audited &= ~requested;
410 		}
411 	}
412 
413 	*deniedp = denied;
414 	return audited;
415 }
416 
417 static inline int avc_xperms_audit(struct selinux_state *state,
418 				   u32 ssid, u32 tsid, u16 tclass,
419 				   u32 requested, struct av_decision *avd,
420 				   struct extended_perms_decision *xpd,
421 				   u8 perm, int result,
422 				   struct common_audit_data *ad)
423 {
424 	u32 audited, denied;
425 
426 	audited = avc_xperms_audit_required(
427 			requested, avd, xpd, perm, result, &denied);
428 	if (likely(!audited))
429 		return 0;
430 	return slow_avc_audit(state, ssid, tsid, tclass, requested,
431 			audited, denied, result, ad);
432 }
433 
434 static void avc_node_free(struct rcu_head *rhead)
435 {
436 	struct avc_node *node = container_of(rhead, struct avc_node, rhead);
437 	avc_xperms_free(node->ae.xp_node);
438 	kmem_cache_free(avc_node_cachep, node);
439 	avc_cache_stats_incr(frees);
440 }
441 
442 static void avc_node_delete(struct selinux_avc *avc, struct avc_node *node)
443 {
444 	hlist_del_rcu(&node->list);
445 	call_rcu(&node->rhead, avc_node_free);
446 	atomic_dec(&avc->avc_cache.active_nodes);
447 }
448 
449 static void avc_node_kill(struct selinux_avc *avc, struct avc_node *node)
450 {
451 	avc_xperms_free(node->ae.xp_node);
452 	kmem_cache_free(avc_node_cachep, node);
453 	avc_cache_stats_incr(frees);
454 	atomic_dec(&avc->avc_cache.active_nodes);
455 }
456 
457 static void avc_node_replace(struct selinux_avc *avc,
458 			     struct avc_node *new, struct avc_node *old)
459 {
460 	hlist_replace_rcu(&old->list, &new->list);
461 	call_rcu(&old->rhead, avc_node_free);
462 	atomic_dec(&avc->avc_cache.active_nodes);
463 }
464 
465 static inline int avc_reclaim_node(struct selinux_avc *avc)
466 {
467 	struct avc_node *node;
468 	int hvalue, try, ecx;
469 	unsigned long flags;
470 	struct hlist_head *head;
471 	spinlock_t *lock;
472 
473 	for (try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++) {
474 		hvalue = atomic_inc_return(&avc->avc_cache.lru_hint) &
475 			(AVC_CACHE_SLOTS - 1);
476 		head = &avc->avc_cache.slots[hvalue];
477 		lock = &avc->avc_cache.slots_lock[hvalue];
478 
479 		if (!spin_trylock_irqsave(lock, flags))
480 			continue;
481 
482 		rcu_read_lock();
483 		hlist_for_each_entry(node, head, list) {
484 			avc_node_delete(avc, node);
485 			avc_cache_stats_incr(reclaims);
486 			ecx++;
487 			if (ecx >= AVC_CACHE_RECLAIM) {
488 				rcu_read_unlock();
489 				spin_unlock_irqrestore(lock, flags);
490 				goto out;
491 			}
492 		}
493 		rcu_read_unlock();
494 		spin_unlock_irqrestore(lock, flags);
495 	}
496 out:
497 	return ecx;
498 }
499 
500 static struct avc_node *avc_alloc_node(struct selinux_avc *avc)
501 {
502 	struct avc_node *node;
503 
504 	node = kmem_cache_zalloc(avc_node_cachep, GFP_NOWAIT | __GFP_NOWARN);
505 	if (!node)
506 		goto out;
507 
508 	INIT_HLIST_NODE(&node->list);
509 	avc_cache_stats_incr(allocations);
510 
511 	if (atomic_inc_return(&avc->avc_cache.active_nodes) >
512 	    avc->avc_cache_threshold)
513 		avc_reclaim_node(avc);
514 
515 out:
516 	return node;
517 }
518 
519 static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
520 {
521 	node->ae.ssid = ssid;
522 	node->ae.tsid = tsid;
523 	node->ae.tclass = tclass;
524 	memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
525 }
526 
527 static inline struct avc_node *avc_search_node(struct selinux_avc *avc,
528 					       u32 ssid, u32 tsid, u16 tclass)
529 {
530 	struct avc_node *node, *ret = NULL;
531 	int hvalue;
532 	struct hlist_head *head;
533 
534 	hvalue = avc_hash(ssid, tsid, tclass);
535 	head = &avc->avc_cache.slots[hvalue];
536 	hlist_for_each_entry_rcu(node, head, list) {
537 		if (ssid == node->ae.ssid &&
538 		    tclass == node->ae.tclass &&
539 		    tsid == node->ae.tsid) {
540 			ret = node;
541 			break;
542 		}
543 	}
544 
545 	return ret;
546 }
547 
548 /**
549  * avc_lookup - Look up an AVC entry.
550  * @avc: the access vector cache
551  * @ssid: source security identifier
552  * @tsid: target security identifier
553  * @tclass: target security class
554  *
555  * Look up an AVC entry that is valid for the
556  * (@ssid, @tsid), interpreting the permissions
557  * based on @tclass.  If a valid AVC entry exists,
558  * then this function returns the avc_node.
559  * Otherwise, this function returns NULL.
560  */
561 static struct avc_node *avc_lookup(struct selinux_avc *avc,
562 				   u32 ssid, u32 tsid, u16 tclass)
563 {
564 	struct avc_node *node;
565 
566 	avc_cache_stats_incr(lookups);
567 	node = avc_search_node(avc, ssid, tsid, tclass);
568 
569 	if (node)
570 		return node;
571 
572 	avc_cache_stats_incr(misses);
573 	return NULL;
574 }
575 
576 static int avc_latest_notif_update(struct selinux_avc *avc,
577 				   int seqno, int is_insert)
578 {
579 	int ret = 0;
580 	static DEFINE_SPINLOCK(notif_lock);
581 	unsigned long flag;
582 
583 	spin_lock_irqsave(&notif_lock, flag);
584 	if (is_insert) {
585 		if (seqno < avc->avc_cache.latest_notif) {
586 			pr_warn("SELinux: avc:  seqno %d < latest_notif %d\n",
587 			       seqno, avc->avc_cache.latest_notif);
588 			ret = -EAGAIN;
589 		}
590 	} else {
591 		if (seqno > avc->avc_cache.latest_notif)
592 			avc->avc_cache.latest_notif = seqno;
593 	}
594 	spin_unlock_irqrestore(&notif_lock, flag);
595 
596 	return ret;
597 }
598 
599 /**
600  * avc_insert - Insert an AVC entry.
601  * @avc: the access vector cache
602  * @ssid: source security identifier
603  * @tsid: target security identifier
604  * @tclass: target security class
605  * @avd: resulting av decision
606  * @xp_node: resulting extended permissions
607  *
608  * Insert an AVC entry for the SID pair
609  * (@ssid, @tsid) and class @tclass.
610  * The access vectors and the sequence number are
611  * normally provided by the security server in
612  * response to a security_compute_av() call.  If the
613  * sequence number @avd->seqno is not less than the latest
614  * revocation notification, then the function copies
615  * the access vectors into a cache entry, returns
616  * avc_node inserted. Otherwise, this function returns NULL.
617  */
618 static struct avc_node *avc_insert(struct selinux_avc *avc,
619 				   u32 ssid, u32 tsid, u16 tclass,
620 				   struct av_decision *avd,
621 				   struct avc_xperms_node *xp_node)
622 {
623 	struct avc_node *pos, *node = NULL;
624 	int hvalue;
625 	unsigned long flag;
626 	spinlock_t *lock;
627 	struct hlist_head *head;
628 
629 	if (avc_latest_notif_update(avc, avd->seqno, 1))
630 		return NULL;
631 
632 	node = avc_alloc_node(avc);
633 	if (!node)
634 		return NULL;
635 
636 	avc_node_populate(node, ssid, tsid, tclass, avd);
637 	if (avc_xperms_populate(node, xp_node)) {
638 		avc_node_kill(avc, node);
639 		return NULL;
640 	}
641 
642 	hvalue = avc_hash(ssid, tsid, tclass);
643 	head = &avc->avc_cache.slots[hvalue];
644 	lock = &avc->avc_cache.slots_lock[hvalue];
645 	spin_lock_irqsave(lock, flag);
646 	hlist_for_each_entry(pos, head, list) {
647 		if (pos->ae.ssid == ssid &&
648 			pos->ae.tsid == tsid &&
649 			pos->ae.tclass == tclass) {
650 			avc_node_replace(avc, node, pos);
651 			goto found;
652 		}
653 	}
654 	hlist_add_head_rcu(&node->list, head);
655 found:
656 	spin_unlock_irqrestore(lock, flag);
657 	return node;
658 }
659 
660 /**
661  * avc_audit_pre_callback - SELinux specific information
662  * will be called by generic audit code
663  * @ab: the audit buffer
664  * @a: audit_data
665  */
666 static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
667 {
668 	struct common_audit_data *ad = a;
669 	struct selinux_audit_data *sad = ad->selinux_audit_data;
670 	u32 av = sad->audited;
671 	const char *const *perms;
672 	int i, perm;
673 
674 	audit_log_format(ab, "avc:  %s ", sad->denied ? "denied" : "granted");
675 
676 	if (av == 0) {
677 		audit_log_format(ab, " null");
678 		return;
679 	}
680 
681 	perms = secclass_map[sad->tclass-1].perms;
682 
683 	audit_log_format(ab, " {");
684 	i = 0;
685 	perm = 1;
686 	while (i < (sizeof(av) * 8)) {
687 		if ((perm & av) && perms[i]) {
688 			audit_log_format(ab, " %s", perms[i]);
689 			av &= ~perm;
690 		}
691 		i++;
692 		perm <<= 1;
693 	}
694 
695 	if (av)
696 		audit_log_format(ab, " 0x%x", av);
697 
698 	audit_log_format(ab, " } for ");
699 }
700 
701 /**
702  * avc_audit_post_callback - SELinux specific information
703  * will be called by generic audit code
704  * @ab: the audit buffer
705  * @a: audit_data
706  */
707 static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
708 {
709 	struct common_audit_data *ad = a;
710 	struct selinux_audit_data *sad = ad->selinux_audit_data;
711 	char *scontext = NULL;
712 	char *tcontext = NULL;
713 	const char *tclass = NULL;
714 	u32 scontext_len;
715 	u32 tcontext_len;
716 	int rc;
717 
718 	rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
719 				     &scontext_len);
720 	if (rc)
721 		audit_log_format(ab, " ssid=%d", sad->ssid);
722 	else
723 		audit_log_format(ab, " scontext=%s", scontext);
724 
725 	rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
726 				     &tcontext_len);
727 	if (rc)
728 		audit_log_format(ab, " tsid=%d", sad->tsid);
729 	else
730 		audit_log_format(ab, " tcontext=%s", tcontext);
731 
732 	tclass = secclass_map[sad->tclass-1].name;
733 	audit_log_format(ab, " tclass=%s", tclass);
734 
735 	if (sad->denied)
736 		audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
737 
738 	trace_selinux_audited(sad, scontext, tcontext, tclass);
739 	kfree(tcontext);
740 	kfree(scontext);
741 
742 	/* in case of invalid context report also the actual context string */
743 	rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
744 					   &scontext_len);
745 	if (!rc && scontext) {
746 		if (scontext_len && scontext[scontext_len - 1] == '\0')
747 			scontext_len--;
748 		audit_log_format(ab, " srawcon=");
749 		audit_log_n_untrustedstring(ab, scontext, scontext_len);
750 		kfree(scontext);
751 	}
752 
753 	rc = security_sid_to_context_inval(sad->state, sad->tsid, &scontext,
754 					   &scontext_len);
755 	if (!rc && scontext) {
756 		if (scontext_len && scontext[scontext_len - 1] == '\0')
757 			scontext_len--;
758 		audit_log_format(ab, " trawcon=");
759 		audit_log_n_untrustedstring(ab, scontext, scontext_len);
760 		kfree(scontext);
761 	}
762 }
763 
764 /*
765  * This is the slow part of avc audit with big stack footprint.
766  * Note that it is non-blocking and can be called from under
767  * rcu_read_lock().
768  */
769 noinline int slow_avc_audit(struct selinux_state *state,
770 			    u32 ssid, u32 tsid, u16 tclass,
771 			    u32 requested, u32 audited, u32 denied, int result,
772 			    struct common_audit_data *a)
773 {
774 	struct common_audit_data stack_data;
775 	struct selinux_audit_data sad;
776 
777 	if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
778 		return -EINVAL;
779 
780 	if (!a) {
781 		a = &stack_data;
782 		a->type = LSM_AUDIT_DATA_NONE;
783 	}
784 
785 	sad.tclass = tclass;
786 	sad.requested = requested;
787 	sad.ssid = ssid;
788 	sad.tsid = tsid;
789 	sad.audited = audited;
790 	sad.denied = denied;
791 	sad.result = result;
792 	sad.state = state;
793 
794 	a->selinux_audit_data = &sad;
795 
796 	common_lsm_audit(a, avc_audit_pre_callback, avc_audit_post_callback);
797 	return 0;
798 }
799 
800 /**
801  * avc_add_callback - Register a callback for security events.
802  * @callback: callback function
803  * @events: security events
804  *
805  * Register a callback function for events in the set @events.
806  * Returns %0 on success or -%ENOMEM if insufficient memory
807  * exists to add the callback.
808  */
809 int __init avc_add_callback(int (*callback)(u32 event), u32 events)
810 {
811 	struct avc_callback_node *c;
812 	int rc = 0;
813 
814 	c = kmalloc(sizeof(*c), GFP_KERNEL);
815 	if (!c) {
816 		rc = -ENOMEM;
817 		goto out;
818 	}
819 
820 	c->callback = callback;
821 	c->events = events;
822 	c->next = avc_callbacks;
823 	avc_callbacks = c;
824 out:
825 	return rc;
826 }
827 
828 /**
829  * avc_update_node - Update an AVC entry
830  * @avc: the access vector cache
831  * @event : Updating event
832  * @perms : Permission mask bits
833  * @driver: xperm driver information
834  * @xperm: xperm permissions
835  * @ssid: AVC entry source sid
836  * @tsid: AVC entry target sid
837  * @tclass : AVC entry target object class
838  * @seqno : sequence number when decision was made
839  * @xpd: extended_perms_decision to be added to the node
840  * @flags: the AVC_* flags, e.g. AVC_EXTENDED_PERMS, or 0.
841  *
842  * if a valid AVC entry doesn't exist,this function returns -ENOENT.
843  * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
844  * otherwise, this function updates the AVC entry. The original AVC-entry object
845  * will release later by RCU.
846  */
847 static int avc_update_node(struct selinux_avc *avc,
848 			   u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,
849 			   u32 tsid, u16 tclass, u32 seqno,
850 			   struct extended_perms_decision *xpd,
851 			   u32 flags)
852 {
853 	int hvalue, rc = 0;
854 	unsigned long flag;
855 	struct avc_node *pos, *node, *orig = NULL;
856 	struct hlist_head *head;
857 	spinlock_t *lock;
858 
859 	node = avc_alloc_node(avc);
860 	if (!node) {
861 		rc = -ENOMEM;
862 		goto out;
863 	}
864 
865 	/* Lock the target slot */
866 	hvalue = avc_hash(ssid, tsid, tclass);
867 
868 	head = &avc->avc_cache.slots[hvalue];
869 	lock = &avc->avc_cache.slots_lock[hvalue];
870 
871 	spin_lock_irqsave(lock, flag);
872 
873 	hlist_for_each_entry(pos, head, list) {
874 		if (ssid == pos->ae.ssid &&
875 		    tsid == pos->ae.tsid &&
876 		    tclass == pos->ae.tclass &&
877 		    seqno == pos->ae.avd.seqno){
878 			orig = pos;
879 			break;
880 		}
881 	}
882 
883 	if (!orig) {
884 		rc = -ENOENT;
885 		avc_node_kill(avc, node);
886 		goto out_unlock;
887 	}
888 
889 	/*
890 	 * Copy and replace original node.
891 	 */
892 
893 	avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);
894 
895 	if (orig->ae.xp_node) {
896 		rc = avc_xperms_populate(node, orig->ae.xp_node);
897 		if (rc) {
898 			avc_node_kill(avc, node);
899 			goto out_unlock;
900 		}
901 	}
902 
903 	switch (event) {
904 	case AVC_CALLBACK_GRANT:
905 		node->ae.avd.allowed |= perms;
906 		if (node->ae.xp_node && (flags & AVC_EXTENDED_PERMS))
907 			avc_xperms_allow_perm(node->ae.xp_node, driver, xperm);
908 		break;
909 	case AVC_CALLBACK_TRY_REVOKE:
910 	case AVC_CALLBACK_REVOKE:
911 		node->ae.avd.allowed &= ~perms;
912 		break;
913 	case AVC_CALLBACK_AUDITALLOW_ENABLE:
914 		node->ae.avd.auditallow |= perms;
915 		break;
916 	case AVC_CALLBACK_AUDITALLOW_DISABLE:
917 		node->ae.avd.auditallow &= ~perms;
918 		break;
919 	case AVC_CALLBACK_AUDITDENY_ENABLE:
920 		node->ae.avd.auditdeny |= perms;
921 		break;
922 	case AVC_CALLBACK_AUDITDENY_DISABLE:
923 		node->ae.avd.auditdeny &= ~perms;
924 		break;
925 	case AVC_CALLBACK_ADD_XPERMS:
926 		avc_add_xperms_decision(node, xpd);
927 		break;
928 	}
929 	avc_node_replace(avc, node, orig);
930 out_unlock:
931 	spin_unlock_irqrestore(lock, flag);
932 out:
933 	return rc;
934 }
935 
936 /**
937  * avc_flush - Flush the cache
938  * @avc: the access vector cache
939  */
940 static void avc_flush(struct selinux_avc *avc)
941 {
942 	struct hlist_head *head;
943 	struct avc_node *node;
944 	spinlock_t *lock;
945 	unsigned long flag;
946 	int i;
947 
948 	for (i = 0; i < AVC_CACHE_SLOTS; i++) {
949 		head = &avc->avc_cache.slots[i];
950 		lock = &avc->avc_cache.slots_lock[i];
951 
952 		spin_lock_irqsave(lock, flag);
953 		/*
954 		 * With preemptable RCU, the outer spinlock does not
955 		 * prevent RCU grace periods from ending.
956 		 */
957 		rcu_read_lock();
958 		hlist_for_each_entry(node, head, list)
959 			avc_node_delete(avc, node);
960 		rcu_read_unlock();
961 		spin_unlock_irqrestore(lock, flag);
962 	}
963 }
964 
965 /**
966  * avc_ss_reset - Flush the cache and revalidate migrated permissions.
967  * @avc: the access vector cache
968  * @seqno: policy sequence number
969  */
970 int avc_ss_reset(struct selinux_avc *avc, u32 seqno)
971 {
972 	struct avc_callback_node *c;
973 	int rc = 0, tmprc;
974 
975 	avc_flush(avc);
976 
977 	for (c = avc_callbacks; c; c = c->next) {
978 		if (c->events & AVC_CALLBACK_RESET) {
979 			tmprc = c->callback(AVC_CALLBACK_RESET);
980 			/* save the first error encountered for the return
981 			   value and continue processing the callbacks */
982 			if (!rc)
983 				rc = tmprc;
984 		}
985 	}
986 
987 	avc_latest_notif_update(avc, seqno, 0);
988 	return rc;
989 }
990 
991 /*
992  * Slow-path helper function for avc_has_perm_noaudit,
993  * when the avc_node lookup fails. We get called with
994  * the RCU read lock held, and need to return with it
995  * still held, but drop if for the security compute.
996  *
997  * Don't inline this, since it's the slow-path and just
998  * results in a bigger stack frame.
999  */
1000 static noinline
1001 struct avc_node *avc_compute_av(struct selinux_state *state,
1002 				u32 ssid, u32 tsid,
1003 				u16 tclass, struct av_decision *avd,
1004 				struct avc_xperms_node *xp_node)
1005 {
1006 	rcu_read_unlock();
1007 	INIT_LIST_HEAD(&xp_node->xpd_head);
1008 	security_compute_av(state, ssid, tsid, tclass, avd, &xp_node->xp);
1009 	rcu_read_lock();
1010 	return avc_insert(state->avc, ssid, tsid, tclass, avd, xp_node);
1011 }
1012 
1013 static noinline int avc_denied(struct selinux_state *state,
1014 			       u32 ssid, u32 tsid,
1015 			       u16 tclass, u32 requested,
1016 			       u8 driver, u8 xperm, unsigned int flags,
1017 			       struct av_decision *avd)
1018 {
1019 	if (flags & AVC_STRICT)
1020 		return -EACCES;
1021 
1022 	if (enforcing_enabled(state) &&
1023 	    !(avd->flags & AVD_FLAGS_PERMISSIVE))
1024 		return -EACCES;
1025 
1026 	avc_update_node(state->avc, AVC_CALLBACK_GRANT, requested, driver,
1027 			xperm, ssid, tsid, tclass, avd->seqno, NULL, flags);
1028 	return 0;
1029 }
1030 
1031 /*
1032  * The avc extended permissions logic adds an additional 256 bits of
1033  * permissions to an avc node when extended permissions for that node are
1034  * specified in the avtab. If the additional 256 permissions is not adequate,
1035  * as-is the case with ioctls, then multiple may be chained together and the
1036  * driver field is used to specify which set contains the permission.
1037  */
1038 int avc_has_extended_perms(struct selinux_state *state,
1039 			   u32 ssid, u32 tsid, u16 tclass, u32 requested,
1040 			   u8 driver, u8 xperm, struct common_audit_data *ad)
1041 {
1042 	struct avc_node *node;
1043 	struct av_decision avd;
1044 	u32 denied;
1045 	struct extended_perms_decision local_xpd;
1046 	struct extended_perms_decision *xpd = NULL;
1047 	struct extended_perms_data allowed;
1048 	struct extended_perms_data auditallow;
1049 	struct extended_perms_data dontaudit;
1050 	struct avc_xperms_node local_xp_node;
1051 	struct avc_xperms_node *xp_node;
1052 	int rc = 0, rc2;
1053 
1054 	xp_node = &local_xp_node;
1055 	if (WARN_ON(!requested))
1056 		return -EACCES;
1057 
1058 	rcu_read_lock();
1059 
1060 	node = avc_lookup(state->avc, ssid, tsid, tclass);
1061 	if (unlikely(!node)) {
1062 		avc_compute_av(state, ssid, tsid, tclass, &avd, xp_node);
1063 	} else {
1064 		memcpy(&avd, &node->ae.avd, sizeof(avd));
1065 		xp_node = node->ae.xp_node;
1066 	}
1067 	/* if extended permissions are not defined, only consider av_decision */
1068 	if (!xp_node || !xp_node->xp.len)
1069 		goto decision;
1070 
1071 	local_xpd.allowed = &allowed;
1072 	local_xpd.auditallow = &auditallow;
1073 	local_xpd.dontaudit = &dontaudit;
1074 
1075 	xpd = avc_xperms_decision_lookup(driver, xp_node);
1076 	if (unlikely(!xpd)) {
1077 		/*
1078 		 * Compute the extended_perms_decision only if the driver
1079 		 * is flagged
1080 		 */
1081 		if (!security_xperm_test(xp_node->xp.drivers.p, driver)) {
1082 			avd.allowed &= ~requested;
1083 			goto decision;
1084 		}
1085 		rcu_read_unlock();
1086 		security_compute_xperms_decision(state, ssid, tsid, tclass,
1087 						 driver, &local_xpd);
1088 		rcu_read_lock();
1089 		avc_update_node(state->avc, AVC_CALLBACK_ADD_XPERMS, requested,
1090 				driver, xperm, ssid, tsid, tclass, avd.seqno,
1091 				&local_xpd, 0);
1092 	} else {
1093 		avc_quick_copy_xperms_decision(xperm, &local_xpd, xpd);
1094 	}
1095 	xpd = &local_xpd;
1096 
1097 	if (!avc_xperms_has_perm(xpd, xperm, XPERMS_ALLOWED))
1098 		avd.allowed &= ~requested;
1099 
1100 decision:
1101 	denied = requested & ~(avd.allowed);
1102 	if (unlikely(denied))
1103 		rc = avc_denied(state, ssid, tsid, tclass, requested,
1104 				driver, xperm, AVC_EXTENDED_PERMS, &avd);
1105 
1106 	rcu_read_unlock();
1107 
1108 	rc2 = avc_xperms_audit(state, ssid, tsid, tclass, requested,
1109 			&avd, xpd, xperm, rc, ad);
1110 	if (rc2)
1111 		return rc2;
1112 	return rc;
1113 }
1114 
1115 /**
1116  * avc_has_perm_noaudit - Check permissions but perform no auditing.
1117  * @state: SELinux state
1118  * @ssid: source security identifier
1119  * @tsid: target security identifier
1120  * @tclass: target security class
1121  * @requested: requested permissions, interpreted based on @tclass
1122  * @flags:  AVC_STRICT or 0
1123  * @avd: access vector decisions
1124  *
1125  * Check the AVC to determine whether the @requested permissions are granted
1126  * for the SID pair (@ssid, @tsid), interpreting the permissions
1127  * based on @tclass, and call the security server on a cache miss to obtain
1128  * a new decision and add it to the cache.  Return a copy of the decisions
1129  * in @avd.  Return %0 if all @requested permissions are granted,
1130  * -%EACCES if any permissions are denied, or another -errno upon
1131  * other errors.  This function is typically called by avc_has_perm(),
1132  * but may also be called directly to separate permission checking from
1133  * auditing, e.g. in cases where a lock must be held for the check but
1134  * should be released for the auditing.
1135  */
1136 inline int avc_has_perm_noaudit(struct selinux_state *state,
1137 				u32 ssid, u32 tsid,
1138 				u16 tclass, u32 requested,
1139 				unsigned int flags,
1140 				struct av_decision *avd)
1141 {
1142 	struct avc_node *node;
1143 	struct avc_xperms_node xp_node;
1144 	int rc = 0;
1145 	u32 denied;
1146 
1147 	if (WARN_ON(!requested))
1148 		return -EACCES;
1149 
1150 	rcu_read_lock();
1151 
1152 	node = avc_lookup(state->avc, ssid, tsid, tclass);
1153 	if (unlikely(!node))
1154 		avc_compute_av(state, ssid, tsid, tclass, avd, &xp_node);
1155 	else
1156 		memcpy(avd, &node->ae.avd, sizeof(*avd));
1157 
1158 	denied = requested & ~(avd->allowed);
1159 	if (unlikely(denied))
1160 		rc = avc_denied(state, ssid, tsid, tclass, requested, 0, 0,
1161 				flags, avd);
1162 
1163 	rcu_read_unlock();
1164 	return rc;
1165 }
1166 
1167 /**
1168  * avc_has_perm - Check permissions and perform any appropriate auditing.
1169  * @state: SELinux state
1170  * @ssid: source security identifier
1171  * @tsid: target security identifier
1172  * @tclass: target security class
1173  * @requested: requested permissions, interpreted based on @tclass
1174  * @auditdata: auxiliary audit data
1175  *
1176  * Check the AVC to determine whether the @requested permissions are granted
1177  * for the SID pair (@ssid, @tsid), interpreting the permissions
1178  * based on @tclass, and call the security server on a cache miss to obtain
1179  * a new decision and add it to the cache.  Audit the granting or denial of
1180  * permissions in accordance with the policy.  Return %0 if all @requested
1181  * permissions are granted, -%EACCES if any permissions are denied, or
1182  * another -errno upon other errors.
1183  */
1184 int avc_has_perm(struct selinux_state *state, u32 ssid, u32 tsid, u16 tclass,
1185 		 u32 requested, struct common_audit_data *auditdata)
1186 {
1187 	struct av_decision avd;
1188 	int rc, rc2;
1189 
1190 	rc = avc_has_perm_noaudit(state, ssid, tsid, tclass, requested, 0,
1191 				  &avd);
1192 
1193 	rc2 = avc_audit(state, ssid, tsid, tclass, requested, &avd, rc,
1194 			auditdata);
1195 	if (rc2)
1196 		return rc2;
1197 	return rc;
1198 }
1199 
1200 u32 avc_policy_seqno(struct selinux_state *state)
1201 {
1202 	return state->avc->avc_cache.latest_notif;
1203 }
1204 
1205 void avc_disable(void)
1206 {
1207 	/*
1208 	 * If you are looking at this because you have realized that we are
1209 	 * not destroying the avc_node_cachep it might be easy to fix, but
1210 	 * I don't know the memory barrier semantics well enough to know.  It's
1211 	 * possible that some other task dereferenced security_ops when
1212 	 * it still pointed to selinux operations.  If that is the case it's
1213 	 * possible that it is about to use the avc and is about to need the
1214 	 * avc_node_cachep.  I know I could wrap the security.c security_ops call
1215 	 * in an rcu_lock, but seriously, it's not worth it.  Instead I just flush
1216 	 * the cache and get that memory back.
1217 	 */
1218 	if (avc_node_cachep) {
1219 		avc_flush(selinux_state.avc);
1220 		/* kmem_cache_destroy(avc_node_cachep); */
1221 	}
1222 }
1223