Blame SOURCES/0002-HID-accepts-bonded-device-connections-only.patch

dd55c8
From b84b23845ec9730b783f4e6efcee70c8b2f09f29 Mon Sep 17 00:00:00 2001
dd55c8
From: Gopal Tiwari <gtiwari@redhat.com>
dd55c8
Date: Fri, 24 Apr 2020 16:27:58 +0530
dd55c8
Subject: [PATCH BlueZ 2/2]     HID accepts bonded device connections only.
dd55c8
dd55c8
commit 3cccdbab2324086588df4ccf5f892fb3ce1f1787
dd55c8
Author: Alain Michaud <alainm@chromium.org>
dd55c8
Date:   Tue Mar 10 02:35:18 2020 +0000
dd55c8
dd55c8
    HID accepts bonded device connections only.
dd55c8
dd55c8
    This change adds a configuration for platforms to choose a more secure
dd55c8
    posture for the HID profile.  While some older mice are known to not
dd55c8
    support pairing or encryption, some platform may choose a more secure
dd55c8
    posture by requiring the device to be bonded  and require the
dd55c8
    connection to be encrypted when bonding is required.
dd55c8
dd55c8
    Reference:
dd55c8
    https://www.intel.com/content/www/us/en/security-center/advisory/intel-sa-00352.html
dd55c8
---
dd55c8
 profiles/input/device.c   | 23 ++++++++++++++++++++++-
dd55c8
 profiles/input/device.h   |  1 +
dd55c8
 profiles/input/input.conf |  8 ++++++++
dd55c8
 profiles/input/manager.c  | 13 ++++++++++++-
dd55c8
 4 files changed, 43 insertions(+), 2 deletions(-)
dd55c8
dd55c8
diff --git a/profiles/input/device.c b/profiles/input/device.c
dd55c8
index 84614784d..3abd2f592 100644
dd55c8
--- a/profiles/input/device.c
dd55c8
+++ b/profiles/input/device.c
dd55c8
@@ -91,6 +91,7 @@ struct input_device {
dd55c8
 
dd55c8
 static int idle_timeout = 0;
dd55c8
 static bool uhid_enabled = false;
dd55c8
+static bool classic_bonded_only = false;
dd55c8
 
dd55c8
 void input_set_idle_timeout(int timeout)
dd55c8
 {
dd55c8
@@ -102,6 +103,11 @@ void input_enable_userspace_hid(bool state)
dd55c8
 	uhid_enabled = state;
dd55c8
 }
dd55c8
 
dd55c8
+void input_set_classic_bonded_only(bool state)
dd55c8
+{
dd55c8
+	classic_bonded_only = state;
dd55c8
+}
dd55c8
+
dd55c8
 static void input_device_enter_reconnect_mode(struct input_device *idev);
dd55c8
 static int connection_disconnect(struct input_device *idev, uint32_t flags);
dd55c8
 
dd55c8
@@ -969,8 +975,18 @@ static int hidp_add_connection(struct input_device *idev)
dd55c8
 	if (device_name_known(idev->device))
dd55c8
 		device_get_name(idev->device, req->name, sizeof(req->name));
dd55c8
 
dd55c8
+	/* Make sure the device is bonded if required */
dd55c8
+	if (classic_bonded_only && !device_is_bonded(idev->device,
dd55c8
+				btd_device_get_bdaddr_type(idev->device))) {
dd55c8
+		error("Rejected connection from !bonded device %s", dst_addr);
dd55c8
+		goto cleanup;
dd55c8
+	}
dd55c8
+
dd55c8
 	/* Encryption is mandatory for keyboards */
dd55c8
-	if (req->subclass & 0x40) {
dd55c8
+	/* Some platforms may choose to require encryption for all devices */
dd55c8
+	/* Note that this only matters for pre 2.1 devices as otherwise the */
dd55c8
+	/* device is encrypted by default by the lower layers */
dd55c8
+	if (classic_bonded_only || req->subclass & 0x40) {
dd55c8
 		if (!bt_io_set(idev->intr_io, &gerr,
dd55c8
 					BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
dd55c8
 					BT_IO_OPT_INVALID)) {
dd55c8
@@ -1202,6 +1218,11 @@ static void input_device_enter_reconnect_mode(struct input_device *idev)
dd55c8
 	DBG("path=%s reconnect_mode=%s", idev->path,
dd55c8
 				reconnect_mode_to_string(idev->reconnect_mode));
dd55c8
 
dd55c8
+	/* Make sure the device is bonded if required */
dd55c8
+	if (classic_bonded_only && !device_is_bonded(idev->device,
dd55c8
+				btd_device_get_bdaddr_type(idev->device)))
dd55c8
+		return;
dd55c8
+
dd55c8
 	/* Only attempt an auto-reconnect when the device is required to
dd55c8
 	 * accept reconnections from the host.
dd55c8
 	 */
dd55c8
diff --git a/profiles/input/device.h b/profiles/input/device.h
dd55c8
index 51a9aee18..3044db673 100644
dd55c8
--- a/profiles/input/device.h
dd55c8
+++ b/profiles/input/device.h
dd55c8
@@ -29,6 +29,7 @@ struct input_conn;
dd55c8
 
dd55c8
 void input_set_idle_timeout(int timeout);
dd55c8
 void input_enable_userspace_hid(bool state);
dd55c8
+void input_set_classic_bonded_only(bool state);
dd55c8
 
dd55c8
 int input_device_register(struct btd_service *service);
dd55c8
 void input_device_unregister(struct btd_service *service);
dd55c8
diff --git a/profiles/input/input.conf b/profiles/input/input.conf
dd55c8
index 3e1d65aae..166aff4a4 100644
dd55c8
--- a/profiles/input/input.conf
dd55c8
+++ b/profiles/input/input.conf
dd55c8
@@ -11,3 +11,11 @@
dd55c8
 # Enable HID protocol handling in userspace input profile
dd55c8
 # Defaults to false (HIDP handled in HIDP kernel module)
dd55c8
 #UserspaceHID=true
dd55c8
+
dd55c8
+# Limit HID connections to bonded devices
dd55c8
+# The HID Profile does not specify that devices must be bonded, however some
dd55c8
+# platforms may want to make sure that input connections only come from bonded
dd55c8
+# device connections. Several older mice have been known for not supporting
dd55c8
+# pairing/encryption.
dd55c8
+# Defaults to false to maximize device compatibility.
dd55c8
+#ClassicBondedOnly=true
dd55c8
diff --git a/profiles/input/manager.c b/profiles/input/manager.c
dd55c8
index 1d31b0652..5cd27b839 100644
dd55c8
--- a/profiles/input/manager.c
dd55c8
+++ b/profiles/input/manager.c
dd55c8
@@ -96,7 +96,7 @@ static int input_init(void)
dd55c8
 	config = load_config_file(CONFIGDIR "/input.conf");
dd55c8
 	if (config) {
dd55c8
 		int idle_timeout;
dd55c8
-		gboolean uhid_enabled;
dd55c8
+		gboolean uhid_enabled, classic_bonded_only;
dd55c8
 
dd55c8
 		idle_timeout = g_key_file_get_integer(config, "General",
dd55c8
 							"IdleTimeout", &err;;
dd55c8
@@ -114,6 +114,17 @@ static int input_init(void)
dd55c8
 			input_enable_userspace_hid(uhid_enabled);
dd55c8
 		} else
dd55c8
 			g_clear_error(&err;;
dd55c8
+
dd55c8
+		classic_bonded_only = g_key_file_get_boolean(config, "General",
dd55c8
+						"ClassicBondedOnly", &err;;
dd55c8
+
dd55c8
+		if (!err) {
dd55c8
+			DBG("input.conf: ClassicBondedOnly=%s",
dd55c8
+					classic_bonded_only ? "true" : "false");
dd55c8
+			input_set_classic_bonded_only(classic_bonded_only);
dd55c8
+		} else
dd55c8
+			g_clear_error(&err;;
dd55c8
+
dd55c8
 	}
dd55c8
 
dd55c8
 	btd_profile_register(&input_profile);
dd55c8
-- 
dd55c8
2.21.1
dd55c8