chiark / gitweb /
bus-proxyd: explicitly address messages to unique and well-known name
[elogind.git] / src / bus-proxyd / bus-proxyd.c
index aaa79243cf8d2b435852b6a1e3730427557e6977..d6607edfd6513ed10852a0789ed02684a8a1316f 100644 (file)
@@ -311,48 +311,6 @@ static int synthesize_name_acquired(sd_bus *a, sd_bus *b, sd_bus_message *m) {
         return sd_bus_send(b, n, NULL);
 }
 
-static int process_policy(sd_bus *a, sd_bus *b, sd_bus_message *m) {
-        _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
-        int r;
-
-        assert(a);
-        assert(b);
-        assert(m);
-
-        if (!a->is_kernel)
-                return 0;
-
-        if (!sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "GetAll"))
-                return 0;
-
-        if (!streq_ptr(m->path, "/org/gnome/DisplayManager/Slave"))
-                return 0;
-
-        r = sd_bus_message_new_method_errorf(m, &n, SD_BUS_ERROR_ACCESS_DENIED, "gdm, you are stupid");
-        if (r < 0)
-                return r;
-
-        r = bus_message_append_sender(n, "org.freedesktop.DBus");
-        if (r < 0) {
-                log_error("Failed to append sender to gdm reply: %s", strerror(-r));
-                return r;
-        }
-
-        r = bus_seal_synthetic_message(b, n);
-        if (r < 0) {
-                log_error("Failed to seal gdm reply: %s", strerror(-r));
-                return r;
-        }
-
-        r = sd_bus_send(b, n, NULL);
-        if (r < 0) {
-                log_error("Failed to send gdm reply: %s", strerror(-r));
-                return r;
-        }
-
-        return 1;
-}
-
 static int synthetic_driver_send(sd_bus *b, sd_bus_message *m) {
         int r;
 
@@ -509,7 +467,95 @@ static int peer_is_privileged(sd_bus *bus, sd_bus_message *m) {
         return false;
 }
 
-static int process_driver(sd_bus *a, sd_bus *b, sd_bus_message *m) {
+static int process_policy(sd_bus *a, sd_bus *b, sd_bus_message *m, Policy *policy, const struct ucred *ucred) {
+        int r;
+        char **name;
+        char **names_strv;
+        bool granted = false;
+        Iterator i;
+
+        assert(a);
+        assert(b);
+        assert(m);
+
+        if (!policy)
+                return 0;
+
+        if (b->is_kernel) {
+
+                /* The message came from the kernel, and is sent to our legacy client. */
+                r = sd_bus_creds_get_well_known_names(&m->creds, &names_strv);
+                if (r < 0)
+                        return r;
+
+                STRV_FOREACH(name, names_strv) {
+                        if (policy_check_send(policy, ucred, m->header->type, *name, m->path, m->interface, m->member)) {
+                                r = free_and_strdup(&m->destination_ptr, *name);
+                                if (r < 0)
+                                        break;
+
+                                granted = true;
+                                break;
+                        }
+                }
+
+                if (r < 0)
+                        return r;
+
+                if (!granted)
+                        return -EPERM;
+
+                granted = false;
+
+                HASHMAP_FOREACH(name, names_hash, i) {
+                        if (policy_check_recv(policy, ucred, m->header->type, *name, m->path, m->interface, m->member))
+                                return 0;
+                }
+
+                return -EPERM;
+        } else {
+                sd_bus_creds *bus_creds;
+
+                /* The message came from the legacy client, and is sent to kdbus. */
+                r = sd_bus_get_name_creds(a, m->destination, SD_BUS_CREDS_WELL_KNOWN_NAMES, &bus_creds);
+                if (r < 0)
+                        return r;
+
+                STRV_FOREACH(name, names_strv) {
+                        if (policy_check_send(policy, ucred, m->header->type, *name, m->path, m->interface, m->member)) {
+                                r = free_and_strdup(&m->destination_ptr, *name);
+                                if (r < 0)
+                                        break;
+
+                                r = bus_kernel_parse_unique_name(m->sender, &m->verify_destination_id);
+                                if (r < 0)
+                                        break;
+
+                                granted = true;
+                                break;
+                        }
+                }
+
+                if (r < 0)
+                        return r;
+
+                if (!granted)
+                        return -EPERM;
+
+                granted = false;
+
+                HASHMAP_FOREACH(name, names_hash, i) {
+                        if (policy_check_recv(policy, ucred, m->header->type, *name, m->path, m->interface, m->member))
+                                return 0;
+                }
+
+                return -EPERM;
+        }
+
+        return 0;
+}
+
+static int process_driver(sd_bus *a, sd_bus *b, sd_bus_message *m, Policy *policy, const struct ucred *ucred) {
         int r;
 
         assert(a);
@@ -859,6 +905,9 @@ static int process_driver(sd_bus *a, sd_bus *b, sd_bus_message *m) {
                 if (r < 0)
                         return synthetic_reply_method_errno(m, r, NULL);
 
+                if (policy && !policy_check_own(policy, ucred, name))
+                        return synthetic_reply_method_errno(m, -EPERM, NULL);
+
                 if (!service_name_is_valid(name))
                         return synthetic_reply_method_errno(m, -EINVAL, NULL);
                 if ((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) != 0)
@@ -997,7 +1046,7 @@ static int process_driver(sd_bus *a, sd_bus *b, sd_bus_message *m) {
         }
 }
 
-static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hello) {
+static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, Policy *policy, const struct ucred *ucred, bool *got_hello) {
         _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
         bool is_hello;
         int r;
@@ -1029,6 +1078,11 @@ static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hell
                 return -EIO;
         }
 
+        if (policy && !policy_check_hello(policy, ucred)) {
+                log_error("Policy denied HELLO");
+                return -EPERM;
+        }
+
         *got_hello = true;
 
         if (!a->is_kernel)
@@ -1135,6 +1189,7 @@ static int patch_sender(sd_bus *a, sd_bus_message *m) {
 
 int main(int argc, char *argv[]) {
 
+        _cleanup_bus_creds_unref_ sd_bus_creds *bus_creds = NULL;
         _cleanup_bus_close_unref_ sd_bus *a = NULL, *b = NULL;
         sd_id128_t server_id;
         int r, in_fd, out_fd;
@@ -1253,6 +1308,12 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        r = sd_bus_get_owner_creds(a, SD_BUS_CREDS_UID, &bus_creds);
+        if (r < 0) {
+                log_error("Failed to get bus creds: %s", strerror(-r));
+                goto finish;
+        }
+
         r = sd_bus_new(&b);
         if (r < 0) {
                 log_error("Failed to allocate bus: %s", strerror(-r));
@@ -1412,13 +1473,21 @@ int main(int argc, char *argv[]) {
                 }
 
                 if (m) {
+                        Policy *p = NULL;
+                        uid_t uid;
+
+                        assert_se(sd_bus_creds_get_uid(bus_creds, &uid) == 0);
+
+                        if (uid == 0 || uid != ucred.uid)
+                                p = &policy;
+
                         /* We officially got EOF, let's quit */
                         if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) {
                                 r = 0;
                                 goto finish;
                         }
 
-                        k = process_hello(a, b, m, &got_hello);
+                        k = process_hello(a, b, m, p, &ucred, &got_hello);
                         if (k < 0) {
                                 r = k;
                                 log_error("Failed to process HELLO: %s", strerror(-r));
@@ -1428,14 +1497,7 @@ int main(int argc, char *argv[]) {
                         if (k > 0)
                                 r = k;
                         else {
-                                k = process_policy(a, b, m);
-                                if (k < 0) {
-                                        r = k;
-                                        log_error("Failed to process policy: %s", strerror(-r));
-                                        goto finish;
-                                }
-
-                                k = process_driver(a, b, m);
+                                k = process_driver(a, b, m, p, &ucred);
                                 if (k < 0) {
                                         r = k;
                                         log_error("Failed to process driver calls: %s", strerror(-r));
@@ -1445,17 +1507,33 @@ int main(int argc, char *argv[]) {
                                 if (k > 0)
                                         r = k;
                                 else {
-                                        k = sd_bus_send(a, m, NULL);
-                                        if (k < 0) {
-                                                if (k == -ECONNRESET)
-                                                        r = 0;
-                                                else {
+                                        bool retry;
+
+                                        do {
+                                                retry = false;
+
+                                                k = process_policy(a, b, m, p, &ucred);
+                                                if (k < 0) {
                                                         r = k;
-                                                        log_error("Failed to send message: %s", strerror(-r));
+                                                        log_error("Failed to process policy: %s", strerror(-r));
+                                                        goto finish;
                                                 }
 
-                                                goto finish;
-                                        }
+                                                k = sd_bus_send(a, m, NULL);
+                                                if (k < 0) {
+                                                        if (k == -ECONNRESET)
+                                                                r = 0;
+                                                        else if (k == -EREMCHG)
+                                                                retry = true;
+                                                        else {
+                                                                r = k;
+                                                                log_error("Failed to send message: %s", strerror(-r));
+                                                        }
+
+                                                        if (!retry)
+                                                                goto finish;
+                                                }
+                                        } while (retry);
                                 }
                         }
                 }