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
--- 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