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

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