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"
33 #include <selinux/selinux.h>
34 #include <selinux/avc.h>
35 #include <selinux/av_permissions.h>
36 #include <selinux/flask.h>
38 #endif /* HAVE_SELINUX */
40 #define BUS_SID_FROM_SELINUX(sid) ((BusSELinuxID*) (sid))
41 #define SELINUX_SID_FROM_BUS(sid) ((security_id_t) (sid))
44 /* Store the value telling us if SELinux is enabled in the kernel. */
45 static dbus_bool_t selinux_enabled = FALSE;
47 /* Store an avc_entry_ref to speed AVC decisions. */
48 static struct avc_entry_ref aeref;
50 static security_id_t bus_sid = SECSID_WILD;
51 #endif /* HAVE_SELINUX */
54 * Log callback to log denial messages from the AVC.
55 * This is used in avc_init. Logs to both standard
58 * @param fmt the format string
59 * @param variable argument list
63 log_callback (const char *fmt, ...)
67 vsyslog (LOG_INFO, fmt, ap);
70 #endif /* HAVE_SELINUX */
74 * Initialize the user space access vector cache (AVC) for D-BUS and set up
78 bus_selinux_init (void)
81 struct avc_log_callback log_cb = {(void*)log_callback, NULL};
85 _dbus_assert (bus_sid == SECSID_WILD);
87 /* Determine if we are running an SELinux kernel. */
88 r = is_selinux_enabled ();
91 _dbus_warn ("Could not tell if SELinux is enabled: %s\n",
92 _dbus_strerror (errno));
96 selinux_enabled = r != 0;
100 _dbus_verbose ("SELinux not enabled in this kernel.\n");
104 _dbus_verbose ("SELinux is enabled in this kernel.\n");
106 avc_entry_ref_init (&aeref);
107 if (avc_init ("avc", NULL, &log_cb, NULL, NULL) < 0)
109 _dbus_warn ("Failed to start Access Vector Cache (AVC).\n");
114 openlog ("dbus", LOG_PERROR, LOG_USER);
115 _dbus_verbose ("Access Vector Cache (AVC) started.\n");
119 bus_sid = SECSID_WILD;
121 if (getcon (&bus_context) < 0)
123 _dbus_verbose ("Error getting context of bus: %s\n",
124 _dbus_strerror (errno));
128 if (avc_context_to_sid (bus_context, &bus_sid) < 0)
130 _dbus_verbose ("Error getting SID from bus context: %s\n",
131 _dbus_strerror (errno));
132 freecon (bus_context);
136 freecon (bus_context);
141 #endif /* HAVE_SELINUX */
146 * Decrement SID reference count.
148 * @param sid the SID to decrement
151 bus_selinux_id_unref (BusSELinuxID *sid)
154 if (!selinux_enabled)
157 _dbus_assert (sid != NULL);
159 sidput (SELINUX_SID_FROM_BUS (sid));
160 #endif /* HAVE_SELINUX */
164 bus_selinux_id_ref (BusSELinuxID *sid)
167 if (!selinux_enabled)
170 _dbus_assert (sid != NULL);
172 sidget (SELINUX_SID_FROM_BUS (sid));
173 #endif /* HAVE_SELINUX */
177 * Determine if the SELinux security policy allows the given sender
178 * security context to go to the given recipient security context.
179 * This function determines if the requested permissions are to be
180 * granted from the connection to the message bus or to another
181 * optionally supplied security identifier (e.g. for a service
182 * context). Currently these permissions are either send_msg or
183 * acquire_svc in the dbus class.
185 * @param sender_sid source security context
186 * @param override_sid is the target security context. If SECSID_WILD this will
187 * use the context of the bus itself (e.g. the default).
188 * @param target_class is the target security class.
189 * @param requested is the requested permissions.
190 * @returns #TRUE if security policy allows the send.
194 bus_selinux_check (BusSELinuxID *sender_sid,
195 BusSELinuxID *override_sid,
196 security_class_t target_class,
197 access_vector_t requested)
199 if (!selinux_enabled)
202 /* Make the security check. AVC checks enforcing mode here as well. */
203 if (avc_has_perm (SELINUX_SID_FROM_BUS (sender_sid),
205 SELINUX_SID_FROM_BUS (override_sid) :
206 SELINUX_SID_FROM_BUS (bus_sid),
207 target_class, requested, &aeref, NULL) < 0)
209 _dbus_verbose ("SELinux denying due to security policy.\n");
215 #endif /* HAVE_SELINUX */
218 * Returns true if the given connection can acquire a service,
219 * assuming the given security ID is needed for that service.
221 * @param connection connection that wants to own the service
222 * @param service_sid the SID of the service from the table
223 * @returns #TRUE if acquire is permitted.
226 bus_selinux_allows_acquire_service (DBusConnection *connection,
227 BusSELinuxID *service_sid)
230 BusSELinuxID *connection_sid;
232 if (!selinux_enabled)
235 connection_sid = bus_connection_get_selinux_id (connection);
237 return bus_selinux_check (connection_sid,
243 #endif /* HAVE_SELINUX */
247 * Check if SELinux security controls allow the message to be sent to a
248 * particular connection based on the security context of the sender and
249 * that of the receiver. The destination connection need not be the
250 * addressed recipient, it could be an "eavesdropper"
252 * @param sender the sender of the message.
253 * @param proposed_recipient the connection the message is to be sent to.
254 * @returns whether to allow the send
257 bus_selinux_allows_send (DBusConnection *sender,
258 DBusConnection *proposed_recipient)
261 BusSELinuxID *recipient_sid;
262 BusSELinuxID *sender_sid;
264 if (!selinux_enabled)
267 sender_sid = bus_connection_get_selinux_id (sender);
268 /* A NULL proposed_recipient means the bus itself. */
269 if (proposed_recipient)
270 recipient_sid = bus_connection_get_selinux_id (proposed_recipient);
272 recipient_sid = BUS_SID_FROM_SELINUX (bus_sid);
274 return bus_selinux_check (sender_sid, recipient_sid,
275 SECCLASS_DBUS, DBUS__SEND_MSG);
278 #endif /* HAVE_SELINUX */
282 * Gets the security context of a connection to the bus. It is up to
283 * the caller to freecon() when they are done.
285 * @param connection the connection to get the context of.
286 * @param con the location to store the security context.
287 * @returns #TRUE if context is successfully obtained.
291 bus_connection_read_selinux_context (DBusConnection *connection,
296 if (!selinux_enabled)
299 _dbus_assert (connection != NULL);
301 if (!dbus_connection_get_unix_fd (connection, &fd))
303 _dbus_verbose ("Failed to get file descriptor of socket.\n");
307 if (getpeercon (fd, con) < 0)
309 _dbus_verbose ("Error getting context of socket peer: %s\n",
310 _dbus_strerror (errno));
314 _dbus_verbose ("Successfully read connection context.\n");
317 #endif /* HAVE_SELINUX */
320 * Read the SELinux ID from the connection.
322 * @param connection the connection to read from
323 * @returns the SID if successfully determined, #NULL otherwise.
326 bus_selinux_init_connection_id (DBusConnection *connection,
333 if (!selinux_enabled)
336 if (!bus_connection_read_selinux_context (connection, &con))
338 dbus_set_error (error, DBUS_ERROR_FAILED,
339 "Failed to read an SELinux context from connection");
340 _dbus_verbose ("Error getting peer context.\n");
344 _dbus_verbose ("Converting context to SID to store on connection\n");
346 if (avc_context_to_sid (con, &sid) < 0)
351 dbus_set_error (error, DBUS_ERROR_FAILED,
352 "Error getting SID from context: %s\n",
353 _dbus_strerror (errno));
355 _dbus_warn ("Error getting SID from context: %s\n",
356 _dbus_strerror (errno));
363 return BUS_SID_FROM_SELINUX (sid);
366 #endif /* HAVE_SELINUX */
370 /* Function for freeing hash table data. These SIDs
371 * should no longer be referenced.
374 bus_selinux_id_table_free_value (BusSELinuxID *sid)
377 /* NULL sometimes due to how DBusHashTable works */
379 bus_selinux_id_unref (sid);
380 #endif /* HAVE_SELINUX */
384 * Creates a new table mapping service names to security ID.
385 * A security ID is a "compiled" security context, a security
386 * context is just a string.
388 * @returns the new table or #NULL if no memory
391 bus_selinux_id_table_new (void)
393 return _dbus_hash_table_new (DBUS_HASH_STRING,
394 (DBusFreeFunction) dbus_free,
395 (DBusFreeFunction) bus_selinux_id_table_free_value);
399 * Hashes a service name and service context into the service SID
400 * table as a string and a SID.
402 * @param service_name is the name of the service.
403 * @param service_context is the context of the service.
404 * @param service_table is the table to hash them into.
405 * @return #FALSE if not enough memory
408 bus_selinux_id_table_insert (DBusHashTable *service_table,
409 const char *service_name,
410 const char *service_context)
417 if (!selinux_enabled)
423 key = _dbus_strdup (service_name);
427 if (avc_context_to_sid (service_context, &sid) < 0)
429 _dbus_assert (errno == ENOMEM);
433 if (!_dbus_hash_table_insert_string (service_table,
435 BUS_SID_FROM_SELINUX (sid)))
438 _dbus_verbose ("Parsed \tservice: %s \n\t\tcontext: %s\n",
442 /* These are owned by the hash, so clear them to avoid unref */
449 if (sid != SECSID_WILD)
458 #endif /* HAVE_SELINUX */
463 * Find the security identifier associated with a particular service
464 * name. Return a pointer to this SID, or #NULL/SECSID_WILD if the
465 * service is not found in the hash table. This should be nearly a
466 * constant time operation. If SELinux support is not available,
467 * always return NULL.
469 * @todo This should return a const security_id_t since we don't
470 * want the caller to mess with it.
472 * @param service_table the hash table to check for service name.
473 * @param service_name the name of the service to look for.
474 * @returns the SELinux ID associated with the service
477 bus_selinux_id_table_lookup (DBusHashTable *service_table,
478 const DBusString *service_name)
483 sid = SECSID_WILD; /* default context */
485 if (!selinux_enabled)
488 _dbus_verbose ("Looking up service SID for %s\n",
489 _dbus_string_get_const_data (service_name));
491 sid = _dbus_hash_table_lookup_string (service_table,
492 _dbus_string_get_const_data (service_name));
494 if (sid == SECSID_WILD)
495 _dbus_verbose ("Service %s not found\n",
496 _dbus_string_get_const_data (service_name));
498 _dbus_verbose ("Service %s found\n",
499 _dbus_string_get_const_data (service_name));
501 return BUS_SID_FROM_SELINUX (sid);
502 #endif /* HAVE_SELINUX */
508 bus_selinux_id_table_copy_over (DBusHashTable *dest,
509 DBusHashTable *override)
516 _dbus_hash_iter_init (override, &iter);
517 while (_dbus_hash_iter_next (&iter))
519 key = _dbus_hash_iter_get_string_key (&iter);
520 sid = _dbus_hash_iter_get_value (&iter);
522 key_copy = _dbus_strdup (key);
523 if (key_copy == NULL)
526 if (!_dbus_hash_table_insert_string (dest,
530 dbus_free (key_copy);
534 bus_selinux_id_ref (sid);
539 #endif /* HAVE_SELINUX */
542 * Creates the union of the two tables (each table maps a service
543 * name to a security ID). In case of the same service name in
544 * both tables, the security ID from "override" will be used.
546 * @param base the base table
547 * @param override the table that takes precedence in the merge
548 * @returns the new table, or #NULL if out of memory
551 bus_selinux_id_table_union (DBusHashTable *base,
552 DBusHashTable *override)
554 DBusHashTable *combined_table;
556 combined_table = bus_selinux_id_table_new ();
558 if (combined_table == NULL)
562 if (!selinux_enabled)
563 return combined_table;
565 if (!bus_selinux_id_table_copy_over (combined_table, base))
567 _dbus_hash_table_unref (combined_table);
571 if (!bus_selinux_id_table_copy_over (combined_table, override))
573 _dbus_hash_table_unref (combined_table);
576 #endif /* HAVE_SELINUX */
578 return combined_table;
582 * For debugging: Print out the current hash table of service SIDs.
585 bus_selinux_id_table_print (DBusHashTable *service_table)
587 #ifdef DBUS_ENABLE_VERBOSE_MODE
591 if (!selinux_enabled)
594 _dbus_verbose ("Service SID Table:\n");
595 _dbus_hash_iter_init (service_table, &iter);
596 while (_dbus_hash_iter_next (&iter))
598 const char *key = _dbus_hash_iter_get_string_key (&iter);
599 security_id_t sid = _dbus_hash_iter_get_value (&iter);
600 _dbus_verbose ("The key is %s\n", key);
601 _dbus_verbose ("The context is %s\n", sid->ctx);
602 _dbus_verbose ("The refcount is %d\n", sid->refcnt);
604 #endif /* HAVE_SELINUX */
605 #endif /* DBUS_ENABLE_VERBOSE_MODE */
609 #ifdef DBUS_ENABLE_VERBOSE_MODE
612 * Print out some AVC statistics.
615 bus_avc_print_stats (void)
617 struct avc_cache_stats cstats;
619 if (!selinux_enabled)
622 _dbus_verbose ("AVC Statistics:\n");
623 avc_cache_stats (&cstats);
625 _dbus_verbose ("AVC Cache Statistics:\n");
626 _dbus_verbose ("Entry lookups: %d\n", cstats.entry_lookups);
627 _dbus_verbose ("Entry hits: %d\n", cstats.entry_hits);
628 _dbus_verbose ("Entry misses %d\n", cstats.entry_misses);
629 _dbus_verbose ("Entry discards: %d\n", cstats.entry_discards);
630 _dbus_verbose ("CAV lookups: %d\n", cstats.cav_lookups);
631 _dbus_verbose ("CAV hits: %d\n", cstats.cav_hits);
632 _dbus_verbose ("CAV probes: %d\n", cstats.cav_probes);
633 _dbus_verbose ("CAV misses: %d\n", cstats.cav_misses);
635 #endif /* HAVE_SELINUX */
636 #endif /* DBUS_ENABLE_VERBOSE_MODE */
640 * Destroy the AVC before we terminate.
643 bus_selinux_shutdown (void)
646 if (!selinux_enabled)
650 bus_sid = SECSID_WILD;
652 #ifdef DBUS_ENABLE_VERBOSE_MODE
653 bus_avc_print_stats ();
654 #endif /* DBUS_ENABLE_VERBOSE_MODE */
657 #endif /* HAVE_SELINUX */