79dacd0
From d46fbfb48deff83943355992e55f9ef4431ff5ef Mon Sep 17 00:00:00 2001
79dacd0
From: Daniel Mack <daniel@zonque.org>
79dacd0
Date: Thu, 9 Oct 2014 13:26:53 +0200
79dacd0
Subject: [PATCH] bus-policy: move name list iteration to policy users
79dacd0
79dacd0
We need to figure out which of the possible names satisfied the policy,
79dacd0
so we cannot do the iteration in check_policy_item() but have to leave it
79dacd0
to the users.
79dacd0
79dacd0
Test cases amended accordingly.
79dacd0
---
79dacd0
 src/bus-proxyd/bus-policy.c      | 29 ++++++++++-------------------
79dacd0
 src/bus-proxyd/bus-policy.h      |  4 ++--
79dacd0
 src/bus-proxyd/test-bus-policy.c | 21 +++++++--------------
79dacd0
 3 files changed, 19 insertions(+), 35 deletions(-)
79dacd0
79dacd0
diff --git a/src/bus-proxyd/bus-policy.c b/src/bus-proxyd/bus-policy.c
79dacd0
index aea8541d50..625f5ddaee 100644
79dacd0
--- a/src/bus-proxyd/bus-policy.c
79dacd0
+++ b/src/bus-proxyd/bus-policy.c
79dacd0
@@ -602,14 +602,10 @@ struct policy_check_filter {
79dacd0
         int class;
79dacd0
         const struct ucred *ucred;
79dacd0
         int message_type;
79dacd0
+        const char *name;
79dacd0
         const char *interface;
79dacd0
         const char *path;
79dacd0
-        union {
79dacd0
-                const char *name;
79dacd0
-                const char *member;
79dacd0
-        };
79dacd0
-        char **names_strv;
79dacd0
-        Hashmap *names_hash;
79dacd0
+        const char *member;
79dacd0
 };
79dacd0
 
79dacd0
 static int is_permissive(PolicyItem *i) {
79dacd0
@@ -628,13 +624,8 @@ static int check_policy_item(PolicyItem *i, const struct policy_check_filter *fi
79dacd0
         case POLICY_ITEM_SEND:
79dacd0
         case POLICY_ITEM_RECV:
79dacd0
 
79dacd0
-                if (i->name) {
79dacd0
-                        if (filter->names_hash && !hashmap_contains(filter->names_hash, i->name))
79dacd0
-                                break;
79dacd0
-
79dacd0
-                        if (filter->names_strv && !strv_contains(filter->names_strv, i->name))
79dacd0
-                                break;
79dacd0
-                }
79dacd0
+                if (i->name && !streq_ptr(i->name, filter->name))
79dacd0
+                        break;
79dacd0
 
79dacd0
                 if ((i->message_type != _POLICY_ITEM_CLASS_UNSET) && (i->message_type != filter->message_type))
79dacd0
                         break;
79dacd0
@@ -651,14 +642,14 @@ static int check_policy_item(PolicyItem *i, const struct policy_check_filter *fi
79dacd0
                 return is_permissive(i);
79dacd0
 
79dacd0
         case POLICY_ITEM_OWN:
79dacd0
-                assert(filter->member);
79dacd0
+                assert(filter->name);
79dacd0
 
79dacd0
                 if (streq(i->name, "*") || streq(i->name, filter->name))
79dacd0
                         return is_permissive(i);
79dacd0
                 break;
79dacd0
 
79dacd0
         case POLICY_ITEM_OWN_PREFIX:
79dacd0
-                assert(filter->member);
79dacd0
+                assert(filter->name);
79dacd0
 
79dacd0
                 if (streq(i->name, "*") || startswith(i->name, filter->name))
79dacd0
                         return is_permissive(i);
79dacd0
@@ -780,8 +771,8 @@ bool policy_check_hello(Policy *p, const struct ucred *ucred) {
79dacd0
 
79dacd0
 bool policy_check_recv(Policy *p,
79dacd0
                        const struct ucred *ucred,
79dacd0
-                       Hashmap *names,
79dacd0
                        int message_type,
79dacd0
+                       const char *name,
79dacd0
                        const char *path,
79dacd0
                        const char *interface,
79dacd0
                        const char *member) {
79dacd0
@@ -789,8 +780,8 @@ bool policy_check_recv(Policy *p,
79dacd0
         struct policy_check_filter filter = {
79dacd0
                 .class        = POLICY_ITEM_RECV,
79dacd0
                 .ucred        = ucred,
79dacd0
-                .names_hash   = names,
79dacd0
                 .message_type = message_type,
79dacd0
+                .name         = name,
79dacd0
                 .interface    = interface,
79dacd0
                 .path         = path,
79dacd0
                 .member       = member,
79dacd0
@@ -801,8 +792,8 @@ bool policy_check_recv(Policy *p,
79dacd0
 
79dacd0
 bool policy_check_send(Policy *p,
79dacd0
                        const struct ucred *ucred,
79dacd0
-                       char **names,
79dacd0
                        int message_type,
79dacd0
+                       const char *name,
79dacd0
                        const char *path,
79dacd0
                        const char *interface,
79dacd0
                        const char *member) {
79dacd0
@@ -810,8 +801,8 @@ bool policy_check_send(Policy *p,
79dacd0
         struct policy_check_filter filter = {
79dacd0
                 .class        = POLICY_ITEM_SEND,
79dacd0
                 .ucred        = ucred,
79dacd0
-                .names_strv   = names,
79dacd0
                 .message_type = message_type,
79dacd0
+                .name         = name,
79dacd0
                 .interface    = interface,
79dacd0
                 .path         = path,
79dacd0
                 .member       = member,
79dacd0
diff --git a/src/bus-proxyd/bus-policy.h b/src/bus-proxyd/bus-policy.h
79dacd0
index 5b4d9d0c10..64fe1ffac5 100644
79dacd0
--- a/src/bus-proxyd/bus-policy.h
79dacd0
+++ b/src/bus-proxyd/bus-policy.h
79dacd0
@@ -80,15 +80,15 @@ bool policy_check_own(Policy *p, const struct ucred *ucred, const char *name);
79dacd0
 bool policy_check_hello(Policy *p, const struct ucred *ucred);
79dacd0
 bool policy_check_recv(Policy *p,
79dacd0
                        const struct ucred *ucred,
79dacd0
-                       Hashmap *names,
79dacd0
                        int message_type,
79dacd0
+                       const char *name,
79dacd0
                        const char *path,
79dacd0
                        const char *interface,
79dacd0
                        const char *member);
79dacd0
 bool policy_check_send(Policy *p,
79dacd0
                        const struct ucred *ucred,
79dacd0
-                       char **names,
79dacd0
                        int message_type,
79dacd0
+                       const char *name,
79dacd0
                        const char *path,
79dacd0
                        const char *interface,
79dacd0
                        const char *member);
79dacd0
diff --git a/src/bus-proxyd/test-bus-policy.c b/src/bus-proxyd/test-bus-policy.c
79dacd0
index 5267788b70..c9a027e877 100644
79dacd0
--- a/src/bus-proxyd/test-bus-policy.c
79dacd0
+++ b/src/bus-proxyd/test-bus-policy.c
79dacd0
@@ -63,8 +63,6 @@ int main(int argc, char *argv[]) {
79dacd0
 
79dacd0
         Policy p = {};
79dacd0
         struct ucred ucred = {};
79dacd0
-        char **names_strv;
79dacd0
-        Hashmap *names_hash;
79dacd0
 
79dacd0
         /* Ownership tests */
79dacd0
         assert_se(test_policy_load(&p, "ownerships.conf") == 0);
79dacd0
@@ -93,32 +91,27 @@ int main(int argc, char *argv[]) {
79dacd0
 
79dacd0
         /* Signaltest */
79dacd0
         assert_se(test_policy_load(&p, "signals.conf") == 0);
79dacd0
-        names_strv = STRV_MAKE("bli.bla.blubb");
79dacd0
 
79dacd0
         ucred.uid = 0;
79dacd0
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == true);
79dacd0
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == true);
79dacd0
 
79dacd0
         ucred.uid = 1;
79dacd0
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_SIGNAL, NULL, "/an/object/path", NULL) == false);
79dacd0
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == false);
79dacd0
 
79dacd0
         policy_free(&p);
79dacd0
 
79dacd0
         /* Method calls */
79dacd0
         assert_se(test_policy_load(&p, "methods.conf") == 0);
79dacd0
-        names_strv = STRV_MAKE("org.test.test1");
79dacd0
         policy_dump(&p);
79dacd0
 
79dacd0
         ucred.uid = 0;
79dacd0
 
79dacd0
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
79dacd0
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "bli.bla.blubb", "Member") == false);
79dacd0
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int1", "Member") == true);
79dacd0
-        assert_se(policy_check_send(&p, &ucred, names_strv, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int2", "Member") == true);
79dacd0
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
79dacd0
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
79dacd0
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == true);
79dacd0
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == true);
79dacd0
 
79dacd0
-        names_hash = hashmap_new(&string_hash_ops);
79dacd0
-        assert(names_hash != NULL);
79dacd0
-        assert_se(hashmap_put(names_hash, "org.test.test3", NULL) >= 0);
79dacd0
-        assert_se(policy_check_recv(&p, &ucred, names_hash, SD_BUS_MESSAGE_METHOD_CALL, "/an/object/path", "org.test.int3", "Member111") == true);
79dacd0
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test3", "/an/object/path", "org.test.int3", "Member111") == true);
79dacd0
 
79dacd0
         policy_free(&p);
79dacd0