author | William Roberts <williamr@symbian.org> |
Fri, 23 Jul 2010 16:09:54 +0100 | |
branch | GCC_SURGE |
changeset 47 | d7383dba13ba |
parent 45 | 4b03adbd26ca |
permissions | -rw-r--r-- |
34
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
2 |
* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
3 |
* selinux.c SELinux security checks for D-Bus |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
4 |
* |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
5 |
* Author: Matthew Rickard <mjricka@epoch.ncsc.mil> |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
6 |
* |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
7 |
* Licensed under the Academic Free License version 2.1 |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
8 |
* |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
9 |
* This program is free software; you can redistribute it and/or modify |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
10 |
* it under the terms of the GNU General Public License as published by |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
11 |
* the Free Software Foundation; either version 2 of the License, or |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
12 |
* (at your option) any later version. |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
13 |
* |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
14 |
* This program is distributed in the hope that it will be useful, |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
17 |
* GNU General Public License for more details. |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
18 |
* |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
19 |
* You should have received a copy of the GNU General Public License |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
20 |
* along with this program; if not, write to the Free Software |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
22 |
* |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
23 |
*/ |
31 | 24 |
#ifndef __SYMBIAN32__ |
25 |
#include <dbus/dbus-internals.h> |
|
26 |
#include <dbus/dbus-string.h> |
|
27 |
#else |
|
28 |
#include "dbus-internals.h" |
|
29 |
#include "dbus-string.h" |
|
30 |
#endif //__SYMBIAN32__ |
|
31 |
#include "selinux.h" |
|
32 |
#include "services.h" |
|
33 |
#include "policy.h" |
|
34 |
#include "utils.h" |
|
35 |
#include "config-parser.h" |
|
36 |
||
37 |
#ifdef HAVE_SELINUX |
|
38 |
#include <errno.h> |
|
39 |
#include <pthread.h> |
|
40 |
#include <syslog.h> |
|
41 |
#include <selinux/selinux.h> |
|
42 |
#include <selinux/avc.h> |
|
43 |
#include <selinux/av_permissions.h> |
|
44 |
#include <selinux/flask.h> |
|
45 |
#include <signal.h> |
|
46 |
#include <stdarg.h> |
|
47 |
#endif /* HAVE_SELINUX */ |
|
48 |
||
49 |
#define BUS_SID_FROM_SELINUX(sid) ((BusSELinuxID*) (sid)) |
|
50 |
#define SELINUX_SID_FROM_BUS(sid) ((security_id_t) (sid)) |
|
51 |
||
52 |
#ifdef HAVE_SELINUX |
|
53 |
/* Store the value telling us if SELinux is enabled in the kernel. */ |
|
54 |
static dbus_bool_t selinux_enabled = FALSE; |
|
55 |
||
56 |
/* Store an avc_entry_ref to speed AVC decisions. */ |
|
57 |
static struct avc_entry_ref aeref; |
|
58 |
||
59 |
/* Store the SID of the bus itself to use as the default. */ |
|
60 |
static security_id_t bus_sid = SECSID_WILD; |
|
61 |
||
62 |
/* Thread to listen for SELinux status changes via netlink. */ |
|
63 |
static pthread_t avc_notify_thread; |
|
64 |
||
65 |
/* Prototypes for AVC callback functions. */ |
|
66 |
static void log_callback (const char *fmt, ...); |
|
67 |
static void log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft); |
|
68 |
static void *avc_create_thread (void (*run) (void)); |
|
69 |
static void avc_stop_thread (void *thread); |
|
70 |
static void *avc_alloc_lock (void); |
|
71 |
static void avc_get_lock (void *lock); |
|
72 |
static void avc_release_lock (void *lock); |
|
73 |
static void avc_free_lock (void *lock); |
|
74 |
||
75 |
/* AVC callback structures for use in avc_init. */ |
|
76 |
static const struct avc_memory_callback mem_cb = |
|
77 |
{ |
|
78 |
.func_malloc = dbus_malloc, |
|
79 |
.func_free = dbus_free |
|
80 |
}; |
|
81 |
static const struct avc_log_callback log_cb = |
|
82 |
{ |
|
83 |
.func_log = log_callback, |
|
84 |
.func_audit = log_audit_callback |
|
85 |
}; |
|
86 |
static const struct avc_thread_callback thread_cb = |
|
87 |
{ |
|
88 |
.func_create_thread = avc_create_thread, |
|
89 |
.func_stop_thread = avc_stop_thread |
|
90 |
}; |
|
91 |
static const struct avc_lock_callback lock_cb = |
|
92 |
{ |
|
93 |
.func_alloc_lock = avc_alloc_lock, |
|
94 |
.func_get_lock = avc_get_lock, |
|
95 |
.func_release_lock = avc_release_lock, |
|
96 |
.func_free_lock = avc_free_lock |
|
97 |
}; |
|
98 |
#endif /* HAVE_SELINUX */ |
|
99 |
||
100 |
/** |
|
101 |
* Log callback to log denial messages from the AVC. |
|
102 |
* This is used in avc_init. Logs to both standard |
|
103 |
* error and syslogd. |
|
104 |
* |
|
105 |
* @param fmt the format string |
|
106 |
* @param variable argument list |
|
107 |
*/ |
|
108 |
#ifdef HAVE_SELINUX |
|
109 |
static void |
|
110 |
log_callback (const char *fmt, ...) |
|
111 |
{ |
|
112 |
va_list ap; |
|
113 |
va_start(ap, fmt); |
|
114 |
vsyslog (LOG_INFO, fmt, ap); |
|
115 |
va_end(ap); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* On a policy reload we need to reparse the SELinux configuration file, since |
|
120 |
* this could have changed. Send a SIGHUP to reload all configs. |
|
121 |
*/ |
|
122 |
static int |
|
123 |
policy_reload_callback (u_int32_t event, security_id_t ssid, |
|
124 |
security_id_t tsid, security_class_t tclass, |
|
125 |
access_vector_t perms, access_vector_t *out_retained) |
|
126 |
{ |
|
127 |
if (event == AVC_CALLBACK_RESET) |
|
128 |
return raise (SIGHUP); |
|
129 |
||
130 |
return 0; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Log any auxiliary data |
|
135 |
*/ |
|
136 |
static void |
|
137 |
log_audit_callback (void *data, security_class_t class, char *buf, size_t bufleft) |
|
138 |
{ |
|
139 |
DBusString *audmsg = data; |
|
140 |
_dbus_string_copy_to_buffer (audmsg, buf, bufleft); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Create thread to notify the AVC of enforcing and policy reload |
|
145 |
* changes via netlink. |
|
146 |
* |
|
147 |
* @param run the thread run function |
|
148 |
* @return pointer to the thread |
|
149 |
*/ |
|
150 |
static void * |
|
151 |
avc_create_thread (void (*run) (void)) |
|
152 |
{ |
|
153 |
int rc; |
|
154 |
||
155 |
rc = pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL); |
|
156 |
if (rc != 0) |
|
157 |
{ |
|
158 |
_dbus_warn ("Failed to start AVC thread: %s\n", _dbus_strerror (rc)); |
|
159 |
exit (1); |
|
160 |
} |
|
161 |
return &avc_notify_thread; |
|
162 |
} |
|
163 |
||
164 |
/* Stop AVC netlink thread. */ |
|
165 |
static void |
|
166 |
avc_stop_thread (void *thread) |
|
167 |
{ |
|
168 |
pthread_cancel (*(pthread_t *) thread); |
|
169 |
} |
|
170 |
||
171 |
/* Allocate a new AVC lock. */ |
|
172 |
static void * |
|
173 |
avc_alloc_lock (void) |
|
174 |
{ |
|
175 |
pthread_mutex_t *avc_mutex; |
|
176 |
||
177 |
avc_mutex = dbus_new (pthread_mutex_t, 1); |
|
178 |
if (avc_mutex == NULL) |
|
179 |
{ |
|
180 |
_dbus_warn ("Could not create mutex: %s\n", _dbus_strerror (errno)); |
|
181 |
exit (1); |
|
182 |
} |
|
183 |
pthread_mutex_init (avc_mutex, NULL); |
|
184 |
||
185 |
return avc_mutex; |
|
186 |
} |
|
187 |
||
188 |
/* Acquire an AVC lock. */ |
|
189 |
static void |
|
190 |
avc_get_lock (void *lock) |
|
191 |
{ |
|
192 |
pthread_mutex_lock (lock); |
|
193 |
} |
|
194 |
||
195 |
/* Release an AVC lock. */ |
|
196 |
static void |
|
197 |
avc_release_lock (void *lock) |
|
198 |
{ |
|
199 |
pthread_mutex_unlock (lock); |
|
200 |
} |
|
201 |
||
202 |
/* Free an AVC lock. */ |
|
203 |
static void |
|
204 |
avc_free_lock (void *lock) |
|
205 |
{ |
|
206 |
pthread_mutex_destroy (lock); |
|
207 |
dbus_free (lock); |
|
208 |
} |
|
209 |
#endif /* HAVE_SELINUX */ |
|
210 |
||
211 |
/** |
|
212 |
* Return whether or not SELinux is enabled; must be |
|
213 |
* called after bus_selinux_init. |
|
214 |
*/ |
|
215 |
dbus_bool_t |
|
216 |
bus_selinux_enabled (void) |
|
217 |
{ |
|
218 |
#ifdef HAVE_SELINUX |
|
219 |
return selinux_enabled; |
|
220 |
#else |
|
221 |
return FALSE; |
|
222 |
#endif /* HAVE_SELINUX */ |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Do early initialization; determine whether SELinux is enabled. |
|
227 |
*/ |
|
228 |
dbus_bool_t |
|
229 |
bus_selinux_pre_init (void) |
|
230 |
{ |
|
231 |
#ifdef HAVE_SELINUX |
|
232 |
int r; |
|
233 |
_dbus_assert (bus_sid == SECSID_WILD); |
|
234 |
||
235 |
/* Determine if we are running an SELinux kernel. */ |
|
236 |
r = is_selinux_enabled (); |
|
237 |
if (r < 0) |
|
238 |
{ |
|
239 |
_dbus_warn ("Could not tell if SELinux is enabled: %s\n", |
|
240 |
_dbus_strerror (errno)); |
|
241 |
return FALSE; |
|
242 |
} |
|
243 |
||
244 |
selinux_enabled = r != 0; |
|
245 |
return TRUE; |
|
246 |
#else |
|
247 |
return TRUE; |
|
248 |
#endif |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Initialize the user space access vector cache (AVC) for D-Bus and set up |
|
253 |
* logging callbacks. |
|
254 |
*/ |
|
255 |
dbus_bool_t |
|
256 |
bus_selinux_full_init (void) |
|
257 |
{ |
|
258 |
#ifdef HAVE_SELINUX |
|
259 |
char *bus_context; |
|
260 |
||
261 |
_dbus_assert (bus_sid == SECSID_WILD); |
|
262 |
||
263 |
if (!selinux_enabled) |
|
264 |
{ |
|
265 |
_dbus_verbose ("SELinux not enabled in this kernel.\n"); |
|
266 |
return TRUE; |
|
267 |
} |
|
268 |
||
269 |
_dbus_verbose ("SELinux is enabled in this kernel.\n"); |
|
270 |
||
271 |
avc_entry_ref_init (&aeref); |
|
272 |
if (avc_init ("avc", &mem_cb, &log_cb, &thread_cb, &lock_cb) < 0) |
|
273 |
{ |
|
274 |
_dbus_warn ("Failed to start Access Vector Cache (AVC).\n"); |
|
275 |
return FALSE; |
|
276 |
} |
|
277 |
else |
|
278 |
{ |
|
279 |
openlog ("dbus", LOG_PERROR, LOG_USER); |
|
280 |
_dbus_verbose ("Access Vector Cache (AVC) started.\n"); |
|
281 |
} |
|
282 |
||
283 |
if (avc_add_callback (policy_reload_callback, AVC_CALLBACK_RESET, |
|
284 |
NULL, NULL, 0, 0) < 0) |
|
285 |
{ |
|
286 |
_dbus_warn ("Failed to add policy reload callback: %s\n", |
|
287 |
_dbus_strerror (errno)); |
|
288 |
avc_destroy (); |
|
289 |
return FALSE; |
|
290 |
} |
|
291 |
||
292 |
bus_context = NULL; |
|
293 |
bus_sid = SECSID_WILD; |
|
294 |
||
295 |
if (getcon (&bus_context) < 0) |
|
296 |
{ |
|
297 |
_dbus_verbose ("Error getting context of bus: %s\n", |
|
298 |
_dbus_strerror (errno)); |
|
299 |
return FALSE; |
|
300 |
} |
|
301 |
||
302 |
if (avc_context_to_sid (bus_context, &bus_sid) < 0) |
|
303 |
{ |
|
304 |
_dbus_verbose ("Error getting SID from bus context: %s\n", |
|
305 |
_dbus_strerror (errno)); |
|
306 |
freecon (bus_context); |
|
307 |
return FALSE; |
|
308 |
} |
|
309 |
||
310 |
freecon (bus_context); |
|
311 |
||
312 |
return TRUE; |
|
313 |
#else |
|
314 |
return TRUE; |
|
315 |
#endif /* HAVE_SELINUX */ |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Decrement SID reference count. |
|
320 |
* |
|
321 |
* @param sid the SID to decrement |
|
322 |
*/ |
|
323 |
void |
|
324 |
bus_selinux_id_unref (BusSELinuxID *sid) |
|
325 |
{ |
|
326 |
#ifdef HAVE_SELINUX |
|
327 |
if (!selinux_enabled) |
|
328 |
return; |
|
329 |
||
330 |
_dbus_assert (sid != NULL); |
|
331 |
||
332 |
sidput (SELINUX_SID_FROM_BUS (sid)); |
|
333 |
#endif /* HAVE_SELINUX */ |
|
334 |
} |
|
335 |
||
336 |
void |
|
337 |
bus_selinux_id_ref (BusSELinuxID *sid) |
|
338 |
{ |
|
339 |
#ifdef HAVE_SELINUX |
|
340 |
if (!selinux_enabled) |
|
341 |
return; |
|
342 |
||
343 |
_dbus_assert (sid != NULL); |
|
344 |
||
345 |
sidget (SELINUX_SID_FROM_BUS (sid)); |
|
346 |
#endif /* HAVE_SELINUX */ |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* Determine if the SELinux security policy allows the given sender |
|
351 |
* security context to go to the given recipient security context. |
|
352 |
* This function determines if the requested permissions are to be |
|
353 |
* granted from the connection to the message bus or to another |
|
354 |
* optionally supplied security identifier (e.g. for a service |
|
355 |
* context). Currently these permissions are either send_msg or |
|
356 |
* acquire_svc in the dbus class. |
|
357 |
* |
|
358 |
* @param sender_sid source security context |
|
359 |
* @param override_sid is the target security context. If SECSID_WILD this will |
|
360 |
* use the context of the bus itself (e.g. the default). |
|
361 |
* @param target_class is the target security class. |
|
362 |
* @param requested is the requested permissions. |
|
363 |
* @returns #TRUE if security policy allows the send. |
|
364 |
*/ |
|
365 |
#ifdef HAVE_SELINUX |
|
366 |
static dbus_bool_t |
|
367 |
bus_selinux_check (BusSELinuxID *sender_sid, |
|
368 |
BusSELinuxID *override_sid, |
|
369 |
security_class_t target_class, |
|
370 |
access_vector_t requested, |
|
371 |
DBusString *auxdata) |
|
372 |
{ |
|
373 |
if (!selinux_enabled) |
|
374 |
return TRUE; |
|
375 |
||
376 |
/* Make the security check. AVC checks enforcing mode here as well. */ |
|
377 |
if (avc_has_perm (SELINUX_SID_FROM_BUS (sender_sid), |
|
378 |
override_sid ? |
|
379 |
SELINUX_SID_FROM_BUS (override_sid) : |
|
380 |
SELINUX_SID_FROM_BUS (bus_sid), |
|
381 |
target_class, requested, &aeref, auxdata) < 0) |
|
382 |
{ |
|
383 |
_dbus_verbose ("SELinux denying due to security policy.\n"); |
|
384 |
return FALSE; |
|
385 |
} |
|
386 |
else |
|
387 |
return TRUE; |
|
388 |
} |
|
389 |
#endif /* HAVE_SELINUX */ |
|
390 |
||
391 |
/** |
|
392 |
* Returns true if the given connection can acquire a service, |
|
393 |
* assuming the given security ID is needed for that service. |
|
394 |
* |
|
395 |
* @param connection connection that wants to own the service |
|
396 |
* @param service_sid the SID of the service from the table |
|
397 |
* @returns #TRUE if acquire is permitted. |
|
398 |
*/ |
|
399 |
dbus_bool_t |
|
400 |
bus_selinux_allows_acquire_service (DBusConnection *connection, |
|
401 |
BusSELinuxID *service_sid, |
|
402 |
const char *service_name, |
|
403 |
DBusError *error) |
|
404 |
{ |
|
405 |
#ifdef HAVE_SELINUX |
|
406 |
BusSELinuxID *connection_sid; |
|
407 |
unsigned long spid; |
|
408 |
DBusString auxdata; |
|
409 |
dbus_bool_t ret; |
|
410 |
||
411 |
if (!selinux_enabled) |
|
412 |
return TRUE; |
|
413 |
||
414 |
connection_sid = bus_connection_get_selinux_id (connection); |
|
415 |
if (!dbus_connection_get_unix_process_id (connection, &spid)) |
|
416 |
spid = 0; |
|
417 |
||
418 |
if (!_dbus_string_init (&auxdata)) |
|
419 |
goto oom; |
|
420 |
||
421 |
if (!_dbus_string_append (&auxdata, "service=")) |
|
422 |
goto oom; |
|
423 |
||
424 |
if (!_dbus_string_append (&auxdata, service_name)) |
|
425 |
goto oom; |
|
426 |
||
427 |
if (spid) |
|
428 |
{ |
|
429 |
if (!_dbus_string_append (&auxdata, " spid=")) |
|
430 |
goto oom; |
|
431 |
||
432 |
if (!_dbus_string_append_uint (&auxdata, spid)) |
|
433 |
goto oom; |
|
434 |
} |
|
435 |
||
436 |
ret = bus_selinux_check (connection_sid, |
|
437 |
service_sid, |
|
438 |
SECCLASS_DBUS, |
|
439 |
DBUS__ACQUIRE_SVC, |
|
440 |
&auxdata); |
|
441 |
||
442 |
_dbus_string_free (&auxdata); |
|
443 |
return ret; |
|
444 |
||
445 |
oom: |
|
446 |
_dbus_string_free (&auxdata); |
|
447 |
BUS_SET_OOM (error); |
|
448 |
return FALSE; |
|
449 |
||
450 |
#else |
|
451 |
return TRUE; |
|
452 |
#endif /* HAVE_SELINUX */ |
|
453 |
} |
|
454 |
||
455 |
/** |
|
456 |
* Check if SELinux security controls allow the message to be sent to a |
|
457 |
* particular connection based on the security context of the sender and |
|
458 |
* that of the receiver. The destination connection need not be the |
|
459 |
* addressed recipient, it could be an "eavesdropper" |
|
460 |
* |
|
461 |
* @param sender the sender of the message. |
|
462 |
* @param proposed_recipient the connection the message is to be sent to. |
|
463 |
* @returns whether to allow the send |
|
464 |
*/ |
|
465 |
dbus_bool_t |
|
466 |
bus_selinux_allows_send (DBusConnection *sender, |
|
467 |
DBusConnection *proposed_recipient, |
|
468 |
const char *msgtype, |
|
469 |
const char *interface, |
|
470 |
const char *member, |
|
471 |
const char *error_name, |
|
472 |
const char *destination, |
|
473 |
DBusError *error) |
|
474 |
{ |
|
475 |
#ifdef HAVE_SELINUX |
|
476 |
BusSELinuxID *recipient_sid; |
|
477 |
BusSELinuxID *sender_sid; |
|
478 |
unsigned long spid, tpid; |
|
479 |
DBusString auxdata; |
|
480 |
dbus_bool_t ret; |
|
481 |
dbus_bool_t string_alloced; |
|
482 |
||
483 |
if (!selinux_enabled) |
|
484 |
return TRUE; |
|
485 |
||
486 |
if (!sender || !dbus_connection_get_unix_process_id (sender, &spid)) |
|
487 |
spid = 0; |
|
488 |
if (!proposed_recipient || !dbus_connection_get_unix_process_id (proposed_recipient, &tpid)) |
|
489 |
tpid = 0; |
|
490 |
||
491 |
string_alloced = FALSE; |
|
492 |
if (!_dbus_string_init (&auxdata)) |
|
493 |
goto oom; |
|
494 |
string_alloced = TRUE; |
|
495 |
||
496 |
if (!_dbus_string_append (&auxdata, "msgtype=")) |
|
497 |
goto oom; |
|
498 |
||
499 |
if (!_dbus_string_append (&auxdata, msgtype)) |
|
500 |
goto oom; |
|
501 |
||
502 |
if (interface) |
|
503 |
{ |
|
504 |
if (!_dbus_string_append (&auxdata, " interface=")) |
|
505 |
goto oom; |
|
506 |
if (!_dbus_string_append (&auxdata, interface)) |
|
507 |
goto oom; |
|
508 |
} |
|
509 |
||
510 |
if (member) |
|
511 |
{ |
|
512 |
if (!_dbus_string_append (&auxdata, " member=")) |
|
513 |
goto oom; |
|
514 |
if (!_dbus_string_append (&auxdata, member)) |
|
515 |
goto oom; |
|
516 |
} |
|
517 |
||
518 |
if (error_name) |
|
519 |
{ |
|
520 |
if (!_dbus_string_append (&auxdata, " error_name=")) |
|
521 |
goto oom; |
|
522 |
if (!_dbus_string_append (&auxdata, error_name)) |
|
523 |
goto oom; |
|
524 |
} |
|
525 |
||
526 |
if (destination) |
|
527 |
{ |
|
528 |
if (!_dbus_string_append (&auxdata, " dest=")) |
|
529 |
goto oom; |
|
530 |
if (!_dbus_string_append (&auxdata, destination)) |
|
531 |
goto oom; |
|
532 |
} |
|
533 |
||
534 |
if (spid) |
|
535 |
{ |
|
536 |
if (!_dbus_string_append (&auxdata, " spid=")) |
|
537 |
goto oom; |
|
538 |
||
539 |
if (!_dbus_string_append_uint (&auxdata, spid)) |
|
540 |
goto oom; |
|
541 |
} |
|
542 |
||
543 |
if (tpid) |
|
544 |
{ |
|
545 |
if (!_dbus_string_append (&auxdata, " tpid=")) |
|
546 |
goto oom; |
|
547 |
||
548 |
if (!_dbus_string_append_uint (&auxdata, tpid)) |
|
549 |
goto oom; |
|
550 |
} |
|
551 |
||
552 |
sender_sid = bus_connection_get_selinux_id (sender); |
|
553 |
/* A NULL proposed_recipient means the bus itself. */ |
|
554 |
if (proposed_recipient) |
|
555 |
recipient_sid = bus_connection_get_selinux_id (proposed_recipient); |
|
556 |
else |
|
557 |
recipient_sid = BUS_SID_FROM_SELINUX (bus_sid); |
|
558 |
||
559 |
ret = bus_selinux_check (sender_sid, |
|
560 |
recipient_sid, |
|
561 |
SECCLASS_DBUS, |
|
562 |
DBUS__SEND_MSG, |
|
563 |
&auxdata); |
|
564 |
||
565 |
_dbus_string_free (&auxdata); |
|
566 |
||
567 |
return ret; |
|
568 |
||
569 |
oom: |
|
570 |
if (string_alloced) |
|
571 |
_dbus_string_free (&auxdata); |
|
572 |
BUS_SET_OOM (error); |
|
573 |
return FALSE; |
|
574 |
||
575 |
#else |
|
576 |
return TRUE; |
|
577 |
#endif /* HAVE_SELINUX */ |
|
578 |
} |
|
579 |
||
580 |
dbus_bool_t |
|
581 |
bus_selinux_append_context (DBusMessage *message, |
|
582 |
BusSELinuxID *sid, |
|
583 |
DBusError *error) |
|
584 |
{ |
|
585 |
#ifdef HAVE_SELINUX |
|
586 |
char *context; |
|
587 |
||
588 |
if (avc_sid_to_context (SELINUX_SID_FROM_BUS (sid), &context) < 0) |
|
589 |
{ |
|
590 |
if (errno == ENOMEM) |
|
591 |
BUS_SET_OOM (error); |
|
592 |
else |
|
593 |
dbus_set_error (error, DBUS_ERROR_FAILED, |
|
594 |
"Error getting context from SID: %s\n", |
|
595 |
_dbus_strerror (errno)); |
|
596 |
return FALSE; |
|
597 |
} |
|
598 |
if (!dbus_message_append_args (message, |
|
599 |
DBUS_TYPE_ARRAY, |
|
600 |
DBUS_TYPE_BYTE, |
|
601 |
&context, |
|
602 |
strlen (context), |
|
603 |
DBUS_TYPE_INVALID)) |
|
604 |
{ |
|
605 |
_DBUS_SET_OOM (error); |
|
606 |
return FALSE; |
|
607 |
} |
|
608 |
freecon (context); |
|
609 |
return TRUE; |
|
610 |
#else |
|
611 |
return TRUE; |
|
612 |
#endif |
|
613 |
} |
|
614 |
||
615 |
/** |
|
616 |
* Gets the security context of a connection to the bus. It is up to |
|
617 |
* the caller to freecon() when they are done. |
|
618 |
* |
|
619 |
* @param connection the connection to get the context of. |
|
620 |
* @param con the location to store the security context. |
|
621 |
* @returns #TRUE if context is successfully obtained. |
|
622 |
*/ |
|
623 |
#ifdef HAVE_SELINUX |
|
624 |
static dbus_bool_t |
|
625 |
bus_connection_read_selinux_context (DBusConnection *connection, |
|
626 |
char **con) |
|
627 |
{ |
|
628 |
int fd; |
|
629 |
||
630 |
if (!selinux_enabled) |
|
631 |
return FALSE; |
|
632 |
||
633 |
_dbus_assert (connection != NULL); |
|
634 |
||
635 |
if (!dbus_connection_get_unix_fd (connection, &fd)) |
|
636 |
{ |
|
637 |
_dbus_verbose ("Failed to get file descriptor of socket.\n"); |
|
638 |
return FALSE; |
|
639 |
} |
|
640 |
||
641 |
if (getpeercon (fd, con) < 0) |
|
642 |
{ |
|
643 |
_dbus_verbose ("Error getting context of socket peer: %s\n", |
|
644 |
_dbus_strerror (errno)); |
|
645 |
return FALSE; |
|
646 |
} |
|
647 |
||
648 |
_dbus_verbose ("Successfully read connection context.\n"); |
|
649 |
return TRUE; |
|
650 |
} |
|
651 |
#endif /* HAVE_SELINUX */ |
|
652 |
||
653 |
/** |
|
654 |
* Read the SELinux ID from the connection. |
|
655 |
* |
|
656 |
* @param connection the connection to read from |
|
657 |
* @returns the SID if successfully determined, #NULL otherwise. |
|
658 |
*/ |
|
659 |
BusSELinuxID* |
|
660 |
bus_selinux_init_connection_id (DBusConnection *connection, |
|
661 |
DBusError *error) |
|
662 |
{ |
|
663 |
#ifdef HAVE_SELINUX |
|
664 |
char *con; |
|
665 |
security_id_t sid; |
|
666 |
||
667 |
if (!selinux_enabled) |
|
668 |
return NULL; |
|
669 |
||
670 |
if (!bus_connection_read_selinux_context (connection, &con)) |
|
671 |
{ |
|
672 |
dbus_set_error (error, DBUS_ERROR_FAILED, |
|
673 |
"Failed to read an SELinux context from connection"); |
|
674 |
_dbus_verbose ("Error getting peer context.\n"); |
|
675 |
return NULL; |
|
676 |
} |
|
677 |
||
678 |
_dbus_verbose ("Converting context to SID to store on connection\n"); |
|
679 |
||
680 |
if (avc_context_to_sid (con, &sid) < 0) |
|
681 |
{ |
|
682 |
if (errno == ENOMEM) |
|
683 |
BUS_SET_OOM (error); |
|
684 |
else |
|
685 |
dbus_set_error (error, DBUS_ERROR_FAILED, |
|
686 |
"Error getting SID from context \"%s\": %s\n", |
|
687 |
con, _dbus_strerror (errno)); |
|
688 |
||
689 |
_dbus_warn ("Error getting SID from context \"%s\": %s\n", |
|
690 |
con, _dbus_strerror (errno)); |
|
691 |
||
692 |
freecon (con); |
|
693 |
return NULL; |
|
694 |
} |
|
695 |
||
696 |
freecon (con); |
|
697 |
return BUS_SID_FROM_SELINUX (sid); |
|
698 |
#else |
|
699 |
return NULL; |
|
700 |
#endif /* HAVE_SELINUX */ |
|
701 |
} |
|
702 |
||
703 |
||
704 |
/** |
|
705 |
* Function for freeing hash table data. These SIDs |
|
706 |
* should no longer be referenced. |
|
707 |
*/ |
|
708 |
static void |
|
709 |
bus_selinux_id_table_free_value (BusSELinuxID *sid) |
|
710 |
{ |
|
711 |
#ifdef HAVE_SELINUX |
|
712 |
/* NULL sometimes due to how DBusHashTable works */ |
|
713 |
if (sid) |
|
714 |
bus_selinux_id_unref (sid); |
|
715 |
#endif /* HAVE_SELINUX */ |
|
716 |
} |
|
717 |
||
718 |
/** |
|
719 |
* Creates a new table mapping service names to security ID. |
|
720 |
* A security ID is a "compiled" security context, a security |
|
721 |
* context is just a string. |
|
722 |
* |
|
723 |
* @returns the new table or #NULL if no memory |
|
724 |
*/ |
|
725 |
DBusHashTable* |
|
726 |
bus_selinux_id_table_new (void) |
|
727 |
{ |
|
728 |
return _dbus_hash_table_new (DBUS_HASH_STRING, |
|
729 |
(DBusFreeFunction) dbus_free, |
|
730 |
(DBusFreeFunction) bus_selinux_id_table_free_value); |
|
731 |
} |
|
732 |
||
733 |
/** |
|
734 |
* Hashes a service name and service context into the service SID |
|
735 |
* table as a string and a SID. |
|
736 |
* |
|
737 |
* @param service_name is the name of the service. |
|
738 |
* @param service_context is the context of the service. |
|
739 |
* @param service_table is the table to hash them into. |
|
740 |
* @return #FALSE if not enough memory |
|
741 |
*/ |
|
742 |
dbus_bool_t |
|
743 |
bus_selinux_id_table_insert (DBusHashTable *service_table, |
|
744 |
const char *service_name, |
|
745 |
const char *service_context) |
|
746 |
{ |
|
747 |
#ifdef HAVE_SELINUX |
|
748 |
dbus_bool_t retval; |
|
749 |
security_id_t sid; |
|
750 |
char *key; |
|
751 |
||
752 |
if (!selinux_enabled) |
|
753 |
return TRUE; |
|
754 |
||
755 |
sid = SECSID_WILD; |
|
756 |
retval = FALSE; |
|
757 |
||
758 |
key = _dbus_strdup (service_name); |
|
759 |
if (key == NULL) |
|
760 |
return retval; |
|
761 |
||
762 |
if (avc_context_to_sid ((char *) service_context, &sid) < 0) |
|
763 |
{ |
|
764 |
if (errno == ENOMEM) |
|
765 |
{ |
|
766 |
dbus_free (key); |
|
767 |
return FALSE; |
|
768 |
} |
|
769 |
||
770 |
_dbus_warn ("Error getting SID from context \"%s\": %s\n", |
|
771 |
(char *) service_context, |
|
772 |
_dbus_strerror (errno)); |
|
773 |
goto out; |
|
774 |
} |
|
775 |
||
776 |
if (!_dbus_hash_table_insert_string (service_table, |
|
777 |
key, |
|
778 |
BUS_SID_FROM_SELINUX (sid))) |
|
779 |
goto out; |
|
780 |
||
781 |
_dbus_verbose ("Parsed \tservice: %s \n\t\tcontext: %s\n", |
|
782 |
key, |
|
783 |
sid->ctx); |
|
784 |
||
785 |
/* These are owned by the hash, so clear them to avoid unref */ |
|
786 |
key = NULL; |
|
787 |
sid = SECSID_WILD; |
|
788 |
||
789 |
retval = TRUE; |
|
790 |
||
791 |
out: |
|
792 |
if (sid != SECSID_WILD) |
|
793 |
sidput (sid); |
|
794 |
||
795 |
if (key) |
|
796 |
dbus_free (key); |
|
797 |
||
798 |
return retval; |
|
799 |
#else |
|
800 |
return TRUE; |
|
801 |
#endif /* HAVE_SELINUX */ |
|
802 |
} |
|
803 |
||
804 |
||
805 |
/** |
|
806 |
* Find the security identifier associated with a particular service |
|
807 |
* name. Return a pointer to this SID, or #NULL/SECSID_WILD if the |
|
808 |
* service is not found in the hash table. This should be nearly a |
|
809 |
* constant time operation. If SELinux support is not available, |
|
810 |
* always return NULL. |
|
811 |
* |
|
812 |
* @param service_table the hash table to check for service name. |
|
813 |
* @param service_name the name of the service to look for. |
|
814 |
* @returns the SELinux ID associated with the service |
|
815 |
*/ |
|
816 |
BusSELinuxID* |
|
817 |
bus_selinux_id_table_lookup (DBusHashTable *service_table, |
|
818 |
const DBusString *service_name) |
|
819 |
{ |
|
820 |
#ifdef HAVE_SELINUX |
|
821 |
security_id_t sid; |
|
822 |
||
823 |
sid = SECSID_WILD; /* default context */ |
|
824 |
||
825 |
if (!selinux_enabled) |
|
826 |
return NULL; |
|
827 |
||
828 |
_dbus_verbose ("Looking up service SID for %s\n", |
|
829 |
_dbus_string_get_const_data (service_name)); |
|
830 |
||
831 |
sid = _dbus_hash_table_lookup_string (service_table, |
|
832 |
_dbus_string_get_const_data (service_name)); |
|
833 |
||
834 |
if (sid == SECSID_WILD) |
|
835 |
_dbus_verbose ("Service %s not found\n", |
|
836 |
_dbus_string_get_const_data (service_name)); |
|
837 |
else |
|
838 |
_dbus_verbose ("Service %s found\n", |
|
839 |
_dbus_string_get_const_data (service_name)); |
|
840 |
||
841 |
return BUS_SID_FROM_SELINUX (sid); |
|
842 |
#endif /* HAVE_SELINUX */ |
|
843 |
return NULL; |
|
844 |
} |
|
845 |
||
846 |
/** |
|
847 |
* Get the SELinux policy root. This is used to find the D-Bus |
|
848 |
* specific config file within the policy. |
|
849 |
*/ |
|
850 |
const char * |
|
851 |
bus_selinux_get_policy_root (void) |
|
852 |
{ |
|
853 |
#ifdef HAVE_SELINUX |
|
854 |
return selinux_policy_root (); |
|
855 |
#else |
|
856 |
return NULL; |
|
857 |
#endif /* HAVE_SELINUX */ |
|
858 |
} |
|
859 |
||
860 |
/** |
|
861 |
* For debugging: Print out the current hash table of service SIDs. |
|
862 |
*/ |
|
863 |
void |
|
864 |
bus_selinux_id_table_print (DBusHashTable *service_table) |
|
865 |
{ |
|
866 |
#ifdef DBUS_ENABLE_VERBOSE_MODE |
|
867 |
#ifdef HAVE_SELINUX |
|
868 |
DBusHashIter iter; |
|
869 |
||
870 |
if (!selinux_enabled) |
|
871 |
return; |
|
872 |
||
873 |
_dbus_verbose ("Service SID Table:\n"); |
|
874 |
_dbus_hash_iter_init (service_table, &iter); |
|
875 |
while (_dbus_hash_iter_next (&iter)) |
|
876 |
{ |
|
877 |
const char *key = _dbus_hash_iter_get_string_key (&iter); |
|
878 |
security_id_t sid = _dbus_hash_iter_get_value (&iter); |
|
879 |
_dbus_verbose ("The key is %s\n", key); |
|
880 |
_dbus_verbose ("The context is %s\n", sid->ctx); |
|
881 |
_dbus_verbose ("The refcount is %d\n", sid->refcnt); |
|
882 |
} |
|
883 |
#endif /* HAVE_SELINUX */ |
|
884 |
#endif /* DBUS_ENABLE_VERBOSE_MODE */ |
|
885 |
} |
|
886 |
||
887 |
||
888 |
#ifdef DBUS_ENABLE_VERBOSE_MODE |
|
889 |
#ifdef HAVE_SELINUX |
|
890 |
/** |
|
891 |
* Print out some AVC statistics. |
|
892 |
*/ |
|
893 |
static void |
|
894 |
bus_avc_print_stats (void) |
|
895 |
{ |
|
896 |
struct avc_cache_stats cstats; |
|
897 |
||
898 |
if (!selinux_enabled) |
|
899 |
return; |
|
900 |
||
901 |
_dbus_verbose ("AVC Statistics:\n"); |
|
902 |
avc_cache_stats (&cstats); |
|
903 |
avc_av_stats (); |
|
904 |
_dbus_verbose ("AVC Cache Statistics:\n"); |
|
905 |
_dbus_verbose ("Entry lookups: %d\n", cstats.entry_lookups); |
|
906 |
_dbus_verbose ("Entry hits: %d\n", cstats.entry_hits); |
|
907 |
_dbus_verbose ("Entry misses %d\n", cstats.entry_misses); |
|
908 |
_dbus_verbose ("Entry discards: %d\n", cstats.entry_discards); |
|
909 |
_dbus_verbose ("CAV lookups: %d\n", cstats.cav_lookups); |
|
910 |
_dbus_verbose ("CAV hits: %d\n", cstats.cav_hits); |
|
911 |
_dbus_verbose ("CAV probes: %d\n", cstats.cav_probes); |
|
912 |
_dbus_verbose ("CAV misses: %d\n", cstats.cav_misses); |
|
913 |
} |
|
914 |
#endif /* HAVE_SELINUX */ |
|
915 |
#endif /* DBUS_ENABLE_VERBOSE_MODE */ |
|
916 |
||
917 |
||
918 |
/** |
|
919 |
* Destroy the AVC before we terminate. |
|
920 |
*/ |
|
921 |
void |
|
922 |
bus_selinux_shutdown (void) |
|
923 |
{ |
|
924 |
#ifdef HAVE_SELINUX |
|
925 |
if (!selinux_enabled) |
|
926 |
return; |
|
927 |
||
928 |
_dbus_verbose ("AVC shutdown\n"); |
|
929 |
||
930 |
if (bus_sid != SECSID_WILD) |
|
931 |
{ |
|
932 |
sidput (bus_sid); |
|
933 |
bus_sid = SECSID_WILD; |
|
934 |
||
935 |
#ifdef DBUS_ENABLE_VERBOSE_MODE |
|
936 |
bus_avc_print_stats (); |
|
937 |
#endif /* DBUS_ENABLE_VERBOSE_MODE */ |
|
938 |
||
939 |
avc_destroy (); |
|
940 |
} |
|
941 |
#endif /* HAVE_SELINUX */ |
|
942 |
} |
|
943 |