1 From: Brian McGillion <brian.mcgillion@intel.com>
2 Date: Mon, 6 Feb 2012 18:46:05 +0200
3 Subject: Enable checking of smack context from DBus interface
8 bus/smack.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++
9 bus/smack.h | 36 +++++++++++++
10 cmake/CMakeLists.txt | 3 ++
11 cmake/bus/CMakeLists.txt | 4 +-
12 configure.ac | 17 +++++-
13 7 files changed, 199 insertions(+), 3 deletions(-)
14 create mode 100644 bus/smack.c
15 create mode 100644 bus/smack.h
17 diff --git a/bus/Makefile.am b/bus/Makefile.am
18 index 6cbc09a..7f63d86 100644
21 @@ -7,6 +7,7 @@ DBUS_BUS_LIBS = \
28 DBUS_LAUNCHER_LIBS = \
29 @@ -21,6 +22,7 @@ AM_CPPFLAGS = \
30 -DDBUS_SYSTEM_CONFIG_FILE=\""$(configdir)/system.conf"\" \
33 + $(LIBSMACK_CFLAGS) \
36 # if assertions are enabled, improve backtraces
37 @@ -93,6 +95,8 @@ BUS_SOURCES= \
46 diff --git a/bus/driver.c b/bus/driver.c
47 index 574e0f3..c6298d7 100644
57 #include <dbus/dbus-string.h>
59 #include <dbus/dbus-marshal-recursive.h>
63 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
64 DBusMessage *hello_message,
65 BusTransaction *transaction,
66 @@ -1736,6 +1738,10 @@ static const MessageHandler dbus_message_handlers[] = {
68 DBUS_TYPE_STRING_AS_STRING,
69 bus_driver_handle_get_id },
70 + { "GetConnectionSmackContext",
71 + DBUS_TYPE_STRING_AS_STRING,
72 + DBUS_TYPE_STRING_AS_STRING,
73 + bus_smack_handle_get_connection_context },
74 { NULL, NULL, NULL, NULL }
77 diff --git a/bus/smack.c b/bus/smack.c
79 index 0000000..b8542c2
83 +/* smack.c - Provide interface to query smack context
85 + * Author: Brian McGillion <brian.mcgillion@intel.com>
86 + * Copyright © 2011 Intel Corporation
88 + * Licensed under the Academic Free License version 2.1
90 + * This program is free software; you can redistribute it and/or modify
91 + * it under the terms of the GNU General Public License as published by
92 + * the Free Software Foundation; either version 2 of the License, or
93 + * (at your option) any later version.
95 + * This program is distributed in the hope that it will be useful,
96 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
97 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98 + * GNU General Public License for more details.
100 + * You should have received a copy of the GNU General Public License
101 + * along with this program; if not, write to the Free Software
102 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
109 +#include <dbus/dbus-internals.h>
111 +#include "connection.h"
112 +#include "services.h"
115 +#ifdef DBUS_ENABLE_SMACK
116 +#include <sys/smack.h>
119 +#ifdef DBUS_ENABLE_SMACK
121 +bus_smack_get_label (DBusConnection *connection)
126 + if (!dbus_connection_get_socket(connection, &sock_fd))
129 + if (smack_new_label_from_socket(sock_fd, &label) < 0)
136 +bus_smack_handle_get_connection_context (DBusConnection *connection,
137 + BusTransaction *transaction,
138 + DBusMessage *message,
141 +#ifdef DBUS_ENABLE_SMACK
142 + const char *remote_end = NULL;
143 + BusRegistry *registry;
144 + DBusString remote_end_str;
145 + BusService *service;
146 + DBusConnection *remote_connection;
147 + DBusMessage *reply = NULL;
150 + _DBUS_ASSERT_ERROR_IS_CLEAR (error);
152 + registry = bus_connection_get_registry (connection);
154 + if (!dbus_message_get_args (message, error, DBUS_TYPE_STRING, &remote_end,
155 + DBUS_TYPE_INVALID))
158 + _dbus_verbose ("asked for label of connection %s\n", remote_end);
160 + _dbus_string_init_const (&remote_end_str, remote_end);
162 + service = bus_registry_lookup (registry, &remote_end_str);
163 + if (service == NULL)
165 + dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
166 + "Bus name '%s' has no owner", remote_end);
170 + remote_connection = bus_service_get_primary_owners_connection (service);
171 + if (remote_connection == NULL)
174 + reply = dbus_message_new_method_return (message);
178 + label = bus_smack_get_label (remote_connection);
181 + dbus_set_error (error, DBUS_ERROR_FAILED,
182 + "Failed to get the socket fd of the connection",
187 + if (!dbus_message_append_args (reply, DBUS_TYPE_STRING,
188 + &label, DBUS_TYPE_INVALID))
191 + if (!bus_transaction_send_from_driver (transaction, connection, reply))
194 + dbus_message_unref (reply);
200 + BUS_SET_OOM (error);
204 + dbus_message_unref (reply);
210 + dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
211 + "SMACK support is not enabled");
215 diff --git a/bus/smack.h b/bus/smack.h
217 index 0000000..04a4a2a
221 +/* smack.h - Provide interface to query smack context
223 + * Author: Brian McGillion <brian.mcgillion@intel.com>
224 + * Copyright © 2011 Intel Corporation
226 + * Based on example from Stats interface
228 + * Licensed under the Academic Free License version 2.1
230 + * This program is free software; you can redistribute it and/or modify
231 + * it under the terms of the GNU General Public License as published by
232 + * the Free Software Foundation; either version 2 of the License, or
233 + * (at your option) any later version.
235 + * This program is distributed in the hope that it will be useful,
236 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
237 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
238 + * GNU General Public License for more details.
240 + * You should have received a copy of the GNU General Public License
241 + * along with this program; if not, write to the Free Software
242 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
251 +dbus_bool_t bus_smack_handle_get_connection_context (DBusConnection *connection,
252 + BusTransaction *transaction,
253 + DBusMessage *message,
257 diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
258 index 000acda..68b7a9e 100644
259 --- a/cmake/CMakeLists.txt
260 +++ b/cmake/CMakeLists.txt
261 @@ -94,6 +94,8 @@ option (DBUS_ENABLE_STATS "enable bus daemon usage statistics" OFF)
263 option (DBUS_ENABLE_STATS "enable bus daemon usage statistics" OFF)
265 +option (DBUS_ENABLE_SMACK "enable smack checks in the daemon" OFF)
268 find_package(LibExpat)
270 @@ -555,6 +557,7 @@ message(" Building bus stats API: ${DBUS_ENABLE_STATS} "
271 message(" installing system libs: ${DBUS_INSTALL_SYSTEM_LIBS} ")
272 #message(" Building SELinux support: ${have_selinux} ")
273 #message(" Building dnotify support: ${have_dnotify} ")
274 +message(" Building Smack support: ${DBUS_ENABLE_SMACK} ")
275 message(" Building Doxygen docs: ${DBUS_ENABLE_DOXYGEN_DOCS} ")
276 message(" Building XML docs: ${DBUS_ENABLE_XML_DOCS} ")
277 #message(" Gettext libs (empty OK): ${INTLLIBS} ")
278 diff --git a/cmake/bus/CMakeLists.txt b/cmake/bus/CMakeLists.txt
279 index 2657605..13fb34c 100644
280 --- a/cmake/bus/CMakeLists.txt
281 +++ b/cmake/bus/CMakeLists.txt
282 @@ -72,7 +72,9 @@ set (BUS_SOURCES
293 diff --git a/configure.ac b/configure.ac
294 index a963d4d..95216c5 100644
297 @@ -207,6 +207,9 @@ if test "x$enable_embedded_tests" = xyes; then
298 [Define to build test code into the library and binaries])
301 +# call early to ensure availability
304 # DBUS_ENABLE_MODULAR_TESTS controls tests that work based on public API.
305 # These use GTest, from GLib, because life's too short. They're enabled by
306 # default (unless you don't have GLib), because they don't bloat the library
307 @@ -907,8 +910,6 @@ fi
308 # unix:path=/foo or unix:abstract=/foo
309 AC_SUBST(DBUS_PATH_OR_ABSTRACT)
313 #### Sort out XML library
316 @@ -1703,6 +1704,17 @@ if test "x$enable_stats" = xyes; then
317 [Define to enable bus daemon usage statistics])
320 +#enable smack label support
321 +AC_ARG_ENABLE([smack], [AS_HELP_STRING([--enable-smack], [enable SMACK security checks])], [], [enable_smack=no])
322 +if test "x$enable_smack" = xyes; then
323 + PKG_CHECK_MODULES([LIBSMACK], [libsmack >= 1.0],
324 + [AC_DEFINE([DBUS_ENABLE_SMACK], [1], [Define to enable SMACK security features])],
325 + [AC_MSG_ERROR([libsmack is required to enable smack support])])
328 +AC_SUBST([LIBSMACK_CFLAGS])
329 +AC_SUBST([LIBSMACK_LIBS])
334 @@ -1781,6 +1793,7 @@ echo "
335 Building checks: ${enable_checks}
336 Building bus stats API: ${enable_stats}
337 Building SELinux support: ${have_selinux}
338 + Building SMACK support: ${enable_smack}
339 Building inotify support: ${have_inotify}
340 Building dnotify support: ${have_dnotify}
341 Building kqueue support: ${have_kqueue}