diff options
Diffstat (limited to 'src/bus-proxyd/proxy.c')
-rw-r--r-- | src/bus-proxyd/proxy.c | 99 |
1 files changed, 78 insertions, 21 deletions
diff --git a/src/bus-proxyd/proxy.c b/src/bus-proxyd/proxy.c index 28ab1c97fc..c37b09b9c0 100644 --- a/src/bus-proxyd/proxy.c +++ b/src/bus-proxyd/proxy.c @@ -45,7 +45,7 @@ #include "formats-util.h" static int proxy_create_destination(Proxy *p, const char *destination, const char *local_sec, bool negotiate_fds) { - _cleanup_bus_close_unref_ sd_bus *b = NULL; + _cleanup_bus_flush_close_unref_ sd_bus *b = NULL; int r; r = sd_bus_new(&b); @@ -101,7 +101,7 @@ static int proxy_create_destination(Proxy *p, const char *destination, const cha } static int proxy_create_local(Proxy *p, int in_fd, int out_fd, bool negotiate_fds) { - _cleanup_bus_close_unref_ sd_bus *b = NULL; + _cleanup_bus_flush_close_unref_ sd_bus *b = NULL; sd_id128_t server_id; int r; @@ -144,6 +144,18 @@ static int proxy_create_local(Proxy *p, int in_fd, int out_fd, bool negotiate_fd return 0; } +static int proxy_match_synthetic(sd_bus_message *m, void *userdata, sd_bus_error *error) { + Proxy *p = userdata; + + p->synthetic_matched = true; + return 0; /* make sure to continue processing it in further handlers */ +} + +/* + * We always need NameOwnerChanged so we can synthesize NameLost and + * NameAcquired. Furthermore, dbus-1 always passes unicast-signals through, so + * subscribe unconditionally. + */ static int proxy_prepare_matches(Proxy *p) { _cleanup_free_ char *match = NULL; const char *unique; @@ -168,7 +180,7 @@ static int proxy_prepare_matches(Proxy *p) { if (!match) return log_oom(); - r = sd_bus_add_match(p->destination_bus, NULL, match, NULL, NULL); + r = sd_bus_add_match(p->destination_bus, NULL, match, proxy_match_synthetic, p); if (r < 0) return log_error_errno(r, "Failed to add match for NameLost: %m"); @@ -185,10 +197,24 @@ static int proxy_prepare_matches(Proxy *p) { if (!match) return log_oom(); - r = sd_bus_add_match(p->destination_bus, NULL, match, NULL, NULL); + r = sd_bus_add_match(p->destination_bus, NULL, match, proxy_match_synthetic, p); if (r < 0) return log_error_errno(r, "Failed to add match for NameAcquired: %m"); + free(match); + match = strjoin("type='signal'," + "destination='", + unique, + "'", + NULL); + if (!match) + return log_oom(); + + r = sd_bus_add_match(p->destination_bus, NULL, match, proxy_match_synthetic, p); + if (r < 0) + log_error_errno(r, "Failed to add match for directed signals: %m"); + /* FIXME: temporarily ignore error to support older kdbus versions */ + return 0; } @@ -238,8 +264,8 @@ Proxy *proxy_free(Proxy *p) { if (!p) return NULL; - sd_bus_close_unrefp(&p->local_bus); - sd_bus_close_unrefp(&p->destination_bus); + sd_bus_flush_close_unref(p->local_bus); + sd_bus_flush_close_unref(p->destination_bus); set_free_free(p->owned_names); free(p); @@ -494,7 +520,16 @@ static int process_policy_unlocked(sd_bus *from, sd_bus *to, sd_bus_message *m, } /* First check if we (the sender) can send to this name */ - if (policy_check_send(policy, our_ucred->uid, our_ucred->gid, m->header->type, NULL, destination_names, m->path, m->interface, m->member, true, &n)) { + if (sd_bus_message_is_signal(m, NULL, NULL)) { + /* If we forward a signal from dbus-1 to kdbus, we have + * no idea who the recipient is. Therefore, we cannot + * apply any dbus-1 policies that match on receiver + * credentials. We know sd-bus always sets + * KDBUS_MSG_SIGNAL, so the kernel applies policies to + * the message. Therefore, skip policy checks in this + * case. */ + return 0; + } else if (policy_check_send(policy, our_ucred->uid, our_ucred->gid, m->header->type, NULL, destination_names, m->path, m->interface, m->member, true, &n)) { if (n) { /* If we made a receiver decision, then remember which * name's policy we used, and to which unique ID it @@ -512,19 +547,8 @@ static int process_policy_unlocked(sd_bus *from, sd_bus *to, sd_bus_message *m, return r; } - if (sd_bus_message_is_signal(m, NULL, NULL)) { - /* If we forward a signal from dbus-1 to kdbus, - * we have no idea who the recipient is. - * Therefore, we cannot apply any dbus-1 - * receiver policies that match on receiver - * credentials. We know sd-bus always sets - * KDBUS_MSG_SIGNAL, so the kernel applies - * receiver policies to the message. Therefore, - * skip policy checks in this case. */ + if (policy_check_recv(policy, destination_uid, destination_gid, m->header->type, owned_names, NULL, m->path, m->interface, m->member, true)) return 0; - } else if (policy_check_recv(policy, destination_uid, destination_gid, m->header->type, owned_names, NULL, m->path, m->interface, m->member, true)) { - return 0; - } } /* Return an error back to the caller */ @@ -663,11 +687,28 @@ static int patch_sender(sd_bus *a, sd_bus_message *m) { static int proxy_process_destination_to_local(Proxy *p) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + bool matched, matched_synthetic; int r; assert(p); + /* + * Usually, we would just take any message that the bus passes to us + * and forward it to the local connection. However, there are actually + * applications that fail if they receive broadcasts that they didn't + * subscribe to. Therefore, we actually emulate a real broadcast + * matching here, and discard any broadcasts that weren't matched. Our + * match-handlers remembers whether a message was matched by any rule, + * by marking it in @p->message_matched. + */ + r = sd_bus_process(p->destination_bus, &m); + + matched = p->message_matched; + matched_synthetic = p->synthetic_matched; + p->message_matched = false; + p->synthetic_matched = false; + if (r == -ECONNRESET || r == -ENOTCONN) /* Treat 'connection reset by peer' as clean exit condition */ return r; if (r < 0) { @@ -683,12 +724,21 @@ static int proxy_process_destination_to_local(Proxy *p) { if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) return -ECONNRESET; - r = synthesize_name_acquired(p->destination_bus, p->local_bus, m); + r = synthesize_name_acquired(p, p->destination_bus, p->local_bus, m); if (r == -ECONNRESET || r == -ENOTCONN) return r; if (r < 0) return log_error_errno(r, "Failed to synthesize message: %m"); + /* discard broadcasts that were not matched by any MATCH rule */ + if (!matched && !sd_bus_message_get_destination(m)) { + if (!matched_synthetic) + log_debug("Dropped unmatched broadcast: uid=" UID_FMT " gid=" GID_FMT " pid=" PID_FMT " message=%s path=%s interface=%s member=%s sender=%s destination=%s", + p->local_creds.uid, p->local_creds.gid, p->local_creds.pid, bus_message_type_to_string(m->header->type), + strna(m->path), strna(m->interface), strna(m->member), strna(m->sender), strna(m->destination)); + return 1; + } + patch_sender(p->destination_bus, m); if (p->policy) { @@ -772,7 +822,7 @@ static int proxy_process_local_to_destination(Proxy *p) { if (r > 0) return 1; - r = bus_proxy_process_driver(p->destination_bus, p->local_bus, m, p->policy, &p->local_creds, p->owned_names); + r = bus_proxy_process_driver(p, p->destination_bus, p->local_bus, m, p->policy, &p->local_creds, p->owned_names); if (r == -ECONNRESET || r == -ENOTCONN) return r; if (r < 0) @@ -818,6 +868,13 @@ static int proxy_process_local_to_destination(Proxy *p) { return 1; } +int proxy_match(sd_bus_message *m, void *userdata, sd_bus_error *error) { + Proxy *p = userdata; + + p->message_matched = true; + return 0; /* make sure to continue processing it in further handlers */ +} + int proxy_run(Proxy *p) { int r; |