Merge branch 'avc_open' into 'master'

Stop using avc_init() which is deprecated and use avc_open() instead. Also, use SELINUX_CB_POLICYLOAD instead of AVC_CALLBACK_RESET callback.

See merge request dbus/dbus!31

Reviewed-by: smcv
This commit is contained in:
Simon McVittie 2018-11-15 15:17:47 +00:00
commit 0732d3ee1b
5 changed files with 125 additions and 132 deletions

View file

@ -995,12 +995,10 @@ bus_context_new (const DBusString *config_file,
*/ */
bus_audit_init (context); bus_audit_init (context);
if (!bus_selinux_full_init ()) if (!bus_selinux_full_init (context, error))
{ {
bus_context_log (context, DBUS_SYSTEM_LOG_ERROR, _DBUS_ASSERT_ERROR_IS_SET (error);
"SELinux enabled but D-Bus initialization failed; " goto failed;
"check system log");
exit (1);
} }
if (!bus_apparmor_full_init (error)) if (!bus_apparmor_full_init (error))
@ -1009,6 +1007,13 @@ bus_context_new (const DBusString *config_file,
goto failed; goto failed;
} }
if (bus_selinux_enabled ())
{
if (context->syslog)
bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
"SELinux support is enabled\n");
}
if (bus_apparmor_enabled ()) if (bus_apparmor_enabled ())
{ {
/* Only print AppArmor mediation message when syslog support is enabled */ /* Only print AppArmor mediation message when syslog support is enabled */

View file

@ -49,6 +49,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <grp.h> #include <grp.h>
#include <dbus/dbus-watch.h>
#endif /* HAVE_SELINUX */ #endif /* HAVE_SELINUX */
#ifdef HAVE_LIBAUDIT #ifdef HAVE_LIBAUDIT
#include <libaudit.h> #include <libaudit.h>
@ -64,45 +65,20 @@ static dbus_bool_t selinux_enabled = FALSE;
/* Store an avc_entry_ref to speed AVC decisions. */ /* Store an avc_entry_ref to speed AVC decisions. */
static struct avc_entry_ref aeref; static struct avc_entry_ref aeref;
/* Store the avc netlink fd. */
static int avc_netlink_fd = -1;
/* Watch to listen for SELinux status changes via netlink. */
static DBusWatch *avc_netlink_watch_obj = NULL;
static DBusLoop *avc_netlink_loop_obj = NULL;
/* Store the SID of the bus itself to use as the default. */ /* Store the SID of the bus itself to use as the default. */
static security_id_t bus_sid = SECSID_WILD; static security_id_t bus_sid = SECSID_WILD;
/* Thread to listen for SELinux status changes via netlink. */
static pthread_t avc_notify_thread;
/* Prototypes for AVC callback functions. */ /* Prototypes for AVC callback functions. */
static void log_callback (const char *fmt, ...) _DBUS_GNUC_PRINTF (1, 2); static int log_callback (int type, const char *fmt, ...) _DBUS_GNUC_PRINTF (2, 3);
static void log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft); static int log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft);
static void *avc_create_thread (void (*run) (void));
static void avc_stop_thread (void *thread);
static void *avc_alloc_lock (void);
static void avc_get_lock (void *lock);
static void avc_release_lock (void *lock);
static void avc_free_lock (void *lock);
/* AVC callback structures for use in avc_init. */
static const struct avc_memory_callback mem_cb =
{
.func_malloc = dbus_malloc,
.func_free = dbus_free
};
static const struct avc_log_callback log_cb =
{
.func_log = log_callback,
.func_audit = log_audit_callback
};
static const struct avc_thread_callback thread_cb =
{
.func_create_thread = avc_create_thread,
.func_stop_thread = avc_stop_thread
};
static const struct avc_lock_callback lock_cb =
{
.func_alloc_lock = avc_alloc_lock,
.func_get_lock = avc_get_lock,
.func_release_lock = avc_release_lock,
.func_free_lock = avc_free_lock
};
#endif /* HAVE_SELINUX */ #endif /* HAVE_SELINUX */
/** /**
@ -115,8 +91,8 @@ static const struct avc_lock_callback lock_cb =
*/ */
#ifdef HAVE_SELINUX #ifdef HAVE_SELINUX
static void static int
log_callback (const char *fmt, ...) log_callback (int type, const char *fmt, ...)
{ {
va_list ap; va_list ap;
#ifdef HAVE_LIBAUDIT #ifdef HAVE_LIBAUDIT
@ -150,6 +126,8 @@ log_callback (const char *fmt, ...)
out: out:
#endif #endif
va_end(ap); va_end(ap);
return 0;
} }
/** /**
@ -157,20 +135,16 @@ out:
* this could have changed. Send a SIGHUP to reload all configs. * this could have changed. Send a SIGHUP to reload all configs.
*/ */
static int static int
policy_reload_callback (u_int32_t event, security_id_t ssid, policy_reload_callback (int seqno)
security_id_t tsid, security_class_t tclass,
access_vector_t perms, access_vector_t *out_retained)
{ {
if (event == AVC_CALLBACK_RESET) _dbus_verbose ("SELinux policy reload callback called, sending SIGHUP\n");
return raise (SIGHUP); return raise (SIGHUP);
return 0;
} }
/** /**
* Log any auxiliary data * Log any auxiliary data
*/ */
static void static int
log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft) log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft)
{ {
DBusString *audmsg = data; DBusString *audmsg = data;
@ -188,73 +162,20 @@ log_audit_callback (void *data, security_class_t class, char *buf, size_t buflef
if (bufleft > (size_t) _dbus_string_get_length(&s)) if (bufleft > (size_t) _dbus_string_get_length(&s))
_dbus_string_copy_to_buffer_with_nul (&s, buf, bufleft); _dbus_string_copy_to_buffer_with_nul (&s, buf, bufleft);
} }
return 0;
} }
/** static dbus_bool_t
* Create thread to notify the AVC of enforcing and policy reload handle_avc_netlink_watch (DBusWatch *passed_watch, unsigned int flags, void *data)
* changes via netlink.
*
* @param run the thread run function
* @return pointer to the thread
*/
static void *
avc_create_thread (void (*run) (void))
{ {
int rc; if (avc_netlink_check_nb () < 0)
rc = pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
if (rc != 0)
{ {
_dbus_warn ("Failed to start AVC thread: %s", _dbus_strerror (rc)); _dbus_warn ("Failed to check the netlink socket for pending messages and process them: %s", _dbus_strerror (errno));
exit (1); return FALSE;
} }
return &avc_notify_thread;
}
/* Stop AVC netlink thread. */ return TRUE;
static void
avc_stop_thread (void *thread)
{
pthread_cancel (*(pthread_t *) thread);
}
/* Allocate a new AVC lock. */
static void *
avc_alloc_lock (void)
{
pthread_mutex_t *avc_mutex;
avc_mutex = dbus_new (pthread_mutex_t, 1);
if (avc_mutex == NULL)
{
_dbus_warn ("Could not create mutex: %s", _dbus_strerror (errno));
exit (1);
}
pthread_mutex_init (avc_mutex, NULL);
return avc_mutex;
}
/* Acquire an AVC lock. */
static void
avc_get_lock (void *lock)
{
pthread_mutex_lock (lock);
}
/* Release an AVC lock. */
static void
avc_release_lock (void *lock)
{
pthread_mutex_unlock (lock);
}
/* Free an AVC lock. */
static void
avc_free_lock (void *lock)
{
pthread_mutex_destroy (lock);
dbus_free (lock);
} }
#endif /* HAVE_SELINUX */ #endif /* HAVE_SELINUX */
@ -335,7 +256,7 @@ static struct security_class_mapping dbus_map[] = {
* logging callbacks. * logging callbacks.
*/ */
dbus_bool_t dbus_bool_t
bus_selinux_full_init (void) bus_selinux_full_init (BusContext *context, DBusError *error)
{ {
#ifdef HAVE_SELINUX #ifdef HAVE_SELINUX
char *bus_context; char *bus_context;
@ -358,9 +279,11 @@ bus_selinux_full_init (void)
} }
avc_entry_ref_init (&aeref); avc_entry_ref_init (&aeref);
if (avc_init ("avc", &mem_cb, &log_cb, &thread_cb, &lock_cb) < 0) if (avc_open (NULL, 0) < 0)
{ {
_dbus_warn ("Failed to start Access Vector Cache (AVC)."); dbus_set_error (error, DBUS_ERROR_FAILED,
"Failed to start Access Vector Cache (AVC): %s",
_dbus_strerror (errno));
return FALSE; return FALSE;
} }
else else
@ -368,35 +291,83 @@ bus_selinux_full_init (void)
_dbus_verbose ("Access Vector Cache (AVC) started.\n"); _dbus_verbose ("Access Vector Cache (AVC) started.\n");
} }
if (avc_add_callback (policy_reload_callback, AVC_CALLBACK_RESET, avc_netlink_fd = avc_netlink_acquire_fd ();
NULL, NULL, 0, 0) < 0) if (avc_netlink_fd < 0)
{ {
_dbus_warn ("Failed to add policy reload callback: %s", dbus_set_error (error, DBUS_ERROR_FAILED,
_dbus_strerror (errno)); "Cannot acquire AVC netlink fd: %s",
avc_destroy (); _dbus_strerror (errno));
return FALSE; goto error;
} }
_dbus_fd_set_close_on_exec (avc_netlink_fd);
avc_netlink_loop_obj = bus_context_get_loop (context);
/* avc_netlink_loop_obj is a global variable */
_dbus_loop_ref (avc_netlink_loop_obj);
avc_netlink_watch_obj = _dbus_watch_new (avc_netlink_fd, DBUS_WATCH_READABLE, TRUE,
handle_avc_netlink_watch, NULL, NULL);
if (avc_netlink_watch_obj == NULL)
{
BUS_SET_OOM (error);
goto error;
}
if (!_dbus_loop_add_watch (avc_netlink_loop_obj, avc_netlink_watch_obj))
{
_dbus_watch_invalidate (avc_netlink_watch_obj);
_dbus_clear_watch (&avc_netlink_watch_obj);
avc_netlink_watch_obj = NULL;
BUS_SET_OOM (error);
goto error;
}
selinux_set_callback (SELINUX_CB_POLICYLOAD, (union selinux_callback) policy_reload_callback);
selinux_set_callback (SELINUX_CB_AUDIT, (union selinux_callback) log_audit_callback);
selinux_set_callback (SELINUX_CB_LOG, (union selinux_callback) log_callback);
bus_context = NULL; bus_context = NULL;
bus_sid = SECSID_WILD; bus_sid = SECSID_WILD;
if (getcon (&bus_context) < 0) if (getcon (&bus_context) < 0)
{ {
_dbus_verbose ("Error getting context of bus: %s\n", dbus_set_error (error, DBUS_ERROR_FAILED,
_dbus_strerror (errno)); "Error getting context of bus: %s",
return FALSE; _dbus_strerror (errno));
goto error;
} }
if (avc_context_to_sid (bus_context, &bus_sid) < 0) if (avc_context_to_sid (bus_context, &bus_sid) < 0)
{ {
_dbus_verbose ("Error getting SID from bus context: %s\n", dbus_set_error (error, DBUS_ERROR_FAILED,
_dbus_strerror (errno)); "Error getting SID from bus context: %s",
_dbus_strerror (errno));
freecon (bus_context); freecon (bus_context);
return FALSE; goto error;
} }
freecon (bus_context); freecon (bus_context);
return TRUE;
error:
if (avc_netlink_watch_obj)
{
_dbus_loop_remove_watch (avc_netlink_loop_obj, avc_netlink_watch_obj);
_dbus_watch_invalidate (avc_netlink_watch_obj);
_dbus_clear_watch (&avc_netlink_watch_obj);
}
_dbus_clear_loop (&avc_netlink_loop_obj);
if (avc_netlink_fd >= 0)
{
avc_netlink_release_fd ();
avc_netlink_fd = -1;
}
avc_destroy ();
_DBUS_ASSERT_ERROR_IS_SET (error);
return FALSE;
#endif /* HAVE_SELINUX */ #endif /* HAVE_SELINUX */
return TRUE; return TRUE;
} }
@ -976,6 +947,20 @@ bus_selinux_shutdown (void)
_dbus_verbose ("AVC shutdown\n"); _dbus_verbose ("AVC shutdown\n");
if (avc_netlink_watch_obj)
{
_dbus_loop_remove_watch (avc_netlink_loop_obj, avc_netlink_watch_obj);
_dbus_watch_invalidate (avc_netlink_watch_obj);
_dbus_clear_watch (&avc_netlink_watch_obj);
}
_dbus_clear_loop (&avc_netlink_loop_obj);
if (avc_netlink_fd >= 0)
{
avc_netlink_release_fd ();
avc_netlink_fd = -1;
}
if (bus_sid != SECSID_WILD) if (bus_sid != SECSID_WILD)
{ {
bus_sid = SECSID_WILD; bus_sid = SECSID_WILD;

View file

@ -28,7 +28,7 @@
#include "services.h" #include "services.h"
dbus_bool_t bus_selinux_pre_init (void); dbus_bool_t bus_selinux_pre_init (void);
dbus_bool_t bus_selinux_full_init(void); dbus_bool_t bus_selinux_full_init(BusContext *context, DBusError *error);
void bus_selinux_shutdown (void); void bus_selinux_shutdown (void);
dbus_bool_t bus_selinux_enabled (void); dbus_bool_t bus_selinux_enabled (void);

View file

@ -47,12 +47,6 @@ static DBusString test_data_dir;
static void static void
test_pre_hook (void) test_pre_hook (void)
{ {
if (_dbus_getenv ("DBUS_TEST_SELINUX")
&& (!bus_selinux_pre_init ()
|| !bus_selinux_full_init ()))
_dbus_test_fatal ("Could not init selinux support");
initial_fds = _dbus_check_fdleaks_enter (); initial_fds = _dbus_check_fdleaks_enter ();
} }

View file

@ -28,6 +28,8 @@
#include <dbus/dbus-internals.h> #include <dbus/dbus-internals.h>
#include <dbus/dbus-list.h> #include <dbus/dbus-list.h>
#include <dbus/dbus-sysdeps.h> #include <dbus/dbus-sysdeps.h>
#include <dbus/dbus-test-tap.h>
#include "selinux.h"
/* The "debug client" watch/timeout handlers don't dispatch messages, /* The "debug client" watch/timeout handlers don't dispatch messages,
* as we manually pull them in order to verify them. This is why they * as we manually pull them in order to verify them. This is why they
@ -307,6 +309,13 @@ bus_context_new_test (const DBusString *test_data_dir,
return NULL; return NULL;
} }
if (_dbus_getenv ("DBUS_TEST_SELINUX")
&& (!bus_selinux_pre_init ()
|| !bus_selinux_full_init (context, &error)))
_dbus_test_fatal ("Could not init selinux support");
dbus_error_free (&error);
_dbus_string_free (&config_file); _dbus_string_free (&config_file);
return context; return context;