2004-11-02 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / bus / selinux.c
1 /* selinux.c  SELinux security checks for D-BUS
2  *
3  * Author: Matthew Rickard <mjricka@epoch.ncsc.mil>
4  *
5  * Licensed under the Academic Free License version 2.1
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 #include <dbus/dbus-internals.h>
23 #include <dbus/dbus-string.h>
24 #include "selinux.h"
25 #include "services.h"
26 #include "policy.h"
27 #include "utils.h"
28 #include "config-parser.h"
29
30 #ifdef HAVE_SELINUX
31 #include <errno.h>
32 #include <pthread.h>
33 #include <syslog.h>
34 #include <selinux/selinux.h>
35 #include <selinux/avc.h>
36 #include <selinux/av_permissions.h>
37 #include <selinux/flask.h>
38 #include <signal.h>
39 #include <stdarg.h>
40 #endif /* HAVE_SELINUX */
41
42 #define BUS_SID_FROM_SELINUX(sid)  ((BusSELinuxID*) (sid))
43 #define SELINUX_SID_FROM_BUS(sid)  ((security_id_t) (sid))
44
45 #ifdef HAVE_SELINUX
46 /* Store the value telling us if SELinux is enabled in the kernel. */
47 static dbus_bool_t selinux_enabled = FALSE;
48
49 /* Store an avc_entry_ref to speed AVC decisions. */
50 static struct avc_entry_ref aeref;
51
52 /* Store the SID of the bus itself to use as the default. */
53 static security_id_t bus_sid = SECSID_WILD;
54
55 /* Thread to listen for SELinux status changes via netlink. */
56 static pthread_t avc_notify_thread;
57
58 /* Prototypes for AVC callback functions.  */
59 static void log_callback (const char *fmt, ...);
60 static void *avc_create_thread (void (*run) (void));
61 static void avc_stop_thread (void *thread);
62 static void *avc_alloc_lock (void);
63 static void avc_get_lock (void *lock);
64 static void avc_release_lock (void *lock);
65 static void avc_free_lock (void *lock);
66
67 /* AVC callback structures for use in avc_init.  */
68 static const struct avc_memory_callback mem_cb =
69 {
70   .func_malloc = dbus_malloc,
71   .func_free = dbus_free
72 };
73 static const struct avc_log_callback log_cb =
74 {
75   .func_log = log_callback,
76   .func_audit = NULL
77 };
78 static const struct avc_thread_callback thread_cb =
79 {
80   .func_create_thread = avc_create_thread,
81   .func_stop_thread = avc_stop_thread
82 };
83 static const struct avc_lock_callback lock_cb =
84 {
85   .func_alloc_lock = avc_alloc_lock,
86   .func_get_lock = avc_get_lock,
87   .func_release_lock = avc_release_lock,
88   .func_free_lock = avc_free_lock
89 };
90 #endif /* HAVE_SELINUX */
91
92 /**
93  * Log callback to log denial messages from the AVC.
94  * This is used in avc_init.  Logs to both standard
95  * error and syslogd.
96  *
97  * @param fmt the format string
98  * @param variable argument list
99  */
100 #ifdef HAVE_SELINUX
101 static void 
102 log_callback (const char *fmt, ...) 
103 {
104   va_list ap;
105   va_start(ap, fmt);
106   vsyslog (LOG_INFO, fmt, ap);
107   va_end(ap);
108 }
109
110 /**
111  * On a policy reload we need to reparse the SELinux configuration file, since
112  * this could have changed.  Send a SIGHUP to reload all configs.
113  */
114 static int
115 policy_reload_callback (u_int32_t event, security_id_t ssid, 
116                         security_id_t tsid, security_class_t tclass, 
117                         access_vector_t perms, access_vector_t *out_retained)
118 {
119   if (event == AVC_CALLBACK_RESET)
120     return raise (SIGHUP);
121   
122   return 0;
123 }
124
125 /**
126  * Create thread to notify the AVC of enforcing and policy reload
127  * changes via netlink.
128  *
129  * @param run the thread run function
130  * @return pointer to the thread
131  */
132 static void *
133 avc_create_thread (void (*run) (void))
134 {
135   int rc;
136
137   rc = pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
138   if (rc != 0)
139     {
140       _dbus_warn ("Failed to start AVC thread: %s\n", _dbus_strerror (rc));
141       exit (1);
142     }
143   return &avc_notify_thread;
144 }
145
146 /* Stop AVC netlink thread.  */
147 static void
148 avc_stop_thread (void *thread)
149 {
150   pthread_cancel (*(pthread_t *) thread);
151 }
152
153 /* Allocate a new AVC lock.  */
154 static void *
155 avc_alloc_lock (void)
156 {
157   pthread_mutex_t *avc_mutex;
158
159   avc_mutex = dbus_new (pthread_mutex_t, 1);
160   if (avc_mutex == NULL)
161     {
162       _dbus_warn ("Could not create mutex: %s\n", _dbus_strerror (errno));
163       exit (1);
164     }
165   pthread_mutex_init (avc_mutex, NULL);
166
167   return avc_mutex;
168 }
169
170 /* Acquire an AVC lock.  */
171 static void
172 avc_get_lock (void *lock)
173 {
174   pthread_mutex_lock (lock);
175 }
176
177 /* Release an AVC lock.  */
178 static void
179 avc_release_lock (void *lock)
180 {
181   pthread_mutex_unlock (lock);
182 }
183
184 /* Free an AVC lock.  */
185 static void
186 avc_free_lock (void *lock)
187 {
188   pthread_mutex_destroy (lock);
189   dbus_free (lock);
190 }
191 #endif /* HAVE_SELINUX */
192
193 /**
194  * Return whether or not SELinux is enabled; must be
195  * called after bus_selinux_init.
196  */
197 dbus_bool_t
198 bus_selinux_enabled (void)
199 {
200 #ifdef HAVE_SELINUX
201   return selinux_enabled;
202 #else
203   return FALSE;
204 #endif /* HAVE_SELINUX */
205 }
206
207 /**
208  * Do early initialization; determine whether SELinux is enabled.
209  */
210 dbus_bool_t
211 bus_selinux_pre_init (void)
212 {
213 #ifdef HAVE_SELINUX
214   int r;
215   char *bus_context;
216
217   _dbus_assert (bus_sid == SECSID_WILD);
218   
219   /* Determine if we are running an SELinux kernel. */
220   r = is_selinux_enabled ();
221   if (r < 0)
222     {
223       _dbus_warn ("Could not tell if SELinux is enabled: %s\n",
224                   _dbus_strerror (errno));
225       return FALSE;
226     }
227
228   selinux_enabled = r != 0;
229   return TRUE;
230 #else
231   return TRUE;
232 #endif
233 }
234
235 /**
236  * Initialize the user space access vector cache (AVC) for D-BUS and set up
237  * logging callbacks.
238  */
239 dbus_bool_t
240 bus_selinux_full_init (void)
241 {
242 #ifdef HAVE_SELINUX
243   int r;
244   char *bus_context;
245
246   _dbus_assert (bus_sid == SECSID_WILD);
247   
248   if (!selinux_enabled)
249     {
250       _dbus_verbose ("SELinux not enabled in this kernel.\n");
251       return TRUE;
252     }
253
254   _dbus_verbose ("SELinux is enabled in this kernel.\n");
255
256   avc_entry_ref_init (&aeref);
257   if (avc_init ("avc", &mem_cb, &log_cb, &thread_cb, &lock_cb) < 0)
258     {
259       _dbus_warn ("Failed to start Access Vector Cache (AVC).\n");
260       return FALSE;
261     }
262   else
263     {
264       openlog ("dbus", LOG_PERROR, LOG_USER);
265       _dbus_verbose ("Access Vector Cache (AVC) started.\n");
266     }
267
268   if (avc_add_callback (policy_reload_callback, AVC_CALLBACK_RESET,
269                        NULL, NULL, 0, 0) < 0)
270     {
271       _dbus_warn ("Failed to add policy reload callback: %s\n",
272                   _dbus_strerror (errno));
273       avc_destroy ();
274       return FALSE;
275     }
276
277   bus_context = NULL;
278   bus_sid = SECSID_WILD;
279
280   if (getcon (&bus_context) < 0)
281     {
282       _dbus_verbose ("Error getting context of bus: %s\n",
283                      _dbus_strerror (errno));
284       return FALSE;
285     }
286       
287   if (avc_context_to_sid (bus_context, &bus_sid) < 0)
288     {
289       _dbus_verbose ("Error getting SID from bus context: %s\n",
290                      _dbus_strerror (errno));
291       freecon (bus_context);
292       return FALSE;
293     }
294
295   freecon (bus_context);
296   
297   return TRUE;
298 #else
299   return TRUE;
300 #endif /* HAVE_SELINUX */
301 }
302
303 /**
304  * Decrement SID reference count.
305  * 
306  * @param sid the SID to decrement
307  */
308 void
309 bus_selinux_id_unref (BusSELinuxID *sid)
310 {
311 #ifdef HAVE_SELINUX
312   if (!selinux_enabled)
313     return;
314
315   _dbus_assert (sid != NULL);
316   
317   sidput (SELINUX_SID_FROM_BUS (sid));
318 #endif /* HAVE_SELINUX */
319 }
320
321 void
322 bus_selinux_id_ref (BusSELinuxID *sid)
323 {
324 #ifdef HAVE_SELINUX
325   if (!selinux_enabled)
326     return;
327
328   _dbus_assert (sid != NULL);
329   
330   sidget (SELINUX_SID_FROM_BUS (sid));
331 #endif /* HAVE_SELINUX */
332 }
333
334 /**
335  * Determine if the SELinux security policy allows the given sender
336  * security context to go to the given recipient security context.
337  * This function determines if the requested permissions are to be
338  * granted from the connection to the message bus or to another
339  * optionally supplied security identifier (e.g. for a service
340  * context).  Currently these permissions are either send_msg or
341  * acquire_svc in the dbus class.
342  *
343  * @param sender_sid source security context
344  * @param override_sid is the target security context.  If SECSID_WILD this will
345  *        use the context of the bus itself (e.g. the default).
346  * @param target_class is the target security class.
347  * @param requested is the requested permissions.
348  * @returns #TRUE if security policy allows the send.
349  */
350 #ifdef HAVE_SELINUX
351 static dbus_bool_t
352 bus_selinux_check (BusSELinuxID        *sender_sid,
353                    BusSELinuxID        *override_sid,
354                    security_class_t     target_class,
355                    access_vector_t      requested)
356 {
357   if (!selinux_enabled)
358     return TRUE;
359
360   /* Make the security check.  AVC checks enforcing mode here as well. */
361   if (avc_has_perm (SELINUX_SID_FROM_BUS (sender_sid),
362                     override_sid ?
363                     SELINUX_SID_FROM_BUS (override_sid) :
364                     SELINUX_SID_FROM_BUS (bus_sid), 
365                     target_class, requested, &aeref, NULL) < 0)
366     {
367       _dbus_verbose ("SELinux denying due to security policy.\n");
368       return FALSE;
369     }
370   else
371     return TRUE;
372 }
373 #endif /* HAVE_SELINUX */
374
375 /**
376  * Returns true if the given connection can acquire a service,
377  * assuming the given security ID is needed for that service.
378  *
379  * @param connection connection that wants to own the service
380  * @param service_sid the SID of the service from the table
381  * @returns #TRUE if acquire is permitted.
382  */
383 dbus_bool_t
384 bus_selinux_allows_acquire_service (DBusConnection     *connection,
385                                     BusSELinuxID       *service_sid)
386 {
387 #ifdef HAVE_SELINUX
388   BusSELinuxID *connection_sid;
389   
390   if (!selinux_enabled)
391     return TRUE;
392
393   connection_sid = bus_connection_get_selinux_id (connection);
394   
395   return bus_selinux_check (connection_sid,
396                             service_sid,
397                             SECCLASS_DBUS,
398                             DBUS__ACQUIRE_SVC);
399 #else
400   return TRUE;
401 #endif /* HAVE_SELINUX */
402 }
403
404 /**
405  * Check if SELinux security controls allow the message to be sent to a
406  * particular connection based on the security context of the sender and
407  * that of the receiver. The destination connection need not be the
408  * addressed recipient, it could be an "eavesdropper"
409  *
410  * @param sender the sender of the message.
411  * @param proposed_recipient the connection the message is to be sent to.
412  * @returns whether to allow the send
413  */
414 dbus_bool_t
415 bus_selinux_allows_send (DBusConnection     *sender,
416                          DBusConnection     *proposed_recipient)
417 {
418 #ifdef HAVE_SELINUX
419   BusSELinuxID *recipient_sid;
420   BusSELinuxID *sender_sid;
421
422   if (!selinux_enabled)
423     return TRUE;
424
425   sender_sid = bus_connection_get_selinux_id (sender);
426   /* A NULL proposed_recipient means the bus itself. */
427   if (proposed_recipient)
428     recipient_sid = bus_connection_get_selinux_id (proposed_recipient);
429   else
430     recipient_sid = BUS_SID_FROM_SELINUX (bus_sid);
431
432   return bus_selinux_check (sender_sid, recipient_sid,
433                             SECCLASS_DBUS, DBUS__SEND_MSG);
434 #else
435   return TRUE;
436 #endif /* HAVE_SELINUX */
437 }
438
439 /**
440  * Gets the security context of a connection to the bus. It is up to
441  * the caller to freecon() when they are done. 
442  *
443  * @param connection the connection to get the context of.
444  * @param con the location to store the security context.
445  * @returns #TRUE if context is successfully obtained.
446  */
447 #ifdef HAVE_SELINUX
448 static dbus_bool_t
449 bus_connection_read_selinux_context (DBusConnection     *connection,
450                                      char              **con)
451 {
452   int fd;
453
454   if (!selinux_enabled)
455     return FALSE;
456
457   _dbus_assert (connection != NULL);
458   
459   if (!dbus_connection_get_unix_fd (connection, &fd))
460     {
461       _dbus_verbose ("Failed to get file descriptor of socket.\n");
462       return FALSE;
463     }
464   
465   if (getpeercon (fd, con) < 0)
466     {
467       _dbus_verbose ("Error getting context of socket peer: %s\n",
468                      _dbus_strerror (errno));
469       return FALSE;
470     }
471   
472   _dbus_verbose ("Successfully read connection context.\n");
473   return TRUE;
474 }
475 #endif /* HAVE_SELINUX */
476
477 /**
478  * Read the SELinux ID from the connection.
479  *
480  * @param connection the connection to read from
481  * @returns the SID if successfully determined, #NULL otherwise.
482  */
483 BusSELinuxID*
484 bus_selinux_init_connection_id (DBusConnection *connection,
485                                 DBusError      *error)
486 {
487 #ifdef HAVE_SELINUX
488   char *con;
489   security_id_t sid;
490   
491   if (!selinux_enabled)
492     return NULL;
493
494   if (!bus_connection_read_selinux_context (connection, &con))
495     {
496       dbus_set_error (error, DBUS_ERROR_FAILED,
497                       "Failed to read an SELinux context from connection");
498       _dbus_verbose ("Error getting peer context.\n");
499       return NULL;
500     }
501
502   _dbus_verbose ("Converting context to SID to store on connection\n");
503
504   if (avc_context_to_sid (con, &sid) < 0)
505     {
506       if (errno == ENOMEM)
507         BUS_SET_OOM (error);
508       else
509         dbus_set_error (error, DBUS_ERROR_FAILED,
510                         "Error getting SID from context: %s\n",
511                         _dbus_strerror (errno));
512       
513       _dbus_warn ("Error getting SID from context: %s\n",
514                   _dbus_strerror (errno));
515       
516       freecon (con);
517       return NULL;
518     }
519  
520   freecon (con); 
521   return BUS_SID_FROM_SELINUX (sid);
522 #else
523   return NULL;
524 #endif /* HAVE_SELINUX */
525 }
526
527
528 /**
529  * Function for freeing hash table data.  These SIDs
530  * should no longer be referenced.
531  */
532 static void
533 bus_selinux_id_table_free_value (BusSELinuxID *sid)
534 {
535 #ifdef HAVE_SELINUX
536   /* NULL sometimes due to how DBusHashTable works */
537   if (sid)
538     bus_selinux_id_unref (sid);
539 #endif /* HAVE_SELINUX */
540 }
541
542 /**
543  * Creates a new table mapping service names to security ID.
544  * A security ID is a "compiled" security context, a security
545  * context is just a string.
546  *
547  * @returns the new table or #NULL if no memory
548  */
549 DBusHashTable*
550 bus_selinux_id_table_new (void)
551 {
552   return _dbus_hash_table_new (DBUS_HASH_STRING,
553                                (DBusFreeFunction) dbus_free,
554                                (DBusFreeFunction) bus_selinux_id_table_free_value);
555 }
556
557 /** 
558  * Hashes a service name and service context into the service SID
559  * table as a string and a SID.
560  *
561  * @param service_name is the name of the service.
562  * @param service_context is the context of the service.
563  * @param service_table is the table to hash them into.
564  * @return #FALSE if not enough memory
565  */
566 dbus_bool_t
567 bus_selinux_id_table_insert (DBusHashTable *service_table,
568                              const char    *service_name,
569                              const char    *service_context)
570 {
571 #ifdef HAVE_SELINUX
572   dbus_bool_t retval;
573   security_id_t sid;
574   char *key;
575
576   if (!selinux_enabled)
577     return TRUE;
578
579   sid = SECSID_WILD;
580   retval = FALSE;
581
582   key = _dbus_strdup (service_name);
583   if (key == NULL)
584     return retval;
585   
586   if (avc_context_to_sid ((char *) service_context, &sid) < 0)
587     {
588       _dbus_assert (errno == ENOMEM);
589       goto out;
590     }
591
592   if (!_dbus_hash_table_insert_string (service_table,
593                                        key,
594                                        BUS_SID_FROM_SELINUX (sid)))
595     goto out;
596
597   _dbus_verbose ("Parsed \tservice: %s \n\t\tcontext: %s\n",
598                   key, 
599                   sid->ctx);
600
601   /* These are owned by the hash, so clear them to avoid unref */
602   key = NULL;
603   sid = SECSID_WILD;
604
605   retval = TRUE;
606   
607  out:
608   if (sid != SECSID_WILD)
609     sidput (sid);
610
611   if (key)
612     dbus_free (key);
613
614   return retval;
615 #else
616   return TRUE;
617 #endif /* HAVE_SELINUX */
618 }
619
620
621 /**
622  * Find the security identifier associated with a particular service
623  * name.  Return a pointer to this SID, or #NULL/SECSID_WILD if the
624  * service is not found in the hash table.  This should be nearly a
625  * constant time operation.  If SELinux support is not available,
626  * always return NULL.
627  *
628  * @param service_table the hash table to check for service name.
629  * @param service_name the name of the service to look for.
630  * @returns the SELinux ID associated with the service
631  */
632 BusSELinuxID*
633 bus_selinux_id_table_lookup (DBusHashTable    *service_table,
634                              const DBusString *service_name)
635 {
636 #ifdef HAVE_SELINUX
637   security_id_t sid;
638
639   sid = SECSID_WILD;     /* default context */
640
641   if (!selinux_enabled)
642     return NULL;
643   
644   _dbus_verbose ("Looking up service SID for %s\n",
645                  _dbus_string_get_const_data (service_name));
646
647   sid = _dbus_hash_table_lookup_string (service_table,
648                                         _dbus_string_get_const_data (service_name));
649
650   if (sid == SECSID_WILD)
651     _dbus_verbose ("Service %s not found\n", 
652                    _dbus_string_get_const_data (service_name));
653   else
654     _dbus_verbose ("Service %s found\n", 
655                    _dbus_string_get_const_data (service_name));
656
657   return BUS_SID_FROM_SELINUX (sid);
658 #endif /* HAVE_SELINUX */
659   return NULL;
660 }
661
662 /**
663  * Copy security ID table mapping from one table into another.
664  *
665  * @param dest the table to copy into
666  * @param override the table to copy from
667  * @returns #FALSE if out of memory
668  */
669 #ifdef HAVE_SELINUX
670 static dbus_bool_t
671 bus_selinux_id_table_copy_over (DBusHashTable    *dest,
672                                 DBusHashTable    *override)
673 {
674   const char *key;
675   char *key_copy;
676   BusSELinuxID *sid;
677   DBusHashIter iter;
678   
679   _dbus_hash_iter_init (override, &iter);
680   while (_dbus_hash_iter_next (&iter))
681     {
682       key = _dbus_hash_iter_get_string_key (&iter);
683       sid = _dbus_hash_iter_get_value (&iter);
684
685       key_copy = _dbus_strdup (key);
686       if (key_copy == NULL)
687         return FALSE;
688
689       if (!_dbus_hash_table_insert_string (dest,
690                                            key_copy,
691                                            sid))
692         {
693           dbus_free (key_copy);
694           return FALSE;
695         }
696
697       bus_selinux_id_ref (sid);
698     }
699
700   return TRUE;
701 }
702 #endif /* HAVE_SELINUX */
703
704 /**
705  * Creates the union of the two tables (each table maps a service
706  * name to a security ID). In case of the same service name in
707  * both tables, the security ID from "override" will be used.
708  *
709  * @param base the base table
710  * @param override the table that takes precedence in the merge
711  * @returns the new table, or #NULL if out of memory
712  */
713 DBusHashTable*
714 bus_selinux_id_table_union (DBusHashTable    *base,
715                             DBusHashTable    *override)
716 {
717   DBusHashTable *combined_table;
718
719   combined_table = bus_selinux_id_table_new ();
720
721   if (combined_table == NULL)
722     return NULL;
723   
724 #ifdef HAVE_SELINUX 
725   if (!selinux_enabled)
726     return combined_table;
727
728   if (!bus_selinux_id_table_copy_over (combined_table, base))
729     {
730       _dbus_hash_table_unref (combined_table);
731       return NULL;
732     }
733
734   if (!bus_selinux_id_table_copy_over (combined_table, override))
735     {
736       _dbus_hash_table_unref (combined_table);
737       return NULL;
738     }
739 #endif /* HAVE_SELINUX */
740   
741   return combined_table;
742 }
743
744 /**
745  * Get the SELinux policy root.  This is used to find the D-BUS
746  * specific config file within the policy.
747  */
748 const char *
749 bus_selinux_get_policy_root (void)
750 {
751 #ifdef HAVE_SELINUX
752   return selinux_policy_root ();
753 #else
754   return NULL;
755 #endif /* HAVE_SELINUX */
756
757
758 /**
759  * For debugging:  Print out the current hash table of service SIDs.
760  */
761 void
762 bus_selinux_id_table_print (DBusHashTable *service_table)
763 {
764 #ifdef DBUS_ENABLE_VERBOSE_MODE
765 #ifdef HAVE_SELINUX
766   DBusHashIter iter;
767
768   if (!selinux_enabled)
769     return;
770   
771   _dbus_verbose ("Service SID Table:\n");
772   _dbus_hash_iter_init (service_table, &iter);
773   while (_dbus_hash_iter_next (&iter))
774     {
775       const char *key = _dbus_hash_iter_get_string_key (&iter);
776       security_id_t sid = _dbus_hash_iter_get_value (&iter);
777       _dbus_verbose ("The key is %s\n", key);
778       _dbus_verbose ("The context is %s\n", sid->ctx);
779       _dbus_verbose ("The refcount is %d\n", sid->refcnt);
780     }
781 #endif /* HAVE_SELINUX */
782 #endif /* DBUS_ENABLE_VERBOSE_MODE */
783 }
784
785
786 #ifdef DBUS_ENABLE_VERBOSE_MODE
787 #ifdef HAVE_SELINUX
788 /**
789  * Print out some AVC statistics.
790  */
791 static void
792 bus_avc_print_stats (void)
793 {
794   struct avc_cache_stats cstats;
795
796   if (!selinux_enabled)
797     return;
798   
799   _dbus_verbose ("AVC Statistics:\n");
800   avc_cache_stats (&cstats);
801   avc_av_stats ();
802   _dbus_verbose ("AVC Cache Statistics:\n");
803   _dbus_verbose ("Entry lookups: %d\n", cstats.entry_lookups);
804   _dbus_verbose ("Entry hits: %d\n", cstats.entry_hits);
805   _dbus_verbose ("Entry misses %d\n", cstats.entry_misses);
806   _dbus_verbose ("Entry discards: %d\n", cstats.entry_discards);
807   _dbus_verbose ("CAV lookups: %d\n", cstats.cav_lookups);
808   _dbus_verbose ("CAV hits: %d\n", cstats.cav_hits);
809   _dbus_verbose ("CAV probes: %d\n", cstats.cav_probes);
810   _dbus_verbose ("CAV misses: %d\n", cstats.cav_misses);
811 }
812 #endif /* HAVE_SELINUX */
813 #endif /* DBUS_ENABLE_VERBOSE_MODE */
814
815
816 /**
817  * Destroy the AVC before we terminate.
818  */
819 void
820 bus_selinux_shutdown (void)
821 {
822 #ifdef HAVE_SELINUX
823   if (!selinux_enabled)
824     return;
825
826   sidput (bus_sid);
827   bus_sid = SECSID_WILD;
828   
829 #ifdef DBUS_ENABLE_VERBOSE_MODE
830   bus_avc_print_stats ();
831 #endif /* DBUS_ENABLE_VERBOSE_MODE */
832
833   avc_destroy ();
834 #endif /* HAVE_SELINUX */
835 }
836