| 1 |
/* |
| 2 |
* Peers |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-rbtree.h> |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/epoll.h> |
| 9 |
#include <sys/socket.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include "bus/bus.h" |
| 12 |
#include "bus/driver.h" |
| 13 |
#include "bus/match.h" |
| 14 |
#include "bus/name.h" |
| 15 |
#include "bus/peer.h" |
| 16 |
#include "bus/policy.h" |
| 17 |
#include "bus/reply.h" |
| 18 |
#include "catalog/catalog-ids.h" |
| 19 |
#include "dbus/address.h" |
| 20 |
#include "dbus/message.h" |
| 21 |
#include "dbus/protocol.h" |
| 22 |
#include "dbus/socket.h" |
| 23 |
#include "util/dispatch.h" |
| 24 |
#include "util/error.h" |
| 25 |
#include "util/fdlist.h" |
| 26 |
#include "util/log.h" |
| 27 |
#include "util/metrics.h" |
| 28 |
#include "util/sockopt.h" |
| 29 |
#include "util/user.h" |
| 30 |
|
| 31 |
static int peer_dispatch_connection(Peer *peer, uint32_t events) { |
| 32 |
int r; |
| 33 |
|
| 34 |
if (!events) |
| 35 |
return 0; |
| 36 |
|
| 37 |
r = connection_dispatch(&peer->connection, events); |
| 38 |
if (r) |
| 39 |
return error_fold(r); |
| 40 |
|
| 41 |
for (;;) { |
| 42 |
_c_cleanup_(message_unrefp) Message *m = NULL; |
| 43 |
|
| 44 |
r = connection_dequeue(&peer->connection, &m); |
| 45 |
if (r == CONNECTION_E_EOF) { |
| 46 |
return PEER_E_EOF; |
| 47 |
} else if (r) { |
| 48 |
NameSet peer_names = NAME_SET_INIT_FROM_OWNER(&peer->owned_names); |
| 49 |
|
| 50 |
if (r == CONNECTION_E_QUOTA) { |
| 51 |
log_append_here(peer->bus->log, LOG_WARNING, 0, NULL); |
| 52 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 53 |
|
| 54 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it does not have the resources to perform an operation.", |
| 55 |
peer->id); |
| 56 |
if (r) |
| 57 |
return error_fold(r); |
| 58 |
|
| 59 |
return PEER_E_QUOTA; |
| 60 |
} else if (r == CONNECTION_E_SASL_VIOLATION) { |
| 61 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 62 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 63 |
|
| 64 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it violated the SASL protocol.", |
| 65 |
peer->id); |
| 66 |
if (r) |
| 67 |
return error_fold(r); |
| 68 |
|
| 69 |
return PEER_E_PROTOCOL_VIOLATION; |
| 70 |
} else if (r == CONNECTION_E_UNEXPECTED_FDS) { |
| 71 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 72 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 73 |
|
| 74 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it attempted to pass file descriptors without negotiating support for it.", |
| 75 |
peer->id); |
| 76 |
if (r) |
| 77 |
return error_fold(r); |
| 78 |
|
| 79 |
return PEER_E_PROTOCOL_VIOLATION; |
| 80 |
} |
| 81 |
|
| 82 |
return error_fold(r); |
| 83 |
} else if (!m) { |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
r = message_parse_metadata(m); |
| 88 |
if (r) { |
| 89 |
NameSet peer_names = NAME_SET_INIT_FROM_OWNER(&peer->owned_names); |
| 90 |
|
| 91 |
if (r == MESSAGE_E_INVALID_HEADER) { |
| 92 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 93 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 94 |
|
| 95 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it sent a message with an invalid header.", |
| 96 |
peer->id); |
| 97 |
if (r) |
| 98 |
return error_fold(r); |
| 99 |
|
| 100 |
return PEER_E_PROTOCOL_VIOLATION; |
| 101 |
} else if (r == MESSAGE_E_INVALID_BODY) { |
| 102 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 103 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 104 |
|
| 105 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it sent a message with an invalid body.", |
| 106 |
peer->id); |
| 107 |
if (r) |
| 108 |
return error_fold(r); |
| 109 |
|
| 110 |
return PEER_E_PROTOCOL_VIOLATION; |
| 111 |
} else if (r == MESSAGE_E_MISSING_FDS) { |
| 112 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 113 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 114 |
|
| 115 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it passed fewer file descriptors than its header declared.", |
| 116 |
peer->id); |
| 117 |
if (r) |
| 118 |
return error_fold(r); |
| 119 |
|
| 120 |
return PEER_E_PROTOCOL_VIOLATION; |
| 121 |
} |
| 122 |
|
| 123 |
return error_fold(r); |
| 124 |
} |
| 125 |
|
| 126 |
message_stitch_sender(m, peer->id); |
| 127 |
|
| 128 |
metrics_sample_start(&peer->bus->metrics); |
| 129 |
r = driver_dispatch(peer, m); |
| 130 |
metrics_sample_end(&peer->bus->metrics); |
| 131 |
if (r) { |
| 132 |
NameSet peer_names = NAME_SET_INIT_FROM_OWNER(&peer->owned_names); |
| 133 |
|
| 134 |
if (r == DRIVER_E_PEER_NOT_REGISTERED) { |
| 135 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 136 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 137 |
message_log_append(m, peer->bus->log); |
| 138 |
|
| 139 |
r = log_commitf(peer->bus->log, "Peer :1.%llu is being disconnected as it sent a message before calling Hello().", |
| 140 |
peer->id); |
| 141 |
if (r) |
| 142 |
return error_fold(r); |
| 143 |
|
| 144 |
return PEER_E_PROTOCOL_VIOLATION; |
| 145 |
} else if (r == DRIVER_E_MONITOR_READ_ONLY) { |
| 146 |
log_append_here(peer->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_PROTOCOL_VIOLATION); |
| 147 |
bus_log_append_sender(peer->bus, peer->id, &peer_names, peer->policy->seclabel); |
| 148 |
message_log_append(m, peer->bus->log); |
| 149 |
|
| 150 |
r = log_commitf(peer->bus->log, "Monitor :1.%llu is being disconnected as it attempted to send a message.", |
| 151 |
peer->id); |
| 152 |
if (r) |
| 153 |
return error_fold(r); |
| 154 |
|
| 155 |
return PEER_E_PROTOCOL_VIOLATION; |
| 156 |
} |
| 157 |
|
| 158 |
return error_fold(r); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
int peer_dispatch(DispatchFile *file) { |
| 166 |
Peer *peer = c_container_of(file, Peer, connection.socket_file); |
| 167 |
static const uint32_t interest[] = { EPOLLIN | EPOLLHUP, EPOLLOUT }; |
| 168 |
size_t i; |
| 169 |
int r; |
| 170 |
|
| 171 |
/* |
| 172 |
* Usually, we would just call |
| 173 |
* peer_dispatch_connection(peer, dispatch_file_events(file)) here. |
| 174 |
* However, a very common scenario is to dispatch D-Bus driver calls. |
| 175 |
* Those calls fetch an incoming message from a peer, handle it and |
| 176 |
* then immediately queue a reply. In those cases we want EPOLLOUT |
| 177 |
* to be handled late. Hence, rather than dispatching the connection |
| 178 |
* in one go, we rather split it into two: |
| 179 |
* |
| 180 |
* peer_dispatch_connection(peer, EPOLLIN | EPOLLHUP); |
| 181 |
* peer_dispatch_connection(peer, EPOLLOUT); |
| 182 |
* |
| 183 |
* This makes sure to first handle all the incoming messages, then the |
| 184 |
* outgointg messages. |
| 185 |
* |
| 186 |
* Note that it is not enough to simply delay the call to |
| 187 |
* connection_dispatch(EPOLLOUT). The socket API requires you to loop |
| 188 |
* over connection_dequeue() after *ANY* call to the dispatcher. This |
| 189 |
* is, because the dequeue function is considered to be the event |
| 190 |
* handler, and as such the only function that performs forward |
| 191 |
* progress on the socket. |
| 192 |
* |
| 193 |
* Furthermore, note that we must not cache the events but rather query |
| 194 |
* dispatch_file_events(), since the connection handler might select or |
| 195 |
* deselect events we want to handle. |
| 196 |
* |
| 197 |
* Lastly, the connection API explicitly allows splitting the events. |
| 198 |
* There is no requirement to provide them in-order. |
| 199 |
*/ |
| 200 |
for (i = 0; i < C_ARRAY_SIZE(interest); ++i) { |
| 201 |
r = peer_dispatch_connection(peer, dispatch_file_events(file) & interest[i]); |
| 202 |
if (r) |
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
if (r) { |
| 207 |
if (r == PEER_E_EOF) { |
| 208 |
metrics_sample_start(&peer->bus->metrics); |
| 209 |
r = driver_goodbye(peer, false); |
| 210 |
metrics_sample_end(&peer->bus->metrics); |
| 211 |
if (r) |
| 212 |
return error_fold(r); |
| 213 |
|
| 214 |
connection_shutdown(&peer->connection); |
| 215 |
} else if (r == PEER_E_QUOTA || |
| 216 |
r == PEER_E_PROTOCOL_VIOLATION) { |
| 217 |
connection_close(&peer->connection); |
| 218 |
|
| 219 |
metrics_sample_start(&peer->bus->metrics); |
| 220 |
r = driver_goodbye(peer, false); |
| 221 |
metrics_sample_end(&peer->bus->metrics); |
| 222 |
if (r) |
| 223 |
return error_fold(r); |
| 224 |
} else { |
| 225 |
return error_fold(r); |
| 226 |
} |
| 227 |
|
| 228 |
if (!connection_is_running(&peer->connection)) |
| 229 |
peer_free(peer); |
| 230 |
} |
| 231 |
|
| 232 |
/* Careful: @peer might be deallocated here */ |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static int peer_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 238 |
Peer *peer = c_container_of(rb, Peer, registry_node); |
| 239 |
uint64_t id = *(uint64_t*)k; |
| 240 |
|
| 241 |
if (id < peer->id) |
| 242 |
return -1; |
| 243 |
if (id > peer->id) |
| 244 |
return 1; |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* peer_new() - XXX |
| 251 |
*/ |
| 252 |
int peer_new_with_fd(Peer **peerp, |
| 253 |
Bus *bus, |
| 254 |
PolicyRegistry *policy, |
| 255 |
const char guid[], |
| 256 |
DispatchContext *dispatcher, |
| 257 |
int fd) { |
| 258 |
_c_cleanup_(peer_freep) Peer *peer = NULL; |
| 259 |
_c_cleanup_(user_unrefp) User *user = NULL; |
| 260 |
_c_cleanup_(c_freep) gid_t *gids = NULL; |
| 261 |
_c_cleanup_(c_freep) char *seclabel = NULL; |
| 262 |
_c_cleanup_(c_closep) int pid_fd = -1; |
| 263 |
CRBNode **slot, *parent; |
| 264 |
size_t n_seclabel, n_gids = 0; |
| 265 |
struct ucred ucred; |
| 266 |
socklen_t socklen = sizeof(ucred); |
| 267 |
int r; |
| 268 |
|
| 269 |
r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &socklen); |
| 270 |
if (r < 0) |
| 271 |
return error_origin(-errno); |
| 272 |
|
| 273 |
r = user_registry_ref_user(&bus->users, &user, ucred.uid); |
| 274 |
if (r) |
| 275 |
return error_fold(r); |
| 276 |
|
| 277 |
r = sockopt_get_peersec(fd, &seclabel, &n_seclabel); |
| 278 |
if (r) |
| 279 |
return error_fold(r); |
| 280 |
|
| 281 |
r = sockopt_get_peergroups(fd, bus->log, ucred.uid, ucred.gid, &gids, &n_gids); |
| 282 |
if (r) |
| 283 |
return error_fold(r); |
| 284 |
|
| 285 |
r = sockopt_get_peerpidfd(fd, &pid_fd); |
| 286 |
if (r) { |
| 287 |
if (r != SOCKOPT_E_UNSUPPORTED && |
| 288 |
r != SOCKOPT_E_UNAVAILABLE && |
| 289 |
r != SOCKOPT_E_REAPED) |
| 290 |
return error_fold(r); |
| 291 |
|
| 292 |
/* keep `pid_fd == -1` if unavailable */ |
| 293 |
} |
| 294 |
|
| 295 |
peer = calloc(1, sizeof(*peer)); |
| 296 |
if (!peer) |
| 297 |
return error_origin(-ENOMEM); |
| 298 |
*peer = (Peer)PEER_INIT(*peer); |
| 299 |
|
| 300 |
peer->bus = bus; |
| 301 |
++peer->bus->peers.n_peers; |
| 302 |
peer->user = user; |
| 303 |
user = NULL; |
| 304 |
peer->pid = ucred.pid; |
| 305 |
peer->pid_fd = pid_fd; |
| 306 |
pid_fd = -1; |
| 307 |
peer->gids = gids; |
| 308 |
gids = NULL; |
| 309 |
peer->n_gids = n_gids; |
| 310 |
peer->seclabel = seclabel; |
| 311 |
seclabel = NULL; |
| 312 |
peer->n_seclabel = n_seclabel; |
| 313 |
|
| 314 |
r = user_charge(peer->user, &peer->charges[0], NULL, USER_SLOT_BYTES, sizeof(Peer)); |
| 315 |
r = r ?: user_charge(peer->user, &peer->charges[1], NULL, USER_SLOT_FDS, 1); |
| 316 |
r = r ?: user_charge(peer->user, &peer->charges[2], NULL, USER_SLOT_OBJECTS, 1); |
| 317 |
if (r) { |
| 318 |
if (r == USER_E_QUOTA) |
| 319 |
return PEER_E_QUOTA; |
| 320 |
|
| 321 |
return error_fold(r); |
| 322 |
} |
| 323 |
|
| 324 |
r = policy_snapshot_new(&peer->policy, policy, peer->seclabel, ucred.uid, peer->gids, peer->n_gids); |
| 325 |
if (r) |
| 326 |
return error_fold(r); |
| 327 |
|
| 328 |
r = policy_snapshot_check_connect(peer->policy); |
| 329 |
if (r) |
| 330 |
return (r == POLICY_E_ACCESS_DENIED) ? PEER_E_CONNECTION_REFUSED : error_fold(r); |
| 331 |
|
| 332 |
r = connection_init_server(&peer->connection, |
| 333 |
dispatcher, |
| 334 |
peer_dispatch, |
| 335 |
peer->user, |
| 336 |
guid, |
| 337 |
fd); |
| 338 |
if (r < 0) |
| 339 |
return error_fold(r); |
| 340 |
|
| 341 |
peer->id = bus->peers.ids++; |
| 342 |
slot = c_rbtree_find_slot(&bus->peers.peer_tree, peer_compare, &peer->id, &parent); |
| 343 |
c_assert(slot); /* peer->id is guaranteed to be unique */ |
| 344 |
c_rbtree_add(&bus->peers.peer_tree, parent, slot, &peer->registry_node); |
| 345 |
|
| 346 |
*peerp = peer; |
| 347 |
peer = NULL; |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* peer_free() - XXX |
| 353 |
*/ |
| 354 |
Peer *peer_free(Peer *peer) { |
| 355 |
int fd; |
| 356 |
|
| 357 |
if (!peer) |
| 358 |
return NULL; |
| 359 |
|
| 360 |
c_assert(!peer->registered); |
| 361 |
|
| 362 |
c_rbnode_unlink(&peer->registry_node); |
| 363 |
c_list_unlink(&peer->listener_link); |
| 364 |
|
| 365 |
fd = peer->connection.socket.fd; |
| 366 |
|
| 367 |
c_close(peer->pid_fd); |
| 368 |
reply_owner_deinit(&peer->owned_replies); |
| 369 |
reply_registry_deinit(&peer->replies); |
| 370 |
match_owner_deinit(&peer->owned_matches); |
| 371 |
match_registry_deinit(&peer->name_owner_changed_matches); |
| 372 |
match_registry_deinit(&peer->sender_matches); |
| 373 |
name_owner_deinit(&peer->owned_names); |
| 374 |
policy_snapshot_free(peer->policy); |
| 375 |
connection_deinit(&peer->connection); |
| 376 |
user_charge_deinit(&peer->charges[2]); |
| 377 |
user_charge_deinit(&peer->charges[1]); |
| 378 |
user_charge_deinit(&peer->charges[0]); |
| 379 |
free(peer->seclabel); |
| 380 |
free(peer->gids); |
| 381 |
user_unref(peer->user); |
| 382 |
--peer->bus->peers.n_peers; |
| 383 |
free(peer); |
| 384 |
|
| 385 |
close(fd); |
| 386 |
|
| 387 |
return NULL; |
| 388 |
} |
| 389 |
|
| 390 |
int peer_spawn(Peer *peer) { |
| 391 |
return error_fold(connection_open(&peer->connection)); |
| 392 |
} |
| 393 |
|
| 394 |
void peer_register(Peer *peer) { |
| 395 |
c_assert(!peer->registered); |
| 396 |
c_assert(!peer->monitor); |
| 397 |
|
| 398 |
peer->registered = true; |
| 399 |
++peer->bus->peers.n_registered; |
| 400 |
} |
| 401 |
|
| 402 |
void peer_unregister(Peer *peer) { |
| 403 |
c_assert(peer->registered); |
| 404 |
c_assert(!peer->monitor); |
| 405 |
|
| 406 |
--peer->bus->peers.n_registered; |
| 407 |
peer->registered = false; |
| 408 |
} |
| 409 |
|
| 410 |
bool peer_is_privileged(Peer *peer) { |
| 411 |
if (peer->user->uid == 0) |
| 412 |
return true; |
| 413 |
|
| 414 |
if (peer->user->uid == peer->bus->user->uid) |
| 415 |
return true; |
| 416 |
|
| 417 |
return false; |
| 418 |
} |
| 419 |
|
| 420 |
int peer_request_name(Peer *peer, const char *name, uint32_t flags, NameChange *change) { |
| 421 |
int r; |
| 422 |
|
| 423 |
if (!strcmp(name, "org.freedesktop.DBus")) |
| 424 |
return PEER_E_NAME_RESERVED; |
| 425 |
|
| 426 |
if (name[0] == ':') |
| 427 |
return PEER_E_NAME_UNIQUE; |
| 428 |
|
| 429 |
r = policy_snapshot_check_own(peer->policy, name); |
| 430 |
if (r) { |
| 431 |
if (r == POLICY_E_ACCESS_DENIED || |
| 432 |
r == POLICY_E_SELINUX_ACCESS_DENIED || |
| 433 |
r == POLICY_E_APPARMOR_ACCESS_DENIED) |
| 434 |
return PEER_E_NAME_REFUSED; |
| 435 |
|
| 436 |
return error_fold(r); |
| 437 |
} |
| 438 |
|
| 439 |
r = name_registry_request_name(&peer->bus->names, |
| 440 |
&peer->owned_names, |
| 441 |
peer->user, |
| 442 |
name, |
| 443 |
flags, |
| 444 |
change); |
| 445 |
if (r == NAME_E_QUOTA) |
| 446 |
return PEER_E_QUOTA; |
| 447 |
else if (r == NAME_E_ALREADY_OWNER) |
| 448 |
return PEER_E_NAME_ALREADY_OWNER; |
| 449 |
else if (r == NAME_E_IN_QUEUE) |
| 450 |
return PEER_E_NAME_IN_QUEUE; |
| 451 |
else if (r == NAME_E_EXISTS) |
| 452 |
return PEER_E_NAME_EXISTS; |
| 453 |
else if (r) |
| 454 |
return error_fold(r); |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
int peer_release_name(Peer *peer, const char *name, NameChange *change) { |
| 460 |
int r; |
| 461 |
|
| 462 |
if (!strcmp(name, "org.freedesktop.DBus")) |
| 463 |
return PEER_E_NAME_RESERVED; |
| 464 |
|
| 465 |
if (name[0] == ':') |
| 466 |
return PEER_E_NAME_UNIQUE; |
| 467 |
|
| 468 |
r = name_registry_release_name(&peer->bus->names, &peer->owned_names, name, change); |
| 469 |
if (r == NAME_E_NOT_FOUND) |
| 470 |
return PEER_E_NAME_NOT_FOUND; |
| 471 |
else if (r == NAME_E_NOT_OWNER) |
| 472 |
return PEER_E_NAME_NOT_OWNER; |
| 473 |
else if (r) |
| 474 |
return error_fold(r); |
| 475 |
|
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
void peer_release_name_ownership(Peer *peer, NameOwnership *ownership, NameChange *change) { |
| 480 |
name_ownership_release(ownership, change); |
| 481 |
} |
| 482 |
|
| 483 |
static int peer_link_match(Peer *peer, MatchRule *rule, bool monitor) { |
| 484 |
MatchCounters *counters = NULL; |
| 485 |
Address addr; |
| 486 |
Peer *sender, *owner; |
| 487 |
int r; |
| 488 |
|
| 489 |
if (!monitor) |
| 490 |
counters = &peer->bus->match_counters; |
| 491 |
|
| 492 |
if (!rule->keys.sender) { |
| 493 |
r = match_rule_link(rule, counters, &peer->bus->wildcard_matches, monitor); |
| 494 |
if (r) |
| 495 |
return error_fold(r); |
| 496 |
} else if (strcmp(rule->keys.sender, "org.freedesktop.DBus") == 0) { |
| 497 |
if (rule->keys.filter.member && |
| 498 |
strcmp(rule->keys.filter.member, "NameOwnerChanged") == 0 && |
| 499 |
rule->keys.filter.args[0] && |
| 500 |
strcmp(rule->keys.filter.args[0], "org.freedesktop.DBus") != 0) { |
| 501 |
/* |
| 502 |
* This rule is a subscription to NameOwnerChanged signals on a specific name, |
| 503 |
* link it on the name or peer that may trigger it. |
| 504 |
*/ |
| 505 |
address_from_string(&addr, rule->keys.filter.args[0]); |
| 506 |
switch (addr.type) { |
| 507 |
case ADDRESS_TYPE_ID: { |
| 508 |
owner = peer_registry_find_peer(&peer->bus->peers, addr.id); |
| 509 |
if (owner) { |
| 510 |
r = match_rule_link(rule, counters, &owner->name_owner_changed_matches, monitor); |
| 511 |
if (r) |
| 512 |
return error_fold(r); |
| 513 |
} else if (addr.id >= peer->bus->peers.ids) { |
| 514 |
/* |
| 515 |
* This peer does not yet exist, but it could |
| 516 |
* appear, keep it with the wildcards. It will |
| 517 |
* stay there even if the peer later appears. |
| 518 |
* This works and is meant for compatibility. |
| 519 |
* It does not perform nicely, but there is |
| 520 |
* also no reason to ever guess the ID of a |
| 521 |
* forthcoming peer, so this is most likely |
| 522 |
* a bug in a client. |
| 523 |
*/ |
| 524 |
r = match_rule_link(rule, counters, &peer->bus->sender_matches, monitor); |
| 525 |
if (r) |
| 526 |
return error_fold(r); |
| 527 |
} else { |
| 528 |
/* |
| 529 |
* The peer has already disconnected and will |
| 530 |
* never reappear, since the ID allocator is |
| 531 |
* already beyond the ID. |
| 532 |
* We can simply skip linking the rule, since |
| 533 |
* it can never have an effect. It stays linked |
| 534 |
* in its owner, though, so we don't lose |
| 535 |
* track. |
| 536 |
*/ |
| 537 |
} |
| 538 |
break; |
| 539 |
} |
| 540 |
case ADDRESS_TYPE_NAME: |
| 541 |
case ADDRESS_TYPE_OTHER: { |
| 542 |
_c_cleanup_(name_unrefp) Name *name = NULL; |
| 543 |
|
| 544 |
r = name_registry_ref_name(&peer->bus->names, &name, rule->keys.filter.args[0]); |
| 545 |
if (r) |
| 546 |
return error_fold(r); |
| 547 |
|
| 548 |
r = match_rule_link(rule, counters, &name->name_owner_changed_matches, monitor); |
| 549 |
if (r) |
| 550 |
return error_fold(r); |
| 551 |
name_ref(name); /* this reference must be explicitly released */ |
| 552 |
break; |
| 553 |
} |
| 554 |
default: |
| 555 |
return error_origin(-ENOTRECOVERABLE); |
| 556 |
} |
| 557 |
} else { |
| 558 |
/* |
| 559 |
* This should be a wildcard match on all NameOwnerChanged signals, we also |
| 560 |
* install other (unexpected) matches here, they will always be false negatives |
| 561 |
* but for the sake of simplicity we do not attempt to optimize them away. |
| 562 |
*/ |
| 563 |
r = match_rule_link(rule, counters, &peer->bus->sender_matches, monitor); |
| 564 |
if (r) |
| 565 |
return error_fold(r); |
| 566 |
} |
| 567 |
} else { |
| 568 |
address_from_string(&addr, rule->keys.sender); |
| 569 |
switch (addr.type) { |
| 570 |
case ADDRESS_TYPE_ID: { |
| 571 |
sender = peer_registry_find_peer(&peer->bus->peers, addr.id); |
| 572 |
if (sender) { |
| 573 |
r = match_rule_link(rule, counters, &sender->sender_matches, monitor); |
| 574 |
if (r) |
| 575 |
return error_fold(r); |
| 576 |
} else if (addr.id >= peer->bus->peers.ids) { |
| 577 |
/* |
| 578 |
* This peer does not yet exist, by the same |
| 579 |
* reasoning as above, keep it as a wildcard |
| 580 |
* match. |
| 581 |
*/ |
| 582 |
r = match_rule_link(rule, counters, &peer->bus->wildcard_matches, monitor); |
| 583 |
if (r) |
| 584 |
return error_fold(r); |
| 585 |
} else { |
| 586 |
/* |
| 587 |
* The peer has already disconnected and will |
| 588 |
* never reappear, don't link it, by the same |
| 589 |
* logic as above. |
| 590 |
*/ |
| 591 |
} |
| 592 |
break; |
| 593 |
} |
| 594 |
case ADDRESS_TYPE_NAME: |
| 595 |
case ADDRESS_TYPE_OTHER: { |
| 596 |
_c_cleanup_(name_unrefp) Name *name = NULL; |
| 597 |
|
| 598 |
r = name_registry_ref_name(&peer->bus->names, &name, rule->keys.sender); |
| 599 |
if (r) |
| 600 |
return error_fold(r); |
| 601 |
|
| 602 |
r = match_rule_link(rule, counters, &name->sender_matches, monitor); |
| 603 |
if (r) |
| 604 |
return error_fold(r); |
| 605 |
name_ref(name); /* this reference must be explicitly released */ |
| 606 |
break; |
| 607 |
} |
| 608 |
default: |
| 609 |
return error_origin(-ENOTRECOVERABLE); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
int peer_add_match(Peer *peer, const char *rule_string) { |
| 617 |
_c_cleanup_(match_rule_user_unrefp) MatchRule *rule = NULL; |
| 618 |
int r; |
| 619 |
|
| 620 |
r = match_owner_ref_rule(&peer->owned_matches, &rule, peer->user, rule_string, false); |
| 621 |
if (r) { |
| 622 |
if (r == MATCH_E_QUOTA) |
| 623 |
return PEER_E_QUOTA; |
| 624 |
else if (r == MATCH_E_INVALID) |
| 625 |
return PEER_E_MATCH_INVALID; |
| 626 |
else |
| 627 |
return error_fold(r); |
| 628 |
} |
| 629 |
|
| 630 |
r = peer_link_match(peer, rule, false); |
| 631 |
if (r) |
| 632 |
return error_trace(r); |
| 633 |
|
| 634 |
rule = NULL; |
| 635 |
|
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static Name *peer_match_rule_to_name(MatchRule *rule) { |
| 640 |
if (!rule->keys.sender) |
| 641 |
return NULL; |
| 642 |
/* |
| 643 |
* A match can be associated with a name in two cases: |
| 644 |
* - if it is a NameOwnerChanged subscription on the name, or |
| 645 |
* - if it is a sender match on the name. |
| 646 |
*/ |
| 647 |
if (strcmp(rule->keys.sender, "org.freedesktop.DBus") == 0) { |
| 648 |
if (rule->keys.filter.member && strcmp(rule->keys.filter.member, "NameOwnerChanged") == 0 && |
| 649 |
rule->keys.filter.args[0] && strcmp(rule->keys.filter.args[0], "org.freedesktop.DBus") != 0 && |
| 650 |
rule->keys.filter.args[0][0] != ':') |
| 651 |
return c_container_of(rule->registry, Name, name_owner_changed_matches); |
| 652 |
} else if (rule->keys.sender[0] != ':') { |
| 653 |
return c_container_of(rule->registry, Name, sender_matches); |
| 654 |
} |
| 655 |
|
| 656 |
return NULL; |
| 657 |
} |
| 658 |
|
| 659 |
int peer_remove_match(Peer *peer, const char *rule_string) { |
| 660 |
_c_cleanup_(name_unrefp) _c_unused_ Name *name = NULL; |
| 661 |
MatchRule *rule; |
| 662 |
int r; |
| 663 |
|
| 664 |
r = match_owner_find_rule(&peer->owned_matches, &rule, rule_string); |
| 665 |
if (r == MATCH_E_INVALID) |
| 666 |
return PEER_E_MATCH_INVALID; |
| 667 |
else if (r) |
| 668 |
return error_fold(r); |
| 669 |
else if (!rule) |
| 670 |
return PEER_E_MATCH_NOT_FOUND; |
| 671 |
|
| 672 |
/* |
| 673 |
* A match may pin a name, in which case first get the name from the |
| 674 |
* rule, then unref the rule, before finally unreffing the name. |
| 675 |
*/ |
| 676 |
name = peer_match_rule_to_name(rule); |
| 677 |
|
| 678 |
match_rule_user_unref(rule); |
| 679 |
|
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
int peer_become_monitor(Peer *peer, MatchOwner *owned_matches) { |
| 684 |
MatchRule *rule; |
| 685 |
size_t i, n_user_refs; |
| 686 |
int r, poison = 0; |
| 687 |
|
| 688 |
c_assert(!peer->registered); |
| 689 |
c_assert(!peer->monitor); |
| 690 |
c_assert(c_rbtree_is_empty(&peer->owned_matches.rule_tree)); |
| 691 |
|
| 692 |
/* only fatal errors may occur after this point */ |
| 693 |
match_owner_move(&peer->owned_matches, owned_matches); |
| 694 |
|
| 695 |
c_rbtree_for_each_entry(rule, &peer->owned_matches.rule_tree, owner_node) { |
| 696 |
rule->owner = &peer->owned_matches; |
| 697 |
n_user_refs = rule->n_user_refs; |
| 698 |
|
| 699 |
/* |
| 700 |
* Link once for each match instance, in case the user provided |
| 701 |
* duplicate matches. No need to optimize this; treat it as |
| 702 |
* individual matches and mirror `peer_add_match()`. We |
| 703 |
* prefetch the user-refs to guarantee they are constant (in |
| 704 |
* case `peer_link_match()` would ever want to acquire more |
| 705 |
* user-refs, for whatever reason). |
| 706 |
*/ |
| 707 |
for (i = 0; i < n_user_refs; ++i) { |
| 708 |
r = peer_link_match(peer, rule, true); |
| 709 |
if (r && !poison) |
| 710 |
poison = error_trace(r); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
if (poison) |
| 715 |
/* a fatal error occured, the peer was modified, but still consistent */ |
| 716 |
return poison; |
| 717 |
|
| 718 |
peer->monitor = true; |
| 719 |
++peer->bus->n_monitors; |
| 720 |
|
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
void peer_stop_monitor(Peer *peer) { |
| 725 |
c_assert(!peer->registered); |
| 726 |
c_assert(peer->monitor); |
| 727 |
c_assert(c_rbtree_is_empty(&peer->owned_matches.rule_tree)); |
| 728 |
|
| 729 |
peer->monitor = false; |
| 730 |
--peer->bus->n_monitors; |
| 731 |
} |
| 732 |
|
| 733 |
void peer_flush_matches(Peer *peer) { |
| 734 |
CRBNode *node; |
| 735 |
|
| 736 |
while ((node = peer->owned_matches.rule_tree.root)) { |
| 737 |
_c_cleanup_(name_unrefp) _c_unused_ Name *name = NULL; |
| 738 |
MatchRule *rule = c_container_of(node, MatchRule, owner_node); |
| 739 |
|
| 740 |
/* |
| 741 |
* As above, a match may pin a name. |
| 742 |
*/ |
| 743 |
name = peer_match_rule_to_name(rule); |
| 744 |
|
| 745 |
match_rule_user_unref(rule); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
static int peer_map_denied_error(int err) { |
| 750 |
switch (err) { |
| 751 |
case POLICY_E_SELINUX_ACCESS_DENIED: |
| 752 |
return BUS_LOG_POLICY_TYPE_SELINUX; |
| 753 |
case POLICY_E_APPARMOR_ACCESS_DENIED: |
| 754 |
return BUS_LOG_POLICY_TYPE_APPARMOR; |
| 755 |
default: |
| 756 |
return BUS_LOG_POLICY_TYPE_INTERNAL; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
int peer_queue_unicast(PolicySnapshot *sender_policy, NameSet *sender_names, ReplyOwner *sender_replies, User *sender_user, uint64_t sender_id, Peer *receiver, Message *message) { |
| 761 |
_c_cleanup_(reply_slot_freep) ReplySlot *slot = NULL; |
| 762 |
NameSet receiver_names = NAME_SET_INIT_FROM_OWNER(&receiver->owned_names); |
| 763 |
uint32_t serial; |
| 764 |
int r; |
| 765 |
|
| 766 |
serial = message_read_serial(message); |
| 767 |
|
| 768 |
if (sender_replies && serial) { |
| 769 |
r = reply_slot_new(&slot, &receiver->replies, sender_replies, |
| 770 |
receiver->user, sender_user, sender_id, serial); |
| 771 |
if (r == REPLY_E_EXISTS) |
| 772 |
return PEER_E_EXPECTED_REPLY_EXISTS; |
| 773 |
else if (r == REPLY_E_QUOTA) |
| 774 |
return PEER_E_QUOTA; |
| 775 |
else if (r) |
| 776 |
return error_fold(r); |
| 777 |
} |
| 778 |
|
| 779 |
r = policy_snapshot_check_receive(receiver->policy, |
| 780 |
sender_policy->seclabel, |
| 781 |
sender_names, |
| 782 |
sender_id, |
| 783 |
message->metadata.fields.interface, |
| 784 |
message->metadata.fields.member, |
| 785 |
message->metadata.fields.path, |
| 786 |
message->header->type, |
| 787 |
false, |
| 788 |
message->metadata.fields.unix_fds); |
| 789 |
if (r) { |
| 790 |
if (r == POLICY_E_ACCESS_DENIED || |
| 791 |
r == POLICY_E_SELINUX_ACCESS_DENIED || |
| 792 |
r == POLICY_E_APPARMOR_ACCESS_DENIED) { |
| 793 |
log_append_here(receiver->bus->log, LOG_WARNING, 0, NULL); |
| 794 |
bus_log_append_policy_receive(receiver->bus, |
| 795 |
peer_map_denied_error(r), |
| 796 |
receiver->id, sender_id, sender_names, &receiver_names, message); |
| 797 |
r = log_commitf(receiver->bus->log, "A security policy denied %s to receive %s %s:%s.%s from :1.%llu.", |
| 798 |
message->metadata.fields.destination, |
| 799 |
message->header->type == DBUS_MESSAGE_TYPE_METHOD_CALL ? "method call" : "signal", |
| 800 |
message->metadata.fields.path, message->metadata.fields.interface, message->metadata.fields.member, |
| 801 |
sender_id); |
| 802 |
if (r) |
| 803 |
return error_fold(r); |
| 804 |
|
| 805 |
return PEER_E_RECEIVE_DENIED; |
| 806 |
} |
| 807 |
|
| 808 |
return error_fold(r); |
| 809 |
} |
| 810 |
|
| 811 |
r = policy_snapshot_check_send(sender_policy, |
| 812 |
receiver->seclabel, |
| 813 |
&receiver_names, |
| 814 |
receiver->id, |
| 815 |
message->metadata.fields.interface, |
| 816 |
message->metadata.fields.member, |
| 817 |
message->metadata.fields.path, |
| 818 |
message->header->type, |
| 819 |
false, |
| 820 |
message->metadata.fields.unix_fds); |
| 821 |
if (r) { |
| 822 |
if (r == POLICY_E_ACCESS_DENIED || |
| 823 |
r == POLICY_E_SELINUX_ACCESS_DENIED || |
| 824 |
r == POLICY_E_APPARMOR_ACCESS_DENIED) { |
| 825 |
log_append_here(receiver->bus->log, LOG_WARNING, 0, NULL); |
| 826 |
bus_log_append_policy_send(receiver->bus, |
| 827 |
peer_map_denied_error(r), |
| 828 |
sender_id, receiver->id, sender_names, &receiver_names, |
| 829 |
sender_policy->seclabel, receiver->policy->seclabel, message); |
| 830 |
r = log_commitf(receiver->bus->log, "A security policy denied :1.%llu to send %s %s:%s.%s to %s.", |
| 831 |
sender_id, |
| 832 |
message->header->type == DBUS_MESSAGE_TYPE_METHOD_CALL ? "method call" : "signal", |
| 833 |
message->metadata.fields.path, message->metadata.fields.interface, message->metadata.fields.member, |
| 834 |
message->metadata.fields.destination); |
| 835 |
if (r) |
| 836 |
return error_fold(r); |
| 837 |
|
| 838 |
return PEER_E_SEND_DENIED; |
| 839 |
} |
| 840 |
|
| 841 |
return error_fold(r); |
| 842 |
} |
| 843 |
|
| 844 |
r = connection_queue(&receiver->connection, sender_user, message); |
| 845 |
if (r) { |
| 846 |
if (r == CONNECTION_E_QUOTA) |
| 847 |
return PEER_E_QUOTA; |
| 848 |
else if (r == CONNECTION_E_UNEXPECTED_FDS) |
| 849 |
return PEER_E_UNEXPECTED_FDS; |
| 850 |
|
| 851 |
return error_fold(r); |
| 852 |
} |
| 853 |
|
| 854 |
slot = NULL; |
| 855 |
return 0; |
| 856 |
} |
| 857 |
|
| 858 |
int peer_queue_reply(Peer *sender, const char *destination, uint32_t reply_serial, Message *message) { |
| 859 |
_c_cleanup_(reply_slot_freep) ReplySlot *slot = NULL; |
| 860 |
Peer *receiver; |
| 861 |
Address addr; |
| 862 |
int r; |
| 863 |
|
| 864 |
address_from_string(&addr, destination); |
| 865 |
if (addr.type != ADDRESS_TYPE_ID) |
| 866 |
return PEER_E_UNEXPECTED_REPLY; |
| 867 |
|
| 868 |
slot = reply_slot_get_by_id(&sender->replies, addr.id, reply_serial); |
| 869 |
if (!slot) |
| 870 |
return PEER_E_UNEXPECTED_REPLY; |
| 871 |
|
| 872 |
receiver = c_container_of(slot->owner, Peer, owned_replies); |
| 873 |
|
| 874 |
r = connection_queue(&receiver->connection, NULL, message); |
| 875 |
if (r) { |
| 876 |
if (r == CONNECTION_E_QUOTA || r == CONNECTION_E_UNEXPECTED_FDS) { |
| 877 |
NameSet sender_names = NAME_SET_INIT_FROM_OWNER(&sender->owned_names); |
| 878 |
NameSet receiver_names = NAME_SET_INIT_FROM_OWNER(&receiver->owned_names); |
| 879 |
|
| 880 |
connection_shutdown(&receiver->connection); |
| 881 |
|
| 882 |
log_append_here(receiver->bus->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_RECEIVE_FAILED); |
| 883 |
bus_log_append_transaction(receiver->bus, sender->id, receiver->id, &sender_names, &receiver_names, |
| 884 |
sender->policy->seclabel, receiver->policy->seclabel, message); |
| 885 |
if (r == CONNECTION_E_QUOTA) |
| 886 |
r = log_commitf(receiver->bus->log, "Peer :1.%llu is being disconnected as it does not have the resources to receive a reply it requested.", |
| 887 |
receiver->id); |
| 888 |
else |
| 889 |
r = log_commitf(receiver->bus->log, "Peer :1.%llu is being disconnected as it does not support receiving file descriptors it requested.", |
| 890 |
receiver->id); |
| 891 |
|
| 892 |
if (r) |
| 893 |
return error_fold(r); |
| 894 |
} else { |
| 895 |
return error_fold(r); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
return 0; |
| 900 |
} |
| 901 |
|
| 902 |
void peer_registry_init(PeerRegistry *registry) { |
| 903 |
*registry = (PeerRegistry)PEER_REGISTRY_INIT; |
| 904 |
} |
| 905 |
|
| 906 |
void peer_registry_deinit(PeerRegistry *registry) { |
| 907 |
c_assert(c_rbtree_is_empty(®istry->peer_tree)); |
| 908 |
registry->ids = 0; |
| 909 |
} |
| 910 |
|
| 911 |
void peer_registry_flush(PeerRegistry *registry) { |
| 912 |
Peer *peer, *safe; |
| 913 |
int r; |
| 914 |
|
| 915 |
c_rbtree_for_each_entry_safe_postorder_unlink(peer, safe, ®istry->peer_tree, registry_node) { |
| 916 |
r = driver_goodbye(peer, true); |
| 917 |
c_assert(!r); /* can not fail in silent mode */ |
| 918 |
peer_free(peer); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
Peer *peer_registry_find_peer(PeerRegistry *registry, uint64_t id) { |
| 923 |
Peer *peer; |
| 924 |
|
| 925 |
peer = c_rbtree_find_entry(®istry->peer_tree, peer_compare, &id, Peer, registry_node); |
| 926 |
|
| 927 |
return peer && peer->registered ? peer : NULL; |
| 928 |
} |