| 1 |
/* |
| 2 |
* D-Bus Policy Converter |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-list.h> |
| 6 |
#include <c-rbtree.h> |
| 7 |
#include <c-stdaux.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <systemd/sd-bus.h> |
| 10 |
#include "dbus/protocol.h" |
| 11 |
#include "launch/config.h" |
| 12 |
#include "launch/policy.h" |
| 13 |
#include "util/common.h" |
| 14 |
#include "util/error.h" |
| 15 |
|
| 16 |
/** |
| 17 |
* policy_record_new_connect() - XXX |
| 18 |
*/ |
| 19 |
int policy_record_new_connect(PolicyRecord **recordp) { |
| 20 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 21 |
|
| 22 |
record = calloc(1, sizeof(*record)); |
| 23 |
if (!record) |
| 24 |
return error_origin(-ENOMEM); |
| 25 |
|
| 26 |
*record = (PolicyRecord)POLICY_RECORD_INIT_CONNECT(*record); |
| 27 |
|
| 28 |
*recordp = record; |
| 29 |
record = NULL; |
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* policy_record_new_own() - XXX |
| 35 |
*/ |
| 36 |
int policy_record_new_own(PolicyRecord **recordp) { |
| 37 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 38 |
|
| 39 |
record = calloc(1, sizeof(*record)); |
| 40 |
if (!record) |
| 41 |
return error_origin(-ENOMEM); |
| 42 |
|
| 43 |
*record = (PolicyRecord)POLICY_RECORD_INIT_OWN(*record); |
| 44 |
|
| 45 |
*recordp = record; |
| 46 |
record = NULL; |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* policy_record_new_xmit() - XXX |
| 52 |
*/ |
| 53 |
int policy_record_new_xmit(PolicyRecord **recordp) { |
| 54 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 55 |
|
| 56 |
record = calloc(1, sizeof(*record)); |
| 57 |
if (!record) |
| 58 |
return error_origin(-ENOMEM); |
| 59 |
|
| 60 |
*record = (PolicyRecord)POLICY_RECORD_INIT_XMIT(*record); |
| 61 |
|
| 62 |
*recordp = record; |
| 63 |
record = NULL; |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* policy_record_new_selinux() - XXX |
| 69 |
*/ |
| 70 |
int policy_record_new_selinux(PolicyRecord **recordp) { |
| 71 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 72 |
|
| 73 |
record = calloc(1, sizeof(*record)); |
| 74 |
if (!record) |
| 75 |
return error_origin(-ENOMEM); |
| 76 |
|
| 77 |
*record = (PolicyRecord)POLICY_RECORD_INIT_SELINUX(*record); |
| 78 |
|
| 79 |
*recordp = record; |
| 80 |
record = NULL; |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* policy_record_free() - XXX |
| 86 |
*/ |
| 87 |
PolicyRecord *policy_record_free(PolicyRecord *record) { |
| 88 |
if (!record) |
| 89 |
return NULL; |
| 90 |
|
| 91 |
c_list_unlink(&record->link); |
| 92 |
free(record); |
| 93 |
|
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
static void policy_record_xmit_trim(PolicyRecord *record) { |
| 98 |
if (record->xmit.name && !strcmp(record->xmit.name, "*")) |
| 99 |
record->xmit.name = NULL; |
| 100 |
if (record->xmit.path && !strcmp(record->xmit.path, "*")) |
| 101 |
record->xmit.path = NULL; |
| 102 |
if (record->xmit.interface && !strcmp(record->xmit.interface, "*")) |
| 103 |
record->xmit.interface = NULL; |
| 104 |
if (record->xmit.member && !strcmp(record->xmit.member, "*")) |
| 105 |
record->xmit.member = NULL; |
| 106 |
} |
| 107 |
|
| 108 |
static void policy_entries_deinit(PolicyEntries *entries) { |
| 109 |
PolicyRecord *record; |
| 110 |
|
| 111 |
while ((record = c_list_first_entry(&entries->recv_list, PolicyRecord, link))) |
| 112 |
policy_record_free(record); |
| 113 |
while ((record = c_list_first_entry(&entries->send_list, PolicyRecord, link))) |
| 114 |
policy_record_free(record); |
| 115 |
while ((record = c_list_first_entry(&entries->own_list, PolicyRecord, link))) |
| 116 |
policy_record_free(record); |
| 117 |
while ((record = c_list_first_entry(&entries->connect_list, PolicyRecord, link))) |
| 118 |
policy_record_free(record); |
| 119 |
} |
| 120 |
|
| 121 |
static bool policy_entries_is_empty(PolicyEntries *entries) { |
| 122 |
return (c_list_is_empty(&entries->connect_list) && |
| 123 |
c_list_is_empty(&entries->own_list) && |
| 124 |
c_list_is_empty(&entries->send_list) && |
| 125 |
c_list_is_empty(&entries->recv_list)); |
| 126 |
} |
| 127 |
|
| 128 |
static int policy_node_compare(CRBTree *t, void *k, CRBNode *n) { |
| 129 |
PolicyNode *node = c_container_of(n, PolicyNode, policy_node); |
| 130 |
uint32_t uidgid = *(uint32_t*)k; |
| 131 |
|
| 132 |
if (uidgid < node->uidgid) |
| 133 |
return -1; |
| 134 |
else if (uidgid > node->uidgid) |
| 135 |
return 1; |
| 136 |
else |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
static PolicyNode *policy_node_free(PolicyNode *node) { |
| 141 |
if (!node) |
| 142 |
return NULL; |
| 143 |
|
| 144 |
policy_entries_deinit(&node->entries); |
| 145 |
|
| 146 |
c_rbnode_unlink(&node->policy_node); |
| 147 |
free(node); |
| 148 |
|
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
C_DEFINE_CLEANUP(PolicyNode *, policy_node_free); |
| 153 |
|
| 154 |
static int policy_node_new(PolicyNode **nodep, uint32_t uidgid) { |
| 155 |
_c_cleanup_(policy_node_freep) PolicyNode *node = NULL; |
| 156 |
|
| 157 |
node = calloc(1, sizeof(*node)); |
| 158 |
if (!node) |
| 159 |
return error_origin(-ENOMEM); |
| 160 |
|
| 161 |
*node = (PolicyNode)POLICY_NODE_NULL(*node); |
| 162 |
node->uidgid = uidgid; |
| 163 |
|
| 164 |
*nodep = node; |
| 165 |
node = NULL; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* policy_init() - XXX |
| 171 |
*/ |
| 172 |
void policy_init(Policy *policy) { |
| 173 |
*policy = (Policy)POLICY_INIT(*policy); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* policy_deinit() - XXX |
| 178 |
*/ |
| 179 |
void policy_deinit(Policy *policy) { |
| 180 |
PolicyNode *node, *t_node; |
| 181 |
PolicyRecord *record; |
| 182 |
|
| 183 |
while ((record = c_list_first_entry(&policy->selinux_list, PolicyRecord, link))) |
| 184 |
policy_record_free(record); |
| 185 |
|
| 186 |
c_rbtree_for_each_entry_safe_postorder_unlink(node, t_node, &policy->gid_tree, policy_node) |
| 187 |
policy_node_free(node); |
| 188 |
c_rbtree_for_each_entry_safe_postorder_unlink(node, t_node, &policy->uid_tree, policy_node) |
| 189 |
policy_node_free(node); |
| 190 |
|
| 191 |
policy_entries_deinit(&policy->no_console_entries); |
| 192 |
policy_entries_deinit(&policy->at_console_entries); |
| 193 |
policy_entries_deinit(&policy->default_entries); |
| 194 |
} |
| 195 |
|
| 196 |
static int policy_at_uidgid(CRBTree *tree, PolicyNode **nodep, uint32_t uidgid) { |
| 197 |
CRBNode *parent, **slot; |
| 198 |
PolicyNode *node; |
| 199 |
int r; |
| 200 |
|
| 201 |
slot = c_rbtree_find_slot(tree, |
| 202 |
policy_node_compare, |
| 203 |
&uidgid, |
| 204 |
&parent); |
| 205 |
if (slot) { |
| 206 |
r = policy_node_new(&node, uidgid); |
| 207 |
if (r) |
| 208 |
return error_trace(r); |
| 209 |
|
| 210 |
c_rbtree_add(tree, parent, slot, &node->policy_node); |
| 211 |
} else { |
| 212 |
node = c_container_of(parent, PolicyNode, policy_node); |
| 213 |
} |
| 214 |
|
| 215 |
*nodep = node; |
| 216 |
node = NULL; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
static int policy_at_uid(Policy *policy, PolicyNode **nodep, uint32_t uid) { |
| 221 |
return policy_at_uidgid(&policy->uid_tree, nodep, uid); |
| 222 |
} |
| 223 |
|
| 224 |
static int policy_at_gid(Policy *policy, PolicyNode **nodep, uint32_t gid) { |
| 225 |
return policy_at_uidgid(&policy->gid_tree, nodep, gid); |
| 226 |
} |
| 227 |
|
| 228 |
static void policy_import_verdict(Policy *policy, |
| 229 |
PolicyRecord *record, |
| 230 |
ConfigNode *cnode) { |
| 231 |
c_assert(cnode->parent); |
| 232 |
c_assert(cnode->parent->type == CONFIG_NODE_POLICY); |
| 233 |
c_assert(cnode->parent->policy.context); |
| 234 |
c_assert(cnode->parent->policy.context < _CONFIG_POLICY_N); |
| 235 |
|
| 236 |
record->verdict = (cnode->type == CONFIG_NODE_ALLOW); |
| 237 |
record->priority = UINT64_MAX / _CONFIG_POLICY_N * |
| 238 |
cnode->parent->policy.context + |
| 239 |
++policy->i_priority; |
| 240 |
} |
| 241 |
|
| 242 |
static int policy_import_connect_self(Policy *policy) { |
| 243 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 244 |
PolicyNode *node; |
| 245 |
int r; |
| 246 |
|
| 247 |
/* |
| 248 |
* According to dbus-daemon(1), the default policy for the |
| 249 |
* controller-UID is ALLOW, as opposed to the default policy for any |
| 250 |
* other UID which is DENY. |
| 251 |
* Since the broker has a DENY-ALL as default, we need to fake a |
| 252 |
* fallback policy here that allows the calling uid to connect. |
| 253 |
*/ |
| 254 |
|
| 255 |
r = policy_record_new_connect(&record); |
| 256 |
if (r) |
| 257 |
return error_trace(r); |
| 258 |
|
| 259 |
record->verdict = true; |
| 260 |
record->priority = ++policy->i_priority; |
| 261 |
|
| 262 |
r = policy_at_uid(policy, &node, getuid()); |
| 263 |
if (r) |
| 264 |
return error_trace(r); |
| 265 |
|
| 266 |
c_list_link_tail(&node->entries.connect_list, &record->link); |
| 267 |
|
| 268 |
record = NULL; |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
static int policy_import_connect(Policy *policy, ConfigNode *cnode) { |
| 273 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 274 |
PolicyNode *node; |
| 275 |
int r; |
| 276 |
|
| 277 |
c_assert(cnode->parent); |
| 278 |
c_assert(cnode->parent->type == CONFIG_NODE_POLICY); |
| 279 |
|
| 280 |
if ((cnode->allow_deny.user == cnode->allow_deny.group) || |
| 281 |
cnode->allow_deny.own || |
| 282 |
cnode->allow_deny.own_prefix || |
| 283 |
cnode->allow_deny.send_interface || |
| 284 |
cnode->allow_deny.send_member || |
| 285 |
cnode->allow_deny.send_error || |
| 286 |
cnode->allow_deny.send_destination || |
| 287 |
cnode->allow_deny.send_path || |
| 288 |
cnode->allow_deny.send_type || |
| 289 |
cnode->allow_deny.send_broadcast || |
| 290 |
cnode->allow_deny.send_requested_reply || |
| 291 |
cnode->allow_deny.recv_interface || |
| 292 |
cnode->allow_deny.recv_member || |
| 293 |
cnode->allow_deny.recv_error || |
| 294 |
cnode->allow_deny.recv_sender || |
| 295 |
cnode->allow_deny.recv_path || |
| 296 |
cnode->allow_deny.recv_type || |
| 297 |
cnode->allow_deny.recv_requested_reply) { |
| 298 |
fprintf(stderr, "Invalid policy attribute combination in %s +%lu\n", |
| 299 |
cnode->file, cnode->lineno); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
if (cnode->parent->policy.context == CONFIG_POLICY_USER || |
| 304 |
cnode->parent->policy.context == CONFIG_POLICY_GROUP) { |
| 305 |
fprintf(stderr, "Connection policy not allowed in user/group context in %s +%lu\n", |
| 306 |
cnode->file, cnode->lineno); |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
r = policy_record_new_connect(&record); |
| 311 |
if (r) |
| 312 |
return error_trace(r); |
| 313 |
|
| 314 |
policy_import_verdict(policy, record, cnode); |
| 315 |
|
| 316 |
if (cnode->allow_deny.user && cnode->allow_deny.uid != (uint32_t)-1) { |
| 317 |
r = policy_at_uid(policy, &node, cnode->allow_deny.uid); |
| 318 |
if (r) |
| 319 |
return error_trace(r); |
| 320 |
|
| 321 |
c_list_link_tail(&node->entries.connect_list, &record->link); |
| 322 |
} else if (cnode->allow_deny.group && cnode->allow_deny.gid != (uint32_t)-1) { |
| 323 |
r = policy_at_gid(policy, &node, cnode->allow_deny.uid); |
| 324 |
if (r) |
| 325 |
return error_trace(r); |
| 326 |
|
| 327 |
c_list_link_tail(&node->entries.connect_list, &record->link); |
| 328 |
} else { |
| 329 |
c_list_link_tail(&policy->default_entries.connect_list, &record->link); |
| 330 |
} |
| 331 |
|
| 332 |
record = NULL; |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
static int policy_import_own(Policy *policy, ConfigNode *cnode) { |
| 337 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 338 |
PolicyNode *node; |
| 339 |
int r; |
| 340 |
|
| 341 |
c_assert(cnode->parent); |
| 342 |
c_assert(cnode->parent->type == CONFIG_NODE_POLICY); |
| 343 |
|
| 344 |
if ((!cnode->allow_deny.own == !cnode->allow_deny.own_prefix) || |
| 345 |
cnode->allow_deny.user || |
| 346 |
cnode->allow_deny.group || |
| 347 |
cnode->allow_deny.send_interface || |
| 348 |
cnode->allow_deny.send_member || |
| 349 |
cnode->allow_deny.send_error || |
| 350 |
cnode->allow_deny.send_destination || |
| 351 |
cnode->allow_deny.send_path || |
| 352 |
cnode->allow_deny.send_type || |
| 353 |
cnode->allow_deny.send_broadcast || |
| 354 |
cnode->allow_deny.send_requested_reply || |
| 355 |
cnode->allow_deny.recv_interface || |
| 356 |
cnode->allow_deny.recv_member || |
| 357 |
cnode->allow_deny.recv_error || |
| 358 |
cnode->allow_deny.recv_sender || |
| 359 |
cnode->allow_deny.recv_path || |
| 360 |
cnode->allow_deny.recv_type || |
| 361 |
cnode->allow_deny.recv_requested_reply) { |
| 362 |
fprintf(stderr, "Invalid policy attribute combination in %s +%lu\n", |
| 363 |
cnode->file, cnode->lineno); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
r = policy_record_new_own(&record); |
| 368 |
if (r) |
| 369 |
return error_trace(r); |
| 370 |
|
| 371 |
policy_import_verdict(policy, record, cnode); |
| 372 |
|
| 373 |
if (cnode->allow_deny.own) { |
| 374 |
if (!strcmp(cnode->allow_deny.own, "*")) { |
| 375 |
record->own.prefix = true; |
| 376 |
record->own.name = ""; |
| 377 |
} else { |
| 378 |
record->own.prefix = false; |
| 379 |
record->own.name = cnode->allow_deny.own; |
| 380 |
} |
| 381 |
} else { |
| 382 |
record->own.prefix = true; |
| 383 |
record->own.name = cnode->allow_deny.own_prefix; |
| 384 |
} |
| 385 |
|
| 386 |
if (cnode->parent->policy.context == CONFIG_POLICY_USER) { |
| 387 |
if (cnode->parent->policy.id != (uint32_t)-1) { |
| 388 |
r = policy_at_uid(policy, &node, cnode->parent->policy.id); |
| 389 |
if (r) |
| 390 |
return error_trace(r); |
| 391 |
|
| 392 |
c_list_link_tail(&node->entries.own_list, &record->link); |
| 393 |
} |
| 394 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_NO_CONSOLE) { |
| 395 |
c_list_link_tail(&policy->no_console_entries.own_list, &record->link); |
| 396 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_AT_CONSOLE) { |
| 397 |
c_list_link_tail(&policy->at_console_entries.own_list, &record->link); |
| 398 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_GROUP) { |
| 399 |
if (cnode->parent->policy.id != (uint32_t)-1) { |
| 400 |
r = policy_at_gid(policy, &node, cnode->parent->policy.id); |
| 401 |
if (r) |
| 402 |
return error_trace(r); |
| 403 |
|
| 404 |
c_list_link_tail(&node->entries.own_list, &record->link); |
| 405 |
} |
| 406 |
} else { |
| 407 |
c_list_link_tail(&policy->default_entries.own_list, &record->link); |
| 408 |
} |
| 409 |
|
| 410 |
if (c_list_is_linked(&record->link)) |
| 411 |
record = NULL; |
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
static int policy_import_send(Policy *policy, ConfigNode *cnode) { |
| 416 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 417 |
PolicyNode *node; |
| 418 |
int r; |
| 419 |
|
| 420 |
c_assert(cnode->parent); |
| 421 |
c_assert(cnode->parent->type == CONFIG_NODE_POLICY); |
| 422 |
|
| 423 |
if (cnode->allow_deny.user || |
| 424 |
cnode->allow_deny.group || |
| 425 |
cnode->allow_deny.own || |
| 426 |
cnode->allow_deny.own_prefix || |
| 427 |
cnode->allow_deny.recv_interface || |
| 428 |
cnode->allow_deny.recv_member || |
| 429 |
cnode->allow_deny.recv_error || |
| 430 |
cnode->allow_deny.recv_sender || |
| 431 |
cnode->allow_deny.recv_path || |
| 432 |
cnode->allow_deny.recv_type || |
| 433 |
cnode->allow_deny.recv_requested_reply) { |
| 434 |
fprintf(stderr, "Invalid policy attribute combination in %s +%lu\n", |
| 435 |
cnode->file, cnode->lineno); |
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
if (cnode->allow_deny.send_type == DBUS_MESSAGE_TYPE_METHOD_RETURN || |
| 440 |
cnode->allow_deny.send_type == DBUS_MESSAGE_TYPE_ERROR || |
| 441 |
cnode->allow_deny.send_type == DBUS_MESSAGE_TYPE_INVALID) { |
| 442 |
if (cnode->type == CONFIG_NODE_DENY && cnode->allow_deny.send_requested_reply == UTIL_TRISTATE_YES) |
| 443 |
fprintf(stderr, "Policy to deny expected replies in %s +%lu: Explicit policies on replies and errors are deprecated and ignored\n", |
| 444 |
cnode->file, cnode->lineno); |
| 445 |
else if (cnode->type == CONFIG_NODE_ALLOW && cnode->allow_deny.send_requested_reply == UTIL_TRISTATE_NO) |
| 446 |
fprintf(stderr, "Policy to allow unexpected replies in %s +%lu: Explicit policies on replies and errors are deprecated and ignored\n", |
| 447 |
cnode->file, cnode->lineno); |
| 448 |
|
| 449 |
if (cnode->allow_deny.send_type != DBUS_MESSAGE_TYPE_INVALID) |
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
if (cnode->allow_deny.eavesdrop == UTIL_TRISTATE_YES) { |
| 454 |
if (cnode->type == CONFIG_NODE_ALLOW) |
| 455 |
/* Ignore the attribute, but keep the rule, it also applies when not eavesdropping. */ |
| 456 |
fprintf(stderr, "Policy to allow eavesdropping in %s +%lu: Eavesdropping is deprecated and ignored\n", |
| 457 |
cnode->file, cnode->lineno); |
| 458 |
else if (cnode->type == CONFIG_NODE_DENY) |
| 459 |
/* The rule applies only when eavesdropping, drop it. */ |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
r = policy_record_new_xmit(&record); |
| 464 |
if (r) |
| 465 |
return error_trace(r); |
| 466 |
|
| 467 |
policy_import_verdict(policy, record, cnode); |
| 468 |
|
| 469 |
record->xmit.name = cnode->allow_deny.send_destination; |
| 470 |
record->xmit.path = cnode->allow_deny.send_path; |
| 471 |
record->xmit.interface = cnode->allow_deny.send_interface; |
| 472 |
record->xmit.member = cnode->allow_deny.send_member; |
| 473 |
record->xmit.type = cnode->allow_deny.send_type; |
| 474 |
record->xmit.broadcast = cnode->allow_deny.send_broadcast; |
| 475 |
record->xmit.min_fds = cnode->allow_deny.min_fds; |
| 476 |
record->xmit.max_fds = cnode->allow_deny.max_fds; |
| 477 |
policy_record_xmit_trim(record); |
| 478 |
|
| 479 |
if (cnode->parent->policy.context == CONFIG_POLICY_USER) { |
| 480 |
if (cnode->parent->policy.id != (uint32_t)-1) { |
| 481 |
r = policy_at_uid(policy, &node, cnode->parent->policy.id); |
| 482 |
if (r) |
| 483 |
return error_trace(r); |
| 484 |
|
| 485 |
c_list_link_tail(&node->entries.send_list, &record->link); |
| 486 |
} |
| 487 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_NO_CONSOLE) { |
| 488 |
c_list_link_tail(&policy->no_console_entries.send_list, &record->link); |
| 489 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_AT_CONSOLE) { |
| 490 |
c_list_link_tail(&policy->at_console_entries.send_list, &record->link); |
| 491 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_GROUP) { |
| 492 |
if (cnode->parent->policy.id != (uint32_t)-1) { |
| 493 |
r = policy_at_gid(policy, &node, cnode->parent->policy.id); |
| 494 |
if (r) |
| 495 |
return error_trace(r); |
| 496 |
|
| 497 |
c_list_link_tail(&node->entries.send_list, &record->link); |
| 498 |
} |
| 499 |
} else { |
| 500 |
c_list_link_tail(&policy->default_entries.send_list, &record->link); |
| 501 |
} |
| 502 |
|
| 503 |
if (c_list_is_linked(&record->link)) |
| 504 |
record = NULL; |
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
static int policy_import_recv(Policy *policy, ConfigNode *cnode) { |
| 509 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 510 |
PolicyNode *node; |
| 511 |
int r; |
| 512 |
|
| 513 |
c_assert(cnode->parent); |
| 514 |
c_assert(cnode->parent->type == CONFIG_NODE_POLICY); |
| 515 |
|
| 516 |
if (cnode->allow_deny.user || |
| 517 |
cnode->allow_deny.group || |
| 518 |
cnode->allow_deny.own || |
| 519 |
cnode->allow_deny.own_prefix || |
| 520 |
cnode->allow_deny.send_interface || |
| 521 |
cnode->allow_deny.send_member || |
| 522 |
cnode->allow_deny.send_error || |
| 523 |
cnode->allow_deny.send_destination || |
| 524 |
cnode->allow_deny.send_path || |
| 525 |
cnode->allow_deny.send_type || |
| 526 |
cnode->allow_deny.send_broadcast || |
| 527 |
cnode->allow_deny.send_requested_reply) { |
| 528 |
fprintf(stderr, "Invalid policy attribute combination in %s +%lu\n", |
| 529 |
cnode->file, cnode->lineno); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
if (cnode->allow_deny.recv_type == DBUS_MESSAGE_TYPE_METHOD_RETURN || |
| 534 |
cnode->allow_deny.recv_type == DBUS_MESSAGE_TYPE_ERROR || |
| 535 |
cnode->allow_deny.recv_type == DBUS_MESSAGE_TYPE_INVALID) { |
| 536 |
if (cnode->type == CONFIG_NODE_DENY && cnode->allow_deny.recv_requested_reply == UTIL_TRISTATE_YES) |
| 537 |
fprintf(stderr, "Policy to deny expected replies in %s +%lu: Explicit policies on replies and errors are deprecated and ignored\n", |
| 538 |
cnode->file, cnode->lineno); |
| 539 |
else if (cnode->type == CONFIG_NODE_ALLOW && cnode->allow_deny.recv_requested_reply == UTIL_TRISTATE_NO) |
| 540 |
fprintf(stderr, "Policy to allow unexpected replies in %s +%lu: Explicit policies on replies and errors are deprecated and ignored\n", |
| 541 |
cnode->file, cnode->lineno); |
| 542 |
|
| 543 |
if (cnode->allow_deny.recv_type != DBUS_MESSAGE_TYPE_INVALID) |
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
if (cnode->allow_deny.eavesdrop == UTIL_TRISTATE_YES) { |
| 548 |
if (cnode->type == CONFIG_NODE_ALLOW) |
| 549 |
/* Ignore the attribute, but keep the rule, it also applies when not eavesdropping. */ |
| 550 |
fprintf(stderr, "Policy to allow eavesdropping in %s +%lu: Eavesdropping is deprecated and ignored\n", |
| 551 |
cnode->file, cnode->lineno); |
| 552 |
else if (cnode->type == CONFIG_NODE_DENY) |
| 553 |
/* The rule applies only when eavesdropping, drop it. */ |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
r = policy_record_new_xmit(&record); |
| 558 |
if (r) |
| 559 |
return error_trace(r); |
| 560 |
|
| 561 |
policy_import_verdict(policy, record, cnode); |
| 562 |
|
| 563 |
record->xmit.name = cnode->allow_deny.recv_sender; |
| 564 |
record->xmit.path = cnode->allow_deny.recv_path; |
| 565 |
record->xmit.interface = cnode->allow_deny.recv_interface; |
| 566 |
record->xmit.member = cnode->allow_deny.recv_member; |
| 567 |
record->xmit.type = cnode->allow_deny.recv_type; |
| 568 |
record->xmit.min_fds = cnode->allow_deny.min_fds; |
| 569 |
record->xmit.max_fds = cnode->allow_deny.max_fds; |
| 570 |
policy_record_xmit_trim(record); |
| 571 |
|
| 572 |
if (cnode->parent->policy.context == CONFIG_POLICY_USER) { |
| 573 |
if (cnode->parent->policy.id != (uint32_t)-1) { |
| 574 |
r = policy_at_uid(policy, &node, cnode->parent->policy.id); |
| 575 |
if (r) |
| 576 |
return error_trace(r); |
| 577 |
|
| 578 |
c_list_link_tail(&node->entries.recv_list, &record->link); |
| 579 |
} |
| 580 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_NO_CONSOLE) { |
| 581 |
c_list_link_tail(&policy->no_console_entries.recv_list, &record->link); |
| 582 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_AT_CONSOLE) { |
| 583 |
c_list_link_tail(&policy->at_console_entries.recv_list, &record->link); |
| 584 |
} else if (cnode->parent->policy.context == CONFIG_POLICY_GROUP) { |
| 585 |
if (cnode->parent->policy.id != (uint32_t)-1) { |
| 586 |
r = policy_at_gid(policy, &node, cnode->parent->policy.id); |
| 587 |
if (r) |
| 588 |
return error_trace(r); |
| 589 |
|
| 590 |
c_list_link_tail(&node->entries.recv_list, &record->link); |
| 591 |
} |
| 592 |
} else { |
| 593 |
c_list_link_tail(&policy->default_entries.recv_list, &record->link); |
| 594 |
} |
| 595 |
|
| 596 |
if (c_list_is_linked(&record->link)) |
| 597 |
record = NULL; |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
static int policy_import_selinux(Policy *policy, ConfigNode *cnode) { |
| 602 |
_c_cleanup_(policy_record_freep) PolicyRecord *record = NULL; |
| 603 |
int r; |
| 604 |
|
| 605 |
c_assert(cnode->parent); |
| 606 |
c_assert(cnode->parent->type == CONFIG_NODE_SELINUX); |
| 607 |
|
| 608 |
if (!cnode->associate.own || |
| 609 |
!cnode->associate.context) { |
| 610 |
fprintf(stderr, "Invalid policy attribute combination in %s +%lu\n", |
| 611 |
cnode->file, cnode->lineno); |
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
r = policy_record_new_own(&record); |
| 616 |
if (r) |
| 617 |
return error_trace(r); |
| 618 |
|
| 619 |
record->selinux.name = cnode->associate.own; |
| 620 |
record->selinux.context = cnode->associate.context; |
| 621 |
|
| 622 |
c_list_link_tail(&policy->selinux_list, &record->link); |
| 623 |
|
| 624 |
record = NULL; |
| 625 |
return 0; |
| 626 |
} |
| 627 |
|
| 628 |
static int policy_import_apparmor(Policy *policy, ConfigNode *cnode) { |
| 629 |
policy->apparmor_mode = cnode->apparmor.mode; |
| 630 |
|
| 631 |
return 0; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* policy_import() - XXX |
| 636 |
*/ |
| 637 |
int policy_import(Policy *policy, ConfigRoot *root) { |
| 638 |
ConfigNode *i_cnode; |
| 639 |
int r; |
| 640 |
|
| 641 |
r = policy_import_connect_self(policy); |
| 642 |
if (r) |
| 643 |
return error_trace(r); |
| 644 |
|
| 645 |
c_list_for_each_entry(i_cnode, &root->node_list, root_link) { |
| 646 |
if (i_cnode->type == CONFIG_NODE_TYPE) { |
| 647 |
policy->bus_type = i_cnode->bustype.name; |
| 648 |
continue; |
| 649 |
} |
| 650 |
|
| 651 |
if (i_cnode->type == CONFIG_NODE_ASSOCIATE) { |
| 652 |
r = policy_import_selinux(policy, i_cnode); |
| 653 |
if (r) |
| 654 |
return error_trace(r); |
| 655 |
continue; |
| 656 |
} |
| 657 |
|
| 658 |
if (i_cnode->type == CONFIG_NODE_APPARMOR) { |
| 659 |
r = policy_import_apparmor(policy, i_cnode); |
| 660 |
if (r) |
| 661 |
return error_trace(r); |
| 662 |
continue; |
| 663 |
} |
| 664 |
|
| 665 |
if (i_cnode->type == CONFIG_NODE_POLICY && |
| 666 |
i_cnode->policy.context == CONFIG_POLICY_AT_CONSOLE) { |
| 667 |
fprintf(stderr, "Deprecated policy context in %s +%lu. The 'at_console' context is deprecated and will be ignored in the future.\n", |
| 668 |
i_cnode->file, i_cnode->lineno); |
| 669 |
} |
| 670 |
|
| 671 |
if (i_cnode->type != CONFIG_NODE_ALLOW && |
| 672 |
i_cnode->type != CONFIG_NODE_DENY) |
| 673 |
continue; |
| 674 |
|
| 675 |
if (!i_cnode->parent || |
| 676 |
i_cnode->parent->type != CONFIG_NODE_POLICY || |
| 677 |
!i_cnode->parent->policy.context) { |
| 678 |
fprintf(stderr, "Policy record without policy context in %s +%lu\n", |
| 679 |
i_cnode->file, i_cnode->lineno); |
| 680 |
continue; |
| 681 |
} |
| 682 |
|
| 683 |
if (i_cnode->allow_deny.user || |
| 684 |
i_cnode->allow_deny.group) { |
| 685 |
r = policy_import_connect(policy, i_cnode); |
| 686 |
if (r) |
| 687 |
return error_trace(r); |
| 688 |
} else if (i_cnode->allow_deny.own || |
| 689 |
i_cnode->allow_deny.own_prefix) { |
| 690 |
r = policy_import_own(policy, i_cnode); |
| 691 |
if (r) |
| 692 |
return error_trace(r); |
| 693 |
} else if (i_cnode->allow_deny.send_interface || |
| 694 |
i_cnode->allow_deny.send_member || |
| 695 |
i_cnode->allow_deny.send_error || |
| 696 |
i_cnode->allow_deny.send_destination || |
| 697 |
i_cnode->allow_deny.send_path || |
| 698 |
i_cnode->allow_deny.send_type || |
| 699 |
i_cnode->allow_deny.send_broadcast || |
| 700 |
i_cnode->allow_deny.send_requested_reply) { |
| 701 |
r = policy_import_send(policy, i_cnode); |
| 702 |
if (r) |
| 703 |
return error_trace(r); |
| 704 |
} else if (i_cnode->allow_deny.recv_interface || |
| 705 |
i_cnode->allow_deny.recv_member || |
| 706 |
i_cnode->allow_deny.recv_error || |
| 707 |
i_cnode->allow_deny.recv_sender || |
| 708 |
i_cnode->allow_deny.recv_path || |
| 709 |
i_cnode->allow_deny.recv_type || |
| 710 |
i_cnode->allow_deny.recv_requested_reply || |
| 711 |
i_cnode->allow_deny.eavesdrop) { |
| 712 |
r = policy_import_recv(policy, i_cnode); |
| 713 |
if (r) |
| 714 |
return error_trace(r); |
| 715 |
} else { |
| 716 |
fprintf(stderr, "Invalid policy without defining attributes in %s +%lu\n", |
| 717 |
i_cnode->file, i_cnode->lineno); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
static PolicyRecord *policy_list_find_top(CList *list) { |
| 725 |
PolicyRecord *i_record, *top_record; |
| 726 |
|
| 727 |
top_record = NULL; |
| 728 |
c_list_for_each_entry(i_record, list, link) |
| 729 |
if (!top_record || i_record->priority > top_record->priority) |
| 730 |
top_record = i_record; |
| 731 |
|
| 732 |
return top_record; |
| 733 |
} |
| 734 |
|
| 735 |
static void policy_list_shrink(CList *list, PolicyRecord *keep) { |
| 736 |
PolicyRecord *i_record, *t_record; |
| 737 |
|
| 738 |
c_list_for_each_entry_safe(i_record, t_record, list, link) |
| 739 |
if (i_record != keep) |
| 740 |
policy_record_free(i_record); |
| 741 |
} |
| 742 |
|
| 743 |
static void policy_optimize_connect(Policy *policy) { |
| 744 |
PolicyRecord *top_default, *top; |
| 745 |
PolicyNode *i_node; |
| 746 |
|
| 747 |
/* |
| 748 |
* In case of CONNECT policies, all information we have is whether or |
| 749 |
* not a given UID or GID is allowed to connect. There is no further |
| 750 |
* filters, hence we can reduce the policy decision for each UID (or |
| 751 |
* GID) to a single boolean by throwing away anything but the highest |
| 752 |
* priority verdict. |
| 753 |
* |
| 754 |
* Furthermore, we can throw out UID or GID entries if the default |
| 755 |
* entry has a higher priority. |
| 756 |
*/ |
| 757 |
|
| 758 |
/* shrink @connect_default down to 1 entry */ |
| 759 |
top_default = policy_list_find_top(&policy->default_entries.connect_list); |
| 760 |
policy_list_shrink(&policy->default_entries.connect_list, top_default); |
| 761 |
|
| 762 |
/* shrink each uid-map down to 1 entry (or 0 if below @top_default) */ |
| 763 |
c_rbtree_for_each_entry(i_node, &policy->uid_tree, policy_node) { |
| 764 |
top = policy_list_find_top(&i_node->entries.connect_list); |
| 765 |
if (top && top_default && top->priority <= top_default->priority) |
| 766 |
top = NULL; |
| 767 |
policy_list_shrink(&i_node->entries.connect_list, top); |
| 768 |
} |
| 769 |
|
| 770 |
/* shrink each gid-map down to 1 entry (or 0 if below @top_default) */ |
| 771 |
c_rbtree_for_each_entry(i_node, &policy->gid_tree, policy_node) { |
| 772 |
top = policy_list_find_top(&i_node->entries.connect_list); |
| 773 |
if (top && top_default && top->priority <= top_default->priority) |
| 774 |
top = NULL; |
| 775 |
policy_list_shrink(&i_node->entries.connect_list, top); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
static void policy_optimize_trim(Policy *policy) { |
| 780 |
PolicyNode *node, *t_node; |
| 781 |
|
| 782 |
/* |
| 783 |
* The optimizations might have dropped entries that are redundant or |
| 784 |
* have no effect. Hence, lets trim our policy and drop all the uid/gid |
| 785 |
* nodes that no longer have any records. |
| 786 |
*/ |
| 787 |
|
| 788 |
c_rbtree_for_each_entry_safe(node, t_node, &policy->uid_tree, policy_node) |
| 789 |
if (policy_entries_is_empty(&node->entries)) |
| 790 |
policy_node_free(node); |
| 791 |
|
| 792 |
c_rbtree_for_each_entry_safe(node, t_node, &policy->gid_tree, policy_node) |
| 793 |
if (policy_entries_is_empty(&node->entries)) |
| 794 |
policy_node_free(node); |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* policy_optimize() - XXX |
| 799 |
*/ |
| 800 |
void policy_optimize(Policy *policy) { |
| 801 |
/* |
| 802 |
* XXX: There are many more optimizations possible. Lets figure out |
| 803 |
* which of those are worth it, and then implement them. |
| 804 |
*/ |
| 805 |
|
| 806 |
policy_optimize_connect(policy); |
| 807 |
policy_optimize_trim(policy); |
| 808 |
} |
| 809 |
|
| 810 |
static int policy_export_connect(Policy *policy, CList *default_list, CList *specific_list, sd_bus_message *m) { |
| 811 |
PolicyRecord *top = NULL; |
| 812 |
int r; |
| 813 |
|
| 814 |
if (specific_list) { |
| 815 |
top = c_list_first_entry(specific_list, PolicyRecord, link); |
| 816 |
/* list must be empty or singular */ |
| 817 |
c_assert(top == c_list_last_entry(specific_list, PolicyRecord, link)); |
| 818 |
} |
| 819 |
|
| 820 |
if (!top && default_list) { |
| 821 |
top = c_list_first_entry(default_list, PolicyRecord, link); |
| 822 |
/* list must be empty or singular */ |
| 823 |
c_assert(top == c_list_last_entry(default_list, PolicyRecord, link)); |
| 824 |
} |
| 825 |
|
| 826 |
if (top) |
| 827 |
r = sd_bus_message_append(m, "bt", top->verdict, top->priority); |
| 828 |
else |
| 829 |
r = sd_bus_message_append(m, "bt", false, POLICY_PRIORITY_DEFAULT); |
| 830 |
if (r < 0) |
| 831 |
return error_origin(r); |
| 832 |
|
| 833 |
return 0; |
| 834 |
} |
| 835 |
|
| 836 |
static int policy_export_own(Policy *policy, CList *list1, CList *list2, sd_bus_message *m) { |
| 837 |
PolicyRecord *i_record; |
| 838 |
int r; |
| 839 |
|
| 840 |
r = sd_bus_message_open_container(m, 'a', "(btbs)"); |
| 841 |
if (r < 0) |
| 842 |
return error_origin(r); |
| 843 |
|
| 844 |
if (list1) { |
| 845 |
c_list_for_each_entry(i_record, list1, link) { |
| 846 |
r = sd_bus_message_append(m, |
| 847 |
"(btbs)", |
| 848 |
i_record->verdict, |
| 849 |
i_record->priority, |
| 850 |
i_record->own.prefix, |
| 851 |
i_record->own.name); |
| 852 |
if (r < 0) |
| 853 |
return error_origin(r); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
if (list2) { |
| 858 |
c_list_for_each_entry(i_record, list2, link) { |
| 859 |
r = sd_bus_message_append(m, |
| 860 |
"(btbs)", |
| 861 |
i_record->verdict, |
| 862 |
i_record->priority, |
| 863 |
i_record->own.prefix, |
| 864 |
i_record->own.name); |
| 865 |
if (r < 0) |
| 866 |
return error_origin(r); |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
r = sd_bus_message_close_container(m); |
| 871 |
if (r < 0) |
| 872 |
return error_origin(r); |
| 873 |
|
| 874 |
return 0; |
| 875 |
} |
| 876 |
|
| 877 |
static int policy_export_xmit(Policy *policy, CList *list1, CList *list2, sd_bus_message *m) { |
| 878 |
PolicyRecord *i_record; |
| 879 |
int r; |
| 880 |
|
| 881 |
r = sd_bus_message_open_container(m, 'a', "(btssssuutt)"); |
| 882 |
if (r < 0) |
| 883 |
return error_origin(r); |
| 884 |
|
| 885 |
if (list1) { |
| 886 |
c_list_for_each_entry(i_record, list1, link) { |
| 887 |
r = sd_bus_message_append(m, |
| 888 |
"(btssssuutt)", |
| 889 |
i_record->verdict, |
| 890 |
i_record->priority, |
| 891 |
i_record->xmit.name, |
| 892 |
i_record->xmit.path, |
| 893 |
i_record->xmit.interface, |
| 894 |
i_record->xmit.member, |
| 895 |
i_record->xmit.type, |
| 896 |
i_record->xmit.broadcast, |
| 897 |
i_record->xmit.min_fds, |
| 898 |
i_record->xmit.max_fds); |
| 899 |
if (r < 0) |
| 900 |
return error_origin(r); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
if (list2) { |
| 905 |
c_list_for_each_entry(i_record, list2, link) { |
| 906 |
r = sd_bus_message_append(m, |
| 907 |
"(btssssuutt)", |
| 908 |
i_record->verdict, |
| 909 |
i_record->priority, |
| 910 |
i_record->xmit.name, |
| 911 |
i_record->xmit.path, |
| 912 |
i_record->xmit.interface, |
| 913 |
i_record->xmit.member, |
| 914 |
i_record->xmit.type, |
| 915 |
i_record->xmit.broadcast, |
| 916 |
i_record->xmit.min_fds, |
| 917 |
i_record->xmit.max_fds); |
| 918 |
if (r < 0) |
| 919 |
return error_origin(r); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
r = sd_bus_message_close_container(m); |
| 924 |
if (r < 0) |
| 925 |
return error_origin(r); |
| 926 |
|
| 927 |
return 0; |
| 928 |
} |
| 929 |
|
| 930 |
#define POLICY_T_BATCH \ |
| 931 |
"bt" \ |
| 932 |
"a(btbs)" \ |
| 933 |
"a(btssssuutt)" \ |
| 934 |
"a(btssssuutt)" |
| 935 |
|
| 936 |
#define POLICY_T \ |
| 937 |
"a(u(" POLICY_T_BATCH "))" \ |
| 938 |
"a(buu(" POLICY_T_BATCH "))" \ |
| 939 |
"a(ss)" \ |
| 940 |
"b" \ |
| 941 |
"s" |
| 942 |
|
| 943 |
static int policy_export_console(Policy *policy, sd_bus_message *m, PolicyEntries *entries, uint32_t uid_start, uint32_t n_uid) { |
| 944 |
int r; |
| 945 |
|
| 946 |
/* check for overflow */ |
| 947 |
c_assert(n_uid == 0 || uid_start + n_uid - 1 >= uid_start); |
| 948 |
|
| 949 |
if (n_uid == 0) |
| 950 |
return 0; |
| 951 |
|
| 952 |
if (policy_entries_is_empty(entries)) |
| 953 |
return 0; |
| 954 |
|
| 955 |
r = sd_bus_message_open_container(m, 'r', "buu(" POLICY_T_BATCH ")"); |
| 956 |
if (r < 0) |
| 957 |
return error_origin(r); |
| 958 |
|
| 959 |
r = sd_bus_message_append(m, "buu", false, uid_start, uid_start + n_uid - 1); |
| 960 |
if (r < 0) |
| 961 |
return error_origin(r); |
| 962 |
|
| 963 |
r = sd_bus_message_open_container(m, 'r', POLICY_T_BATCH); |
| 964 |
if (r < 0) |
| 965 |
return error_origin(r); |
| 966 |
|
| 967 |
r = policy_export_connect(policy, NULL, &entries->connect_list, m); |
| 968 |
r = r ?: policy_export_own(policy, NULL, &entries->own_list, m); |
| 969 |
r = r ?: policy_export_xmit(policy, NULL, &entries->send_list, m); |
| 970 |
r = r ?: policy_export_xmit(policy, NULL, &entries->recv_list, m); |
| 971 |
if (r) |
| 972 |
return error_trace(r); |
| 973 |
|
| 974 |
r = sd_bus_message_close_container(m); |
| 975 |
if (r < 0) |
| 976 |
return error_origin(r); |
| 977 |
|
| 978 |
r = sd_bus_message_close_container(m); |
| 979 |
if (r < 0) |
| 980 |
return error_origin(r); |
| 981 |
|
| 982 |
return 0; |
| 983 |
} |
| 984 |
|
| 985 |
static int policy_uid_compare(const void *_a, const void *_b) { |
| 986 |
uint32_t a = *(uint32_t*)_a, b = *(uint32_t*)_b; |
| 987 |
|
| 988 |
if (a < b) |
| 989 |
return -1; |
| 990 |
if (a > b) |
| 991 |
return 1; |
| 992 |
|
| 993 |
return 0; |
| 994 |
} |
| 995 |
|
| 996 |
/** |
| 997 |
* policy_export() - XXX |
| 998 |
*/ |
| 999 |
int policy_export(Policy *policy, sd_bus_message *m, uint32_t *at_console_uids, size_t n_at_console_uids) { |
| 1000 |
PolicyNode *node; |
| 1001 |
PolicyRecord *i_record; |
| 1002 |
int r; |
| 1003 |
|
| 1004 |
r = sd_bus_message_open_container(m, 'v', "(" POLICY_T ")"); |
| 1005 |
if (r < 0) |
| 1006 |
return error_origin(r); |
| 1007 |
|
| 1008 |
r = sd_bus_message_open_container(m, 'r', POLICY_T); |
| 1009 |
if (r < 0) |
| 1010 |
return error_origin(r); |
| 1011 |
|
| 1012 |
r = sd_bus_message_open_container(m, 'a', "(u(" POLICY_T_BATCH "))"); |
| 1013 |
if (r < 0) |
| 1014 |
return error_origin(r); |
| 1015 |
|
| 1016 |
r = sd_bus_message_open_container(m, 'r', "u(" POLICY_T_BATCH ")"); |
| 1017 |
if (r < 0) |
| 1018 |
return error_origin(r); |
| 1019 |
|
| 1020 |
r = sd_bus_message_append(m, "u", (uint32_t)-1); |
| 1021 |
if (r < 0) |
| 1022 |
return error_origin(r); |
| 1023 |
|
| 1024 |
r = sd_bus_message_open_container(m, 'r', POLICY_T_BATCH); |
| 1025 |
if (r < 0) |
| 1026 |
return error_origin(r); |
| 1027 |
|
| 1028 |
r = policy_export_connect(policy, &policy->default_entries.connect_list, NULL, m); |
| 1029 |
r = r ?: policy_export_own(policy, &policy->default_entries.own_list, NULL, m); |
| 1030 |
r = r ?: policy_export_xmit(policy, &policy->default_entries.send_list, NULL, m); |
| 1031 |
r = r ?: policy_export_xmit(policy, &policy->default_entries.recv_list, NULL, m); |
| 1032 |
if (r) |
| 1033 |
return error_trace(r); |
| 1034 |
|
| 1035 |
r = sd_bus_message_close_container(m); |
| 1036 |
if (r < 0) |
| 1037 |
return error_origin(r); |
| 1038 |
|
| 1039 |
r = sd_bus_message_close_container(m); |
| 1040 |
if (r < 0) |
| 1041 |
return error_origin(r); |
| 1042 |
|
| 1043 |
c_rbtree_for_each_entry(node, &policy->uid_tree, policy_node) { |
| 1044 |
r = sd_bus_message_open_container(m, 'r', "u(" POLICY_T_BATCH ")"); |
| 1045 |
if (r < 0) |
| 1046 |
return error_origin(r); |
| 1047 |
|
| 1048 |
r = sd_bus_message_append(m, "u", node->uidgid); |
| 1049 |
if (r < 0) |
| 1050 |
return error_origin(r); |
| 1051 |
|
| 1052 |
r = sd_bus_message_open_container(m, 'r', POLICY_T_BATCH); |
| 1053 |
if (r < 0) |
| 1054 |
return error_origin(r); |
| 1055 |
|
| 1056 |
r = policy_export_connect(policy, &policy->default_entries.connect_list, &node->entries.connect_list, m); |
| 1057 |
r = r ?: policy_export_own(policy, &policy->default_entries.own_list, &node->entries.own_list, m); |
| 1058 |
r = r ?: policy_export_xmit(policy, &policy->default_entries.send_list, &node->entries.send_list, m); |
| 1059 |
r = r ?: policy_export_xmit(policy, &policy->default_entries.recv_list, &node->entries.recv_list, m); |
| 1060 |
if (r) |
| 1061 |
return error_trace(r); |
| 1062 |
|
| 1063 |
r = sd_bus_message_close_container(m); |
| 1064 |
if (r < 0) |
| 1065 |
return error_origin(r); |
| 1066 |
|
| 1067 |
r = sd_bus_message_close_container(m); |
| 1068 |
if (r < 0) |
| 1069 |
return error_origin(r); |
| 1070 |
} |
| 1071 |
|
| 1072 |
r = sd_bus_message_close_container(m); |
| 1073 |
if (r < 0) |
| 1074 |
return error_origin(r); |
| 1075 |
|
| 1076 |
r = sd_bus_message_open_container(m, 'a', "(buu(" POLICY_T_BATCH "))"); |
| 1077 |
if (r < 0) |
| 1078 |
return error_origin(r); |
| 1079 |
|
| 1080 |
c_rbtree_for_each_entry(node, &policy->gid_tree, policy_node) { |
| 1081 |
r = sd_bus_message_open_container(m, 'r', "buu(" POLICY_T_BATCH ")"); |
| 1082 |
if (r < 0) |
| 1083 |
return error_origin(r); |
| 1084 |
|
| 1085 |
r = sd_bus_message_append(m, "buu", true, node->uidgid, node->uidgid); |
| 1086 |
if (r < 0) |
| 1087 |
return error_origin(r); |
| 1088 |
|
| 1089 |
r = sd_bus_message_open_container(m, 'r', POLICY_T_BATCH); |
| 1090 |
if (r < 0) |
| 1091 |
return error_origin(r); |
| 1092 |
|
| 1093 |
r = policy_export_connect(policy, &policy->default_entries.connect_list, &node->entries.connect_list, m); |
| 1094 |
r = r ?: policy_export_own(policy, NULL, &node->entries.own_list, m); |
| 1095 |
r = r ?: policy_export_xmit(policy, NULL, &node->entries.send_list, m); |
| 1096 |
r = r ?: policy_export_xmit(policy, NULL, &node->entries.recv_list, m); |
| 1097 |
if (r) |
| 1098 |
return error_trace(r); |
| 1099 |
|
| 1100 |
r = sd_bus_message_close_container(m); |
| 1101 |
if (r < 0) |
| 1102 |
return error_origin(r); |
| 1103 |
|
| 1104 |
r = sd_bus_message_close_container(m); |
| 1105 |
if (r < 0) |
| 1106 |
return error_origin(r); |
| 1107 |
} |
| 1108 |
|
| 1109 |
{ |
| 1110 |
uint32_t next_uid = 0; |
| 1111 |
|
| 1112 |
if (n_at_console_uids > 1) |
| 1113 |
qsort(at_console_uids, n_at_console_uids, sizeof(*at_console_uids), policy_uid_compare); |
| 1114 |
|
| 1115 |
while (n_at_console_uids > 0 && at_console_uids[n_at_console_uids - 1] > (uint32_t)SYSTEMUIDMAX) |
| 1116 |
--n_at_console_uids; |
| 1117 |
|
| 1118 |
for (size_t i = 0; i < n_at_console_uids; ++i) { |
| 1119 |
r = policy_export_console(policy, m, &policy->no_console_entries, next_uid, at_console_uids[i] - next_uid); |
| 1120 |
if (r < 0) |
| 1121 |
return error_trace(r); |
| 1122 |
|
| 1123 |
r = policy_export_console(policy, m, &policy->at_console_entries, at_console_uids[i], 1); |
| 1124 |
if (r < 0) |
| 1125 |
return error_trace(r); |
| 1126 |
|
| 1127 |
next_uid = at_console_uids[i] + 1; |
| 1128 |
} |
| 1129 |
|
| 1130 |
r = policy_export_console(policy, m, &policy->no_console_entries, next_uid, (uint32_t)SYSTEMUIDMAX + 1 - next_uid); |
| 1131 |
if (r < 0) |
| 1132 |
return error_trace(r); |
| 1133 |
} |
| 1134 |
|
| 1135 |
r = policy_export_console(policy, m, &policy->at_console_entries, (uint32_t)(SYSTEMUIDMAX + 1), ((uint32_t)-1) - (uint32_t)SYSTEMUIDMAX); |
| 1136 |
if (r < 0) |
| 1137 |
return error_trace(r); |
| 1138 |
|
| 1139 |
r = sd_bus_message_close_container(m); |
| 1140 |
if (r < 0) |
| 1141 |
return error_origin(r); |
| 1142 |
|
| 1143 |
r = sd_bus_message_open_container(m, 'a', "(ss)"); |
| 1144 |
if (r < 0) |
| 1145 |
return error_origin(r); |
| 1146 |
|
| 1147 |
c_list_for_each_entry(i_record, &policy->selinux_list, link) { |
| 1148 |
r = sd_bus_message_append(m, "(ss)", |
| 1149 |
i_record->selinux.name, |
| 1150 |
i_record->selinux.context); |
| 1151 |
if (r < 0) |
| 1152 |
return error_origin(r); |
| 1153 |
} |
| 1154 |
|
| 1155 |
r = sd_bus_message_close_container(m); |
| 1156 |
if (r < 0) |
| 1157 |
return error_origin(r); |
| 1158 |
|
| 1159 |
r = sd_bus_message_append(m, "b", (policy->apparmor_mode != CONFIG_APPARMOR_DISABLED)); |
| 1160 |
if (r < 0) |
| 1161 |
return error_origin(r); |
| 1162 |
|
| 1163 |
r = sd_bus_message_append(m, "s", policy->bus_type); |
| 1164 |
if (r < 0) |
| 1165 |
return error_origin(r); |
| 1166 |
|
| 1167 |
r = sd_bus_message_close_container(m); |
| 1168 |
if (r < 0) |
| 1169 |
return error_origin(r); |
| 1170 |
|
| 1171 |
r = sd_bus_message_close_container(m); |
| 1172 |
if (r < 0) |
| 1173 |
return error_origin(r); |
| 1174 |
|
| 1175 |
return 0; |
| 1176 |
} |