1 /*
2  * Implementation of the userspace access vector cache (AVC).
3  *
4  * Author : Eamon Walsh <ewalsh@epoch.ncsc.mil>
5  *
6  * Derived from the kernel AVC implementation by
7  * Stephen Smalley <sds@epoch.ncsc.mil> and
8  * James Morris <jmorris@redhat.com>.
9  */
10 #include <selinux/avc.h>
11 #include "selinux_internal.h"
12 #include <assert.h>
13 #include "avc_sidtab.h"
14 #include "avc_internal.h"
15 
16 #define AVC_CACHE_SLOTS		512
17 #define AVC_CACHE_MAXNODES	410
18 
19 struct avc_entry {
20 	security_id_t ssid;
21 	security_id_t tsid;
22 	security_class_t tclass;
23 	struct av_decision avd;
24 	security_id_t	create_sid;
25 	int used;		/* used recently */
26 };
27 
28 struct avc_node {
29 	struct avc_entry ae;
30 	struct avc_node *next;
31 };
32 
33 struct avc_cache {
34 	struct avc_node *slots[AVC_CACHE_SLOTS];
35 	uint32_t lru_hint;	/* LRU hint for reclaim scan */
36 	uint32_t active_nodes;
37 	uint32_t latest_notif;	/* latest revocation notification */
38 };
39 
40 struct avc_callback_node {
41 	int (*callback) (uint32_t event, security_id_t ssid,
42 			 security_id_t tsid,
43 			 security_class_t tclass, access_vector_t perms,
44 			 access_vector_t * out_retained);
45 	uint32_t events;
46 	security_id_t ssid;
47 	security_id_t tsid;
48 	security_class_t tclass;
49 	access_vector_t perms;
50 	struct avc_callback_node *next;
51 };
52 
53 static void *avc_netlink_thread = NULL;
54 static void *avc_lock = NULL;
55 static void *avc_log_lock = NULL;
56 static struct avc_node *avc_node_freelist = NULL;
57 static struct avc_cache avc_cache;
58 static char *avc_audit_buf = NULL;
59 static struct avc_cache_stats cache_stats;
60 static struct avc_callback_node *avc_callbacks = NULL;
61 static struct sidtab avc_sidtab;
62 
avc_hash(security_id_t ssid,security_id_t tsid,security_class_t tclass)63 static inline int avc_hash(security_id_t ssid,
64 			   security_id_t tsid, security_class_t tclass)
65 {
66 	return ((uintptr_t) ssid ^ ((uintptr_t) tsid << 2) ^ tclass)
67 	    & (AVC_CACHE_SLOTS - 1);
68 }
69 
avc_context_to_sid(const char * ctx,security_id_t * sid)70 int avc_context_to_sid(const char * ctx, security_id_t * sid)
71 {
72 	int rc;
73 	/* avc_init needs to be called before this function */
74 	assert(avc_running);
75 
76 	avc_get_lock(avc_lock);
77 	rc = sidtab_context_to_sid(&avc_sidtab, ctx, sid);
78 	avc_release_lock(avc_lock);
79 	return rc;
80 }
81 
avc_sid_to_context(security_id_t sid,char ** ctx)82 int avc_sid_to_context(security_id_t sid, char ** ctx)
83 {
84 	int rc;
85 	*ctx = NULL;
86 	avc_get_lock(avc_lock);
87 	*ctx = strdup(sid->ctx);	/* caller must free via freecon */
88 	rc = *ctx ? 0 : -1;
89 	avc_release_lock(avc_lock);
90 	return rc;
91 }
92 
avc_get_initial_sid(const char * name,security_id_t * sid)93 int avc_get_initial_sid(const char * name, security_id_t * sid)
94 {
95 	int rc;
96 	char * con;
97 
98 	rc = security_get_initial_context(name, &con);
99 	if (rc < 0)
100 		return rc;
101 	rc = avc_context_to_sid(con, sid);
102 
103 	freecon(con);
104 
105 	return rc;
106 }
107 
avc_open(struct selinux_opt * opts,unsigned nopts)108 int avc_open(struct selinux_opt *opts, unsigned nopts)
109 {
110 	avc_setenforce = 0;
111 
112 	while (nopts--)
113 		switch(opts[nopts].type) {
114 		case AVC_OPT_SETENFORCE:
115 			avc_setenforce = 1;
116 			avc_enforcing = !!opts[nopts].value;
117 			break;
118 		}
119 
120 	return avc_init("avc", NULL, NULL, NULL, NULL);
121 }
122 
avc_init(const char * prefix,const struct avc_memory_callback * mem_cb,const struct avc_log_callback * log_cb,const struct avc_thread_callback * thread_cb,const struct avc_lock_callback * lock_cb)123 int avc_init(const char *prefix,
124 	     const struct avc_memory_callback *mem_cb,
125 	     const struct avc_log_callback *log_cb,
126 	     const struct avc_thread_callback *thread_cb,
127 	     const struct avc_lock_callback *lock_cb)
128 {
129 	struct avc_node *new;
130 	int i, rc = 0;
131 
132 	if (avc_running)
133 		return 0;
134 
135 	if (prefix)
136 		strncpy(avc_prefix, prefix, AVC_PREFIX_SIZE - 1);
137 
138 	set_callbacks(mem_cb, log_cb, thread_cb, lock_cb);
139 
140 	avc_lock = avc_alloc_lock();
141 	avc_log_lock = avc_alloc_lock();
142 
143 	memset(&cache_stats, 0, sizeof(cache_stats));
144 
145 	for (i = 0; i < AVC_CACHE_SLOTS; i++)
146 		avc_cache.slots[i] = 0;
147 	avc_cache.lru_hint = 0;
148 	avc_cache.active_nodes = 0;
149 	avc_cache.latest_notif = 0;
150 
151 	rc = sidtab_init(&avc_sidtab);
152 	if (rc) {
153 		avc_log(SELINUX_ERROR,
154 			"%s:  unable to initialize SID table\n",
155 			avc_prefix);
156 		goto out;
157 	}
158 
159 	avc_audit_buf = (char *)avc_malloc(AVC_AUDIT_BUFSIZE);
160 	if (!avc_audit_buf) {
161 		avc_log(SELINUX_ERROR,
162 			"%s:  unable to allocate audit buffer\n",
163 			avc_prefix);
164 		rc = -1;
165 		goto out;
166 	}
167 
168 	for (i = 0; i < AVC_CACHE_MAXNODES; i++) {
169 		new = avc_malloc(sizeof(*new));
170 		if (!new) {
171 			avc_log(SELINUX_WARNING,
172 				"%s:  warning: only got %d av entries\n",
173 				avc_prefix, i);
174 			break;
175 		}
176 		memset(new, 0, sizeof(*new));
177 		new->next = avc_node_freelist;
178 		avc_node_freelist = new;
179 	}
180 
181 	if (!avc_setenforce) {
182 		rc = security_getenforce();
183 		if (rc < 0) {
184 			avc_log(SELINUX_ERROR,
185 				"%s:  could not determine enforcing mode: %s\n",
186 				avc_prefix,
187 				strerror(errno));
188 			goto out;
189 		}
190 		avc_enforcing = rc;
191 	}
192 
193 	rc = avc_netlink_open(0);
194 	if (rc < 0) {
195 		avc_log(SELINUX_ERROR,
196 			"%s:  can't open netlink socket: %d (%s)\n",
197 			avc_prefix, errno, strerror(errno));
198 		goto out;
199 	}
200 	if (avc_using_threads) {
201 		avc_netlink_thread = avc_create_thread(&avc_netlink_loop);
202 		avc_netlink_trouble = 0;
203 	}
204 	avc_running = 1;
205       out:
206 	return rc;
207 }
208 
avc_cache_stats(struct avc_cache_stats * p)209 void avc_cache_stats(struct avc_cache_stats *p)
210 {
211 	memcpy(p, &cache_stats, sizeof(cache_stats));
212 }
213 
avc_sid_stats(void)214 void avc_sid_stats(void)
215 {
216 	/* avc_init needs to be called before this function */
217 	assert(avc_running);
218 	avc_get_lock(avc_log_lock);
219 	avc_get_lock(avc_lock);
220 	sidtab_sid_stats(&avc_sidtab, avc_audit_buf, AVC_AUDIT_BUFSIZE);
221 	avc_release_lock(avc_lock);
222 	avc_log(SELINUX_INFO, "%s", avc_audit_buf);
223 	avc_release_lock(avc_log_lock);
224 }
225 
avc_av_stats(void)226 void avc_av_stats(void)
227 {
228 	int i, chain_len, max_chain_len, slots_used;
229 	struct avc_node *node;
230 
231 	avc_get_lock(avc_lock);
232 
233 	slots_used = 0;
234 	max_chain_len = 0;
235 	for (i = 0; i < AVC_CACHE_SLOTS; i++) {
236 		node = avc_cache.slots[i];
237 		if (node) {
238 			slots_used++;
239 			chain_len = 0;
240 			while (node) {
241 				chain_len++;
242 				node = node->next;
243 			}
244 			if (chain_len > max_chain_len)
245 				max_chain_len = chain_len;
246 		}
247 	}
248 
249 	avc_release_lock(avc_lock);
250 
251 	avc_log(SELINUX_INFO, "%s:  %d AV entries and %d/%d buckets used, "
252 		"longest chain length %d\n", avc_prefix,
253 		avc_cache.active_nodes,
254 		slots_used, AVC_CACHE_SLOTS, max_chain_len);
255 }
256 
hidden_def(avc_av_stats)257 hidden_def(avc_av_stats)
258 
259 static inline struct avc_node *avc_reclaim_node(void)
260 {
261 	struct avc_node *prev, *cur;
262 	int try;
263 	uint32_t hvalue;
264 
265 	hvalue = avc_cache.lru_hint;
266 	for (try = 0; try < 2; try++) {
267 		do {
268 			prev = NULL;
269 			cur = avc_cache.slots[hvalue];
270 			while (cur) {
271 				if (!cur->ae.used)
272 					goto found;
273 
274 				cur->ae.used = 0;
275 
276 				prev = cur;
277 				cur = cur->next;
278 			}
279 			hvalue = (hvalue + 1) & (AVC_CACHE_SLOTS - 1);
280 		} while (hvalue != avc_cache.lru_hint);
281 	}
282 
283 	errno = ENOMEM;		/* this was a panic in the kernel... */
284 	return NULL;
285 
286       found:
287 	avc_cache.lru_hint = hvalue;
288 
289 	if (prev == NULL)
290 		avc_cache.slots[hvalue] = cur->next;
291 	else
292 		prev->next = cur->next;
293 
294 	return cur;
295 }
296 
avc_clear_avc_entry(struct avc_entry * ae)297 static inline void avc_clear_avc_entry(struct avc_entry *ae)
298 {
299 	memset(ae, 0, sizeof(*ae));
300 }
301 
avc_claim_node(security_id_t ssid,security_id_t tsid,security_class_t tclass)302 static inline struct avc_node *avc_claim_node(security_id_t ssid,
303 					      security_id_t tsid,
304 					      security_class_t tclass)
305 {
306 	struct avc_node *new;
307 	int hvalue;
308 
309 	if (!avc_node_freelist)
310 		avc_cleanup();
311 
312 	if (avc_node_freelist) {
313 		new = avc_node_freelist;
314 		avc_node_freelist = avc_node_freelist->next;
315 		avc_cache.active_nodes++;
316 	} else {
317 		new = avc_reclaim_node();
318 		if (!new)
319 			goto out;
320 	}
321 
322 	hvalue = avc_hash(ssid, tsid, tclass);
323 	avc_clear_avc_entry(&new->ae);
324 	new->ae.used = 1;
325 	new->ae.ssid = ssid;
326 	new->ae.tsid = tsid;
327 	new->ae.tclass = tclass;
328 	new->next = avc_cache.slots[hvalue];
329 	avc_cache.slots[hvalue] = new;
330 
331       out:
332 	return new;
333 }
334 
avc_search_node(security_id_t ssid,security_id_t tsid,security_class_t tclass,int * probes)335 static inline struct avc_node *avc_search_node(security_id_t ssid,
336 					       security_id_t tsid,
337 					       security_class_t tclass,
338 					       int *probes)
339 {
340 	struct avc_node *cur;
341 	int hvalue;
342 	int tprobes = 1;
343 
344 	hvalue = avc_hash(ssid, tsid, tclass);
345 	cur = avc_cache.slots[hvalue];
346 	while (cur != NULL &&
347 	       (ssid != cur->ae.ssid ||
348 		tclass != cur->ae.tclass || tsid != cur->ae.tsid)) {
349 		tprobes++;
350 		cur = cur->next;
351 	}
352 
353 	if (cur == NULL) {
354 		/* cache miss */
355 		goto out;
356 	}
357 
358 	/* cache hit */
359 	if (probes)
360 		*probes = tprobes;
361 
362 	cur->ae.used = 1;
363 
364       out:
365 	return cur;
366 }
367 
368 /**
369  * avc_lookup - Look up an AVC entry.
370  * @ssid: source security identifier
371  * @tsid: target security identifier
372  * @tclass: target security class
373  * @requested: requested permissions, interpreted based on @tclass
374  * @aeref:  AVC entry reference
375  *
376  * Look up an AVC entry that is valid for the
377  * @requested permissions between the SID pair
378  * (@ssid, @tsid), interpreting the permissions
379  * based on @tclass.  If a valid AVC entry exists,
380  * then this function updates @aeref to refer to the
381  * entry and returns %0.  Otherwise, -1 is returned.
382  */
avc_lookup(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t requested,struct avc_entry_ref * aeref)383 static int avc_lookup(security_id_t ssid, security_id_t tsid,
384 		      security_class_t tclass,
385 		      access_vector_t requested, struct avc_entry_ref *aeref)
386 {
387 	struct avc_node *node;
388 	int probes, rc = 0;
389 
390 	avc_cache_stats_incr(cav_lookups);
391 	node = avc_search_node(ssid, tsid, tclass, &probes);
392 
393 	if (node && ((node->ae.avd.decided & requested) == requested)) {
394 		avc_cache_stats_incr(cav_hits);
395 		avc_cache_stats_add(cav_probes, probes);
396 		aeref->ae = &node->ae;
397 		goto out;
398 	}
399 
400 	avc_cache_stats_incr(cav_misses);
401 	rc = -1;
402       out:
403 	return rc;
404 }
405 
406 /**
407  * avc_insert - Insert an AVC entry.
408  * @ssid: source security identifier
409  * @tsid: target security identifier
410  * @tclass: target security class
411  * @ae: AVC entry
412  * @aeref:  AVC entry reference
413  *
414  * Insert an AVC entry for the SID pair
415  * (@ssid, @tsid) and class @tclass.
416  * The access vectors and the sequence number are
417  * normally provided by the security server in
418  * response to a security_compute_av() call.  If the
419  * sequence number @ae->avd.seqno is not less than the latest
420  * revocation notification, then the function copies
421  * the access vectors into a cache entry, updates
422  * @aeref to refer to the entry, and returns %0.
423  * Otherwise, this function returns -%1 with @errno set to %EAGAIN.
424  */
avc_insert(security_id_t ssid,security_id_t tsid,security_class_t tclass,struct avc_entry * ae,struct avc_entry_ref * aeref)425 static int avc_insert(security_id_t ssid, security_id_t tsid,
426 		      security_class_t tclass,
427 		      struct avc_entry *ae, struct avc_entry_ref *aeref)
428 {
429 	struct avc_node *node;
430 	int rc = 0;
431 
432 	if (ae->avd.seqno < avc_cache.latest_notif) {
433 		avc_log(SELINUX_WARNING,
434 			"%s:  seqno %d < latest_notif %d\n", avc_prefix,
435 			ae->avd.seqno, avc_cache.latest_notif);
436 		errno = EAGAIN;
437 		rc = -1;
438 		goto out;
439 	}
440 
441 	node = avc_claim_node(ssid, tsid, tclass);
442 	if (!node) {
443 		rc = -1;
444 		goto out;
445 	}
446 
447 	memcpy(&node->ae.avd, &ae->avd, sizeof(ae->avd));
448 	aeref->ae = &node->ae;
449       out:
450 	return rc;
451 }
452 
avc_cleanup(void)453 void avc_cleanup(void)
454 {
455 }
456 
hidden_def(avc_cleanup)457 hidden_def(avc_cleanup)
458 
459 int avc_reset(void)
460 {
461 	struct avc_callback_node *c;
462 	int i, ret, rc = 0, errsave = 0;
463 	struct avc_node *node, *tmp;
464 	errno = 0;
465 
466 	if (!avc_running)
467 		return 0;
468 
469 	avc_get_lock(avc_lock);
470 
471 	for (i = 0; i < AVC_CACHE_SLOTS; i++) {
472 		node = avc_cache.slots[i];
473 		while (node) {
474 			tmp = node;
475 			node = node->next;
476 			avc_clear_avc_entry(&tmp->ae);
477 			tmp->next = avc_node_freelist;
478 			avc_node_freelist = tmp;
479 			avc_cache.active_nodes--;
480 		}
481 		avc_cache.slots[i] = 0;
482 	}
483 	avc_cache.lru_hint = 0;
484 
485 	avc_release_lock(avc_lock);
486 
487 	memset(&cache_stats, 0, sizeof(cache_stats));
488 
489 	for (c = avc_callbacks; c; c = c->next) {
490 		if (c->events & AVC_CALLBACK_RESET) {
491 			ret = c->callback(AVC_CALLBACK_RESET, 0, 0, 0, 0, 0);
492 			if (ret && !rc) {
493 				rc = ret;
494 				errsave = errno;
495 			}
496 		}
497 	}
498 	errno = errsave;
499 	return rc;
500 }
501 
hidden_def(avc_reset)502 hidden_def(avc_reset)
503 
504 void avc_destroy(void)
505 {
506 	struct avc_callback_node *c;
507 	struct avc_node *node, *tmp;
508 	int i;
509 	/* avc_init needs to be called before this function */
510 	assert(avc_running);
511 
512 	avc_get_lock(avc_lock);
513 
514 	if (avc_using_threads)
515 		avc_stop_thread(avc_netlink_thread);
516 	avc_netlink_close();
517 
518 	for (i = 0; i < AVC_CACHE_SLOTS; i++) {
519 		node = avc_cache.slots[i];
520 		while (node) {
521 			tmp = node;
522 			node = node->next;
523 			avc_free(tmp);
524 		}
525 	}
526 	while (avc_node_freelist) {
527 		tmp = avc_node_freelist;
528 		avc_node_freelist = tmp->next;
529 		avc_free(tmp);
530 	}
531 	avc_release_lock(avc_lock);
532 
533 	while (avc_callbacks) {
534 		c = avc_callbacks;
535 		avc_callbacks = c->next;
536 		avc_free(c);
537 	}
538 	sidtab_destroy(&avc_sidtab);
539 	avc_free_lock(avc_lock);
540 	avc_free_lock(avc_log_lock);
541 	avc_free(avc_audit_buf);
542 	avc_running = 0;
543 }
544 
545 /* ratelimit stuff put aside for now --EFW */
546 #if 0
547 /*
548  * Copied from net/core/utils.c:net_ratelimit and modified for
549  * use by the AVC audit facility.
550  */
551 #define AVC_MSG_COST	5*HZ
552 #define AVC_MSG_BURST	10*5*HZ
553 
554 /*
555  * This enforces a rate limit: not more than one kernel message
556  * every 5secs to make a denial-of-service attack impossible.
557  */
558 static int avc_ratelimit(void)
559 {
560 	static unsigned long toks = 10 * 5 * HZ;
561 	static unsigned long last_msg;
562 	static int missed, rc = 0;
563 	unsigned long now = jiffies;
564 	void *ratelimit_lock = avc_alloc_lock();
565 
566 	avc_get_lock(ratelimit_lock);
567 	toks += now - last_msg;
568 	last_msg = now;
569 	if (toks > AVC_MSG_BURST)
570 		toks = AVC_MSG_BURST;
571 	if (toks >= AVC_MSG_COST) {
572 		int lost = missed;
573 		missed = 0;
574 		toks -= AVC_MSG_COST;
575 		avc_release_lock(ratelimit_lock);
576 		if (lost) {
577 			avc_log(SELINUX_WARNING,
578 				"%s:  %d messages suppressed.\n", avc_prefix,
579 				lost);
580 		}
581 		rc = 1;
582 		goto out;
583 	}
584 	missed++;
585 	avc_release_lock(ratelimit_lock);
586       out:
587 	avc_free_lock(ratelimit_lock);
588 	return rc;
589 }
590 
591 static inline int check_avc_ratelimit(void)
592 {
593 	if (avc_enforcing)
594 		return avc_ratelimit();
595 	else {
596 		/* If permissive, then never suppress messages. */
597 		return 1;
598 	}
599 }
600 #endif				/* ratelimit stuff */
601 
602 /**
603  * avc_dump_av - Display an access vector in human-readable form.
604  * @tclass: target security class
605  * @av: access vector
606  */
avc_dump_av(security_class_t tclass,access_vector_t av)607 static void avc_dump_av(security_class_t tclass, access_vector_t av)
608 {
609 	const char *permstr;
610 	access_vector_t bit = 1;
611 
612 	if (av == 0) {
613 		log_append(avc_audit_buf, " null");
614 		return;
615 	}
616 
617 	log_append(avc_audit_buf, " {");
618 
619 	while (av) {
620 		if (av & bit) {
621 			permstr = security_av_perm_to_string(tclass, bit);
622 			if (!permstr)
623 				break;
624 			log_append(avc_audit_buf, " %s", permstr);
625 			av &= ~bit;
626 		}
627 		bit <<= 1;
628 	}
629 
630 	if (av)
631 		log_append(avc_audit_buf, " 0x%x", av);
632 	log_append(avc_audit_buf, " }");
633 }
634 
635 /**
636  * avc_dump_query - Display a SID pair and a class in human-readable form.
637  * @ssid: source security identifier
638  * @tsid: target security identifier
639  * @tclass: target security class
640  */
avc_dump_query(security_id_t ssid,security_id_t tsid,security_class_t tclass)641 static void avc_dump_query(security_id_t ssid, security_id_t tsid,
642 			   security_class_t tclass)
643 {
644 	avc_get_lock(avc_lock);
645 
646 	log_append(avc_audit_buf, "scontext=%s tcontext=%s",
647 		   ssid->ctx, tsid->ctx);
648 
649 	avc_release_lock(avc_lock);
650 	log_append(avc_audit_buf, " tclass=%s",
651 		   security_class_to_string(tclass));
652 }
653 
avc_audit(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t requested,struct av_decision * avd,int result,void * a)654 void avc_audit(security_id_t ssid, security_id_t tsid,
655 	       security_class_t tclass, access_vector_t requested,
656 	       struct av_decision *avd, int result, void *a)
657 {
658 	access_vector_t denied, audited;
659 
660 	denied = requested & ~avd->allowed;
661 	if (denied)
662 		audited = denied & avd->auditdeny;
663 	else if (!requested || result)
664 		audited = denied = requested;
665 	else
666 		audited = requested & avd->auditallow;
667 	if (!audited)
668 		return;
669 #if 0
670 	if (!check_avc_ratelimit())
671 		return;
672 #endif
673 	/* prevent overlapping buffer writes */
674 	avc_get_lock(avc_log_lock);
675 	snprintf(avc_audit_buf, AVC_AUDIT_BUFSIZE,
676 		 "%s:  %s ", avc_prefix, (denied || !requested) ? "denied" : "granted");
677 	avc_dump_av(tclass, audited);
678 	log_append(avc_audit_buf, " for ");
679 
680 	/* get any extra information printed by the callback */
681 	avc_suppl_audit(a, tclass, avc_audit_buf + strlen(avc_audit_buf),
682 			AVC_AUDIT_BUFSIZE - strlen(avc_audit_buf));
683 
684 	log_append(avc_audit_buf, " ");
685 	avc_dump_query(ssid, tsid, tclass);
686 	log_append(avc_audit_buf, "\n");
687 	avc_log(SELINUX_AVC, "%s", avc_audit_buf);
688 
689 	avc_release_lock(avc_log_lock);
690 }
691 
hidden_def(avc_audit)692 hidden_def(avc_audit)
693 
694 
695 static void avd_init(struct av_decision *avd)
696 {
697 	avd->allowed = 0;
698 	avd->auditallow = 0;
699 	avd->auditdeny = 0xffffffff;
700 	avd->seqno = avc_cache.latest_notif;
701 	avd->flags = 0;
702 }
703 
avc_has_perm_noaudit(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t requested,struct avc_entry_ref * aeref,struct av_decision * avd)704 int avc_has_perm_noaudit(security_id_t ssid,
705 			 security_id_t tsid,
706 			 security_class_t tclass,
707 			 access_vector_t requested,
708 			 struct avc_entry_ref *aeref, struct av_decision *avd)
709 {
710 	struct avc_entry *ae;
711 	int rc = 0;
712 	struct avc_entry entry;
713 	access_vector_t denied;
714 	struct avc_entry_ref ref;
715 
716 	if (avd)
717 		avd_init(avd);
718 
719 	if (!avc_using_threads && !avc_app_main_loop) {
720 		(void)avc_netlink_check_nb();
721 	}
722 
723 	if (!aeref) {
724 		avc_entry_ref_init(&ref);
725 		aeref = &ref;
726 	}
727 
728 	avc_get_lock(avc_lock);
729 	avc_cache_stats_incr(entry_lookups);
730 	ae = aeref->ae;
731 	if (ae) {
732 		if (ae->ssid == ssid &&
733 		    ae->tsid == tsid &&
734 		    ae->tclass == tclass &&
735 		    ((ae->avd.decided & requested) == requested)) {
736 			avc_cache_stats_incr(entry_hits);
737 			ae->used = 1;
738 		} else {
739 			avc_cache_stats_incr(entry_discards);
740 			ae = 0;
741 		}
742 	}
743 
744 	if (!ae) {
745 		avc_cache_stats_incr(entry_misses);
746 		rc = avc_lookup(ssid, tsid, tclass, requested, aeref);
747 		if (rc) {
748 			rc = security_compute_av(ssid->ctx, tsid->ctx,
749 						 tclass, requested,
750 						 &entry.avd);
751 			if (rc && errno == EINVAL && !avc_enforcing) {
752 				rc = errno = 0;
753 				goto out;
754 			}
755 			if (rc)
756 				goto out;
757 			rc = avc_insert(ssid, tsid, tclass, &entry, aeref);
758 			if (rc)
759 				goto out;
760 		}
761 		ae = aeref->ae;
762 	}
763 
764 	if (avd)
765 		memcpy(avd, &ae->avd, sizeof(*avd));
766 
767 	denied = requested & ~(ae->avd.allowed);
768 
769 	if (!requested || denied) {
770 		if (!avc_enforcing ||
771 		    (ae->avd.flags & SELINUX_AVD_FLAGS_PERMISSIVE))
772 			ae->avd.allowed |= requested;
773 		else {
774 			errno = EACCES;
775 			rc = -1;
776 		}
777 	}
778 
779       out:
780 	avc_release_lock(avc_lock);
781 	return rc;
782 }
783 
hidden_def(avc_has_perm_noaudit)784 hidden_def(avc_has_perm_noaudit)
785 
786 int avc_has_perm(security_id_t ssid, security_id_t tsid,
787 		 security_class_t tclass, access_vector_t requested,
788 		 struct avc_entry_ref *aeref, void *auditdata)
789 {
790 	struct av_decision avd;
791 	int errsave, rc;
792 
793 	rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, aeref, &avd);
794 	errsave = errno;
795 	avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
796 	errno = errsave;
797 	return rc;
798 }
799 
avc_compute_create(security_id_t ssid,security_id_t tsid,security_class_t tclass,security_id_t * newsid)800 int avc_compute_create(security_id_t ssid,  security_id_t tsid,
801 		       security_class_t tclass, security_id_t *newsid)
802 {
803 	int rc;
804 	struct avc_entry_ref aeref;
805 	struct avc_entry entry;
806 	char * ctx;
807 
808 	*newsid = NULL;
809 	avc_entry_ref_init(&aeref);
810 
811 	avc_get_lock(avc_lock);
812 
813 	/* check for a cached entry */
814 	rc = avc_lookup(ssid, tsid, tclass, 0, &aeref);
815 	if (rc) {
816 		/* need to make a cache entry for this tuple */
817 		rc = security_compute_av(ssid->ctx, tsid->ctx,
818 					 tclass, 0, &entry.avd);
819 		if (rc)
820 			goto out;
821 		rc = avc_insert(ssid, tsid, tclass, &entry, &aeref);
822 		if (rc)
823 			goto out;
824 	}
825 
826 	/* check for a saved compute_create value */
827 	if (!aeref.ae->create_sid) {
828 		/* need to query the kernel policy */
829 		rc = security_compute_create(ssid->ctx, tsid->ctx, tclass,
830 						 &ctx);
831 		if (rc)
832 			goto out;
833 		rc = sidtab_context_to_sid(&avc_sidtab, ctx, newsid);
834 		freecon(ctx);
835 		if (rc)
836 			goto out;
837 
838 		aeref.ae->create_sid = *newsid;
839 	} else {
840 		/* found saved value */
841 		*newsid = aeref.ae->create_sid;
842 	}
843 
844 	rc = 0;
845 out:
846 	avc_release_lock(avc_lock);
847 	return rc;
848 }
849 
avc_add_callback(int (* callback)(uint32_t event,security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,access_vector_t * out_retained),uint32_t events,security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms)850 int avc_add_callback(int (*callback) (uint32_t event, security_id_t ssid,
851 				      security_id_t tsid,
852 				      security_class_t tclass,
853 				      access_vector_t perms,
854 				      access_vector_t * out_retained),
855 		     uint32_t events, security_id_t ssid,
856 		     security_id_t tsid,
857 		     security_class_t tclass, access_vector_t perms)
858 {
859 	struct avc_callback_node *c;
860 	int rc = 0;
861 
862 	c = avc_malloc(sizeof(*c));
863 	if (!c) {
864 		rc = -1;
865 		goto out;
866 	}
867 
868 	c->callback = callback;
869 	c->events = events;
870 	c->ssid = ssid;
871 	c->tsid = tsid;
872 	c->tclass = tclass;
873 	c->perms = perms;
874 	c->next = avc_callbacks;
875 	avc_callbacks = c;
876       out:
877 	return rc;
878 }
879 
avc_sidcmp(security_id_t x,security_id_t y)880 static inline int avc_sidcmp(security_id_t x, security_id_t y)
881 {
882 	return (x == y || x == SECSID_WILD || y == SECSID_WILD);
883 }
884 
avc_update_node(uint32_t event,struct avc_node * node,access_vector_t perms)885 static inline void avc_update_node(uint32_t event, struct avc_node *node,
886 				   access_vector_t perms)
887 {
888 	switch (event) {
889 	case AVC_CALLBACK_GRANT:
890 		node->ae.avd.allowed |= perms;
891 		break;
892 	case AVC_CALLBACK_TRY_REVOKE:
893 	case AVC_CALLBACK_REVOKE:
894 		node->ae.avd.allowed &= ~perms;
895 		break;
896 	case AVC_CALLBACK_AUDITALLOW_ENABLE:
897 		node->ae.avd.auditallow |= perms;
898 		break;
899 	case AVC_CALLBACK_AUDITALLOW_DISABLE:
900 		node->ae.avd.auditallow &= ~perms;
901 		break;
902 	case AVC_CALLBACK_AUDITDENY_ENABLE:
903 		node->ae.avd.auditdeny |= perms;
904 		break;
905 	case AVC_CALLBACK_AUDITDENY_DISABLE:
906 		node->ae.avd.auditdeny &= ~perms;
907 		break;
908 	}
909 }
910 
avc_update_cache(uint32_t event,security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms)911 static int avc_update_cache(uint32_t event, security_id_t ssid,
912 			    security_id_t tsid, security_class_t tclass,
913 			    access_vector_t perms)
914 {
915 	struct avc_node *node;
916 	int i;
917 
918 	avc_get_lock(avc_lock);
919 
920 	if (ssid == SECSID_WILD || tsid == SECSID_WILD) {
921 		/* apply to all matching nodes */
922 		for (i = 0; i < AVC_CACHE_SLOTS; i++) {
923 			for (node = avc_cache.slots[i]; node; node = node->next) {
924 				if (avc_sidcmp(ssid, node->ae.ssid) &&
925 				    avc_sidcmp(tsid, node->ae.tsid) &&
926 				    tclass == node->ae.tclass) {
927 					avc_update_node(event, node, perms);
928 				}
929 			}
930 		}
931 	} else {
932 		/* apply to one node */
933 		node = avc_search_node(ssid, tsid, tclass, 0);
934 		if (node) {
935 			avc_update_node(event, node, perms);
936 		}
937 	}
938 
939 	avc_release_lock(avc_lock);
940 
941 	return 0;
942 }
943 
944 /* avc_control - update cache and call callbacks
945  *
946  * This should not be called directly; use the individual event
947  * functions instead.
948  */
avc_control(uint32_t event,security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,uint32_t seqno,access_vector_t * out_retained)949 static int avc_control(uint32_t event, security_id_t ssid,
950 		       security_id_t tsid, security_class_t tclass,
951 		       access_vector_t perms,
952 		       uint32_t seqno, access_vector_t * out_retained)
953 {
954 	struct avc_callback_node *c;
955 	access_vector_t tretained = 0, cretained = 0;
956 	int ret, rc = 0, errsave = 0;
957 	errno = 0;
958 
959 	/*
960 	 * try_revoke only removes permissions from the cache
961 	 * state if they are not retained by the object manager.
962 	 * Hence, try_revoke must wait until after the callbacks have
963 	 * been invoked to update the cache state.
964 	 */
965 	if (event != AVC_CALLBACK_TRY_REVOKE)
966 		avc_update_cache(event, ssid, tsid, tclass, perms);
967 
968 	for (c = avc_callbacks; c; c = c->next) {
969 		if ((c->events & event) &&
970 		    avc_sidcmp(c->ssid, ssid) &&
971 		    avc_sidcmp(c->tsid, tsid) &&
972 		    c->tclass == tclass && (c->perms & perms)) {
973 			cretained = 0;
974 			ret = c->callback(event, ssid, tsid, tclass,
975 					  (c->perms & perms), &cretained);
976 			if (ret && !rc) {
977 				rc = ret;
978 				errsave = errno;
979 			}
980 			if (!ret)
981 				tretained |= cretained;
982 		}
983 	}
984 
985 	if (event == AVC_CALLBACK_TRY_REVOKE) {
986 		/* revoke any unretained permissions */
987 		perms &= ~tretained;
988 		avc_update_cache(event, ssid, tsid, tclass, perms);
989 		*out_retained = tretained;
990 	}
991 
992 	avc_get_lock(avc_lock);
993 	if (seqno > avc_cache.latest_notif)
994 		avc_cache.latest_notif = seqno;
995 	avc_release_lock(avc_lock);
996 
997 	errno = errsave;
998 	return rc;
999 }
1000 
1001 /**
1002  * avc_ss_grant - Grant previously denied permissions.
1003  * @ssid: source security identifier or %SECSID_WILD
1004  * @tsid: target security identifier or %SECSID_WILD
1005  * @tclass: target security class
1006  * @perms: permissions to grant
1007  * @seqno: policy sequence number
1008  */
avc_ss_grant(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,uint32_t seqno)1009 int avc_ss_grant(security_id_t ssid, security_id_t tsid,
1010 		 security_class_t tclass, access_vector_t perms,
1011 		 uint32_t seqno)
1012 {
1013 	return avc_control(AVC_CALLBACK_GRANT,
1014 			   ssid, tsid, tclass, perms, seqno, 0);
1015 }
1016 
1017 /**
1018  * avc_ss_try_revoke - Try to revoke previously granted permissions.
1019  * @ssid: source security identifier or %SECSID_WILD
1020  * @tsid: target security identifier or %SECSID_WILD
1021  * @tclass: target security class
1022  * @perms: permissions to grant
1023  * @seqno: policy sequence number
1024  * @out_retained: subset of @perms that are retained
1025  *
1026  * Try to revoke previously granted permissions, but
1027  * only if they are not retained as migrated permissions.
1028  * Return the subset of permissions that are retained via @out_retained.
1029  */
avc_ss_try_revoke(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,uint32_t seqno,access_vector_t * out_retained)1030 int avc_ss_try_revoke(security_id_t ssid, security_id_t tsid,
1031 		      security_class_t tclass,
1032 		      access_vector_t perms, uint32_t seqno,
1033 		      access_vector_t * out_retained)
1034 {
1035 	return avc_control(AVC_CALLBACK_TRY_REVOKE,
1036 			   ssid, tsid, tclass, perms, seqno, out_retained);
1037 }
1038 
1039 /**
1040  * avc_ss_revoke - Revoke previously granted permissions.
1041  * @ssid: source security identifier or %SECSID_WILD
1042  * @tsid: target security identifier or %SECSID_WILD
1043  * @tclass: target security class
1044  * @perms: permissions to grant
1045  * @seqno: policy sequence number
1046  *
1047  * Revoke previously granted permissions, even if
1048  * they are retained as migrated permissions.
1049  */
avc_ss_revoke(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,uint32_t seqno)1050 int avc_ss_revoke(security_id_t ssid, security_id_t tsid,
1051 		  security_class_t tclass, access_vector_t perms,
1052 		  uint32_t seqno)
1053 {
1054 	return avc_control(AVC_CALLBACK_REVOKE,
1055 			   ssid, tsid, tclass, perms, seqno, 0);
1056 }
1057 
1058 /**
1059  * avc_ss_reset - Flush the cache and revalidate migrated permissions.
1060  * @seqno: policy sequence number
1061  */
avc_ss_reset(uint32_t seqno)1062 int avc_ss_reset(uint32_t seqno)
1063 {
1064 	int rc;
1065 
1066 	rc = avc_reset();
1067 
1068 	avc_get_lock(avc_lock);
1069 	if (seqno > avc_cache.latest_notif)
1070 		avc_cache.latest_notif = seqno;
1071 	avc_release_lock(avc_lock);
1072 
1073 	return rc;
1074 }
1075 
1076 /**
1077  * avc_ss_set_auditallow - Enable or disable auditing of granted permissions.
1078  * @ssid: source security identifier or %SECSID_WILD
1079  * @tsid: target security identifier or %SECSID_WILD
1080  * @tclass: target security class
1081  * @perms: permissions to grant
1082  * @seqno: policy sequence number
1083  * @enable: enable flag.
1084  */
avc_ss_set_auditallow(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,uint32_t seqno,uint32_t enable)1085 int avc_ss_set_auditallow(security_id_t ssid, security_id_t tsid,
1086 			  security_class_t tclass, access_vector_t perms,
1087 			  uint32_t seqno, uint32_t enable)
1088 {
1089 	if (enable)
1090 		return avc_control(AVC_CALLBACK_AUDITALLOW_ENABLE,
1091 				   ssid, tsid, tclass, perms, seqno, 0);
1092 	else
1093 		return avc_control(AVC_CALLBACK_AUDITALLOW_DISABLE,
1094 				   ssid, tsid, tclass, perms, seqno, 0);
1095 }
1096 
1097 /**
1098  * avc_ss_set_auditdeny - Enable or disable auditing of denied permissions.
1099  * @ssid: source security identifier or %SECSID_WILD
1100  * @tsid: target security identifier or %SECSID_WILD
1101  * @tclass: target security class
1102  * @perms: permissions to grant
1103  * @seqno: policy sequence number
1104  * @enable: enable flag.
1105  */
avc_ss_set_auditdeny(security_id_t ssid,security_id_t tsid,security_class_t tclass,access_vector_t perms,uint32_t seqno,uint32_t enable)1106 int avc_ss_set_auditdeny(security_id_t ssid, security_id_t tsid,
1107 			 security_class_t tclass, access_vector_t perms,
1108 			 uint32_t seqno, uint32_t enable)
1109 {
1110 	if (enable)
1111 		return avc_control(AVC_CALLBACK_AUDITDENY_ENABLE,
1112 				   ssid, tsid, tclass, perms, seqno, 0);
1113 	else
1114 		return avc_control(AVC_CALLBACK_AUDITDENY_DISABLE,
1115 				   ssid, tsid, tclass, perms, seqno, 0);
1116 }
1117