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

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