Bug 1420124 - Verify dbus interface/path strings at DBus remote service, r?jhorak draft
authorMartin Stransky <stransky@redhat.com>
Thu, 23 Nov 2017 12:27:38 +0100
changeset 703156 975d80e4c57c9f6a1c0ab5408d322056a3571328
parent 701872 5378dcb45044a160fad93b02eed0c617f3324dbc
child 703157 b6aa9e052eabfb495d1d459d785e15ba2bfec3b9
push id90727
push userstransky@redhat.com
push dateFri, 24 Nov 2017 12:09:18 +0000
reviewersjhorak
bugs1420124, 1418985
milestone59.0a1
Bug 1420124 - Verify dbus interface/path strings at DBus remote service, r?jhorak Check DBus arguments by "dbus_validate_*" routines before pass them to DBus interface. If the name is invalid try to create a fallback and eventually give up to avoid crashes (Bug 1418985). Also remove DBusError where it's not useful. MozReview-Commit-ID: DWnzFGUYybp
toolkit/components/remote/nsDBusRemoteService.cpp
--- a/toolkit/components/remote/nsDBusRemoteService.cpp
+++ b/toolkit/components/remote/nsDBusRemoteService.cpp
@@ -7,16 +7,17 @@
 
 #include "nsDBusRemoteService.h"
 #include "nsRemoteService.h"
 
 #include "nsIBaseWindow.h"
 #include "nsIDocShell.h"
 #include "nsPIDOMWindow.h"
 #include "mozilla/ModuleUtils.h"
+#include "mozilla/Base64.h"
 #include "nsIServiceManager.h"
 #include "nsIWeakReference.h"
 #include "nsIWidget.h"
 #include "nsIAppShellService.h"
 #include "nsAppShellCID.h"
 #include "nsPrintfCString.h"
 
 #include "nsCOMPtr.h"
@@ -146,57 +147,60 @@ unregister(DBusConnection *conn, void *u
 static DBusObjectPathVTable remoteHandlersTable = {
   .unregister_function  = unregister,
   .message_function = message_handler,
 };
 
 NS_IMETHODIMP
 nsDBusRemoteService::Startup(const char* aAppName, const char* aProfileName)
 {
-  if (!aAppName || !aProfileName)
-    return NS_ERROR_INVALID_ARG;
-
   if (mConnection && dbus_connection_get_is_connected(mConnection)) {
     // We're already connected so we don't need to reconnect
     return NS_ERROR_ALREADY_INITIALIZED;
   }
 
+  if (!aAppName || !aProfileName)
+    return NS_ERROR_INVALID_ARG;
+
+  mConnection = already_AddRefed<DBusConnection>(
+    dbus_bus_get(DBUS_BUS_SESSION, nullptr));
+  if (!mConnection) {
+    return NS_ERROR_FAILURE;
+  }
+  dbus_connection_set_exit_on_disconnect(mConnection, false);
+
   mAppName = aAppName;
   ToLowerCase(mAppName);
 
+  // D-Bus names can contain only [a-z][A-Z][0-9]_
+  // characters so adjust the profile string properly.
+  nsAutoCString profileName;
+  nsresult rv = mozilla::Base64Encode(nsAutoCString(aProfileName), profileName);
+  NS_ENSURE_SUCCESS(rv, rv);
+  profileName.ReplaceChar("+/=", '_');
+
+  nsAutoCString busName;
+  busName = nsPrintfCString("org.mozilla.%s.%s", mAppName.get(),
+                                                 profileName.get());
   DBusError err;
   dbus_error_init(&err);
-  mConnection = already_AddRefed<DBusConnection>(
-    dbus_bus_get(DBUS_BUS_SESSION, &err));
-  if (dbus_error_is_set(&err)) {
-    dbus_error_free(&err);
-    return NS_ERROR_FAILURE;
-  }
-
-  dbus_connection_set_exit_on_disconnect(mConnection, false);
-
-  nsAutoCString interfaceName;
-  interfaceName = nsPrintfCString("org.mozilla.%s.%s", aAppName, aProfileName);
-
-  dbus_error_init(&err);
-  dbus_bus_request_name(mConnection, interfaceName.get(),
+  dbus_bus_request_name(mConnection, busName.get(),
                        DBUS_NAME_FLAG_DO_NOT_QUEUE, &err);
   // The interface is already owned - there is another application/profile
   // instance already running.
   if (dbus_error_is_set(&err)) {
     dbus_error_free(&err);
     mConnection = nullptr;
     return NS_ERROR_FAILURE;
   }
 
-  nsAutoCString objectName;
-  objectName = nsPrintfCString("/org/mozilla/%s/Remote", aAppName);
-
-  if (!dbus_connection_register_object_path(mConnection, objectName.get(),
-                                           &remoteHandlersTable, this)) {
+  nsAutoCString pathName;
+  pathName = nsPrintfCString("/org/mozilla/%s/Remote", mAppName.get());
+  if (!dbus_connection_register_object_path(mConnection, pathName.get(),
+                                            &remoteHandlersTable, this)) {
     mConnection = nullptr;
     return NS_ERROR_FAILURE;
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP