phdc: Check for the sender identity before registering the agent
authorSamuel Ortiz <sameo@linux.intel.com>
Sun, 24 Nov 2013 23:39:30 +0000 (00:39 +0100)
committerSamuel Ortiz <sameo@linux.intel.com>
Sun, 24 Nov 2013 23:39:30 +0000 (00:39 +0100)
In order to avoid DoS type of errors by only allowing the registrer to
unregister.

Code review done by Sebastian Krahmer <krahmer@suse.de>.

plugins/phdc.c

index 575ee83..e77914e 100644 (file)
@@ -88,6 +88,12 @@ static DBusMessage *error_not_found(DBusMessage *msg)
                                                ".NotFound", "Not found");
 }
 
+static DBusMessage *error_permission_denied(DBusMessage *msg)
+{
+       return g_dbus_create_error(msg, NFC_ERROR_INTERFACE
+                               ".PermissionDenied", "PermissionDenied");
+}
+
 static DBusMessage *error_failed(DBusMessage *msg, int errnum)
 {
        const char *str = strerror(errnum);
@@ -397,6 +403,8 @@ static DBusMessage *dbus_register_phdc_agent(DBusConnection *conn,
        /* Get the the sender name */
        phdc_mgr->sender = g_strdup(dbus_message_get_sender(msg));
 
+       DBG("%s", phdc_mgr->sender);
+
        /* default p2p values */
        phdc_mgr->p2p_driver->fallback_service_name = NULL;
        phdc_mgr->p2p_driver->sock_type = SOCK_STREAM;
@@ -476,8 +484,7 @@ static DBusMessage *dbus_unregister_phdc_agent(DBusConnection *conn,
 {
        struct near_phdc_data *mgr;
        DBusMessageIter iter;
-       const char *path;
-       const char *role;
+       const char *path, *role, *sender;
 
        DBG("conn %p", conn);
 
@@ -487,6 +494,8 @@ static DBusMessage *dbus_unregister_phdc_agent(DBusConnection *conn,
        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
                return error_invalid_arguments(msg);
 
+       sender = dbus_message_get_sender(msg);
+
        dbus_message_iter_get_basic(&iter, &path);
        dbus_message_iter_next(&iter);
 
@@ -500,6 +509,11 @@ static DBusMessage *dbus_unregister_phdc_agent(DBusConnection *conn,
        if (!mgr)
                return error_not_found(msg);
 
+       DBG("%s", mgr->sender);
+
+       if (strncmp(sender, mgr->sender, strlen(mgr->sender)))
+               return error_permission_denied(msg);
+
        /* remove it */
        near_p2p_unregister(mgr->p2p_driver);