2004-10-18 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  * Initialize the user space access vector cache (AVC) for D-BUS and set up
209  * logging callbacks.
210  */
211 dbus_bool_t
212 bus_selinux_init (void)
213 {
214 #ifdef HAVE_SELINUX
215   int r;
216   char *bus_context;
217
218   _dbus_assert (bus_sid == SECSID_WILD);
219   
220   /* Determine if we are running an SELinux kernel. */
221   r = is_selinux_enabled ();
222   if (r < 0)
223     {
224       _dbus_warn ("Could not tell if SELinux is enabled: %s\n",
225                   _dbus_strerror (errno));
226       return FALSE;
227     }
228
229   selinux_enabled = r != 0;
230
231   if (!selinux_enabled)
232     {
233       _dbus_verbose ("SELinux not enabled in this kernel.\n");
234       return TRUE;
235     }
236
237   _dbus_verbose ("SELinux is enabled in this kernel.\n");
238
239   avc_entry_ref_init (&aeref);
240   if (avc_init ("avc", &mem_cb, &log_cb, &thread_cb, &lock_cb) < 0)
241     {
242       _dbus_warn ("Failed to start Access Vector Cache (AVC).\n");
243       return FALSE;
244     }
245   else
246     {
247       openlog ("dbus", LOG_PERROR, LOG_USER);
248       _dbus_verbose ("Access Vector Cache (AVC) started.\n");
249     }
250
251   if (avc_add_callback (policy_reload_callback, AVC_CALLBACK_RESET,
252                        NULL, NULL, 0, 0) < 0)
253     {
254       _dbus_warn ("Failed to add policy reload callback: %s\n",
255                   _dbus_strerror (errno));
256       avc_destroy ();
257       return FALSE;
258     }
259
260   bus_context = NULL;
261   bus_sid = SECSID_WILD;
262
263   if (getcon (&bus_context) < 0)
264     {
265       _dbus_verbose ("Error getting context of bus: %s\n",
266                      _dbus_strerror (errno));
267       return FALSE;
268     }
269       
270   if (avc_context_to_sid (bus_context, &bus_sid) < 0)
271     {
272       _dbus_verbose ("Error getting SID from bus context: %s\n",
273                      _dbus_strerror (errno));
274       freecon (bus_context);
275       return FALSE;
276     }
277
278   freecon (bus_context);
279   
280   return TRUE;
281 #else
282   return TRUE;
283 #endif /* HAVE_SELINUX */
284 }
285
286 /**
287  * Decrement SID reference count.
288  * 
289  * @param sid the SID to decrement
290  */
291 void
292 bus_selinux_id_unref (BusSELinuxID *sid)
293 {
294 #ifdef HAVE_SELINUX
295   if (!selinux_enabled)
296     return;
297
298   _dbus_assert (sid != NULL);
299   
300   sidput (SELINUX_SID_FROM_BUS (sid));
301 #endif /* HAVE_SELINUX */
302 }
303
304 void
305 bus_selinux_id_ref (BusSELinuxID *sid)
306 {
307 #ifdef HAVE_SELINUX
308   if (!selinux_enabled)
309     return;
310
311   _dbus_assert (sid != NULL);
312   
313   sidget (SELINUX_SID_FROM_BUS (sid));
314 #endif /* HAVE_SELINUX */
315 }
316
317 /**
318  * Determine if the SELinux security policy allows the given sender
319  * security context to go to the given recipient security context.
320  * This function determines if the requested permissions are to be
321  * granted from the connection to the message bus or to another
322  * optionally supplied security identifier (e.g. for a service
323  * context).  Currently these permissions are either send_msg or
324  * acquire_svc in the dbus class.
325  *
326  * @param sender_sid source security context
327  * @param override_sid is the target security context.  If SECSID_WILD this will
328  *        use the context of the bus itself (e.g. the default).
329  * @param target_class is the target security class.
330  * @param requested is the requested permissions.
331  * @returns #TRUE if security policy allows the send.
332  */
333 #ifdef HAVE_SELINUX
334 static dbus_bool_t
335 bus_selinux_check (BusSELinuxID        *sender_sid,
336                    BusSELinuxID        *override_sid,
337                    security_class_t     target_class,
338                    access_vector_t      requested)
339 {
340   if (!selinux_enabled)
341     return TRUE;
342
343   /* Make the security check.  AVC checks enforcing mode here as well. */
344   if (avc_has_perm (SELINUX_SID_FROM_BUS (sender_sid),
345                     override_sid ?
346                     SELINUX_SID_FROM_BUS (override_sid) :
347                     SELINUX_SID_FROM_BUS (bus_sid), 
348                     target_class, requested, &aeref, NULL) < 0)
349     {
350       _dbus_verbose ("SELinux denying due to security policy.\n");
351       return FALSE;
352     }
353   else
354     return TRUE;
355 }
356 #endif /* HAVE_SELINUX */
357
358 /**
359  * Returns true if the given connection can acquire a service,
360  * assuming the given security ID is needed for that service.
361  *
362  * @param connection connection that wants to own the service
363  * @param service_sid the SID of the service from the table
364  * @returns #TRUE if acquire is permitted.
365  */
366 dbus_bool_t
367 bus_selinux_allows_acquire_service (DBusConnection     *connection,
368                                     BusSELinuxID       *service_sid)
369 {
370 #ifdef HAVE_SELINUX
371   BusSELinuxID *connection_sid;
372   
373   if (!selinux_enabled)
374     return TRUE;
375
376   connection_sid = bus_connection_get_selinux_id (connection);
377   
378   return bus_selinux_check (connection_sid,
379                             service_sid,
380                             SECCLASS_DBUS,
381                             DBUS__ACQUIRE_SVC);
382 #else
383   return TRUE;
384 #endif /* HAVE_SELINUX */
385 }
386
387 /**
388  * Check if SELinux security controls allow the message to be sent to a
389  * particular connection based on the security context of the sender and
390  * that of the receiver. The destination connection need not be the
391  * addressed recipient, it could be an "eavesdropper"
392  *
393  * @param sender the sender of the message.
394  * @param proposed_recipient the connection the message is to be sent to.
395  * @returns whether to allow the send
396  */
397 dbus_bool_t
398 bus_selinux_allows_send (DBusConnection     *sender,
399                          DBusConnection     *proposed_recipient)
400 {
401 #ifdef HAVE_SELINUX
402   BusSELinuxID *recipient_sid;
403   BusSELinuxID *sender_sid;
404
405   if (!selinux_enabled)
406     return TRUE;
407
408   sender_sid = bus_connection_get_selinux_id (sender);
409   /* A NULL proposed_recipient means the bus itself. */
410   if (proposed_recipient)
411     recipient_sid = bus_connection_get_selinux_id (proposed_recipient);
412   else
413     recipient_sid = BUS_SID_FROM_SELINUX (bus_sid);
414
415   return bus_selinux_check (sender_sid, recipient_sid,
416                             SECCLASS_DBUS, DBUS__SEND_MSG);
417 #else
418   return TRUE;
419 #endif /* HAVE_SELINUX */
420 }
421
422 /**
423  * Gets the security context of a connection to the bus. It is up to
424  * the caller to freecon() when they are done. 
425  *
426  * @param connection the connection to get the context of.
427  * @param con the location to store the security context.
428  * @returns #TRUE if context is successfully obtained.
429  */
430 #ifdef HAVE_SELINUX
431 static dbus_bool_t
432 bus_connection_read_selinux_context (DBusConnection     *connection,
433                                      char              **con)
434 {
435   int fd;
436
437   if (!selinux_enabled)
438     return FALSE;
439
440   _dbus_assert (connection != NULL);
441   
442   if (!dbus_connection_get_unix_fd (connection, &fd))
443     {
444       _dbus_verbose ("Failed to get file descriptor of socket.\n");
445       return FALSE;
446     }
447   
448   if (getpeercon (fd, con) < 0)
449     {
450       _dbus_verbose ("Error getting context of socket peer: %s\n",
451                      _dbus_strerror (errno));
452       return FALSE;
453     }
454   
455   _dbus_verbose ("Successfully read connection context.\n");
456   return TRUE;
457 }
458 #endif /* HAVE_SELINUX */
459
460 /**
461  * Read the SELinux ID from the connection.
462  *
463  * @param connection the connection to read from
464  * @returns the SID if successfully determined, #NULL otherwise.
465  */
466 BusSELinuxID*
467 bus_selinux_init_connection_id (DBusConnection *connection,
468                                 DBusError      *error)
469 {
470 #ifdef HAVE_SELINUX
471   char *con;
472   security_id_t sid;
473   
474   if (!selinux_enabled)
475     return NULL;
476
477   if (!bus_connection_read_selinux_context (connection, &con))
478     {
479       dbus_set_error (error, DBUS_ERROR_FAILED,
480                       "Failed to read an SELinux context from connection");
481       _dbus_verbose ("Error getting peer context.\n");
482       return NULL;
483     }
484
485   _dbus_verbose ("Converting context to SID to store on connection\n");
486
487   if (avc_context_to_sid (con, &sid) < 0)
488     {
489       if (errno == ENOMEM)
490         BUS_SET_OOM (error);
491       else
492         dbus_set_error (error, DBUS_ERROR_FAILED,
493                         "Error getting SID from context: %s\n",
494                         _dbus_strerror (errno));
495       
496       _dbus_warn ("Error getting SID from context: %s\n",
497                   _dbus_strerror (errno));
498       
499       freecon (con);
500       return NULL;
501     }
502  
503   freecon (con); 
504   return BUS_SID_FROM_SELINUX (sid);
505 #else
506   return NULL;
507 #endif /* HAVE_SELINUX */
508 }
509
510
511 /**
512  * Function for freeing hash table data.  These SIDs
513  * should no longer be referenced.
514  */
515 static void
516 bus_selinux_id_table_free_value (BusSELinuxID *sid)
517 {
518 #ifdef HAVE_SELINUX
519   /* NULL sometimes due to how DBusHashTable works */
520   if (sid)
521     bus_selinux_id_unref (sid);
522 #endif /* HAVE_SELINUX */
523 }
524
525 /**
526  * Creates a new table mapping service names to security ID.
527  * A security ID is a "compiled" security context, a security
528  * context is just a string.
529  *
530  * @returns the new table or #NULL if no memory
531  */
532 DBusHashTable*
533 bus_selinux_id_table_new (void)
534 {
535   return _dbus_hash_table_new (DBUS_HASH_STRING,
536                                (DBusFreeFunction) dbus_free,
537                                (DBusFreeFunction) bus_selinux_id_table_free_value);
538 }
539
540 /** 
541  * Hashes a service name and service context into the service SID
542  * table as a string and a SID.
543  *
544  * @param service_name is the name of the service.
545  * @param service_context is the context of the service.
546  * @param service_table is the table to hash them into.
547  * @return #FALSE if not enough memory
548  */
549 dbus_bool_t
550 bus_selinux_id_table_insert (DBusHashTable *service_table,
551                              const char    *service_name,
552                              const char    *service_context)
553 {
554 #ifdef HAVE_SELINUX
555   dbus_bool_t retval;
556   security_id_t sid;
557   char *key;
558
559   if (!selinux_enabled)
560     return TRUE;
561
562   sid = SECSID_WILD;
563   retval = FALSE;
564
565   key = _dbus_strdup (service_name);
566   if (key == NULL)
567     return retval;
568   
569   if (avc_context_to_sid ((char *) service_context, &sid) < 0)
570     {
571       _dbus_assert (errno == ENOMEM);
572       goto out;
573     }
574
575   if (!_dbus_hash_table_insert_string (service_table,
576                                        key,
577                                        BUS_SID_FROM_SELINUX (sid)))
578     goto out;
579
580   _dbus_verbose ("Parsed \tservice: %s \n\t\tcontext: %s\n",
581                   key, 
582                   sid->ctx);
583
584   /* These are owned by the hash, so clear them to avoid unref */
585   key = NULL;
586   sid = SECSID_WILD;
587
588   retval = TRUE;
589   
590  out:
591   if (sid != SECSID_WILD)
592     sidput (sid);
593
594   if (key)
595     dbus_free (key);
596
597   return retval;
598 #else
599   return TRUE;
600 #endif /* HAVE_SELINUX */
601 }
602
603
604 /**
605  * Find the security identifier associated with a particular service
606  * name.  Return a pointer to this SID, or #NULL/SECSID_WILD if the
607  * service is not found in the hash table.  This should be nearly a
608  * constant time operation.  If SELinux support is not available,
609  * always return NULL.
610  *
611  * @param service_table the hash table to check for service name.
612  * @param service_name the name of the service to look for.
613  * @returns the SELinux ID associated with the service
614  */
615 BusSELinuxID*
616 bus_selinux_id_table_lookup (DBusHashTable    *service_table,
617                              const DBusString *service_name)
618 {
619 #ifdef HAVE_SELINUX
620   security_id_t sid;
621
622   sid = SECSID_WILD;     /* default context */
623
624   if (!selinux_enabled)
625     return NULL;
626   
627   _dbus_verbose ("Looking up service SID for %s\n",
628                  _dbus_string_get_const_data (service_name));
629
630   sid = _dbus_hash_table_lookup_string (service_table,
631                                         _dbus_string_get_const_data (service_name));
632
633   if (sid == SECSID_WILD)
634     _dbus_verbose ("Service %s not found\n", 
635                    _dbus_string_get_const_data (service_name));
636   else
637     _dbus_verbose ("Service %s found\n", 
638                    _dbus_string_get_const_data (service_name));
639
640   return BUS_SID_FROM_SELINUX (sid);
641 #endif /* HAVE_SELINUX */
642   return NULL;
643 }
644
645 /**
646  * Copy security ID table mapping from one table into another.
647  *
648  * @param dest the table to copy into
649  * @param override the table to copy from
650  * @returns #FALSE if out of memory
651  */
652 #ifdef HAVE_SELINUX
653 static dbus_bool_t
654 bus_selinux_id_table_copy_over (DBusHashTable    *dest,
655                                 DBusHashTable    *override)
656 {
657   const char *key;
658   char *key_copy;
659   BusSELinuxID *sid;
660   DBusHashIter iter;
661   
662   _dbus_hash_iter_init (override, &iter);
663   while (_dbus_hash_iter_next (&iter))
664     {
665       key = _dbus_hash_iter_get_string_key (&iter);
666       sid = _dbus_hash_iter_get_value (&iter);
667
668       key_copy = _dbus_strdup (key);
669       if (key_copy == NULL)
670         return FALSE;
671
672       if (!_dbus_hash_table_insert_string (dest,
673                                            key_copy,
674                                            sid))
675         {
676           dbus_free (key_copy);
677           return FALSE;
678         }
679
680       bus_selinux_id_ref (sid);
681     }
682
683   return TRUE;
684 }
685 #endif /* HAVE_SELINUX */
686
687 /**
688  * Creates the union of the two tables (each table maps a service
689  * name to a security ID). In case of the same service name in
690  * both tables, the security ID from "override" will be used.
691  *
692  * @param base the base table
693  * @param override the table that takes precedence in the merge
694  * @returns the new table, or #NULL if out of memory
695  */
696 DBusHashTable*
697 bus_selinux_id_table_union (DBusHashTable    *base,
698                             DBusHashTable    *override)
699 {
700   DBusHashTable *combined_table;
701
702   combined_table = bus_selinux_id_table_new ();
703
704   if (combined_table == NULL)
705     return NULL;
706   
707 #ifdef HAVE_SELINUX 
708   if (!selinux_enabled)
709     return combined_table;
710
711   if (!bus_selinux_id_table_copy_over (combined_table, base))
712     {
713       _dbus_hash_table_unref (combined_table);
714       return NULL;
715     }
716
717   if (!bus_selinux_id_table_copy_over (combined_table, override))
718     {
719       _dbus_hash_table_unref (combined_table);
720       return NULL;
721     }
722 #endif /* HAVE_SELINUX */
723   
724   return combined_table;
725 }
726
727 /**
728  * Get the SELinux policy root.  This is used to find the D-BUS
729  * specific config file within the policy.
730  */
731 const char *
732 bus_selinux_get_policy_root (void)
733 {
734 #ifdef HAVE_SELINUX
735   return selinux_policy_root ();
736 #else
737   return NULL;
738 #endif /* HAVE_SELINUX */
739
740
741 /**
742  * For debugging:  Print out the current hash table of service SIDs.
743  */
744 void
745 bus_selinux_id_table_print (DBusHashTable *service_table)
746 {
747 #ifdef DBUS_ENABLE_VERBOSE_MODE
748 #ifdef HAVE_SELINUX
749   DBusHashIter iter;
750
751   if (!selinux_enabled)
752     return;
753   
754   _dbus_verbose ("Service SID Table:\n");
755   _dbus_hash_iter_init (service_table, &iter);
756   while (_dbus_hash_iter_next (&iter))
757     {
758       const char *key = _dbus_hash_iter_get_string_key (&iter);
759       security_id_t sid = _dbus_hash_iter_get_value (&iter);
760       _dbus_verbose ("The key is %s\n", key);
761       _dbus_verbose ("The context is %s\n", sid->ctx);
762       _dbus_verbose ("The refcount is %d\n", sid->refcnt);
763     }
764 #endif /* HAVE_SELINUX */
765 #endif /* DBUS_ENABLE_VERBOSE_MODE */
766 }
767
768
769 #ifdef DBUS_ENABLE_VERBOSE_MODE
770 #ifdef HAVE_SELINUX
771 /**
772  * Print out some AVC statistics.
773  */
774 static void
775 bus_avc_print_stats (void)
776 {
777   struct avc_cache_stats cstats;
778
779   if (!selinux_enabled)
780     return;
781   
782   _dbus_verbose ("AVC Statistics:\n");
783   avc_cache_stats (&cstats);
784   avc_av_stats ();
785   _dbus_verbose ("AVC Cache Statistics:\n");
786   _dbus_verbose ("Entry lookups: %d\n", cstats.entry_lookups);
787   _dbus_verbose ("Entry hits: %d\n", cstats.entry_hits);
788   _dbus_verbose ("Entry misses %d\n", cstats.entry_misses);
789   _dbus_verbose ("Entry discards: %d\n", cstats.entry_discards);
790   _dbus_verbose ("CAV lookups: %d\n", cstats.cav_lookups);
791   _dbus_verbose ("CAV hits: %d\n", cstats.cav_hits);
792   _dbus_verbose ("CAV probes: %d\n", cstats.cav_probes);
793   _dbus_verbose ("CAV misses: %d\n", cstats.cav_misses);
794 }
795 #endif /* HAVE_SELINUX */
796 #endif /* DBUS_ENABLE_VERBOSE_MODE */
797
798
799 /**
800  * Destroy the AVC before we terminate.
801  */
802 void
803 bus_selinux_shutdown (void)
804 {
805 #ifdef HAVE_SELINUX
806   if (!selinux_enabled)
807     return;
808
809   sidput (bus_sid);
810   bus_sid = SECSID_WILD;
811   
812 #ifdef DBUS_ENABLE_VERBOSE_MODE
813   bus_avc_print_stats ();
814 #endif /* DBUS_ENABLE_VERBOSE_MODE */
815
816   avc_destroy ();
817 #endif /* HAVE_SELINUX */
818 }
819