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