[daemon-fix] fixed matching for kernel broadcasts
[platform/upstream/dbus.git] / bus / kdbus-d.c
index 5e783fd..db97c25 100644 (file)
@@ -127,7 +127,7 @@ dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rul
   /*parsing rule and calculating size of command*/
   size = sizeof(struct kdbus_cmd_match);
   if(parse_match_key(rule, "interface='", &pInterface))       /*actual size is not important for interface because bloom size is defined by bus*/
-    size += KDBUS_PART_HEADER_SIZE + bloom_size;
+    size += KDBUS_ITEM_HEADER_SIZE + bloom_size;
   name_size = parse_match_key(rule, "sender='", &pName);
   if(name_size)
   {
@@ -138,7 +138,7 @@ dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rul
       pName = NULL;
     }
     else
-      size += KDBUS_PART_SIZE(name_size + 1);  //well known name
+      size += KDBUS_ITEM_SIZE(name_size + 1);  //well known name
   }
 
   pCmd_match = alloca(size);
@@ -154,14 +154,14 @@ dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rul
   if(pName)
   {
     pItem->type = KDBUS_MATCH_SRC_NAME;
-    pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
+    pItem->size = KDBUS_ITEM_HEADER_SIZE + name_size + 1;
     memcpy(pItem->str, pName, strlen(pName) + 1);
     pItem = KDBUS_PART_NEXT(pItem);
   }
   if(pInterface)
   {
     pItem->type = KDBUS_MATCH_BLOOM;
-    pItem->size = KDBUS_PART_HEADER_SIZE + bloom_size;
+    pItem->size = KDBUS_ITEM_HEADER_SIZE + bloom_size;
     strncpy(pItem->data, pInterface, bloom_size);
   }
 
@@ -228,8 +228,7 @@ int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo*
   uint64_t size;
   __u64 id = 0;
 
-  pInfo->sec_label_len = 0;
-  pInfo->sec_label = NULL;
+  memset(pInfo, 0, sizeof(struct nameInfo));
 
   if(!_dbus_transport_get_socket_fd(transport, &fd))
     return -EPERM;
@@ -247,6 +246,7 @@ int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo*
     return -errno;
   }
 
+  memset(cmd, 0, sizeof(struct kdbus_cmd_conn_info));
   cmd->size = size;
   cmd->id = id;
   if(id == 0)
@@ -275,12 +275,12 @@ int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo*
       if(item->type == KDBUS_ITEM_CREDS)
         {
           pInfo->userId = item->creds.uid;
-          pInfo->processId = item->creds.uid;
+          pInfo->processId = item->creds.pid;
         }
 
       if(item->type == KDBUS_ITEM_SECLABEL)
         {
-          pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
+          pInfo->sec_label_len = item->size - KDBUS_ITEM_HEADER_SIZE - 1;
           if(pInfo->sec_label_len != 0)
             {
               pInfo->sec_label = malloc(pInfo->sec_label_len);
@@ -312,9 +312,12 @@ int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo*
  */
 char* make_kdbus_bus(DBusBusType type, const char* address, DBusError *error)
 {
-  struct kdbus_cmd_bus_make *bus_make;
+  // TODO Function alloca() used. In upstream there was a patch proposing to
+  // replace alloca() with malloc() to assure memory alignment. If there will be
+  // suggestion to use malloc instead of alloca this function has to be modified
+  struct kdbus_cmd_make *bus_make;
   struct kdbus_item *item;
-  __u64 name_size, item_size, bus_make_size;
+  __u64 name_size, bus_make_size;
   int fdc, ret;
   char *addr_value = NULL;
   char *bus = NULL;
@@ -327,34 +330,41 @@ char* make_kdbus_bus(DBusBusType type, const char* address, DBusError *error)
   else
     name_size = snprintf(name, 0, "%u-kdbus-%u", getuid(), getpid()) + 1;
 
-  item_size = KDBUS_PART_HEADER_SIZE + name_size;
-  bus_make_size = sizeof(struct kdbus_cmd_bus_make) + item_size;
+  name = alloca(name_size);
+  if (!name)
+    {
+      return NULL;
+    }
 
+  bus_make_size = sizeof(struct kdbus_cmd_make) + KDBUS_ITEM_SIZE(name_size) + KDBUS_ITEM_SIZE(sizeof(__u64));
   bus_make = alloca(bus_make_size);
   if (!bus_make)
     {
       return NULL;
     }
 
+  bus_make->size = bus_make_size;
+#ifdef POLICY_TO_KDBUS
+  bus_make->flags = KDBUS_MAKE_ACCESS_WORLD;
+#else
+  bus_make->flags = KDBUS_MAKE_ACCESS_WORLD | KDBUS_MAKE_POLICY_OPEN;
+#endif
   item = bus_make->items;
-  item->size = item_size;
-  item->type = KDBUS_ITEM_MAKE_NAME;
 
+  item->type = KDBUS_ITEM_MAKE_NAME;
+  item->size = KDBUS_ITEM_HEADER_SIZE + name_size;
   if(type == DBUS_BUS_SYSTEM)
-    sprintf(item->str, "%u-kdbus-%s", getuid(), "system");
+    sprintf(name, "%u-kdbus-%s", getuid(), "system");
   else if(type == DBUS_BUS_SESSION)
-    sprintf(item->str, "%u-kdbus", getuid());
+    sprintf(name, "%u-kdbus", getuid());
   else
-    sprintf(item->str, "%u-kdbus-%u", getuid(), getpid());
+    sprintf(name, "%u-kdbus-%u", getuid(), getpid());
+  memcpy((bus_make->items)->str, name, name_size);
 
-  bus_make->bloom_size = 64;
-  bus_make->size = bus_make_size;
-
-#ifdef POLICY_TO_KDBUS
-  bus_make->flags = KDBUS_MAKE_ACCESS_WORLD;
-#else
-  bus_make->flags = KDBUS_MAKE_POLICY_OPEN;
-#endif
+  item = KDBUS_PART_NEXT(item);
+  item->type = KDBUS_ITEM_BLOOM_SIZE;
+  item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
+  item->data64[0] = 64;
 
   addr_value = strchr(address, ':') + 1;
   if(*addr_value)
@@ -412,7 +422,6 @@ static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
   uint64_t size;
   int fd;
   DBusTransport *transport;
-  const char* unique_name;
 
   transport = dbus_connection_get_transport(connection);
 
@@ -423,7 +432,7 @@ static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
     }
 
   size = sizeof(struct kdbus_cmd_match);
-  size += KDBUS_PART_SIZE(1)*3 + KDBUS_PART_SIZE(sizeof(__u64))*2;  /*3 name related items plus 2 id related items*/
+  size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;  /*3 name related items plus 2 id related items*/
 
   pCmd_match = alloca(size);
   if(pCmd_match == NULL)
@@ -432,28 +441,28 @@ static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
       return FALSE;
     }
 
-  unique_name = dbus_bus_get_unique_name(connection);
-
-  pCmd_match->id = strtoull(&unique_name[3], NULL, 10);
+  pCmd_match->id = 0;
   pCmd_match->cookie = 1;
   pCmd_match->size = size;
+  pCmd_match->src_id = 0;
 
   pItem = pCmd_match->items;
-  pCmd_match->src_id = 0;
   pItem->type = KDBUS_MATCH_NAME_CHANGE;
-  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_NAME_ADD;
-  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_NAME_REMOVE;
-  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_ID_ADD;
-  pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
+  pItem->id = KDBUS_MATCH_SRC_ID_ANY;
   pItem = KDBUS_PART_NEXT(pItem);
   pItem->type = KDBUS_MATCH_ID_REMOVE;
-  pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
+  pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
+  pItem->id = KDBUS_MATCH_SRC_ID_ANY;
 
   if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
     {
@@ -533,8 +542,12 @@ dbus_bool_t register_daemon_name(DBusConnection* connection)
     retval = TRUE;
 
 out:
-       bus_transaction_cancel_and_free(transaction);
-    return retval;
+  if(retval)
+    bus_transaction_execute_and_free(transaction);
+  else
+    bus_transaction_cancel_and_free(transaction);
+
+  return retval;
 }
 
 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
@@ -601,7 +614,7 @@ again:
 
   for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
   {
-      if(*name->name)
+      if(name->size > sizeof(struct kdbus_cmd_name) )
       {
         list[i] = strdup(name->name);
         if(list[i] == NULL)
@@ -1031,8 +1044,12 @@ out:
     }
     dbus_free_string_array (services);
     _dbus_string_free(&name);
+  if(retval)
+    bus_transaction_execute_and_free(transaction);
+  else
     bus_transaction_cancel_and_free(transaction);
-    return retval;
+
+  return retval;
 }
 
 /*
@@ -1088,8 +1105,12 @@ dbus_bool_t update_kdbus_starters(DBusConnection* connection)
     retval = TRUE;
 
 out:
-       bus_transaction_cancel_and_free(transaction);
-    return retval;
+  if(retval)
+    bus_transaction_execute_and_free(transaction);
+  else
+    bus_transaction_cancel_and_free(transaction);
+
+  return retval;
 }
 
 /*