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