1 /* -*- mode: C; c-file-style: "gnu" -*-
2 * selinux.c SELinux security checks for D-Bus
4 * Author: Matthew Rickard <mjricka@epoch.ncsc.mil>
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <dbus/dbus-internals.h>
24 #include <dbus/dbus-string.h>
29 #include "config-parser.h"
35 #include <selinux/selinux.h>
36 #include <selinux/avc.h>
37 #include <selinux/av_permissions.h>
38 #include <selinux/flask.h>
41 #endif /* HAVE_SELINUX */
43 #define BUS_SID_FROM_SELINUX(sid) ((BusSELinuxID*) (sid))
44 #define SELINUX_SID_FROM_BUS(sid) ((security_id_t) (sid))
47 /* Store the value telling us if SELinux is enabled in the kernel. */
48 static dbus_bool_t selinux_enabled = FALSE;
50 /* Store an avc_entry_ref to speed AVC decisions. */
51 static struct avc_entry_ref aeref;
53 /* Store the SID of the bus itself to use as the default. */
54 static security_id_t bus_sid = SECSID_WILD;
56 /* Thread to listen for SELinux status changes via netlink. */
57 static pthread_t avc_notify_thread;
59 /* Prototypes for AVC callback functions. */
60 static void log_callback (const char *fmt, ...);
61 static void log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft);
62 static void *avc_create_thread (void (*run) (void));
63 static void avc_stop_thread (void *thread);
64 static void *avc_alloc_lock (void);
65 static void avc_get_lock (void *lock);
66 static void avc_release_lock (void *lock);
67 static void avc_free_lock (void *lock);
69 /* AVC callback structures for use in avc_init. */
70 static const struct avc_memory_callback mem_cb =
72 .func_malloc = dbus_malloc,
73 .func_free = dbus_free
75 static const struct avc_log_callback log_cb =
77 .func_log = log_callback,
78 .func_audit = log_audit_callback
80 static const struct avc_thread_callback thread_cb =
82 .func_create_thread = avc_create_thread,
83 .func_stop_thread = avc_stop_thread
85 static const struct avc_lock_callback lock_cb =
87 .func_alloc_lock = avc_alloc_lock,
88 .func_get_lock = avc_get_lock,
89 .func_release_lock = avc_release_lock,
90 .func_free_lock = avc_free_lock
92 #endif /* HAVE_SELINUX */
95 * Log callback to log denial messages from the AVC.
96 * This is used in avc_init. Logs to both standard
99 * @param fmt the format string
100 * @param variable argument list
104 log_callback (const char *fmt, ...)
108 vsyslog (LOG_INFO, fmt, ap);
113 * On a policy reload we need to reparse the SELinux configuration file, since
114 * this could have changed. Send a SIGHUP to reload all configs.
117 policy_reload_callback (u_int32_t event, security_id_t ssid,
118 security_id_t tsid, security_class_t tclass,
119 access_vector_t perms, access_vector_t *out_retained)
121 if (event == AVC_CALLBACK_RESET)
122 return raise (SIGHUP);
128 * Log any auxiliary data
131 log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft)
133 DBusString *audmsg = data;
134 _dbus_string_copy_to_buffer (audmsg, buf, bufleft);
138 * Create thread to notify the AVC of enforcing and policy reload
139 * changes via netlink.
141 * @param run the thread run function
142 * @return pointer to the thread
145 avc_create_thread (void (*run) (void))
149 rc = pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
152 _dbus_warn ("Failed to start AVC thread: %s\n", _dbus_strerror (rc));
155 return &avc_notify_thread;
158 /* Stop AVC netlink thread. */
160 avc_stop_thread (void *thread)
162 pthread_cancel (*(pthread_t *) thread);
165 /* Allocate a new AVC lock. */
167 avc_alloc_lock (void)
169 pthread_mutex_t *avc_mutex;
171 avc_mutex = dbus_new (pthread_mutex_t, 1);
172 if (avc_mutex == NULL)
174 _dbus_warn ("Could not create mutex: %s\n", _dbus_strerror (errno));
177 pthread_mutex_init (avc_mutex, NULL);
182 /* Acquire an AVC lock. */
184 avc_get_lock (void *lock)
186 pthread_mutex_lock (lock);
189 /* Release an AVC lock. */
191 avc_release_lock (void *lock)
193 pthread_mutex_unlock (lock);
196 /* Free an AVC lock. */
198 avc_free_lock (void *lock)
200 pthread_mutex_destroy (lock);
203 #endif /* HAVE_SELINUX */
206 * Return whether or not SELinux is enabled; must be
207 * called after bus_selinux_init.
210 bus_selinux_enabled (void)
213 return selinux_enabled;
216 #endif /* HAVE_SELINUX */
220 * Do early initialization; determine whether SELinux is enabled.
223 bus_selinux_pre_init (void)
227 _dbus_assert (bus_sid == SECSID_WILD);
229 /* Determine if we are running an SELinux kernel. */
230 r = is_selinux_enabled ();
233 _dbus_warn ("Could not tell if SELinux is enabled: %s\n",
234 _dbus_strerror (errno));
238 selinux_enabled = r != 0;
246 * Initialize the user space access vector cache (AVC) for D-Bus and set up
250 bus_selinux_full_init (void)
255 _dbus_assert (bus_sid == SECSID_WILD);
257 if (!selinux_enabled)
259 _dbus_verbose ("SELinux not enabled in this kernel.\n");
263 _dbus_verbose ("SELinux is enabled in this kernel.\n");
265 avc_entry_ref_init (&aeref);
266 if (avc_init ("avc", &mem_cb, &log_cb, &thread_cb, &lock_cb) < 0)
268 _dbus_warn ("Failed to start Access Vector Cache (AVC).\n");
273 openlog ("dbus", LOG_PERROR, LOG_USER);
274 _dbus_verbose ("Access Vector Cache (AVC) started.\n");
277 if (avc_add_callback (policy_reload_callback, AVC_CALLBACK_RESET,
278 NULL, NULL, 0, 0) < 0)
280 _dbus_warn ("Failed to add policy reload callback: %s\n",
281 _dbus_strerror (errno));
287 bus_sid = SECSID_WILD;
289 if (getcon (&bus_context) < 0)
291 _dbus_verbose ("Error getting context of bus: %s\n",
292 _dbus_strerror (errno));
296 if (avc_context_to_sid (bus_context, &bus_sid) < 0)
298 _dbus_verbose ("Error getting SID from bus context: %s\n",
299 _dbus_strerror (errno));
300 freecon (bus_context);
304 freecon (bus_context);
309 #endif /* HAVE_SELINUX */
313 * Decrement SID reference count.
315 * @param sid the SID to decrement
318 bus_selinux_id_unref (BusSELinuxID *sid)
321 if (!selinux_enabled)
324 _dbus_assert (sid != NULL);
326 sidput (SELINUX_SID_FROM_BUS (sid));
327 #endif /* HAVE_SELINUX */
331 bus_selinux_id_ref (BusSELinuxID *sid)
334 if (!selinux_enabled)
337 _dbus_assert (sid != NULL);
339 sidget (SELINUX_SID_FROM_BUS (sid));
340 #endif /* HAVE_SELINUX */
344 * Determine if the SELinux security policy allows the given sender
345 * security context to go to the given recipient security context.
346 * This function determines if the requested permissions are to be
347 * granted from the connection to the message bus or to another
348 * optionally supplied security identifier (e.g. for a service
349 * context). Currently these permissions are either send_msg or
350 * acquire_svc in the dbus class.
352 * @param sender_sid source security context
353 * @param override_sid is the target security context. If SECSID_WILD this will
354 * use the context of the bus itself (e.g. the default).
355 * @param target_class is the target security class.
356 * @param requested is the requested permissions.
357 * @returns #TRUE if security policy allows the send.
361 bus_selinux_check (BusSELinuxID *sender_sid,
362 BusSELinuxID *override_sid,
363 security_class_t target_class,
364 access_vector_t requested,
367 if (!selinux_enabled)
370 /* Make the security check. AVC checks enforcing mode here as well. */
371 if (avc_has_perm (SELINUX_SID_FROM_BUS (sender_sid),
373 SELINUX_SID_FROM_BUS (override_sid) :
374 SELINUX_SID_FROM_BUS (bus_sid),
375 target_class, requested, &aeref, auxdata) < 0)
377 _dbus_verbose ("SELinux denying due to security policy.\n");
383 #endif /* HAVE_SELINUX */
386 * Returns true if the given connection can acquire a service,
387 * assuming the given security ID is needed for that service.
389 * @param connection connection that wants to own the service
390 * @param service_sid the SID of the service from the table
391 * @returns #TRUE if acquire is permitted.
394 bus_selinux_allows_acquire_service (DBusConnection *connection,
395 BusSELinuxID *service_sid,
396 const char *service_name,
400 BusSELinuxID *connection_sid;
405 if (!selinux_enabled)
408 connection_sid = bus_connection_get_selinux_id (connection);
409 if (!dbus_connection_get_unix_process_id (connection, &spid))
412 if (!_dbus_string_init (&auxdata))
415 if (!_dbus_string_append (&auxdata, "service="))
418 if (!_dbus_string_append (&auxdata, service_name))
423 if (!_dbus_string_append (&auxdata, " spid="))
426 if (!_dbus_string_append_uint (&auxdata, spid))
430 ret = bus_selinux_check (connection_sid,
436 _dbus_string_free (&auxdata);
440 _dbus_string_free (&auxdata);
446 #endif /* HAVE_SELINUX */
450 * Check if SELinux security controls allow the message to be sent to a
451 * particular connection based on the security context of the sender and
452 * that of the receiver. The destination connection need not be the
453 * addressed recipient, it could be an "eavesdropper"
455 * @param sender the sender of the message.
456 * @param proposed_recipient the connection the message is to be sent to.
457 * @returns whether to allow the send
460 bus_selinux_allows_send (DBusConnection *sender,
461 DBusConnection *proposed_recipient,
463 const char *interface,
465 const char *error_name,
466 const char *destination,
470 BusSELinuxID *recipient_sid;
471 BusSELinuxID *sender_sid;
472 unsigned long spid, tpid;
475 dbus_bool_t string_alloced;
477 if (!selinux_enabled)
480 if (!sender || !dbus_connection_get_unix_process_id (sender, &spid))
482 if (!proposed_recipient || !dbus_connection_get_unix_process_id (proposed_recipient, &tpid))
485 string_alloced = FALSE;
486 if (!_dbus_string_init (&auxdata))
488 string_alloced = TRUE;
490 if (!_dbus_string_append (&auxdata, "msgtype="))
493 if (!_dbus_string_append (&auxdata, msgtype))
498 if (!_dbus_string_append (&auxdata, " interface="))
500 if (!_dbus_string_append (&auxdata, interface))
506 if (!_dbus_string_append (&auxdata, " member="))
508 if (!_dbus_string_append (&auxdata, member))
514 if (!_dbus_string_append (&auxdata, " error_name="))
516 if (!_dbus_string_append (&auxdata, error_name))
522 if (!_dbus_string_append (&auxdata, " dest="))
524 if (!_dbus_string_append (&auxdata, destination))
530 if (!_dbus_string_append (&auxdata, " spid="))
533 if (!_dbus_string_append_uint (&auxdata, spid))
539 if (!_dbus_string_append (&auxdata, " tpid="))
542 if (!_dbus_string_append_uint (&auxdata, tpid))
546 sender_sid = bus_connection_get_selinux_id (sender);
547 /* A NULL proposed_recipient means the bus itself. */
548 if (proposed_recipient)
549 recipient_sid = bus_connection_get_selinux_id (proposed_recipient);
551 recipient_sid = BUS_SID_FROM_SELINUX (bus_sid);
553 ret = bus_selinux_check (sender_sid,
559 _dbus_string_free (&auxdata);
565 _dbus_string_free (&auxdata);
571 #endif /* HAVE_SELINUX */
575 bus_selinux_append_context (DBusMessage *message,
582 if (avc_sid_to_context (SELINUX_SID_FROM_BUS (sid), &context) < 0)
587 dbus_set_error (error, DBUS_ERROR_FAILED,
588 "Error getting context from SID: %s\n",
589 _dbus_strerror (errno));
592 if (!dbus_message_append_args (message,
599 _DBUS_SET_OOM (error);
610 * Gets the security context of a connection to the bus. It is up to
611 * the caller to freecon() when they are done.
613 * @param connection the connection to get the context of.
614 * @param con the location to store the security context.
615 * @returns #TRUE if context is successfully obtained.
619 bus_connection_read_selinux_context (DBusConnection *connection,
624 if (!selinux_enabled)
627 _dbus_assert (connection != NULL);
629 if (!dbus_connection_get_unix_fd (connection, &fd))
631 _dbus_verbose ("Failed to get file descriptor of socket.\n");
635 if (getpeercon (fd, con) < 0)
637 _dbus_verbose ("Error getting context of socket peer: %s\n",
638 _dbus_strerror (errno));
642 _dbus_verbose ("Successfully read connection context.\n");
645 #endif /* HAVE_SELINUX */
648 * Read the SELinux ID from the connection.
650 * @param connection the connection to read from
651 * @returns the SID if successfully determined, #NULL otherwise.
654 bus_selinux_init_connection_id (DBusConnection *connection,
661 if (!selinux_enabled)
664 if (!bus_connection_read_selinux_context (connection, &con))
666 dbus_set_error (error, DBUS_ERROR_FAILED,
667 "Failed to read an SELinux context from connection");
668 _dbus_verbose ("Error getting peer context.\n");
672 _dbus_verbose ("Converting context to SID to store on connection\n");
674 if (avc_context_to_sid (con, &sid) < 0)
679 dbus_set_error (error, DBUS_ERROR_FAILED,
680 "Error getting SID from context \"%s\": %s\n",
681 con, _dbus_strerror (errno));
683 _dbus_warn ("Error getting SID from context \"%s\": %s\n",
684 con, _dbus_strerror (errno));
691 return BUS_SID_FROM_SELINUX (sid);
694 #endif /* HAVE_SELINUX */
699 * Function for freeing hash table data. These SIDs
700 * should no longer be referenced.
703 bus_selinux_id_table_free_value (BusSELinuxID *sid)
706 /* NULL sometimes due to how DBusHashTable works */
708 bus_selinux_id_unref (sid);
709 #endif /* HAVE_SELINUX */
713 * Creates a new table mapping service names to security ID.
714 * A security ID is a "compiled" security context, a security
715 * context is just a string.
717 * @returns the new table or #NULL if no memory
720 bus_selinux_id_table_new (void)
722 return _dbus_hash_table_new (DBUS_HASH_STRING,
723 (DBusFreeFunction) dbus_free,
724 (DBusFreeFunction) bus_selinux_id_table_free_value);
728 * Hashes a service name and service context into the service SID
729 * table as a string and a SID.
731 * @param service_name is the name of the service.
732 * @param service_context is the context of the service.
733 * @param service_table is the table to hash them into.
734 * @return #FALSE if not enough memory
737 bus_selinux_id_table_insert (DBusHashTable *service_table,
738 const char *service_name,
739 const char *service_context)
746 if (!selinux_enabled)
752 key = _dbus_strdup (service_name);
756 if (avc_context_to_sid ((char *) service_context, &sid) < 0)
764 _dbus_warn ("Error getting SID from context \"%s\": %s\n",
765 (char *) service_context,
766 _dbus_strerror (errno));
770 if (!_dbus_hash_table_insert_string (service_table,
772 BUS_SID_FROM_SELINUX (sid)))
775 _dbus_verbose ("Parsed \tservice: %s \n\t\tcontext: %s\n",
779 /* These are owned by the hash, so clear them to avoid unref */
786 if (sid != SECSID_WILD)
795 #endif /* HAVE_SELINUX */
800 * Find the security identifier associated with a particular service
801 * name. Return a pointer to this SID, or #NULL/SECSID_WILD if the
802 * service is not found in the hash table. This should be nearly a
803 * constant time operation. If SELinux support is not available,
804 * always return NULL.
806 * @param service_table the hash table to check for service name.
807 * @param service_name the name of the service to look for.
808 * @returns the SELinux ID associated with the service
811 bus_selinux_id_table_lookup (DBusHashTable *service_table,
812 const DBusString *service_name)
817 sid = SECSID_WILD; /* default context */
819 if (!selinux_enabled)
822 _dbus_verbose ("Looking up service SID for %s\n",
823 _dbus_string_get_const_data (service_name));
825 sid = _dbus_hash_table_lookup_string (service_table,
826 _dbus_string_get_const_data (service_name));
828 if (sid == SECSID_WILD)
829 _dbus_verbose ("Service %s not found\n",
830 _dbus_string_get_const_data (service_name));
832 _dbus_verbose ("Service %s found\n",
833 _dbus_string_get_const_data (service_name));
835 return BUS_SID_FROM_SELINUX (sid);
836 #endif /* HAVE_SELINUX */
841 * Get the SELinux policy root. This is used to find the D-Bus
842 * specific config file within the policy.
845 bus_selinux_get_policy_root (void)
848 return selinux_policy_root ();
851 #endif /* HAVE_SELINUX */
855 * For debugging: Print out the current hash table of service SIDs.
858 bus_selinux_id_table_print (DBusHashTable *service_table)
860 #ifdef DBUS_ENABLE_VERBOSE_MODE
864 if (!selinux_enabled)
867 _dbus_verbose ("Service SID Table:\n");
868 _dbus_hash_iter_init (service_table, &iter);
869 while (_dbus_hash_iter_next (&iter))
871 const char *key = _dbus_hash_iter_get_string_key (&iter);
872 security_id_t sid = _dbus_hash_iter_get_value (&iter);
873 _dbus_verbose ("The key is %s\n", key);
874 _dbus_verbose ("The context is %s\n", sid->ctx);
875 _dbus_verbose ("The refcount is %d\n", sid->refcnt);
877 #endif /* HAVE_SELINUX */
878 #endif /* DBUS_ENABLE_VERBOSE_MODE */
882 #ifdef DBUS_ENABLE_VERBOSE_MODE
885 * Print out some AVC statistics.
888 bus_avc_print_stats (void)
890 struct avc_cache_stats cstats;
892 if (!selinux_enabled)
895 _dbus_verbose ("AVC Statistics:\n");
896 avc_cache_stats (&cstats);
898 _dbus_verbose ("AVC Cache Statistics:\n");
899 _dbus_verbose ("Entry lookups: %d\n", cstats.entry_lookups);
900 _dbus_verbose ("Entry hits: %d\n", cstats.entry_hits);
901 _dbus_verbose ("Entry misses %d\n", cstats.entry_misses);
902 _dbus_verbose ("Entry discards: %d\n", cstats.entry_discards);
903 _dbus_verbose ("CAV lookups: %d\n", cstats.cav_lookups);
904 _dbus_verbose ("CAV hits: %d\n", cstats.cav_hits);
905 _dbus_verbose ("CAV probes: %d\n", cstats.cav_probes);
906 _dbus_verbose ("CAV misses: %d\n", cstats.cav_misses);
908 #endif /* HAVE_SELINUX */
909 #endif /* DBUS_ENABLE_VERBOSE_MODE */
913 * Destroy the AVC before we terminate.
916 bus_selinux_shutdown (void)
919 if (!selinux_enabled)
922 _dbus_verbose ("AVC shutdown\n");
924 if (bus_sid != SECSID_WILD)
927 bus_sid = SECSID_WILD;
929 #ifdef DBUS_ENABLE_VERBOSE_MODE
930 bus_avc_print_stats ();
931 #endif /* DBUS_ENABLE_VERBOSE_MODE */
935 #endif /* HAVE_SELINUX */