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