kdbus: patch for error=NULL argument 37/175937/2 accepted/tizen/unified/20180416.213838 submit/tizen/20180416.012717
authorAleksy Barcz <a.barcz@partner.samsung.com>
Fri, 13 Apr 2018 07:59:45 +0000 (09:59 +0200)
committerAleksy Barcz <a.barcz@partner.samsung.com>
Fri, 13 Apr 2018 14:01:45 +0000 (16:01 +0200)
Fixes segfault in *error dereference introduced in:
"kdbus: make access error behaviour consistent with dbus-daemon".

Change-Id: I4790cac9c051927d6c9f03c665ef5f2f7650294c

gio/gkdbus.c

index b3885d2..9ec411a 100755 (executable)
@@ -3558,17 +3558,69 @@ deliver_synthetic_reply (gpointer user_data)
   return FALSE;
 }
 
+#ifdef LIBDBUSPOLICY
 static GDBusMessage*
 create_access_error_reply (GDBusMessage *message,
-                           GError *error)
+                           const GQuark  domain,
+                           const gint    code,
+                           const gchar  *format,
+                           ...)
 {
-  GDBusMessage *error_reply;
+  gchar *error_text;
+  GError *access_error;
   gchar *dbus_error_name;
-  dbus_error_name = g_dbus_error_encode_gerror (error);
-  error_reply = g_dbus_message_new_method_error (message, dbus_error_name, "%s", error->message);
+  GDBusMessage *error_reply;
+  va_list args;
+
+  va_start (args, format);
+  error_text = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  access_error = NULL;
+  g_set_error (&access_error, domain, code, "%s", error_text);
+  dbus_error_name = g_dbus_error_encode_gerror (access_error);
+  g_error_free (access_error);
+
+  error_reply = g_dbus_message_new_method_error (message, dbus_error_name, "%s", error_text);
   g_free (dbus_error_name);
+  g_free (error_text);
   return error_reply;
 }
+#endif
+
+#ifdef LIBDBUSPOLICY
+static GDBusMessage*
+check_result_to_error_reply (GDBusMessage *message,
+                             const gint    check_result)
+{
+  switch (check_result)
+    {
+      case DBUSPOLICY_RESULT_DENY:
+        return create_access_error_reply (message, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
+                     "Cannot send message - message rejected due to XML security policies");
+
+      case DBUSPOLICY_RESULT_DEST_NOT_AVAILABLE:
+        if (g_dbus_message_get_flags (message) & G_DBUS_MESSAGE_FLAGS_NO_AUTO_START)
+          return create_access_error_reply (message, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER,
+                       "Name \"%s\" does not exist", g_dbus_message_get_destination (message));
+        else
+          return create_access_error_reply (message, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN,
+                       "Cannot send message - destination '%s' not known", g_dbus_message_get_destination (message));
+
+      case DBUSPOLICY_RESULT_KDBUS_ERROR:
+        return create_access_error_reply (message, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
+                     "Cannot send message - message rejected due to internal libdbuspolicy error (kdbus)");
+
+      case DBUSPOLICY_RESULT_CYNARA_ERROR:
+        return create_access_error_reply (message, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
+                     "Cannot send message - message rejected due to internal libdbuspolicy error (Cynara)");
+
+      default:
+        return create_access_error_reply (message, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
+                     "Cannot send message - unknown libdbuspolicy error");
+    }
+}
+#endif
 
 static void
 send_error_message (GKDBusWorker *worker,
@@ -3606,8 +3658,6 @@ _g_kdbus_send (GKDBusWorker  *worker,
   gsize header_size;
   gsize msg_size;
 
-  GDBusMessage *access_error_message;
-
   g_return_val_if_fail (G_IS_KDBUS_WORKER (worker), FALSE);
 
   send = NULL;
@@ -3754,41 +3804,8 @@ _g_kdbus_send (GKDBusWorker  *worker,
                                          NULL, 0, 0);
           if (check != DBUSPOLICY_RESULT_ALLOW)
             {
-              switch (check)
-                {
-                  case DBUSPOLICY_RESULT_DENY:
-                    g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
-                                 "Cannot send message - message rejected due to XML security policies");
-                  break;
-
-                  case DBUSPOLICY_RESULT_DEST_NOT_AVAILABLE:
-                    if (g_dbus_message_get_flags (message) & G_DBUS_MESSAGE_FLAGS_NO_AUTO_START)
-                      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER,
-                                   "Name \"%s\" does not exist", g_dbus_message_get_destination (message));
-                    else
-                      g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN,
-                                   "Cannot send message - destination '%s' not known", g_dbus_message_get_destination (message));
-                  break;
-
-                  case DBUSPOLICY_RESULT_KDBUS_ERROR:
-                    g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
-                                 "Cannot send message - message rejected due to internal libdbuspolicy error (kdbus)");
-                  break;
-
-                  case DBUSPOLICY_RESULT_CYNARA_ERROR:
-                    g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
-                                 "Cannot send message - message rejected due to internal libdbuspolicy error (Cynara)");
-                  break;
-
-                  default:
-                    g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
-                                 "Cannot send message - unknown libdbuspolicy error");
-                  break;
-                }
-
-
-              access_error_message = create_access_error_reply(message, *error);
-              g_clear_error(error); /* we send back an error reply, so error must be NULL */
+              GDBusMessage *access_error_message;
+              access_error_message = check_result_to_error_reply (message, check);
               result = TRUE;
               if (out_reply != NULL)
                 {
@@ -3796,7 +3813,7 @@ _g_kdbus_send (GKDBusWorker  *worker,
                 }
               else
                 {
-                  send_error_message(worker, access_error_message);
+                  send_error_message (worker, access_error_message);
                 }
 
               goto out;