HID accepts bonded device connections only.
authorAlain Michaud <alainm@chromium.org>
Tue, 10 Mar 2020 02:35:18 +0000 (02:35 +0000)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 12 Apr 2021 09:00:48 +0000 (14:30 +0530)
This change adds a configuration for platforms to choose a more secure
posture for the HID profile.  While some older mice are known to not
support pairing or encryption, some platform may choose a more secure
posture by requiring the device to be bonded  and require the
connection to be encrypted when bonding is required.

Reference:
https://www.intel.com/content/www/us/en/security-center/advisory/intel-sa-00352.html
Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
profiles/input/device.c
profiles/input/device.h
profiles/input/input.conf
profiles/input/manager.c

index 8d24b1e..b47127c 100644 (file)
@@ -94,6 +94,7 @@ struct input_device {
 
 static int idle_timeout = 0;
 static bool uhid_enabled = false;
+static bool classic_bonded_only = false;
 
 void input_set_idle_timeout(int timeout)
 {
@@ -105,6 +106,11 @@ void input_enable_userspace_hid(bool state)
        uhid_enabled = state;
 }
 
+void input_set_classic_bonded_only(bool state)
+{
+       classic_bonded_only = state;
+}
+
 static void input_device_enter_reconnect_mode(struct input_device *idev);
 static int connection_disconnect(struct input_device *idev, uint32_t flags);
 
@@ -976,8 +982,18 @@ static int hidp_add_connection(struct input_device *idev)
        if (device_name_known(idev->device))
                device_get_name(idev->device, req->name, sizeof(req->name));
 
+       /* Make sure the device is bonded if required */
+       if (classic_bonded_only && !device_is_bonded(idev->device,
+                               btd_device_get_bdaddr_type(idev->device))) {
+               error("Rejected connection from !bonded device %s", dst_addr);
+               goto cleanup;
+       }
+
        /* Encryption is mandatory for keyboards */
-       if (req->subclass & 0x40) {
+       /* Some platforms may choose to require encryption for all devices */
+       /* Note that this only matters for pre 2.1 devices as otherwise the */
+       /* device is encrypted by default by the lower layers */
+       if (classic_bonded_only || req->subclass & 0x40) {
                if (!bt_io_set(idev->intr_io, &gerr,
                                        BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
                                        BT_IO_OPT_INVALID)) {
@@ -1239,6 +1255,11 @@ static void input_device_enter_reconnect_mode(struct input_device *idev)
        DBG("path=%s reconnect_mode=%s", idev->path,
                                reconnect_mode_to_string(idev->reconnect_mode));
 
+       /* Make sure the device is bonded if required */
+       if (classic_bonded_only && !device_is_bonded(idev->device,
+                               btd_device_get_bdaddr_type(idev->device)))
+               return;
+
        /* Only attempt an auto-reconnect when the device is required to
         * accept reconnections from the host.
         */
index 20aba31..e8cad27 100755 (executable)
@@ -29,6 +29,7 @@ struct input_conn;
 
 void input_set_idle_timeout(int timeout);
 void input_enable_userspace_hid(bool state);
+void input_set_classic_bonded_only(bool state);
 
 int input_device_register(struct btd_service *service);
 void input_device_unregister(struct btd_service *service);
index 3e1d65a..166aff4 100755 (executable)
 # Enable HID protocol handling in userspace input profile
 # Defaults to false (HIDP handled in HIDP kernel module)
 #UserspaceHID=true
+
+# Limit HID connections to bonded devices
+# The HID Profile does not specify that devices must be bonded, however some
+# platforms may want to make sure that input connections only come from bonded
+# device connections. Several older mice have been known for not supporting
+# pairing/encryption.
+# Defaults to false to maximize device compatibility.
+#ClassicBondedOnly=true
index 2aaeb15..696dbc9 100755 (executable)
@@ -127,7 +127,7 @@ static int input_init(void)
        config = load_config_file(CONFIGDIR "/input.conf");
        if (config) {
                int idle_timeout;
-               gboolean uhid_enabled;
+               gboolean uhid_enabled, classic_bonded_only;
 
                idle_timeout = g_key_file_get_integer(config, "General",
                                                        "IdleTimeout", &err);
@@ -145,6 +145,17 @@ static int input_init(void)
                        input_enable_userspace_hid(uhid_enabled);
                } else
                        g_clear_error(&err);
+
+               classic_bonded_only = g_key_file_get_boolean(config, "General",
+                                               "ClassicBondedOnly", &err);
+
+               if (!err) {
+                       DBG("input.conf: ClassicBondedOnly=%s",
+                                       classic_bonded_only ? "true" : "false");
+                       input_set_classic_bonded_only(classic_bonded_only);
+               } else
+                       g_clear_error(&err);
+
        }
 
        btd_profile_register(&input_profile);