kdbus-test: augment seclabel test with UID/GID checks and rename to metadata-update
authorKonrad Lipinski <konrad.l@samsung.com>
Thu, 8 Dec 2016 16:32:42 +0000 (17:32 +0100)
committerKonrad Lipinski <konrad.l@samsung.com>
Mon, 12 Dec 2016 06:55:56 +0000 (07:55 +0100)
include/uapi/linux/kdbus.h
tools/testing/selftests/kdbus/kdbus-test.c
tools/testing/selftests/kdbus/kdbus-test.h
tools/testing/selftests/kdbus/kdbus-util.c
tools/testing/selftests/kdbus/test-match.c

index c160849ab99fbf47174832ca94feb7f4b9a40906..778bccb2400ea15f50b9bc6ba8d0291dc67ce79d 100644 (file)
@@ -964,7 +964,6 @@ enum kdbus_ioctl_type {
                                             struct kdbus_cmd_info),
        KDBUS_CMD_LIST =                _IOR(KDBUS_IOCTL_MAGIC, 0x86,
                                             struct kdbus_cmd_list),
-       KDBUS_CMD_UPDATE_METADATA = _IO(KDBUS_IOCTL_MAGIC, 0x89),
 
        KDBUS_CMD_SEND =                _IOW(KDBUS_IOCTL_MAGIC, 0x90,
                                             struct kdbus_cmd_send),
@@ -980,6 +979,9 @@ enum kdbus_ioctl_type {
                                             struct kdbus_cmd_match),
        KDBUS_CMD_MATCH_REMOVE =        _IOW(KDBUS_IOCTL_MAGIC, 0xb1,
                                             struct kdbus_cmd_match),
+
+       /* Tizen */
+       KDBUS_CMD_UPDATE_METADATA = _IO(KDBUS_IOCTL_MAGIC, 0xd0),
 };
 
 #endif /* _UAPI_KDBUS_H_ */
index cf12eb5b25c6a6e6724d44650dff0a73acd43b7b..5774f5caf57cb972e79f7f1bdf2672707bbc12d9 100644 (file)
@@ -201,9 +201,9 @@ static const struct kdbus_test tests[] = {
                .timeout = 10,
        },
        {
-               .name   = "seclabel",
-               .desc   = "dynamic seclabel change for messages and CONN_INFO",
-               .func   = kdbus_test_dynamic_seclabel,
+               .name   = "metadata-update",
+               .desc   = "post-hello metadata update",
+               .func   = kdbus_test_update_metadata,
                .flags  = TEST_CREATE_BUS | TEST_CREATE_CONN,
                .timeout = 10,
        },
@@ -519,6 +519,7 @@ static wur int start_all_tests(struct kdbus_test_args const *kdbus_args)
 
                if (!kdbus_args->tap_output) {
                        unsigned int n;
+                       assert(strlen(t->desc) < 60);
 
                        print("Testing %s (%s) ", t->desc, t->name);
                        for (n = 0; n < 60 - strlen(t->desc) - strlen(t->name); n++)
index 10cb7d68970cc1261042407f645b7b683418d97f..a0f19deea07f2cff34f7faecf6b8f67fb3a24411 100644 (file)
@@ -107,7 +107,7 @@ wur int kdbus_test_policy_priv(struct kdbus_test_env *env);
 wur int kdbus_test_sync_byebye(struct kdbus_test_env *env);
 wur int kdbus_test_sync_reply(struct kdbus_test_env *env);
 wur int kdbus_test_big_metadata(struct kdbus_test_env *env);
-wur int kdbus_test_dynamic_seclabel(struct kdbus_test_env *env);
+wur int kdbus_test_update_metadata(struct kdbus_test_env *env);
 wur int kdbus_test_timeout(struct kdbus_test_env *env);
 wur int kdbus_test_writable_pool(struct kdbus_test_env *env);
 
index dd79147849d74934c7f049c0b1b4094157b8ecff..523c1b51b4ec4708b10545641b262b7aad38aef9 100644 (file)
@@ -833,7 +833,7 @@ static wur bool kdbus_dump_meta_item(struct kdbus_item const *item) {
                        kdbus_printf("\n");
                        break;
                }
-               defaul: return 0;
+               default: return 0;
        }
        return 1;
 }
index b0caf0fe968a64bbf47a307cba9acc20f1c63d60..8176cd9230affbb00e7dbae4abb1771e1c617a77 100644 (file)
@@ -579,12 +579,35 @@ wur int kdbus_test_match_itemless(struct kdbus_test_env *env)
        return TEST_OK;
 }
 
-static wur int assert_label(char const *label, struct kdbus_item const *item)
+static wur int assert_label(struct kdbus_item const *item, char const *label)
 {
-       while (KDBUS_ITEM_SECLABEL != item->type)
+       for (;;) {
+               if (KDBUS_ITEM_SECLABEL == item->type) {
+                       ASSERT_ZERO(strcmp(item->str, label));
+                       return TEST_OK;
+               }
                item = KDBUS_ITEM_NEXT(item);
-       ASSERT_ZERO(strcmp(item->str, label));
-       return TEST_OK;
+       }
+}
+
+static wur int assert_label_uid_gid(struct kdbus_item const *item, char const *label, uid_t uid, gid_t gid)
+{
+       unsigned mask = 3;
+       for (;;) {
+               if (KDBUS_ITEM_SECLABEL == item->type) {
+                       ASSERT_ZERO(strcmp(item->str, label));
+                       if (!(mask &= ~2U))
+                               return TEST_OK;
+               } else if (KDBUS_ITEM_CREDS == item->type) {
+                       ASSERT_RETURN(uid,==,item->creds.uid);
+                       ASSERT_RETURN(uid,==,item->creds.euid);
+                       ASSERT_RETURN(gid,==,item->creds.gid);
+                       ASSERT_RETURN(gid,==,item->creds.egid);
+                       if (!(mask &= ~1U))
+                               return TEST_OK;
+               }
+               item = KDBUS_ITEM_NEXT(item);
+       }
 }
 
 static wur int get_label(char *buf, unsigned buf_size)
@@ -596,7 +619,7 @@ static wur int get_label(char *buf, unsigned buf_size)
        do len = read(fd, buf, buf_size); while (len < 0 && (EAGAIN==errno || EINTR==errno));
        if (len < 0)
                return -errno;
-       if (len == buf_size)
+       if ((unsigned)len == buf_size)
                return -ENAMETOOLONG;
        buf[len] = 0;
        if (0 > close(fd))
@@ -621,12 +644,23 @@ static wur int assert_info_label(struct kdbus_conn *conn, uint64_t id, char cons
        ASSERT_ZERO(kdbus_conn_info(conn, id, NULL, KDBUS_ATTACH_SECLABEL, &offset));
        struct kdbus_info *info = (struct kdbus_info *)(conn->buf + offset);
        ASSERT_RETURN(info->id,==,id);
-       ASSERT_ZERO(assert_label(label, info->items));
+       ASSERT_ZERO(assert_label(info->items, label));
+       ASSERT_ZERO(kdbus_free(conn, offset));
+       return 0;
+}
+
+static wur int assert_info_label_uid_gid(struct kdbus_conn *conn, uint64_t id, char const *label, uid_t uid, gid_t gid)
+{
+       uint64_t offset = 0;
+       ASSERT_ZERO(kdbus_conn_info(conn, id, NULL, KDBUS_ATTACH_SECLABEL|KDBUS_ATTACH_CREDS, &offset));
+       struct kdbus_info *info = (struct kdbus_info *)(conn->buf + offset);
+       ASSERT_RETURN(info->id,==,id);
+       ASSERT_ZERO(assert_label_uid_gid(info->items, label, uid, gid));
        ASSERT_ZERO(kdbus_free(conn, offset));
        return 0;
 }
 
-wur int kdbus_test_dynamic_seclabel(struct kdbus_test_env *env)
+wur int kdbus_test_update_metadata(struct kdbus_test_env *env)
 {
        char old_label[65536];
        char const *labels[] = {"System::Privileged", "UserTest"};
@@ -642,7 +676,7 @@ wur int kdbus_test_dynamic_seclabel(struct kdbus_test_env *env)
                        struct kdbus_msg *msg;
                        ASSERT_ZERO(kdbus_msg_send(env->conn, NULL, 1, 0, 0, 0, env->conn->id));
                        ASSERT_ZERO(kdbus_msg_recv(env->conn, &msg, NULL));
-                       ASSERT_ZERO(assert_label(label, msg->items));
+                       ASSERT_ZERO(assert_label(msg->items, label));
                        kdbus_msg_free(msg);
                }
                ASSERT_ZERO(assert_info_label(env->conn, env->conn->id, prev_label));
@@ -655,6 +689,8 @@ wur int kdbus_test_dynamic_seclabel(struct kdbus_test_env *env)
                struct kdbus_conn *child;
                ASSERT_EXIT_ZERO(set_label("wuj"));
                ASSERT_EXIT_NONZERO(child = kdbus_hello(env->buspath, 0, NULL, 0));
+               ASSERT_EXIT_ZERO(drop_privileges(UNPRIV_UID, UNPRIV_GID));
+               ASSERT_EXIT_ZERO(kdbus_cmd_update_metadata(child->fd));
                for (i=TABSIZE(labels); --i;) {
                        char const *label = labels[i];
                        ASSERT_EXIT_ZERO(kdbus_msg_send(child, NULL, 1+i, 0, 0, 0, env->conn->id));
@@ -662,7 +698,7 @@ wur int kdbus_test_dynamic_seclabel(struct kdbus_test_env *env)
                                struct kdbus_msg *msg;
                                ASSERT_EXIT_ZERO(kdbus_msg_recv_poll(child, 500, &msg, NULL));
                                ASSERT_EXIT(i,==,msg->cookie_reply-1);
-                               ASSERT_EXIT_ZERO(assert_label(label, msg->items));
+                               ASSERT_EXIT_ZERO(assert_label(msg->items, label));
                                kdbus_msg_free(msg);
                        }
                        ASSERT_EXIT_ZERO(assert_info_label(child, env->conn->id, label));
@@ -674,11 +710,11 @@ wur int kdbus_test_dynamic_seclabel(struct kdbus_test_env *env)
                        struct kdbus_msg *msg;
                        ASSERT_ZERO(kdbus_msg_recv_poll(env->conn, 500, &msg, NULL));
                        ASSERT_RETURN(i,==,msg->cookie-1);
-                       ASSERT_ZERO(assert_label("wuj", msg->items));
+                       ASSERT_ZERO(assert_label_uid_gid(msg->items, "wuj", UNPRIV_UID, UNPRIV_GID));
                        ASSERT_ZERO(set_label(labels[i]));
                        ASSERT_ZERO(kdbus_cmd_update_metadata(env->conn->fd));
                        ASSERT_ZERO(kdbus_msg_send_reply(env->conn, i+1, msg->src_id));
-                       ASSERT_ZERO(assert_info_label(env->conn, msg->src_id, "wuj"));
+                       ASSERT_ZERO(assert_info_label_uid_gid(env->conn, msg->src_id, "wuj", UNPRIV_UID, UNPRIV_GID));
                        kdbus_msg_free(msg);
                }
        }));