1 /* selinux.c SELinux security checks for D-BUS
3 * Author: Matthew Rickard <mjricka@epoch.ncsc.mil>
5 * Licensed under the Academic Free License version 2.1
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.
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.
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
22 #include <dbus/dbus-internals.h>
23 #include <dbus/dbus-string.h>
28 #include "config-parser.h"
34 #include <selinux/selinux.h>
35 #include <selinux/avc.h>
36 #include <selinux/av_permissions.h>
37 #include <selinux/flask.h>
40 #endif /* HAVE_SELINUX */
42 #define BUS_SID_FROM_SELINUX(sid) ((BusSELinuxID*) (sid))
43 #define SELINUX_SID_FROM_BUS(sid) ((security_id_t) (sid))
46 /* Store the value telling us if SELinux is enabled in the kernel. */
47 static dbus_bool_t selinux_enabled = FALSE;
49 /* Store an avc_entry_ref to speed AVC decisions. */
50 static struct avc_entry_ref aeref;
52 /* Store the SID of the bus itself to use as the default. */
53 static security_id_t bus_sid = SECSID_WILD;
55 /* Thread to listen for SELinux status changes via netlink. */
56 static pthread_t avc_notify_thread;
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);
67 /* AVC callback structures for use in avc_init. */
68 static const struct avc_memory_callback mem_cb =
70 .func_malloc = dbus_malloc,
71 .func_free = dbus_free
73 static const struct avc_log_callback log_cb =
75 .func_log = log_callback,
78 static const struct avc_thread_callback thread_cb =
80 .func_create_thread = avc_create_thread,
81 .func_stop_thread = avc_stop_thread
83 static const struct avc_lock_callback lock_cb =
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
90 #endif /* HAVE_SELINUX */
93 * Log callback to log denial messages from the AVC.
94 * This is used in avc_init. Logs to both standard
97 * @param fmt the format string
98 * @param variable argument list
102 log_callback (const char *fmt, ...)
106 vsyslog (LOG_INFO, fmt, ap);
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.
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)
119 if (event == AVC_CALLBACK_RESET)
120 return raise (SIGHUP);
126 * Create thread to notify the AVC of enforcing and policy reload
127 * changes via netlink.
129 * @param run the thread run function
130 * @return pointer to the thread
133 avc_create_thread (void (*run) (void))
137 rc = pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
140 _dbus_warn ("Failed to start AVC thread: %s\n", _dbus_strerror (rc));
143 return &avc_notify_thread;
146 /* Stop AVC netlink thread. */
148 avc_stop_thread (void *thread)
150 pthread_cancel (*(pthread_t *) thread);
153 /* Allocate a new AVC lock. */
155 avc_alloc_lock (void)
157 pthread_mutex_t *avc_mutex;
159 avc_mutex = dbus_new (pthread_mutex_t, 1);
160 if (avc_mutex == NULL)
162 _dbus_warn ("Could not create mutex: %s\n", _dbus_strerror (errno));
165 pthread_mutex_init (avc_mutex, NULL);
170 /* Acquire an AVC lock. */
172 avc_get_lock (void *lock)
174 pthread_mutex_lock (lock);
177 /* Release an AVC lock. */
179 avc_release_lock (void *lock)
181 pthread_mutex_unlock (lock);
184 /* Free an AVC lock. */
186 avc_free_lock (void *lock)
188 pthread_mutex_destroy (lock);
191 #endif /* HAVE_SELINUX */
194 * Return whether or not SELinux is enabled; must be
195 * called after bus_selinux_init.
198 bus_selinux_enabled (void)
200 return selinux_enabled;
204 * Initialize the user space access vector cache (AVC) for D-BUS and set up
208 bus_selinux_init (void)
214 _dbus_assert (bus_sid == SECSID_WILD);
216 /* Determine if we are running an SELinux kernel. */
217 r = is_selinux_enabled ();
220 _dbus_warn ("Could not tell if SELinux is enabled: %s\n",
221 _dbus_strerror (errno));
225 selinux_enabled = r != 0;
227 if (!selinux_enabled)
229 _dbus_verbose ("SELinux not enabled in this kernel.\n");
233 _dbus_verbose ("SELinux is enabled in this kernel.\n");
235 avc_entry_ref_init (&aeref);
236 if (avc_init ("avc", &mem_cb, &log_cb, &thread_cb, &lock_cb) < 0)
238 _dbus_warn ("Failed to start Access Vector Cache (AVC).\n");
243 openlog ("dbus", LOG_PERROR, LOG_USER);
244 _dbus_verbose ("Access Vector Cache (AVC) started.\n");
247 if (avc_add_callback (policy_reload_callback, AVC_CALLBACK_RESET,
248 NULL, NULL, 0, 0) < 0)
250 _dbus_warn ("Failed to add policy reload callback: %s\n",
251 _dbus_strerror (errno));
257 bus_sid = SECSID_WILD;
259 if (getcon (&bus_context) < 0)
261 _dbus_verbose ("Error getting context of bus: %s\n",
262 _dbus_strerror (errno));
266 if (avc_context_to_sid (bus_context, &bus_sid) < 0)
268 _dbus_verbose ("Error getting SID from bus context: %s\n",
269 _dbus_strerror (errno));
270 freecon (bus_context);
274 freecon (bus_context);
279 #endif /* HAVE_SELINUX */
283 * Decrement SID reference count.
285 * @param sid the SID to decrement
288 bus_selinux_id_unref (BusSELinuxID *sid)
291 if (!selinux_enabled)
294 _dbus_assert (sid != NULL);
296 sidput (SELINUX_SID_FROM_BUS (sid));
297 #endif /* HAVE_SELINUX */
301 bus_selinux_id_ref (BusSELinuxID *sid)
304 if (!selinux_enabled)
307 _dbus_assert (sid != NULL);
309 sidget (SELINUX_SID_FROM_BUS (sid));
310 #endif /* HAVE_SELINUX */
314 * Determine if the SELinux security policy allows the given sender
315 * security context to go to the given recipient security context.
316 * This function determines if the requested permissions are to be
317 * granted from the connection to the message bus or to another
318 * optionally supplied security identifier (e.g. for a service
319 * context). Currently these permissions are either send_msg or
320 * acquire_svc in the dbus class.
322 * @param sender_sid source security context
323 * @param override_sid is the target security context. If SECSID_WILD this will
324 * use the context of the bus itself (e.g. the default).
325 * @param target_class is the target security class.
326 * @param requested is the requested permissions.
327 * @returns #TRUE if security policy allows the send.
331 bus_selinux_check (BusSELinuxID *sender_sid,
332 BusSELinuxID *override_sid,
333 security_class_t target_class,
334 access_vector_t requested)
336 if (!selinux_enabled)
339 /* Make the security check. AVC checks enforcing mode here as well. */
340 if (avc_has_perm (SELINUX_SID_FROM_BUS (sender_sid),
342 SELINUX_SID_FROM_BUS (override_sid) :
343 SELINUX_SID_FROM_BUS (bus_sid),
344 target_class, requested, &aeref, NULL) < 0)
346 _dbus_verbose ("SELinux denying due to security policy.\n");
352 #endif /* HAVE_SELINUX */
355 * Returns true if the given connection can acquire a service,
356 * assuming the given security ID is needed for that service.
358 * @param connection connection that wants to own the service
359 * @param service_sid the SID of the service from the table
360 * @returns #TRUE if acquire is permitted.
363 bus_selinux_allows_acquire_service (DBusConnection *connection,
364 BusSELinuxID *service_sid)
367 BusSELinuxID *connection_sid;
369 if (!selinux_enabled)
372 connection_sid = bus_connection_get_selinux_id (connection);
374 return bus_selinux_check (connection_sid,
380 #endif /* HAVE_SELINUX */
384 * Check if SELinux security controls allow the message to be sent to a
385 * particular connection based on the security context of the sender and
386 * that of the receiver. The destination connection need not be the
387 * addressed recipient, it could be an "eavesdropper"
389 * @param sender the sender of the message.
390 * @param proposed_recipient the connection the message is to be sent to.
391 * @returns whether to allow the send
394 bus_selinux_allows_send (DBusConnection *sender,
395 DBusConnection *proposed_recipient)
398 BusSELinuxID *recipient_sid;
399 BusSELinuxID *sender_sid;
401 if (!selinux_enabled)
404 sender_sid = bus_connection_get_selinux_id (sender);
405 /* A NULL proposed_recipient means the bus itself. */
406 if (proposed_recipient)
407 recipient_sid = bus_connection_get_selinux_id (proposed_recipient);
409 recipient_sid = BUS_SID_FROM_SELINUX (bus_sid);
411 return bus_selinux_check (sender_sid, recipient_sid,
412 SECCLASS_DBUS, DBUS__SEND_MSG);
415 #endif /* HAVE_SELINUX */
419 * Gets the security context of a connection to the bus. It is up to
420 * the caller to freecon() when they are done.
422 * @param connection the connection to get the context of.
423 * @param con the location to store the security context.
424 * @returns #TRUE if context is successfully obtained.
428 bus_connection_read_selinux_context (DBusConnection *connection,
433 if (!selinux_enabled)
436 _dbus_assert (connection != NULL);
438 if (!dbus_connection_get_unix_fd (connection, &fd))
440 _dbus_verbose ("Failed to get file descriptor of socket.\n");
444 if (getpeercon (fd, con) < 0)
446 _dbus_verbose ("Error getting context of socket peer: %s\n",
447 _dbus_strerror (errno));
451 _dbus_verbose ("Successfully read connection context.\n");
454 #endif /* HAVE_SELINUX */
457 * Read the SELinux ID from the connection.
459 * @param connection the connection to read from
460 * @returns the SID if successfully determined, #NULL otherwise.
463 bus_selinux_init_connection_id (DBusConnection *connection,
470 if (!selinux_enabled)
473 if (!bus_connection_read_selinux_context (connection, &con))
475 dbus_set_error (error, DBUS_ERROR_FAILED,
476 "Failed to read an SELinux context from connection");
477 _dbus_verbose ("Error getting peer context.\n");
481 _dbus_verbose ("Converting context to SID to store on connection\n");
483 if (avc_context_to_sid (con, &sid) < 0)
488 dbus_set_error (error, DBUS_ERROR_FAILED,
489 "Error getting SID from context: %s\n",
490 _dbus_strerror (errno));
492 _dbus_warn ("Error getting SID from context: %s\n",
493 _dbus_strerror (errno));
500 return BUS_SID_FROM_SELINUX (sid);
503 #endif /* HAVE_SELINUX */
508 * Function for freeing hash table data. These SIDs
509 * should no longer be referenced.
512 bus_selinux_id_table_free_value (BusSELinuxID *sid)
515 /* NULL sometimes due to how DBusHashTable works */
517 bus_selinux_id_unref (sid);
518 #endif /* HAVE_SELINUX */
522 * Creates a new table mapping service names to security ID.
523 * A security ID is a "compiled" security context, a security
524 * context is just a string.
526 * @returns the new table or #NULL if no memory
529 bus_selinux_id_table_new (void)
531 return _dbus_hash_table_new (DBUS_HASH_STRING,
532 (DBusFreeFunction) dbus_free,
533 (DBusFreeFunction) bus_selinux_id_table_free_value);
537 * Hashes a service name and service context into the service SID
538 * table as a string and a SID.
540 * @param service_name is the name of the service.
541 * @param service_context is the context of the service.
542 * @param service_table is the table to hash them into.
543 * @return #FALSE if not enough memory
546 bus_selinux_id_table_insert (DBusHashTable *service_table,
547 const char *service_name,
548 const char *service_context)
555 if (!selinux_enabled)
561 key = _dbus_strdup (service_name);
565 if (avc_context_to_sid ((char *) service_context, &sid) < 0)
567 _dbus_assert (errno == ENOMEM);
571 if (!_dbus_hash_table_insert_string (service_table,
573 BUS_SID_FROM_SELINUX (sid)))
576 _dbus_verbose ("Parsed \tservice: %s \n\t\tcontext: %s\n",
580 /* These are owned by the hash, so clear them to avoid unref */
587 if (sid != SECSID_WILD)
596 #endif /* HAVE_SELINUX */
601 * Find the security identifier associated with a particular service
602 * name. Return a pointer to this SID, or #NULL/SECSID_WILD if the
603 * service is not found in the hash table. This should be nearly a
604 * constant time operation. If SELinux support is not available,
605 * always return NULL.
607 * @param service_table the hash table to check for service name.
608 * @param service_name the name of the service to look for.
609 * @returns the SELinux ID associated with the service
612 bus_selinux_id_table_lookup (DBusHashTable *service_table,
613 const DBusString *service_name)
618 sid = SECSID_WILD; /* default context */
620 if (!selinux_enabled)
623 _dbus_verbose ("Looking up service SID for %s\n",
624 _dbus_string_get_const_data (service_name));
626 sid = _dbus_hash_table_lookup_string (service_table,
627 _dbus_string_get_const_data (service_name));
629 if (sid == SECSID_WILD)
630 _dbus_verbose ("Service %s not found\n",
631 _dbus_string_get_const_data (service_name));
633 _dbus_verbose ("Service %s found\n",
634 _dbus_string_get_const_data (service_name));
636 return BUS_SID_FROM_SELINUX (sid);
637 #endif /* HAVE_SELINUX */
642 * Copy security ID table mapping from one table into another.
644 * @param dest the table to copy into
645 * @param override the table to copy from
646 * @returns #FALSE if out of memory
650 bus_selinux_id_table_copy_over (DBusHashTable *dest,
651 DBusHashTable *override)
658 _dbus_hash_iter_init (override, &iter);
659 while (_dbus_hash_iter_next (&iter))
661 key = _dbus_hash_iter_get_string_key (&iter);
662 sid = _dbus_hash_iter_get_value (&iter);
664 key_copy = _dbus_strdup (key);
665 if (key_copy == NULL)
668 if (!_dbus_hash_table_insert_string (dest,
672 dbus_free (key_copy);
676 bus_selinux_id_ref (sid);
681 #endif /* HAVE_SELINUX */
684 * Creates the union of the two tables (each table maps a service
685 * name to a security ID). In case of the same service name in
686 * both tables, the security ID from "override" will be used.
688 * @param base the base table
689 * @param override the table that takes precedence in the merge
690 * @returns the new table, or #NULL if out of memory
693 bus_selinux_id_table_union (DBusHashTable *base,
694 DBusHashTable *override)
696 DBusHashTable *combined_table;
698 combined_table = bus_selinux_id_table_new ();
700 if (combined_table == NULL)
704 if (!selinux_enabled)
705 return combined_table;
707 if (!bus_selinux_id_table_copy_over (combined_table, base))
709 _dbus_hash_table_unref (combined_table);
713 if (!bus_selinux_id_table_copy_over (combined_table, override))
715 _dbus_hash_table_unref (combined_table);
718 #endif /* HAVE_SELINUX */
720 return combined_table;
724 * Get the SELinux policy root. This is used to find the D-BUS
725 * specific config file within the policy.
728 bus_selinux_get_policy_root (void)
731 return selinux_policy_root ();
734 #endif /* HAVE_SELINUX */
738 * For debugging: Print out the current hash table of service SIDs.
741 bus_selinux_id_table_print (DBusHashTable *service_table)
743 #ifdef DBUS_ENABLE_VERBOSE_MODE
747 if (!selinux_enabled)
750 _dbus_verbose ("Service SID Table:\n");
751 _dbus_hash_iter_init (service_table, &iter);
752 while (_dbus_hash_iter_next (&iter))
754 const char *key = _dbus_hash_iter_get_string_key (&iter);
755 security_id_t sid = _dbus_hash_iter_get_value (&iter);
756 _dbus_verbose ("The key is %s\n", key);
757 _dbus_verbose ("The context is %s\n", sid->ctx);
758 _dbus_verbose ("The refcount is %d\n", sid->refcnt);
760 #endif /* HAVE_SELINUX */
761 #endif /* DBUS_ENABLE_VERBOSE_MODE */
765 #ifdef DBUS_ENABLE_VERBOSE_MODE
768 * Print out some AVC statistics.
771 bus_avc_print_stats (void)
773 struct avc_cache_stats cstats;
775 if (!selinux_enabled)
778 _dbus_verbose ("AVC Statistics:\n");
779 avc_cache_stats (&cstats);
781 _dbus_verbose ("AVC Cache Statistics:\n");
782 _dbus_verbose ("Entry lookups: %d\n", cstats.entry_lookups);
783 _dbus_verbose ("Entry hits: %d\n", cstats.entry_hits);
784 _dbus_verbose ("Entry misses %d\n", cstats.entry_misses);
785 _dbus_verbose ("Entry discards: %d\n", cstats.entry_discards);
786 _dbus_verbose ("CAV lookups: %d\n", cstats.cav_lookups);
787 _dbus_verbose ("CAV hits: %d\n", cstats.cav_hits);
788 _dbus_verbose ("CAV probes: %d\n", cstats.cav_probes);
789 _dbus_verbose ("CAV misses: %d\n", cstats.cav_misses);
791 #endif /* HAVE_SELINUX */
792 #endif /* DBUS_ENABLE_VERBOSE_MODE */
796 * Destroy the AVC before we terminate.
799 bus_selinux_shutdown (void)
802 if (!selinux_enabled)
806 bus_sid = SECSID_WILD;
808 #ifdef DBUS_ENABLE_VERBOSE_MODE
809 bus_avc_print_stats ();
810 #endif /* DBUS_ENABLE_VERBOSE_MODE */
813 #endif /* HAVE_SELINUX */