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

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