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