| 1 |
/* |
| 2 |
* BusSELinuxRegistry Helpers |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-rbtree.h> |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <selinux/selinux.h> |
| 8 |
#include <selinux/avc.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include "util/audit.h" |
| 11 |
#include "util/error.h" |
| 12 |
#include "util/log.h" |
| 13 |
#include "util/ref.h" |
| 14 |
#include "util/selinux.h" |
| 15 |
|
| 16 |
struct BusSELinuxRegistry { |
| 17 |
_Atomic unsigned long n_refs; |
| 18 |
const char *fallback_context; |
| 19 |
CRBTree names; |
| 20 |
}; |
| 21 |
|
| 22 |
struct BusSELinuxName { |
| 23 |
char *context; |
| 24 |
CRBNode rb; |
| 25 |
char name[]; |
| 26 |
}; |
| 27 |
|
| 28 |
typedef struct BusSELinuxName BusSELinuxName; |
| 29 |
|
| 30 |
static Log *bus_selinux_log = NULL; |
| 31 |
static bool bus_selinux_avc_open; |
| 32 |
static bool bus_selinux_status_open; |
| 33 |
|
| 34 |
/** |
| 35 |
* bus_selinux_is_enabled() - checks if SELinux is currently enabled |
| 36 |
* |
| 37 |
* Returns: true if SELinux is enabled, false otherwise. |
| 38 |
*/ |
| 39 |
bool bus_selinux_is_enabled(void) { |
| 40 |
return is_selinux_enabled(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* bus_selinux_is_enforcing() - checks if SELinux is in enforcing mode |
| 45 |
* |
| 46 |
* If selinux is not enabled or otherwise unavailable, this will return true. |
| 47 |
* That is, this will only return false, if selinux is enabled and in |
| 48 |
* permissive mode. |
| 49 |
* |
| 50 |
* Returns: true if SELinux is in enforcing mode, false otherwise. |
| 51 |
*/ |
| 52 |
bool bus_selinux_is_enforcing(void) { |
| 53 |
if (bus_selinux_status_open) |
| 54 |
return selinux_status_getenforce() != 0; |
| 55 |
else |
| 56 |
return security_getenforce() != 0; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* bus_selinux_policy_root() - the root directory where the current SELinux policy can be found |
| 61 |
* |
| 62 |
* The current SELinux policy can be found in a different directory depending on the |
| 63 |
* current configuration. |
| 64 |
* |
| 65 |
* Return: a path to the directory, or NULL if it is not known. |
| 66 |
*/ |
| 67 |
const char *bus_selinux_policy_root(void) { |
| 68 |
return selinux_policy_root(); |
| 69 |
} |
| 70 |
|
| 71 |
static BusSELinuxName *bus_selinux_name_free(BusSELinuxName *name) { |
| 72 |
if (!name) |
| 73 |
return NULL; |
| 74 |
|
| 75 |
c_assert(!c_rbnode_is_linked(&name->rb)); |
| 76 |
|
| 77 |
free(name->context); |
| 78 |
free(name); |
| 79 |
|
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
C_DEFINE_CLEANUP(BusSELinuxName *, bus_selinux_name_free); |
| 84 |
|
| 85 |
static int bus_selinux_name_new(const char *name, const char *context, CRBTree *tree, CRBNode *parent, CRBNode **slot) { |
| 86 |
_c_cleanup_(bus_selinux_name_freep) BusSELinuxName *selinux_name = NULL; |
| 87 |
size_t n_name = strlen(name) + 1; |
| 88 |
|
| 89 |
selinux_name = malloc(sizeof(*selinux_name) + n_name); |
| 90 |
if (!selinux_name) |
| 91 |
return error_origin(-ENOMEM); |
| 92 |
selinux_name->rb = (CRBNode)C_RBNODE_INIT(selinux_name->rb); |
| 93 |
selinux_name->context = NULL; |
| 94 |
c_memcpy(selinux_name->name, name, n_name); |
| 95 |
|
| 96 |
selinux_name->context = strdup(context); |
| 97 |
if (!selinux_name->context) |
| 98 |
return error_origin(-ENOMEM); |
| 99 |
|
| 100 |
c_rbtree_add(tree, parent, slot, &selinux_name->rb); |
| 101 |
selinux_name = NULL; |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* bus_selinux_registry_new() - create a new SELinux registry |
| 108 |
* @registryp: pointer to the new registry |
| 109 |
* @fallback_context: fallback security context for queries against this registry |
| 110 |
* |
| 111 |
* A registry contains a set of names associated with security contexts, and |
| 112 |
* a fallback security context that should be used as the context of the broker |
| 113 |
* itself and for names without anything explicitly associated. |
| 114 |
* |
| 115 |
* Return: 0 on success, or a negative error code on failure. |
| 116 |
*/ |
| 117 |
int bus_selinux_registry_new(BusSELinuxRegistry **registryp, const char *fallback_context) { |
| 118 |
_c_cleanup_(bus_selinux_registry_unrefp) BusSELinuxRegistry *registry = NULL; |
| 119 |
size_t n_fallback_context = strlen(fallback_context) + 1; |
| 120 |
|
| 121 |
registry = malloc(sizeof(*registry) + n_fallback_context); |
| 122 |
if (!registry) |
| 123 |
return error_origin(-ENOMEM); |
| 124 |
|
| 125 |
registry->n_refs = REF_INIT; |
| 126 |
registry->fallback_context = (const char *)(registry + 1); |
| 127 |
registry->names = (CRBTree)C_RBTREE_INIT; |
| 128 |
c_memcpy((char *)registry->fallback_context, fallback_context, n_fallback_context); |
| 129 |
|
| 130 |
*registryp = registry; |
| 131 |
registry = NULL; |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
static void bus_selinux_registry_free(_Atomic unsigned long *n_refs, void *userdata) { |
| 136 |
BusSELinuxRegistry *registry = c_container_of(n_refs, BusSELinuxRegistry, n_refs); |
| 137 |
BusSELinuxName *name, *name_safe; |
| 138 |
|
| 139 |
c_rbtree_for_each_entry_safe_postorder_unlink(name, name_safe, ®istry->names, rb) |
| 140 |
bus_selinux_name_free(name); |
| 141 |
|
| 142 |
free(registry); |
| 143 |
} |
| 144 |
|
| 145 |
BusSELinuxRegistry *bus_selinux_registry_ref(BusSELinuxRegistry *registry) { |
| 146 |
if (registry) |
| 147 |
ref_inc(®istry->n_refs); |
| 148 |
|
| 149 |
return registry; |
| 150 |
} |
| 151 |
|
| 152 |
BusSELinuxRegistry *bus_selinux_registry_unref(BusSELinuxRegistry *registry) { |
| 153 |
if (registry) |
| 154 |
ref_dec(®istry->n_refs, bus_selinux_registry_free, NULL); |
| 155 |
|
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
static int name_compare(CRBTree *t, void *k, CRBNode *rb) { |
| 160 |
const char *name = (const char *)k; |
| 161 |
BusSELinuxName *selinux_name = c_container_of(rb, BusSELinuxName, rb); |
| 162 |
|
| 163 |
return strcmp(name, selinux_name->name); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* bus_selinux_registry_add_name() - add a name and its associated security context to the registry |
| 168 |
* @registry: the registry to operate on |
| 169 |
* @name: the add to associate with a security context |
| 170 |
* @context: a valid SELinux security context |
| 171 |
* |
| 172 |
* Associates the given name with the given SELinux security context in the registry. |
| 173 |
* |
| 174 |
* If the name already exists in the registry, it is silently updated with the new |
| 175 |
* security context. |
| 176 |
* |
| 177 |
* Return: 0 on success, or a negative error code on failure. |
| 178 |
*/ |
| 179 |
int bus_selinux_registry_add_name(BusSELinuxRegistry *registry, const char *name, const char *context) { |
| 180 |
CRBNode *parent, **slot; |
| 181 |
int r; |
| 182 |
|
| 183 |
slot = c_rbtree_find_slot(®istry->names, name_compare, name, &parent); |
| 184 |
if (slot) { |
| 185 |
r = bus_selinux_name_new(name, context, ®istry->names, parent, slot); |
| 186 |
if (r) |
| 187 |
return error_trace(r); |
| 188 |
} else { |
| 189 |
BusSELinuxName *selinux_name; |
| 190 |
char *context_new; |
| 191 |
|
| 192 |
selinux_name = c_container_of(parent, BusSELinuxName, rb); |
| 193 |
|
| 194 |
/* The name already exists, simply silently override the context. */ |
| 195 |
context_new = strdup(context); |
| 196 |
if (!context_new) |
| 197 |
return error_origin(-ENOMEM); |
| 198 |
|
| 199 |
free(selinux_name->context); |
| 200 |
selinux_name->context = context_new; |
| 201 |
} |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* bus_selinux_check_own() - check if the given transaction is allowed |
| 208 |
* @registry: SELinux registry to operate on |
| 209 |
* @owner_context: security context of the owner |
| 210 |
* @name: name to be owned |
| 211 |
* |
| 212 |
* Check if the given owner context is allowed to own the given name. |
| 213 |
* |
| 214 |
* The query is performed with the contexts associated with names in the |
| 215 |
* SELinux registry. If no context is associated with a given name the |
| 216 |
* registry-wide fallback context is used instead. |
| 217 |
* |
| 218 |
* The contexts are pinned when peers connect, and as such could in principle |
| 219 |
* become invalid in case a new policy is loaded that does not know the |
| 220 |
* old labels. In this case we treat this as if the ownership request was |
| 221 |
* denied. |
| 222 |
* |
| 223 |
* Return: 0 if the ownership is allowed, SELINUX_E_DENIED if it is not, |
| 224 |
* or a negative error code on failure. |
| 225 |
*/ |
| 226 |
int bus_selinux_check_own(BusSELinuxRegistry *registry, |
| 227 |
const char *owner_context, |
| 228 |
const char *name) { |
| 229 |
BusSELinuxName *selinux_name; |
| 230 |
const char *name_context; |
| 231 |
int r; |
| 232 |
|
| 233 |
if (!is_selinux_enabled()) |
| 234 |
return 0; |
| 235 |
|
| 236 |
selinux_name = c_rbtree_find_entry(®istry->names, name_compare, name, BusSELinuxName, rb); |
| 237 |
if (selinux_name) |
| 238 |
name_context = selinux_name->context; |
| 239 |
else |
| 240 |
name_context = registry->fallback_context; |
| 241 |
|
| 242 |
r = selinux_check_access(owner_context, |
| 243 |
name_context, |
| 244 |
"dbus", |
| 245 |
"acquire_svc", |
| 246 |
NULL); |
| 247 |
if (r < 0 && bus_selinux_is_enforcing()) { |
| 248 |
/* |
| 249 |
* Treat unknown contexts (possibly due to policy reload) |
| 250 |
* as access denied. |
| 251 |
*/ |
| 252 |
if (errno == EACCES || errno == EINVAL) |
| 253 |
return SELINUX_E_DENIED; |
| 254 |
|
| 255 |
return error_origin(-errno); |
| 256 |
} |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* bus_selinux_check_send() - check if the given transaction is allowed |
| 263 |
* @registry: SELinux registry to operate on |
| 264 |
* @sender_context: security context of the sender |
| 265 |
* @receiver_context: security context of the receiver, or NULL |
| 266 |
* |
| 267 |
* Check if the given sender context is allowed to send a message to the |
| 268 |
* given receiver context. If the receiver context is given as NULL, the |
| 269 |
* per-registry fallback context is used instead. |
| 270 |
* |
| 271 |
* The contexts are pinned when peers connect, and as such could in principle |
| 272 |
* become invalid in case a new policy is loaded that does not know the |
| 273 |
* old labels. In this case we treat this as if the transaction was |
| 274 |
* denied. |
| 275 |
* |
| 276 |
* Return: 0 if the transaction is allowed, SELINUX_E_DENIED if it is not, |
| 277 |
* or a negative error code on failure. |
| 278 |
*/ |
| 279 |
int bus_selinux_check_send(BusSELinuxRegistry *registry, |
| 280 |
const char *sender_context, |
| 281 |
const char *receiver_context) { |
| 282 |
int r; |
| 283 |
|
| 284 |
if (!is_selinux_enabled()) |
| 285 |
return 0; |
| 286 |
|
| 287 |
receiver_context = receiver_context ?: registry->fallback_context; |
| 288 |
|
| 289 |
r = selinux_check_access(sender_context, |
| 290 |
receiver_context, |
| 291 |
"dbus", |
| 292 |
"send_msg", |
| 293 |
NULL); |
| 294 |
if (r < 0 && bus_selinux_is_enforcing()) { |
| 295 |
/* |
| 296 |
* Treat unknown contexts (possibly due to policy reload) |
| 297 |
* as access denied. |
| 298 |
*/ |
| 299 |
if (errno == EACCES || errno == EINVAL) |
| 300 |
return SELINUX_E_DENIED; |
| 301 |
|
| 302 |
return error_origin(-errno); |
| 303 |
} |
| 304 |
|
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
static int bus_selinux_log_fn(int type, const char *fmt, ...) { |
| 309 |
_c_cleanup_(c_freep) char *message = NULL; |
| 310 |
const char *loghdr = NULL; |
| 311 |
va_list ap; |
| 312 |
int r, loglvl = 0; |
| 313 |
|
| 314 |
va_start(ap, fmt); |
| 315 |
r = vasprintf(&message, fmt, ap); |
| 316 |
va_end(ap); |
| 317 |
if (r < 0) |
| 318 |
return r; |
| 319 |
|
| 320 |
switch (type) { |
| 321 |
case SELINUX_AVC: |
| 322 |
/* XXX: we don't have access to any context, so can't find |
| 323 |
* the right UID to use, follow dbus-daemon(1) and use our |
| 324 |
* own. */ |
| 325 |
r = util_audit_log(UTIL_AUDIT_TYPE_AVC, message, getuid()); |
| 326 |
if (r == UTIL_AUDIT_E_UNAVAILABLE) { |
| 327 |
loghdr = "selinux/avc"; |
| 328 |
loglvl = LOG_INFO; |
| 329 |
} else if (r) { |
| 330 |
return error_fold(r); |
| 331 |
} |
| 332 |
|
| 333 |
break; |
| 334 |
case SELINUX_POLICYLOAD: |
| 335 |
r = util_audit_log(UTIL_AUDIT_TYPE_POLICYLOAD, message, getuid()); |
| 336 |
if (r == UTIL_AUDIT_E_UNAVAILABLE) { |
| 337 |
loghdr = "selinux/policyload"; |
| 338 |
loglvl = LOG_INFO; |
| 339 |
} else if (r) { |
| 340 |
return error_fold(r); |
| 341 |
} |
| 342 |
|
| 343 |
break; |
| 344 |
case SELINUX_SETENFORCE: |
| 345 |
r = util_audit_log(UTIL_AUDIT_TYPE_MAC_STATUS, message, getuid()); |
| 346 |
if (r == UTIL_AUDIT_E_UNAVAILABLE) { |
| 347 |
loghdr = "selinux/macstatus"; |
| 348 |
loglvl = LOG_INFO; |
| 349 |
} else if (r) { |
| 350 |
return error_fold(r); |
| 351 |
} |
| 352 |
|
| 353 |
break; |
| 354 |
case SELINUX_ERROR: |
| 355 |
loghdr = "selinux/error"; |
| 356 |
loglvl = LOG_ERR; |
| 357 |
break; |
| 358 |
case SELINUX_WARNING: |
| 359 |
loghdr = "selinux/warning"; |
| 360 |
loglvl = LOG_WARNING; |
| 361 |
break; |
| 362 |
case SELINUX_INFO: |
| 363 |
loghdr = "selinux/info"; |
| 364 |
loglvl = LOG_INFO; |
| 365 |
break; |
| 366 |
default: |
| 367 |
loghdr = "selinux/unknown"; |
| 368 |
loglvl = LOG_WARNING; |
| 369 |
break; |
| 370 |
} |
| 371 |
|
| 372 |
if (loghdr && bus_selinux_log) { |
| 373 |
log_append_here(bus_selinux_log, loglvl, 0, NULL); |
| 374 |
r = log_commitf(bus_selinux_log, "%s: %s", loghdr, message); |
| 375 |
if (r) |
| 376 |
return error_fold(r); |
| 377 |
} |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* bus_selinux_init_global() - initialize the global SELinux context |
| 384 |
* @log: log object to use |
| 385 |
* |
| 386 |
* Initialize the global SELinux context. This must be called before any |
| 387 |
* other SELinux function. |
| 388 |
* |
| 389 |
* Return: 0 on success, or a negative error code on failure. |
| 390 |
*/ |
| 391 |
int bus_selinux_init_global(Log *log) { |
| 392 |
int r; |
| 393 |
|
| 394 |
if (!is_selinux_enabled()) |
| 395 |
return 0; |
| 396 |
|
| 397 |
bus_selinux_log = log; |
| 398 |
|
| 399 |
if (!bus_selinux_avc_open) { |
| 400 |
r = avc_open(NULL, 0); |
| 401 |
if (r) { |
| 402 |
r = error_origin(-errno); |
| 403 |
goto error; |
| 404 |
} |
| 405 |
|
| 406 |
bus_selinux_avc_open = true; |
| 407 |
} |
| 408 |
|
| 409 |
if (!bus_selinux_status_open) { |
| 410 |
r = selinux_status_open(0); |
| 411 |
if (r == 0) { |
| 412 |
/* |
| 413 |
* The status page was successfully opened and can now |
| 414 |
* be used for faster selinux status-checks. |
| 415 |
*/ |
| 416 |
bus_selinux_status_open = true; |
| 417 |
} else if (r > 0) { |
| 418 |
/* |
| 419 |
* >0 indicates success but with the netlink-fallback. |
| 420 |
* We didn't request the netlink-fallback, so close the |
| 421 |
* status-page again and treat it as unavailable. |
| 422 |
*/ |
| 423 |
selinux_status_close(); |
| 424 |
} else { |
| 425 |
/* |
| 426 |
* If the status page could not be opened, treat it as |
| 427 |
* unavailable and use the slower fallback functions. |
| 428 |
*/ |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback)bus_selinux_log_fn); |
| 433 |
|
| 434 |
/* XXX: set audit callback to get more metadata in the audit log? */ |
| 435 |
|
| 436 |
return 0; |
| 437 |
|
| 438 |
error: |
| 439 |
bus_selinux_log = NULL; |
| 440 |
return error_trace(r); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* bus_selinux_deinit_global() - deinitialize the global SELinux context |
| 445 |
* |
| 446 |
* Cleans up the resources initialized by bus_selinux_init_global(). This |
| 447 |
* must be called exactly once, after which no more SELinux functions can |
| 448 |
* be called. |
| 449 |
*/ |
| 450 |
void bus_selinux_deinit_global(void) { |
| 451 |
if (!is_selinux_enabled()) |
| 452 |
return; |
| 453 |
|
| 454 |
if (bus_selinux_status_open) { |
| 455 |
selinux_status_close(); |
| 456 |
bus_selinux_status_open = false; |
| 457 |
} |
| 458 |
|
| 459 |
if (bus_selinux_avc_open) { |
| 460 |
avc_destroy(); |
| 461 |
bus_selinux_avc_open = false; |
| 462 |
} |
| 463 |
|
| 464 |
bus_selinux_log = NULL; |
| 465 |
} |