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

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